Repository navigation
Add ALTER TABLE ... RECOMPRESS COLUMN - #109453
alexey-milovidov wants to merge 154 commits into
Conversation
Introduce a new `ALTER TABLE ... RECOMPRESS COLUMN col` statement that re-compresses the existing data of a column with the column's current compression codec. Changing a column's codec with `MODIFY COLUMN col CODEC(...)` is a metadata-only operation: the new codec applies to newly written data, while data already stored in existing parts keeps its old codec until the parts happen to be merged. `RECOMPRESS COLUMN` rewrites the data of `col` in existing parts so that it is compressed with the codec currently set in the table metadata. Because a compression codec does not change the serialized representation of a column, for `Wide` parts the recompression is performed without deserializing the values: each compressed block of every substream `.bin` is decompressed and re-compressed one-to-one with the new codec, which keeps the decompressed content and granule boundaries byte-identical, so the marks file only needs its compressed offsets remapped (the decompressed offsets, per-granule row counts, the primary index and skip indexes are preserved and hardlinked). The decompressed content is unchanged, so the `uncompressed_size`/`uncompressed_hash` checksums are carried over from the source part and only the on-disk `file_size`/`file_hash` are recomputed. `Compact` parts (and dynamic-subcolumn types) cannot recompress a single column in isolation, so they fall back to a normal whole-part re-serialization that writes every column with its current codec. Implemented as a mutation. Closes: #109432 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
Workflow [PR], commit [b9b452a] Summary: ❌
AI ReviewSummaryAdds experimental Findings
Missing context / blind spots
Tests
Final VerdictStatus: Enforce memory limits for the per-block tables and marks buffers before merging. |
Two issues found in review of ALTER TABLE ... RECOMPRESS COLUMN: 1. Heap-buffer-overflow (caught by the `amd_asan_ubsan` stress test). The wide-part fast path in `RawCompressedBlockReader::readBlock` sized the decompression buffer to exactly the decompressed size. LZ4 decompression (`wildCopyFromInput`) writes in 8-byte chunks and legitimately overruns the end of the output by up to `getAdditionalSizeAtTheEndOfBuffer` bytes, so a full ~1 MiB block wrote a few bytes past the `PODArray` end. Reserve that trailing slack (as `CompressedReadBuffer::nextImpl` does) while keeping the logical `size` at the exact decompressed length, which callers use as the block's decompressed size. 2. Inherited default codec silently ignored. A column without an explicit `CODEC(...)` was recompressed with the part's stored `default_codec`, which is not updated when the table's `default_compression_codec` setting changes, so `RECOMPRESS COLUMN` did nothing after such a change. Route those columns through the whole-part rewrite, which re-serializes every column with the current effective codec (`getCompressionCodecForPart`) and rewrites `default_compression_codec.txt`. The stateless test now also recompresses an LZ4-compressed source (exercising the decompress path that overflowed) and an inherited-default column after a `default_compression_codec` change. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109453&sha=ca89ad937baf9f199287b0e7dc09521a6890d62a&name_0=PR&name_1=Stress%20test%20%28amd_asan_ubsan%29 PR: #109453 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
On a table with a `UNIQUE KEY`, `RECOMPRESS COLUMN` of a `Compact` part (or of a column that inherits the table's default codec) goes through the whole-part rewrite path (`MutateAllPartColumnsTask`), which only hardlinks checksummed files. The `delete_bitmap_*.rbm` sidecars are not checksummed, so they would be dropped, resurrecting deleted rows. Reject the command universally on `UNIQUE KEY` tables in `MergeTreeData::checkMutationIsPossible` (the gate runs before per-part dispatch), mirroring the existing guards for `MATERIALIZE COLUMN` / `CLEAR COLUMN` on such tables. A sidecar-preserving implementation is left for a follow-up. PR: #109453 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the AI-review findings and the stress-test crash (pushed in
Regression coverage: The |
# Conflicts: # src/Storages/MergeTree/MergeTreeData.cpp
…t rewrite Addresses the AI-review blocker on the wide-part fast path: the `recompress_needs_full_rewrite` gate only checked `!column_desc->codec`, so a column with an *explicit* codec AST that references `Default` -- either directly (`CODEC(Default)`) or inside a pipeline (`CODEC(Delta, Default)`) -- kept using the wide in-place fast path. That path resolves the codec against the part's stored `default_codec`, which is not updated when the table's `default_compression_codec` setting changes, so `ALTER TABLE ... MODIFY SETTING default_compression_codec = 'ZSTD'` followed by `ALTER TABLE ... RECOMPRESS COLUMN x` was still a no-op on wide parts for those columns. `Default` resolves through the `current_default` argument in `CompressionCodecFactory::get`, exactly like a column with no explicit `CODEC`. New helper `codecDependsOnDefault` detects such codec ASTs (mirroring how the factory parses them) and routes those columns through the whole-part rewrite, which re-serializes every column with the current effective codec (`getCompressionCodecForPart`) and rewrites `default_compression_codec.txt`. Regression test `04506_recompress_column_default_codec_ast` covers `CODEC(Default)` and `CODEC(Delta, Default)` on wide parts after a `default_compression_codec` change. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the remaining AI-review blocker (default-dependent explicit codecs) in The Since Regression coverage: new |
`RECOMPRESS COLUMN` recompresses a column's data streams one column at a time (`recompressColumnStreams`). With `share_nested_offsets` enabled, `Nested` siblings such as `n.a` and `n.b` share a single on-disk offsets stream (`n.size0`), so recompressing both in one `ALTER` reached that shared `.bin`/marks pair once per sibling and rewrote it twice; whichever pass ran last decided the codec of the shared stream. The data stays readable -- the codec is self-describing and each pass writes a self-consistent block/marks pair, so `CHECK TABLE` passes -- but the shared stream was written more than once and its codec was non-deterministic. Share a `recompressed_streams` set across the per-column calls so that every on-disk stream is rewritten exactly once, by the first column that reaches it -- mirroring the wide-part writer, which also writes a shared offsets stream only once. New test `04600_recompress_column_nested_shared_offsets` recompresses two `Nested` siblings with different codecs (and, separately, only one sibling) on wide parts and validates the data, the rewritten marks, and CHECK TABLE. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the remaining AI-review blocker (
The data itself stayed readable — the compression codec is self-describing via the block header, and each pass always reads the pristine source stream and writes a self-consistent block/marks pair, so Regression coverage: new |
`ALTER TABLE ... RECOMPRESS COLUMN` of `Nested` siblings that share a single offsets stream (`share_nested_offsets = 1`) already rewrote that shared `n.size0` stream exactly once (via the `recompressed_streams` dedup), but the sibling whose codec won was taken from `columns_to_recompress`, a `NameSet`. Its iteration order is `std::unordered_set` hash-bucket order, so for siblings with different codecs the shared stream got an arbitrary codec from run to run -- neither the `ALTER` query order nor any stable schema order. Build `ctx->columns_to_recompress` by iterating the part's stored-column order and filtering by the recompress set, so the first sibling in stored order deterministically owns the shared stream, matching how a fresh write of the part assigns it. Add `04601_recompress_column_shared_offsets_codec`, which asserts the shared `n.size0` stream takes the schema-first sibling's codec (`ZSTD`) regardless of the column order in the `ALTER`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the remaining AI-review blocker (non-deterministic codec for the shared The earlier Fix: New test |
`ALTER TABLE ... RECOMPRESS COLUMN` is parsed straight into a `MutationCommand` and never goes through the `AlterCommands` validation, so an unknown / `ALIAS` / `EPHEMERAL` target used to slip into the per-part mutation path: on wide/full parts the ALTER silently did nothing (the column is simply absent from the part), and on compact/non-full parts it fell through to a whole-part rewrite of every unrelated physical column. Validate the target in `MergeTreeData::checkMutationIsPossible` (before per-part dispatch): reject an unknown name with `NO_SUCH_COLUMN_IN_TABLE` and an `ALIAS` / `EPHEMERAL` column with `BAD_ARGUMENTS`, since neither has an on-disk stream to recompress. Addresses the AI review "Request changes" verdict on the PR. Adds `04602_recompress_column_non_physical_guard`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the AI review
Fix ( New test |
`forEachColumnStream` recomputed each data stream's on-disk file name from the table's *current* `replace_long_file_name_to_hash` / `max_file_name_length` settings. When those settings change after a part is written, the recomputed name no longer matches the file actually on disk: the wide fast path then treated the stream as absent and silently skipped it, so `RECOMPRESS COLUMN` became a no-op that left the column untouched. Resolve the stream name against the source part's recorded files (its checksums) via `IMergeTreeDataPart::getStreamNameForColumn`, which tries both the plain and the hashed name (and the alternative stream-file-name settings) and returns the one the part actually has. This subsumes the previous existence check. Addresses an AI review finding on `MergeTreeColumnRecompression.cpp`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`ALTER TABLE ... RECOMPRESS COLUMN` re-compresses a column's on-disk data streams. A `Memory` table keeps column data in RAM and has no on-disk streams, so `StorageMemory::mutate` dropped the command from its command list. Since `StorageMemory::checkMutationIsPossible` did no validation, the statement succeeded as a silent no-op, breaking the feature's contract (and the same for `MaterializedView` targets that forward mutations to a `Memory` table). Reject `RECOMPRESS_COLUMN` up front in `StorageMemory::checkMutationIsPossible` with `NOT_IMPLEMENTED`, so a bad `ALTER` fails loudly before any mutation is submitted (a combined `ALTER ... UPDATE ..., RECOMPRESS COLUMN ...` is rejected as a whole, and other supported mutations on `Memory` are unaffected). Addresses an AI review finding on `StorageMemory.cpp`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the AI review verdict at `610205723d3`. Pushed `4f32ff3ab8e` + `e90b68d6d3a`. Blocker 1 — Major — Blocker 2 —
Happy to add an explicit guard rejecting the combination if you'd prefer defense-in-depth, but it looks unreachable today. |
|
Updating the branch to fix the |
|
The fix is in progress: #127925 (test only).
@ fm4v reviewed it and confirmed the root cause against master; it needs an approval to merge. |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109453&sha=2f858e67031471c587a86a202291089d1e96886d&name_0=PR&name_1=AST%20fuzzer%20%28amd_release%2C%20oracle%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The failure is an The |
|
The fix is in progress: #119465 This is not truncation. The data changed between the reads (server.log). The fuzzer had created #119465 records the active parts of each |
# Conflicts: # src/Storages/MergeTree/MutateTask.cpp
…ecompress_column` `ALTER TABLE ... RECOMPRESS COLUMN` runs a new part rewriter (compressed block re-emission, mark remapping, checksum carry-over), so it is now experimental and disabled by default. The setting is checked at submission time in `InterpreterAlterQuery`, next to `checkStatisticsMutationsAreAllowed`, with the submitting session's settings; mutations that were already accepted keep executing in the background regardless of it. Existing tests enable the setting; a new test checks the rejection and the opt-in. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…recompress_column_unaligned_source_blocks` The style check `server_data_manipulation_in_stateless_tests` misparsed the reassignment `CLICKHOUSE_LOCAL="$CLICKHOUSE_LOCAL ..."`: it stripped the `clickhouse-local` invocation up to the closing quote, left an unbalanced quote, and then took the scratch-directory `data_paths` query of `clickhouse-local` for a query to the server. Pass the setting on each `clickhouse-local` call instead. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109453&sha=latest&name_0=PR&name_1=Style%20check Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ettings` Fixes the `cpp` style check. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109453&sha=latest&name_0=PR&name_1=Style%20check Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109453&sha=0184d47dc745ab7c5170a983847f105da4b7bb73&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.
|
|
The fix is already in progress: #126995 (mine, open, CI green on its current head, waiting for a review). This run has the same cause as the one I described in #116234 (comment). The server log of this job (server time):
The pool process inherits the pseudo-terminal master ( |
# Conflicts: # src/Compression/CompressionCodecMultiple.cpp
…ent ownership precisely `codecDependsOnDefault` now looks into the alternatives of `FirstApplicable` and `Multiple` recursively: `CompressionCodecFirstApplicable` resolves a nested `Default` against the table default too, so `CODEC(FirstApplicable(T64, Default))` on a `String` column must take the whole-part rewrite after a change of `default_compression_codec`. `MergeTreeData::checkLossyRecompressionIsPossible` now resolves the stored column that owns a required name by type (`ColumnsDescription::tryGetColumnOrSubcolumn` and `getNameInStorage`) instead of `Nested::tryGetColumnNameInStorage`, which picks the first stored prefix: with columns `a` and `a.b Tuple(x Float64)`, `a.b.x` was attributed to `a`, so a lossy recompression of `a.b` kept a stale skip index or `MATERIALIZED` column computed from `a.b.x`. Subcolumns of `ALIAS` columns, which `ColumnsDescription` does not register, are resolved by their types. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The flaky check reported "Test runs too long (> 180s)": https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109453&sha=091531cbbfa112642467e8a4c22b8bb152efbba4&name_0=PR&name_1=Stateless%20tests%20%28amd_binary%2C%20flaky%20check%29 PR: #109453 Only one of 37 runs was slow: the randomized `index_granularity = 6` produced 3335 granules for 20000 rows, and each `RECOMPRESS COLUMN` (whole-part rewrite) took ~111 s, of which only ~12 s was CPU time; the rest was waiting under the CI thread fuzzer. The runs with 201 granules took ~1 s. Pin `index_granularity` and `index_granularity_bytes` so the part has a handful of granules. Co-Authored-By: Claude Opus 5.5 <[email protected]>
It ran longer than the 15 s limit for new tests in `Fast test (arm_darwin)`: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109453&sha=c879cf47531328df3499ab9bae1870aa2bd9f03c&name_0=PR&name_1=Fast%20test%20%28arm_darwin%29 #109453 Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/Storages/MergeTree/MutateTask.cpp # src/Storages/MutationCommands.h
…COMPRESS COLUMN` docs The documentation example threw `SUPPORT_IS_DISABLED` with default settings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109453&sha=806162ffe23baed3bee142174285056d8247ab1d&name_0=PR&name_1=Integration%20tests%20%28arm_binary%2C%202%2F5%29 ( |
|
The fix is already merged: #131921 (merged 2026-11-19 00:48 UTC, commit d9c002e). Root cause: in the This head (806162f) and the failing runs on #129180, #129199, #132536 and #126329 all predate the merge, so they don't have the fix. |
# Conflicts: # tests/queries/0_stateless/01271_show_privileges.reference
|
🕵 Merged the latest The failure of |
…the settings history The current version on master is now 26.12, and the `settings_changes_history` style check requires a newly added setting to be recorded under the current version. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109453&sha=af1d3207d0b4bdb43bec9e1ee521966639ec3714&name_0=PR&name_1=Style%20check #109453 Co-Authored-By: Claude Fable 5.2 <[email protected]>
| std::unordered_map<UInt64, UInt64> source_block_starts; | ||
| /// (position in the decompressed stream, offset in the recompressed file) of the start of every | ||
| /// block of the recompressed file, in ascending order. | ||
| std::vector<std::pair<UInt64, UInt64>> new_block_starts; |
There was a problem hiding this comment.
[Major] Enforce memory limits for the raw rewriter's metadata
source_block_starts and new_block_starts retain entries for every compressed block until the entire stream finishes, but their ordinary container allocations do not enforce MemoryTracker limits with the default server settings. After the input/output scratch buffers reach capacity, an aligned NONE-to-NONE recompression can keep processing blocks without another enforcing allocation: the codecs only copy bytes, and check_cancelled only checks cancellation. A large part with small compression blocks can therefore grow these tables past max_server_memory_usage and exhaust process memory instead of failing the mutation.
The same issue applies to the two complete marks buffers in rewriteMarksFile (String old_marks and WriteBufferFromOwnString), which grow with the number of marks. Please use UnorderedMapWithMemoryTracking / VectorWithMemoryTracking for these internal tables and enforcing or bounded buffers for the marks, or process this metadata incrementally. This is source-traced; I have not reproduced an OOM in this job.
Closes: #109432
Introduce a new
ALTER TABLE ... RECOMPRESS COLUMN colstatement that re-compresses the existing data of a column with the column's current compression codec.Changing a column's codec with
MODIFY COLUMN col CODEC(...)is metadata-only: the new codec applies to newly written data, while data already stored in existing parts keeps its old codec until the parts happen to be merged.RECOMPRESS COLUMNrewrites the data ofcolin existing parts so that it is compressed with the codec currently set in the table metadata.Because a compression codec does not change the serialized representation of a column, for
Wideparts the recompression is done without deserializing the values: each compressed block of every substream.binis decompressed and re-compressed one-to-one with the new codec. This keeps the decompressed content and granule boundaries byte-identical, so the marks file only needs its compressed offsets remapped (the decompressed offsets, per-granule row counts, the primary index and skip indexes are preserved and hardlinked). The decompressed content is unchanged, so theuncompressed_size/uncompressed_hashchecksums are carried over from the source part and only the on-diskfile_size/file_hashare recomputed.Compactparts (and dynamic-subcolumn types) cannot recompress a single column in isolation, so they fall back to a normal whole-part re-serialization that writes every column with its current codec.Implemented as a mutation. A new
ALTER RECOMPRESS COLUMNgrant is added. The statement is experimental and requires the settingallow_experimental_recompress_column, which is checked when theALTERis submitted.The issue also asks to check
RECOMPRESSTTL mutations, which currently go through a full deserialize/re-serialize merge; that is left for a follow-up.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added
ALTER TABLE ... RECOMPRESS COLUMN col, which re-compresses a column's existing data with its current codec. For wide parts the data is recompressed without deserializing the column values. The statement is experimental and requiresallow_experimental_recompress_column = 1.Workflow [PR]
Sync PR [sync-upstream/pr/109453]