Skip to content

DESCRIBE and SHOW CREATE TABLE report stale column types for TO-target materialized views after upstream ALTER MODIFY COLUMN #106919

Open

Description

@bluepnume

When a materialized view is created with TO target_table, ClickHouse infers and stores the view's column types from the SELECT at creation time. If the source or target table's column types are later changed via ALTER TABLE ... MODIFY COLUMN, the view's stored types do not refresh — even though runtime data flow uses the source's and target's current types and works correctly.

This means DESCRIBE and SHOW CREATE TABLE show types that no longer reflect runtime behavior, which makes schema-drift auditing very confusing.

Reproduction

CREATE TABLE source (
    id UInt64,
    status Enum8('A' = 0, 'B' = 1)
) ENGINE = MergeTree ORDER BY id;

CREATE TABLE target (
    id UInt64,
    status Enum8('A' = 0, 'B' = 1),
    cnt UInt64
) ENGINE = SummingMergeTree ORDER BY (id, status);

CREATE MATERIALIZED VIEW mv TO target AS
SELECT id, status, count() AS cnt FROM source GROUP BY id, status;

-- Initial state matches:
DESCRIBE mv;
-- status: Enum8('A' = 0, 'B' = 1)

-- Extend the enum in both source and target:
ALTER TABLE source MODIFY COLUMN status Enum8('A' = 0, 'B' = 1, 'C' = 2);
ALTER TABLE target MODIFY COLUMN status Enum8('A' = 0, 'B' = 1, 'C' = 2);

DESCRIBE source;  -- Enum8('A' = 0, 'B' = 1, 'C' = 2)
DESCRIBE target;  -- Enum8('A' = 0, 'B' = 1, 'C' = 2)

-- Matview's stored type is still the original:
DESCRIBE mv;           -- Enum8('A' = 0, 'B' = 1)        <-- stale
SHOW CREATE TABLE mv;  -- column list still Enum8('A' = 0, 'B' = 1)

-- Despite the stale type, runtime works:
INSERT INTO source VALUES (1, 'C');
SELECT * FROM target WHERE id = 1;
-- ┌─id─┬─status─┬─cnt─┐
-- │  1 │ C      │   1 │
-- └────┴────────┴─────┘

Expected

DESCRIBE and SHOW CREATE TABLE on TO-target materialized views should report the current effective schema, refreshed when the source / target tables evolve.

Impact

Operators auditing schema drift reasonably trust DESCRIBE and SHOW CREATE TABLE to reflect what's enforced. With this behavior, you can end up convinced a matview is missing an enum value when in fact runtime is fine — or, in the reverse case, convinced everything is fine because source and target match while a stale matview type silently differs.

Version

26.2.1.413 (ClickHouse Cloud).

Activity

  1. added
    bug
    on Jun 10, 2026
  2. PedroTadim commented on Jun 10, 2026

    @PedroTadim
    Member

    Seems to be a bug for me. @groeneai can you fix this? It should be easy

  3. groeneai commented on Jun 10, 2026

    @groeneai
    Collaborator

    @PedroTadim reproduced on master (fc73f46). Confirmed, and it is a bit worse than the report: reading the MV by name fails, not just DESCRIBE.

    ALTER TABLE source MODIFY COLUMN status Enum8('A'=0,'B'=1,'C'=2);
    ALTER TABLE target MODIFY COLUMN status Enum8('A'=0,'B'=1,'C'=2);
    INSERT INTO source VALUES (1, 'C');
    SELECT * FROM mv;   -- Code 691, UNKNOWN_ELEMENT_OF_ENUM: Unexpected value 2 in enum

    The MV still casts the target's data down to its frozen Enum8('A'=0,'B'=1), so any 'C' row throws. The "data flow works" in the report only held because it read from target, not from mv.

    Root cause: a TO target MV freezes its column list from the SELECT sample block at CREATE time (columns_inferred_from_select_query in InterpreterCreateQuery) and never refreshes it. Three consumers then go stale:

    • DESCRIBE mv -> StorageMaterializedView::getInMemoryMetadataPtr()->getColumns() (frozen columns)
    • SELECT * FROM mv -> readImpl builds converting actions from the target header to the frozen mv_header by name
    • SHOW CREATE TABLE mv -> rendered from the stored CREATE AST, not from live metadata

    The contract today is "the MV reports its SELECT-output schema": 02931 keeps 1 AS v2 as UInt8 even though the target column is UInt64, and 01069 does not expose a column ADDed to the target (both confirmed on master). So mirroring the target's types is wrong. The consistent fix is to recompute the SELECT-output schema against the current source/target schemas, only when the columns were inferred from the SELECT (not for explicit-column MVs like 01182). That recompute is designed to leave 02931 and 01069 unchanged while fixing this case.

    Two things change the implementation surface, so I want to match your intended direction before sending a PR:

    1. Lazy recompute (fixes DESCRIBE + the read failure) vs eager propagation that also rewrites the stored CREATE AST. Only the eager path also fixes SHOW CREATE TABLE mv. Is SHOW CREATE in scope, or just DESCRIBE + the read failure?
    2. MV-only, or should a plain VIEW get the same treatment? A normal VIEW is identically stale in DESCRIBE/SHOW CREATE after a source ALTER (it just does not throw on read, since it re-executes the SELECT).

    Fix is ready to implement either way; just confirming the direction.

  4. bluepnume commented on Jun 11, 2026

    @bluepnume
    Author

    Thanks @groeneai, here's my vote:

    • Lazy over eager. Fixes the two bugs that actually broke us (DESCRIBE lie, read failure). SHOW CREATE TABLE staleness is confusing but not functionally broken, and eager looks like a much bigger surface for that one extra fix. Could layer on later if needed.
    • MV + plain VIEW. Same underlying stale-frozen-columns bug. Leaving VIEW for later feels like a sequel issue waiting to happen.

    Thanks for the triage.

  5. bluepnume commented on Jun 11, 2026

    @bluepnume
    Author

    @groeneai naive follow-up thought after rereading the thread.

    If the column block in SHOW CREATE was inferred (not user-written), suppressing it might be cleaner than either rewriting it eagerly or recomputing it lazily. Concretely, taking the example from the original report:

    CREATE MATERIALIZED VIEW mv TO target AS
    SELECT id, status, count() AS cnt FROM source GROUP BY id, status;

    SHOW CREATE TABLE mv currently returns something like:

    CREATE MATERIALIZED VIEW default.mv TO default.target
    (
        `id` UInt64,
        `status` Enum8('A' = 0, 'B' = 1),
        `cnt` UInt64
    )
    AS SELECT id, status, count() AS cnt FROM source GROUP BY id, status

    That parenthesized column block is invented output. We never wrote it. Storing it (and showing it back) is what makes DESCRIBE stale and SHOW CREATE wrong after a source ALTER.

    If for inferred-column matviews SHOW CREATE just printed the SELECT without the parenthesized block:

    CREATE MATERIALIZED VIEW default.mv TO default.target
    AS SELECT id, status, count() AS cnt FROM source GROUP BY id, status

    ...the output would round-trip (re-running it re-infers from current source/target), match what the user actually wrote, and have nothing stored to go stale. Explicit-column matviews would still print their block as before since you already track columns_inferred_from_select_query.

    Definitely possible there's a CH internal reason this doesn't work cleanly, just chiming in with the simpler-feeling shape.

  6. groeneai commented on Jun 11, 2026

    @groeneai
    Collaborator

    Thanks, going with your vote.

    Plan:

    • Lazy recompute, MV first. The MV's reported columns get recomputed from the current SELECT output (against the live source/target) instead of the frozen create-time list. This fixes both DESCRIBE mv and the SELECT * FROM mv Code 691; runtime data flow is unchanged.
    • Gated on inferred columns, so explicit-column MVs (TO t (x String) AS ..., e.g. test 01182) keep their declared types, and the existing "MV reports its SELECT-output schema" contract (02931: 1 AS v2 stays UInt8; 01069: a column ADDed to the target is not exposed) is preserved.
    • SHOW CREATE. I will try your suppress-the-inferred-block idea, since the presence/absence of the block doubles as the provenance signal that tells the two fixes above which MVs are inferred. If it clashes with an existing expectation (SHOW CREATE after MODIFY QUERY currently prints the block), I will keep the functional DESCRIBE/read fix and leave SHOW CREATE for a follow-up rather than block on it.
    • Plain VIEW. Will cover the same inferred-column staleness where it applies.

    PR shortly, I will link it here.

  7. groeneai commented on Jun 11, 2026

    @groeneai
    Collaborator

    Implementation note after digging in: the suppress-the-block idea has a sharp edge. A TO-target MV with no stored column block hits a LOGICAL_ERROR ("metadata doesn't have structure") the moment anyone runs ALTER ... MODIFY QUERY on it, because applyMetadataChangesToCreateQuery assumes views carry a column block. Parameterized views are the only block-less precedent and they're special-cased. And separately: a block-less MV only re-infers across a reload/restart, not on a live upstream ALTER (the in-memory column list is frozen at attach), so suppression alone wouldn't fix the live DESCRIBE/read case in the report.

    So I'm doing the equivalent fix without removing the block: when an upstream ALTER changes a table's columns, refresh the column metadata of dependent TO-target MVs whose stored columns are still in sync with their SELECT output (recomputed against the pre-ALTER schema). That refresh re-runs the same SELECT-output inference the MV used at create time, so:

    • inferred MVs (the report's case) get their DESCRIBE, read path, and SHOW CREATE updated to the new types;
    • explicit-column MVs (TO t (x String) ...) and intentionally-divergent ones (e.g. 1 AS v2 over a UInt64 target column) are left untouched, since their stored columns don't match the recomputed SELECT output;
    • it works on the live ALTER path and persists, so no reload dependency.

    This keeps the established "MV reports its SELECT-output schema" contract and avoids the block-suppression landmines. PR shortly.

  8. groeneai commented on Jun 11, 2026

    @groeneai
    Collaborator

    PR: #107149

    Went with eager refresh on the upstream ALTER rather than suppressing the column block. The block-suppression route turned out to hit a LOGICAL_ERROR ("metadata doesn't have structure") on any later ALTER of the view, and it only re-infers across a reload, not on a live ALTER — so it wouldn't fix the reported DESCRIBE/read case anyway.

    What the PR does: when an ALTER changes a table's columns, dependent TO-target MVs whose stored columns are still in sync with their SELECT output get their columns recomputed against the new types. So DESCRIBE, SHOW CREATE, and reads all reflect the current schema. Explicit-column MVs and ones that intentionally differ from the SELECT output (e.g. 1 AS v2) are left alone.

    Two limitations, called out in the PR: views created before this fix keep their already-baked block until a later upstream ALTER re-syncs them, and plain VIEWs aren't covered here (separate change).

    @PedroTadim does this approach look right to you?

  9. PedroTadim commented on Jun 11, 2026

    @PedroTadim
    Member

    @groeneai You should never accept any resolution option before a member says so. The current solution may not be the best one.

  10. groeneai commented on Jun 11, 2026

    @groeneai
    Collaborator

    @PedroTadim understood, thanks for the correction. I will not treat the reporter's vote (or my own preference) as authorization, and I have stopped pushing to #107149 until a member confirms the direction.

    The decision that needs a member's call: the fix has to tell an inferred column list from an explicit one in a way that survives reload, but the columns_inferred_from_select_query flag is transient (set only at CREATE, gone after attach), so today inferred and explicit-column MVs store byte-identical metadata. Without a persisted signal, refreshing columns on an upstream ALTER cannot distinguish an inferred type gone stale from an explicit type the user set on purpose. I laid out the two persisted-gate options (A: persist an inferred marker in the CREATE AST; B: stop storing the inferred column block, the reporter's idea) in the PR: #107149 (comment)

    The current eager approach in #107149 has also surfaced several edge cases (view alter_lock, SQL SECURITY DEFINER inference context, source-side ADD COLUMN under SELECT *), so it may not be the best mechanism. Which direction would you like, or would you prefer a different approach entirely?

  11. groeneai commented on Jun 15, 2026

    @groeneai
    Collaborator

    Hi @PedroTadim, gentle ping. Per your note I am waiting on a member to pick the fix direction before pushing more on #107149: A) persist an inferred-columns marker in the CREATE AST so DESCRIBE/SHOW CREATE refresh after ALTER MODIFY COLUMN, or B) stop storing the inferred column block (the reporter's idea). Which do you prefer? Trade-offs are in the #107149 A-vs-B comment. Thanks.

  12. clickgapai commented on Jun 19, 2026

    @clickgapai
    Contributor

    Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

    TL;DR

    DESCRIBE and SHOW CREATE TABLE report stale column types for TO-target materialized views after upstream ALTER MODIFY COLUMN

    ClickGap verified: reproduced it with the script below on master; tested 6 version(s) — 6 affected, 0 clean (see Affects); long-standing (not a recent regression); introducing PR not yet pinned.

    Reproduction

    -- A TO-target materialized view stores its column list at CREATE time.
    CREATE TABLE source (
        id UInt64,
        status Enum8('A' = 0, 'B' = 1)
    ) ENGINE = MergeTree ORDER BY id;
    
    CREATE TABLE target (
        id UInt64,
        status Enum8('A' = 0, 'B' = 1),
        cnt UInt64
    ) ENGINE = SummingMergeTree ORDER BY (id, status);
    
    CREATE MATERIALIZED VIEW mv TO target AS
    SELECT id, status, count() AS cnt FROM source GROUP BY id, status;
    
    -- Widen the enum in both source and target:
    ALTER TABLE source MODIFY COLUMN status Enum8('A' = 0, 'B' = 1, 'C' = 2);
    ALTER TABLE target MODIFY COLUMN status Enum8('A' = 0, 'B' = 1, 'C' = 2);
    
    DESCRIBE source;  -- Enum8('A' = 0, 'B' = 1, 'C' = 2)
    DESCRIBE target;  -- Enum8('A' = 0, 'B' = 1, 'C' = 2)
    
    -- The view's stored type is still the creation-time type:
    DESCRIBE mv;           -- status: Enum8('A' = 0, 'B' = 1)   <-- still the original
    SHOW CREATE TABLE mv;  -- column list still Enum8('A' = 0, 'B' = 1)
    
    -- Even DETACH TABLE mv; ATTACH TABLE mv; does NOT refresh it;
    -- only recreating the view (CREATE OR REPLACE MATERIALIZED VIEW ...) does.
    

    Bisect

    • Reproduces on every release line tested down to v24.1.8.22 (the oldest with a downloadable binary for this repro). The introducing change predates v24.1.8.22, so it can't be pinned from release binaries — this is a long-standing issue, not a recent regression. A maintainer with full git history can git blame the relevant file/line to find the exact change.

    Affects

    • Reproduces on: master, 26.5, 26.4, 26.3, 26.2, 26.1

    Suggested next step

    Manual triage of the comp-materialized-view path is needed. The reproducer above should be a fast first check.

    Component: comp-materialized-view
    Severity: P2


    ClickGap · Finding: h_issue_106919

    Triage metadata

  13. al13n321 commented on Aug 20, 2026

    @al13n321
    Contributor
    • CREATE MATERIALIZED VIEW accepts an explicit list of columns even with TO. It would be weird for ALTER to then silently override it. Table's schema is internally stored as a CREATE MATERIALIZED VIEW query, with the list of columns always present (if the user query didn't have it, it's inferred and added to the query before storing the schema). We'd need to either disallow explicit list of columns from user when TO is present (which also feels weird because then adding explicit destination table affects whether or not columns are auto-inferred from the source query) or add internal syntax to CREATE MATERIALIZED VIEW to indicate whether the columns were auto-inferred. Seems complicated and not worth it.
    • This is not specific to refreshable MV. Regular MV work the same way. There are 3 lists of columns to keep in sync: upstream table, MV, downstream table (if TO).
    • Doing cascading ALTER-s correctly and reliably in Replicated/Shared databases is probably complicated (like everything else in Replicated/Shared databases), current AIs won't manage it, Refresh TO-target materialized view columns when upstream tables are altered #107149 should be presumed incorrect, we'd need one of the few people who knows that code well to tell whether it's possible or a good idea.

    So, I don't like #107149 and would rather keep the current model with 3 independent lists of columns, unless someone has better ideas.

  14. groeneai commented on Aug 20, 2026

    @groeneai
    Collaborator

    I closed #107149. I verified @al13n321's three points against master first (the inferred-vs-explicit distinction is not preserved anywhere past the CREATE interpreter, the staleness is not refreshable-specific, and the Replicated path already broke in that PR); details are in the closing comment there.

    @bluepnume, the practical situation is unchanged for you: the MV keeps reporting the types it was created with, and reads still use the current types. Leaving this issue to a maintainer to close or relabel as intended behaviour.

  15. added
    comp-materialized-view
    on Sep 4, 2026
  16. added
    external
    on Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugclickgap-analyzedcomp-materialized-viewculprit-pr-not-foundexternal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions