diff --git a/src/expr/impl/src/scalar/timestamptz.rs b/src/expr/impl/src/scalar/timestamptz.rs index 0855ef086f6b..75a53f8a4b3c 100644 --- a/src/expr/impl/src/scalar/timestamptz.rs +++ b/src/expr/impl/src/scalar/timestamptz.rs @@ -80,10 +80,9 @@ pub fn timestamp_at_time_zone(input: Timestamp, time_zone: &str) -> Result timestamp")] -pub fn timezone_timestamp_at_time_zone(time_zone: &str, input: Timestamp) -> Result { +#[function("timezone(varchar, timestamp) -> timestamptz")] +pub fn timezone_timestamp_at_time_zone(time_zone: &str, input: Timestamp) -> Result { timestamp_at_time_zone(input, time_zone) - .map(|timestamptz| timestamptz.to_datetime_utc().naive_utc().into()) } #[function("timezone(varchar, timestamptz) -> timestamp")] diff --git a/src/tests/regress/data/expected/timestamptz.out b/src/tests/regress/data/expected/timestamptz.out index 29fc1c4b089c..4677335daebb 100644 --- a/src/tests/regress/data/expected/timestamptz.out +++ b/src/tests/regress/data/expected/timestamptz.out @@ -2636,191 +2636,76 @@ SELECT '2014-10-26 02:00:00'::timestamp AT TIME ZONE 'MSK'; 2014-10-25 23:00:00+00 (1 row) -SELECT timezone('Asia/Shanghai', '2024-11-20 20:00:00 UTC'::timestamptz); - timezone ---------------------- - 2024-11-21 04:00:00 -(1 row) - -SELECT timezone('Asia/Shanghai', '2023-02-28 20:00:00 UTC'::timestamptz); - timezone ---------------------- - 2023-03-01 04:00:00 -(1 row) - -SELECT timezone('Asia/Shanghai', '2024-02-28 20:00:00 UTC'::timestamptz); - timezone ---------------------- - 2024-02-29 04:00:00 -(1 row) - -SELECT timezone('Europe/Moscow', '2011-03-26 22:00:00 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 01:00:00 -(1 row) - -SELECT timezone('Europe/Moscow', '2011-03-26 22:59:59 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 01:59:59 -(1 row) - -SELECT timezone('Europe/Moscow', '2011-03-26 23:00:00 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 03:00:00 -(1 row) - -SELECT timezone('Europe/Moscow', '2011-03-26 23:00:01 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 03:00:01 -(1 row) - -SELECT timezone('Europe/Moscow', '2011-03-27 01:00:00 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 05:00:00 -(1 row) - -SELECT timezone('Europe/Moscow', '2011-03-27 02:00:00 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 06:00:00 -(1 row) - -SELECT timezone('Europe/Moscow', '2011-03-27 02:59:59 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 06:59:59 -(1 row) - -SELECT timezone('Europe/Moscow', '2011-03-27 03:00:00 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 07:00:00 -(1 row) - -SELECT timezone('MSK', '2011-03-26 22:00:00 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 01:00:00 -(1 row) - -SELECT timezone('MSK', '2011-03-26 22:59:59 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 01:59:59 -(1 row) - -SELECT timezone('MSK', '2011-03-26 23:00:00 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 03:00:00 -(1 row) - -SELECT timezone('MSK', '2011-03-26 23:00:01 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 03:00:01 -(1 row) - -SELECT timezone('MSK', '2011-03-27 01:00:00 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 05:00:00 -(1 row) - -SELECT timezone('MSK', '2011-03-27 02:00:00 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 06:00:00 -(1 row) - -SELECT timezone('MSK', '2011-03-27 02:59:59 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 06:59:59 -(1 row) - -SELECT timezone('MSK', '2011-03-27 03:00:00 UTC'::timestamptz); - timezone ---------------------- - 2011-03-27 07:00:00 -(1 row) - - -SELECT timezone('Europe/Moscow', '2014-10-25 21:59:59 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 01:59:59 +SELECT timezone('Europe/Moscow', '2011-03-27 02:00:00'::timestamp); + timezone +--------------------------- + 2011-03-26 23:00:00+00 (1 row) -SELECT timezone('Europe/Moscow', '2014-10-25 22:00:00 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 01:00:00 +SELECT timezone('Europe/Moscow', '2011-03-27 02:59:59'::timestamp); + timezone +--------------------------- + 2011-03-26 23:59:59+00 (1 row) -SELECT timezone('Europe/Moscow', '2014-10-25 22:59:59 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 01:59:59 +SELECT timezone('Europe/Moscow', '2011-03-27 03:00:00'::timestamp); + timezone +--------------------------- + 2011-03-26 23:00:00+00 (1 row) -SELECT timezone('Europe/Moscow', '2014-10-25 23:00:00 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 02:00:00 +SELECT timezone('MSK', '2011-03-27 02:00:00'::timestamp); + timezone +------------------------ + 2011-03-26 22:00:00+00 (1 row) -SELECT timezone('Europe/Moscow', '2014-10-25 23:00:01 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 02:00:01 +SELECT timezone('MSK', '2011-03-27 02:59:59'::timestamp); + timezone +------------------------ + 2011-03-26 22:59:59+00 (1 row) -SELECT timezone('Europe/Moscow', '2014-10-26 02:00:01 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 05:00:01 +SELECT timezone('MSK', '2011-03-27 03:00:00'::timestamp); + timezone +------------------------ + 2011-03-26 23:00:00+00 (1 row) -SELECT timezone('MSK', '2014-10-25 21:59:59 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 01:59:59 +SELECT timezone('Europe/Moscow', '2014-10-26 00:59:59'::timestamp); + timezone +--------------------------- + 2014-10-25 20:59:59+00 (1 row) -SELECT timezone('MSK', '2014-10-25 22:00:00 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 01:00:00 +SELECT timezone('Europe/Moscow', '2014-10-26 01:00:00'::timestamp); + timezone +--------------------------- + 2014-10-25 22:00:00+00 (1 row) -SELECT timezone('MSK', '2014-10-25 22:59:59 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 01:59:59 +SELECT timezone('Europe/Moscow', '2014-10-26 01:00:01'::timestamp); + timezone +--------------------------- + 2014-10-25 22:00:01+00 (1 row) -SELECT timezone('MSK', '2014-10-25 23:00:00 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 02:00:00 +SELECT timezone('MSK', '2014-10-26 00:59:59'::timestamp); + timezone +------------------------ + 2014-10-25 20:59:59+00 (1 row) -SELECT timezone('MSK', '2014-10-25 23:00:01 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 02:00:01 +SELECT timezone('MSK', '2014-10-26 01:00:00'::timestamp); + timezone +------------------------ + 2014-10-25 22:00:00+00 (1 row) -SELECT timezone('MSK', '2014-10-26 02:00:01 UTC'::timestamptz); - timezone ---------------------- - 2014-10-26 05:00:01 +SELECT timezone('MSK', '2014-10-26 01:00:01'::timestamp); + timezone +------------------------ + 2014-10-25 22:00:01+00 (1 row) SELECT make_timestamptz(2014, 10, 26, 0, 0, 0, 'MSK'); @@ -3094,6 +2979,193 @@ SELECT '2014-10-25 23:00:00 UTC'::timestamptz AT TIME ZONE 'MSK'; 2014-10-26 02:00:00 (1 row) +SELECT timezone('Asia/Shanghai', '2024-11-20 20:00:00 UTC'::timestamptz); + timezone +--------------------- + 2024-11-21 04:00:00 +(1 row) + +SELECT timezone('Asia/Shanghai', '2023-02-28 20:00:00 UTC'::timestamptz); + timezone +--------------------- + 2023-03-01 04:00:00 +(1 row) + +SELECT timezone('Asia/Shanghai', '2024-02-28 20:00:00 UTC'::timestamptz); + timezone +--------------------- + 2024-02-29 04:00:00 +(1 row) + +SELECT timezone('Europe/Moscow', '2011-03-26 22:00:00 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 01:00:00 +(1 row) + +SELECT timezone('Europe/Moscow', '2011-03-26 22:59:59 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 01:59:59 +(1 row) + +SELECT timezone('Europe/Moscow', '2011-03-26 23:00:00 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 03:00:00 +(1 row) + +SELECT timezone('Europe/Moscow', '2011-03-26 23:00:01 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 03:00:01 +(1 row) + +SELECT timezone('Europe/Moscow', '2011-03-27 01:00:00 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 05:00:00 +(1 row) + +SELECT timezone('Europe/Moscow', '2011-03-27 02:00:00 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 06:00:00 +(1 row) + +SELECT timezone('Europe/Moscow', '2011-03-27 02:59:59 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 06:59:59 +(1 row) + +SELECT timezone('Europe/Moscow', '2011-03-27 03:00:00 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 07:00:00 +(1 row) + +SELECT timezone('MSK', '2011-03-26 22:00:00 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 01:00:00 +(1 row) + +SELECT timezone('MSK', '2011-03-26 22:59:59 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 01:59:59 +(1 row) + +SELECT timezone('MSK', '2011-03-26 23:00:00 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 03:00:00 +(1 row) + +SELECT timezone('MSK', '2011-03-26 23:00:01 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 03:00:01 +(1 row) + +SELECT timezone('MSK', '2011-03-27 01:00:00 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 05:00:00 +(1 row) + +SELECT timezone('MSK', '2011-03-27 02:00:00 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 06:00:00 +(1 row) + +SELECT timezone('MSK', '2011-03-27 02:59:59 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 06:59:59 +(1 row) + +SELECT timezone('MSK', '2011-03-27 03:00:00 UTC'::timestamptz); + timezone +--------------------- + 2011-03-27 07:00:00 +(1 row) + + +SELECT timezone('Europe/Moscow', '2014-10-25 21:59:59 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 01:59:59 +(1 row) + +SELECT timezone('Europe/Moscow', '2014-10-25 22:00:00 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 01:00:00 +(1 row) + +SELECT timezone('Europe/Moscow', '2014-10-25 22:59:59 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 01:59:59 +(1 row) + +SELECT timezone('Europe/Moscow', '2014-10-25 23:00:00 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 02:00:00 +(1 row) + +SELECT timezone('Europe/Moscow', '2014-10-25 23:00:01 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 02:00:01 +(1 row) + +SELECT timezone('Europe/Moscow', '2014-10-26 02:00:01 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 05:00:01 +(1 row) + +SELECT timezone('MSK', '2014-10-25 21:59:59 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 01:59:59 +(1 row) + +SELECT timezone('MSK', '2014-10-25 22:00:00 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 01:00:00 +(1 row) + +SELECT timezone('MSK', '2014-10-25 22:59:59 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 01:59:59 +(1 row) + +SELECT timezone('MSK', '2014-10-25 23:00:00 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 02:00:00 +(1 row) + +SELECT timezone('MSK', '2014-10-25 23:00:01 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 02:00:01 +(1 row) + +SELECT timezone('MSK', '2014-10-26 02:00:01 UTC'::timestamptz); + timezone +--------------------- + 2014-10-26 05:00:01 +(1 row) + -- -- Test that AT TIME ZONE isn't misoptimized when using an index (bug #14504) -- diff --git a/src/tests/regress/data/sql/timestamptz.sql b/src/tests/regress/data/sql/timestamptz.sql index 296e5021a849..c1b34a3503a1 100644 --- a/src/tests/regress/data/sql/timestamptz.sql +++ b/src/tests/regress/data/sql/timestamptz.sql @@ -480,41 +480,21 @@ SELECT '2014-10-26 01:00:00'::timestamp AT TIME ZONE 'MSK'; SELECT '2014-10-26 01:00:01'::timestamp AT TIME ZONE 'MSK'; SELECT '2014-10-26 02:00:00'::timestamp AT TIME ZONE 'MSK'; -SELECT timezone('Asia/Shanghai', '2024-11-20 20:00:00 UTC'::timestamptz); -SELECT timezone('Asia/Shanghai', '2023-02-28 20:00:00 UTC'::timestamptz); -SELECT timezone('Asia/Shanghai', '2024-02-28 20:00:00 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2011-03-27 02:00:00'::timestamp); +SELECT timezone('Europe/Moscow', '2011-03-27 02:59:59'::timestamp); +SELECT timezone('Europe/Moscow', '2011-03-27 03:00:00'::timestamp); -SELECT timezone('Europe/Moscow', '2011-03-26 22:00:00 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2011-03-26 22:59:59 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2011-03-26 23:00:00 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2011-03-26 23:00:01 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2011-03-27 01:00:00 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2011-03-27 02:00:00 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2011-03-27 02:59:59 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2011-03-27 03:00:00 UTC'::timestamptz); - -SELECT timezone('MSK', '2011-03-26 22:00:00 UTC'::timestamptz); -SELECT timezone('MSK', '2011-03-26 22:59:59 UTC'::timestamptz); -SELECT timezone('MSK', '2011-03-26 23:00:00 UTC'::timestamptz); -SELECT timezone('MSK', '2011-03-26 23:00:01 UTC'::timestamptz); -SELECT timezone('MSK', '2011-03-27 01:00:00 UTC'::timestamptz); -SELECT timezone('MSK', '2011-03-27 02:00:00 UTC'::timestamptz); -SELECT timezone('MSK', '2011-03-27 02:59:59 UTC'::timestamptz); -SELECT timezone('MSK', '2011-03-27 03:00:00 UTC'::timestamptz); +SELECT timezone('MSK', '2011-03-27 02:00:00'::timestamp); +SELECT timezone('MSK', '2011-03-27 02:59:59'::timestamp); +SELECT timezone('MSK', '2011-03-27 03:00:00'::timestamp); -SELECT timezone('Europe/Moscow', '2014-10-25 21:59:59 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2014-10-25 22:00:00 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2014-10-25 22:59:59 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2014-10-25 23:00:00 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2014-10-25 23:00:01 UTC'::timestamptz); -SELECT timezone('Europe/Moscow', '2014-10-26 02:00:01 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2014-10-26 00:59:59'::timestamp); +SELECT timezone('Europe/Moscow', '2014-10-26 01:00:00'::timestamp); +SELECT timezone('Europe/Moscow', '2014-10-26 01:00:01'::timestamp); -SELECT timezone('MSK', '2014-10-25 21:59:59 UTC'::timestamptz); -SELECT timezone('MSK', '2014-10-25 22:00:00 UTC'::timestamptz); -SELECT timezone('MSK', '2014-10-25 22:59:59 UTC'::timestamptz); -SELECT timezone('MSK', '2014-10-25 23:00:00 UTC'::timestamptz); -SELECT timezone('MSK', '2014-10-25 23:00:01 UTC'::timestamptz); -SELECT timezone('MSK', '2014-10-26 02:00:01 UTC'::timestamptz); +SELECT timezone('MSK', '2014-10-26 00:59:59'::timestamp); +SELECT timezone('MSK', '2014-10-26 01:00:00'::timestamp); +SELECT timezone('MSK', '2014-10-26 01:00:01'::timestamp); SELECT make_timestamptz(2014, 10, 26, 0, 0, 0, 'MSK'); SELECT make_timestamptz(2014, 10, 26, 1, 0, 0, 'MSK'); @@ -577,6 +557,42 @@ SELECT '2014-10-25 22:00:00 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT '2014-10-25 22:00:01 UTC'::timestamptz AT TIME ZONE 'MSK'; SELECT '2014-10-25 23:00:00 UTC'::timestamptz AT TIME ZONE 'MSK'; +SELECT timezone('Asia/Shanghai', '2024-11-20 20:00:00 UTC'::timestamptz); +SELECT timezone('Asia/Shanghai', '2023-02-28 20:00:00 UTC'::timestamptz); +SELECT timezone('Asia/Shanghai', '2024-02-28 20:00:00 UTC'::timestamptz); + +SELECT timezone('Europe/Moscow', '2011-03-26 22:00:00 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2011-03-26 22:59:59 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2011-03-26 23:00:00 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2011-03-26 23:00:01 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2011-03-27 01:00:00 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2011-03-27 02:00:00 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2011-03-27 02:59:59 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2011-03-27 03:00:00 UTC'::timestamptz); + +SELECT timezone('MSK', '2011-03-26 22:00:00 UTC'::timestamptz); +SELECT timezone('MSK', '2011-03-26 22:59:59 UTC'::timestamptz); +SELECT timezone('MSK', '2011-03-26 23:00:00 UTC'::timestamptz); +SELECT timezone('MSK', '2011-03-26 23:00:01 UTC'::timestamptz); +SELECT timezone('MSK', '2011-03-27 01:00:00 UTC'::timestamptz); +SELECT timezone('MSK', '2011-03-27 02:00:00 UTC'::timestamptz); +SELECT timezone('MSK', '2011-03-27 02:59:59 UTC'::timestamptz); +SELECT timezone('MSK', '2011-03-27 03:00:00 UTC'::timestamptz); + +SELECT timezone('Europe/Moscow', '2014-10-25 21:59:59 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2014-10-25 22:00:00 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2014-10-25 22:59:59 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2014-10-25 23:00:00 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2014-10-25 23:00:01 UTC'::timestamptz); +SELECT timezone('Europe/Moscow', '2014-10-26 02:00:01 UTC'::timestamptz); + +SELECT timezone('MSK', '2014-10-25 21:59:59 UTC'::timestamptz); +SELECT timezone('MSK', '2014-10-25 22:00:00 UTC'::timestamptz); +SELECT timezone('MSK', '2014-10-25 22:59:59 UTC'::timestamptz); +SELECT timezone('MSK', '2014-10-25 23:00:00 UTC'::timestamptz); +SELECT timezone('MSK', '2014-10-25 23:00:01 UTC'::timestamptz); +SELECT timezone('MSK', '2014-10-26 02:00:01 UTC'::timestamptz); + -- -- Test that AT TIME ZONE isn't misoptimized when using an index (bug #14504) --