Apache DataFusion SQL Query Engine
Feat: Make current_date aware of execution timezone. (#18034)
Sriram Sundar77ec31981af1
diff --git a/datafusion/functions/src/datetime/current_date.rs b/datafusion/functions/src/datetime/current_date.rsindex d7239e93de..0ba3afd19b 100644--- a/datafusion/functions/src/datetime/current_date.rs+++ b/datafusion/functions/src/datetime/current_date.rs@@ -17,9 +17,10 @@use std::any::Any;+use arrow::array::timezone::Tz;use arrow::datatypes::DataType;use arrow::datatypes::DataType::Date32;-use chrono::{Datelike, NaiveDate};+use chrono::{Datelike, NaiveDate, TimeZone};use datafusion_common::{internal_err, Result, ScalarValue};use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo};@@ -31,7 +32,7 @@ use datafusion_macros::user_doc;#[user_doc(doc_section(label = "Time and Date Functions"),description = r#"-Returns the current UTC date.+Returns the current date in the session time zone.The `current_date()` return value is determined at query time and will return the same date, no matter when in the query plan the function executes."#,@@ -100,14 +101,21 @@ impl ScalarUDFImpl for CurrentDateFunc {info: &dyn SimplifyInfo,) -> Result<ExprSimplifyResult> {let now_ts = info.execution_props().query_execution_start_time;- let days = Some(- now_ts.num_days_from_ce()- - NaiveDate::from_ymd_opt(1970, 1, 1)- .unwrap()- .num_days_from_ce(),- );++ // Get timezone from config and convert to local time+ let days = info+ .execution_props()+ .config_options()+ .and_then(|config| config.execution.time_zone.parse::<Tz>().ok())+ .map_or_else(+ || datetime_to_days(&now_ts),+ |tz| {+ let local_now = tz.from_utc_datetime(&now_ts.naive_utc());+ datetime_to_days(&local_now)+ },+ );Ok(ExprSimplifyResult::Simplified(Expr::Literal(- ScalarValue::Date32(days),+ ScalarValue::Date32(Some(days)),None,)))}@@ -116,3 +124,11 @@ impl ScalarUDFImpl for CurrentDateFunc {self.doc()}}++/// Converts a DateTime to the number of days since Unix epoch (1970-01-01)+fn datetime_to_days<T: Datelike>(dt: &T) -> i32 {+ dt.num_days_from_ce()+ - NaiveDate::from_ymd_opt(1970, 1, 1)+ .unwrap()+ .num_days_from_ce()+}diff --git a/datafusion/sqllogictest/test_files/current_date_timezone.slt b/datafusion/sqllogictest/test_files/current_date_timezone.sltnew file mode 100644index 0000000000..1b9c3cddee--- /dev/null+++ b/datafusion/sqllogictest/test_files/current_date_timezone.slt@@ -0,0 +1,80 @@+# 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_date with timezone tests+##########++# Test 1: Verify current_date is consistent within the same query (default UTC)+query B+SELECT current_date() = current_date();+----+true++# Test 2: Verify alias 'today' works the same as current_date+query B+SELECT current_date() = today();+----+true++# Test 3: Set timezone to +05:00 and verify current_date is still stable+statement ok+SET datafusion.execution.time_zone = '+05:00';++query B+SELECT current_date() = current_date();+----+true++#Test 4: Verify current_date matches cast(now() as date) in the same timezone+query B+SELECT current_date() = cast(now() as date);+----+true++# Test 5: Test with negative offset timezone+statement ok+SET datafusion.execution.time_zone = '-08:00';++query B+SELECT current_date() = today();+----+true++# Test 6: Test with named timezone (America/New_York)+statement ok+SET datafusion.execution.time_zone = 'America/New_York';++query B+SELECT current_date() = current_date();+----+true++# Test 7: Verify date type is preserved+query T+SELECT arrow_typeof(current_date());+----+Date32++# Test 8: Reset to UTC+statement ok+SET datafusion.execution.time_zone = '+00:00';++query B+SELECT current_date() = today();+----+truediff --git a/docs/source/user-guide/sql/scalar_functions.md b/docs/source/user-guide/sql/scalar_functions.mdindex 4a1069d4fd..9fcaac7628 100644--- a/docs/source/user-guide/sql/scalar_functions.md+++ b/docs/source/user-guide/sql/scalar_functions.md@@ -2403,7 +2403,7 @@ Additional examples can be found [here](https://github.com/apache/datafusion/blo### `current_date`-Returns the current UTC date.+Returns the current date in the session time zone.The `current_date()` return value is determined at query time and will return the same date, no matter when in the query plan the function executes.