Repository navigation
Fix crash in constraint optimization with correlated subqueries - #106349
Algunenano merged 8 commits into
Conversation
…d subqueries `ConvertLogicalExpressionToCNFPass` optimizes filters using table constraints. Two problems show up when a filter references a correlated subquery: 1. Column substitution treated subqueries as opaque only when they were reached as a child node. When constraint reduction collapses a filter such as `cond OR (subquery)` into the bare subquery, the subquery becomes the clause root, so substitution walked into its `correlated_columns_list` and replaced the correlated `ColumnNode`s with `_CAST` `FunctionNode`s. Planning then called `ColumnNode::getColumnSource` on a non-column node and aborted on an out-of-bounds vector access. Subqueries are now treated as opaque boundaries whether they are a clause root or nested inside an expression, via a shared `isSubquery` helper. 2. The reduction itself can collapse a filter into a bare correlated subquery (for example when the constraint proves the rest of an `OR` is always false). Such a subquery cannot be decorrelated and the query failed with `NOT_FOUND_COLUMN_IN_BLOCK`, even though it runs fine with the optimization disabled. This collapse is now detected in `optimizeNode` and the original filter is kept. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [1035d9f] Summary: ✅
AI ReviewSummaryThis PR makes constraint-based CNF optimization treat subqueries as opaque boundaries, extends the same boundary to Final VerdictStatus: ✅ Approve |
`optimizeNode` checked `isCorrelatedQueryOrUnionNode(new_node)` without guarding against a null `new_node`. When constraint reduction proves the whole filter always-true it is dropped and `cnf->toQueryTree` returns null (for example `WHERE b = 1 OR b < 18 OR b > 5` under a constraint). `isCorrelatedQueryOrUnionNode` dereferences the node, so this segfaulted. Guard the check with a null test; a dropped filter is fine to apply as before. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`replaceToConstants` (used by `optimizeWithConstraints` via `transformAtoms`) recursed into every child without stopping at subqueries. With a constant-equality constraint such as `ASSUME a = 'x'`, an atom like `exists(SELECT ... WHERE outer.a = inner.a)` had its correlated `ColumnNode` replaced by a `ConstantNode`, corrupting the subquery's `correlated_columns_list`. The planner casts those entries as `ColumnNode`s, raising `Bad cast from type DB::ConstantNode to DB::ColumnNode` (`LOGICAL_ERROR`). Make `replaceToConstants` treat subqueries as opaque boundaries, like the other tree walks in this pass. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
@groeneai Could you review please? @clickgapai Could you review please? |
|
LGTM. The bot's
A couple of observations not blocking merge:
The Bugfix-validation red is unrelated infrastructure noise: the runner self-terminated with a cron:clickhouse-ci-task-worker:20260608-165700 |
…-correlated-subquery-root
Drop the local `isSubquery` helper and call the existing `isSubqueryNodeType` from `Analyzer/Utils.h`, which has the same semantics. No behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
A standalone correlated subquery predicate is not inherently un-decorrelatable (e.g. `SELECT * ... WHERE (SELECT a < c)` works). It currently fails only when the outer plan does not keep the captured columns (e.g. `SELECT count() ... WHERE (SELECT a < c)` raises NOT_FOUND_COLUMN_IN_BLOCK). Reword the comment to describe that accurately instead of claiming such subqueries cannot be decorrelated. No behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Reviewed the second half of the fix — the new early-return in Confirmed the bug is real. On the master binary (no fix), the SELECT count() FROM t_cr WHERE (b < d) OR (SELECT a < c)
SETTINGS enable_analyzer=1, convert_query_to_cnf=1, optimize_substitute_columns=1, optimize_using_constraints=1;(constraint forces all columns equal, so The point-2 guard is correctly narrow.
Scope is honest and conservative. The guard only catches a correlated subquery that is the entire No new bug found, and I don't think an extra test is warranted — the two shipped tests plus the disabled-optimization cross-checks cover both the substitution and collapse paths. LGTM from me too. |
|
@Algunenano I reviewed the diff and reproduced the underlying crash. Findings: 1. The documented crash is real — I reproduced it. Running the PR's own repro (trimmed for brevity) |
|
@Algunenano I reviewed the diff and reproduced both failure modes the PR fixes:
Source review of the fix (ConvertQueryToCNFPass.cpp) holds up:
One caveat: I verified the bugs reproduce but couldn't run the patched binary directly, so I'm relying on the CI coverage report and @groeneai's review for the fix side. Optional belt-and-suspenders: a nested-but-non-root correlated case ( |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 27/28 (96.43%) | Lost baseline coverage: none · Uncovered code |
Under `s3 storage` the stateless randomizer could pick a tiny `index_granularity` (e.g. 19), producing ~31578 marks for the test tables. On object storage a multi-column read issues roughly one S3 GET per granule (single-column reads coalesce into a couple of large GETs, multi-column reads do not), so each string-key `GROUP BY a` plus aggregate-of-`b` query made ~15792 S3 GET requests and spent ~70s in S3 read initialization. Eight such queries pushed the `long` test past the 600s limit and timed out. Clamp the randomized `index_granularity` to a floor of 8192 (still randomized up to 65536) via the `Random settings limits` directive, keeping coverage while bounding the per-granule read amplification. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106349&sha=1035d9f91c950f79da75fd6429d6c56dfe7999b4&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20parallel%2C%201%2F2%29 PR with the report: ClickHouse#106349 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
abdb6a3
Cherry pick #106349 to 26.5: Fix crash in constraint optimization with correlated subqueries
…orrelated subqueries
Backport #106349 to 26.5: Fix crash in constraint optimization with correlated subqueries
Cherry pick #106349 to 26.3: Fix crash in constraint optimization with correlated subqueries
…orrelated subqueries
Cherry pick #106349 to 26.4: Fix crash in constraint optimization with correlated subqueries
…orrelated subqueries
Cherry pick #106349 to 25.8: Fix crash in constraint optimization with correlated subqueries
…orrelated subqueries
Backport #106349 to 26.3: Fix crash in constraint optimization with correlated subqueries
Backport #106349 to 26.4: Fix crash in constraint optimization with correlated subqueries
Backport #106349 to 25.8: Fix crash in constraint optimization with correlated subqueries
…ion with correlated subqueries
…6349 Backport ClickHouse#106349 to 26.4: Fix crash in constraint optimization with correlated subqueries
…ion with correlated subqueries
…6349 Backport ClickHouse#106349 to 26.3: Fix crash in constraint optimization with correlated subqueries
Constraint-based query optimization (
optimize_using_constraints+convert_query_to_cnf+optimize_substitute_columns) could crash the server, or fail a valid query, when a filter contained a correlated subquery.When constraint reduction proves part of an
ORis always false, it collapses a filter such asWHERE (b < d) OR (SELECT a < c)into the bare correlated subqueryWHERE (SELECT a < c). Two things then went wrong:Column substitution only treated subqueries as opaque when they were reached as a child node, so with the subquery now at the clause root it rewrote the subquery's correlated columns into
_CASTFunctionNodes. Planning later calledColumnNode::getColumnSourceon a non-column node and aborted (signal 6). Subqueries are now treated as opaque boundaries whether they are a clause root or nested in an expression.The collapse itself leaves a bare correlated subquery as the whole predicate, which cannot be decorrelated and failed with
NOT_FOUND_COLUMN_IN_BLOCK, even though the query runs fine with the optimization disabled. This collapse is now detected and the original filter is kept.cc @novikd
Closes #106118
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a crash and a possible
NOT_FOUND_COLUMN_IN_BLOCKerror when constraint-based optimization (optimize_using_constraints) is used with correlated subqueries.Version info
26.6.1.99026.5.3.47,26.4.5.55,26.3.14.45,25.8.25.28