Repository navigation
Fix NOT_FOUND_COLUMN_IN_BLOCK in ConvertOrLikeChainPass for a LIKE chain over an alias that contains another chain - #129177
Conversation
The pass rewrote `or` chains top-down and deep-copied the `like` branches into the `indexHint` payload (and the rewritten `or` into the `and`) at the moment the chain was entered. A haystack shared through the identifier resolve cache could then be copied by one reference before another reference (e.g. `WHERE h != ''`) rewrote a chain inside it in place, and copied again afterwards by a third (the separately resolved GROUP BY key). `indexHint` payloads are never revisited, so the projection and the GROUP BY key ended up with different `indexHint` arguments, and the planner failed with NOT_FOUND_COLUMN_IN_BLOCK after aggregation. Rewriting in `leaveImpl` makes every descendant final before any copy is taken, so equal chains get equal payloads regardless of clause order. Closes ClickHouse#126788 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Rewriting bottom-up made every `indexHint` payload and executed `or` copy hold the already rewritten inner chains, so nested alias haystacks grew about 5x per level: 5 nested `WITH` levels, each an OR-LIKE chain on the previous alias, went from 1.10 s to 70.95 s on queries that work on master. Keep the top-down visit and all its decisions, but only record each chain's new arguments and `indexHint` node during it, then apply them in three steps over all chains: set the arguments of every recorded `or`, copy every `or` that gets an `indexHint`, then wrap each of them into `and(<copy>, indexHint(<payload>))` in place. Payload copies are taken while the tree is unmodified, so they are always the query as written, and every executed copy is taken in the same state. Chains sharing a haystack through the identifier resolve cache therefore get equal copies in any clause order. Trees without a nested chain are unchanged; master's existing blow-up when each alias is referenced before its chain (75 s at 5 levels) is gone as well (1.1 s). Closes ClickHouse#126788 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copying every rewritten `or` before wrapping any of them left the copy of a nested haystack inside the outer chain without the inner `indexHint` wrapper, so its action name differed from the live haystack and `... GROUP BY h` with a chain over `h` failed with NOT_FOUND_COLUMN_IN_BLOCK. Record each chain in post-order (`leaveImpl`, still without changing the tree) and copy and wrap the chains in that order, so a chain is copied only after the chains inside it are final. Payloads are still copies of the query as written. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The old pattern matched the outer chain's name whether or not the chain inside the haystack was rewritten, so a regression that skips the inner rewrite kept the guard green. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (3 rounds)
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261104-040800 |
|
Workflow [PR], commit [76b2e2c] Summary: ✅
AI ReviewSummaryReviewed #129177 at Final VerdictStatus: ✅ Approve |
|
cc @yakov-olkhovskiy @vdimir, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline. |
|
@fm4v who should review this? |
2f06a79
…roup-by-key Resolve the conflict with ClickHouse#129177 in ConvertOrLikeChainPass.cpp. Master now collects the rewrites in leaveImpl and applies them in applyRewrites after the whole visit, so: - the GROUP BY key set is still pushed in enterImpl of each QueryNode and popped in its leaveImpl, and the OR collection loop in leaveImpl keeps a key branch as written; - addCopiesOfNodes is dropped: it tracked the copies made by the indexHint wrap's node->clone(), which the visitor used to descend into. The clone now happens in applyRewrites, after the visit, so the visitor only ever sees the original nodes, which collectGroupByKeyPatternNodes records. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #126788
Related: #124480
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
NOT_FOUND_COLUMN_IN_BLOCKfor a query that groups by an alias of anORchain ofLIKEconditions whose haystack is another alias containing such a chain, when that inner alias is also used elsewhere, for exampleWITH if(s LIKE '%a%' OR s LIKE '%b%' OR s LIKE '%c%' OR s LIKE '%1%', s, 'z') AS h SELECT (h LIKE '%a%' OR h LIKE '%b%' OR h LIKE '%c%' OR h LIKE '%1%') AS r, count() FROM t WHERE h != '' GROUP BY r. Closes #126788.Description
On 26.10.2.103, the query from the changelog entry fails with
Code: 10 NOT_FOUND_COLUMN_IN_BLOCKon default settings (also on 26.8, 26.9 and master).Root cause: with
enable_identifier_resolve_cachethe projection'sr,WHERE h != ''and the separately resolvedGROUP BY rshare one haystack nodeh.ConvertOrLikeChainPassrewrites each chain in place during its top-down visit and copies the chain into theindexHintpayload (and the executedor) at that moment. The projection copieshwhile its inner chain is still raw,WHEREthen rewrites that inner chain in place, andGROUP BY rcopies it rewritten.indexHintarguments are never revisited, so the key and the projection get different names and the planner cannot find the projection after aggregation.The change: the visit only records each chain's rewrite, in post-order. After it, the pass sets the new arguments of every chain, then copies and wraps each chain in that order, so a chain is copied after the chains inside it are final. Payloads are copies of the query as written, so equal chains get equal payloads in any clause order.
Validation: 5 new cases in
05346_optimize_or_like_chain_shared_alias(WHERE, a latermax(h),Nullable,LowCardinality, three levels) fail on master and pass with the fix, also 50 times with random settings. One more case (GROUP BY h) and anEXPLAINcheck that both nested chains are rewritten guard the change.EXPLAIN QUERY TREEandEXPLAIN actionsare byte-identical to master for 13 chain shapes without a nested chain. Nested aliases get cheaper: five levels, each alias referenced before its chain, took 75.5 s on master and 1.2 s now.Workflow [PR]
Sync PR [sync-upstream/pr/129177]
Version info
26.11.1.2702-master(included in26.11and later)