Skip to content

Fix EXPLAIN SYNTAX when expanding Parameterized Views - #103263

Merged
pamarcos merged 11 commits into
ClickHouse:masterfrom
jrdi:fix-explain-syntax-parameterized-views-103249
May 27, 2026
Merged

pamarcos merged 11 commits into
ClickHouse:masterfrom
jrdi:fix-explain-syntax-parameterized-views-103249

Conversation

@jrdi

@jrdi jrdi commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

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):

EXPLAIN SYNTAX expands Parameterized Views

Closes #103249

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Note

Medium Risk
Changes EXPLAIN SYNTAX rewriting to consult the catalog and replace parameterized-view calls with inlined subqueries, which could affect query formatting/aliasing and edge-case resolution (e.g., shadowing table functions, FINAL/SAMPLE modifiers). Scope is limited to explain-time AST transformation and related tests.

Overview
Fixes EXPLAIN SYNTAX to inline parameterized view calls by rewriting FROM/JOIN view(args…) table-function syntax into the view’s parameter-substituted inner SELECT (as a subquery) before printing.

The rewrite preserves explicit aliases, avoids expanding when FINAL/SAMPLE modifiers are present, and skips expansion when the name matches a registered table function (to match execution precedence). Tests are updated and a new stateless test is added to cover basic, nested, join, alias, shadowing, and modifier cases.

Reviewed by Cursor Bugbot for commit 88a454c. Bugbot is set up for automated code reviews on this repo. Configure here.

Version info

  • Merged into: 26.6.1.198

@jrdi
jrdi force-pushed the fix-explain-syntax-parameterized-views-103249 branch from b60d212 to c97eba3 Compare April 21, 2026 13:04
@nihalzp nihalzp added the can be tested Allows running workflows for external contributors label Apr 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6195da1]

Summary: ⏳

job_name test_name status info comment
Stateless tests (amd_asan_ubsan, flaky check) FAIL
02428_parameterized_view FAIL cidb, issue
02428_parameterized_view FAIL cidb, issue
02428_parameterized_view FAIL cidb, issue
02428_parameterized_view FAIL cidb, issue
02428_parameterized_view FAIL cidb, issue

AI Review

Summary

This PR fixes EXPLAIN SYNTAX handling of parameterized views by expanding across FROM/JOIN table expressions, preserving explicit aliases, and adding guards for table-function shadowing, FINAL/SAMPLE, and non-INVOKER SQL security. I reviewed the current code and all prior discussion threads/replies; previously raised issues are addressed in the current head, and I did not find new correctness, safety, or performance issues in the touched paths.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 21, 2026
@jrdi
jrdi force-pushed the fix-explain-syntax-parameterized-views-103249 branch from aefafe2 to a3857d0 Compare April 21, 2026 17:13
@jrdi

jrdi commented Apr 22, 2026

Copy link
Copy Markdown
Contributor Author

Looks like the tests failing are unrelated to the changes in this PR

@jrdi
jrdi force-pushed the fix-explain-syntax-parameterized-views-103249 branch from 2b8177c to 8e56e72 Compare April 22, 2026 07:17
@jrdi

jrdi commented Apr 24, 2026

Copy link
Copy Markdown
Contributor Author

@nihalzp since you added the tag to be tested, do you think this PR is ready to be reviewed? Any major concerns?

@jrdi
jrdi force-pushed the fix-explain-syntax-parameterized-views-103249 branch from 8e56e72 to e13dd5c Compare April 29, 2026 13:22
@jrdi
jrdi force-pushed the fix-explain-syntax-parameterized-views-103249 branch from e13dd5c to d58ba67 Compare May 3, 2026 10:09
@pamarcos pamarcos self-assigned this May 6, 2026
@pamarcos
pamarcos self-requested a review May 6, 2026 09:38
jrdi added 4 commits May 17, 2026 21:37
`EXPLAIN SYNTAX` rewrote `SELECT t.number FROM my_pv(n=1) AS t` into a
subquery aliased with the view's table name (`AS my_pv`), which made
analyzer-path resolution fail with an unknown identifier exception for
the still-present `t.number` reference. Use the original table function
alias when present, falling back to the view name only if no alias was
provided. Adds a regression test for the aliased case.
@jrdi
jrdi force-pushed the fix-explain-syntax-parameterized-views-103249 branch from 8ff075b to 121b519 Compare May 17, 2026 19:37

@pamarcos pamarcos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please find my comment regarding a case that is not handled properly right now.

I also found another issue, but it's not something that you introduced. It was already there: #105634

jrdi added 2 commits May 23, 2026 17:40
When a parameterized view shadows a built-in table function name (e.g. a
user view named `numbers`), `EXPLAIN SYNTAX` was expanding the view while
regular execution correctly resolved the built-in table function, producing
inconsistent behaviour (and an `UNKNOWN_QUERY_PARAMETER` exception when the
view's parameters were not supplied).

Match `QueryAnalyzer::resolveTableFunction` by checking `TableFunctionFactory`
first and skipping view expansion when the name is a registered table
function.
A parameterized view called with FINAL or SAMPLE executes correctly at run
time, but the EXPLAIN SYNTAX rewrite turned the call into a subquery while
leaving the modifiers on the table expression, which produced
UNSUPPORTED_METHOD ("Table expression modifiers ... are not supported for
subquery") for valid queries.

Skip the expansion when these modifiers are present so EXPLAIN SYNTAX
prints the original form, which is non lossy and matches what execution
accepts.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 7b1012c. Configure here.

jrdi and others added 2 commits May 23, 2026 18:07
The earlier commits added three EXPLAIN SYNTAX cases (builtin shadowing,
FINAL, SAMPLE) but only updated the new analyzer reference file, so the
test would mismatch under the old analyzer.

Add the corresponding output to the old analyzer reference. Pin the
builtin shadowing case to the new analyzer with `SETTINGS
allow_experimental_analyzer = 1` because the old analyzer cannot resolve
that precedence even at execution time (pre-existing limitation), so the
test stays meaningful in both modes.
@jrdi
jrdi requested a review from pamarcos May 26, 2026 07:54
@pamarcos
pamarcos requested a review from Copilot May 26, 2026 14:23

Copilot AI left a comment

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

jrdi and others added 2 commits May 26, 2026 21:37
For parameterized views declared with `SQL SECURITY DEFINER` or `NONE`,
execution resolves inner tables under an overridden context. Inlining
the view in `EXPLAIN SYNTAX` instead re-analyzes the inner query under
the invoker's context, which fails with `ACCESS_DENIED` for users that
can query the view but not its inner tables. Leave the original
parameterized call intact in that case.

Also clarify the `ExpandParameterizedViewsMatcher` comment: in the
legacy path it complements `ExplainAnalyzedSyntaxMatcher` rather than
being redundant, since `StorageView::replaceWithSubquery` only rewrites
the first table expression and leaves JOIN sides untouched.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
…iews-103249' into fix-explain-syntax-parameterized-views-103249
@jrdi
jrdi requested a review from Copilot May 26, 2026 19:40

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Note explicitly that the FINAL/SAMPLE and SQL SECURITY guards live in
the new analyzer rewrite (`ExpandParameterizedViewsMatcher`). The
legacy path goes through `ExplainAnalyzedSyntaxMatcher` and
`StorageView::replaceWithSubquery`, which is pre-existing behavior
captured in `.oldanalyzer.reference`.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@jrdi
jrdi requested a review from Copilot May 26, 2026 19:57

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

@clickhouse-gh

clickhouse-gh Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.20% +0.10%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 83.93% (47/56) · Uncovered code

Full report · Diff report

@pamarcos pamarcos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nooooice 🚀

@pamarcos

Copy link
Copy Markdown
Member

The CI test that is timing out is unrelated to this PR. Should be addressed in #102207

@pamarcos
pamarcos added this pull request to the merge queue May 27, 2026
Merged via the queue into ClickHouse:master with commit 628fc12 May 27, 2026
162 of 165 checks passed
@jrdi
jrdi deleted the fix-explain-syntax-parameterized-views-103249 branch May 27, 2026 16:14
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fully resolved SQL when using Parameterized Views

5 participants