Repository navigation
Cherry pick #106349 to 25.8: Fix crash in constraint optimization with correlated subqueries - #107890
Merged
Merged
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]>
`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]>
…-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]>
…ted-subquery-root Fix crash in constraint optimization with correlated subqueries
…rypick/25.8/106349 # Conflicts: # src/Analyzer/Passes/ConvertQueryToCNFPass.cpp # tests/queries/0_stateless/04058_constraint_subst_correlated_subquery_crash.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #106349
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job