Skip to content

Allow reading MergeTree parts with mixed codecs in one stream - #108592

Merged
rienath merged 2 commits into
masterfrom
mixed-codec
Jun 29, 2026
Merged

rienath merged 2 commits into
masterfrom
mixed-codec

Conversation

@rienath

@rienath rienath commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

This is the read side of the adaptive codec selection RFC (#105404). The write side chooses a codec per block during merges and mutations, producing streams whose consecutive blocks use different codecs. Today the reader rejects such a stream with CANNOT_DECOMPRESS as soon as two consecutive blocks have different method bytes, unless allow_different_codecs is set. This PR makes the reader tolerate different codecs in one stream.

No writer produces mixed-codec parts yet, so on its own this PR has no observable effect. It needs backporting down to 26.3, well before any writer can create such parts. During a rolling upgrade or rollback, a replica running the experimental allow_experimental_adaptive_codec_selection could write a multi-codec column, so every replica that might fetch that part must already be able to read it. Backporting the reader tolerance first closes that window.

Changelog category (leave one):

  • Improvement

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

MergeTree can now read a compressed stream whose blocks use different codecs. This is the read-side prerequisite for adaptive codec selection (#105404).

Version info

  • Backported to: 26.6.2.68, 26.5.6.38, 26.4.5.126, 26.3.17.42

@clickhouse-gh

clickhouse-gh Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4d38aef]

Summary: ✅


AI Review

Summary

This PR makes MergeTreeReaderStream tolerate compressed blocks with different codec method bytes in the same stream, extends data-part checking to the same case, and adds focused gtest/stateless coverage for mixed-codec streams and compact parts. I did not find any actionable correctness, safety, compatibility, or test-coverage issues in the current diff.

Missing context / blind spots
  • ⚠️ I could not run the new unit_tests_dbms filter or stateless test locally because this checkout has no existing build directory. The fetched Praktika PR report showed 0 failed tests at review time.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jun 26, 2026
@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Jun 26, 2026
@CheSema CheSema self-assigned this Jun 26, 2026
@rienath
rienath marked this pull request as ready for review June 26, 2026 12:40
@@ -120,7 +120,7 @@ void MergeTreeReaderStream::init()
return pipeline.build();
},
uncompressed_cache,
settings.allow_different_codecs);
/* allow_different_codecs */ true);

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.

How to be sure that /* allow_different_codecs */ true is set in all places?
Why do we even need reading with /* allow_different_codecs */ false?

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.

How to be sure that /* allow_different_codecs */ true is set in all places?

There is effectively one place to get right. All decompression of part data goes through the MergeTreeReaderStream::init, which is now hardcoded to true. The new gtest pins the uncached CompressedReadBufferFromFile branch of that method. The only other reader of part data is checkDataPart (for CHECK TABLE ...), also set to true. Pinning this one down is very difficult without having adaptive compression. I will add a test in another PR that makes it trivial.

There is no compile time guarantee that some future path for reading part data opts in. But the surface area is small (one reader class).

Why do we even need reading with /* allow_different_codecs */ false?

Because false is a real integrity check. CompressedWriteBuffer takes one codec at construction and stamps it on every block, so every stream we produce outside the mixed codec ones above (+ spliced Distributed spools sent over network + Compact parts) uses a single codec. Examples are marks files, engine backups, metadata, and single Distributed spool files.

Checking checksums on each block does not cover this. A corrupted method byte would be caught, but a stream wrongly assembled from two codecs (a producer bug, or an older or foreign writer) has each block individually valid, so every checksum on each block would pass and only this check catches it. And it runs independently of the checksum, so it still fires when checksums are off (checksum_on_read=0, or http_native_compression_disable_checksumming_on_decompress). It is one comparison per block, so there is no reason to demote it where the stream really does use a single codec.

@clickhouse-gh

clickhouse-gh Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.30% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.50% 77.50% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 76/76 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

This PR removes the allow_different_codecs reader setting and hardcodes the tolerance to always-on in the MergeTree read path. That tolerance branch only does work when adjacent compressed blocks within one stream use different codecs; for the single-codec column streams ClickBench scans, the change is a no-op, so it cannot plausibly shift query timings in either direction. The four flagged queries (improvements on Q4/Q15, regressions on Q31/Q32) are all on this read path yet move both ways, which points to run-to-run variance rather than a real effect, so all four are downgraded to not-sure. No query crosses the large-and-consistent threshold that would force a verdict to stand.

clickbench

⚠️ 4 inconclusive

Flagged queries (4 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 4 not_sure 263 217 -17.5% <0.0001 io: PR only flips a decompress tolerance flag; no-op for single-codec ClickBench columns. -17% is variance.
⚠️ 15 not_sure 246 201 -18.3% <0.0001 io: Same read-path flag flip is a no-op for uniform-codec streams; -18% not plausibly caused here.
⚠️ 31 not_sure 302 334 +10.6% <0.0001 io: +10.6% regression off-path: codec-tolerance flag never triggers for single-codec columns. Variance.
⚠️ 32 not_sure 1330 1408 +5.8% <0.0001 io: +5.8% with noisy measurements; flag flip leaves decompress hot path unchanged for these reads.

q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: e34a9a57-358b-49f0-981d-bfc8e15e53a7
  • MIRAI run: 343dfb49-80f7-404b-bf6a-670ab4251171
  • PR check IDs:
    • clickbench_380807_1782738718
    • clickbench_380821_1782738718
    • clickbench_380827_1782738718
    • tpch_adapted_1_official_380834_1782738718
    • tpch_adapted_1_official_380844_1782738718
    • tpch_adapted_1_official_380906_1782738718

@rienath
rienath added this pull request to the merge queue Jun 29, 2026
Merged via the queue into master with commit 2983797 Jun 29, 2026
339 of 493 checks passed
@rienath
rienath deleted the mixed-codec branch June 29, 2026 15:39
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 29, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @rienath @CheSema — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Jul 4, 2026
@rienath rienath added pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-must-backport Pull request should be backported intentionally. Use this label with great care! and removed pr-backport Changes, backported to release branch. Do not use manually - automated use only! labels Jul 14, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 14, 2026
robot-ch-test-poll added a commit that referenced this pull request Jul 14, 2026
Cherry pick #108592 to 26.3: Allow reading MergeTree parts with mixed codecs in one stream
robot-clickhouse added a commit that referenced this pull request Jul 14, 2026
robot-ch-test-poll added a commit that referenced this pull request Jul 14, 2026
Cherry pick #108592 to 26.4: Allow reading MergeTree parts with mixed codecs in one stream
robot-clickhouse added a commit that referenced this pull request Jul 14, 2026
robot-ch-test-poll added a commit that referenced this pull request Jul 14, 2026
Cherry pick #108592 to 26.5: Allow reading MergeTree parts with mixed codecs in one stream
robot-clickhouse added a commit that referenced this pull request Jul 14, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 14, 2026
Backport #108592 to 26.6: Allow reading MergeTree parts with mixed codecs in one stream
rienath added a commit that referenced this pull request Jul 14, 2026
Backport #108592 to 26.3: Allow reading MergeTree parts with mixed codecs in one stream
rienath added a commit that referenced this pull request Jul 14, 2026
Backport #108592 to 26.4: Allow reading MergeTree parts with mixed codecs in one stream
rienath added a commit that referenced this pull request Jul 14, 2026
Backport #108592 to 26.5: Allow reading MergeTree parts with mixed codecs in one stream
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Allow reading MergeTree parts with mixed codecs in one stream
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

6 participants