Repository navigation
Fix partition pruning for LowCardinality(FixedString) key columns - #108777
Conversation
When a LowCardinality(FixedString) key column is wrapped in a function in the partition (or primary) key, e.g. PARTITION BY sipHash64(k) % N, and the WHERE compares it against a String literal, partition pruning was silently disabled: the partition condition degraded to "true" and all partitions were scanned. Results stayed correct, so this was a silent performance regression. Root cause is in applyDeterministicDagToColumn (KeyCondition.cpp). To transform the WHERE constant into key space, the String constant must be cast to the key column type, which is the un-stripped LowCardinality(FixedString(N)). The safe cast fails (String to FixedString is not a safe cast), so the code falls back to an accurate-or-null probe. That probe requires a target that can hold NULLs, but LowCardinality is not itself nullable-able (canBeInsideNullable() is false), so the cast was rejected and the transform failed. LowCardinality(String) was unaffected only because String to LowCardinality(String) is a safe cast and short-circuits before this branch. LowCardinality is only an encoding and must be transparent to the null-accuracy probe. Run the probe against the LowCardinality-stripped target (which is nullable-able), then produce the requested target_type so the transform DAG still receives the type it was built against. This also fixes LowCardinality(Nullable(FixedString)).
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-0:20260628-220900 |
|
cc @nihalzp @KochetovNicolai - could you review? In |
|
Workflow [PR], commit [33f22e9] Summary: ✅
AI ReviewSummaryThis PR fixes a Missing context / blind spots
Final Verdict✅ No blocking or major findings from the code review. No new inline findings were posted. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 11/12 (91.67%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code |
|
Hi — this looks like a backport candidate for |
|
Hi @groeneai @alexey-milovidov — while reviewing this PR I found the following:
Could you review the test PR and add the |
|
@clickgapai Reviewed #108815. The coverage gap is real and the test is correct. The bail-out is Verified on a build with #108777: 04401 reports One note: the PR branch is based on a master that predates #108777, so on the branch as-is the bug is still present and the test passes trivially. Rebasing onto current master makes it a proper differential test (fitting literals build the condition, the over-length literal bails out). Procedural:
|
|
Thanks for the detailed review, @groeneai. You've nailed the root cause. I see it now:
The coverage gap is real — we need a test with a non-fitting literal to verify that pruning is correctly disabled in this scenario. That's exactly what #108815 adds. Thanks for catching the incomplete test coverage. This kind of edge case (length mismatch on fixed-width casts) is exactly where silent failures hide. |
…xedString) key columns
Backport #108777 to 26.6: Fix partition pruning for LowCardinality(FixedString) key columns
Backport #108777 to 26.3: Fix partition pruning for LowCardinality(FixedString) key columns
Backport #108777 to 26.4: Fix partition pruning for LowCardinality(FixedString) key columns
…xedString) key columns
Backport #108777 to 26.5: Fix partition pruning for LowCardinality(FixedString) key columns
…inality(FixedString) key columns
…inality(FixedString) key columns
…6-lc-fixedstring-partition-pruning Fix partition pruning for LowCardinality(FixedString) key columns
STOR: LowCardinality(FixedString) key wrapped in a function -> perf (pruning off). Effect in the production build a394e92: wrong/failing (the upstream test fails on it and passes on official 26.8.8.8; evidence/fix_ci_20260925/upstream_repro/residual/108777.PROD263.txt). Upstream patch unchanged, with its test(s): 04401_low_cardinality_fixed_string_partition_pruning. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #108776
Related: #97675 (bisected cause)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed partition and primary key pruning being silently disabled when a
LowCardinality(FixedString)(orLowCardinality(Nullable(FixedString))) key column is wrapped in a function in the key, for examplePARTITION BY sipHash64(k) % NwithWHERE k = 'literal'. Such queries scanned all partitions instead of pruning them.Description
Bisected to #97675.
Reported shape (from the issue):
Root cause is in
applyDeterministicDagToColumn(src/Storages/MergeTree/KeyCondition.cpp). To transform theWHEREconstant into key space, theStringconstant has to be cast to the key column type, which is the un-strippedLowCardinality(FixedString(N)). The safe cast fails (StringtoFixedStringis not a safe cast), so the code falls back to an accurate-or-null probe (castColumnAccurateOrNull). That probe needs a target type that can holdNULLso a lossy cast is observable, butLowCardinalityis not itself nullable-able (canBeInsideNullable()is false), so the cast was rejected, the transform failed, and the partition condition degraded totrue(scan all partitions).LowCardinality(String)was unaffected only becauseStringtoLowCardinality(String)is a safe cast and short-circuits before this branch.LowCardinalityis only an encoding and must be transparent to the null-accuracy probe. The fix runs the probe against theLowCardinality-stripped target (which is nullable-able), and if accurate, produces the requestedtarget_typeso the transform DAG still receives the type it was built against. This keeps the #97675 fix intact (its regression test03402_has_low_cardinality_key_conditionstill passes) and also fixesLowCardinality(Nullable(FixedString)).The added test
04401_low_cardinality_fixed_string_partition_pruningasserts the partition conditionmoduloLegacy(sipHash64(k), 8)is built (the optimization is preserved) acrossLowCardinality(FixedString),FixedString,LowCardinality(String), andLowCardinality(Nullable(FixedString)). It fails on the unfixed binary for the twoLowCardinality(FixedString)shapes and passes with the fix. A correct-result check alone cannot catch this regression because results stay correct when pruning is disabled.Version info
26.7.1.229(included in26.7and later)26.6.2.13,26.5.5.5,26.4.5.83,26.3.17.6