Skip to content

Fix parser treating COMMENT as implicit alias in CREATE MATERIALIZED VIEW - #110372

Merged
PedroTadim merged 12 commits into
ClickHouse:masterfrom
adityaksolves:fix/mv-comment-parser-issue
Jul 16, 2026
Merged

PedroTadim merged 12 commits into
ClickHouse:masterfrom
adityaksolves:fix/mv-comment-parser-issue

Conversation

@adityaksolves

@adityaksolves adityaksolves commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #110331

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

Fixed a parser bug where COMMENT was incorrectly consumed as an implicit alias when a SELECT query ended directly after a bare table identifier (e.g. ... FROM t COMMENT 'x'), causing a misleading syntax error instead of the comment being applied to the view/table.

Previously, a query like 'FROM src COMMENT '\''x'\''' failed to parse
because COMMENT was consumed as an implicit alias for the preceding
table identifier, causing the parser to fail on the string literal.

This adds COMMENT to the list of restricted keywords that cannot be
used as an implicit (AS-less) alias, consistent with FORMAT and
SETTINGS. Explicit 'AS comment' aliases are unaffected.

Closes ClickHouse#110331 (form 2, per maintainer confirmation)
@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jul 14, 2026
@PedroTadim

Copy link
Copy Markdown
Member

Please use the ChangeLog format

@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2a9e47c]

Summary: ✅


AI Review

Summary

This PR fixes the parser ambiguity between an implicit alias named COMMENT and trailing COMMENT '...' clauses in CREATE ... AS statements, and it now also emits explicit duplicate-comment diagnostics across ordinary/materialized/window views and CREATE TABLE ... AS {SELECT|table|table_function}. The current head narrows the alias disambiguation to the end-of-query case and adds focused regression coverage for the previously flagged sibling grammars, so I did not find a remaining correctness issue in the patch.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 14, 2026
@adityaksolves

Copy link
Copy Markdown
Contributor Author

Hi @PedroTadim, thanks for pointing that out — I've updated the PR description to match the official Changelog template format (### Changelog category / ### Changelog entry with proper headers and bullet list). Also linked the issue (#110331) for auto-close. Pushed an empty commit to re-trigger CI. Let me know if anything else needs adjustment!

…SELECT

Per review feedback: previously, specifying a comment in both the
pre-AS and post-SELECT position for a materialized view resulted in
a confusing generic parse error ('Expected end of query'), since the
post-SELECT comment was silently left unconsumed once a pre-AS
comment had already been parsed.

Now both positions are parsed independently, and an explicit
SYNTAX_ERROR is thrown if both are present, clarifying that only one
comment position should be used.

Added a corresponding test case.
@adityaksolves

Copy link
Copy Markdown
Contributor Author

@PedroTadim added the explicit check — comments in both positions now throw a clear SYNTAX_ERROR: "Comment for a view cannot be specified both before and after AS SELECT; please use only one." Also added a test case covering this. Verified locally that the existing single-comment forms (pre-AS and post-SELECT) still work as expected.

@PedroTadim

Copy link
Copy Markdown
Member

LGTM, let's wait for the CI

The previous fix added COMMENT to ParserAlias::restricted_keywords[],
but that list is shared by all alias-parsing sites in ClickHouse, not
just CREATE VIEW. This broke previously-valid queries like
'SELECT 1 comment' or 'FROM t comment' everywhere in the codebase.

Replace the global restriction with a thread_local scoped flag
(ParserAlias::comment_as_alias_restricted) that ParserCreateQuery sets
only while parsing a materialized view's AS SELECT body, leaving all
other SELECT/alias parsing unaffected.
Comment thread src/Parsers/ParserCreateQuery.cpp Resolved
@PedroTadim PedroTadim self-assigned this Jul 15, 2026
…f scoped thread_local flag

The previous approach (thread_local ParserAlias::comment_as_alias_restricted,
set/reset around the entire view AS SELECT body) restricted COMMENT as an
implicit alias everywhere inside the parsed view body, breaking valid queries
like 'CREATE VIEW v COMMENT '\''x'\'' AS SELECT 1 comment' and materialized
views with a trailing implicit 'comment' alias/table-alias, as flagged in
review.

This replaces that with a local lookahead directly in ParserAlias::parseImpl:
COMMENT is only rejected as an implicit alias when immediately followed by a
string literal at the true end of the query (the exact ambiguous case this
PR targets). No shared or thread-local state is needed anymore, which also
addresses the question of why thread_local was required in the first place.

Verified manually and via the existing test file:
- SELECT 1 comment
- FROM t comment (table alias)
- CREATE VIEW v COMMENT 'x' AS SELECT 1 comment
- CREATE MATERIALIZED VIEW mv TO dst AS SELECT x FROM src comment
- Original bug: CREATE MATERIALIZED VIEW mv TO dst AS SELECT x FROM src COMMENT 'x'
@adityaksolves

Copy link
Copy Markdown
Contributor Author

Pushed 5e50e5a — reworked this to remove the thread_local flag entirely and replaced it with a local lookahead directly in ParserAlias::parseImpl. COMMENT is now only rejected as an implicit alias when immediately followed by a string literal at the true end of the query — the exact ambiguous case from the original bug report. No shared/thread-local state anymore, so it also resolves the "why is thread_local needed" question — it isn't, with this approach.
Verified manually and via the test file:

SELECT 1 comment
CREATE VIEW v COMMENT 'x' AS SELECT 1 comment (the case flagged by the bot)
CREATE MATERIALIZED VIEW mv TO dst AS SELECT x FROM src comment
Original bug: ... FROM src COMMENT 'trailing comment'

Waiting on CI now — should resolve the ~80 Stateless tests failures from the previous commit since they were all the same scoping regression.

Comment thread src/Parsers/ParserCreateQuery.cpp Resolved
ParserCreateViewQuery already throws a clear SYNTAX_ERROR when a comment
is specified both before and after AS SELECT. ParserCreateWindowViewQuery
uses a separate parser and was missing this check, so the same scenario
for window views produced a generic, confusing SYNTAX_ERROR instead.

This mirrors the existing check into ParserCreateWindowViewQuery and adds
a regression test.
parseComment() uses ParserStringLiteral, which accepts both
TokenType::StringLiteral and TokenType::HereDoc (dollar-quoted strings).
The lookahead in ParserAlias::parseImpl only checked for StringLiteral,
so a materialized view with a heredoc-style trailing comment (e.g.
COMMENT $tag$note$tag$) still went down the old implicit-alias path
and reproduced the original bug, as flagged in review.

Fixed by reusing ParserStringLiteral().parse() directly for the lookahead
instead of manually checking the token type, so it now covers exactly the
same literal forms parseComment() accepts.

Added a regression test covering the heredoc comment form on a
materialized view.
The existing .sql regression cases for the duplicate pre/post-AS SELECT
comment scenario only assert SYNTAX_ERROR. The old (pre-fix) behavior
also produced SYNTAX_ERROR by leaving the trailing COMMENT token
unconsumed, so the error code alone doesn't prove the dedicated
duplicate-comment diagnostic path is what actually fired.

Add a .sh test that greps for the exact message text ('Comment for a
view cannot be specified both before and after AS SELECT; please use
only one') for both CREATE MATERIALIZED VIEW and CREATE WINDOW VIEW,
distinguishing the new diagnostic from the old generic failure mode.
The COMMENT-as-implicit-alias lookahead fix in ParserAlias is global,
so it also affects CREATE TABLE ... AS SELECT ... COMMENT 'x'. The
table branch of ParserCreateTableQuery::parseImpl still only did a
single trailing parseComment() after select_p, so a comment specified
both before and after AS SELECT fell back to a generic trailing-token
SYNTAX_ERROR instead of the dedicated duplicate-comment diagnostic
already present for views and window views.

Mirror the same select_comment duplicate-check into the table branch,
scoped to only trigger when the AS SELECT path was actually taken (the
'select' AST is populated), leaving the 'AS table', 'AS table_function',
and plain ENGINE=... forms untouched.

Add regression coverage: a .sql duplicate-comment case plus two
no-regression cases (single comment on AS SELECT, plain CREATE TABLE
without AS SELECT), and a message-sensitive .sh case asserting the
exact diagnostic text, mirroring the existing view/window-view tests.
Comment thread src/Parsers/ParserCreateQuery.cpp Resolved
CREATE TABLE already supported a trailing post-AS comment for the
AS table and AS table_function() forms (see
03033_create_as_copies_comment.sql), not just AS SELECT. The
duplicate-comment check added for CREATE TABLE only triggered when
the 'select' AST was populated, so CREATE TABLE t COMMENT 'pre' AS
base COMMENT 'post' (and the AS table_function() variant) still fell
back to the old generic trailing-token SYNTAX_ERROR instead of the
dedicated diagnostic.

Extend the condition to select || as_table || as_table_function, and
generalize the message text from 'AS SELECT' to 'AS' since it now
covers all three AS forms uniformly.

Add regression coverage: duplicate-comment cases for both AS table
and AS table_function(), a no-regression case for a single comment
on AS table, and a message-sensitive .sh case for the AS table form.
Verified locally that the existing 03033_create_as_copies_comment.sql
test (which already covers the single post-AS comment case) still
passes.
…mment

The AS table_function() duplicate-comment case (e.g. AS numbers(5))
only asserted SYNTAX_ERROR, which the old code already produced by
leaving the trailing COMMENT unconsumed. Add the same message-grep
coverage already present for views, window views, AS SELECT, and
AS table, so this form also proves the dedicated duplicate-comment
diagnostic fires rather than the old generic error.
@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.90% 85.90% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 78.10% 78.00% -0.10%

Changed lines: Changed C/C++ lines covered: 41/43 (95.35%) · Uncovered code

Full report · Diff report

@PedroTadim
PedroTadim added this pull request to the merge queue Jul 16, 2026
Merged via the queue into ClickHouse:master with commit 092108d Jul 16, 2026
179 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 16, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…t-parser-issue

Fix parser treating COMMENT as implicit alias in CREATE MATERIALIZED VIEW
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.

Parser error for COMMENT in CREATE MATERIALIZED VIEW ... TO ... AS SELECT without trailing WHERE/GROUP BY

3 participants