Skip to content

CREATE TABLE ... AS SELECT and MODIFY QUERY do not inherit the serialization of a Quantized codec - #127801

Open
groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/ctas-quantized-codec-serialization
Open

groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/ctas-quantized-codec-serialization

Conversation

@groeneai

@groeneai groeneai commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #127795

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A table created by CREATE TABLE ... AS SELECT from a column with the experimental Quantized codec now gets a plain column, the same as its stored definition, so a Log table created this way stays readable after a restart. The same applies to the columns of a materialized view after ALTER TABLE ... MODIFY QUERY.

Description

On master, CREATE TABLE ... ENGINE = Log AS SELECT from a Quantized column is refused, although the new table declares no codec. Before #127795 it was created and could not be read after a reload:

SET enable_quantized_codec = 1;
CREATE TABLE src (id UInt32, vec Array(Float32) CODEC(Quantized('int8', 8))) ENGINE = MergeTree ORDER BY id;
INSERT INTO src SELECT number, arrayMap(j -> toFloat32((number * 7 + j) % 13), range(8)) FROM numbers(4);
CREATE TABLE c ENGINE = Log AS SELECT * FROM src; -- Code: 44 ILLEGAL_COLUMN (before #127795: Code 91 after DETACH/ATTACH)

The codec attaches SerializationQuantizedVector to the column's type object, without a custom name. CTAS builds the new columns from the SELECT sample block, so the new table gets that serialization but stores a plain Array(Float32) with no codec. Log now refuses it (before, it wrote an extra vec.quantized stream whose marks did not match after a reload). A MergeTree target, or a VIEW, exposes vec.quantized only until the reload, and so does a materialized view after ALTER TABLE ... MODIFY QUERY, which infers its columns from the new SELECT the same way. The same happens when the column is wrapped ([vec], tuple(vec), map(...), groupArray, JOIN, UNION, EMPTY AS SELECT).

The CTAS branch and the MODIFY QUERY branch of AlterCommand::apply now re-create every inferred type that carries this serialization from its name, which is the type a reload parses. Custom names that are part of the type, such as SimpleAggregateFunction(anyLast, Array(Float32)), are kept. Other types are returned unchanged, and the source column keeps its subcolumn.

The AS SELECT statement of #127795's 05452_log_buffer_quantized_codec now creates a plain table instead of expecting ILLEGAL_COLUMN.

The new test fails on master (ILLEGAL_COLUMN at the Log CTAS) and passes 50/50 with randomized settings.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/127801]

groeneai and others added 2 commits October 26, 2026 00:33
…AS SELECT

The `Quantized(...)` codec attaches `SerializationQuantizedVector` to the
column's type object as a nameless customization. Query analysis passes that
object on, so `CREATE TABLE ... AS SELECT` from such a column (also wrapped in
Array/Tuple/Map, through a JOIN, UNION, subquery, `EMPTY AS SELECT`, a VIEW)
created the new table with the codec's serialization but without the codec.
The stored definition is a plain `Array(Float32)`, so the in-memory table and
the reloaded one disagreed: a `Log` table wrote a `vec.quantized` stream and
marks for it, and after a restart or DETACH/ATTACH every read failed with
SIZES_OF_MARKS_FILES_ARE_INCONSISTENT; a MergeTree target exposed
`vec.quantized` only until the reload.

An inferred column takes only the type from the SELECT, so the CTAS branch now
re-creates any inferred type that carries the serialization from its name,
which is exactly what a reload parses. Custom names that are part of the type
(`SimpleAggregateFunction(anyLast, Array(Float32))`, tuple element names) are
kept; types without the serialization are returned unchanged.
`ALTER TABLE ... MODIFY QUERY` infers a view's columns from the new SELECT the
same way and gets the same treatment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the CTAS inference comment and the removeQuantizeSerializations declaration comment to one line each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Three review rounds, an independent cold review and a second-model gate in each. The last round (head 36d53471588ba) had no findings.

# Sev Finding Verdict Evidence / action
1 ❌ ALTER TABLE ... MODIFY QUERY re-infers a materialized view's columns from the new SELECT and keeps the Quantized serialization that the stored definition lacks AGREE Measured: vec.quantized through a TO view resolved only between MODIFY QUERY and the next reload. Fixed with the same helper in the MODIFY_QUERY branch of AlterCommand::apply, plus a test case.
2 ⚠️ The MODIFY QUERY case covered only an ordinary materialized view AGREE The branch also handles refreshable views (the leak was measured on master, and the fix covers it). Added a refreshable TO view case. A post-reattach assertion was not added because it passes without the fix.
3 💡 Coupling with #127795: its five CTAS statements expecting ILLEGAL_COLUMN will create plain tables noted Disclosed in the description. Whichever PR merges second updates them.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. A Log CTAS from a Quantized('int8') column, then DETACH/ATTACH (or a restart), gives Code: 91 SIZES_OF_MARKS_FILES_ARE_INCONSISTENT every time.
b Root cause explained? The codec's serialization sits on the shared type object with no custom name. CTAS copies the object but not the codec, so the new table writes an extra quantized stream that its stored definition does not have.
c Fix matches root cause? Yes. CTAS inference re-creates such types from their name, so the in-memory table equals the reloaded one. The read path is not guarded.
d Test intent preserved / new tests added? New test 05453_quantized_codec_create_as_select: Log (plain, EMPTY AS SELECT, Array/Tuple/Map), MergeTree, a SimpleAggregateFunction custom name, MODIFY QUERY on a TO materialized view and on a refreshable one, and the source keeping its subcolumn.
e Both directions demonstrated? Fails on the unpatched build of the same tree (Code 91, and in-session vec.quantized resolves). Passes with the fix, 50/50 with random settings.
f Fix is general across code paths? One branch serves AS SELECT, EMPTY AS SELECT, TEMPORARY, OR REPLACE and views; all measured. ALTER TABLE ... MODIFY QUERY, which re-infers a materialized view's columns from the new SELECT, gets the same treatment. AS table, MV inner tables and explicit-column INSERT SELECT were already consistent.
g Fix generalizes across inputs (params/datatypes/wrappers)? Array, Tuple, Map, groupArray, any, if, coalesce, JOIN, UNION and subquery all read back after reload. A custom-named SimpleAggregateFunction keeps its name.
h Backward compatible? (maintainer-approved exception only) No setting or format change. vec.quantized no longer resolves in-session through a CTAS table or a view; it never resolved after a reload.
i Invariants and contracts preserved? A type carries the codec's serialization only when its own column has the codec. Other types are returned as the same object, and the source type is never modified.

Session id: cron:clickhouse-impl-self-slot-57:20261026-003000

@clickhouse-gh

clickhouse-gh Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [583c57d]

Summary: ✅


AI Review

Summary

Reviewed PR #127801 at 583c57da7dbb552a62e08e7faae32bf5cc6c3d59, including all prior discussion and the current code. The change removes inherited Quantized codec serialization from column types inferred by CREATE TABLE ... AS SELECT and ALTER TABLE ... MODIFY QUERY, keeping the live schema consistent with persisted definitions. No actionable findings. The new regression test 05453_quantized_codec_create_as_select passed in the head commit's Fast test report. No local build or tests were run.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 26, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @shankar-iyer @Algunenano, could you review this and add the can be tested label so CI runs? CREATE TABLE ... AS SELECT and MODIFY QUERY copied the SerializationQuantizedVector that the codec attaches to the source type, but not the codec, so a Log table created this way wrote an extra vec.quantized stream and became unreadable after a reload; inferred types are now re-created from their name.

@clickhouse-gh clickhouse-gh Bot added the comp-codecs Compression/encoding codecs (LZ4/ZSTD/etc.), archive formats (tar/zip/7z), codec selection and IO... label Oct 26, 2026
@shankar-iyer shankar-iyer self-assigned this Oct 26, 2026
@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label Oct 26, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - d9d8bc5

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (amd_debug, sequential) / 05218_text_index_substreams_opened_lazily flaky, not from this PR (it changes only how CREATE TABLE ... AS SELECT and MODIFY QUERY infer column types for Quantized codecs): in_range_above_threshold 1 -> 0; the runner's random-settings diagnosis isolates use_columns_cache = 1 (21/21 fail with the draw, 23/23 pass without). The same failure is on 64 other PRs and 17 master runs in the last 3 days #127781 (mine, open)
Stateless tests (arm_binary, sequential) / 05218_text_index_substreams_opened_lazily same flaky test, same culprit use_columns_cache = 1 (25/25 fail with the draw, 27/27 pass without) #127781 (mine, open)
Upgrade check (amd_release) / Error message in clickhouse-server.log (see upgrade_error_messages.txt) flaky fixture leak, not from this PR: during the stress phase on the previous release an injected memory-limit error stopped the REPLACE PARTITION of test 05259 while cleanup was stopped, so test_1.t_replace_cover_rep kept uncovered parts and the upgraded server attached it read-only with Code 231 (TOO_MANY_UNEXPECTED_DATA_PARTS) #127843 (mine, open)
Mergeable Check, PR praktika rollups of the rows above #127781, #127843 (the owners of the rows above)

Fast test, Style check and all five Stateless tests (*, flaky check) jobs passed, including the new test 05453. The two Config Workflow / Pre Hooks rows in CIDB are from the runs before the can be tested label was added; the re-run passed it and Finish Workflow. CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261026-153005

@shankar-iyer

Copy link
Copy Markdown
Member

@groeneai Check now after merge of #127795

groeneai and others added 2 commits November 16, 2026 03:19
With this branch, a table created AS SELECT from a Quantized column gets
the type without the codec's serialization, so Log no longer refuses it.
The statement no longer differs between an Atomic and a Replicated
database, so it runs in the test's own database.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

I merged master (with #127795) and updated its test: 583c57d

The only interaction is the ENGINE = Log AS SELECT tuple(vec) statement in 05452_log_buffer_quantized_codec. With this PR the CTAS column has no Quantized serialization, so it creates a plain table instead of failing with ILLEGAL_COLUMN. It now runs in the test's own database, since Atomic and Replicated behave the same.

The PR is still needed after #127795: on master that CTAS is refused although the new table declares no codec, and a MergeTree CTAS, or a materialized view after MODIFY QUERY, shows vec.quantized only until a restart.

Measured on master with #127795: 05452 and 05453 pass 50/50 with random settings; without the fix both fail with ILLEGAL_COLUMN.

@clickhouse-gh

clickhouse-gh Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 583c57da7 with master 1e0344d69 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

1152 translation units recompiled, 10310 s compile time in total, 1152 of them have a recent master baseline.

Job report

This branch has not been deployed

No deployments
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 comp-codecs Compression/encoding codecs (LZ4/ZSTD/etc.), archive formats (tar/zip/7z), codec selection and IO... pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants