Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add builtin function timezone #19524

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions e2e_test/batch/functions/timezone.slt.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
statement ok
SET TimeZone to 'UTC';

query T
SELECT timezone('Europe/Moscow', '2011-03-27 02:00:00'::timestamp);
----
2011-03-26 23:00:00+00:00

query T
SELECT timezone('Europe/Moscow', '2011-03-27 02:59:59'::timestamp);
----
2011-03-26 23:59:59+00:00

query T
SELECT timezone('Europe/Moscow', '2011-03-27 03:00:00'::timestamp);
----
2011-03-26 23:00:00+00:00

query T
SELECT timezone('Europe/Moscow', '2014-10-26 00:59:59'::timestamp);
----
2014-10-25 20:59:59+00:00

query T
SELECT timezone('Europe/Moscow', '2014-10-26 01:00:00'::timestamp);
----
2014-10-25 22:00:00+00:00

query T
SELECT timezone('Europe/Moscow', '2014-10-26 01:00:01'::timestamp);
----
2014-10-25 22:00:01+00:00

query T
SELECT timezone('Asia/Shanghai', '2024-11-20 20:00:00 UTC'::timestamptz);
----
2024-11-21 04:00:00

query T
SELECT timezone('Asia/Shanghai', '2023-02-28 20:00:00 UTC'::timestamptz);
----
2023-03-01 04:00:00

query T
SELECT timezone('Asia/Shanghai', '2024-02-28 20:00:00 UTC'::timestamptz);
----
2024-02-29 04:00:00

query T
SELECT timezone('Europe/Moscow', '2011-03-26 22:00:00 UTC'::timestamptz);
----
2011-03-27 01:00:00

query T
SELECT timezone('Europe/Moscow', '2011-03-26 23:00:00 UTC'::timestamptz);
----
2011-03-27 03:00:00

query T
SELECT timezone('Europe/Moscow', '2011-03-26 23:00:01 UTC'::timestamptz);
----
2011-03-27 03:00:01

query T
SELECT timezone('Europe/Moscow', '2011-03-27 01:00:00 UTC'::timestamptz);
----
2011-03-27 05:00:00

query T
SELECT timezone('Europe/Moscow', '2011-03-27 02:00:00 UTC'::timestamptz);
----
2011-03-27 06:00:00

query T
SELECT timezone('Europe/Moscow', '2011-03-27 02:59:59 UTC'::timestamptz);
----
2011-03-27 06:59:59

query T
SELECT timezone('Europe/Moscow', '2011-03-27 03:00:00 UTC'::timestamptz);
----
2011-03-27 07:00:00

query T
SELECT timezone('Europe/Moscow', '2014-10-25 21:59:59 UTC'::timestamptz);
----
2014-10-26 01:59:59

query T
SELECT timezone('Europe/Moscow', '2014-10-25 22:00:00 UTC'::timestamptz);
----
2014-10-26 01:00:00

query T
SELECT timezone('Europe/Moscow', '2014-10-25 22:59:59 UTC'::timestamptz);
----
2014-10-26 01:59:59

query T
SELECT timezone('Europe/Moscow', '2014-10-25 23:00:00 UTC'::timestamptz);
----
2014-10-26 02:00:00

query T
SELECT timezone('Europe/Moscow', '2014-10-25 23:00:01 UTC'::timestamptz);
----
2014-10-26 02:00:01

query T
SELECT timezone('Europe/Moscow', '2014-10-26 02:00:01 UTC'::timestamptz);
----
2014-10-26 05:00:01
16 changes: 8 additions & 8 deletions src/expr/impl/src/scalar/timestamptz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ pub fn f64_sec_to_timestamptz(elem: F64) -> Result<Timestamptz> {
Ok(Timestamptz::from_micros(micros))
}

#[function("at_time_zone(timestamptz, varchar) -> timestamp")]
pub fn timestamptz_at_time_zone(input: Timestamptz, time_zone: &str) -> Result<Timestamp> {
let time_zone = Timestamptz::lookup_time_zone(time_zone).map_err(time_zone_err)?;
let instant_local = input.to_datetime_in_zone(time_zone);
let naive = instant_local.naive_local();
Ok(Timestamp(naive))
}

#[function("at_time_zone(timestamp, varchar) -> timestamptz")]
pub fn timestamp_at_time_zone(input: Timestamp, time_zone: &str) -> Result<Timestamptz> {
let time_zone = Timestamptz::lookup_time_zone(time_zone).map_err(time_zone_err)?;
Expand Down Expand Up @@ -96,14 +104,6 @@ pub fn str_to_timestamptz(elem: &str, time_zone: &str) -> Result<Timestamptz> {
})
}

#[function("at_time_zone(timestamptz, varchar) -> timestamp")]
pub fn timestamptz_at_time_zone(input: Timestamptz, time_zone: &str) -> Result<Timestamp> {
let time_zone = Timestamptz::lookup_time_zone(time_zone).map_err(time_zone_err)?;
let instant_local = input.to_datetime_in_zone(time_zone);
let naive = instant_local.naive_local();
Ok(Timestamp(naive))
}

/// This operation is zone agnostic.
#[function("subtract(timestamptz, timestamptz) -> interval")]
pub fn timestamptz_timestamptz_sub(l: Timestamptz, r: Timestamptz) -> Result<Interval> {
Expand Down
12 changes: 10 additions & 2 deletions src/frontend/src/binder/expr/function/builtin_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl Binder {
("scale", raw_call(ExprType::Scale)),
("min_scale", raw_call(ExprType::MinScale)),
("trim_scale", raw_call(ExprType::TrimScale)),

// date and time
(
"to_timestamp",
dispatch_by_len(vec![
Expand All @@ -223,8 +223,16 @@ impl Binder {
("make_date", raw_call(ExprType::MakeDate)),
("make_time", raw_call(ExprType::MakeTime)),
("make_timestamp", raw_call(ExprType::MakeTimestamp)),
("to_date", raw_call(ExprType::CharToDate)),
("make_timestamptz", raw_call(ExprType::MakeTimestamptz)),
("timezone", rewrite(ExprType::AtTimeZone, |mut inputs|{
if inputs.len() == 2 {
inputs.swap(0, 1);
Ok(inputs)
} else {
Err(ErrorCode::ExprError("unexpected arguments number".into()).into())
}
})),
("to_date", raw_call(ExprType::CharToDate)),
// string
("substr", raw_call(ExprType::Substr)),
("length", raw_call(ExprType::Length)),
Expand Down
2 changes: 1 addition & 1 deletion src/tests/regress/data/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ test: strings date time timestamp interval
test: case arrays delete
test: jsonb jsonb_jsonpath
test: regex
test: contrib-pgcrypto-rijndael
test: contrib-pgcrypto-rijndael
Loading