Repository navigation
Fix std::out_of_range when a space-filling curve is not a key column - #114233
Conversation
KeyCondition::getAllSpaceFillingCurves iterates every action of the key-expression DAG and looked up each curve node in key_columns with at(). key_columns holds only the top-level key columns, so a mortonEncode or hilbertEncode call that sits in a non-key-column position of the key expression is an intermediate DAG node whose result_name is absent, and the lookup threw std::out_of_range out of KeyCondition's constructor. Such a table accepts CREATE and INSERT, then rejects almost every read with Code: 1001, and cannot be mutated (Code: 341); debug and sanitizer builds abort instead. Reproduced on 25.8.30.12 LTS, 26.6.3.28 and 26.7.4.14, so it is not new. Found by the AST fuzzer on an unrelated carrier PR. A curve description is only ever consumed through key_column_pos as a key column index, so a curve that is not a key column cannot participate in curve-aware key analysis at all. Restrict the enumeration to curve nodes that are key columns instead of catching the failed lookup. This was the only unconditional at() on key_columns in the file; the five other lookups already use find() with an end() check. Three construction sites reach that line and are all fixed: the primary and sorting key, the partition key, and minmax/set skip indexes. Nothing that worked before changes: for a plain curve key, and for a nested curve that is itself a key column, EXPLAIN indexes = 1 is byte-identical before and after, so curve pruning is preserved rather than silently downgraded.
…mization tag The source fix is unchanged. The test gains a fifth arm asserting that curve pruning still applies to a nested curve that is itself a key column: the three failure arms assert count() = 11, which an absent curve also returns via the safe-degradation paths, and the only previous pruning arm used a non-nested key, so an over-broad membership filter could have disabled curve pruning for the whole nested-but-valid class with every arm still green. The no-random-merge-tree-settings tag is replaced by an explicit index_granularity on the two EXPLAIN-arm tables. The arms assert presence of a FUNCTION_ARGS_IN_HYPERRECTANGLE element, which randomized granule counts and physical layout do not affect, so the remaining tables keep exercising randomized MergeTree settings on the read path this fix touches.
Internal second-model review (2 rounds, 0 open findings)An independent model reviewed this change cold against the repository review checklist, twice: once
Also checked and cleared: all four consumers of the curve list degrade safely on a reduced set Not addressed, disclosed for completeness: |
Pre-PR validation gate (click to expand)
|
|
cc @al13n321 @hanfei1991, could you review this? |
|
Workflow [PR], commit [f43c6db] Summary: ❌
AI ReviewSummaryThis PR fixes Final Verdict✅ No new issues found in the current patch. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 10/10 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units8 translation units recompiled, 47 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - f43c6dbEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Owner resolved from 175 jobs, 0 incomplete, 0 dropped, 155 green, 19 skipped. Not caused by this PR: the diff is confined to the space-filling-curve path in Session id: cron:our-pr-ci-monitor:20260811-060000 |
8d5be60
ReproducerCREATE TABLE t (x UInt32, y UInt32) ENGINE = MergeTree ORDER BY x PARTITION BY mortonEncode(x, y) % 4;
INSERT INTO t VALUES (5, 5);
SELECT count() FROM t WHERE x = 5;
-- Code: 1001. DB::Exception: std::out_of_range: map::at: key not found. (STD_EXCEPTION)
-- Same with ORDER BY mortonEncode(hilbertEncode(x, y), x, y). Expected: 1.Results:
CC component owner: @CurtizJ @alexey-milovidov Analysis metadata
Introducing change: not identified
|
|
@al13n321 could you apply Why that exact label: the version-specific form marks the oldest release to reach and expands to every newer active branch. Running Verification I ran, since the earlier triage comment could not check master:
Only the primary key path is user visible on a bare |
|
@groeneai @al13n321 — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
|
Confirmed real, and pre-existing rather than caused or changed by #114233. I hit this while working on #114233 and disclosed it in that PR's description as out of scope: for a skip index the Re-verified today on master at One correction to the open risks: the Iceberg site is not reachable. Not closing #115829, since the finding is not wrong. One note for whoever fixes it: the guard needs to be narrow enough to leave primary-key curve pruning alive. On master today |
…oss-type-set-exactness-nonnullable The sole conflict is the accurateCastOrNull target guard in tryPrepareSetColumnsForIndex. Master's side is taken and this branch's local setIndexKeyTypeSupportsAccurateCastOrNull is deleted. Master reached the same fix through ClickHouse#115152 and ClickHouse#114233, which export canBeAccurateCastOrNullTarget from CastOverloadResolver.h and call it at three guards. That is what the local helper's own commit message asked for: the throwing validator and the predicate now share one implementation instead of mirroring each other by hand, so they cannot drift. The two forms are not equivalent and master's is narrower. For a Nullable key type the local helper recursed into the nested type and could proceed, while master keeps the outer canBeInsideNullable test, which DataTypeNullable does not override and therefore answers false. Master declines there, exactly as the pre-fix guard did, so taking master's side removes a widening this branch had introduced rather than losing coverage. The five predicates carrying this PR's contract are untouched and still absent from master.
Backport #114233 to 26.7: Fix std::out_of_range when a space-filling curve is not a key column
…e-filling curve is not a key column
Backport #114233 to 26.6: Fix std::out_of_range when a space-filling curve is not a key column
Backport #114233 to 26.3: Fix std::out_of_range when a space-filling curve is not a key column
…e-filling curve is not a key column
…e-filling curve is not a key column
STOR: mortonEncode/hilbertEncode inside a key expression -> exc std::out_of_range. 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/114233.PROD263.txt). Upstream patch unchanged, with its test(s): 04860_space_filling_curve_not_a_key_column. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
std::out_of_range: map::at: key not foundwhen aMergeTreekey expression contains amortonEncodeorhilbertEncodecall that is not itself a key column, for exampleORDER BY mortonEncode(hilbertEncode(x, y), x, y)orPARTITION BY mortonEncode(x, y) % 4.Description
KeyCondition::getAllSpaceFillingCurveswalks every action of the key-expression DAG and looks up each curve node inkey_columnswithat().key_columnsholds only top-level key columns, so a curve nested inside another expression is an intermediate node whoseresult_nameis absent, and the lookup throws out ofKeyCondition's constructor.Such a table accepts
CREATEandINSERT, then rejects almost every read. On released26.7.4.14withORDER BY mortonEncode(hilbertEncode(x, y), x, y),SELECT *,SELECT count() WHERE x = 5andSELECT sum(y)all fail withCode: 1001andALTER TABLE ... DELETEwithCode: 341; only a barecount()works. Debug and sanitizer builds abort instead. It reproduces on25.8.30.12(LTS),26.6.3.28and26.7.4.14, so it is not new.A curve is only ever consumed through
key_column_posas a key column index, so one that is not a key column cannot participate in curve-aware key analysis at all. Rather than catch the failed lookup, the fix restricts the enumeration to curve nodes that are key columns, using thekey_columns.findform already used at the five other lookups here. Line 1386 was the file's only unconditionalat()onkey_columns.Three construction sites reach that line and are all fixed: the primary/sorting key, the partition key, and minmax/set skip indexes. Nothing that worked before changes: for
ORDER BY mortonEncode(x, y), and for a nested curve that is also a key column,EXPLAIN indexes = 1is byte-identical before and after, so curve pruning is preserved.Found by the AST fuzzer on an unrelated PR that does not touch this code: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113022&sha=03db3c42bad3adf593a621b427aa706d7d81e966&name_0=PR&name_1=AST%20fuzzer%20%28amd_tsan%29. No related open issue found.
Not addressed here:
INDEX i mortonEncode(x, y) TYPE minmax, where the curve is the index key column, failsCode: 10 NOT_FOUND_COLUMN_IN_BLOCKboth before and after. There the lookup succeeds and the failure is downstream, a different defect.Version info
26.8.1.1727(included in26.8and later)26.7.6.30,26.6.5.71,26.3.33.22