xangelix opened a new pull request, #27977:
URL: https://github.com/apache/datafusion/pull/27977

   ## Which issue does this PR close?
   
   - Closes #27955.
   
   ## Rationale for this change
   
   Functions that list the same arguments for each timestamp unit with 
`TIMEZONE_WILDCARD`, nanoseconds first, cast a timezone-aware argument to 
nanoseconds: `"+TZ"` never equals a real timezone, so coercion takes the first 
listed signature the arguments can be coerced to. `date_bin` then returns 
another type than its input, and `date_bin` and `to_char` fail on timestamps 
that nanoseconds cannot hold, such as 1601-01-01 (the Windows FILETIME epoch) 
in microseconds.
   
   ## What changes are included in this PR?
   
   `try_coerce_types`, for signatures that are not "well supported" (such as a 
`OneOf` of `Exact`), used to return the first signature the arguments could be 
coerced to. It now returns the first one that keeps the unit of the most 
timezone-aware timestamps, where a timestamp keeps its unit by matching a 
`Timestamp(unit, Some(TIMEZONE_WILDCARD))` of its own unit 
(`kept_timestamp_units`). A signature without a wildcard keeps no units, so for 
those functions the first match is chosen exactly as before.
   
   Counting rather than requiring an exact match also covers calls where 
another argument still has to be cast: `date_bin` with a `TIMESTAMP '…'` 
(timezone-less) or string origin, and `to_char` with a Utf8View format.
   
   `date_bin` and `to_char` are the only functions in the workspace whose 
signatures use `TIMEZONE_WILDCARD`. Resolved types before and after, for a 
timezone-aware argument (UTC, `+05:30` and America/New_York give the same 
results):
   
   | call | s | ms | µs | ns |
   | --- | --- | --- | --- | --- |
   | `date_bin(i, ts)`, also with a `Timestamp(ns, tz)`, `TIMESTAMP '…'`, 
string or `Timestamp(µs, tz)` origin | ns → **s** | ns → **ms** | ns → **µs** | 
ns → ns |
   | `to_char(ts, fmt)` (returns Utf8) | arg cast to ns → kept | same | same | 
unchanged |
   | `date_bin` / `to_char` on a timezone-less timestamp | unchanged | 
unchanged | unchanged | unchanged |
   | `date_trunc`, `date_part`, `extract`, `to_local_time`, `to_unixtime`, 
`to_timestamp*`, `to_date`, `CAST(… AS DATE)` | unchanged | unchanged | 
unchanged | unchanged |
   
   The binned and formatted values are identical before and after for every 
timestamp in the nanosecond range (checked for the calls above, with DST 
transitions and negative timestamps); only `date_bin`'s result type changes. 
Both functions already ran their per-unit code for timezone-less timestamps, 
and the timezone only labels the result (`date_bin`) or goes to arrow's 
formatter (`to_char`).
   
   ## What is the testing strategy for this PR?
   
   - `timestamps_timezone.slt`: `date_bin` keeps µs/UTC and ms/Europe/Berlin, 
also with a timezone-less, string or millisecond origin; bins and formats 1601 
values as columns and as scalars; `to_char` with a Utf8View format; nanoseconds 
and timezone-less timestamps unchanged; seconds at chrono's first and last 
timestamps. All but the "unchanged" case fail on `main`.
   - Unit test `test_timezone_wildcard_keeps_unit`: a `OneOf` laid out like 
`date_bin`'s keeps each unit, also when another argument is cast, and falls 
back to the first signature as before when no signature has the argument's unit 
or the argument has no timezone.
   
   ## Are there any user-facing changes?
   
   Yes, one type change: **`date_bin` over a timezone-aware second, millisecond 
or microsecond timestamp now returns that unit** (for example `Timestamp(µs, 
"UTC")`) instead of `Timestamp(ns, <tz>)`, as it already did for timezone-less 
input. The values are the same, but anything that depends on the result type, 
such as a table created from it (`CREATE TABLE … AS SELECT date_bin(…)`) or a 
Parquet file written from it, gets the input's unit.
   
   `date_bin` and `to_char` also accept timezone-aware timestamps outside the 
nanosecond range, which failed to cast before. A user-defined function with the 
same kind of signature keeps the argument's unit too.
   
   ## Benchmarks
   
   The removed cast is visible end to end. `SELECT f(ts) FROM t` over 1M 
timezone-aware rows (America/New_York) in a `MemTable`, on one x86-64 machine 
with nothing else scheduled. The benchmark ran four times, interleaved: run 1 
on `main`, run 2 on this PR, run 3 on this PR, run 4 on `main`. The table shows 
each run's Criterion median in ms, and Criterion's change estimate with its 95% 
confidence interval for each `main`/PR pair: runs 1 and 2, then runs 4 and 3 
(negative = the PR is faster):
   
   | column | main (run 1) | PR (run 2) | PR (run 3) | main (run 4) | change, 
runs 1→2 | change, runs 4→3 |
   | --- | ---: | ---: | ---: | ---: | --- | --- |
   | `Timestamp(s, tz)` | 1.20 | 0.77 | 0.78 | 1.16 | −36.1% [−38.1, −34.3] | 
−33.3% [−34.5, −32.2] |
   | `Timestamp(ms, tz)` | 0.78 | 0.83 | 0.66 | 0.75 | −1.3% [−7.9, +4.8] | 
−12.2% [−13.2, −11.3] |
   | `Timestamp(µs, tz)` | 0.77 | 0.66 | 0.66 | 0.76 | −18.6% [−21.5, −15.8] | 
−13.6% [−14.9, −12.5] |
   | `Timestamp(ns, tz)` (same code before and after) | 0.64 | 0.66 | 0.64 | 
0.63 | +3.4% [+0.2, +6.5] | +2.6% [+1.1, +4.2] |
   
   - `date_bin` with a fixed stride no longer casts the column to nanoseconds: 
12–36% faster for seconds, milliseconds and microseconds.
   - `date_bin` with a month stride and `to_char` spend their time in chrono 
and arrow's formatter, so the removed cast does not show; their results are 
within the run-to-run noise of this benchmark, which the unchanged nanosecond 
rows show (up to ±11% in pair a, ±4% in pair b).
   - `date_part` and `date_trunc` resolve the same plan before and after; they 
are here as controls.
   
   The benchmark (`datafusion/core/benches/datetime_tz_query_sql.rs`) is not 
part of this PR; it can be added if reviewers want it.
   
   ## AI Disclaimer
   
   Claude Code helped with this one, as the co-author suggests. We're migrating 
away from duckdb into using pure datafusion, and our test suite we've been 
using for a while with duckdb has picked up some issues with datafusion we're 
hoping to get resolved upstream.
   
   Thanks so much for the great support of this library.


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to