Repository navigation
Fix index analysis of date and time functions over a Nullable(DateTime('tz')) key - #125327
Conversation
…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]>
The changelog entry names skip index analysis, so the test also needs a
skip index over a `Nullable(DateTime('America/New_York'))` column with
`ORDER BY tuple()`, where only the index can prune. On master the
`toHour(dt) = 23` line returns 8 rows instead of 12.
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; 2 rounds, 1 finding) plus my own cold review (1 nit).
Re-review of the fix commit: 0 findings. Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261012-063300 |
|
Workflow [PR], commit [0b4f52e] Summary: ✅
AI ReviewSummaryThis PR fixes Final Verdict✅ No blocking issues found in the current diff. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units54 translation units recompiled, 171 s compile time in total, 54 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.95 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed primary key and skip index analysis that could skip or over-count rows for conditions on date and time functions such as
toHour,toDayOfMonth,timezoneOffsetortoTimeWithFixedDateover aNullable(DateTime)orLowCardinality(Nullable(DateTime))key whose time zone differs from the session time zone.Description
Closes: #125306
Closes: #121179
Related: #124474
Related: #121265
On 26.9.7.6 and 26.3.33.24, with
dt Nullable(DateTime('America/New_York'))as the sorting key (allow_nullable_key = 1) andsession_timezone = 'UTC',SELECT count() FROM t WHERE toHour(dt) = 23over rows 15 minutes apart returns 2 instead of 12. On master, after #124474,timezoneOffset(dt) = -14400on the same data also returns 105 instead of 120.IFunctionDateOrDateTime::getMonotonicityForRangetook the time zone fromdynamic_cast<const TimezoneMixin *>(&type)before unwrappingLowCardinalityandNullable. The wrappers carry no time zone, so the factor was compared in the session time zone while the function runs in the column's one, and ranges were declared monotonic where they are not. The cast now runs on the unwrapped type, as inIFunctionCustomWeeksince #118655. This covers every function of the class with a non-trivial factor (toHour,toMinute,toDayOfMonth,toMonth,timezoneOffset,toTimeWithFixedDate, ...), for primary keys and skip indexes; non-wrapped types are unchanged.#121265 by @ DeviousCardi has the same change to this file (its
IFunctionCustomWeekhalf is already on master through #118655).The test extends
05339_timezone_offset_monotonicity_dstwithcount()vscountIflines forNullable,LowCardinality(Nullable)andNullable(DateTime64)keys and aminmaxskip index, and amax_rows_to_readline checking that aNullablekey is still pruned; every new line fails on master. Locally, a matrix over four time zones, two session zones, five functions and=,<,>went from 720 wrong counts out of 4260 to 0.toStringoverNullable(DateTime('tz'))andNullable(Enum)keys has a similar defect inToStringMonotonicity(#119557, fix in #121081); it is not changed here.Workflow [PR]
Sync PR [sync-upstream/pr/125327]
Version info
26.10.1.2438-master(included in26.10and later)