Repository navigation
Read the column that is smallest on disk when a MergeTree read needs no column - #131371
Conversation
…no column With `query_plan_remove_unused_columns = 1` (the default since 25.12), a `MergeTree` read that needs no column of the table (`SELECT count()` that has to scan, e.g. after a lightweight `DELETE`; `sum(1)`; `count()` over a subquery; `count() ... FINAL`; a constant-folded `isNullable(c)`) read the column with the smallest data type instead of the one smallest on disk. The planner chooses the row-count column by its size on disk (`chooseSmallestColumnToReadFromStorage`), but the plan-level column pruning removes it from `ReadFromMergeTree` because nothing above uses it, and the static `ReadFromMergeTree::selectRangesToRead` then re-adds a column with `ExpressionActions::getSmallestColumn`, which ranks by `getMaximumSizeOfValueInMemory` only: a `Nullable(UInt8)` of 118 MB beat a sparse `Int32` of 1.5 KB on the table from ClickHouse#131235 (126 MB read instead of 91 KB). The same fallback causes ClickHouse#106597 (`isNullable(lc)` in the perf test `low_cardinality_argument` reads `x`, 40 MB, instead of `lc`, 1.8 MB). The <compressed size, type size, uncompressed size> ranking, written twice (planner `ColumnWithSize`, `TreeRewriter` `ColumnSizeTuple`), becomes one function, `ExpressionActions::getCheapestColumnToRead`, used by both of them unchanged (same candidates, order, ties and fallback) and by `selectRangesToRead` right before its normal return, from the table's cached column sizes, when no column is needed and at least one non-projection part is read. The early returns keep the type-ranked carrier and make no size lookup (none of them reads a part through it), and projection parts keep it too, because the table's column sizes do not describe them. Closes ClickHouse#131235 Closes ClickHouse#106597 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the getCheapestColumnToRead header comment from three lines to two. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings), plus my own cold review of the same head.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261113-161100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261113-145200 |
|
Workflow [PR], commit [524e0ac] Summary: ✅
AI ReviewSummaryReviewed PR #131371 at Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units368 translation units recompiled, 3611 s compile time in total, 368 of them have a recent master baseline. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
It still uses ExpressionActions, that we want to remove. Too bad, @KochetovNicolai
82d74f6
|
@groeneai @alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
Resolve the conflict in src/Planner/PlannerJoinTree.cpp: keep the 26.10 side and take only the lines of #131371. The `skip_subcolumns` argument of the master call refers to `filtered_by_allowed_columns`, which comes from #103402 (not in 26.10), so the call uses the default, as the replaced `getSmallestColumn` call on 26.10 did.
Cherry pick #131371 to 26.10: Read the column that is smallest on disk when a MergeTree read needs no column
…hen a MergeTree read needs no column
Backport #131371 to 26.10: Read the column that is smallest on disk when a MergeTree read needs no column
Closes: #131235
Closes: #106597
Related: #107368
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
MergeTreequery that needs no column of the table, such asSELECT count()over a table with lightweight-deleted rows,count()over a subquery orSELECT isNullable(c) FROM t, again reads the column that is smallest on disk, as it does withquery_plan_remove_unused_columns = 0. Since 25.12 it read the column with the smallest data type, which could be more than 1000 times larger. Closes #131235. Closes #106597.Description
With
query_plan_remove_unused_columns = 1(the default since 25.12), aMergeTreeread that needs no column of the table read the column with the smallest data type instead of the one smallest on disk. On the table from #131235 (100M rows, one lightweightDELETE),SELECT count()read 124.8 MB ofc4 Nullable(UInt8)instead of 84 KB ofc3 Int32.The planner picks the column to count rows by its size on disk (
chooseSmallestColumnToReadFromStorage), but the plan-level column pruning then removes it fromReadFromMergeTreeas unused, andReadFromMergeTree::selectRangesToReadadds a column back withExpressionActions::getSmallestColumn, which only compares the in-memory size of the types.The <compressed size, type size, uncompressed size> ranking that the planner and
TreeRewritereach implemented is now one function,ExpressionActions::getCheapestColumnToRead. The planner andTreeRewritercall it unchanged, andselectRangesToReaduses it on the table's column sizes once the parts to read are known. Reads of projection parts keep the type ranking, since the table's column sizes do not describe them.This supersedes my #107368, which targets the same fallback with a separate per-part ranking.
Validation: the new test fails on master in every randomized run (150 of 150) and passes with the fix (150 of 150, 100 of them in a flaky-check style run).
ReadCompressedByteson 10M rows, master vs this PR:count()after a lightweight delete 18.0 MB vs 79 KB,sum(1)18.1 MB vs 77 KB,count()over a subquery 18.1 MB vs 65 KB,count() ... FINAL15.7 MB vs 94 KB, andisNullable(lc)from perf testlow_cardinality_argument(#106597) 40.05 MB vs 1.76 MB.Workflow [PR]
Sync PR [sync-upstream/pr/131371]
Version info
26.11.1.2271-master(included in26.11and later)26.10.7.3