Skip to content

Cherry pick #106349 to 26.3: Fix crash in constraint optimization with correlated subqueries - #107891

Merged
robot-ch-test-poll4 merged 10 commits into
backport/26.3/106349from
cherrypick/26.3/106349
Jun 19, 2026
Merged

robot-ch-test-poll4 merged 10 commits into
backport/26.3/106349from
cherrypick/26.3/106349

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

Algunenano and others added 9 commits June 2, 2026 20:28
…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]>
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
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Jun 18, 2026
…rypick/26.3/106349

# Conflicts:
#	src/Analyzer/Passes/ConvertQueryToCNFPass.cpp
#	tests/queries/0_stateless/04058_constraint_subst_correlated_subquery_crash.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants