Repository navigation
Fix toUnixTimestamp monotonicity for Date and Date32 keys - #117709
Conversation
`toUnixTimestamp` used `ToNumberMonotonicity`, which models every conversion to a number as a plain cast of the argument's internal representation. That is right for the `toInt*`/`toUInt*` family, but the `Date` and `Date32` arms of `toUnixTimestamp` multiply the day number by 86400 and wrap the product around `UInt32`, so the conversion reorders values past 2106-02-07 and for negative day numbers. `KeyCondition` transformed primary key range endpoints through the function under the unconditional always-monotonic claim, and the statistics-based part pruning consulted the same claim, so both silently dropped granules and parts holding matching rows. `toUnixTimestamp` now has its own monotonicity, which claims nothing for a `Date` or `Date32` range that reaches the wrapping days and delegates every other argument type to `ToNumberMonotonicity`. Closes: #117400
|
Workflow [PR], commit [b8525ce] Summary: ✅
AI ReviewSummaryThis PR replaces Tests
Final VerdictChanges requested: the fix itself looks correct, but the LLVM Coverage ReportMeasured on commit b8525ce.
Changed lines: Changed C/C++ lines covered: 21/24 (87.50%) · Uncovered code |
|
Verified the boundary arithmetic against the execution path ( One test nit: the description says ranges inside the representable window keep their pruning, but the test asserts only result equality ( The remaining silent-wrong siblings in this family are tracked separately (#116946, #117682, #117697, #117696, #111759) and are orthogonal to this diff. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units40 translation units recompiled, 290 s compile time in total, 40 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.11 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
…mp-date-monotonicity-117400
The "safe window" query only compared `count()` against a full scan, which cannot distinguish "correct with pruning" from "correct after reading every granule". Add a `Granules:` assertion for an in-window range, plus the contrasting range that reaches the wrapping days and therefore keeps every granule. Verified with a control binary in which `ToUnixTimestampMonotonicity` returns no claim for `Date`/`Date32`: the in-window query then reads `5/5` granules instead of `4/5` and the test fails, so the new assertion is load-bearing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Updated:
|
| SETTINGS index_granularity = 1, index_granularity_bytes = 0, min_bytes_for_wide_part = 0; | ||
| INSERT INTO t_unix_ts_date32 VALUES ('1900-01-01'), ('2000-01-01'), ('2100-01-01'), ('2200-01-01'), ('2299-12-31'); | ||
|
|
||
| SELECT count(), (SELECT countIf(toUnixTimestamp(d) >= 4000000000) FROM t_unix_ts_date32) FROM t_unix_ts_date32 WHERE toUnixTimestamp(d) >= 4000000000; |
There was a problem hiding this comment.
The Date half now proves that an in-window range still prunes, but the Date32 half still only checks result equality after filtering.
That leaves the new Date32-specific branch in ToUnixTimestampMonotonicity effectively unpinned: if src/Functions/FunctionsConversion.h stopped recognizing the Int64 key-range endpoints used for Date32, toUnixTimestamp(d) >= 4000000000 would full-scan every granule and this assertion would still stay green.
Please add a focused pruning assertion for the in-window Date32 case as well, for example an EXPLAIN indexes = 1 / Granules: check on this predicate or a small unit test that exercises ToUnixTimestampMonotonicity::get(DataTypeDate32(), ...).
The `toUnixTimestamp` half of this branch landed on master independently as ClickHouse#117709, as a `ToUnixTimestampMonotonicity<T>` template with its own test. Both conflicting hunks are that half, so master's version is kept here and this branch's non-template `ToUnixTimestampMonotonicity` and its two instantiations are dropped. What survives is the range-aware `ToDateTimeMonotonicity<DataTypeDateTime>` block: the `Date`/`Date32` to `DateTime` conversions are still unfixed on master.
Master's ClickHouse#117709 now owns `toUnixTimestamp` monotonicity, including its own regression test 05060_to_unix_timestamp_date_key_wrap, so the assertions here that name `toUnixTimestamp` no longer cover code this branch changes. They are removed, along with the two blocks that existed only for them: the `Nullable(Date)` block, whose `DateTime` spelling is already correct on master and would therefore assert nothing, and the `toUnixTimestamp`-only window edges, which 05060 and ClickHouse#117709 own. The wrapped-sorting-key block keeps its scenario and moves to `toDateTime32`, where it still reddens: on master `ORDER BY toDateTime32(d)` with a predicate on the raw column returns 0 of the 20 matching rows, and `force_primary_key = 1` succeeds instead of raising `INDEX_NOT_USED`. The day-number window check moves from a free helper into the trait as a local lambda. The helper's shape existed to be shared with the dropped `toUnixTimestamp` trait; with one caller left its `max_day_num` parameter is always `MAX_DATETIME_DAY_NUM`, and master's sibling trait already keeps the equivalent check local to itself.
…ion-monotonicity-wrap-116946 Conflict in `src/Functions/FunctionsConversion.h`: both sides added a block after `ToNumberMonotonicity`. Kept both: this branch's `dateTimeConversionRangeCannotWrap` and master's `ToUnixTimestampMonotonicity` from #117709.
toUnixTimestampusedToNumberMonotonicity, which models every conversion to a number as a plain cast of the argument's internal representation. That is right for thetoInt*/toUInt*family, but theDateandDate32arms oftoUnixTimestampmultiply the day number by 86400 and wrap the product aroundUInt32(seeconvertToUnixTimestampFromDate), so the conversion reorders values past 2106-02-07 and for negative day numbers.KeyConditiontransformed primary key range endpoints through the function under the unconditional always-monotonic claim, and the statistics-based part pruning consulted the same claim, so both silently dropped granules and parts holding matching rows:toUnixTimestampnow has its own monotonicity, which claims nothing for aDate/Date32range that reaches the wrapping days and delegates every other argument type toToNumberMonotonicity. Ranges that stay inside the representable window keep their pruning, which the test asserts.Closes: #117400
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed silently missing rows for predicates on
toUnixTimestampof aDate/Date32primary key or a column with statistics: the conversion wraps aroundUInt32for dates after 2106-02-07, but index analysis assumed it was monotonic everywhere.Workflow [PR]
Sync PR [sync-upstream/pr/117709]
Version info
26.9.1.1341(included in26.9and later)