Repository navigation
Fix crash in constraint substitution with correlated subqueries - #100756
alexey-milovidov merged 2 commits into
Conversation
Pre-PR Validation Gate (Session ce168e98)a) Deterministic repro? ✅ -- On a debug build without the fix:
CREATE TABLE column_swap_test_test (i Int64, a String, b UInt64, CONSTRAINT c1 ASSUME b = cityHash64(a)) ENGINE = MergeTree ORDER BY i SETTINGS min_bytes_for_wide_part = 0;
INSERT INTO column_swap_test_test VALUES (1, 'cat', 1), (2, 'dog', 2);
INSERT INTO column_swap_test_test SELECT number AS i, toString(number) AS a, 1 AS b FROM numbers(1000);
SELECT count() FROM column_swap_test_test
WHERE exists((SELECT toUInt8(1) PREWHERE murmurHash3_64(xxHash32(a, intDiv(2, xxHash32(0, toInt256(0) = b))), multiply(10, 10))))
SETTINGS enable_analyzer = 1, convert_query_to_cnf = 1, optimize_substitute_columns = 1, optimize_using_constraints = 1;
-- Crashes EVERY TIME with: Logical error: 'Bad cast from type DB::FunctionNode to DB::ColumnNode'b) Root cause explained? ✅ c) Fix matches root cause? ✅ d) Test intent preserved? ✅ e) Demonstrated in both directions? ✅
|
|
Workflow [PR], commit [7ef8a13] Summary: ✅ AI ReviewSummaryThis PR fixes a real analyzer correctness issue by preventing Findings
ClickHouse Rules
Final VerdictStatus: Minimum required action:
|
| @@ -0,0 +1,61 @@ | |||
| -- Tags: no-parallel | |||
There was a problem hiding this comment.
-- Tags: no-parallel seems unnecessary for this test and will reduce 0_stateless parallelism in CI. The test only uses local tables and should be isolated by the test harness database; can we drop this tag unless there is a proven race/shared-global dependency?
|
cc @KochetovNicolai @yakov-olkhovskiy — could you review this? It fixes a crash in ConvertQueryToCNFPass when handling correlated subqueries in constraints. |
|
@groeneai Investigate what happened in this test run https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100756&sha=e0a1dd7e86b2c287d3bcaa9ab6550ff127ee0423&name_0=PR&name_1=Unit+tests+%28msan%29 and update the branch. |
|
@nikitamikhaylov Investigated the Unit tests (msan) failure on commit e0a1dd7. Finding: Infrastructure error, not a test failure. The error is CIDB confirms this is a widespread infrastructure issue:
I've updated the branch with latest master (merge commit b0ae85f). CI should re-run now. |
|
The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix. |
ComponentCollectorVisitor and SubstituteColumnVisitor in ConvertQueryToCNFPass did not have needChildVisit guards, so they descended into QUERY/UNION child nodes during tree traversal. This caused them to collect and substitute correlated column references from the constraint graph inside subqueries. When a substitution replaced a ColumnNode with a FunctionNode (via buildCastFunction for type-mismatched equivalences like b = cityHash64(a)), the correlated_columns_list of the QueryNode was corrupted. Later, CollectTopLevelColumnIdentifiersVisitor tried to cast the corrupted node as ColumnNode, triggering: "Logical error: Bad cast from type DB::FunctionNode to DB::ColumnNode" The fix adds needChildVisit to both visitors to skip QUERY and UNION children, scoping constraint substitution to the outer query only. Closes STID 1635-2ab2. Co-Authored-By: Claude Opus 4.6 <[email protected]>
3efab92 to
3d616ed
Compare
|
@alexey-milovidov Done — rebased onto current master to pick up PR #101239 (merged 2026-04-07) which fixes the Stress test (arm_msan) failure you flagged. Also dropped the merge commits to keep the branch clean: a single commit on top of master containing only the fix ( CI re-running on Session: cron:clickhouse-ci-task-worker:20260428-031500 |
|
|
||
| SELECT count() IGNORE NULLS FROM t_constraint_trans | ||
| WHERE a != (SELECT d) | ||
| SETTINGS enable_analyzer = 1, convert_query_to_cnf = 1, optimize_substitute_columns = 1; |
There was a problem hiding this comment.
This assertion depends on constraint reasoning for the WHERE predicate, but optimize_using_constraints is not set explicitly here (it is set in case 1). Can we add optimize_using_constraints = 1 to make this regression test independent of default-setting changes and compatibility profiles?
There was a problem hiding this comment.
Done in 7ef8a13 — added optimize_using_constraints = 1 to the SETTINGS clause of case 2 (matches case 1's explicit pin).
The transitivity case really does depend on this setting: without it the constraint optimizer doesn't deduce a = d from a = b ∧ b = c ∧ c = d, so a != (SELECT d) is not rewritten to False and the assertion of count() = 0 fails. Better to pin it explicitly than rely on the default.
Verified locally: 30/30 passes with full --random-settings and --random-merge-tree-settings.
…bility The transitivity-constraint case relies on `optimize_using_constraints` to deduce `a = d` from the constraints `a = b`, `b = c`, `c = d` and rewrite `a != (SELECT d)` to False. Without that setting, the predicate is not optimized and the assertion of `count() = 0` would fail. Case 1 already pins this setting; case 2 was implicitly relying on the default. Make the dependency explicit so the regression test stays stable under default-setting changes and compatibility profiles. Suggested by clickhouse-gh[bot] on the rebased PR. cc @alexey-milovidov, @KochetovNicolai, @nikitamikhaylov, @rschu1ze
LLVM Coverage Report
Changed lines: 100.00% (12/12) · Uncovered code |
|
Hi — this PR may need backporting to Affected code: Why: If this should be backported, consider adding |
|
Hi @groeneai @alexey-milovidov — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
ComponentCollectorVisitorandSubstituteColumnVisitorinConvertQueryToCNFPasslackedneedChildVisitguards, so they descended into QUERY/UNION child nodes during tree traversal. This caused them to collect and substitute correlated column references from the constraint comparison graph inside subqueries.When a substitution replaced a ColumnNode with a FunctionNode (via
buildCastFunctionfor type-mismatched equivalences, e.g.ASSUME b = cityHash64(a)), thecorrelated_columns_listof the inner QueryNode was corrupted. Later,CollectTopLevelColumnIdentifiersVisitortried to cast the corrupted node asColumnNode, triggering:The fix adds
needChildVisitto both visitors to skip QUERY and UNION children, scoping constraint substitution to the outer query only. This is consistent with how constraint graphs are built — they are scoped to a single query's table expressions and should not cross subquery boundaries.Reproduction: Any query combining:
CONSTRAINT ... ASSUMEinvolving a function expressionenable_analyzer=1, convert_query_to_cnf=1, optimize_substitute_columns=1Triggers a server crash (SIGABRT in debug builds).
CI history (STID 1635-2ab2): 4 hits across unrelated PRs in the last 30 days (AST fuzzer amd_msan, arm_asan_ubsan, Stress test amd_tsan). Zero master hits — the randomized settings
convert_query_to_cnfandoptimize_substitute_columns(both off by default) must be enabled together.Changelog category (leave one):
Changelog entry:
Fixed a server crash (
Logical error: Bad cast from type DB::FunctionNode to DB::ColumnNode) that could occur when using queries with correlated subqueries on tables that haveCONSTRAINT ... ASSUMEdefinitions, whenoptimize_substitute_columnsandconvert_query_to_cnfsettings are enabled.Documentation entry for user-facing changes
Version info
26.5.1.177