Repository navigation
Fix NOT_FOUND_COLUMN_IN_BLOCK when the optimize_and_compare_chain budget runs out between copies of an alias - #125735
Conversation
…or not at all
`optimize_and_compare_chain_max_hash_work` is one budget per run of
`LogicalExpressionOptimizerVisitor`, and `tryOptimizeAndCompareChain` stops
once it is spent. The analyzer keeps separate copies of an alias expression
(the projection and the GROUP BY key, ORDER BY, ...), which the visitor
reaches at different times. When the budget ran out between two copies, one
of them got the derived conjuncts (e.g. `indexHint(number < 3)`) and the other
did not, their action names differed, and the planner failed with
`NOT_FOUND_COLUMN_IN_BLOCK`:
SELECT (number < (number + 1)) AND ((number + 1) < 3) AS r
FROM numbers(5) GROUP BY r
SETTINGS optimize_and_compare_chain_max_hash_work = 100
The same happens with the default budget on a large query whose WHERE
exhausts it between the projection and GROUP BY.
The derivation now runs as its own traversal before the other rewrites of
the pass. It records every `and` node it appended to (with the original
argument count and function), and if the budget was exceeded anywhere,
it restores all of them, so equal copies are rewritten the same way. The
main traversal no longer calls the compare chain; it still runs after the
derivation, so the conflict detector sees the derived conjuncts as before.
The budget keeps its unit and bounds the same hashing.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…erivation The first traversal now also runs the `JOIN ON` visitor at the join node, where the single traversal ran it before, so the derivation sees the rewritten `JOIN ON` again (including its common-expression extraction). The second traversal no longer runs the `JOIN ON` visitor. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the budget and revert comments; drop the sentence duplicated by the revert function's comment. 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 (engine: codex) plus my own cold review, two rounds; 5 findings.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261014-034100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261014-032200 |
|
cc @vdimir @novikd, could you review this? The |
|
Workflow [PR], commit [d866847] Summary: ✅
AI ReviewSummaryThis PR fixes Final Verdict✅ Approve. The focused regression test passed in Fast test. Additional local checks against that job's binary covered grouping, windows, joins, and nested subqueries; result comparisons and 4,422 rollback variants passed. |
Backport #125735 to 26.10: Fix `NOT_FOUND_COLUMN_IN_BLOCK` when the `optimize_and_compare_chain` budget runs out between copies of an alias
…optimize_and_compare_chain` budget runs out between copies of an alias
The flaky check runs a changed test 50 times on parallel workers that contend only with each other, and 05394 sends 368 statements per run. Its master version already averaged 131.7 s there (ClickHouse#125735), and the PR's copy failed 2 of 50 runs at 182-183 s with the other 48 averaging 126.9 s, so any PR touching it is a coin flip against the 180 s limit. On master it also averages 175.6 s per run in the `amd_msan` parallel job. `long` is the exemption `tests/clickhouse-test` documents for this case; the test now runs 5 times instead of 50 in the flaky check and is skipped by Fast test. 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):
Fixed
NOT_FOUND_COLUMN_IN_BLOCKwhen theoptimize_and_compare_chain_max_hash_workbudget ran out between two copies of an expression, for example aSELECTalias of anANDchain of comparisons that is reused inGROUP BY. The transitive comparisons ofoptimize_and_compare_chainare now derived for the whole query or not at all.Description
On 26.8.18.2 and 26.9.9.28,
SELECT (number < (number + 1)) AND ((number + 1) < 3) AS r FROM numbers(5) GROUP BY r SETTINGS optimize_and_compare_chain_max_hash_work = 100fails withCode: 10 NOT_FOUND_COLUMN_IN_BLOCKinstead of returning0and1. With the default budget a large query fails the same way when itsWHEREspends the budget (projection,WHEREandGROUP BYare visited in that order).The budget (#108757) is one counter per run of
LogicalExpressionOptimizerVisitor, and the compare chain stops once it is spent. The analyzer keeps separate copies of an alias (projection,GROUP BYkey, ...). When the budget ran out between them, only one copy got the derivedindexHint(number < 3), the action names differed and the planner could not find the key.The derivation now runs as its own traversal, after the
JOIN ONrewrites as before, and ahead of the other rewrites of the pass. It records eachandnode it appended to, and if the budget was exceeded it restores all of them, so equal copies stay equal. The other rewrites run afterwards, so the conflict detector still sees derived conjuncts. A query over the budget now gets no derived conjuncts at all.The new test sweeps the budget over 0..600 with the identifier resolve cache on and off; on master 20 to 31 of the 122 runs per shape fail. A 721-test batch of tests that use the optimization or
EXPLAINanAND, with random settings on and off, shows no new failure and no changed reference.EXPLAIN QUERY TREE, median of 7: 5000 disjuncts 0.87 s before and after; an over-budget query 5.26 s before, 4.75 s after.Related: #123925 (its review found this; a different root cause)
Related: #108757
Workflow [PR]
Sync PR [sync-upstream/pr/125735]
Version info
26.11.1.192-master(included in26.11and later)