Repository navigation
Conversation
…bquery The constraint-substitution visitors in `ConvertQueryToCNFPass` (`ComponentCollectorVisitor`, `ColumnNameCollectorVisitor`, and `SubstituteColumnVisitor`) descend into subquery scopes when the visitor is invoked directly on a `QueryNode`/`UnionNode`. Their `needChildVisit` only filtered children with `child_type == QUERY/UNION`, so when the visit root was itself a `QueryNode` (e.g. when CNF reduces a multi-atom WHERE down to a single correlated subquery), the visitors traversed the subquery's children, including its `correlated_columns_list`. For mixed-type constraints, the substitution wraps the canonical column in a `_CAST(...)` `FunctionNode`. The planner later iterates the correlated columns expecting `ColumnNode`, calls `column->as<ColumnNode>()` on a `FunctionNode` (returns nullptr), and dereferences the null `this` inside `getSourceWeakPointer`. libc++ hardening catches this as a `vector[]` out-of-bounds — surfacing as a `Received signal 6` crash on the MSAN AST fuzzer (STID 3452-4527). The fix adds a parent-type check to each visitor's `needChildVisit`: when the parent is a `QueryNode`/`UnionNode`, do not descend. Constraint substitution is scoped to the current query — nested subqueries have their own scope and are processed independently when the outer `ConvertQueryToCNFVisitor` recurses into them. The regression test (`04143_constraint_correlated_subquery_substitute_columns_crash`) mirrors the exact AST fuzzer query that crashed on PR ClickHouse#103539 (`MasterCI` AST fuzzer, amd_msan, commit 14d6e0f). Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=103539&sha=14d6e0f664204a8815f9bf7fd496e90a83efd434&name_0=PR&name_1=AST%20fuzzer%20%28amd_msan%29 Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
cc @novikd @KochetovNicolai — could you review this? Adds a parent-type check to |
|
Pre-PR validation gate (per TASK.md Phase 4 Step 9): (a) Deterministic repro? Yes. Triggered locally (debug build, libc++ hardened mode) with: CREATE TABLE t (a UInt16, b String, c String, d String,
CONSTRAINT c1 ASSUME (a <= b) AND (b <= c) AND (c <= d) AND (d <= a)) ENGINE = TinyLog;
INSERT INTO t VALUES (1, '2', '3', '4');
SELECT count() FROM t WHERE (a >= c) AND (SELECT b >= d)
SETTINGS convert_query_to_cnf=1, optimize_using_constraints=1, optimize_substitute_columns=1, allow_experimental_correlated_subqueries=1;
-- Aborts: libc++ Hardening assertion `__n < size()` failed: vector[] index out of bounds(b) Root cause explained? Yes.
(c) Fix matches root cause? Yes. Adds the missing parent-type check: when (d) Test intent preserved? / New tests added? New regression test
Existing tests in the constraint substitution area ( (e) Both directions demonstrated? Yes.
(f) Fix is general, not a narrow patch? Yes.
Session: cron:clickhouse-ci-task-worker:20260429-191500 |
|
Workflow [PR], commit [89065eb] Summary: ❌
AI ReviewSummaryThis PR fixes a real planner safety issue in ClickHouse Rules
Final VerdictStatus: ✅ Approve |
LLVM Coverage Report
Changed lines: 100.00% (23/23) · Uncovered code |
|
Closing as superseded. This crash (STID 3452-4527) is already fixed on master by the constraint-optimization correlated-subquery work in c667358, 55a371b, and 7e7720a (merged 2026-06-02), refined in 259e104 and 1035d9f (2026-06-17). Master guards the bare-correlated-subquery-as-clause-root case at the invocation site ( CIDB shows STID 3452-4527 with 0 occurrences in the last 60 days (last seen 2026-05-29, before the master fix merged) and 0 on master at any point. The branch also has unrelated drift conflicts after 13.5k commits. Nothing left to add. |
Fix STID 3452-4527 (P0 AST fuzzer crash in correlated-subquery planning).
Root cause
The constraint-substitution visitors in
ConvertQueryToCNFPass(ComponentCollectorVisitor,ColumnNameCollectorVisitor,SubstituteColumnVisitor) only filtered subquery children —needChildVisitcheckedchild_type == QUERY/UNIONbut ignored the parent. When the visitor is invoked directly on aQueryNode(e.g. CNF + constraints reduce a multi-atomWHEREdown to a single correlated subquery), the visitor enters theQueryNodeand the parent of its children is theQueryNode. The child filter cannot help — the children (projection,correlated_columns_list, etc.) areListNodeinstances, notQueryNode/UnionNode. So the visitor traverses inside the subquery and operates on itscorrelated_columns_list.For mixed-type constraints (
a UInt16, b String, ... CONSTRAINT ASSUME a <= b ...), the substitution wraps the canonical class member in a_CAST(...)FunctionNodebecause types differ. The planner later iteratescorrelated_columns_listexpectingColumnNode, callscolumn->as<ColumnNode>()on aFunctionNode(returns nullptr), and dereferences the nullthisinsidegetSourceWeakPointer. libc++ hardening catches this asvector[]index out of bounds insidestd::vector<std::weak_ptr<DB::IQueryTreeNode>>::operator[], surfacing asReceived signal 6(Aborted) on the MSAN AST fuzzer.Fix
Add a parent-type check to each visitor's
needChildVisit: if the parent is aQueryNode/UnionNode, return false. Constraint substitution is scoped to the current query — nested subqueries are processed independently when the outerConvertQueryToCNFVisitorrecurses into them viaenterImpl.ColumnNameCollectorVisitorpreviously had noQUERY/UNIONfiltering at all (only the existingquery_node_to_componentcheck); the same parent-and-child check is added there.Reproduction
The exact fuzzer-generated AST that crashed on PR #103539 (commit
14d6e0f6):The fix turns the crash into a regular planner exception. The remaining
NOT_FOUND_COLUMN_IN_BLOCKis an independent pre-existing planner issue: when CNF reducesWHEREto a single correlated-subquery atom,collectTableExpressionDataskips the WHERE because its top-level node is aQUERY. That registration gap is unrelated to the crash and can be addressed separately.Tests
tests/queries/0_stateless/04143_constraint_correlated_subquery_substitute_columns_crash.sqlexercises:UInt16+Stringcolumns with constraint, correlated subquery inWHERE) — must not crash; expectsNOT_FOUND_COLUMN_IN_BLOCK.PREWHERE— same expectation, plusILLEGAL_PREWHEREallowed (depends on planner ordering).Stringcolumns (no_CASTwrapping) should execute correctly and return the expected count.Context
Found by AST fuzzer (amd_msan) on PR #103539. Per @alexey-milovidov's directive on PR #103539, this fix is sent as a separate PR.
Related (different crash, same file area, my own open PR): #103733 fixes the analogous "Bad cast from
ConstantNodetoColumnNode" path in the recursivereplaceToConstantsfunction.Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=103539&sha=14d6e0f664204a8815f9bf7fd496e90a83efd434&name_0=PR&name_1=AST%20fuzzer%20%28amd_msan%29
Changelog category (leave one):
Documentation entry for user-facing changes