Skip to content

Keep the database qualification of a parameterized view reference across an AST round-trip - #119027

Open
Ergus wants to merge 9 commits into
ClickHouse:masterfrom
Ergus:Fix_SE_8633
Open

Ergus wants to merge 9 commits into
ClickHouse:masterfrom
Ergus:Fix_SE_8633

Conversation

@Ergus

@Ergus Ergus commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

A qualified parameterized view reference db.view(params) was formatted back as the single
quoted token `db.view`(params), because ASTFunction keeps only a boolean saying that
the parser saw the dot outside backticks, and that bit does not survive format-and-parse.
Storing a view, ON CLUSTER DDL and shipping a query fragment to a replica all go through
such a round-trip, so CREATE VIEW v AS SELECT * FROM db.pv(p = 1) was stored with the
qualification collapsed. The analyzer tolerates the collapsed spelling because it always
splits the name on the dot, but Context::executeTableFunction only split it when the
parser had flagged the name as compound, so on the old analyzer the reference resolved to
a table literally named db.pv and failed with UNKNOWN_FUNCTION.

ASTFunction::formatImpl now quotes the parts of a compound name separately, so the
reference round-trips unchanged, and Context::executeTableFunction considers both readings
of a dotted name — database.table, and the literal name in the current database —
preferring the one the parser saw, which repairs views whose metadata already carries the
collapsed spelling. TableFunctionNode::toASTImpl marks a qualified parameterized-view name
as compound so fragments sent to replicas format the same way.

Related: #87267

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A database-qualified reference to a parameterized view, such as SELECT * FROM db.pv(p = 1), is no longer stored with its qualification collapsed into a single quoted identifier, and such a collapsed reference in existing metadata now resolves again instead of failing with Unknown table function db.pv.


Workflow [PR]
Sync PR [sync-upstream/pr/119027]

@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [823d37c]

Summary: ✅


AI Review

Summary

This PR fixes part of the parameterized-view qualification round-trip: formatting stored view definitions and EXPLAIN SYNTAX now preserve or expand the qualified spelling. But two correctness gaps remain in the paths the PR explicitly claims to repair: query_tree->toAST() still collapses already-qualified parameterized-view names back to `db.pv`(...), and the legacy Context::executeTableFunction fallback still stops on a shadowing non-parameterized current_db.db.pv before it ever tries the real db.pv view. I cannot approve it in the current state.

Findings

❌ Blockers

  • [src/Analyzer/TableFunctionNode.cpp:176-189] TableFunctionNode::toASTImpl only sets isCompoundName when the original query-tree name had one part. When the analyzer already carries the qualified name (table_function_name == "db.pv"), query_tree->toAST() still formats it as `db.pv`(…). That breaks the PR's stated AST round-trip contract, shows the wrong spelling in EXPLAIN QUERY TREE ... dump_ast, and can still misroute forwarded analyzed queries to a shadowing current_db.db.pv on a legacy receiver. Suggested fix: decide setIsCompoundName(true) from the final emitted name (table_function_ast->name) whenever it has exactly two identifier parts, not from the original table_function_name.
  • [src/Interpreters/Context.cpp:3192-3199] The new two-candidate lookup breaks on the first existing storage even when that storage is not a parameterized view. A collapsed old spelling `db.pv`(p = 1) is therefore still broken whenever current_db.db.pv exists as a plain table or ordinary view: execution picks that storage, skips the parameterized-view branch, and never tries the real db.pv. That leaves the "existing collapsed metadata now resolves again" part of the fix incomplete. Suggested fix: keep scanning candidates until you find a parameterized view, and only fall back to TableFunctionFactory after both interpretations have been checked.
Tests
  • ⚠️ tests/queries/0_stateless/05153_parameterized_view_qualified_name.sh no longer exercises the --enable_analyzer 0 resolution path and never checks a query_tree->toAST() or forwarded-query round-trip. As written, it would stay green even with the two remaining bugs above. Please keep an explicit legacy-analyzer case and add one focused dump_ast or equivalent round-trip regression for an already-qualified parameterized-view name.
Final Verdict
  • Status: ❌ Block
  • Minimum required actions: fix the TableFunctionNode::toASTImpl compound-name handling for already-qualified query-tree nodes, make Context::executeTableFunction keep looking until it finds a parameterized view, and extend 05153 so those two behaviors are covered.

LLVM Coverage Report

Measured on commit 823d37c.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 59/62 (95.16%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 9, 2026
Comment thread src/Interpreters/Context.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-materialized-view Materialized views: insert-triggered MV pipeline, insert dependencies, deduplication. label Sep 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 823d37ca2c846981c95ee4f87427a7b6da4826cd was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

if (isParameterizedView() && storage_id.hasDatabase()
if (isParameterizedView()
&& storage_id.hasDatabase()
&& Identifier{table_function_name}.getPartsSize() == 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TableFunctionNode::toASTImpl still misses the case where the query tree already carries a qualified parameterized-view name. For a direct SELECT * FROM db.pv(p = 1), QueryTreeBuilder stores table_function_name == "db.pv", so this getPartsSize() == 1 guard is false and the emitted ASTFunction keeps isCompoundName == false. Any query_tree->toAST() user (EXPLAIN QUERY TREE ... dump_ast, ClusterProxy::executeQuery, etc.) then formats the call back as `db.pv`(p = 1).

That is not only cosmetic with the new Context::executeTableFunction fallback: a receiver that still resolves table functions through that legacy path now tries the literal current-database view db.pv first. If both current_db.db.pv and db.pv exist, the forwarded analyzed query binds the wrong parameterized view. setIsCompoundName(true) needs to depend on the final emitted name having two parts, not on the original table_function_name having one part.

StoragePtr table;
for (const auto & [candidate_database, candidate_table] : candidates)
{
table = DatabaseCatalog::instance().tryGetTable({candidate_database, candidate_table}, getQueryContext());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new two-candidate lookup still stops at the first existing table, even if that storage is not a parameterized view.

A concrete trace is:

  • currentDatabase() contains a plain table or non-parameterized view named `db.pv`
  • database db contains the parameterized view pv
  • stored old metadata or another round-trip produces `db.pv`(p = 1)

Here candidates becomes {current_db, "db.pv"} and {"db", "pv"}. The loop breaks on the first storage at current_db.db.pv, the isParameterizedView() branch is skipped, and execution falls through to TableFunctionFactory::get, so the split db.pv candidate is never tried.

That means the "repair already-collapsed metadata" part of the fix is still incomplete whenever a plain dotted-name table shadows the literal reading. The lookup should keep scanning until it finds a parameterized view, not stop at the first arbitrary storage.

# the name and when the whole name arrives inside one quoted identifier: an AST round-trip (view
# metadata, `ON CLUSTER` DDL, a query shipped to another server) collapses `db.pv` into the single
# quoted token `db.pv`.
echo "--- resolution"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

05153 no longer covers the two paths this PR still relies on fixing: the legacy --enable_analyzer 0 resolution in Context::executeTableFunction, and the query_tree->toAST() / forwarded-query round-trip for an already-qualified parameterized-view name.

The current assertions all pass on the default analyzer path plus formatting / EXPLAIN SYNTAX, so they would not fail on either remaining bug above. Please keep an explicit legacy-analyzer case and add one focused dump_ast or equivalent round-trip assertion for the qualified query-tree path.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 3, 2026
…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]>
alexey-milovidov pushed a commit to chiranmoyf/ClickHouse that referenced this pull request Nov 4, 2026
… 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]>
@clickhouse-gh clickhouse-gh Bot added comp-view Logical (non-materialized) views. and removed comp-materialized-view Materialized views: insert-triggered MV pipeline, insert dependencies, deduplication. labels Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-view Logical (non-materialized) views. pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants