Repository navigation
Fix key analysis for timezoneOffset and toTimeWithFixedDate over a range that spans a UTC offset change - #124474
Conversation
…UTC offset change
Key analysis calls a date/time function monotonic on a key range when its
factor transform gives the same value at both ends. The factor of
`timezoneOffset` was `toTimeWithFixedDate` (same local time of day), so a
range from local midnight in January to local midnight in December was
called monotonic although the offset changes twice in between: on
26.3.33.24 and master `timezoneOffset(dt) = -14400` over a
`DateTime('America/New_York')` key returned 0 rows instead of 238, and
`= -18000` returned 366 instead of 128. The factor of `toTimeWithFixedDate`
was `toDate` (same local day), which ignores that the wall clock goes back
on a fall-back day, so `[01:50 EDT, 01:10 EST]` became an empty range and
the granule was dropped.
Both functions now use the pair (unclamped local day, UTC offset) as the
factor. The lookup table stores at most one offset change per day, so equal
pairs mean the offset is constant on the range: `timezoneOffset` is then
constant and `toTimeWithFixedDate` increases. Outside the lookup table range
the cctz path clamps the day to [0000, 9999] while the time of day keeps
cycling, so there the factor is unique to the end point and nothing is
claimed. The `toTimeWithFixedDate` variant reads `DateTime64` through its
whole part like the function; the `timezoneOffset` factor has no
`DecimalComponents` overload, so `TransformDateTime64` floors it the same
way it floors the function's argument.
Ranges within one day without an offset change become prunable for
`timezoneOffset`; ranges across midnight or an offset change no longer are.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the factor comment to its invariant and state the out-of-LUT case as one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (3 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261007-182400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261007-173600 |
|
Workflow [PR], commit [d10cea5] Summary: ✅
AI ReviewSummaryThis PR changes the monotonicity factors for Missing context / blind spots
Findings❌ Blockers
Final Verdict❌ Changes requested — the default-zone DST regression is fixed, but LLVM Coverage ReportMeasured on commit d10cea5.
Changed lines: Changed C/C++ lines covered: 6/9 (66.67%) · Uncovered code |
The existing lines only compare counts with and without the key condition, so they also pass if `timezoneOffset` and `toTimeWithFixedDate` stopped claiming monotonicity altogether. Add a table with one granule per hour of a single local day and bound the rows read with `max_rows_to_read`: the ranges inside the day that keep one UTC offset must still be skipped. With a factor that never declares monotonicity, 24 of 25 rows are read and both new lines fail. The PartsSplitter injection is clamped to 0 and index analysis is enabled on every parallel replica, as in 00926 and 00979, so that the bounds do not depend on the randomized read plan. Also cover the `DateTime64` overload of the `toTimeWithFixedDate` factor: before the epoch, the function truncates a negative timestamp towards zero, while the generic `DateTime64` path floors it. Without the overload, 1969-10-26 01:59:59.000 and 01:59:59.500 -04:00 get the same factor although the function maps them to 01:59:59 and 01:00:00, and the row at 01:59:59 is skipped (0 instead of 1). Co-Authored-By: Claude Opus 5.5 <[email protected]>
| static constexpr bool hasPreimage() { return false; } | ||
|
|
||
| using FactorTransform = ToDateImpl<>; | ||
| using FactorTransform = ToTimeWithFixedDateFactorImpl; |
There was a problem hiding this comment.
Switching toTimeWithFixedDate to this factor fixes the default-column-timezone path, but the supported toTimeWithFixedDate(datetime, timezone) form is still unsound. IFunctionDateOrDateTime::getMonotonicityForRange derives date_lut from the source type, not from the explicit timezone argument, so for DateTime('America/New_York') a range like 2024-11-03 02:00:00 EST .. 2024-11-03 23:30:00 EST gets the same (day, offset) here while toTimeWithFixedDate(dt, 'UTC') wraps at 2024-11-04 00:00:00 UTC in the middle. Key analysis can therefore still treat the function as monotonic and skip / over-count rows. We need to either thread the constant timezone argument into monotonicity or conservatively disable monotonicity when the second argument is present.
There was a problem hiding this comment.
The explicit time zone is already used here: FunctionWithOptionalConstArg::getMonotonicityForRange in KeyCondition.cpp rebuilds the argument type as DateTime(tz) / DateTime64(s, tz) from the constant argument before calling the function, so the factor reads that zone's LUT, not the column's.
Measured on current master (d741c98) and on this PR's CI build:
- the case above (
DateTime('America/New_York')key, 2024-11-03 02:00 to 23:30 EST,toTimeWithFixedDate(dt, 'UTC')): correct counts on both, with the index still used (Granules: 3/44); - the reverse case (
DateTime('UTC')key, rows at 01:50 EDT and 01:10 EST,toTimeWithFixedDate(dt, 'America/New_York')equal to 01:50): master returns 0 rows instead of 1, this PR returns 1 through the primary key, the partition minmax and a minmax skip index.
So I don't see a follow-up to do. If you want the reverse case in the test, I can send it as a small separate PR.
|
@groeneai @alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
…h `DateTime64` down The `fall64` case of `05339_timezone_offset_monotonicity_dst` (added in #124474) relied on `toTimeWithFixedDate` truncating a pre-epoch `DateTime64` towards zero, which moved `1969-10-26 05:59:59.500` UTC to the next second, after the fall-back. This PR rounds the fractional part down, so both rows are at 01:59:59 -04:00, with and without the key condition. The factor of `toTimeWithFixedDate` reads the same rounded whole part through `TransformDateTime64`, so key analysis stays consistent with the function. This failed the `Fast test` in the merge queue: https://github.com/ClickHouse/ClickHouse/actions/runs/38282390263 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ullable keys
`IFunctionDateOrDateTime::getMonotonicityForRange` picked the lookup table with
`dynamic_cast<const TimezoneMixin *>(&type)` before unwrapping `LowCardinality`
and `Nullable`. The wrappers are not `TimezoneMixin`, so for a
`Nullable(DateTime('tz'))` key the factor was compared in the session time zone
while the function itself runs in the column's time zone. A range whose factor
is equal at both ends in the session zone but not in the column zone was
declared monotonic, and primary key / skip index analysis skipped matching
granules or over-counted rows.
This is the long-standing ClickHouse#121179 (`toHour`, `toDayOfMonth`, ... over such keys);
ClickHouse#124474 changed the `timezoneOffset` factor to (local day, UTC offset), which in
a UTC lookup table is constant over a whole UTC day, so sub-daily `Nullable`
`timezoneOffset` conditions started losing rows on master (ClickHouse#125306).
Take the time zone from the unwrapped type, as `IFunctionCustomWeek` does since
ClickHouse#118655. ClickHouse#121265 contains the same change to this file.
Closes ClickHouse#125306
Closes ClickHouse#121179
Co-Authored-By: Claude Opus 5.5 <[email protected]>
toHour and toTimeWithFixedDate over a pre-epoch DateTime and DateTime64 are fixed on master by ClickHouse#124530 and ClickHouse#124474, so their queries pass there and no longer test this change. toTimeWithFixedDate also rounds a pre-epoch DateTime64 down to whole seconds on master now, so the expected row of the removed DateTime64 case no longer exists. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed primary key, partition key and skip index analysis that could skip or over-count rows for conditions on
timezoneOffsetandtoTimeWithFixedDateover aDateTimeorDateTime64column in a time zone with daylight saving time, when the key range spans a change of the UTC offset.Description
Related: #124470 (also defines
ToTimeWithFixedDateFactorImpl; the PR merged second keeps one definition, deriving fromToDayAndUTCOffsetFactorImpl)On 26.3.33.24 and master, with
dt DateTime('America/New_York')as the sorting key and one row per day of 2024 at local midnight,SELECT count() FROM t WHERE timezoneOffset(dt) = -14400returns 0 instead of 238, and= -18000returns 366 instead of 128.Key analysis treats a function as monotonic on a range when its factor is equal at both ends. The factor of
timezoneOffsetwas the local time of day, so a range from midnight to midnight across the whole summer was called monotonic and got the single value[-18000, -18000]. The factor oftoTimeWithFixedDatewas the local day, which ignores that the clock goes back on a fall-back day, so[01:50 EDT, 01:10 EST]became the empty range[01:50, 01:10].Both now use the pair (local day, UTC offset). The lookup table holds at most one offset change per day, so equal pairs mean the offset is constant between the ends. Where the lookup table does not apply (
DateTime64within two days of the ends of its range, or beyond them), no monotonicity is claimed. Ranges within one day without an offset change can now be pruned fortimezoneOffset; ranges across midnight or an offset change cannot.Most test lines compare
count()with acountIfover the table, for daily, hourly and per-minute data inAmerica/New_Yorkand forDateTime64before 1970 and past 9999-12-31; two bound the rows read withmax_rows_to_readto check that ranges within one day are still pruned.Nullable(DateTime(tz))keys also need #121265, which fixes the time zone used.toYYYYMMDDhhmmss(declared always monotonic) andtoMinutein a zone with a 30-minute change such asAustralia/Lord_Howealso skip rows on a fall-back; they need their own factors and are not changed here.Workflow [PR]
Sync PR [sync-upstream/pr/124474]
Version info
26.10.1.2061-master(included in26.10and later)