Skip to content

Columns cache for compact parts - #127287

Merged
alexey-milovidov merged 2 commits into
masterfrom
columns-cache-compact-parts
Oct 25, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
columns-cache-compact-parts

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

The columns cache was implemented only in MergeTreeReaderWide, so data in compact parts was never served from it or written to it. Compact parts are the format of every small part - fresh inserts, patch parts of lightweight updates, small tables - so whether a query was accelerated depended on the part format, which is invisible to the user.

Every granule of every column of a compact part is serialized on its own, with its own prefix, and is read after a seek to its mark. So a granule of a column can be served from the cache or read from the part independently of the other granules and columns, and there are no stream positions to restore, unlike in wide parts. MergeTreeReaderCompact now does this:

  • a column whose entry covers the granule is served from it, the others are read from the part;
  • a column served from the cache for the whole block is served stripe by stripe, and a block that is a whole entry is shared with the cache without a copy;
  • the granules read from the part are accumulated per column into runs of granules of one stripe and written; a block that is a whole stripe (or the beginning of one where the range ends) becomes the entry without a copy;
  • getColumnsCacheStripeMarks and canReadWholeColumnsCacheStripe are implemented, so the read tasks and blocks are aligned to the stripes as for wide parts.

The format-independent part of the cache logic (column identities, admission, the write budget, deferred writes, serving rows from an entry) is moved from MergeTreeReaderWide to IMergeTreeReader and shared by both readers. IMergeTreeDataPart::mayStoreColumnsInColumnsCache, the write estimate and the task alignment in the read pools now include compact parts, so their entries are removed when the part goes away.

Columns that are not in the part, or read only for the offsets of a Nested sibling, or not a function of the granule (the distinct-paths subcolumn of JSON) have no entries and are read from the part, as in the wide reader.

Tested with the new test, the existing columns cache tests, and about 2800 stateless tests on Dynamic, JSON, Variant, Nested, sparse columns, subcolumns, PREWHERE, lazy materialization and patch parts run with use_columns_cache = 1 forced on (with randomized MergeTree settings, which produce compact parts): no differences from the runs with the cache off.

Changelog category (leave one):

  • Experimental Feature

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

The columns cache (use_columns_cache) now applies to compact parts of MergeTree tables, not only to wide parts.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/127287]

Version info

  • Merged into: 26.11.1.739-master (included in 26.11 and later)

The columns cache was implemented only in `MergeTreeReaderWide`, so data in
compact parts was never served from it or written to it. Compact parts are
the format of every small part (fresh inserts, patch parts of lightweight
updates, small tables), so whether a query was accelerated depended on the
part format.

Every granule of every column of a compact part is serialized on its own,
with its own prefix, and is read after a seek to its mark. So a granule of a
column can be served from the cache or read from the part independently of
the other granules and columns, without any stream positions to restore.
`MergeTreeReaderCompact` now does this: a column whose entry covers the
granule is served from it, the others are read; a block that is a whole
entry is shared with the cache without a copy, and the granules read from
the part are accumulated per column into runs of a stripe and written.

The part of the cache logic that does not depend on the part format (the
identities of the columns, the admission, the write budget, the deferred
writes, serving rows from an entry) is moved from `MergeTreeReaderWide` to
`IMergeTreeReader`, and both readers use it. The entries of compact parts are
removed when the part goes away, as for wide parts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [973a0a3]

Summary: ✅


AI Review

Summary

PR #127287, reviewed at 973a0a37eb4a2acb30b7803df4f321071354c601, extends the experimental columns cache to compact MergeTree parts and moves shared cache logic into IMergeTreeReader. No actionable findings. The new stateless test passes in CI, and six local checks passed using the PR's CI binary, covering the new test, mixed cache/disk subcolumn reads, projections, read/write controls, missing Nested members, and legacy compact marks. Local runs omitted assertions requiring system.query_log.

Final Verdict

✅ Approve — no required changes.

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Oct 21, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-filesystem-cache Filesystem cache for remote/object storage, page cache, and read caching infrastructure. label Oct 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 973a0a37e with master 11d1ea9b2 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

22 object files changed (+47.33 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeReaderCompact.cpp.o 293.41 KiB 330.11 KiB +36.70 KiB (+12.51%)
src/CMakeFiles/dbms.dir/Storages/MergeTree/IMergeTreeReader.cpp.o 361.61 KiB 395.92 KiB +34.30 KiB (+9.49%)
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeReaderWide.cpp.o 441.02 KiB 412.45 KiB -28.57 KiB (-6.48%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

150 translation units recompiled, 1764 s compile time in total, 150 of them have a recent master baseline.

Job report

@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. and removed comp-filesystem-cache Filesystem cache for remote/object storage, page cache, and read caching infrastructure. labels Oct 22, 2026

@alexey-milovidov alexey-milovidov 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.

Good. The ideas are clear, the code meets the quality standard, and it is easy to follow.

@alexey-milovidov alexey-milovidov self-assigned this Oct 25, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 25, 2026
Merged via the queue into master with commit 1bd452b Oct 25, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the columns-cache-compact-parts branch October 25, 2026 11:22
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 25, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 25, 2026
Since the columns cache serves Compact parts (ClickHouse#127287, 2c9139d), the
Compact reader returns a cached granule without reading the JSON structure
prefix, the same way the Wide reader already did. Once the calibration scan
has filled the cache, the cancelled scans can no longer land inside the
prefix read, so "cancelled inside the reader at least once" reads 0.

Measured on the merged head (debug, Build ID 052cbf4d):
- use_columns_cache=1 with reads and writes on: 5/5 FAIL with that diff;
  use_columns_cache=0 with reads and writes on: 3/3 OK.
- with the clamp, randomized, reads and writes forced on: 10/10 OK, and
  query_log shows use_columns_cache never set in any run.
- negative control (clamp line made unparseable): use_columns_cache=1 in
  5/10 runs and exactly those 5 FAIL.
- compact + wide, randomized, 25 runs each, 4 jobs: 50/50 OK.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov added a commit that referenced this pull request Oct 26, 2026
…the columns cache

After merging master, the readers of compact parts write to the columns cache
too (#127287), so the condition
`isWidePart` let the reader of a compact part apply the string filter and
then cache the substituted empty strings: an unfiltered query served from the
cache saw them. Drop the condition and check the compact part in the test the
same way as the wide part.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
leshikus pushed a commit to leshikus/ClickHouse that referenced this pull request Oct 27, 2026
Since ClickHouse#127287 the columns cache serves compact parts, so with a
randomized `use_columns_cache = 1` the warm-up query caches the columns
and the measured queries open no data files. That turns the FileOpen
oracle blind: the in-range control fails (FileOpen 30, not > PARTS), and
the selective and postings-cached arms pass vacuously (0 files opened).

Pin `use_columns_cache = 0` in the settings every measured query and
warm-up uses, next to the query cache pins. With the setting at 0 the
cache is neither read nor written, so the `enable_reads_from_columns_cache`
and `enable_writes_to_columns_cache` draws need no pin.

Failing job: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127715&sha=9500cdadc54b2d1ef5c3825d11b4d749668a5fd2&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20sequential%29

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov added a commit that referenced this pull request Oct 28, 2026
…ed disk tests

Since #127287 the columns cache also covers compact
parts, so with `use_columns_cache` on by default the repeated reads in these tests were served from
it and never reached the filesystem cache, the page cache, or the (broken) underlying storage.
Also drop the duplicate `use_columns_cache = 0` in `05218_text_index_substreams_opened_lazily`,
which master already sets.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=123890&sha=5501a1e6376dc3fce82c0fbb6fbbd9c6cfc6d3fa&name_0=PR
PR: #123890

Co-Authored-By: Claude Opus 5.5 <[email protected]>
valerypetrov pushed a commit to valerypetrov/ClickHouse that referenced this pull request Oct 28, 2026
Since ClickHouse#127287 the columns cache also serves compact parts. The runner
randomizes `use_columns_cache` + `enable_reads_from_columns_cache` +
`enable_writes_to_columns_cache`. When all three are on and the parts are
compact, the read after ATTACH fills the cache (DETACH destroys the parts
and removes their entries), and `SYSTEM CLEAR MARK CACHE` does not drop it.
The next `SELECT ... WHERE NOT ignore(*)`, the test's positive control
("a read after clearing the mark cache loads marks"), is then served
entirely from the columns cache and requests no marks. Only the
speculative async marks load scheduled by the compact reader can still
count `LoadedMarksCount`, and it is cancelled when the reader finishes
first, so the reference `1` becomes `0` in a fraction of runs. Under the
same settings the expected-0 "prewarm worked" rows are vacuous, since a
cache-served read loads no marks either way.

Disable the columns cache for the whole test, as 2376801 did for
the sibling 03254_prewarm_mark_cache_columns. A session SET beats the
runner's injected value and also holds where randomization is off.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PedroTadim pushed a commit that referenced this pull request Nov 5, 2026
On 26.10 only MergeTreeReaderWide uses the columns cache; the Compact reader got it in #127287, which is master-only,
so the Compact case's system.columns_cache probe returns no rows there. Test only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 8, 2026
…lation-compact-reader-mark-loop

Conflicts resolved:

- MergeTreeReaderCompactSingleBuffer.cpp: master (ClickHouse#127287) moved the mark
  loop out of readRows into MergeTreeReaderCompact::readGranules, and ClickHouse#128083
  already guards the reportBroken catch with isQueryCancellation. Took master's
  file as is; the per-granule checkTimeLimit moves to both loops of
  MergeTreeReaderCompact (readGranules and readGranulesWithColumnsCache).
- checkDataPart.{cpp,h}: ClickHouse#128083 added isQueryCancellation under the same name.
  Kept master's function and declaration; dropped ours. Master accepts only the
  three cancellation codes on a DB::Exception, so isCodeOfCancelledQuery stays as
  an extra clause in both catch branches: a killed query can rethrow the error it
  was cancelled with, of any code and any exception type.
- MergeTreeSequentialSource.cpp: master's catch line is identical to ours.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 8, 2026
Since ClickHouse#127287 the compact reader reads the granules of a block in one of two
loops: the plain one, or the columns cache one when use_columns_cache is on.
The cancellation check is in both, so the timeout arm now also runs with
use_columns_cache = 1 and cache writes off, which keeps every granule a read
from the part. ColumnsCacheMisses, counted only by that loop, proves the arm
went through it.

Disabling either loop's check fails exactly the arms of that loop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
leshikus pushed a commit that referenced this pull request Nov 13, 2026
On 26.10 only MergeTreeReaderWide uses the columns cache; the Compact reader got it in #127287, which is master-only,
so the Compact case's system.columns_cache probe returns no rows there. Test only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-experimental Experimental Feature 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.

2 participants