Skip to content

Fix NOT_FOUND_COLUMN_IN_BLOCK in ConvertOrLikeChainPass for a LIKE chain over an alias that contains another chain - #129177

Merged
vdimir merged 4 commits into
ClickHouse:masterfrom
groeneai:fix-or-like-chain-shared-haystack-index-hint
Nov 18, 2026
Merged

vdimir merged 4 commits into
ClickHouse:masterfrom
groeneai:fix-or-like-chain-shared-haystack-index-hint

Conversation

@groeneai

@groeneai groeneai commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #126788
Related: #124480

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed NOT_FOUND_COLUMN_IN_BLOCK for a query that groups by an alias of an OR chain of LIKE conditions whose haystack is another alias containing such a chain, when that inner alias is also used elsewhere, for example WITH 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_BLOCK on default settings (also on 26.8, 26.9 and master).

Root cause: with enable_identifier_resolve_cache the projection's r, WHERE h != '' and the separately resolved GROUP BY r share one haystack node h. ConvertOrLikeChainPass rewrites each chain in place during its top-down visit and copies the chain into the indexHint payload (and the executed or) at that moment. The projection copies h while its inner chain is still raw, WHERE then rewrites that inner chain in place, and GROUP BY r copies it rewritten. indexHint arguments 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 later max(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 an EXPLAIN check that both nested chains are rewritten guard the change. EXPLAIN QUERY TREE and EXPLAIN actions are 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

  • Merged into: 26.11.1.2702-master (included in 26.11 and later)

groeneai and others added 4 commits November 4, 2026 02:41
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]>
@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (3 rounds)
  • ❌ AGREE, fixed: copying every executed chain before wrapping any of them broke GROUP BY h / HAVING on the haystack; chains are now copied and wrapped in post-order (GROUP BY h case added).
  • ⚠️ AGREE, fixed: the EXPLAIN check now requires both nested chains to be rewritten and wrapped (it failed to notice an unrewritten inner chain).
  • 💡 AGREE, fixed: visible Closes/Related lines; the description says payloads (not every copy) are equal in any clause order.
  • Final round: no findings.

@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The issue's query (WITH if(<4 LIKE on s>, s, 'z') AS h SELECT (<4 LIKE on h>) AS r, count() FROM t WHERE h != '' GROUP BY r) fails with Code: 10 NOT_FOUND_COLUMN_IN_BLOCK on every run on master at default settings; all 5 new 05346 cases fail.
b Root cause explained? Through the identifier resolve cache, the projection, WHERE h != '' and the GROUP BY key share one haystack node. The pass rewrites chains in place during its top-down visit and copies them (indexHint payload, executed or) at the same moment, so the projection copies the inner chain raw and the key copies it rewritten. indexHint is never revisited, the names differ, and the projection is missing after aggregation.
c Fix matches root cause? Yes. The visit only records each chain's rewrite, in post-order, and changes nothing. 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. No guard, setting or test weakening.
d Test intent preserved / new tests added? 6 cases and one EXPLAIN check added to 05346_optimize_or_like_chain_shared_alias (additions only): WHERE, a later max(h), Nullable, LowCardinality, three levels, GROUP BY h; the check asserts that both nested chains are rewritten and wrapped. No existing test changed.
e Both directions demonstrated? Master: 5/5 Code: 10, runner FAIL. Fix: the 9 reference lines, the GROUP BY h case and the EXPLAIN check pass, runner OK, 50/50 with random settings. The check returns 0 when the inner chain is not rewritten (3 LIKE, below optimize_or_like_chain_min_substrings).
f Fix is general across code paths? All copy sites of the pass are covered: payload clone, executed or clone, in-place wrap of a shared node (#124480), and non-pure chains. The two other passes that build an indexHint (LogicalExpressionOptimizerPass, ConvertQueryToCNFPass) build it from new nodes, not from copies of a subtree that the same visit rewrites later.
g Fix generalizes across inputs (params/datatypes/wrappers)? String, Nullable(String) and LowCardinality(String) haystacks, 3 nesting levels, GROUP BY and HAVING on the haystack itself, ILIKE, multiMatchAny, PREWHERE, WITH ROLLUP + group_by_use_nulls, identifier cache on and off. Non-String haystacks keep the original branches, as before.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change. For 13 non-nested chain shapes, and for nested shapes where the chain comes first, EXPLAIN QUERY TREE and EXPLAIN actions are byte-identical to master. Other nested shapes have the same executed tree as master and return the same results as with optimize_or_like_chain = 0.
i Invariants and contracts preserved? Each chain is copied only after every chain inside it is final, and each indexHint payload is the query as written. The shared or node is still rewritten in place, and each node is recorded once (processed_nodes). An exception during the visit leaves the tree unmodified. Index analysis is unchanged: same granule counts on an ngrambf_v1 table.

Session id: cron:clickhouse-impl-slot-40:20261104-040800

@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [76b2e2c]

Summary: ✅


AI Review

Summary

Reviewed #129177 at 76b2e2cac76da0e6242d08019c732e0a1d7ec13f. The change fixes NOT_FOUND_COLUMN_IN_BLOCK for nested, shared OR LIKE aliases by recording rewrites before mutating the tree and copying enclosing chains after their descendants are finalized. No actionable findings. Using the PR's CI binary, the shared-alias regression test matches its reference, and 400 comparisons covering mixed chains, nullable and low-cardinality haystacks, grouping, lambdas, aggregates, and subqueries produce identical results with optimization enabled and disabled.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 4, 2026
@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

cc @yakov-olkhovskiy @vdimir, could you review this and add the can be tested label so CI runs? ConvertOrLikeChainPass copied each chain into its indexHint payload during the top-down visit, so with a haystack shared through the identifier cache the projection copied an inner chain before another reference rewrote it in place and the GROUP BY key after; the visit now only records the rewrites and applies them afterwards in post-order, so a chain is copied only after the chains inside it are final.

@clickhouse-gh clickhouse-gh Bot added comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). can be tested Allows running workflows for external contributors labels Nov 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 76b2e2cac with master 7f11488e0 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline.

Job report

@PedroTadim

Copy link
Copy Markdown
Member

@fm4v who should review this?

@vdimir vdimir self-assigned this Nov 18, 2026
@vdimir
vdimir added this pull request to the merge queue Nov 18, 2026
Merged via the queue into ClickHouse:master with commit 2f06a79 Nov 18, 2026
546 of 550 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 18, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 18, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

indexHint snapshot of a shared haystack diverges: NOT_FOUND_COLUMN_IN_BLOCK

4 participants