Skip to content

Commit

Permalink
fix make_timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
KeXiangWang committed Jan 31, 2024
1 parent 18b9965 commit c8cdc3f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
22 changes: 19 additions & 3 deletions e2e_test/batch/basic/make_time.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ SELECT make_timestamptz(1973, 07, 15, 08, 15, 55.33);
query T
SELECT make_timestamptz(-1973, 07, 15, 08, 15, 55.33);
----
-1973-07-15 08:15:55.330+00:00
-1972-07-15 08:15:55.330+00:00

query error Invalid parameter year, month, day: invalid date: -3-2-29
SELECT make_timestamptz(-4, 02, 29, 08, 15, 55.33);

query T
SELECT make_timestamptz(-5, 02, 29, 08, 15, 55.33);
----
-0004-02-29 08:15:55.330+00:00

query error Invalid parameter sec: invalid sec: -55.33
SELECT make_timestamptz(1973, 07, 15, 08, 15, -55.33);
Expand Down Expand Up @@ -102,7 +110,7 @@ SELECT make_date(-2024, 1, 26);
----
2024-01-26 BC

query error Invalid parameter year, month, day: invalid date: -4-2-29
query error Invalid parameter year, month, day: invalid date: -3-2-29
SELECT make_date(-4, 2, 29);

query T
Expand Down Expand Up @@ -141,4 +149,12 @@ SELECT make_timestamp(2024, 1, 26, 14, 20, 26);
query T
SELECT make_timestamp(-1973, 07, 15, 08, 15, 55.33);
----
-1973-07-15 08:15:55.330
-1972-07-15 08:15:55.330

query error Invalid parameter year, month, day: invalid date: -3-2-29
SELECT make_timestamp(-4, 02, 29, 08, 15, 55.33);

query T
SELECT make_timestamp(-5, 02, 29, 08, 15, 55.33);
----
-0004-02-29 08:15:55.330
27 changes: 11 additions & 16 deletions src/expr/impl/src/scalar/make_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,19 @@ use risingwave_expr::{capture_context, function, ExprError, Result};

use crate::scalar::timestamptz::timestamp_at_time_zone;

pub fn make_naive_date(year: i32, month: i32, day: i32, for_timestamp: bool) -> Result<NaiveDate> {
pub fn make_naive_date(mut year: i32, month: i32, day: i32) -> Result<NaiveDate> {
if year == 0 {
return Err(ExprError::InvalidParam {
name: "year, month, day",
reason: format!("invalid date: {}-{}-{}", year, month, day).into(),
});
}
let adjusted_year = if year < 0 && !for_timestamp {
// For a year `-X`, The `Date` type is printed as `X+1 BC`, while `Timestamp` is printed as `-X`.
year + 1
} else {
year
};
NaiveDate::from_ymd_opt(adjusted_year, month as u32, day as u32).ok_or_else(|| {
ExprError::InvalidParam {
name: "year, month, day",
reason: format!("invalid date: {}-{}-{}", year, month, day).into(),
}
if year < 0 {
year += 1
}
NaiveDate::from_ymd_opt(year, month as u32, day as u32).ok_or_else(|| ExprError::InvalidParam {
name: "year, month, day",
reason: format!("invalid date: {}-{}-{}", year, month, day).into(),
})
}

Expand All @@ -60,7 +55,7 @@ fn make_naive_time(hour: i32, min: i32, sec: F64) -> Result<NaiveTime> {
// year int, month int, day int
#[function("make_date(int4, int4, int4) -> date")]
pub fn make_date(year: i32, month: i32, day: i32) -> Result<Date> {
Ok(Date(make_naive_date(year, month, day, false)?))
Ok(Date(make_naive_date(year, month, day)?))
}

// hour int, min int, sec double precision
Expand All @@ -80,7 +75,7 @@ pub fn make_timestamp(
sec: F64,
) -> Result<Timestamp> {
Ok(Timestamp(NaiveDateTime::new(
make_naive_date(year, month, day, true)?,
make_naive_date(year, month, day)?,
make_naive_time(hour, min, sec)?,
)))
}
Expand Down Expand Up @@ -123,7 +118,7 @@ fn make_timestamptz_impl(
sec: F64,
) -> Result<Timestamptz> {
let naive_date_time = NaiveDateTime::new(
make_naive_date(year, month, day, true)?,
make_naive_date(year, month, day)?,
make_naive_time(hour, min, sec)?,
);
timestamp_at_time_zone(Timestamp(naive_date_time), time_zone)
Expand All @@ -135,7 +130,7 @@ mod tests {
use risingwave_common::types::{Date, Timestamp};

/// This test is to testify that our `Date` expressess a year `-X` as `X+1 BC`, while `Timestamp` expresses it as `-X`.
/// Can be removed if we change the implementation of `Date`.
/// Can be removed if we change the `ToText` implementation of `Date` or `Timestamp`.
#[test]
fn test_naive_date_and_time() {
let year = -1973;
Expand Down

0 comments on commit c8cdc3f

Please sign in to comment.