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

   ## Which issue does this PR close?
   
   - Closes #25464.
   
   This follows #27547, which restored the ordering for `minute` and finer 
granularities. #25635 covered `UTC` and fixed offsets and has been closed. This 
PR covers those zones too, because a zone without transitions keeps the order 
at every granularity.
   
   ## Rationale for this change
   
   #25409 stopped `date_trunc` from propagating its input ordering for 
timezone-aware timestamps, because truncating in local time can go backwards 
across a timezone transition (#25353). After #27547, `hour` and coarser 
granularities still drop the ordering for every timezone, including `UTC` and 
fixed offsets. So sorting or grouping by `date_trunc('day', ts)` over input 
ordered on `ts` adds a sort, or keeps every group in the hash table until the 
input ends.
   
   Most zones never reverse at these granularities. While the UTC offset is 
constant, truncating to a day or longer is monotone, because consecutive 
buckets start at least 24 hours apart and no offset change is that large. So 
the order can only break at a transition, and checking the instants around each 
transition is exact. For example, America/Goose_Bay fell back from 00:01 to 
23:01 the previous day, so `day` goes backwards there. America/New_York changes 
its offset at 02:00, away from midnight, so `day` keeps the order.
   
   ## What changes are included in this PR?
   
   - `DateTruncFunc::output_ordering` also keeps the input ordering for a 
timezone-aware timestamp when `local_truncation_preserves_order` holds for the 
constant granularity.
   - `local_truncation_preserves_order` finds every UTC offset transition of 
the zone between 1800 and 2200. It steps a day at a time and bisects to the 
second. Steps of a day catch every transition, since tz database transitions 
are at least several days apart; the closest pair in chrono-tz 0.10.4 is 6.9 
days apart. For `day`, `week`, `month`, `quarter` and `year`, it compares the 
kernel's output 1 ns before and at each transition. `hour` is only accepted for 
zones without transitions, such as `UTC` and fixed offsets.
   - The result is cached per timezone name. In a release build, the first call 
took 0.6 to 4.6 ms in the zones I timed (`America/New_York`, 359 transitions: 
4.6 ms; `UTC`: 1.2 ms; `+05:30`: 0.6 ms). Later calls take about 40 ns.
   - `minute` and finer granularities are unchanged (#27547).
   
   ## What is the testing strategy for this PR?
   
   - `timestamps.slt` adds two `EXPLAIN` cases next to the America/Goose_Bay 
regression from #25353. `date_trunc('day', ...)` in America/New_York needs no 
`SortExec`, and in America/Goose_Bay the `SortExec` stays. The New York case 
fails without this change.
   - The `output_ordering_with_timezone_depends_on_granularity` unit test also 
asserts that America/New_York is ordered for `day` through `year` and unordered 
for `hour`, that `UTC` and `+05:30` are ordered for `hour`, `day` and `year`, 
and that America/Goose_Bay stays unordered for `hour` and `day`.
   - The new `timezone_offsets_are_constant_outside_transition_scan_window` 
unit test checks every chrono-tz zone, so a chrono-tz upgrade that adds 
transitions outside the scan window fails it.
   
   As a check outside the test suite, I compared the new check with dense 
sampling of the kernel around every transition, for all 597 chrono-tz zones and 
`hour` through `year`. The sampling covered ±3 s, every minute for ±2 h, every 
30 min for ±2 days and every 12 h for ±40 days. No zone that the check reports 
as ordered reverses. The daily-step transitions matched an independent hourly 
scan in every zone. For `day` through `year` the check and the sampling agreed 
exactly: 21 zones reverse at `day`, 7 at `month`, 4 at `quarter` and `year`, 
and none at `week`. 43 zones reverse at `hour`, the same count as the sweep in 
#27547.
   
   The plan change is the same one #27547 makes for `minute`: no sort, and 
clustered aggregation. So the trade-off measured there applies here too. It is 
also the plan `main` already picks for timezone-less timestamps.
   
   ## Are there any user-facing changes?
   
   Queries that sort or group timezone-aware timestamps by `date_trunc`, over 
input ordered on the timestamp, no longer add a sort and use clustered 
aggregation in these cases:
   
   - `day` or coarser granularity, in zones where truncation keeps the order
   - `hour`, in zones without transitions such as `UTC` and fixed offsets
   
   Results are unchanged. No API changes.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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