Skip to content

Support per-column compression codecs in projections - #120281

Open
robinld wants to merge 165 commits into
ClickHouse:masterfrom
robinld:projection-column-codecs
Open

robinld wants to merge 165 commits into
ClickHouse:masterfrom
robinld:projection-column-codecs

Conversation

@robinld

@robinld robinld commented Sep 15, 2026 •

Copy link
Copy Markdown

Related: #28932
Related: #74234

Projections store their own copy of selected columns, often in a different sort order from the parent table. This PR lets a projection override compression codecs for selected output columns:

CREATE TABLE t
(
    id UInt64,
    ts DateTime,
    PROJECTION p (ts CODEC(DoubleDelta, ZSTD)) AS (SELECT id, ts ORDER BY ts)
)
ENGINE = MergeTree ORDER BY id;

The column list is optional and partial. Each listed name must be an output column of the projection query; its type is inferred from that query. Only CODEC may be declared. The first release permits NONE, LZ4, ZSTD, Delta, and DoubleDelta, with the usual codec and output-type validation under a fixed policy that does not depend on session opt-ins. Explicit types, subcolumns, and CODEC(Default) are unsupported.

The syntax works in CREATE TABLE and ALTER TABLE ... ADD PROJECTION. Replicated tables and ON CLUSTER DDL require allow_projection_column_codecs_in_replicated_or_distributed_ddl = 1; ON CLUSTER also requires distributed_ddl_entry_format_version >= 2, or version 3 for CREATE TABLE ... AS copies. It does not add skip indexes inside projections.

Added stateless tests covering creation, alteration, replication and cluster DDL, and AST JSON round trips. Focused local checks also covered projection part copying, backup and restore, and unavailable-projection behavior.

Changelog category (leave one):

  • New Feature

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

A projection can now override compression codecs for selected output columns by adding an optional column list before the projection query in CREATE TABLE or ALTER TABLE ... ADD PROJECTION. The first release supports NONE, LZ4, ZSTD, Delta, and DoubleDelta; column types are inferred from the projection query, and explicit types, subcolumns, and CODEC(Default) are unsupported. Replicated and distributed DDL requires allow_projection_column_codecs_in_replicated_or_distributed_ddl = 1; distributed DDL also requires distributed_ddl_entry_format_version >= 2, or version 3 for CREATE TABLE ... AS copies.


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

@CLAassistant

CLAassistant commented Sep 15, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@robinld
robinld force-pushed the projection-column-codecs branch 2 times, most recently from 69cd02c to 9af150b Compare September 15, 2026 21:22
@clickhouse-gh clickhouse-gh Bot added comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). external Author is not a member of the ClickHouse organization labels Sep 15, 2026
@KochetovNicolai KochetovNicolai added the can be tested Allows running workflows for external contributors label Sep 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [10724cb]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug, targeted, old_compatibility) FAIL
Logical error: Sort order of blocks violated for column number A, left: B, right: C. Chunk D, rows read E.F (STID: 3413-395e) FAIL cidb
Build profile diff ERROR

AI Review

Summary

This PR adds optional per-column projection codec declarations, binds visible output names to existing physical projection columns, and gates publication through replicated metadata and distributed DDL. Reviewed the current diff and implementation against all 94 prior review threads, their replies, and the conversation. No actionable findings remain under the current, deliberately narrower codec contract.

The new declaration form was traced through parsing, formatting, AST cloning/hashing/JSON, the query fuzzer, CREATE/ATTACH/CREATE TABLE ... AS, ALTER, projection insert/merge/mutation writers, replicated metadata comparison, DDL queues, and metadata restoration. Writers consume the existing column-codec representation; physical projection column names remain unchanged. The PR's Fast test report records 14,376 passing tests and no failures. Additional paired creation/insertion probes using its binary found no confirmed unexpected codec-binding failure.

Final Verdict

Status: ✅ Approve. No new inline findings or thread replies are required.

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Sep 23, 2026
@rienath rienath self-assigned this Sep 23, 2026
Add an optional column list to a projection declaration, accepted by both
`CREATE TABLE` and `ALTER TABLE ... ADD PROJECTION`:

    PROJECTION p (ts CODEC(DoubleDelta, ZSTD)) AS (SELECT id, ts ORDER BY ts)

Only the columns being overridden need to be listed, and `CODEC` is the
only property that may be declared. Declaring a column's type is optional;
doing so pins it against `ALTER TABLE ... MODIFY COLUMN` for as long as the
projection exists.

`system.projections` gains a `codecs` column.
- Extracts `isLossyCodecForType` into CompressionFactory to deduplicate logic between MergeTreeData and ProjectionsDescription.
- Adds `validateDeclaredColumnCodecs` to InterpreterHypotheticalObjectQuery to reject invalid codecs in hypothetical queries rather than silently ignoring them.
- Updates documentation regarding projection column codecs overriding, changing declared codecs, and aggregate projections.
- Adds a stateful mutation test to verify column type changes correctly re-infer codec arguments during projection rebuilds.
@robinld
robinld force-pushed the projection-column-codecs branch from a5f4703 to 7bc8cc4 Compare September 23, 2026 18:03
Comment thread src/Storages/AlterCommands.cpp Resolved
Comment thread src/Storages/AlterCommands.cpp Resolved
Comment thread src/Storages/AlterCommands.cpp Resolved
@robinld
robinld force-pushed the projection-column-codecs branch from e5d5040 to ac0631b Compare September 24, 2026 06:39
Comment thread src/Storages/ProjectionsDescription.cpp Resolved
Comment thread src/Storages/ProjectionsDescription.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing commit 10724cbcd81d09746e2771c8bde22e8b5c5c5258 with master failed: LogClusterNotReady: the endpoint never became ready.

See the job log for details.

Comment thread src/Storages/AlterCommands.cpp Resolved
The Analyzer can rename `countDistinct` to `uniqExact`, while other expression rewrites retain their original spellings in projection metadata. Check both spellings against the stored columns before binding a codec, and reject ambiguous matches.

Cover `countDistinct` writes and reads across `ATTACH TABLE`.

Related: ClickHouse#120281 (comment)
A projection may store both `count()` and `count() IGNORE NULLS`. When a codec is declared only for another output, their alternative spellings need no disambiguation. Keep ambiguity rejection for outputs named by codec declarations.

Add a stateless regression with a codec on the grouping key.

Related: ClickHouse#120281
Comment thread src/Storages/ProjectionsDescription.cpp Resolved
Projection outputs may share one physical column. Require uniqueness only when a codec declaration targets that physical column, so a declaration for another output does not reject an otherwise valid projection.

Exercise creation, writing, and forced reads for aggregate and normal projections.

ClickHouse#120281 (comment)
Comment thread src/Storages/ProjectionsDescription.cpp Resolved
Comment thread src/Storages/ProjectionsDescription.cpp Resolved
Use the original `SELECT` output set for binding when appended `ORDER BY`
expressions expand to multiple columns. Read physical names from the
legacy interpreter's rewritten `SELECT`, which already expands table
`ALIAS` expressions before projection metadata is built.

Add stateless coverage for aggregate and filtered alias outputs, a
visible output under `COLUMNS`, and hidden ordering columns.

Related: ClickHouse#120281
Review: ClickHouse#120281 (comment)
Review: ClickHouse#120281 (comment)
Keep table `ALIAS` and `MATERIALIZED` markers in the temporary analyzer schema while omitting their expressions. This makes `SELECT *` expand to the same visible columns as the projection interpreter and avoids evaluating an alias during `ATTACH TABLE`.

Cover accepted stored output `v` and rejected hidden alias `av`.

Related: ClickHouse#120281
Bring in the upstream fix for flaky `02317_distinct_in_order_optimization_explain` while keeping the projection codec changes on current master.

Related: ClickHouse#125742
Related: ClickHouse#120281
Comment thread src/Storages/ProjectionsDescription.cpp Resolved
Expand projection `SELECT` matchers with the source columns used by the stored projection interpreter. This keeps `ALIAS` and `MATERIALIZED` columns out of matcher expansion while preserving the original expression names used in codec declarations.

Cover `CREATE`, `ALTER`, materialization, and an aggregate expression name in a stateless regression.

Related: ClickHouse#120281 (comment)
Comment thread src/Storages/ProjectionsDescription.cpp Resolved
The Analyzer expands `untuple` into multiple visible outputs. Bind codec
declarations against those resolved outputs instead of requiring the
unexpanded `SELECT` AST to have the same number of children.

Cover `CREATE`, `ALTER`, materialization, and reattach with a projection
that stores codec targets on both sides of `untuple`.

Related: ClickHouse#120281 (comment)
Comment thread src/Storages/ProjectionsDescription.cpp Resolved
The legacy interpreter name identifies each output when present in projection metadata. The Analyzer rewritten name may refer to another stored output, so consult it only when the original is absent. Add a stateless regression for creation, alteration, materialization, and reload.\n\nReview: ClickHouse#120281 (comment)
@CurtizJ CurtizJ self-assigned this Oct 16, 2026
@mintlify

mintlify Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 10724cb.

Approve preview

@robinld
robinld force-pushed the projection-column-codecs branch from 916b357 to d4fe67a Compare October 23, 2026 06:25
…dl` under 26.11

Master is now at 26.11, so the `settings_changes_history` style check requires
the new setting to carry a 26.11 history record. The setting has not shipped
in any release, so its only record moves from 26.10 to 26.11.
@robinld
robinld force-pushed the projection-column-codecs branch from 09384a9 to b137d43 Compare October 23, 2026 16:19
The test asserted that a legacy `CREATE TABLE ... ON CLUSTER ... AS` (with
`distributed_ddl_entry_format_version` 2) succeeds when the source table
exists only on the worker. Master now resolves the inherited definition on
the initiator for that path to check the `TABLE_ENGINE` grant, so the
initiator rejects that scenario by design and the test fails in the flaky
check.

The behavior the test covered is not changed by this PR. The
projection-codec handling of legacy `CREATE ... AS` stays covered by
`05322_projection_codec_legacy_cluster_copy`. Also remove the
`cluster_worker_only` clusters that only this test used.

Related: ClickHouse#118866
…ECT`

For a filtered normal projection, `fillProjectionDescriptionByQuery` replaces
the `ORDER BY` expression appended to the `SELECT` list with the columns that
expression reads. Codec binding still dropped only the last child of that
rewritten list, so ordering inputs remained among the visible outputs:

- `PROJECTION p (k CODEC(ZSTD)) AS (SELECT k, v WHERE v > 0 ORDER BY (k, j))`
  failed with "Column `k` ambiguously names more than one output", in both
  `CREATE TABLE` and `ALTER TABLE ... ADD PROJECTION`.
- `SELECT v WHERE v > 0 ORDER BY (k, j)` accepted a codec on `k`, which is
  not an output of the projection.

Take the visible outputs from the projection's own query, which carries the
single appended `ORDER BY` child. Also stop requiring at least one stored
child after the visible outputs, since an `ORDER BY` that reads no columns
appends none; such a projection now reaches its own sorting-key error.

Reported by the AI review on
ClickHouse#120281
…ted` database

In a `Replicated` database the `CREATE TABLE` is published to Keeper, so the
projection column codec gate rejected it with `SUPPORT_IS_DISABLED` in the
DBReplicated stateless job. Use the same tags as the sibling codec binding
tests, such as `05401_projection_codec_visible_output_binding`.
@robinld

robinld commented Oct 27, 2026

Copy link
Copy Markdown
Author

@CurtizJ any thoughts or guidance on this pr? Happy to meet sometime to talk about it.

@clickhouse-gh

clickhouse-gh Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Dear @CurtizJ, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

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-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). external Author is not a member of the ClickHouse organization pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants