Repository navigation
Keep the correlated column of a subquery out of the subcolumn rewrite - #118287
Conversation
A correlated subquery lists the outer columns it reads, and decorrelation joins
exactly those columns in. When the subquery reads a tuple element of such a
column through its position - `o.p.1`, which is `tupleElement(o.p, 1)` -
`optimize_functions_to_subcolumns` replaced the function with a subcolumn read
of the column. The list then named a column the subquery no longer reads, so
decorrelation looked for the subcolumn in a join that carries the whole column:
CREATE TABLE tc (p Tuple(a Int32, b String)) ENGINE = Memory;
SELECT count() FROM tc AS o WHERE EXISTS (SELECT 1 FROM tc AS i WHERE i.p.a = o.p.1);
Code: 10. Not found column __table1.`p.a` in block __table4.`p.a` Int32 Int32(size = 0).
Accessing the same element by name resolves to that subcolumn during analysis,
which the list records, so only the positional form failed. The first pass of
the rewrite now counts a use of a correlated column as one it cannot optimize,
which leaves every use of that column alone - the same conservative answer it
already gives for an index column or a column read in full elsewhere.
`rewrite_in_to_join` builds such a correlated subquery out of `x IN (subquery)`,
so `p.2 IN (SELECT ...)` failed the same way and is fixed as well.
Closes: #111729
Closes: #112129
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [541fb77] Summary: ✅
AI ReviewSummaryThis PR keeps Final Verdict
LLVM Coverage ReportMeasured on commit 541fb77.
Changed lines: Changed C/C++ lines covered: 63/66 (95.45%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
…al-tuple-element-111729
Review pointed out that the guard added for the direct transformer path does not cover the chained one, where `optimizeJSONArrayElement` rewrites `json.a[1].b` into a subcolumn of `json.a`. That path cannot reach the rewrite for a correlated column today: the subquery's list of correlated columns is itself a read of the whole column, so the use count never matches the optimizable count, and the chained path - unlike the direct one, where `tupleElement` over `Tuple` is in `transformers_optimize_in_filter_with_full_column` - has no filter-only escape hatch. Verified with a control binary built without the new guard: the query tree of a correlated `o.json.a[1].b` is identical either way, and only the subquery's own access is rewritten. Still state the invariant explicitly instead of relying on that accident of the counting, and pin the behaviour with an assertion on the query tree. Also pin `enable_analyzer` in the test: correlated subqueries are a feature of the new analyzer, and the test failed in the old analyzer run of `Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DBReplicated)`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118287&sha=1029303102b10635bdb4a271efee46dd9887e031&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. It is a segmentation fault in The other two failures on that commit are handled here: |
|
Acknowledged. Nothing tracks this and no fix is in progress: no open issue and no open PR (searched open PRs and issues by text and by the touched file). In CIDB this segfault (STID 2844-4ada) has exactly one occurrence in the last 14 days, the one you linked at Closest prior art is my #109200 (merged 2026-07-03), which fixed a SIGSEGV sharing this stack bucket (STID 2844-3b6e): a I will follow up here with the fix as a separate PR, or with my findings if the crash turns out not to be reproducible. |
|
Fix: #119401, against Root cause. Not a One correction on the CI database, because it changes the picture. The seven pull requests you list do The I also ran the fix-presence probe I promised: relative to the #109200 merge commit You are right that nothing tracked this, and I have not opened an issue. |
…ment` The Style check rejects the phrase "new analyzer" in comments, since the analyzer has been the default since 24.3. CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118287&sha=23ed62aa70a5f061692199e586e97fe93a45bc42&name_0=PR&name_1=Style%20check PR: #118287 Co-Authored-By: Claude Fable 5.1 <[email protected]>
…al-tuple-element-111729
|
🕵 Thank you for the root cause and for the correction on the CI database rows: the 2026-07 occurrences I listed were This branch is now merged with |
…al-tuple-element-111729
…nctionToSubcolumnsPass` The first pass skipped correlated uses, but a use of the same column in the outer query's `WHERE` puts the identifier into `filter_only`, and the second pass then rewrote the correlated use inside the subquery's `WHERE` anyway, recreating `NOT_FOUND_COLUMN_IN_BLOCK`. Move the stack of correlated columns into a `CorrelatedColumnsStack` helper and consult it in both passes, and add the mixed correlated/non-correlated `WHERE` cases to the test. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The only conflict was `src/Analyzer/Passes/FunctionToSubcolumnsPass.cpp`, resolved to master's version: ClickHouse#118287 already fixes the same bug there, with the correlated-column check in both passes. This branch now only adds the regression test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The positional, `tupleElement(o.p, 1)`, named-identifier, inner-positional and scalar `Tuple` queries are pinned by `05090_correlated_positional_tuple_element`, added with the fix in ClickHouse#118287. Keep only the shapes it does not cover: named `tupleElement(o.p, 'a')`, the scope-selectivity checks, `UNION ALL`, `Variant` and `Map`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A correlated subquery lists the outer columns it reads, and decorrelation joins exactly those columns in. When the subquery reads a tuple element of such a column through its position -
o.p.1, which istupleElement(o.p, 1)-optimize_functions_to_subcolumnsreplaced the function with a subcolumn read of the column. The list then named a column the subquery no longer reads, so decorrelation looked for the subcolumn in a join that carries the whole column:Accessing the same element by name (
o.p.a) resolves to that subcolumn during analysis, which the list records, so only the positional form failed. The first pass of the rewrite now counts a use of a correlated column as one it cannot optimize, which leaves every use of that column alone - the same conservative answer it already gives for an index column, or for a column that is also read in full elsewhere.rewrite_in_to_joinbuilds such a correlated subquery out ofx IN (subquery), sop.2 IN (SELECT 'x')failed the same way and is fixed as well. Both issues have the same cause: withoptimize_functions_to_subcolumns = 0neither reproduces.Closes: #111729
Closes: #112129
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed
NOT_FOUND_COLUMN_IN_BLOCKfor a correlated subquery that reads a tuple element of an outer column by position (o.p.1), and for the same shape on the left ofIN (subquery)underrewrite_in_to_join.Workflow [PR]
Sync PR [sync-upstream/pr/118287]
Version info
26.10.1.646-master(included in26.10and later)