Repository navigation
Normalize a constant through the key column type before the key DAG - #118688
Conversation
```sql CREATE TABLE t (d DateTime64(3, 'UTC')) ENGINE = MergeTree ORDER BY d::String; INSERT INTO t VALUES (toDateTime64(1675252800, 3, 'UTC')); SELECT count() FROM t WHERE d = toDateTime64(1675252800, 6, 'UTC'); -- 0, expected 1 ``` `EXPLAIN indexes = 1` shows the condition over `['2023-02-01 12:00:00.000000', ...]` - six fractional digits, while every key value in the part has the three of the column's scale - so the range check concludes the value lies outside the part and prunes it. Under `PARTITION BY d::String` every partition's minimum equals its maximum, so every lookup with a mismatched scale loses its row. `applyDeterministicDagToColumn` preferred a fast path that applies the `CAST` of the key DAG straight to the constant's own type, skipping the normalizing round trip through the key column's type. That fast path is for round trips that cannot be done safely - `String` -> `Dynamic` -> `String`, where the normalizing cast refuses the target and returns false - so the normalizing cast is tried first now, and the direct cast remains the fallback for exactly those cases. A constant the key column's type cannot represent exactly (a `DateTime64(6)` value with a microsecond part against a `DateTime64(3)` column) still declines: no value of the column can equal it, which is what the row comparison computes as well. Closes: #117813 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [6a44b78] Summary: ✅
AI ReviewSummaryThis PR fixes the Findings
Final Verdict
LLVM Coverage ReportMeasured on commit 6a44b78.
Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes24 object files changed (+79.76 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units189 translation units recompiled, 1937 s compile time in total, 189 of them have a recent master baseline. |
The same helper transforms whole set columns, so falling back to the direct
`CAST` whenever the normalizing cast fails let one unrepresentable element of a
set drag the representable ones into the constant's own value space: a mixed
`IN (toDateTime64(..., 6), toDateTime64('...000001', 6))` against a
`DateTime64(3)` key with `ORDER BY d::String` rendered six fractional digits for
both and could prune the row that matches.
The fallback now applies only when the target cannot be probed by
`castColumnAccurateOrNull` at all - the `String` -> `Dynamic` -> `String` case it
exists for. When the target can be probed and the cast still failed, the value
is simply not representable in the key column's type, so the atom is declined
and the caller reads more instead of pruning by a value the key space does not
hold.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Addressed: the direct-
|
…ime64-scale-117813
…` to `Dynamic` keys The fallback used to fire for every key type that `castColumnAccurateOrNull` cannot probe, which includes containers: for `ORDER BY arr::String` over `Array(DateTime64(3))`, the constant `[toDateTime64(..., 6)]` was rendered from its own scale and the part holding the row was pruned. Only a `Dynamic` key keeps the value in the constant's own type, so only there the direct `CAST` equals the normalized round trip; every other key type declines and the caller reads more instead. Adds test `05227_key_condition_datetime64_scale_array_cast_key` covering `Array` and `Tuple` keys with mismatched-scale constants. Co-Authored-By: Claude Fable 5.1 <[email protected]>
| @@ -0,0 +1,63 @@ | |||
| -- A `DateTime64` constant of a scale other than the column's must not change which parts and | |||
There was a problem hiding this comment.
These assertions are weaker than the contract in the header comment and PR description. If convertColumnForDeterministicDag regressed to return false for the mismatched-scale scalar case, every count() here would still pass by full-scanning the table, so we would stop testing that the normalized key condition is actually built.
Can we add at least one pruning assertion for the scalar path, e.g. SETTINGS force_primary_key = 1 on the ORDER BY d::String block and an EXPLAIN indexes = 1 check for the PARTITION BY d::String block, along the lines of 04846_tounixtimestamp_datetime64_key_pruning.sql?
Closes: #117813
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix rows silently dropped by index analysis when a
DateTime64column is compared with a constant of another scale and the sorting or partition key contains aCASTof that column, such asORDER BY d::String.Description
EXPLAIN indexes = 1showsCondition: (CAST(d, 'String') in ['2023-02-01 12:00:00.000000', ...])- six fractional digits, while every key value in the part has the three of the column's scale, so the range check concludes the value is outside the part and prunes it. UnderPARTITION BY d::Stringevery partition's minimum equals its maximum, so every mismatched-scale lookup loses its row.applyDeterministicDagToColumnpreferred a fast path that applies the key DAG'sCASTstraight to the constant's own type, skipping the normalizing round trip through the key column's type. That fast path exists for round trips that cannot be done safely (String->Dynamic->String), where the normalizing cast refuses the target and returns false - so the normalizing cast is tried first now and the direct cast stays as the fallback only for aDynamickey, the one type that keeps the constant in its own type so both renderings agree. For any other key type the normalizing cast cannot handle - containers such asArray(DateTime64(3))underORDER BY arr::String- the atom is declined and the reader reads more rather than pruning by a value in the wrong scale; test05227_key_condition_datetime64_scale_array_cast_keycoversArrayandTuplekeys.A constant that the key column's type cannot represent exactly (
toDateTime64('...12:00:00.000001', 6)against aDateTime64(3)column) still declines: no value of the column can equal it, which is also what the row comparison computes.Workflow [PR]
Sync PR [sync-upstream/pr/118688]
Version info
26.10.1.126(included in26.10and later)