Repository navigation
Description
Company or project name
No response
Describe what's wrong
CREATE MATERIALIZED VIEW ... TO ... AS SELECT fails to parse when a view-level COMMENT clause is used and the SELECT ends directly after FROM ... without trailing clauses such as GROUP BY or WHERE.
Two placements appear problematic:
COMMENTbeforeAS SELECT:
CREATE MATERIALIZED VIEW default.checkout_cmi_detail_mv
TO default.checkout_cmi_detail_2
COMMENT 'checkout_cmi__raw@checkout_cmi_detail_2'
AS SELECT
transaction_id,
item_id,
sess_site_id,
auct_type_code,
cust_traffic_source_level1,
cust_traffic_source_level2,
gmv_plan_usd,
core_item_cnt,
paid_ind,
created_dt,
version,
mv_idempotent_col
FROM default.checkout_cmi__raw;This fails with:
Expected one of: token, Dot, UUID, OpeningRoundBracket, storage definition, ENGINE, PARTITION BY, PRIMARY KEY, ORDER BY, SAMPLE BY, TTL, POPULATE, EMPTY, sql security, DEFINER, SQL SECURITY, AS
COMMENTafter theSELECTbody:
CREATE MATERIALIZED VIEW default.checkout_cmi_detail_mv
TO default.checkout_cmi_detail_2
AS SELECT
transaction_id,
item_id,
sess_site_id,
auct_type_code,
cust_traffic_source_level1,
cust_traffic_source_level2,
gmv_plan_usd,
core_item_cnt,
paid_ind,
created_dt,
version,
mv_idempotent_col
FROM default.checkout_cmi__raw COMMENT 'checkout_cmi__raw@checkout_cmi_detail_2';This fails with:
Expected one of: FINAL, SAMPLE, table, table function, subquery or list of joined tables, array join, LEFT ARRAY JOIN, INNER, ARRAY JOIN, GLOBAL, LOCAL, ANY, ALL, ASOF, SEMI, ANTI, ONLY, LEFT, RIGHT, FULL, CROSS, PASTE, JOIN, PREWHERE, WHERE, GROUP BY, WITH, HAVING, WINDOW, QUALIFY, ORDER BY, LIMIT, OFFSET, FETCH, SETTINGS, UNION, EXCEPT, INTERSECT, COMMENT, INTO OUTFILE, FORMAT, end of query
Based on src/Parsers/ParserCreateQuery.cpp, ParserCreateViewQuery appears intended to accept a view-level COMMENT before AS SELECT, but the parser still rejects it for materialized views with TO .... For the post-AS form, the inner SELECT parser appears to consume or reject COMMENT before the outer CREATE MATERIALIZED VIEW parser can attach it as a view comment.
Does it reproduce on the most recent release?
Yes
How to reproduce
Version tested: 24.4.1.1.
Reproducer:
CREATE MATERIALIZED VIEW default.checkout_cmi_detail_mv
TO default.checkout_cmi_detail_2
COMMENT 'checkout_cmi__raw@checkout_cmi_detail_2'
AS SELECT
transaction_id,
item_id,
sess_site_id,
auct_type_code,
cust_traffic_source_level1,
cust_traffic_source_level2,
gmv_plan_usd,
core_item_cnt,
paid_ind,
created_dt,
version,
mv_idempotent_col
FROM default.checkout_cmi__raw;Observed error:
Expected one of: token, Dot, UUID, OpeningRoundBracket, storage definition, ENGINE, PARTITION BY, PRIMARY KEY, ORDER BY, SAMPLE BY, TTL, POPULATE, EMPTY, sql security, DEFINER, SQL SECURITY, AS
Alternative reproducer:
CREATE MATERIALIZED VIEW default.checkout_cmi_detail_mv
TO default.checkout_cmi_detail_2
AS SELECT
transaction_id,
item_id,
sess_site_id,
auct_type_code,
cust_traffic_source_level1,
cust_traffic_source_level2,
gmv_plan_usd,
core_item_cnt,
paid_ind,
created_dt,
version,
mv_idempotent_col
FROM default.checkout_cmi__raw COMMENT 'checkout_cmi__raw@checkout_cmi_detail_2';Observed error:
Expected one of: FINAL, SAMPLE, table, table function, subquery or list of joined tables, array join, LEFT ARRAY JOIN, INNER, ARRAY JOIN, GLOBAL, LOCAL, ANY, ALL, ASOF, SEMI, ANTI, ONLY, LEFT, RIGHT, FULL, CROSS, PASTE, JOIN, PREWHERE, WHERE, GROUP BY, WITH, HAVING, WINDOW, QUALIFY, ORDER BY, LIMIT, OFFSET, FETCH, SETTINGS, UNION, EXCEPT, INTERSECT, COMMENT, INTO OUTFILE, FORMAT, end of query
Parser context:
- Repository:
ClickHouse/ClickHouse - Suspected area:
src/Parsers/ParserCreateQuery.cpp - In
ParserCreateViewQuery, there is explicit logic to parseCOMMENTbeforeAS SELECTfor views/materialized views, but this form still fails forCREATE MATERIALIZED VIEW ... TO .... - The post-
ASform appears to fail becauseParserSelectWithUnionQueryhandlesCOMMENTin the innerSELECTgrammar before the outer parser can treat it as a view-level comment.
Expected behavior
A view-level comment on a materialized view should either:
- be accepted in the documented/implemented position before
AS SELECT, for example:
CREATE MATERIALIZED VIEW ... TO ... COMMENT 'x' AS SELECT ...or
- if the syntax is intentionally unsupported for this form, fail with a clear and specific syntax restriction rather than a misleading generic parse error.
At minimum, the COMMENT-before-AS form appears intended by the parser and should parse successfully.
Error message and/or stacktrace
Expected one of: token, Dot, UUID, OpeningRoundBracket, storage definition, ENGINE, PARTITION BY, PRIMARY KEY, ORDER BY, SAMPLE BY, TTL, POPULATE, EMPTY, sql security, DEFINER, SQL SECURITY, AS
and
Expected one of: FINAL, SAMPLE, table, table function, subquery or list of joined tables, array join, LEFT ARRAY JOIN, INNER, ARRAY JOIN, GLOBAL, LOCAL, ANY, ALL, ASOF, SEMI, ANTI, ONLY, LEFT, RIGHT, FULL, CROSS, PASTE, JOIN, PREWHERE, WHERE, GROUP BY, WITH, HAVING, WINDOW, QUALIFY, ORDER BY, LIMIT, OFFSET, FETCH, SETTINGS, UNION, EXCEPT, INTERSECT, COMMENT, INTO OUTFILE, FORMAT, end of query
Related issues and pull requests
Possibly related, but not the same issue:
Additional context
Search note: I found a related issue about comments on materialized views not being persisted/exposed (#91212), but not an obvious duplicate specifically about parse errors for COMMENT with CREATE MATERIALIZED VIEW ... TO ... AS SELECT.
Potential root cause from code reading:
parseComment()insrc/Parsers/ParserCreateQuery.cppis optional and non-transactional.ParserCreateViewQueryparsesCOMMENTbeforeAS, then hands off toParserSelectWithUnionQueryafterAS.- For the post-
ASplacement,COMMENTappears to be interpreted in theSELECTparser rather than as a view-level clause. - For the pre-
ASplacement, the parser appears intended to support it, so the current rejection looks like a bug or at least an unintended grammar edge case.
If useful, I can also provide a reduced reproducer using a minimal source/target table pair.
Activity
adityaksolves commentedon Jul 14, 2026
Hi @qiuyangliu, thanks for the detailed writeup — the repro steps and parser context you've included make this easy to follow. I'd like to pick this up.
I'll start by reproducing both cases on the current master branch to confirm the exact failure points before touching ParserCreateQuery.cpp. Will post an update here once I've confirmed the repro and have a proposed fix direction (likely adjusting where parseComment() is invoked relative to TO and AS in ParserCreateViewQuery). Will follow up with a PR.
PedroTadim commentedon Jul 14, 2026
Thanks for the detailed report. I tested both forms — they're actually two different situations, and neither is quite what the issue description suggests.
Form 1:
TO ... COMMENT 'x' AS SELECTThis was a genuine parser gap, but it's already fixed. You tested 24.4.1.1, which is over two years old. I can reproduce your exact error up to and including 25.10.2.65, but on current builds it parses fine and the comment is persisted:
ParserCreateViewQuerynow explicitly acceptsCOMMENTbeforeAS SELECT, and the change was also backported to recent patch releases. Please retest on a current version — this form should just work for you.Form 2: trailing
... FROM src COMMENT 'x'This one is real and still reproduces on master, but the root cause isn't the SELECT parser consuming the comment clause. The trailing position is the documented placement (
AS SELECT ... [COMMENT 'comment']) and works fine in general. The problem is narrower:COMMENTis not inParserAlias::restricted_keywords, so when the SELECT ends with a bare table identifier, the parser consumesCOMMENTas an implicit alias for that table — i.e.FROM src COMMENT 'x'is parsed asFROM src AS COMMENT, and the parse then fails at the string literal. That's why the error position in your second message points at the literal rather than the keyword, and why you only see it when the query ends directly afterFROM ...— any intervening clause (WHERE,GROUP BY, an explicit alias) removes the ambiguity.Workarounds, all verified to persist the comment (including on older versions):
A possible fix for form 2 would be adding
COMMENTtoParserAlias::restricted_keywords, same asFORMAT,SETTINGS, etc. The tradeoff: any existing query usingcommentas an implicit alias (SELECT x comment FROM t,FROM t comment) would start failing — explicitAS commentwould keep working. Given the current behavior produces a confusing error in a natural query shape, that seems worth it, but it's a compatibility call.So: form 1 → please retest on a current release; form 2 → confirmed, root cause is implicit-alias swallowing, easy workarounds available.
PedroTadim commentedon Jul 14, 2026
@adityaksolves you can add the form 2 to close the issue
adityaksolves commentedon Jul 14, 2026
Thanks for the clarification, @PedroTadim!
Retested form 1 on current master — confirmed, TO ... COMMENT 'x' AS SELECT now parses correctly and the comment is persisted in system.tables. No further action needed there.
For form 2, I'll add COMMENT to ParserAlias::restricted_keywords (consistent with FORMAT/SETTINGS). Before finalizing, I'll also grep the existing test suite for any query relying on comment as an implicit alias, to flag potential compatibility breakage alongside the PR description.
Will follow up with a PR shortly.
Add COMMENT to ParserAlias::restricted_keywords
Add COMMENT to ParserAlias::restricted_keywords