Skip to content

Add CompressionCodecAdaptive with per-block codec selection - #111134

Merged
rienath merged 4 commits into
masterfrom
adaptive-codec
Jul 24, 2026
Merged

rienath merged 4 commits into
masterfrom
adaptive-codec

Conversation

@rienath

@rienath rienath commented Jul 20, 2026 •

Copy link
Copy Markdown
Member

CompressionCodecAdaptive picks the smallest-output codec for each block from a pool (NONE, the deployment default, and type-appropriate candidates) and compresses the block with it. Candidates are priced by CompressedSizeCalculator (#109054). A codec that can compute its compressed size is priced without compressing, the rest compress into scratch and are measured. The winner's method byte lands on disk, so readers decode each block with no knowledge of the wrapper.

This is the selection core of the RFC (#105404): how CODEC(Default) resolves per block. Nothing constructs the codec yet, a follow-up wires it into merges and mutations behind an experimental setting.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

Version info

  • Merged into: 26.8.1.98 (included in 26.8 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f1862a3]

Summary: ✅


AI Review

Summary

This PR adds the internal CompressionCodecAdaptive selection core and good unit coverage around winner selection, hashing, and encrypting defaults, but the wrapper currently does not satisfy the existing ICompressionCodec descriptor contract. It serializes itself as CODEC(Adaptive) even though CompressionCodecFactory cannot rebuild that family, so wiring it through the normal MergeTree default_codec path would fail on omitted-codec streams and would also make default_compression_codec.txt unreadable on part load.

Findings

⚠️ Majors

  • [src/Compression/CompressionCodecAdaptive.cpp:125] CompressionCodecAdaptive exposes a non-round-trippable codec descriptor. Unchanged MergeTree writers obtain default_codec->getFullCodecDesc() for columns without an explicit codec and immediately pass that AST back into CompressionCodecFactory::get, so CODEC(Adaptive) will throw UNKNOWN_CODEC because Adaptive is not a registered family. If such a part were written, IMergeTreeDataPart::loadDefaultCompressionCodec would also fail to parse default_compression_codec.txt and silently fall back to deducing a single codec from on-disk data, which does not preserve per-block adaptivity. Suggested fix: keep the surface descriptor round-trippable (for example preserve CODEC(Default) and detect adaptivity out of band), or avoid using ICompressionCodec descriptor serialization for this wrapper altogether.
Final Verdict

Changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 20, 2026
friend int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size);

/// Return size of compressed data without header
/// Upper bound on the compressed data size without the header. While compressing, a codec may temporarily write more bytes

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Old contract was incorrect

return false;
}

bool CompressionCodecMultiple::isEncryption() const

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't needed before. Now it is because someone can set default_compression_codec to be a chain of codecs one of which uses encryption. In that case, we wouldn't want adaptivity to work, even if it is on and CODEC(Default) is selected for the column

: pool(AdaptiveCodec::poolForType(type, deployment_default))
{
chassert(!pool.empty());
setCodecDescription("Adaptive");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setCodecDescription("Adaptive") makes this wrapper look like a normal codec family, but Adaptive is not registered in CompressionCodecFactory. Unchanged MergeTree writer code round-trips default_codec through getFullCodecDesc() for columns without an explicit CODEC and immediately feeds that AST back into CompressionCodecFactory::get(...) when it builds the stream codec, so the first implicit-default stream would fail with UNKNOWN_CODEC as soon as this wrapper is passed via the existing default_codec slot. If it ever reaches disk, default_compression_codec.txt has the same round-trip problem on part load and falls back to "detect one codec from data", which does not preserve per-block adaptivity. This needs either a descriptor that round-trips through the existing codec interfaces or a separate path that never serializes/reparses CODEC(Adaptive).

@rienath rienath Jul 21, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make much sense. CODEC(Adaptive) doesn't and won't exist. Adaptive is never recorded on disk. We record in metadata that default codec was used and within the blocks the actually used codec.

@clickhouse-gh

clickhouse-gh Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 78.30% 78.30% +0.00%

Changed lines: Changed C/C++ lines covered: 234/245 (95.51%) · Uncovered code

Full report · Diff report

Comment on lines +113 to +115
const bool is_smaller = size < best_size;
best_idx = is_smaller ? i : best_idx;
best_size = is_smaller ? size : best_size;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need ternary operator here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be better without ternary

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed 76ac222

@rienath rienath left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Michicosun. I've replied all the comments. Could you let me know if we can merge?

Comment on lines +113 to +115
const bool is_smaller = size < best_size;
best_idx = is_smaller ? i : best_idx;
best_size = is_smaller ? size : best_size;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rienath

rienath commented Jul 24, 2026

Copy link
Copy Markdown
Member Author

He did in real life: yes

Merged via the queue into master with commit 3a9e924 Jul 24, 2026
350 of 351 checks passed
@rienath
rienath deleted the adaptive-codec branch July 24, 2026 16:35
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 24, 2026
@rschu1ze

Copy link
Copy Markdown
Member

@rienath End user docs (under a big bold "experimental" banner) please :-)

@rienath

rienath commented Jul 27, 2026

Copy link
Copy Markdown
Member Author

Will do, thanks @rschu1ze

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants