Skip to content

Keep the columns a correlated subquery correlates on through the outer filter - #118402

Merged
alexey-milovidov merged 5 commits into
masterfrom
correlated-scalar-subquery-required-columns-112027
Sep 20, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
correlated-scalar-subquery-required-columns-112027

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

A correlated subquery in the SELECT list failed with NOT_IMPLEMENTED as soon as the outer query had a
WHERE, unless the correlated column also appeared in the outer SELECT list:

CREATE TABLE t (id UInt32, v Int64) ENGINE = MergeTree ORDER BY id;
INSERT INTO t SELECT number, number % 5 FROM numbers(100);

SELECT o.id, (SELECT count() FROM t AS i WHERE i.v = o.v) FROM t AS o WHERE o.id = 5;
-- Code: 48. DB::Exception: Current query is not supported yet, because can't find correlated column
--   '__table3.v' in current header: __table3.id. (NOT_IMPLEMENTED)

The column a subquery correlates on is read from the input of the step the subquery belongs to, by the join
the subquery is planned into, but no expression of that step reads it, so the step below - here the filter -
pruned it away and planning the subquery had nothing to correlate against. Adding the column to the SELECT
list "rescued" the query for exactly that reason.

An actions chain step now names those columns as inputs it requires beyond the ones its own actions read, so
the step below keeps them. Both sections that can hold a correlated subquery - a filter and the projection -
declare them.

A second, pre-existing gap in the same feature is fixed here as well: addFilterStep decorrelates the
correlated subqueries of a filter before it builds the step, but the WITH TOTALS branch moved the HAVING
expression straight into TotalsHavingStep, which evaluates HAVING itself, so the subquery result stayed an
unresolved input and the query failed with NOT_FOUND_COLUMN_IN_BLOCK:

SELECT o.v, count() FROM t AS o GROUP BY o.v WITH TOTALS
HAVING (SELECT count() FROM t AS i WHERE i.v = o.v) > 0;
-- Code: 10. DB::Exception: Not found column __table2 in block ... (NOT_FOUND_COLUMN_IN_BLOCK)

addTotalsHavingStep now decorrelates them the same way. The expected output of the new test is the output of
the same queries with the correlated subquery replaced by an equivalent plain aggregate, for the
before_having, after_having_exclusive and after_having_inclusive totals modes.

Closes: #112027

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 to CHANGELOG.md):

Fixed NOT_IMPLEMENTED ("can't find correlated column ... in current header") for a correlated subquery in
the SELECT list of a query that has a WHERE, when the correlated column is not also selected, and
NOT_FOUND_COLUMN_IN_BLOCK for a correlated subquery in HAVING of a query with WITH TOTALS.


Workflow [PR]
Sync PR [sync-upstream/pr/118402]

Version info

  • Merged into: 26.10.1.310-master (included in 26.10 and later)

…r filter

A correlated subquery in the `SELECT` list failed with `NOT_IMPLEMENTED` as soon as the outer query had a
`WHERE`, unless the correlated column also appeared in the outer `SELECT` list:

    SELECT o.id, (SELECT count() FROM t AS i WHERE i.v = o.v) FROM t AS o WHERE o.id = 5;
    -- Code: 48. ... can't find correlated column '__table3.v' in current header: __table3.id

The column a subquery correlates on is read from the input of the step the subquery belongs to, by the
join the subquery is planned into, but no expression of that step reads it, so the step below - here the
filter - pruned it away and planning the subquery had nothing to correlate against.

An actions chain step now names those columns as inputs it requires beyond the ones its own actions read,
so the step below keeps them. Both sections that can hold a correlated subquery - a filter and the
projection - declare them.

Closes: #112027

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9a17af2]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

This PR fixes two planner gaps around correlated subqueries: it preserves outer columns that a correlated subquery needs when a filter or projection step would otherwise prune them, and it decorrelates HAVING subqueries on the WITH TOTALS path before TotalsHavingStep evaluates the filter. I did not find a remaining correctness, compatibility, or coverage issue in the current diff that warrants a new review comment.

Final Verdict

✅ No blocking or major issues found in the current diff.

LLVM Coverage Report

Measured on commit 9a17af2.

Metric Baseline Current Δ
Lines 88.40% 88.40% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 80.70% 80.60% -0.10%

Changed lines: Changed C/C++ lines covered: 39/39 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 6, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Sep 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing commit 9a17af202afdc2e4b2cfb6195923f093d7baa7d5 with master failed: RuntimeError: CI logs cluster query failed: SELECT file, symbol, sumIf(size, side = 'pr….

See the job log for details.

alexey-milovidov and others added 4 commits September 6, 2026 17:52
The correlated subqueries this test is about are not supported without the analyzer, where the query
fails with `Missing columns` instead, as the `old analyzer` CI configuration showed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`addFilterStep` calls `buildQueryPlanForCorrelatedSubquery` for every correlated
subquery of the filter before it builds the step, but the `WITH TOTALS` branch
moved `having_analysis_result.filter_actions` straight into `TotalsHavingStep`,
which evaluates `HAVING` itself. The result of the correlated subquery therefore
stayed an unresolved input of the `HAVING` expression, and a query such as

    SELECT o.v, count() FROM t AS o
    GROUP BY o.v WITH TOTALS
    HAVING (SELECT count() FROM t AS i WHERE i.v = o.v) > 0

failed with `NOT_FOUND_COLUMN_IN_BLOCK`. Verified with a control binary that this
is not a regression of this pull request - the same query fails the same way on
`master`.

The results of the new test match the same queries with the correlated subquery
replaced by an equivalent plain aggregate, for `before_having`,
`after_having_exclusive` and `after_having_inclusive` totals modes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

@alexey-milovidov alexey-milovidov self-assigned this Sep 20, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 20, 2026
Merged via the queue into master with commit d92648b Sep 20, 2026
174 of 176 checks passed
@alexey-milovidov
alexey-milovidov deleted the correlated-scalar-subquery-required-columns-112027 branch September 20, 2026 22:36
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 20, 2026
CurtizJ pushed a commit to CurtizJ/ClickHouse that referenced this pull request Oct 1, 2026
…w row

Since `751254f3455` (PR ClickHouse#118402) a correlated subquery in `HAVING` of a
`WITH TOTALS` query is decorrelated into a join before `TotalsHavingStep`.
The join drops the `AggregatedChunkInfo` of the aggregated chunks and mixes
the overflow row into the ordinary rows, so with `max_rows_to_group_by`,
`group_by_overflow_mode = 'any'` and `totals_mode` other than
`after_having_exclusive` the query threw the logical error
`Chunk should have AggregatedChunkInfo in TotalsHavingTransform`.
Before ClickHouse#118402 such a query failed with `NOT_FOUND_COLUMN_IN_BLOCK`, so the
combination is now rejected with `NOT_IMPLEMENTED`.

Found by the server AST fuzzer in the stress test:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=93ef7aa65ae2b9ecacff6f55c5e0ae9e155baafd&name_0=MasterCI&name_1=Stress%20test%20%28azure%2C%20amd_tsan%29

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
alexey-milovidov pushed a commit to zhongyuankai/ClickHouse that referenced this pull request Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

2 participants