Repository navigation
Fix NOT_FOUND_COLUMN_IN_BLOCK when an alias of an OR chain of LIKE conditions is reused by GROUP BY and other clauses - #124480
Conversation
…LikeChainPass With enable_identifier_resolve_cache the GROUP BY key and its ORDER BY / HAVING references are one `or` node. ConvertOrLikeChainPass rewrote that node in place and then replaced only the slot it reached first with and(<rewritten or>, indexHint(<original chain>)); the other reference was skipped by processed_nodes and kept the bare rewritten `or`, so the key and ORDER BY had different names and the planner asked for the source column after aggregation (Code 10). Record the slot replacement and put the same `and` node at every later reference of the replaced node in needChildVisit. The map is keyed by the node pointer itself (not structural hash), so the projection's own copy is not conflated with the shared node, and it keeps replaced nodes alive for the pass. A tree without a shared node is unchanged. 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; 2 findings) plus my own cold review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261007-195400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261007-191300 |
|
Workflow [PR], commit [c55750d] Summary: ✅
AI ReviewSummaryReviewed #124480 at Final VerdictStatus: ✅ Approve |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
|
@groeneai 🕵️ The fix works: Could the shared node become the
As a bonus, the node keeps its alias, which |
…nPass As suggested in review: instead of replacing the slot with a new and(<rewritten or>, indexHint(<original chain>)) node and replaying that replacement at later references through a map, turn the OR node itself into the AND. Every reference to a shared node, including ones the visitor never reaches (under an indexHint parent), then sees the same expression, and the map and the needChildVisit redirect are not needed. Add two test arms: a reference under indexHint, and ORDER BY ALL with enable_identifier_resolve_cache = 0, where ORDER BY shares the projection node. Both fail on master with Code 10. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, done in c55750d: the
Queries without a shared node get the same query tree as before ( |
| /// Replace the original node with the AND expression | ||
| node = std::move(and_node); | ||
| /// Mark the cloned OR as processed to prevent re-visiting | ||
| processed_nodes.insert(optimized_or_clone->as<FunctionNode>()); |
There was a problem hiding this comment.
@groeneai are you sure no performance regressions are going to be created from this change?
There was a problem hiding this comment.
I measured it with the CI release builds: this PR's merge build (579bfca2f69) against master 16add56e8d1, which differs from the merge parent only in docs and tests, so this PR is the only source difference. 30M-row MergeTree table, 16 query shapes that the pass rewrites (to multiSearchAny and to multiMatchAny): the chain in WHERE, PREWHERE or the projection, and as an alias shared by WHERE, ORDER BY, GROUP BY, HAVING and two aggregates.
EXPLAIN actions = 1, indexes = 1is byte-identical to master for 11 of 16 shapes. Of the other 5, the 3 GROUP BY shapes fail on master withNOT_FOUND_COLUMN_IN_BLOCK; in the 2WHERE r ORDER BY rshapes the sort key becomes(...) AND indexHint(), the column the WHERE filter already computes.- For all 16 shapes the plan is byte-identical to master's plan with
enable_identifier_resolve_cache = 0, so the default now gets the plan the cache-off path always had. At executionindexHint()returns a constant1without evaluating the chain (FunctionIndexHint::executeImpl). - Median of 45 runs each,
max_threads = 8, master / PR, ms:
| shape | multiSearchAny |
multiMatchAny |
|---|---|---|
| WHERE, not shared | 147 / 153 | 196 / 192 |
WHERE r ORDER BY r |
236 / 236 | 306 / 308 |
two aggregates on r |
152 / 154 | 195 / 194 |
| PREWHERE | 174 / 175 | |
GROUP BY r (master with the cache off) |
158 / 154 | 198 / 201 |
Every pair is within 3% either way except the first, whose plan is byte-identical: two re-runs (one with the binary order swapped) read 145 / 147 and 145 / 146. Analysis of a query with 300 chains: 55 / 55 ms.
- In this PR's Performance Comparison (arm_release) the 10
optimize_or_like_chain_hitsqueries are unchanged. The onlyslowerflag isand_function #0, which has noOR(the pass returns at its first check) and was flaggedsloweron 218 other PRs in the last 30 days.
So I see no regression.
| processed_nodes.insert(optimized_or_clone->as<FunctionNode>()); | ||
|
|
||
| /// Rewrite the node in place to optimized_or AND indexHint(original_like_chain), as it may be shared by several references. | ||
| function_node->getArguments().getNodes() = {std::move(optimized_or_clone), std::move(index_hint_node)}; |
There was a problem hiding this comment.
@novikd maybe you can help here a little? This change is difficult for me to understand. The comment in the original code mentions indexHint, while the PR is to fix LIKE expressions?
There was a problem hiding this comment.
The indexHint is part of the LIKE rewrite itself. Since #94517, optimize_or_like_chain (on by default) rewrites s LIKE '%a%' OR s LIKE '%b%' OR ... into multiSearchAny(s, ['a', 'b', ...]) AND indexHint(s LIKE '%a%' OR s LIKE '%b%' OR ...). multiSearchAny is what gets executed; indexHint keeps the original LIKE conditions visible to primary key and skip index analysis and returns a constant 1 at execution (comment in the pass).
The bug is in how that AND was attached. With enable_identifier_resolve_cache, the r in GROUP BY r ORDER BY r is one shared or node, and the old code put the new and node only into the reference it visited first. EXPLAIN QUERY TREE dump_ast = 1 on master for SELECT (s LIKE '%a%' OR s LIKE '%b%' OR s LIKE '%c%' OR s LIKE '%1%') AS r FROM t GROUP BY r ORDER BY r:
GROUP BY and((multiSearchAny(__table1.s, _CAST(['a', 'b', 'c', '1'], 'Array(String)')) OR 0), indexHint(or(__table1.s LIKE '%a%', ...)))
ORDER BY (multiSearchAny(__table1.s, _CAST(['a', 'b', 'c', '1'], 'Array(String)')) OR 0) ASC
The ORDER BY expression is no longer the GROUP BY key, so the planner tries to compute it from __table1.s after aggregation, which gives NOT_FOUND_COLUMN_IN_BLOCK. With enable_identifier_resolve_cache = 0, ORDER BY gets the full and(..., indexHint(...)) like GROUP BY, and the query works.
With this PR the shared or node itself becomes the and (your suggestion from yesterday), so SELECT, GROUP BY and ORDER BY all get ... AND indexHint(...).
|
Good to merge |
Backport #124480 to 26.10: Fix `NOT_FOUND_COLUMN_IN_BLOCK` when an alias of an `OR` chain of `LIKE` conditions is reused by `GROUP BY` and other clauses
…ias of an `OR` chain of `LIKE` conditions is reused by `GROUP BY` and other clauses
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 ofLIKE/ILIKEconditions and reuses that alias inORDER BYorHAVING, for exampleSELECT (s LIKE '%a%' OR s LIKE '%b%' OR s LIKE '%c%' OR s LIKE '%1%') AS r FROM t GROUP BY r ORDER BY r.Description
On 26.8.2.7,
SELECT (s LIKE '%a%' OR s LIKE '%b%' OR s LIKE '%c%' OR s LIKE '%1%') AS r FROM t GROUP BY r ORDER BY rfails withCode: 10 NOT_FOUND_COLUMN_IN_BLOCKon default settings (also on 26.7 and master).With
enable_identifier_resolve_cache(default since 26.6) theGROUP BYkey and itsORDER BY/HAVINGreferences are one sharedornode.ConvertOrLikeChainPassrewrites that node in place intomultiSearchAny(...)and then replaces only the reference it reached first withand(<rewritten or>, indexHint(<original chain>))(theindexHint, added in #94517, keeps theLIKEconditions visible to index analysis). The other reference keeps the bare rewrittenor, so the key andORDER BYdiffer by name and the planner looks for__table1.safter aggregation.The change: the pass turns the
ornode itself intoand(<rewritten or>, indexHint(<original chain>))in place, so every reference to a shared node sees the same expression, including references the visitor does not descend into (underindexHint). A query without a shared node gets an identical query tree (EXPLAIN QUERY TREEbyte-identical with the cache off).The new test covers 11 shapes (
ORDER BY,HAVING,ILIKE,multiMatchAny,NullableandLowCardinalityhaystacks, two key groups in one chain,WITH ROLLUP, a chain nested in the haystack of another, a reference underindexHint, andORDER BY ALLwith the cache off, which shares the projection node); all 11 fail on master and pass with the fix, also with random settings. The existingoptimize_or_like_chainandindexHint-consuming index tests are unchanged.Related: #122404, same class as #108553. Same issue in other passes, separate PRs: #123925, #122512, #122726.
Workflow [PR]
Sync PR [sync-upstream/pr/124480]
Version info
26.11.1.221-master(included in26.11and later)