Apache DataFusion SQL Query Engine
Feat: Make current_time aware of execution timezone. (#18040)
Sriram Sundar37aad28424da
diff --git a/datafusion/functions/src/datetime/current_time.rs b/datafusion/functions/src/datetime/current_time.rsindex 79d5bfc178..b1592d8df2 100644--- a/datafusion/functions/src/datetime/current_time.rs+++ b/datafusion/functions/src/datetime/current_time.rs@@ -15,24 +15,28 @@// specific language governing permissions and limitations// under the License.+use arrow::array::timezone::Tz;use arrow::datatypes::DataType;use arrow::datatypes::DataType::Time64;use arrow::datatypes::TimeUnit::Nanosecond;-use std::any::Any;-+use chrono::TimeZone;+use chrono::Timelike;use datafusion_common::{internal_err, Result, ScalarValue};use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo};use datafusion_expr::{ColumnarValue, Documentation, Expr, ScalarUDFImpl, Signature, Volatility,};use datafusion_macros::user_doc;+use std::any::Any;#[user_doc(doc_section(label = "Time and Date Functions"),description = r#"-Returns the current UTC time.+Returns the current time in the session time zone.The `current_time()` return value is determined at query time and will return the same time, no matter when in the query plan the function executes.++The session time zone can be set using the statement 'SET datafusion.execution.time_zone = desired time zone'. The time zone can be a value like +00:00, 'Europe/London' etc."#,syntax_example = "current_time()")]@@ -93,7 +97,20 @@ impl ScalarUDFImpl for CurrentTimeFunc {info: &dyn SimplifyInfo,) -> Result<ExprSimplifyResult> {let now_ts = info.execution_props().query_execution_start_time;- let nano = now_ts.timestamp_nanos_opt().map(|ts| ts % 86400000000000);++ // Try to get timezone from config and convert to local time+ let nano = info+ .execution_props()+ .config_options()+ .and_then(|config| config.execution.time_zone.parse::<Tz>().ok())+ .map_or_else(+ || datetime_to_time_nanos(&now_ts),+ |tz| {+ let local_now = tz.from_utc_datetime(&now_ts.naive_utc());+ datetime_to_time_nanos(&local_now)+ },+ );+Ok(ExprSimplifyResult::Simplified(Expr::Literal(ScalarValue::Time64Nanosecond(nano),None,@@ -104,3 +121,97 @@ impl ScalarUDFImpl for CurrentTimeFunc {self.doc()}}++// Helper function for conversion of datetime to a timestamp.+fn datetime_to_time_nanos<Tz: TimeZone>(dt: &chrono::DateTime<Tz>) -> Option<i64> {+ let hour = dt.hour() as i64;+ let minute = dt.minute() as i64;+ let second = dt.second() as i64;+ let nanosecond = dt.nanosecond() as i64;+ Some((hour * 3600 + minute * 60 + second) * 1_000_000_000 + nanosecond)+}++#[cfg(test)]+mod tests {+ use super::*;+ use arrow::datatypes::{DataType, TimeUnit::Nanosecond};+ use chrono::{DateTime, Utc};+ use datafusion_common::{Result, ScalarValue};+ use datafusion_expr::execution_props::ExecutionProps;+ use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo};+ use std::sync::Arc;++ struct MockSimplifyInfo {+ execution_props: ExecutionProps,+ }++ impl SimplifyInfo for MockSimplifyInfo {+ fn is_boolean_type(&self, _expr: &Expr) -> Result<bool> {+ Ok(false)+ }++ fn nullable(&self, _expr: &Expr) -> Result<bool> {+ Ok(true)+ }++ fn execution_props(&self) -> &ExecutionProps {+ &self.execution_props+ }++ fn get_data_type(&self, _expr: &Expr) -> Result<DataType> {+ Ok(Time64(Nanosecond))+ }+ }++ fn set_session_timezone_env(tz: &str, start_time: DateTime<Utc>) -> MockSimplifyInfo {+ let mut config = datafusion_common::config::ConfigOptions::default();+ config.execution.time_zone = tz.to_string();+ let mut execution_props =+ ExecutionProps::new().with_query_execution_start_time(start_time);+ execution_props.config_options = Some(Arc::new(config));+ MockSimplifyInfo { execution_props }+ }++ #[test]+ fn test_current_time_timezone_offset() {+ // Use a fixed start time for consistent testing+ let start_time = Utc.with_ymd_and_hms(2025, 1, 1, 12, 0, 0).unwrap();++ // Test with UTC+05:00+ let info_plus_5 = set_session_timezone_env("+05:00", start_time);+ let result_plus_5 = CurrentTimeFunc::new()+ .simplify(vec![], &info_plus_5)+ .unwrap();++ // Test with UTC-05:00+ let info_minus_5 = set_session_timezone_env("-05:00", start_time);+ let result_minus_5 = CurrentTimeFunc::new()+ .simplify(vec![], &info_minus_5)+ .unwrap();++ // Extract nanoseconds from results+ let nanos_plus_5 = match result_plus_5 {+ ExprSimplifyResult::Simplified(Expr::Literal(+ ScalarValue::Time64Nanosecond(Some(n)),+ _,+ )) => n,+ _ => panic!("Expected Time64Nanosecond literal"),+ };++ let nanos_minus_5 = match result_minus_5 {+ ExprSimplifyResult::Simplified(Expr::Literal(+ ScalarValue::Time64Nanosecond(Some(n)),+ _,+ )) => n,+ _ => panic!("Expected Time64Nanosecond literal"),+ };++ // Calculate the difference: UTC+05:00 should be 10 hours ahead of UTC-05:00+ let difference = nanos_plus_5 - nanos_minus_5;++ // 10 hours in nanoseconds+ let expected_offset = 10i64 * 3600 * 1_000_000_000;++ assert_eq!(difference, expected_offset, "Expected 10-hour offset difference in nanoseconds between UTC+05:00 and UTC-05:00");+ }+}diff --git a/datafusion/sqllogictest/test_files/current_time_timezone.slt b/datafusion/sqllogictest/test_files/current_time_timezone.sltnew file mode 100644index 0000000000..a9e27bd404--- /dev/null+++ b/datafusion/sqllogictest/test_files/current_time_timezone.slt@@ -0,0 +1,100 @@+# 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.++##########+## current_time with timezone tests+##########++# Test 1: Verify current_time is consistent within the same query (default UTC)+query B+SELECT current_time() = current_time();+----+true++# Test 2: Verify data type is correct+query T+SELECT arrow_typeof(current_time());+----+Time64(Nanosecond)++# Test 3: Set timezone to +08:00 and verify current_time is still stable+statement ok+SET datafusion.execution.time_zone = '+08:00';++query B+SELECT current_time() = current_time();+----+true++# Test 4: Verify current_time returns Time64 type in different timezone+query T+SELECT arrow_typeof(current_time());+----+Time64(Nanosecond)++# Test 5: Test with negative offset timezone+statement ok+SET datafusion.execution.time_zone = '-05:00';++query B+SELECT current_time() = current_time();+----+true++# Test 6: Test with named timezone (America/New_York)+statement ok+SET datafusion.execution.time_zone = 'America/New_York';++query B+SELECT current_time() = current_time();+----+true++# Test 7: Verify current_time is stable within a query+query B+SELECT+ current_time() = current_time() AND+ current_time() = current_time();+----+true++# Test 8: Reset to UTC+statement ok+SET datafusion.execution.time_zone = '+00:00';++query B+SELECT current_time() = current_time();+----+true++# Test 9: Verify current_time with Asia/Tokyo timezone+statement ok+SET datafusion.execution.time_zone = 'Asia/Tokyo';++query B+SELECT current_time() = current_time();+----+true++# Test 10: Verify current_time with Europe/London timezone+statement ok+SET datafusion.execution.time_zone = 'Europe/London';++query B+SELECT current_time() = current_time();+----+truediff --git a/docs/source/user-guide/sql/scalar_functions.md b/docs/source/user-guide/sql/scalar_functions.mdindex ec2faf8b3d..4ca3a822db 100644--- a/docs/source/user-guide/sql/scalar_functions.md+++ b/docs/source/user-guide/sql/scalar_functions.md@@ -2417,10 +2417,12 @@ current_date()### `current_time`-Returns the current UTC time.+Returns the current time in the session time zone.The `current_time()` return value is determined at query time and will return the same time, no matter when in the query plan the function executes.+The session time zone can be set using the statement 'SET datafusion.execution.time_zone = desired time zone'. The time zone can be a value like +00:00, 'Europe/London' etc.+```sqlcurrent_time()```