Repository navigation
Fix server crash on correlated subquery in ALIAS column expression - #100753
alexey-milovidov merged 12 commits into
Conversation
ClickHouse validates column default expressions by checking only
direct children for subquery nodes, missing nested correlated
subqueries inside function arguments. This allows creating ALIAS
columns like:
ALTER TABLE t ADD COLUMN s ALIAS toString(intDivOrZero(x,
(SELECT max(number) FROM system.numbers WHERE number = x)))
When a query later reads such a column, the planner encounters
the correlated subquery and throws LOGICAL_ERROR, which in debug
builds calls abort() — killing the server.
The fix changes the error from LOGICAL_ERROR to NOT_IMPLEMENTED,
so the server returns a user-facing error instead of crashing.
Closes STID: 0250-3bee
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Pre-PR Validation Gatea) Deterministic repro? Yes. The following reproduces the crash deterministically: CREATE TABLE t_alias_correlated (x Int64) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t_alias_correlated SELECT * FROM system.numbers LIMIT 10;
ALTER TABLE t_alias_correlated ADD COLUMN s Decimal(2,0) ALIAS toString(intDivOrZero(x, (SELECT max(number) FROM system.numbers WHERE number = x LIMIT 1)));
SELECT arraySort(groupArray(x)), arraySort(groupArray(s)) FROM t_alias_correlated; -- crashes without fixb) Root cause explained? c) Fix matches root cause? Yes — changes the error from d) Test intent preserved? N/A (new test). The test verifies: correlated subquery in ALIAS returns e) Both directions demonstrated? Yes — without the fix, the SELECT crashes the server. With the fix, it returns Session: |
|
Workflow [PR], commit [249d662] Summary: ✅ AI ReviewSummaryThis PR moves the fix to the root cause by making subquery detection in column default expressions recursive, so nested subqueries in Final VerdictStatus: ✅ Approve |
The old analyzer resolves ALIAS column references at DDL time, producing UNKNOWN_IDENTIFIER (Code 47) instead of the new analyzer's NOT_IMPLEMENTED path tested here. Skip the test under old analyzer mode. Co-Authored-By: Claude Opus 4.6 <[email protected]>
The old analyzer resolves ALIAS column references at DDL time, producing UNKNOWN_IDENTIFIER (Code 47) instead of the new analyzer's NOT_IMPLEMENTED path tested here. Skip the test under old analyzer mode. Co-Authored-By: Claude Opus 4.6 <[email protected]>
f6ba9b8 to
a703723
Compare
|
cc @vdimir @KochetovNicolai — could you review this? It fixes a server crash when a correlated subquery appears in an ALIAS column expression (CollectTableExpressionData). |
| throw Exception(ErrorCodes::LOGICAL_ERROR, | ||
| "Expected single output in actions dag for alias column {}. Actual {}", column_node->dumpTree(), outputs.size()); | ||
| /// ColumnsDescription validation (ColumnsDescription.cpp) rejects top-level | ||
| /// subqueries in ALIAS expressions, but the check is shallow — it only inspects |
There was a problem hiding this comment.
@groeneai, Why don't you make that check deep to fix the root cause?
|
The |
|
@groeneai look at the review comments. |
Address review feedback: make the subquery validation in ColumnsDescription recursive so that subqueries nested inside function arguments (e.g. `ALIAS toString((SELECT ...))`) are rejected during CREATE/ALTER TABLE rather than persisting invalid schema and crashing later at query time. Previously, the check only inspected direct children of the default expression AST, so a subquery wrapped in a function call would slip through validation. The planner would then hit a LOGICAL_ERROR assertion on correlated subqueries. Changes: - Add `hasSubqueryInTree()` recursive helper in ColumnsDescription.cpp - Replace the shallow type-check with the recursive walk - Keep the planner-level NOT_IMPLEMENTED as a safety net for tables created before this validation was added - Update regression test to verify DDL-time rejection
|
Addressed in b97e4ea. The subquery check in Fix: Added a The planner-level Verified locally: 50/50 test runs pass with full randomization. |
Validation in validateColumnsDefaultsAndGetSampleBlockImpl now rejects subqueries (via hasSubqueryInTree) before branching on allow_experimental_analyzer, so the test exercises the same code path on both analyzers and does not need the no-old-analyzer tag. Addresses clickhouse-gh[bot] review on PR ClickHouse#100753.
|
Thanks for the follow-up review. Addressed both comments: PR description / changelog mismatch (line 18):
|
|
This was fixed by #105146. Let's update the branch. |
LLVM Coverage Report
Changed lines: 80.00% (16/20) · Uncovered code |
|
Hi — could this be a backport candidate for Affected code: validateColumnsDefaultsAndGetSampleBlockImpl in ColumnsDescription.cpp and CollectSourceColumnsVisitor::enterImpl in CollectTableExpressionData.cpp — core code present in all supported branches. Why this might apply: This PR fixes a crash (LOGICAL_ERROR exception which calls abort() in debug builds) when reading from tables with correlated subqueries in ALIAS columns. The affected code path in validateColumnsDefaultsAndGetSampleBlockImpl and CollectTableExpressionData has existed since before all supported branches. The crash can be triggered by valid DDL followed by a SELECT, making it a user-facing crash that should be backported. Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider |
ColumnsDescriptionvalidation rejects subqueries in column default expressions,but the check only inspected direct children of the expression AST. A correlated
subquery nested inside function arguments — e.g.
ALIAS toString(intDivOrZero(x, (SELECT max(number) FROM system.numbers WHERE number = x)))— passed validation and reached the planner. When the planner encountered the
correlated subtree inside
CollectTableExpressionData, it threwLOGICAL_ERROR,which in debug builds calls
abortand kills the server.Per the review on the earlier (planner-side) version of this fix, the validation
is now made deep at the source.
hasSubqueryInTreerecursively walks thedefault expression AST and rejects any
ASTSubquery/ASTSelectQuery/ASTSelectWithUnionQuerynode at any depth duringCREATE/ALTER TABLE.This runs in
validateColumnsDefaultsAndGetSampleBlockImplbefore theallow_experimental_analyzerbranch, so the behavior is identical under bothanalyzers.
This is a behavior change beyond just turning a
LOGICAL_ERRORinto anexception:
(e.g.
ALIAS x + (SELECT 1)) passed DDL validation and worked at read time.THERE_IS_NO_DEFAULT_VALUE, matchingthe existing treatment of top-level subqueries (
DEFAULT (SELECT 1)) and thecorrelated-subquery case that previously crashed.
The planner-side
NOT_IMPLEMENTEDpath is kept as a safety net for anypre-existing schema that bypassed the older shallow check.
Found by the AST fuzzer (STID: 0250-3bee), recurring across 3+ unrelated PRs
over 30 days.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix server crash when reading from a table whose
ALIAScolumn contained a correlated subquery nested inside a function call (e.g.ALIAS toString(intDivOrZero(x, (SELECT ...)))).CREATE TABLEandALTER TABLEnow reject any subquery at any depth inDEFAULT/ALIAS/MATERIALIZEDcolumn expressions withTHERE_IS_NO_DEFAULT_VALUE. Previously, the shallow validation accepted nested subqueries, so the correlated case crashed at read time and the nested non-correlated case appeared to work but could lead to unexpected behavior; both are now rejected at DDL time.Documentation entry for user-facing changes
Version info
26.5.1.738