Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
37 changes: 29 additions & 8 deletions datafusion/functions/src/datetime/current_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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.
"#,
Expand Down Expand Up @@ -100,12 +101,32 @@ 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(),
);

let days = if let Some(config) = info.execution_props().config_options() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can rewrite this into something more idiomatic

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)
        },
    );

if let Ok(tz) = config.execution.time_zone.parse::<Tz>() {
let local_now = tz.from_utc_datetime(&now_ts.naive_utc());
Some(
local_now.num_days_from_ce()
- NaiveDate::from_ymd_opt(1970, 1, 1)
.unwrap()
.num_days_from_ce(),
)
} else {
Some(
now_ts.num_days_from_ce()
- NaiveDate::from_ymd_opt(1970, 1, 1)
.unwrap()
.num_days_from_ce(),
)
}
} else {
Some(
now_ts.num_days_from_ce()
- NaiveDate::from_ymd_opt(1970, 1, 1)
.unwrap()
.num_days_from_ce(),
)
};
Ok(ExprSimplifyResult::Simplified(Expr::Literal(
ScalarValue::Date32(days),
None,
Expand Down
80 changes: 80 additions & 0 deletions datafusion/sqllogictest/test_files/current_date_timezone.slt
Original file line number Diff line number Diff line change
@@ -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 +19:00 and verify current_date is still stable
statement ok
SET datafusion.execution.time_zone = '+19: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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, this test appears to be failing on main:

----
false

# 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();
----
true
2 changes: 1 addition & 1 deletion docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down