Skip to content

Merge tables report rows and sizes only of the tables the user can see - #129818

Merged
PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:merge-hidden-tables-metadata
Nov 10, 2026
Merged

PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:merge-hidden-tables-metadata

Conversation

@groeneai

@groeneai groeneai commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #129597
Related: #129730

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

A Merge table and the merge table function no longer count the matched tables the user cannot see (no SHOW TABLES) when count() is answered from metadata or in system.tables total_rows/total_bytes, and their system.columns sizes include only the columns the user can see (SHOW COLUMNS) on each matched table. Before, these disclosed the row count and size of tables the user has no access to, while reading the same data skipped them.

Description

On 26.8.2.3 and 26.10.2.103, a user with SELECT on a Merge table m over t1 and t2 and on t1 only gets SELECT count() FROM m SETTINGS apply_patch_parts = 0, apply_mutations_on_fly = 0 = 6 instead of 3, and system.tables shows total_rows/total_bytes of m as 6/1502 instead of 3/751: the size of a table they cannot even list.

Root cause: the read (ReadFromMerge::getSelectedTables) skips matched tables without SHOW TABLES (#78339) and checks SELECT on the rest, but the metadata-only answers of StorageMerge (totalRows/totalBytes, supportsTrivialCountOptimization, tryGetColumnSizes) went through every matched table with no check.

Change: these now enumerate with the query context and skip tables without SHOW TABLES, asking the database-wide grant once per database like the read does. The trivial count is declined for a visible table the user cannot read, so the read raises its ACCESS_DENIED. IStorage::tryGetColumnSizes takes the query context (its only caller is system.columns), so a nested Merge, an Alias over a Merge and CREATE TABLE ... AS merge(...) filter too. A child's column size is added only with SHOW COLUMNS on that column, the check system.columns makes for the child itself; an Alias keeps only the columns granted through its chain.

Unchanged: the plan-shape walks (prewhere, subcolumns, processing stage) still look at hidden tables; Buffer over a hidden destination is #129801. Cost: within noise for a fully granted user; a partially granted user pays one check per matched table, like the read (~+11% CPU for a trivial count over 1000 tables, debug build).

Validation: the new test 05616_merge_storage_hidden_tables_metadata fails on master in every leak case and passes 50/50 with randomized settings.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.1848-master (included in 26.11 and later)
  • Backported to: 26.10.5.45, 26.9.31.21, 26.8.39.19, 26.3.66.12

groeneai and others added 2 commits November 6, 2026 15:31
A `Merge` table or `merge()` reads only the matched tables the user has
`SHOW TABLES` on (ClickHouse#78339), but its metadata-only answers walked every
matched table: the trivial count (`SELECT count()` with
`apply_patch_parts = 0, apply_mutations_on_fly = 0`), `system.tables`
`total_rows`/`total_bytes` and `system.columns` sizes included the rows
and on-disk size of a table the user holds no privilege on, and the
trivial count answered for a visible child the user cannot read.

`totalRows`/`totalBytes`, `supportsTrivialCountOptimization` and
`tryGetColumnSizes` now enumerate the children with the query context
and skip those without `SHOW TABLES`, like `getSelectedTables` does. The
trivial count is also declined for a visible child with no `SELECT` on
any column, so the read path raises its `ACCESS_DENIED`.
`tryGetColumnSizes` takes the query context (its only caller is
`system.columns`), and the Merge sums each visible child's own
context-aware sizes, so a nested `Merge` or an `Alias` over a `Merge`
filters its own children too. `Alias`, `StorageProxy` and
`StorageTableFunctionProxy` forward the context.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The metadata walks of a `Merge` table now ask for `SHOW TABLES` (and,
for the trivial count, `SELECT`) on the whole database once per
database, like `getSelectedTables` does, and check a single table only
when the database-wide grant is missing. A fully granted user pays no
per-table access check, and the query's `used_privileges` no longer
lists every matched table.

An `Alias` reports column sizes only when the user can see its target,
as its `totalRows`/`totalBytes` already do, so an `Alias` child of a
`Merge` no longer reports the sizes of a hidden target in
`system.columns`.

Drop the `StorageProxy::tryGetColumnSizes` forwarder: no `StorageProxy`
wraps a `Merge` or an `Alias`.

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

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (2 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ The m_col (column-only grant) and direct merge() count arms could pass if the trivial count silently fell back to a normal read DISAGREE For a child with only a column-level SELECT, the normal read of count() over a Merge raises ACCESS_DENIED (measured: "necessary to have the grant SELECT ON db1.t2"), so a fallback fails the m_col arm. merge() builds the same StorageMerge and goes through the same supportsTrivialCountOptimization, which the EXPLAIN arm on m already covers; the merge() arm itself fails on master (1003 vs 3).
2 💡 A dangling Alias child (target dropped) now makes the Merge's system.columns sizes 0 instead of the sum of the other children AGREE, not changed Same partial-answer rule as total_rows, which is already NULL for such a Merge on master.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-self-slot-58:20261106-192000

@groeneai

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. As a user with SELECT on m and t1 only: trivial count() of m / merge() 6 (expected 3), system.tables 6/1502 (3/751), system.columns sizes of m include t2; a Merge over t_visible + Alias(t_hidden) reports t_hidden's sizes. Every run.
b Root cause explained? StorageMerge's metadata-only walks (totalRows/totalBytes, supportsTrivialCountOptimization, tryGetColumnSizes) went through every matched table, while the read skips tables without SHOW TABLES and checks SELECT on the rest. StorageAlias::tryGetColumnSizes forwarded its target's sizes with no target check.
c Fix matches root cause? Yes. One helper applies the read's visibility rule (with its per-database shortcut) to every metadata walk; the Alias guard mirrors its totalRows. No filtering at the consumers, and hidden tables are skipped, not turned into NULL (that would itself reveal them).
d Test intent preserved / new tests added? New test 03400_merge_storage_hidden_tables_metadata (13 arms): trivial count, system.tables, system.columns through Merge, merge(), CREATE AS merge(), Alias over Merge, nested Merge, an Alias child; SHOW-only, column-level and database-wide-with-REVOKE grants; trivial count still used, admin values unchanged.
e Both directions demonstrated? Master build fails every leak arm; the fix passes, 50/50 with randomized settings. Two mutations (database-wide shortcut forced on; children asked for context-less sizes) each fail the test.
f Fix is general across code paths? Covers every consumer of totalRows/totalBytes (trivial count, system.tables, statistics, DROP TABLE ... IF EMPTY), system.columns, and every wrapper that forwards to a Merge. Buffer over a hidden destination is the same class in another engine: #129801.
g Fix generalizes across inputs (params/datatypes/wrappers)? No type-dependent path. Grant matrix: none, SHOW only, column-level SELECT, table SELECT, database-wide, database-wide with a table REVOKE, full; matched tables in several databases.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; only users without SHOW TABLES on some matched table see smaller totals, plus an ACCESS_DENIED where reading the same data already throws it.
i Invariants and contracts preserved? Every value a Merge reports without reading now covers exactly the tables the read would read for this user; nullopt propagation and the existing error handling are kept.

Session id: cron:clickhouse-impl-self-slot-57:20261106-175000

@groeneai

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author

cc @PedroTadim @vitlibar, could you review this and add the can be tested label so CI runs? The metadata-only answers of a Merge table (trivial count(), system.tables total_rows/total_bytes, system.columns sizes) went through every matched table and so disclosed the rows and size of tables the user cannot see; they now skip tables without SHOW TABLES, the visibility rule the read already follows (#129597).

@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ba1de54]

Summary: ✅


AI Review

Summary

This PR makes metadata-only answers from Merge tables respect source-table visibility and column privileges, propagating the query context through nested Merge tables, Alias tables, and table-function proxies. No actionable findings remain in the current diff. The regression test 05616_merge_storage_hidden_tables_metadata passes in Fast test for the reviewed head. Validation used source inspection and existing CI results; no local build or tests were run.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-storage-merge Merge table engine (logical union over tables) and related mechanics. labels Nov 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ba1de54f8 with master 4ce893869 (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.

Compile time of recompiled translation units

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

Job report

@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 3b2b763

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 05515_global_subquery_projection_where not from this PR (the test is not in its diff): Code: 47 UNKNOWN_IDENTIFIER on a _data_<hash> name at the INSERT, deterministic on master in this lane since the test was added #129836 (mine, open)
Mergeable Check, PR praktika rollups of the row above #129836 (the owner of the row above)

No job was dropped, and CH Inc sync passed. The only other CIDB FAIL rows on this head are Config Workflow / Pre Hooks and Finish Workflow / Post Hooks of the run before the can be tested label; the labelled run passed both.

Session id: cron:our-pr-ci-monitor:20261107-023005

@PedroTadim PedroTadim self-assigned this Nov 9, 2026
@PedroTadim

Copy link
Copy Markdown
Member

🕵️ @groeneai

The fix looks right to me. Every metadata-only answer of StorageMerge (totalRows/totalBytes, supportsTrivialCountOptimization, tryGetColumnSizes) now enumerates with the query context and skips tables without SHOW TABLES, the same rule the read applies. A nested Merge, an Alias and a merge() proxy pass the query context down, so the filter applies at every level. A visible table without SELECT on any column declines the trivial count, so the read raises ACCESS_DENIED instead of counting it.

One question about system.columns. For a table read directly, its per-column sizes need SHOW COLUMNS on it (need_to_check_access_for_columns). Through m they need only SHOW TABLES on the child, so a user with SHOW TABLES ON db.t2 and no SHOW COLUMNS on it gets t2's per-column sizes added into m's. Is that intended, or should tryGetColumnSizes also skip a child without SHOW COLUMNS?

Nit: 03400 is an old prefix that many tests already use; tests/queries/0_stateless/add-test assigns the next free number.

… see

`system.columns` shows a table's per-column sizes only for the columns the
user has `SHOW COLUMNS` on. Through a Merge table they needed only
`SHOW TABLES` on the child, so a user with `SHOW TABLES ON db.t2` (or a
column-level grant such as `SELECT(id)`) got the other columns' sizes of `t2`
added into the Merge table's sizes. `StorageMerge::tryGetColumnSizes` now adds
a child's column size only when the user has `SHOW COLUMNS` on that column of
the child, the check `system.columns` applies to the child itself.

An Alias child forwards its target's sizes, so `StorageAlias::tryGetColumnSizes`
now keeps only the columns granted `SHOW COLUMNS` through the alias chain, as
`system.columns` does for an Alias table read directly. This replaces its
`SHOW TABLES` guard on the target (which `SHOW COLUMNS` implies), so a Merge
over an Alias of a hidden table now reports the sizes of its other children
instead of none.

`total_rows`/`total_bytes` keep the `SHOW TABLES` rule: `system.tables` shows
them for such a table directly.

Rename the test to the next free number (`add-test`): 03400 -> 05616.

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

groeneai commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

Not intended: a size seen through m should need what the child's own size needs. Fixed in ba1de54:

  • StorageMerge::tryGetColumnSizes adds a child's column size only when the user has SHOW COLUMNS on that column of the child, the same check system.columns makes for the child itself. It is per column rather than per child, so SELECT(id) ON db.t2 adds t2.id and nothing else.
  • An Alias child forwards its target's sizes, so StorageAlias::tryGetColumnSizes now keeps only the columns granted SHOW COLUMNS through its chain, as system.columns does for an Alias read directly. This replaces its SHOW TABLES guard (SHOW COLUMNS implies it), so a Merge over an Alias of a hidden table now reports its other children's sizes instead of none.
  • total_rows/total_bytes keep the SHOW TABLES rule, since system.tables shows them for such a table directly.

Compressed bytes of id/s as a user with SELECT on t_visible, SHOW TABLES on t_show and t_target, SELECT(id) on t_col (before -> after):

Merge over before after
t_visible, t_show 4072 / 567 38 / 68 (= t_visible)
t_visible, t_col 4072 / 567 4072 / 68 (t_col.id is visible)
t_visible, Alias(t_target) 4072 / 567 38 / 68

The test is now 05616_merge_storage_hidden_tables_metadata (the next free number on master). It checks that each Merge table's sizes equal the sums the user sees on its children, fails on master and on the previous head, and passes 50/50 with randomized settings. I updated the PR description to match.

@PedroTadim
PedroTadim added this pull request to the merge queue Nov 10, 2026
Merged via the queue into ClickHouse:master with commit e1e3033 Nov 10, 2026
187 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 10, 2026
@SmitaRKulkarni SmitaRKulkarni added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 16, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 16, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 16, 2026
Backport #129818 to 26.10: Merge tables report rows and sizes only of the tables the user can see
PedroTadim added a commit that referenced this pull request Nov 17, 2026
Cherry pick #129818 to 26.9: Merge tables report rows and sizes only of the tables the user can see
PedroTadim added a commit that referenced this pull request Nov 17, 2026
Cherry pick #129818 to 26.8: Merge tables report rows and sizes only of the tables the user can see
PedroTadim added a commit that referenced this pull request Nov 17, 2026
Cherry pick #129818 to 26.3: Merge tables report rows and sizes only of the tables the user can see
robot-clickhouse added a commit that referenced this pull request Nov 17, 2026
robot-clickhouse added a commit that referenced this pull request Nov 17, 2026
robot-clickhouse added a commit that referenced this pull request Nov 17, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 17, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 17, 2026
Backport #129818 to 26.8: Merge tables report rows and sizes only of the tables the user can see
clickhouse-gh Bot added a commit that referenced this pull request Nov 17, 2026
Backport #129818 to 26.9: Merge tables report rows and sizes only of the tables the user can see
PedroTadim added a commit that referenced this pull request Nov 17, 2026
Backport #129818 to 26.3: Merge tables report rows and sizes only of the tables the user can see
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-storage-merge Merge table engine (logical union over tables) and related mechanics. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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