Repository navigation
Conversation
…ed subqueries `replaceToConstants` (a free recursive function in `ConvertQueryToCNFPass.cpp`) walked `term->getChildren()` unconditionally, descending into `QueryNode`/`UnionNode` subtrees and into their `correlated_columns_list`. When a correlated column had a constant equivalence in the constraint comparison graph (e.g. via `ASSUME a = 5`), the `ColumnNode` in `correlated_columns_list` was replaced with a `ConstantNode`. Later, `CollectTopLevelColumnIdentifiersVisitor` raised `Bad cast from type DB::ConstantNode to DB::ColumnNode` (LOGICAL_ERROR) in `PlannerContext::getColumnNodeIdentifierOrNull`. This is the third unguarded tree-walker in the same pass. PR ClickHouse#100756 added the same `QUERY`/`UNION` skip guard to `ComponentCollectorVisitor` and `SubstituteColumnVisitor` but missed `replaceToConstants` because it is a free function rather than a visitor. The fix mirrors the visitor-class guards: skip `QUERY`/`UNION` children during the recursive descent. The constraint comparison graph is scoped to the outer query's table expressions, so descending into subqueries was always semantically wrong, not just crash-inducing. The new test `04142_replace_to_constants_correlated_subquery_crash` exercises this code path specifically by setting `optimize_substitute_columns = 0`, which bypasses `SubstituteColumnVisitor` (already fixed in ClickHouse#100756). Closes ClickHouse#103730
Pre-PR validation gate (TASK.md Phase 4 step 9)a) Deterministic repro? Yes. The reproduction SQL deterministically triggers b) Root cause explained? Yes. c) Fix matches root cause? Yes. The fix prevents the unwanted descent into d) Test intent preserved? Yes — and a new regression test was added. The new e) Both directions demonstrated? Yes.
f) Fix is general, not narrow patch? Yes. I audited every
The legacy AST-based twin in — groeneai (cron:clickhouse-ci-task-worker:20260429-154500) |
|
cc @novikd @KochetovNicolai — could you review this? It guards the third unguarded tree-walker ( |
|
LGTM. Verified the fix against all three test cases on master (with #100756 but without this patch):
The fix correctly guards Verified that |
|
Workflow [PR], commit [0354cbd] Summary: ✅
AI ReviewSummaryThe PR fixes the Final VerdictStatus: ✅ Approve |
The previous fix only skipped QUERY/UNION children during the recursive descent, but the atom passed as `term` can itself be a correlated scalar subquery, for example `WHERE (SELECT a = 5)`. In that case `replaceToConstants` walked the QueryNode's `correlated_columns_list` directly and replaced a correlated `ColumnNode` (with a constant equivalence from the constraint graph) by a `ConstantNode`. A later `CollectTopLevelColumnIdentifiersVisitor` then hit a null `ColumnNode` cast, aborting under UBSan at PlannerContext.cpp:72 (`member call on null pointer of type 'DB::ColumnNode'`) and raising `Bad cast from type DB::ConstantNode to DB::ColumnNode` (LOGICAL_ERROR) in the debug build. Add an early return at the top of `replaceToConstants` for root QUERY/UNION nodes so the atom itself is skipped, not just its children. Extend the regression test 04142_replace_to_constants_correlated_subquery_crash with the direct correlated scalar-subquery case. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ted-subqueries' into fix/replace-to-constants-correlated-subqueries
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 12/12 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
CI finished on HEAD
Session: cron:our-pr-ci-monitor:20260603-200000 |
|
Closing in favor of #106349 (merged 2026-06-18), which fixes this same bug more comprehensively. This PR guarded void replaceToConstants(QueryTreeNodePtr & term, const ComparisonGraph<QueryTreeNodePtr> & graph)
{
if (isSubqueryNodeType(term->getNodeType()))
return;
...
Thanks @novikd for the review. The underlying defect (issue #103730) is resolved on master. |
PR #100756 (just merged) added
needChildVisitguards toComponentCollectorVisitorandSubstituteColumnVisitorinsrc/Analyzer/Passes/ConvertQueryToCNFPass.cppto stop the constraint-substitution machinery from descending into subqueries and corruptingcorrelated_columns_list.The free function
replaceToConstants(line 159 of the same file) has the same problem: it walksterm->getChildren()unconditionally, so when invoked on an atom that contains a correlated subquery it descends into the subquery'sQueryNodechild and intocorrelated_columns_list. For eachColumnNodewhose underlying column has a constant equivalence in the constraint comparison graph (e.g. viaCONSTRAINT c ASSUME a = 5), theColumnNodeis replaced with aConstantNode. Later,CollectTopLevelColumnIdentifiersVisitorcallscolumn_node->as<ColumnNode &>()(PlannerContext::getColumnNodeIdentifierOrNull) on the corrupted list and thetypeid_castraisesBad cast from type DB::ConstantNode to DB::ColumnNode(LOGICAL_ERROR).This was reported by @clickgapai automated review on PR #100756 as the third unguarded tree-walker in the file. The fix mirrors PR #100756's approach: skip
QUERY/UNIONchildren during the recursive descent.The new regression test
04142_replace_to_constants_correlated_subquery_crashcovers this specific code path by settingoptimize_substitute_columns = 0, which bypassesSubstituteColumnVisitor(already fixed in #100756) and routes throughreplaceToConstantsonly. It exercises three correlated-subquery shapes:EXISTS+PREWHERE,UNION ALL, andNOT EXISTS.Verified locally on debug build:
Logical error: 'Bad cast from type DB::ConstantNode to DB::ColumnNode'atPlannerContext.cpp:178and aborts.Closes #103730
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
Bad cast from type DB::ConstantNode to DB::ColumnNodeexception whenoptimize_using_constraints = 1is set on a query with a correlated subquery referencing a column that has a constant equivalence constraint (e.g.ASSUME a = 5). The constraint substitution inConvertQueryToCNFPassno longer descends into subqueries, mirroring the fix in #100756.Documentation entry for user-facing changes