Repository navigation
Keep the columns a correlated subquery correlates on through the outer filter - #118402
Conversation
…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]>
|
Workflow [PR], commit [9a17af2] Summary: ❌
AI ReviewSummaryThis 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 Final Verdict✅ No blocking or major issues found in the current diff. LLVM Coverage ReportMeasured on commit 9a17af2.
Changed lines: Changed C/C++ lines covered: 39/39 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing commit See the job log for details. |
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]>
…ubquery-required-columns-112027
`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]>
…ubquery-required-columns-112027
…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]>
A correlated subquery in the
SELECTlist failed withNOT_IMPLEMENTEDas soon as the outer query had aWHERE, unless the correlated column also appeared in the outerSELECTlist: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
SELECTlist "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:
addFilterStepdecorrelates thecorrelated subqueries of a filter before it builds the step, but the
WITH TOTALSbranch moved theHAVINGexpression straight into
TotalsHavingStep, which evaluatesHAVINGitself, so the subquery result stayed anunresolved input and the query failed with
NOT_FOUND_COLUMN_IN_BLOCK:addTotalsHavingStepnow decorrelates them the same way. The expected output of the new test is the output ofthe same queries with the correlated subquery replaced by an equivalent plain aggregate, for the
before_having,after_having_exclusiveandafter_having_inclusivetotals modes.Closes: #112027
Changelog category (leave one):
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 inthe
SELECTlist of a query that has aWHERE, when the correlated column is not also selected, andNOT_FOUND_COLUMN_IN_BLOCKfor a correlated subquery inHAVINGof a query withWITH TOTALS.Workflow [PR]
Sync PR [sync-upstream/pr/118402]
Version info
26.10.1.310-master(included in26.10and later)