Skip to content

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

Closed

Description

@qiuyangliu

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:

  1. COMMENT before AS 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
  1. COMMENT after the SELECT body:
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 parse COMMENT before AS SELECT for views/materialized views, but this form still fails for CREATE MATERIALIZED VIEW ... TO ....
  • The post-AS form appears to fail because ParserSelectWithUnionQuery handles COMMENT in the inner SELECT grammar before the outer parser can treat it as a view-level comment.

Expected behavior

A view-level comment on a materialized view should either:

  1. be accepted in the documented/implemented position before AS SELECT, for example:
CREATE MATERIALIZED VIEW ... TO ... COMMENT 'x' AS SELECT ...

or

  1. 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() in src/Parsers/ParserCreateQuery.cpp is optional and non-transactional.
  • ParserCreateViewQuery parses COMMENT before AS, then hands off to ParserSelectWithUnionQuery after AS.
  • For the post-AS placement, COMMENT appears to be interpreted in the SELECT parser rather than as a view-level clause.
  • For the pre-AS placement, 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

  1. added
    potential bug
    on Jul 14, 2026
  2. adityaksolves commented on Jul 14, 2026

    @adityaksolves
    Contributor

    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.

  3. PedroTadim commented on Jul 14, 2026

    @PedroTadim
    Member

    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 SELECT

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

    CREATE MATERIALIZED VIEW mv TO dst COMMENT 'c1' AS SELECT x FROM src;  -- OK
    SELECT comment FROM system.tables WHERE name = 'mv';  -- 'c1'

    ParserCreateViewQuery now explicitly accepts COMMENT before AS 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: COMMENT is not in ParserAlias::restricted_keywords, so when the SELECT ends with a bare table identifier, the parser consumes COMMENT as an implicit alias for that table — i.e. FROM src COMMENT 'x' is parsed as FROM 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 after FROM ... — any intervening clause (WHERE, GROUP BY, an explicit alias) removes the ambiguity.

    Workarounds, all verified to persist the comment (including on older versions):

    ... FROM src AS s COMMENT 'x';          -- explicit alias
    ... FROM src WHERE 1 COMMENT 'x';       -- any trailing clause
    AS (SELECT x FROM src) COMMENT 'x';     -- parenthesized SELECT
    ... TO dst COMMENT 'x' AS SELECT ...;   -- pre-AS position, on current releases

    A possible fix for form 2 would be adding COMMENT to ParserAlias::restricted_keywords, same as FORMAT, SETTINGS, etc. The tradeoff: any existing query using comment as an implicit alias (SELECT x comment FROM t, FROM t comment) would start failing — explicit AS comment would 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.

  4. PedroTadim commented on Jul 14, 2026

    @PedroTadim
    Member

    @adityaksolves you can add the form 2 to close the issue

  5. adityaksolves commented on Jul 14, 2026

    @adityaksolves
    Contributor

    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.

  6. added a commit that references this issue on Jul 16, 2026
    94ccbe7
  7. added a commit that references this issue on Sep 11, 2026
    1847e39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

potential bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions