Skip to content

Add mergeTreeCodecBlockCounts table function - #109623

Merged
rienath merged 9 commits into
masterfrom
block-walk
Jul 23, 2026
Merged

rienath merged 9 commits into
masterfrom
block-walk

Conversation

@rienath

@rienath rienath commented Jul 7, 2026 •

Copy link
Copy Markdown
Member

It returns one row per (active part, column, substream) with a map counting the substream's compressed blocks by codec, plus the substream's compressed and uncompressed sizes.

system.columns reports a single codec taken from metadata, while the codec that actually compressed each block is recorded only in the block's own header. Seeing the difference is useful already, e.g. ALTER TABLE ... MODIFY COLUMN ... CODEC changes only metadata, and existing parts keep the old codec until something rewrites them. It's a must with adaptive codec selection (RFC: #105404), which picks codecs per block. This PR implements the RFC's observability part.

Changelog category (leave one):

  • New Feature

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

Added the mergeTreeCodecBlockCounts(database, table) table function that reports, per (part, column, substream) of a MergeTree table, how many compressed blocks use each codec.

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5d4f278]

Summary: ✅


AI Review

Summary

This PR adds the mergeTreeCodecBlockCounts(database, table) table function and covers the main happy paths well: per-substream codec counting, Compact parts, patch parts, key-column filter pushdown, and row-policy denial for codec_block_counts. I found one new RBAC regression in the read path, and one previously raised omission that still appears real on upgraded tables with old Wide parts.

Findings

⚠️ Majors

  • [src/Storages/StorageMergeTreeCodecBlockCounts.cpp:328] The new table function drops column-level SELECT semantics and requires access to every physical source column before it can read anything. That is stricter than comparable MergeTree introspection surfaces such as mergeTreeIndex, which already supports column-scoped grants (02947_merge_tree_index_table_3.sh). A role with GRANT SELECT(b) cannot use mergeTreeCodecBlockCounts(... ) WHERE column = 'b', so the feature becomes unusable in least-privilege setups unless users also receive table-wide SELECT.
    Suggested fix: derive the required source columns from the proven column predicate before checkAccess, and add a focused RBAC regression test.

  • [dismissed by author -- https://github.com/Add mergeTreeCodecBlockCounts table function #109623#discussion_r3536106253] [src/Storages/StorageMergeTreeCodecBlockCounts.cpp:100-103] Parts with empty columns_substreams are still silently skipped, and that is not limited to legacy untouched data. MutationHelpers::getColumnsForNewDataPart only preserves columns_substreams when the source part already had them, so a lightweight mutation or codec rewrite of an old Wide part can still produce a brand-new active part that this function omits completely. That breaks the PR’s core observability use case on upgraded tables exactly when users try to inspect rewritten bytes.
    Suggested fix: fall back to reconstructible stream enumeration for ordinary columns, and fail explicitly only for cases such as dynamic substreams that cannot be recovered safely.

Final Verdict

⚠️ Needs fixes before approval. The feature shape is good, but the current RBAC behavior is stricter than existing MergeTree introspection tooling, and the omitted-old-part path still leaves real active data invisible on upgraded tables.

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jul 7, 2026
@mintlify

mintlify Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🟢 Ready View Preview Jul 7, 2026, 12:01 PM

@Michicosun Michicosun left a comment

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.

Why isn't it implemented as a part of system.parts_columns?

@rienath

rienath commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

@Michicosun multiple reasons. First of all, codec_block_counts walks .bin files and SELECT * would be terribly slow. Also, this functionality is very specific. As such, I didn't want to add a column that adds information that is useful only in marginal scenarios and can slow things down.

Second reason is row policies. When user tries to access codec_block_counts on a table they do not have full access to, we'd throw. System tables don't give us such mechanism. We can display nothing, but then it is vague: did we not display codecs because of compact parts or row policies got applied?

@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 for the comment. I committed WHERE pushdown on key columns and row policy protection as discussed. On top, mergeTreeCodecBlockCounts now displays codecs of patch parts.

@rienath
rienath requested a review from CurtizJ July 17, 2026 13:41
@rienath

rienath commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

@groeneai merge master into this PR, failing test was fixed

@groeneai

Copy link
Copy Markdown
Collaborator

I cannot push to block-walk: it lives in ClickHouse/ClickHouse rather than my fork, and as an external contributor my push returns 403 (Permission denied). So I am unable to merge master in for you.

You can do it yourself with the "Update branch" button on this PR, or on the branch: git merge origin/master && git push. I ran the merge locally to check and it is clean: no conflicts and no submodule drift.

@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 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: 191/196 (97.45%) · Uncovered code

Full report · Diff report

@rienath
rienath added this pull request to the merge queue Jul 23, 2026
Merged via the queue into master with commit 89e6f70 Jul 23, 2026
180 checks passed
@rienath
rienath deleted the block-walk branch July 23, 2026 15:20
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 23, 2026
guilian-newiger pushed a commit to guilian-newiger/ClickHouse that referenced this pull request Jul 30, 2026
alexey-milovidov added a commit that referenced this pull request Aug 29, 2026
…curately

The worked example said "Neither path had a test". That is wrong: #109623 shipped
ten `.sql` files for the function, and both paths the paragraph names are
exercised. `04267_mergeTreeCodecBlockCounts_basic.sql` hits the non-`MergeTree`
`BAD_ARGUMENTS` on the structure path, and
`04509_mergeTreeCodecBlockCounts_row_policy.sql` asserts `ACCESS_DENIED` at read
time.

The narrowing suggested in review — that only a regression test for the
unprivileged `DESCRIBE` was missing — is not the whole gap either. No
`mergeTreeCodecBlockCounts` test creates a user or grants anything, and none
issues a `DESCRIBE`, so every one of them runs as the privileged current user.
The `checkAccess(AccessType::SELECT, …)` in `read` therefore had no test on any
path: `04509` exercises the row-policy branch that sits beside it in `read`,
which is a different guard, reached from `getRowPolicyFilter` rather than from the
grant check.

So the accurate statement is stronger than either version and is the rule's own
point: the suite covered the code paths but never once ran without the grant, so
nothing in it distinguished a check on one entrypoint from a check on all of them.
The gap was "no negative case", not "no tests" — which is why the rule asks for
one negative case per guarded entrypoint rather than for path coverage.
pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Aug 29, 2026
`mergeTreeCodecBlockCounts` (ClickHouse#109623)
checked `SELECT` on its source table in `StorageMergeTreeCodecBlockCounts::read`
only. The review looked straight at that check and argued its column granularity
was too coarse; the author answered that the strict check was intentional, and the
review ended there. Meanwhile `getActualTableStructure` went straight to
`DatabaseCatalog`, so `DESCRIBE mergeTreeCodecBlockCounts(db, t)` succeeded for a
user with no privilege on `db.t`, and the `BAD_ARGUMENTS` thrown for a table of
another engine disclosed that engine. Fixed by
ClickHouse#116647.

The miss was not a failure to read the right file. It was answering the shape
question about a guard instead of the prior question of whether every path reaches
it, and having no inventory of what the entrypoints of a table function are.

- New triggered expansion for an access check or a new surface derived from
  another object, pointing at `references.md#access-checks`.
- New `references.md` section with the procedure: completeness before granularity,
  the entrypoint inventory (`getActualTableStructure`, reached by `DESCRIBE` and
  `CREATE TABLE ... AS`, `executeImpl`, `read`, `totalRows`/`totalBytes`,
  inherited mutating operations), the note that
  `getActualTableStructureWithAccess` covers only source access and never `SELECT`
  on a table named in the arguments, why metadata and error text are protected
  information, one negative test per entrypoint, the sibling sweep, and severity.
- New general rule "Do not let one finding close a topic": a finding about a
  mechanism is not a review of it, and an author's "this is intentional" closes the
  shape question only.
- Blocker for a check any entrypoint can bypass, including one that exposes only
  structure, metadata, or an error message naming a property of the object.
- Test-coverage rule: a new access check needs a negative test per guarded
  entrypoint; no test at all is a Major, since nothing then distinguishes a check
  on every path from a check on one.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Add `mergeTreeCodecBlockCounts` table function

This branch was successfully deployed

1 active deployment
staging - docs — 5d4f2781 Deployed Jul 23, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product 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.

5 participants