Repository navigation
Resolve a database-qualified parameterized view call in INSERT SELECT and EXPLAIN SYNTAX the same way as in SELECT - #128992
Conversation
… and EXPLAIN SYNTAX like SELECT does A stored query writes `db.view(...)` as `` `db.view`(...) ``, which parses back as a one-part name without the parser's compound-name flag (Replicated database DDL entries, Atomic metadata after a restart or ATTACH). The analyzer splits such a name on the dot with extractDatabaseAndTableNameForParameterizedView, but Context::executeTableFunction and the EXPLAIN SYNTAX view inlining split it only when the flag is set, so they looked for a view literally named `db.view` in the current database and failed with UNKNOWN_FUNCTION. INSERT ... SELECT reaches executeTableFunction through JoinedTables::getLeftTableStorage whenever parallel_distributed_insert_select is not 0 (the default): the replicated and Distributed destination checks resolve the source there. So an INSERT into ReplicatedMergeTree or Distributed, and the refresh of a refreshable materialized view into a replicated table, failed. Both places now call the analyzer's helper. Names with three or more parts still fall through to the table function factory, as before. Closes ClickHouse#128872 Related: ClickHouse#119027, ClickHouse#118193 Co-Authored-By: Claude Opus 5.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 review of the same head (0 findings).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261103-124500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261103-112900 |
|
Workflow [PR], commit [3e8fc3b] Summary: ✅
AI ReviewSummaryReviewed PR #128992 at Consumers traced: parser/formatter round trips preserve the function-name string; Missing context / blind spots
Final VerdictStatus: ✅ Approve. No new inline findings to post. |
|
cc @SmitaRKulkarni, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units10 translation units recompiled, 56 s compile time in total, 10 of them have a recent master baseline. |
|
🕵️ @groeneai Using the analyzer's split in
|
…ormatting The formatter wrote the compound name of `db.view(...)` as one quoted identifier, `` `db.view`(...) ``, which parses back as a one-part name. Stored definitions, Replicated database DDL and queries sent to other servers then carried the ambiguous spelling, and a server that splits such a name only for a compound identifier (any release before the previous commit) failed with UNKNOWN_FUNCTION. ASTFunction now writes a compound name part by part, and TableFunctionNode marks a two-part parameterized view name as compound when the analyzer converts the query tree back to an AST. Context::executeTableFunction and the EXPLAIN SYNTAX view inlining also fall back to the whole name in the current database when the split name does not resolve, so a view whose own name contains a dot, `` `v.x`(...) ``, is still found where it was before (the old analyzer on releases that still have it). Related: ClickHouse#119027 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, all three are addressed in 0a69293.
The new test cases fail on master and pass with the fix (50/50 randomized runs), and the parameterized view and |
|
🕵️ @groeneai Thanks, the formatter change is what I hoped for, and the A keyword part makes the query unparsable. Each part goes through Before this commit, all of them were written as
|
Write each part of a compound function name with `FormatSettings::writeIdentifier`, as `ASTIdentifier` does, so the quoting rule and style apply to it as well. A part that is a keyword is passed as ambiguous and stays quoted: after the per-part change in the previous commit, `1 IN \`partition\`.pv(1)` was formatted as `1 IN partition.pv(1)`, which does not parse (the expression parser stops before `IN PARTITION`), and a debug build aborted on it in the format round-trip check of `executeQuery`. A sweep of every single-word keyword in 44 expression and FROM contexts finds no other part that breaks the round trip, before or after this change; `null`, `not`, `true`, `all` and `distinct` were already quoted by `backQuoteIfNeed`. The test checks `null` and `not` in FROM and IN, and `partition` in IN. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The |
The literal-name fallback in `Context::executeTableFunction` and in the `EXPLAIN SYNTAX` view inlining exists for a view whose own name has a dot, written `` `v.x`(...) ``. For an unquoted `v.x(...)` the parser already says the name is the view `x` of database `v`, which is what the analyzer resolves and what both places resolved before; retrying it as a view named `v.x` in the current database made `EXPLAIN SYNTAX` inline a view that `SELECT` rejects with `UNKNOWN_FUNCTION`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, fixed in 5509515, with one related change in 7f0ac12.
|
|
🕵️ @groeneai One corner case in the new fallback in The current database is tried only when the split lookup finds no table at all ( CREATE DATABASE v;
CREATE TABLE v.x (a UInt8) ENGINE = Memory;
CREATE VIEW `v.x` AS SELECT {p:UInt8} AS a; -- in the current database
INSERT INTO dst SELECT * FROM `v.x`(p = 1); -- now UNKNOWN_FUNCTIONThis only matters where a view named auto is_parameterized_view = [](const StoragePtr & storage)
{
const auto * view = storage ? storage->as<StorageView>() : nullptr;
return view && view->isParameterizedView();
};
if (!is_parameterized_view(table) && !function->isCompoundName() && table_name != function->name)Apart from that, I don't see a regression in other places. The parser reads back every form the formatter now writes under any quoting setting ( |
…a parameterized view A stored `v.x`(...) is first read as the view `x` of database `v`. If that names a table that exists but is not a parameterized view, the lookup in `Context::executeTableFunction` and the `EXPLAIN SYNTAX` inlining skipped the current-database view `v.x`, which master resolved there. Fall back to the literal name whenever the split does not find a parameterized view. Reported by @ PedroTadim in ClickHouse#128992 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, fixed in 3e8fc3b: With your fixture, |
… definition `ExpandParameterizedViewsMatcher::tryExpandTableExpression` replaced a parameterized view call with the view's stored query before any analysis and without any access check. A user refused `SHOW CREATE` on the view (no `SHOW COLUMNS`) therefore read the whole definition, literals included, from `EXPLAIN SYNTAX` or `EXPLAIN AST optimize = 1`. With the default `run_query_tree_passes = 0` nothing checks access afterwards; with `run_query_tree_passes = 1` only the tables behind the view are checked. Require `SHOW COLUMNS` on the view at the point the definition is read, the privilege `SHOW CREATE`, `system.tables.create_table_query` and `DESCRIBE pv(...)` already require, and otherwise leave the call as written, like the existing `SQL SECURITY DEFINER` branch. Not throwing keeps an inaccessible view printing like an unknown name in the default mode, as `DESCRIBE` does. The check is keyed on the resolved storage rather than the parsed name. Reported by PedroTadim in the review of ClickHouse#128992. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… a dot The parser joins the parts of a compound function name with a dot and keeps only the joined string plus the compound flag. A back-quoted part may itself contain dots. Since ClickHouse#128992 the formatter writes a compound name part by part and split the joined name on every dot, so `..` inside a part, or a part that starts or ends with a dot, produced an empty part, written as an empty quoted identifier that ParserIdentifier rejects: SELECT `a..b`.f(1) -> SELECT a.``.b.f(1) The debug-build round trip in executeQuery then throws LOGICAL_ERROR "Inconsistent AST formatting". The AST fuzzer reaches it through `{N..M}` globs turned into quoted identifiers (STID 1941-26fa, 1941-1bfa). The original part boundaries are not recoverable from the joined name, so choose them leftmost-greedy: a dot separates two parts only when the part before it is non-empty and text follows it. Every name the parser produces (at least two non-empty parts) still gets at least two parts with the same joined name, so it parses back as a compound name; Context::executeTableFunction and the EXPLAIN SYNTAX view inlining take a different path for a non-compound name, so writing such a name as one quoted token would change their decision after a round trip. Names without an empty part are written exactly as before. A name with no compound spelling (`v.`, only from AST JSON or the analyzer) is written as one quoted token, as before ClickHouse#128992. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #128872
Related: #119027
Related: #118193
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
UNKNOWN_FUNCTIONin the refresh of a refreshable materialized view that reads a parameterized view asdb.view(...)into aReplicatedMergeTreetable (always in aReplicateddatabase, after a restart in anAtomicone), and inINSERT ... SELECTfrom`db.view`(...)into aReplicatedMergeTreeorDistributedtable. A formatted query now keepsdb.view(...)in two parts. Closes #128872.Description
On 26.10.3.7 and 25.8,
SYSTEM REFRESH VIEWof a refreshable materialized view that readsdb.view(...)into aReplicatedMergeTreetable fails withUNKNOWN_FUNCTION: always in aReplicateddatabase, after a restart in anAtomicone.The formatter wrote
db.view(...)as`db.view`(...), which parses back as a one-part name. The analyzer splits such a name on the dot, soSELECTworks.Context::executeTableFunctionand theEXPLAIN SYNTAXview inlining split it only for a name the parser marked as compound.INSERT ... SELECTreachesexecuteTableFunctionwheneverparallel_distributed_insert_selectis non-zero (the default is 2).Both places now use
extractDatabaseAndTableNameForParameterizedView, so views already stored as`db.view`(...)work unchanged; if the split of such a one-part name is not a parameterized view, they try the whole name in the current database, so a view named`v.x`still resolves with the old analyzer of 26.3 and 26.8.ASTFunctionwrites a compound name part by part, quoting a keyword part, and the analyzer marks a two-part parameterized view name compound in its AST, so stored definitions,ReplicatedDDL and queries sent to shards keepdb.view(...).The test covers
INSERT ... SELECTintoReplicatedMergeTreeandDistributed, a refreshable view stored as`db.view`(...)reloaded byDETACH/ATTACHthat appends into the source of an ordinary materialized view, the same in aReplicateddatabase,EXPLAIN SYNTAX, the formatter (also with keyword parts) and the analyzer's AST; each fails on master and passes with the fix (randomized runs), plus three guards for a view named`v.x`.#119027 makes the same formatter change but no longer applies to master. #118193 rewrites the same lookup for hierarchical names; either merge order keeps this call.
Workflow [PR]
Sync PR [sync-upstream/pr/128992]
Version info
26.11.1.1361-master(included in26.11and later)