Repository navigation
Fix rows skipped when a sub-second DateTime64 key is compared with an integer by > or < - #123999
Conversation
… integer by > or < Since ClickHouse#98410, `cast_not_needed` in `KeyCondition::extractAtomFromTree` keeps an integer constant (and a `DateTime` constant, whose `Field` is `UInt64` seconds) as an integer `Field` against a `DateTime64(N)` key. The comparison is exact, but `Range::createLeftBounded` / `createRightBounded` call `shrinkToIncludedIfPossible`, which closes an open bound on an integer `Field` by stepping it by one unit of the constant, i.e. one second rather than one key tick. `dt > 1577836800` became `dt >= 1577836801` and `dt < 1577836801` became `dt <= 1577836800`, so every granule, part or Parquet row group inside that second was pruned (primary key, partition key, `minmax` skip index). For a `DateTime64` key with scale > 0, store such a constant as `DecimalField<Decimal256>(value, 0)`. `FieldVisitorAccurateLess` / `Equals` already compared a decimal with an integer by building exactly that field, so comparison results are unchanged and only the bogus step goes away. `Decimal256` at scale 0 is used rather than the key's `Decimal64` at scale N because `value * 10^N` overflows `Int64` for large constants and the decimal comparison would throw `DECIMAL_OVERFLOW` during index analysis. `Bool` is converted too, before `normalizeBoolFields` would turn it into a steppable `UInt64`. `DateTime64(0)` keeps the integer form: its domain is whole seconds. `Range` is unchanged, so integer keys and `numbers()` range extraction keep their normalization. Closes ClickHouse#101276 Closes ClickHouse#113993 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the DateTime64 integer-bound comment in KeyCondition to one line. 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 (codex; 1 finding; no fix round needed).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261005-100200 |
|
Workflow [PR], commit [228de07] Summary: ✅
AI ReviewSummaryReviewed PR #123999 at Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 25 s compile time in total, 7 of them have a recent master baseline. |
…-bound # Conflicts: # src/Storages/MergeTree/KeyCondition.cpp
|
@groeneai please update the branch with master |
…me64-key-integer-strict-bound
Backport #123999 to 26.10: Fix rows skipped when a sub-second `DateTime64` key is compared with an integer by `>` or `<`
…e64` key is compared with an integer by `>` or `<`
…e64` key is compared with an integer by `>` or `<`
…e64` key is compared with an integer by `>` or `<`
Backport #123999 to 26.9: Fix rows skipped when a sub-second `DateTime64` key is compared with an integer by `>` or `<`
Backport #123999 to 26.8: Fix rows skipped when a sub-second `DateTime64` key is compared with an integer by `>` or `<`
Backport #123999 to 26.7: Fix rows skipped when a sub-second `DateTime64` key is compared with an integer by `>` or `<`
Closes: #101276
Closes: #113993
Related: #114001
Related: #98410
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix rows silently skipped by primary key, partition key and
minmaxskip index analysis when aDateTime64column with a fractional scale is compared with an integer orDateTimeconstant by>or<, for exampleWHERE dt > 1577836800on aDateTime64(3)key. The regression appeared in 26.3.Description
On 26.3 and later (measured 26.3.33.24 and 26.8.10.6),
WHERE dt > 1577836800on aDateTime64(3)primary key returns 2 rows instead of 3, anddt < 1577836801returns 1 instead of 3; 25.3.14.14 returns 3 for both.Root cause. Since #98410,
KeyConditionkeeps an integer constant as an integerFieldagainst aDateTime64key. The comparison is exact, butRangecloses an open bound on an integerFieldby stepping it by one, which here is one second, not one key tick:dt > 1577836800becomesdt >= 1577836801and every granule inside that second is pruned.Change. For a
DateTime64key with scale > 0, the integer (orDateTime,Bool) constant is stored as a scale-0Decimal256. Comparisons with the key are the same decimal comparison as before, andRangeno longer steps the bound.Rangeitself, integer keys andDateTime64(0)are unchanged. #114001 takes the other route (moving the step out ofRange); this one leavesRangeuntouched, sonumbers()range extraction, which relies on that step, is unaffected.Validation. The new test covers the primary key (plain, reversed,
NOT, monotonic chain,toDateTimebound,Nullable), the partition key and aminmaxskip index; 11 of its 12 lines fail on master. Also measured on scales 6 and 9, composite keys and Parquet row-group pruning, with no change forTime64,Decimal,DateTimeor integer keys.Workflow [PR]
Sync PR [sync-upstream/pr/123999]
Version info
26.11.1.433-master(included in26.11and later)26.10.2.64,26.9.25.16,26.8.34.14,26.7.38.8