You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "codetyri0n (via GitHub)" <gi...@apache.org> on 2025/11/11 11:08:01 UTC
[PR] feat: Make extract SQL expression timezone aware. [datafusion]
codetyri0n opened a new pull request, #18615:
URL: https://github.com/apache/datafusion/pull/18615
## Which issue does this PR close?
Closes #18228
## What changes are included in this PR?
The PR supports the enabling of timezone as an execution config and also recognises proper and improper timezone offsets (HH:MM) with the timestamp.
## Are these changes tested?
Yes (slt)
## Are there any user-facing changes?
Yes, users will be able to query extract expression with the enablement of the timezone execution config.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Make extract SQL expression timezone aware. [datafusion]
Posted by "codetyri0n (via GitHub)" <gi...@apache.org>.
codetyri0n commented on code in PR #18615:
URL: https://github.com/apache/datafusion/pull/18615#discussion_r2557584814
##########
datafusion/sqllogictest/test_files/extract_tz.slt:
##########
@@ -0,0 +1,158 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Tests for timezone-aware extract SQL statement support.
+# Test with different timezone
+statement ok
+SET datafusion.execution.time_zone = '-03:00';
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-11-18 10:00:00');
+----
+7
+
+query II
+SELECT EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 10:45:30'),
+ EXTRACT(SECOND FROM TIMESTAMP '2023-10-30 10:45:30');
+----
+45 30
+
+query III
+SELECT EXTRACT(YEAR FROM DATE '2023-10-30'),
+ EXTRACT(MONTH FROM DATE '2023-10-30'),
+ EXTRACT(DAY FROM DATE '2023-10-30');
+----
+2023 10 30
+
+query I
+SELECT EXTRACT(HOUR FROM CAST(NULL AS TIMESTAMP));
+----
+NULL
+
+statement ok
+SET datafusion.execution.time_zone = '+04:00';
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 02:00:00');
+----
+6
+
+query III
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 18:20:59'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 18:20:59'),
+ EXTRACT(SECOND FROM TIMESTAMP '2023-10-30 18:20:59');
+----
+22 20 59
+
+query II
+SELECT EXTRACT(DOW FROM DATE '2025-11-01'),
+ EXTRACT(DOY FROM DATE '2026-12-31');
+----
+6 365
+
+statement ok
+SET datafusion.execution.time_zone = '+00:00';
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-10-30 10:45:30+02:00');
+----
+8
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-10-30 10:45:30-05:00');
+----
+15
+
+query II
+SELECT EXTRACT(YEAR FROM TIMESTAMP '2026-11-30 10:45:30Z'),
+ EXTRACT(MONTH FROM TIMESTAMP '2023-10-30 10:45:30Z');
+----
+2026 10
+
+query III
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 18:20:59+04:00'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 18:20:59+04:00'),
+ EXTRACT(SECOND FROM TIMESTAMP '2023-10-30 18:20:59+04:00');
+----
+14 20 59
+
+query II
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-10-30 10:25:30+02:30'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 18:20:59-04:30');
+----
+7 50
+
+query III
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 18:20:59-08:00'),
+ EXTRACT(DAY FROM TIMESTAMP '2023-10-30 18:20:59-07:00'),
+ EXTRACT(DAY FROM TIMESTAMP '2023-10-30 07:20:59+12:00');
+----
+2 31 29
+
+query IIIIII
+SELECT EXTRACT(YEAR FROM TIMESTAMP '2023-12-31 18:20:59-08:45'),
+ EXTRACT(MONTH FROM TIMESTAMP '2023-12-31 18:20:59-08:45'),
+ EXTRACT(DAY FROM TIMESTAMP '2023-12-31 18:20:59-08:45'),
+ EXTRACT(HOUR FROM TIMESTAMP '2023-12-31 18:20:59-08:45'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2023-12-31 18:20:59-08:45'),
+ EXTRACT(SECOND FROM TIMESTAMP '2023-12-31 18:20:59-08:45');
+----
+2024 1 1 3 5 59
+
+query IIIIII
+SELECT EXTRACT(YEAR FROM TIMESTAMP '2024-01-01 03:05:59+08:45'),
+ EXTRACT(MONTH FROM TIMESTAMP '2024-01-01 03:05:59+08:45'),
+ EXTRACT(DAY FROM TIMESTAMP '2024-01-01 03:05:59+08:45'),
+ EXTRACT(HOUR FROM TIMESTAMP '2024-01-01 03:05:59+08:45'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2024-01-01 03:05:59+08:45'),
+ EXTRACT(SECOND FROM TIMESTAMP '2024-01-01 03:05:59+08:45');
+----
+2023 12 31 18 20 59
+
+statement ok
+SET datafusion.execution.time_zone = 'Asia/Kolkata';
Review Comment:
included support for named timezone config
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Make extract SQL expression timezone aware. [datafusion]
Posted by "Omega359 (via GitHub)" <gi...@apache.org>.
Omega359 commented on PR #18615:
URL: https://github.com/apache/datafusion/pull/18615#issuecomment-3528978519
timezone parsing, don't do it manually. Use chrono:
```Rust
use chrono::TimeZone;
use chrono_tz::Tz;
use chrono_tz::UTC;
let tz: Tz = "Antarctica/South_Pole".parse().unwrap();
let dt = tz.ymd(2016, 10, 22).and_hms(12, 0, 0);
let utc = dt.with_timezone(&UTC);
assert_eq!(utc.to_string(), "2016-10-21 23:00:00 UTC");
```
From https://github.com/chronotope/chrono-tz
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Make extract SQL expression timezone aware. [datafusion]
Posted by "codetyri0n (via GitHub)" <gi...@apache.org>.
codetyri0n closed pull request #18615: feat: Make extract SQL expression timezone aware.
URL: https://github.com/apache/datafusion/pull/18615
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Make extract SQL expression timezone aware. [datafusion]
Posted by "codetyri0n (via GitHub)" <gi...@apache.org>.
codetyri0n commented on PR #18615:
URL: https://github.com/apache/datafusion/pull/18615#issuecomment-3518178712
@martin-g @Omega359 This PR is a direct port of #18417 which got really messy (my apologies 🥲) due to a whole lot of unwanted commits. Closed that so that we could have a clean PR.
This contains the commit which addressed @martin-g thoughts for :
1. handling offsets that included tz such as +02:30 and the subsequent test cases.
2. minor error handling suggestions and tweaks.
3. refactor of a common function - implemented in mod.rs as a helper function and correspondingly used in to_local_time and date_part files.
Improvements that could be made for future releases:
1. Named timezone support ('Europe/Brussels' , 'Asia/Beijing' etc) that I was unclear on how to carry forward with.
Lmk wdyt (my apologies again for the messy closure and reopening of a separate PR for this)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Make extract SQL expression timezone aware. [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #18615:
URL: https://github.com/apache/datafusion/pull/18615#discussion_r2518824855
##########
datafusion/sqllogictest/test_files/extract_tz.slt:
##########
@@ -0,0 +1,107 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Tests for timezone-aware extract SQL statement support.
+# Test with different timezone
+statement ok
+SET datafusion.execution.time_zone = '-03:00';
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-11-18 10:00:00');
+----
+7
+
+query II
+SELECT EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 10:45:30'),
+ EXTRACT(SECOND FROM TIMESTAMP '2023-10-30 10:45:30');
+----
+45 30
+
+query III
+SELECT EXTRACT(YEAR FROM DATE '2023-10-30'),
+ EXTRACT(MONTH FROM DATE '2023-10-30'),
+ EXTRACT(DAY FROM DATE '2023-10-30');
+----
+2023 10 30
+
+query I
+SELECT EXTRACT(HOUR FROM CAST(NULL AS TIMESTAMP));
+----
+NULL
+
+statement ok
+SET datafusion.execution.time_zone = '+04:00';
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 02:00:00');
+----
+6
+
+query III
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 18:20:59'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 18:20:59'),
+ EXTRACT(SECOND FROM TIMESTAMP '2023-10-30 18:20:59');
+----
+22 20 59
+
+query II
+SELECT EXTRACT(DOW FROM DATE '2025-11-01'),
+ EXTRACT(DOY FROM DATE '2026-12-31');
+----
+6 365
+
+statement ok
+SET datafusion.execution.time_zone = '+00:00';
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-10-30 10:45:30+02:00');
+----
+8
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-10-30 10:45:30-05:00');
+----
+15
+
+query II
+SELECT EXTRACT(YEAR FROM TIMESTAMP '2026-11-30 10:45:30Z'),
+ EXTRACT(MONTH FROM TIMESTAMP '2023-10-30 10:45:30Z');
+----
+2026 10
+
+query III
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 18:20:59+04:00'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 18:20:59+04:00'),
+ EXTRACT(SECOND FROM TIMESTAMP '2023-10-30 18:20:59+04:00');
+----
+14 20 59
+
+query II
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-10-30 10:25:30+02:30'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 18:20:59-04:30');
+----
+7 50
+
+query III
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 18:20:59-08:00'),
+ EXTRACT(DAY FROM TIMESTAMP '2023-10-30 18:20:59-07:00'),
+ EXTRACT(DAY FROM TIMESTAMP '2023-10-30 07:20:59+12:00');
+----
+2 31 29
Review Comment:
Please add tests for New Year Eve:
```suggestion
2 31 29
query IIII
SELECT EXTRACT(YEAR FROM TIMESTAMP '2023-12-31 18:20:59-08:45'),
EXTRACT(MONTH FROM TIMESTAMP '2023-12-31 18:20:59-08:45'),
EXTRACT(DAY FROM TIMESTAMP '2023-12-31 18:20:59-08:45'),
EXTRACT(HOUR FROM TIMESTAMP '2023-12-31 18:20:59-08:45'),
EXTRACT(HOUR FROM TIMESTAMP '2023-12-31 18:20:59-08:45'),
EXTRACT(SECOND FROM TIMESTAMP '2023-12-31 18:20:59-08:45');
----
2024 01 01 03 05 59
query V
SELECT EXTRACT(YEAR FROM TIMESTAMP '2024-01-01 03:05:59+08:45'),
EXTRACT(MONTH FROM TIMESTAMP '2024-01-01 03:05:59+08:45'),
EXTRACT(DAY FROM TIMESTAMP '2024-01-01 03:05:59+08:45'),
EXTRACT(HOUR FROM TIMESTAMP '2024-01-01 03:05:59+08:45'),
EXTRACT(HOUR FROM TIMESTAMP '2024-01-01 03:05:59+08:45'),
EXTRACT(SECOND FROM TIMESTAMP '2024-01-01 03:05:59+08:45');
----
2023 12 31 18 20 59
```
I hope I got them right! :-)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Make extract SQL expression timezone aware. [datafusion]
Posted by "codetyri0n (via GitHub)" <gi...@apache.org>.
codetyri0n commented on code in PR #18615:
URL: https://github.com/apache/datafusion/pull/18615#discussion_r2518994793
##########
datafusion/sqllogictest/test_files/extract_tz.slt:
##########
@@ -0,0 +1,107 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Tests for timezone-aware extract SQL statement support.
+# Test with different timezone
+statement ok
+SET datafusion.execution.time_zone = '-03:00';
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-11-18 10:00:00');
+----
+7
+
+query II
+SELECT EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 10:45:30'),
+ EXTRACT(SECOND FROM TIMESTAMP '2023-10-30 10:45:30');
+----
+45 30
+
+query III
+SELECT EXTRACT(YEAR FROM DATE '2023-10-30'),
+ EXTRACT(MONTH FROM DATE '2023-10-30'),
+ EXTRACT(DAY FROM DATE '2023-10-30');
+----
+2023 10 30
+
+query I
+SELECT EXTRACT(HOUR FROM CAST(NULL AS TIMESTAMP));
+----
+NULL
+
+statement ok
+SET datafusion.execution.time_zone = '+04:00';
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 02:00:00');
+----
+6
+
+query III
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 18:20:59'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 18:20:59'),
+ EXTRACT(SECOND FROM TIMESTAMP '2023-10-30 18:20:59');
+----
+22 20 59
+
+query II
+SELECT EXTRACT(DOW FROM DATE '2025-11-01'),
+ EXTRACT(DOY FROM DATE '2026-12-31');
+----
+6 365
+
+statement ok
+SET datafusion.execution.time_zone = '+00:00';
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-10-30 10:45:30+02:00');
+----
+8
+
+query I
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-10-30 10:45:30-05:00');
+----
+15
+
+query II
+SELECT EXTRACT(YEAR FROM TIMESTAMP '2026-11-30 10:45:30Z'),
+ EXTRACT(MONTH FROM TIMESTAMP '2023-10-30 10:45:30Z');
+----
+2026 10
+
+query III
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 18:20:59+04:00'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 18:20:59+04:00'),
+ EXTRACT(SECOND FROM TIMESTAMP '2023-10-30 18:20:59+04:00');
+----
+14 20 59
+
+query II
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2025-10-30 10:25:30+02:30'),
+ EXTRACT(MINUTE FROM TIMESTAMP '2023-10-30 18:20:59-04:30');
+----
+7 50
+
+query III
+SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-10-30 18:20:59-08:00'),
+ EXTRACT(DAY FROM TIMESTAMP '2023-10-30 18:20:59-07:00'),
+ EXTRACT(DAY FROM TIMESTAMP '2023-10-30 07:20:59+12:00');
+----
+2 31 29
Review Comment:
thanks for adding this scenario! I have committed with minor tweaks with the right test case format for slt : )
(the characters after 'query' tells us the type and rows returned)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Make extract SQL expression timezone aware. [datafusion]
Posted by "codetyri0n (via GitHub)" <gi...@apache.org>.
codetyri0n commented on code in PR #18615:
URL: https://github.com/apache/datafusion/pull/18615#discussion_r2557591802
##########
datafusion/functions/src/datetime/mod.rs:
##########
@@ -37,6 +44,55 @@ pub mod to_local_time;
pub mod to_timestamp;
pub mod to_unixtime;
+// Adjusts a timestamp to local time by applying the timezone offset.
Review Comment:
refactored adjust_to_local_time fn that is used in both to_local_time.rs and date_part.rs as a helper fn in mod.rs
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org