Repository navigation
Fix parser treating COMMENT as implicit alias in CREATE MATERIALIZED VIEW - #110372
Conversation
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)
|
Please use the ChangeLog format |
|
Workflow [PR], commit [2a9e47c] Summary: ✅
AI ReviewSummaryThis PR fixes the parser ambiguity between an implicit alias named Final VerdictStatus: ✅ Approve |
|
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.
|
@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. |
|
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.
…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'
|
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. SELECT 1 comment Waiting on CI now — should resolve the ~80 Stateless tests failures from the previous commit since they were all the same scoping regression. |
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.
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.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 41/43 (95.35%) · Uncovered code |
…t-parser-issue Fix parser treating COMMENT as implicit alias in CREATE MATERIALIZED VIEW
Closes: #110331
Changelog category (leave one):
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.