Repository navigation
Use actual codec to estimate compressed column sizes for dataflow statistics - #114535
Conversation
`RuntimeDataflowStatistics` estimates how many compressed bytes a read step will read, and `considerEnablingParallelReplicas` turns that into `effective_max_reading_threads` when deciding whether parallel replicas are worth it. Wide parts store per-column sizes, so the estimate reads them off the part. Compact parts store none, and the estimate fell back to compressing a sample of each column with `CompressedWriteBuffer`, which uses the built-in default codec - `LZ4`. That models the wrong thing whenever the data is stored with another codec, and it is wrong in one direction only: `LZ4` compresses worse than `ZSTD`, so the estimate comes out too large. Measured on a compact part of TPC-H `lineitem` columns stored with `ZSTD(1)`, the estimate was 54.0 MB against 35.8 MB really read, and on the sequential integers of the new test 4.52 MB against 1.29 MB - a 3.5x overestimate. The error does not stay proportional either: it inflates `effective_max_reading_threads`, which is what flips the decision to spread a query across replicas that cannot pay for the coordination. A compact part does have measured compressed and uncompressed totals for all its columns together, so use that ratio instead of modelling one. It is codec-correct by construction - including per-column `CODEC` overrides, which are not recorded in the part's columns metadata at all and so cannot be recovered from it - and it costs nothing, where sampling costs a compression pass per block. The assumption it makes in exchange is that columns inside a part compress alike, which matters only when a query reads a subset of them; it is exact for the whole part. Sampling stays for parts with no measured totals, such as those whose checksums are not loaded.
The previous commit derived the compression ratio of a compact part from the part's measured totals. That is codec-correct, but it assumes every column in a part compresses alike, and they do not: on `hits` the ratio ranges from barely compressible to more than an order of magnitude, so a query reading a subset of the columns gets an estimate smeared towards the part average. Measured on two `lineitem` columns stored with `ZSTD(1)`, the part ratio was off by -29.5% and +26.5% respectively. Go back to sampling, but hand `CompressedWriteBuffer` the codec the column is actually stored with instead of letting it use its built-in default. The codec cannot be recovered from the part - `default_codec` is only the part-wide default, and a part's `ColumnsDescription` is built from a `NamesAndTypesList`, which carries no codec at all - so it is resolved from the table metadata that the readers already hold, the same way `IMergeTreeDataPartWriter` resolved it when writing. Only columns that override the part default need an entry, and they are resolved once per read task rather than per block. This keeps per-column resolution and is accurate either way: on the same two columns the estimate is now within 0.2%, and the whole-part reads that the part ratio got exactly right stay within 0.1%.
|
Workflow [PR], commit [aae8153] Summary: ✅
AI ReviewSummaryThe PR changes compact-part sampling in Findings
Tests
Final VerdictLLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 50/56 (89.29%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes69 object files changed (+43.99 KiB total), 0 added.
737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units298 translation units recompiled, 2911 s compile time in total, 298 of them have a recent master baseline. |
The estimate resolved a column's `CODEC` against the whole column type, but a codec is never applied to a column as a whole: the writer resolves it per substream against the substream's type, and for structural substreams (`Array` offsets, null map, ...) it drops the type-specific codecs and keeps only the generic ones. Asking the factory for a type-specific codec with a composite type throws, so a legal table such as `Tuple(UInt32, Array(UInt64)) CODEC(T64)` or `Nullable(Int64) CODEC(GCD)` failed to read as soon as the statistics updater was attached, with the exception escaping into `MergeTreeSelect`: Code: 431. T64 codec is not supported for specified type Tuple(UInt32, Array(UInt64)) Code: 36. Codec GCD cannot be applied to column Nullable(Int64) Follow the same rule as the writer: use the type-specific codec chain only when the column is a single stream carrying the column type itself, i.e. a plain numeric or date column, where `Delta`/`T64` do drive the ratio; otherwise keep only the generic codecs, which dominate the ratio anyway and can be built for any type. Also substitute the server default when a part carries no codec of its own, so a borrowed part cannot pass a null codec to `CompressedWriteBuffer`. Reproduced by tests `01504_compression_multiple_streams` and `02872_gcd_codec` under randomized parallel-replicas settings: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114535&sha=e760449095e7e7062620682d2112d9e35b727bf9&name_0=PR #114535 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Mo4KqdD9kXtX2Rz8DGDJtF
`test_cluster_one_shard_three_replicas_secret` was swept into `dd2a92f39ecbb` from a different investigation. No test on this branch, or anywhere else in the repository, references it. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01USSfxYr3RqkPzJ4GVphmtS
`ColumnCodecByName` is built once per read task and lives as long as the task, so it is one allocation per task per column with a `CODEC`, on the query's memory. Use `UnorderedMapWithMemoryTracking`, which accounts through the throwing methods of the `MemoryTracker` instead of the non-throwing ones that plain `std::unordered_map` goes through. Both the map and everything that touches it use the same alias, so nothing is copied by the change. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01USSfxYr3RqkPzJ4GVphmtS
|
📊 Cloud Performance Report ✅ AI verdict: Release-transition baseline: compared against the last 26.8 master because no 26.9 master baseline exists at or before the trusted base-commit anchor. This PR changes the runtime dataflow-statistics input-bytes estimation to compress column samples with each column's actual codec instead of the built-in default, which alters the per-read serialization work done during MergeTree reads. ClickBench Q34 shows a +5.1% shift that is consistent across both tests but lands inside master's normal variance band, so the math suppressed it to not_sure; because Q34 is a read that directly exercises the changed sample-compression path, we surface it rather than hide it as pure noise. Estimating with a heavier real codec is a plausible cause of the small added cost, but the delta is borderline and not a confirmed regression. No other queries were flagged. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. 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
|
| NullWriteBuffer null_buf; | ||
| CompressedWriteBuffer compressed_buf(null_buf); | ||
| CompressedWriteBuffer compressed_buf(null_buf, codec); |
There was a problem hiding this comment.
| NullWriteBuffer null_buf; | |
| CompressedWriteBuffer compressed_buf(null_buf); | |
| CompressedWriteBuffer compressed_buf(null_buf, codec); | |
| CompressedSizeCalculator calc(codec); |
We can use CompressedSizeCalculator here. Not critical, just to raise awareness it exists :)
…es not match
The input-bytes estimate for a compact part serializes a sample of every column it
reads and compresses it with the column's `CODEC`, resolved once per read task from
the table metadata. The metadata does not describe that sample on its own, and the
estimate used the type-specific codec regardless:
- the serialization is chosen per block from the column at hand, so a column stored
`Sparse` (or `Replicated`) arrives as several substreams funnelled into one buffer;
- a column read from a part keeps its pre-`ALTER` type until the mutation that
rewrites it has run, while the codec was resolved for the new type.
A type-specific codec then measures a stream it was never resolved for. Most only
mismeasure it, but `ALP` validates its input and fails the query outright:
Cannot compress with ALP(RD) codec, data size 19209 is not aligned to 4.
(CANNOT_COMPRESS)
Resolve both the type-specific and the generic-only codec up front and pick between
them from the serialization and the type the sample actually has, keeping only the
generic codecs for every other shape - exactly what the writer does for structural
substreams.
Report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=3324e948a5143641935d0389afbe55193545c4f2&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%29
Caused by: ClickHouse#114535
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Take a column's CODEC into account when estimating how many bytes a query reads from compact parts. The estimate compressed a sample with the default codec (
LZ4) no matter how the data is really stored, so a column stored with a stronger codec was overestimated, which inflateseffective_max_reading_threadsand can make automatic parallel replicas distribute a query that is cheaper to run on a single node.Workflow [PR]
Sync PR [sync-upstream/pr/114535]
Version info
26.9.1.742(included in26.9and later)