Repository navigation
Use many key columns per condition for pruning - #96225
Conversation
|
Workflow [PR], commit [975594c] Summary: ✅
AI ReviewSummaryThis PR lets one predicate constrain multiple key expressions, controlled by Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block. Prevent the five unsafe inferences before enabling this behavior by default, cover their reproductions, and retain performance coverage for redundant derived-key constraints. No existing thread was replied to, resolved, or reopened. |
|
The The bug was reverted by PR #102247 (merged at ~14:48 UTC). All nessie/lakekeeper test failures between these two merges are unrelated to this PR. |
|
Hi team, this would be a great feature to have. Not sure if it is a heavy lifting, but adding my +1. |
|
📊 Cloud Performance Report
No query in either benchmark has a verdict that should block this PR. Only clickbench Q33 (-14.3%) and Q34 (-13.7%) were flagged, and both stay not_sure because master's own timings for them jumped during the comparison window. Both are full-scan GROUP BY queries with no WHERE filter, so they never run the code this PR changes: primary-key analysis that lets one condition narrow several key columns. Their lower CPU time (about 16.2 s to 14.4 s) follows master's shift rather than anything in the diff, and tpch_adapted_1_official shows no changes. clickbenchFlagged queries (2 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
Acknowledged. I'm going through the remaining review findings to confirm which ones reproduce on master with a single key column query; I'll follow up here with the classification and a PR that fixes them. |
|
I checked the threads that are not marked fixed here against master (
The Single-key repros (comment: master result, full scan result)SET use_query_condition_cache = 0;
-- toDate wrap
CREATE TABLE t_date (ts DateTime64(3, 'UTC')) ENGINE = MergeTree ORDER BY toDate(ts) SETTINGS index_granularity = 1;
INSERT INTO t_date VALUES ('1969-12-31 12:00:00'), ('1970-01-01 12:00:00');
SELECT count() FROM t_date WHERE ts <= '1970-01-02 00:00:00'; -- 1, 2
-- DateTime64 scale
CREATE TABLE t_scale (x DateTime64(6, 'UTC')) ENGINE = MergeTree ORDER BY x SETTINGS index_granularity = 1;
INSERT INTO t_scale SELECT toDateTime64('2026-01-01 00:00:00.123456', 6, 'UTC') FROM numbers(8);
SELECT count() FROM t_scale WHERE x IN (SELECT toDateTime64('2026-01-01 00:00:00.123', 3, 'UTC')); -- 0, 8
-- FixedString in a tuple
CREATE TABLE t_pad (t Tuple(String, UInt8)) ENGINE = MergeTree ORDER BY cityHash64(t) SETTINGS index_granularity = 1;
INSERT INTO t_pad SELECT (if(number % 2, 'abc', 'abc\0'), 1) FROM numbers(512);
SELECT count() FROM t_pad WHERE t = (toFixedString('abc', 8), toUInt8(1)); -- 257, 512
-- named tuple
CREATE TABLE t_named (t Tuple(b UInt8, a UInt8)) ENGINE = MergeTree ORDER BY cityHash64(t) SETTINGS index_granularity = 1;
INSERT INTO t_named SELECT (1, 2) FROM numbers(8);
SELECT count() FROM t_named WHERE t = CAST((1, 2), 'Tuple(a UInt8, b UInt8)'); -- 0, 8
-- date-time text in a tuple
SET session_timezone = 'UTC';
CREATE TABLE t_zone (t Tuple(DateTime, UInt8)) ENGINE = MergeTree ORDER BY cityHash64(t) SETTINGS index_granularity = 1;
INSERT INTO t_zone SELECT (toDateTime('2026-01-01 12:00:00', 'America/New_York'), 1) FROM numbers(8);
SET session_timezone = 'America/New_York';
SELECT count() FROM t_zone WHERE t = ('2026-01-01 12:00:00', toUInt8(1)); -- 0, 8 |
|
|
|
@yariks5s Thanks for the thorough review. I think it's safe to keep it enabled by default because existing master bugs are either very rare edge case or known limitation. |
|
The fix for the |
…rapping-todate-tuple-text-timezone Resolve the conflict with ClickHouse#96225 in `KeyCondition.cpp`: the zone-less date-time text normalization now takes the constant as a `ColumnWithTypeAndName`. The tuple recursion moves into a `Field`-level helper, and the column is rebuilt only when a text element was converted, so other constants keep their original columns as on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ments
The comparison pairs tuple elements by position, while the cast of a constant
to the key's tuple type pairs two named tuples by name. Converting the text
elements of such a constant by position made that cast succeed on swapped
values: with `t Tuple(d DateTime, n UInt8)` and
`t = CAST(('2026-01-01 12:00:00', 1), 'Tuple(n String, d UInt8)')` the index
skipped all 8 matching rows, and a nested `Nullable(Tuple)` element made the
cast throw `TYPE_MISMATCH`, where master declines the transform because the
text does not parse as `UInt8`. A tuple level whose text was converted and whose
names differ from the key's is now declined, so the constant pairs with the key
as on master; named constants with the key's own names are still normalized.
05402 covers a same-order, a reordered and a nested reordered named constant.
A named constant without text whose names reorder the key's elements is left
as it is: its name-mapped cast is the separate finding of ClickHouse#96225 that ClickHouse#123868
addresses.
Found by the second-model review of the merge with master.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ues apart `keyToSetConversionMayCollapse` is now fail-closed: the index uses a set only for conversions of the key into the set's type that are known to keep distinct key values apart (the same type, integers and floats, widenings, element-wise composites); every other pair declines. This also covers a number key against an `Enum` set: the lookup cast has no context, so it uses `static_cast`, and an `Int64` key holding 257 matches an `Enum8` element of value 1. The 05331 and 03991 expectations are taken from Nihal Z. Miaji's 2818b87 and 0efe8bc on ClickHouse#96225. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@nihalzp The |
04330 and 05245 (added by ClickHouse#96225) create tables with an alias in ORDER BY, which this PR now rejects at CREATE ("Alias 'd' / 'bucket' is not allowed in ORDER BY"); this kicked the PR out of the merge queue. 05245: drop `AS bucket`. The alias is not referenced anywhere, and the reference is unchanged on master and on this branch. 04330: delete the `test_det_alias` case. Its subject is an ALIAS node in the key expression, which a new table can no longer have; without the alias it is the `ORDER BY (toDate(ts), ts)` shape already covered by other multi-atom tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
If we have,
Then, for query such as:
Currently, we use only one primary key column:
PR will use all the keys applicable:
The behavior is controlled by the new setting
analyze_index_with_multiple_key_columns_per_condition(enabled by default). When it is disabled, each condition constrains at most one key column, matching the scope of the analysis before this change.Closes: #33056
Closes: #99960
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Allow a single condition to constrain multiple key columns during index analysis. For example, if the primary key is
(toYYYYMMDD(dt), toDate(dt), dt)and the query hasWHERE dt >= toDateTime('2026-01-10 00:00:00'), all three key columns are now used for pruning, instead of only one of them as before. This behavior is controlled by the settinganalyze_index_with_multiple_key_columns_per_condition(enabled by default).Documentation entry for user-facing changes
Workflow [PR]
Sync PR [sync-upstream/pr/96225]
Version info
26.10.1.2454-master(included in26.10and later)