Repository navigation
Fix LOGICAL_ERROR Incorrect table expression in EXPLAIN over table function eval - #121272
Conversation
`TableFunctionEval::executeImpl` wraps the generated query in a `StorageView`, the same
way the `view` table function does. `InterpreterSelectQuery` therefore resolves a
`StorageView` and its `analyze` lambda calls `StorageView::replaceWithSubquery`. There the
first table expression has no `database_and_table_name`, because the source is a table
function, so a fake `db.table` name has to be synthesized from the table function name.
The hard-coded list of names that resolve to a view knew only `view`, `viewIfPermitted`
and `merge`, so for `eval` no name was produced and the function threw
`LOGICAL_ERROR: Incorrect table expression`. In a debug or sanitizer build
`abortOnFailedAssertion` turns that into SIGABRT; in a release build it is returned to the
client, so a valid `EXPLAIN` is rejected with an internal error.
The AST-based arms of `EXPLAIN AST optimize = 1` and `EXPLAIN SYNTAX` build that
interpreter directly through `ExplainAnalyzedSyntaxVisitor`, so
`EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x')` was enough to reach it.
Found by the AST fuzzer in `Stress test (arm_debug)` on master while it was running
04512_eval_table_function.sql.
`viewIfPermitted` was added to the same list in the same shape by fbb2e14 when that
table function was introduced. `eval` (7019b6d) did not get the same treatment.
The parser lowercases a table function name before choosing the layer and `EvalLayer`
emits the canonical `eval`, so the exact comparison already covers every spelling.
Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings), plus my own cold
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20260921-044300 |
|
Workflow [PR], commit [6d36d98] Summary: ✅
AI ReviewSummaryThis PR adds the missing Final Verdict
LLVM Coverage ReportMeasured on commit 6d36d98.
Changed lines: Changed C/C++ lines covered: 4/6 (66.67%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline. |
| SELECT | ||
| countIf(explain ILIKE '%Function eval%') AS table_function_node_left, | ||
| countIf(explain ILIKE '%Identifier x%') AS asterisk_resolved | ||
| FROM (EXPLAIN AST optimize = 1 SELECT * FROM EVAL('SELECT 5 AS x')); |
There was a problem hiding this comment.
@groeneai can you confirm no other issues happen with the eval function for every other explain statement, or sql statement?
There was a problem hiding this comment.
Confirmed, and measured: 80 eval shapes on two binaries built from the same tree with only this line differing, one process per shape, debug builds, so a LOGICAL_ERROR aborts and cannot be missed. Coverage is all 10 ASTExplainQuery::ExplainKind values with their option flags, both AST based arms over non-SELECT and nested tops, and 21 statements with no EXPLAIN at all (full list below).
- On master 27 of the 80 abort, every one with the same
Logical error: 'Incorrect table expression'thrown atStorageView.cpp:666inreplaceWithSubquery. With this PR: zero, no logical error in any shape. - Only two arms reach that code at all:
EXPLAIN AST optimize = 1(anygraph), andEXPLAIN SYNTAXwhen the top level is not a plain SELECT (measured: INSERT andCREATE TABLE ... AS SELECT). PlainEXPLAIN SYNTAX SELECT, the other eight kinds and every statement without EXPLAIN never reach it, on either binary. - No second error class turned up. Every non-OK result is identical on both binaries and is a deliberate check:
evalin an expression contextCode: 46,evalinsideevalCode: 36,evalas an argument ofremote/clusterCode: 1, materialized view from a table functionCode: 397, PREWHERE on a ViewCode: 182,EXPLAIN WHATIFwants a MergeTree tableCode: 48,EXPLAIN TABLE OVERRIDE eval(...)Code: 80, the setting offCode: 344.EXPLAIN SYNTAXofCREATE MATERIALIZED VIEWor ofALTERisCode: 62from the EXPLAIN parser, with or withouteval(checked both). - The change is inert outside the throwing path: of the shapes that already worked, stdout is byte identical on 49 of the 50 I diffed, and the exception is
EXPLAIN ANALYZE, whose output carries timings and differs the same way between two runs of the same binary. - The other places that hard code this name list need nothing for
eval:QueryAnalyzerhas such a list forviewandmergeas an argument of another table function, whichevalcan never be (Code: 1), andASTFunction::appendColumnNameImpllists the table functions that cannot be used as an expression, whichevalreaches as an unknown function first (Code: 46).
coverage, and the 27 shapes that abort on master
EXPLAIN kinds, each with its option flags (optimize, graph, oneline, run_query_tree_passes, run_passes, json, indexes, actions, header, compact): AST, SYNTAX, QUERY TREE, PLAN, PIPELINE, ESTIMATE, ANALYZE, WHATIF, TABLE OVERRIDE, CURRENT TRANSACTION.
AST based arms over: INSERT, INSERT INTO FUNCTION, CREATE TABLE AS SELECT, CREATE VIEW AS SELECT, CREATE MATERIALIZED VIEW AS SELECT, UNION, CTE, IN, scalar subquery, subquery, JOIN with eval on either or both sides, comma join, DISTINCT with GROUP BY and ORDER BY, FINAL, SAMPLE, PREWHERE, ARRAY JOIN, eval nested in view / viewIfPermitted / remote, eval inside eval, eval((SELECT ...)), query level SETTINGS, and the fuzzer query from the CI fatal log. Controls: the same shapes with view, merge and numbers.
Without EXPLAIN: SELECT, count, ordered multi row read, JOIN, IN, CTE, subquery, INSERT SELECT, CREATE TABLE AS SELECT, CREATE VIEW then SELECT from it, CREATE MATERIALIZED VIEW, DESCRIBE, remote, cluster, eval nested in view and viewIfPermitted, expression context, FINAL, PREWHERE, and the experimental setting turned off.
Aborting on master, passing with this PR:
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x');
EXPLAIN AST graph = 1, optimize = 1 SELECT * FROM eval('SELECT 5 AS x');
EXPLAIN AST graph = 1, optimize = 1 SELECT DISTINCT * FROM eval((SELECT toLowCardinality('SELECT 5 AS lc') LIMIT 539));
EXPLAIN AST optimize = 1 INSERT INTO t SELECT * FROM eval('SELECT 5 AS x');
EXPLAIN SYNTAX INSERT INTO t SELECT * FROM eval('SELECT 5 AS x');
EXPLAIN AST optimize = 1 CREATE TABLE t ENGINE = Memory AS SELECT * FROM eval('SELECT 5 AS x');
EXPLAIN SYNTAX CREATE TABLE t ENGINE = Memory AS SELECT * FROM eval('SELECT 5 AS x');
EXPLAIN AST optimize = 1 CREATE VIEW v AS SELECT * FROM eval('SELECT 5 AS x');
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x') UNION ALL SELECT 6 AS x;
EXPLAIN AST optimize = 1 SELECT * FROM (SELECT * FROM eval('SELECT 5 AS x'));
EXPLAIN AST optimize = 1 WITH c AS (SELECT * FROM eval('SELECT 5 AS x')) SELECT * FROM c;
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x') WHERE x IN (SELECT * FROM eval('SELECT 5 AS y'));
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x') AS a JOIN numbers(1) AS n ON 1;
EXPLAIN AST optimize = 1 SELECT * FROM numbers(1) AS n JOIN eval('SELECT 5 AS x') AS a ON 1;
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x') AS a JOIN eval('SELECT 5 AS x') AS b USING (x);
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x') AS a, eval('SELECT 6 AS y') AS b;
EXPLAIN AST optimize = 1 SELECT DISTINCT x FROM eval('SELECT 5 AS x') GROUP BY x ORDER BY x LIMIT 1;
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x') FINAL;
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x') SAMPLE 1 / 2;
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x') PREWHERE x > 1;
EXPLAIN AST optimize = 1 SELECT a FROM eval('SELECT [1, 2] AS arr') ARRAY JOIN arr AS a;
EXPLAIN AST optimize = 1 SELECT * FROM eval((SELECT 'SELECT 5 AS x'));
EXPLAIN AST optimize = 1 CREATE MATERIALIZED VIEW mv ENGINE = Memory AS SELECT * FROM eval('SELECT 5 AS x');
EXPLAIN AST optimize = 1 SELECT (SELECT count() FROM eval('SELECT 5 AS x'));
EXPLAIN AST optimize = 1 INSERT INTO FUNCTION null('x UInt8') SELECT * FROM eval('SELECT 5 AS x');
SELECT count() FROM (EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x'));
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x') SETTINGS max_threads = 1;Tell me if you want any of these pinned in the test, I kept it to five cases for a one line fix.
There was a problem hiding this comment.
Agreed, keeping it at five. They still cover both shapes that reach the rewrite: cases 1, 3 and 4 are EXPLAIN AST optimize = 1 (plain, UNION arm, upper-case name) and case 5 is EXPLAIN SYNTAX over a non-SELECT top. Each of the four eval cases aborts at StorageView.cpp:666 without the one-line change and passes with it; the view case is green either way. Nothing pushed.
| { | ||
| auto table_function_name = table_expression->table_function->as<ASTFunction>()->name; | ||
| if (table_function_name == "view" || table_function_name == "viewIfPermitted") | ||
| if (table_function_name == "view" || table_function_name == "viewIfPermitted" || table_function_name == "eval") |
There was a problem hiding this comment.
@groeneai I am not sure if we should create a new identifier to eval such as "__eval"?
There was a problem hiding this comment.
The placeholder never leaves the rewrite: "__view" appears once in src/, nothing compares against it, and restoreViewName writes back the same ASTPtr, so it reaches neither name resolution nor the catalog. Measured on this branch: EXPLAIN AST optimize = 1 prints no alias for the rewritten node, EXPLAIN SYNTAX restores the original eval('...') text, and __view is not usable as a qualifier (Code: 47 on SELECT __view.x FROM eval('SELECT 5 AS x')). viewIfPermitted already shares it, so the name tracks the rewrite rather than the function, and __eval would be a rename with no behavior change. I can push it if you prefer the distinct name; otherwise the current head is green and I would leave it.
6d5a377
…allowlist-120188 Pulls in #121272, which fixes the fleet-wide `Logical error: Incorrect table expression` (STID 3510-55f2) in `StorageView::replaceWithSubquery` that was the only non-infra red on this branch. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
LOGICAL_ERROR: Incorrect table expressionwhenEXPLAIN AST optimize = 1orEXPLAIN SYNTAXreads from the experimental table functioneval.Description
EXPLAIN AST optimize = 1 SELECT * FROM eval('SELECT 5 AS x')failed withLOGICAL_ERROR: Incorrect table expression: aSIGABRTin a debug or sanitizer build, an internalerror returned to the client in a release build. Reachable with
allow_experimental_eval_table_function = 1on 26.7, 26.8 and 26.9.TableFunctionEval::executeImplwraps the generated query in aStorageView, as theviewtablefunction does, so
InterpreterSelectQuerycallsStorageView::replaceWithSubquery. There the firsttable expression has no
database_and_table_name, the source being a table function, so a fakedb.tablename must be synthesized from the function name. The list of view-producing names heldonly
view,viewIfPermittedandmerge, soevalgot none and the function threw.fbb2e14d543f4closed the same one-line gap forviewIfPermitted. The AST-based arms ofEXPLAIN AST optimize = 1andEXPLAIN SYNTAXbuild that interpreter directly, which is how anordinary query reaches it.
Found by the AST fuzzer on master while it was running
04512_eval_table_function.sql, CIDBtest_nameLogical error: Incorrect table expression (STID: 3510-55f2), first occurrence in 180days:
Stress test (arm_debug)on shaa1cfa117f551,report
and job.
No open issue exists.
Validated in both directions with a deterministic repro whose stack matches CI's frame for frame,
100/100 runs of the new test with and without settings randomization, and an A/B over 259 existing
view/ parameterized-view /EXPLAINtests with identical failure sets.After the rewrite
EXPLAIN SYNTAXstill printsFROM eval(...), as it already does forview.Workflow [PR]
Sync PR [sync-upstream/pr/121272]
Version info
26.10.1.348-master(included in26.10and later)