Skip to content

Conversation

@Weijun-H
Copy link
Member

@Weijun-H Weijun-H commented Oct 11, 2025

Which issue does this PR close?

Rationale for this change

DataFusion CLI v50.1.0
> SET TIME ZONE = '+08:00';
0 row(s) fetched. 
Elapsed 0.011 seconds.

> SELECT arrow_typeof(now());
+---------------------------------------+
| arrow_typeof(now())                   |
+---------------------------------------+
| Timestamp(Nanosecond, Some("+08:00")) |
+---------------------------------------+
1 row(s) fetched. 
Elapsed 0.015 seconds.

> SELECT count(1) result FROM (SELECT now() as n) a WHERE n > '2000-01-01'::date;
+--------+
| result |
+--------+
| 1      |
+--------+
1 row(s) fetched. 
Elapsed 0.029 seconds.

What changes are included in this PR?

When the timezone changes, re-register now() function

Are these changes tested?

Are there any user-facing changes?

@Weijun-H Weijun-H changed the title fix: Use dynamic timezone in now() function for accurate timestamp WIP fix: Use dynamic timezone in now() function for accurate timestamp Oct 11, 2025
@github-actions github-actions bot added the functions Changes to functions implementation label Oct 11, 2025
@Weijun-H Weijun-H force-pushed the 17993-make-now-aware-time-zone branch from 8db266f to 33732c5 Compare October 11, 2025 12:25
@github-actions github-actions bot added the sqllogictest SQL Logic Tests (.slt) label Oct 11, 2025
@Weijun-H Weijun-H changed the title WIP fix: Use dynamic timezone in now() function for accurate timestamp fix: Use dynamic timezone in now() function for accurate timestamp Oct 11, 2025
@Weijun-H Weijun-H marked this pull request as draft October 11, 2025 12:28
@Weijun-H Weijun-H force-pushed the 17993-make-now-aware-time-zone branch 2 times, most recently from 70ed548 to b81ebf6 Compare October 12, 2025 10:03
@github-actions github-actions bot added the core Core DataFusion crate label Oct 13, 2025
@Weijun-H Weijun-H force-pushed the 17993-make-now-aware-time-zone branch from f7ba4e5 to 9f1b55f Compare October 13, 2025 19:00
@Weijun-H Weijun-H requested a review from Copilot October 13, 2025 19:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the now() function to return timestamps with the correct timezone set in the session configuration, instead of always using the default "+00:00" timezone.

Key changes:

  • Updated now() function to use dynamic timezone from session configuration
  • Added configuration-aware UDF creation macro for functions that depend on session state
  • Added test coverage for timezone-aware now() function behavior

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
datafusion/functions/src/macros.rs Added new macro for creating UDFs with configuration parameters
datafusion/functions/src/datetime/now.rs Modified NowFunc to accept and use timezone from configuration instead of hardcoded "+00:00"
datafusion/core/src/execution/context/mod.rs Added logic to re-register now() UDF when timezone configuration changes
datafusion/sqllogictest/test_files/timestamps.slt Added test to verify now() returns correct timezone type
datafusion/sqllogictest/test_files/dates.slt Updated expected error message to reflect new default timezone format

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Weijun-H Weijun-H marked this pull request as ready for review October 13, 2025 19:05
Comment on lines 1078 to 1093

// Register UDFs that return values based on session configuration
// e.g. now() which depends on the time_zone configuration option
if variable == "datafusion.execution.time_zone" {
let config_options = self.state.read().config().options().clone();
let now_udf = {
// recreate the function so it captures the new time zone
make_udf_function_with_config!(NowFunc, now, &ConfigOptions);
now(&config_options)
};
self.state.write().register_udf(now_udf)?;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

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 the above approach will handle all future udf changes that follow this pattern

@Weijun-H Weijun-H marked this pull request as draft October 13, 2025 20:00
@Weijun-H Weijun-H force-pushed the 17993-make-now-aware-time-zone branch from 34ad88f to e7a6ab9 Compare October 13, 2025 20:11
@Weijun-H Weijun-H marked this pull request as ready for review October 13, 2025 20:11
@Weijun-H Weijun-H force-pushed the 17993-make-now-aware-time-zone branch from 8a1de2c to ed73c86 Compare October 13, 2025 21:30
@Weijun-H Weijun-H requested a review from Omega359 October 14, 2025 07:25
@kosiew
Copy link
Contributor

kosiew commented Oct 14, 2025

Re-registering now() inside set_config_option covers interactive SET commands, but sessions that are constructed differently eg with a pre-configured SessionConfig (e.g. via builder APIs or server defaults) still get the default NowFunc::new() that ignores the configured timezone.

eg.

❯ DATAFUSION_EXECUTION_TIME_ZONE='+05:45' cargo run --bin datafusion-cli -- -c "SELECT arrow_typeof(now());"

DataFusion CLI v50.1.0
+------------------------------------+
| arrow_typeof(now())                |
+------------------------------------+
| Timestamp(Nanosecond, Some("+00")) |
+------------------------------------+
1 row(s) fetched.
Elapsed 0.092 seconds.
❯ cargo run --bin datafusion-cli -- -c "SET datafusion.execution.time_zone = '+05:45'; SELECT arrow_typeof(now());"
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.48s
     Running `target/debug/datafusion-cli -c 'SET datafusion.execution.time_zone = '\''+05:45'\''; SELECT arrow_typeof(now());'`
DataFusion CLI v50.1.0
0 row(s) fetched.
Elapsed 0.034 seconds.

+---------------------------------------+
| arrow_typeof(now())                   |
+---------------------------------------+
| Timestamp(Nanosecond, Some("+05:45")) |
+---------------------------------------+
1 row(s) fetched.
Elapsed 0.040 seconds.

@Weijun-H
Copy link
Member Author

Thanks @Omega359 and @kosiew for reviewing. I think this pr is ready for review.

╰─ DATAFUSION_EXECUTION_TIME_ZONE='+05:45' cargo run --bin datafusion-cli -- -c "SELECT arrow_typeof(now());"                                           ─╯
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.53s
     Running `target/debug/datafusion-cli -c 'SELECT arrow_typeof(now());'`
DataFusion CLI v50.1.0
+---------------------------------------+
| arrow_typeof(now())                   |
+---------------------------------------+
| Timestamp(Nanosecond, Some("+05:45")) |
+---------------------------------------+
1 row(s) fetched. 
Elapsed 0.011 seconds.

@Weijun-H Weijun-H force-pushed the 17993-make-now-aware-time-zone branch 3 times, most recently from 1310d0f to 179607a Compare October 14, 2025 20:52
@Weijun-H Weijun-H force-pushed the 17993-make-now-aware-time-zone branch from f9fbf67 to 4e8a0c3 Compare October 20, 2025 07:46
@Weijun-H
Copy link
Member Author

@kosiew @Jefffrey @Omega359 @alamb

Any additional feedback on this PR? If there are no blocking issues, I'd like to merge soon.

Copy link
Contributor

@kosiew kosiew left a comment

Choose a reason for hiding this comment

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

LGTM except for the deprecated NowFunc::new(), still seeding the timezone with "+00"

Self {
signature: Signature::nullary(Volatility::Stable),
aliases: vec!["current_timestamp".to_string()],
timezone: Some(Arc::from("+00")),
Copy link
Contributor

Choose a reason for hiding this comment

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

NowFunc::new(), though deprecated seeds the timezone with "+00" (missing minutes) while the canonical UTC offset remains "+00:00" in ConfigOptions::default().
Existing callers of the deprecated constructor will therefore get a different datatype/ScalarValue than before this change.

@Weijun-H Weijun-H added this pull request to the merge queue Oct 22, 2025
Merged via the queue into apache:main with commit b7a10ad Oct 22, 2025
55 of 56 checks passed
@Weijun-H
Copy link
Member Author

Thanks again @kosiew @Jefffrey @Omega359 @alamb

@Omega359
Copy link
Contributor

LGTM except for the deprecated NowFunc::new(), still seeding the timezone with "+00"

Filed as #18219

github-merge-queue bot pushed a commit that referenced this pull request Oct 24, 2025
## Which issue does this PR close?

- Relates #18062 
- Relates #18065

## Rationale for this change

We disabled these tests because CI was failing on main.

The test: `current_date() = cast(now() as date)` was added in #18034 
requires `now` to use configured timezone, but it is only available
after #18017.

Since #18017 has been merged, these tests should be enable.

## What changes are included in this PR?

This reverts commit a65a2cb.

## Are these changes tested?

Yes.

## Are there any user-facing changes?
No.
tobixdev pushed a commit to tobixdev/datafusion that referenced this pull request Nov 2, 2025
…pache#18017)

## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Closes apache#17993

## Rationale for this change

```
DataFusion CLI v50.1.0
> SET TIME ZONE = '+08:00';
0 row(s) fetched. 
Elapsed 0.011 seconds.

> SELECT arrow_typeof(now());
+---------------------------------------+
| arrow_typeof(now())                   |
+---------------------------------------+
| Timestamp(Nanosecond, Some("+08:00")) |
+---------------------------------------+
1 row(s) fetched. 
Elapsed 0.015 seconds.

> SELECT count(1) result FROM (SELECT now() as n) a WHERE n > '2000-01-01'::date;
+--------+
| result |
+--------+
| 1      |
+--------+
1 row(s) fetched. 
Elapsed 0.029 seconds.
```

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

## What changes are included in this PR?
When the timezone changes, re-register `now()` function

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->

---------

Co-authored-by: Andrew Lamb <[email protected]>
tobixdev pushed a commit to tobixdev/datafusion that referenced this pull request Nov 2, 2025
## Which issue does this PR close?

- Relates apache#18062 
- Relates apache#18065

## Rationale for this change

We disabled these tests because CI was failing on main.

The test: `current_date() = cast(now() as date)` was added in apache#18034 
requires `now` to use configured timezone, but it is only available
after apache#18017.

Since apache#18017 has been merged, these tests should be enable.

## What changes are included in this PR?

This reverts commit a65a2cb.

## Are these changes tested?

Yes.

## Are there any user-facing changes?
No.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate functions Changes to functions implementation logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make now aware of execution timezone

5 participants