Skip to content

Add column allowlist/denylist settings for automatic statistics - #111273

Open
ylw510 wants to merge 36 commits into
ClickHouse:masterfrom
ylw510:auto-statistics-column-lists
Open

ylw510 wants to merge 36 commits into
ClickHouse:masterfrom
ylw510:auto-statistics-column-lists

Conversation

@ylw510

@ylw510 ylw510 commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Add exclude_materialize_statistics_on_insert and exclude_materialize_statistics_on_merge to control which columns have statistics built and stored during automatic materialization on MergeTree tables. The design mirrors the existing exclude_materialize_skip_indexes_on_{insert,merge} settings.

The new settings only skip building and persisting statistics for named columns during INSERT or merge. They do not affect explicit per-column STATISTICS(...) declarations or MATERIALIZE STATISTICS.

  • exclude_materialize_statistics_on_insert (session setting): comma-delimited column list excluded from statistics materialization on INSERT. Can be set per query via INSERT ... SETTINGS. Has no effect when materialize_statistics_on_insert = 0.
  • exclude_materialize_statistics_on_merge (MergeTree table setting): comma-delimited column list excluded from statistics materialization during background or explicit merges. Has no effect when materialize_statistics_on_merge = 0.

Excluded columns can still receive statistics via MATERIALIZE STATISTICS or through the other materialization path (e.g. a column excluded on INSERT is materialized on a subsequent merge unless it is also listed in exclude_materialize_statistics_on_merge).

Closes: #111231

Changelog category (leave one):

  • New Feature

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

You can now skip automatic statistics materialization for specific columns on MergeTree tables: use the session setting exclude_materialize_statistics_on_insert to exclude columns on INSERT, and the table setting exclude_materialize_statistics_on_merge to exclude columns during merges. Explicit MATERIALIZE STATISTICS and per-column STATISTICS(...) declarations are unchanged.


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

ylw510 and others added 2 commits July 21, 2026 18:03
Introduce `auto_statistics_columns` and `auto_statistics_exclude_columns` so
MergeTree tables can limit which columns receive implicit statistics from
`auto_statistics_types`, without affecting explicit per-column STATISTICS.

Related: ClickHouse#111231
Co-authored-by: Cursor <[email protected]>
@rschu1ze

Copy link
Copy Markdown
Member

@ylw510 Please implement this analogous to https://clickhouse.com/docs/operations/settings/merge-tree-settings#exclude_materialize_skip_indexes_on_merge and exclude_materialize_skip_indexes_on_insert (the latter doesn't exist currently but you get the point).

@rschu1ze rschu1ze added the can be tested Allows running workflows for external contributors label Jul 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0258934]

Summary: ✅


AI Review

Summary

The PR adds column exclusion settings for statistics materialization during inserts and merges on MergeTree tables. The current implementation documents that exclusions cover both implicit and explicitly declared statistics, while MATERIALIZE STATISTICS and mutations retain their existing behavior. Reviewed PR head 0258934c388a04418c3412ae237593371984494a; no actionable findings remain. Fast test passed all eight focused statistics exclusion tests, including the replacement configuration test.

Missing context / blind spots
  • ⚠️ The full CI matrix remains unverified for this head: the workflow was limited by the review-thread gate and reports that a full rerun is required after the threads were resolved. No build or tests were run locally.
Final Verdict

Status: ✅ Approve. No new inline findings or changes to existing threads are needed.

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jul 21, 2026
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
@rschu1ze

rschu1ze commented Jul 28, 2026 •

Copy link
Copy Markdown
Member

@cv4g Would you like to take this PR for review? Feel free to change it according to the discussion in #111231

ylw510 added 2 commits August 1, 2026 00:38
…cludes

Exclude columns from statistics materialization during merge/insert via
exclude_materialize_statistics_on_{merge,insert}. Explicit MATERIALIZE
STATISTICS is unaffected.
@ylw510
ylw510 marked this pull request as ready for review July 31, 2026 18:01
@ylw510

ylw510 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

cc @cv4g. Can you take a look?

@cv4g

cv4g commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

cc @cv4g. Can you take a look?

Thank you for updating the PR! Yes, I will have a look

@groeneai

groeneai commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator

Not a false positive, but the header you added was not wrong either: it was correct when you wrote it, and master moved the declaration out from under you.

Commit dedf9b905913 ("Remove abseil, cctz and the settings schema from the SQL parser, and confine re2", 2026-07-27) split the overloads that take a whole String plus Settings out of Parsers/parseIdentifierOrStringLiteral.h into Interpreters/parseIdentifiersOrStringLiteralsWithSettings.h, so that the parser no longer depends on the settings schema. That commit is not in your branch: at your head 01aa9ee0 the Parsers header still declares parseIdentifiersOrStringLiteralsToSet on line 25, which is why your branch compiles on its own. CI builds the merge with master, and in the merged tree that header no longer declares it, so Statistics.cpp:921 has no declaration in scope.

The clearest confirmation is inside your own diff: the same master commit rewrote this include in two files your PR also touches. In the merged tree, MergeTask.cpp:24 and MergeTreeDataWriter.cpp:43 both read #include <Interpreters/parseIdentifiersOrStringLiteralsWithSettings.h>, while your new Statistics.cpp:27 still reads the old Parsers path.

One line in Statistics.cpp fixes it:

-#include <Parsers/parseIdentifierOrStringLiteral.h>
+#include <Interpreters/parseIdentifiersOrStringLiteralsWithSettings.h>

That matches every caller of this overload on master (Context.cpp, MergeTask.cpp, MergeTreeDataWriter.cpp, ReadFromMergeTree.cpp, WhatIfIndexEstimator.cpp). Worth merging master in the same push: the new header does not exist on your branch yet, so until you do, the changed include only resolves in the merged tree that CI builds, not in a local build of your branch alone.

Fast test on the same head fails on exactly the same line with the same message, so the one line should clear both. That is the only compile error in either job.

Comment thread src/Storages/MergeTree/MergeTask.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 111/111 (100.00%) · Uncovered code

Full report · Diff report

Resolved conflicts in statistics-related settings and merge logic:
- Combined materialize_statistics_on_insert_max_table_size from master
  with exclude_materialize_statistics_on_insert from this branch
- Updated documentation links to match master's URL format
@cv4g

This comment was marked as resolved.

Comment thread src/Storages/MergeTree/MergeTreeData.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0258934c3 with master 213bd8dd0 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 35e918caf; 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

10 object files changed (+59.81 KiB total), 0 added.

Object file Master PR Δ
src/Parsers/CMakeFiles/clickhouse_parsers.dir/parseIdentifierOrStringLiteral.cpp.o 32.55 KiB 73.53 KiB +40.98 KiB (+125.87%)

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

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

Job report

Comment thread src/Core/Settings.cpp Resolved
ylw510 added 3 commits August 29, 2026 11:21
Add the session and MergeTree exclude settings to the current version
block so settings_changes_history CI passes, and drop an unused
Statistics.h include from MergeTreeData.h.
@ylw510

ylw510 commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author

@cv4g Everything looks good. Please take a review. Thanks.

@clickhouse-gh

This comment was marked as resolved.

…umn-lists

Resolve the conflict with the move of the settings changes history into the setting declarations
(ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as
trailing arguments of the `DECLARE` of each setting.

- retargeted PR record [email protected] to 26.10 (settings-history check rule)
- retargeted PR record [email protected] to 26.10 (settings-history check rule)

Also resolved conflicts in:
- `src/Storages/StorageReplicatedMergeTree.cpp`

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@cv4g cv4g self-assigned this Oct 1, 2026
cv4g and others added 18 commits October 1, 2026 17:01
Both `exclude_materialize_statistics_on_insert` and `exclude_materialize_statistics_on_merge`
had a second "New setting" history record for 26.8, left over from a merge that put them into
both the 26.9 and the 26.8 blocks of `SettingsChangesHistory.cpp`. `system.settings_changes`
and the generated documentation therefore claimed that 26.8, which never shipped them, added
them. Keep only the 26.10 record.
`parseIdentifiersOrStringLiteralsToSet` stops at the first token that does not continue the list
and does not check that it reached the end of the string, and it only reads undotted names. So
`n.x`, a column of a Nested structure, was read as `n` and excluded nothing, `a B` excluded only
`a`, and `n.x, b` excluded neither `n.x` nor `b`.

Add `parseColumnNameList`, which reads a name as an identifier, identifiers joined by dots or a
non-empty string literal, requires the whole string to be the list and reports the offending
token. It uses the default parser limits instead of the query settings, so that the outcome does
not depend on the context the value is parsed in. Use it for both
`exclude_materialize_statistics_on_insert` and `exclude_materialize_statistics_on_merge`; the
released skip-index excludes keep their parser. `collectStatisticsToMaterialize` no longer needs
the settings, which also drops the unneeded `Core/Settings.h` include from `Statistics.cpp`.

Document the syntax: comma-separated, case-sensitive, backquotes or string literals for names with
special characters, `n.x` for Nested columns, and that unknown names are ignored.
…needed

An invalid `exclude_materialize_statistics_on_merge` was accepted by CREATE TABLE, ALTER TABLE
and the `merge_tree` server configuration, and only failed when a merge parsed it. Every merge of
the table then failed and was retried forever, which eventually leads to "too many parts", and a
bad server-wide value did that to every MergeTree table including the system log tables. The list
was also parsed before checking whether the table has statistics at all, so even a table with
`auto_statistics_types = ''` could not merge.

- Check the value in `MergeTreeSettingsImpl::sanityCheck`, which runs on CREATE, on a
  full-definition ATTACH, on ALTER ... MODIFY/RESET SETTING and for the `merge_tree` section at
  server startup. An invalid value is rejected there with an error naming the setting, and the
  server refuses to start with a bad server-wide value.
- Parse the lists for merges and INSERTs only if the table has statistics, so a table without
  statistics is never affected.
- On INSERT, parse `exclude_materialize_statistics_on_insert` before the
  `materialize_statistics_on_insert_max_table_size` check. An invalid value used to fail INSERTs
  into small tables only and to be silently accepted for tables above the limit; now it fails the
  INSERT into every table with statistics. The statistics themselves are still only created when
  they are built.
- `collectStatisticsToMaterialize` now takes the parsed list and builds on the
  `ColumnsStatistics(const ColumnsDescription &)` constructor instead of repeating it.
A background merge between the two INSERTs and the first check would build statistics and make
the check fail. Stop merges around it, as `02346_exclude_materialize_skip_indexes_on_merge` does.
The generator always set the table setting to an empty list, so merges with excluded columns
were never fuzzed. Generate random combinations of the columns `c0` to `c4`, as is done for the
skip index names of `exclude_materialize_skip_indexes_on_merge`. Names of columns that a table
does not have are ignored, so the values can never make a CREATE TABLE fail.
The documentation of `exclude_materialize_statistics_on_merge` said that the statistics of the
excluded columns are still built and stored by MATERIALIZE STATISTICS or during INSERTs. They are
built, but the next merge drops them, even if every merged part has them. With the default
`materialize_statistics_on_insert = 1` only the parts inserted since the last merge then have
statistics for such a column, and the planner applies their selectivity to the whole table: a
condition that matches all rows of a fresh part is estimated to match the whole table.

Describe this, and recommend excluding the column on INSERT as well. Pin the behaviour in 04660:
a merge of parts that all have statistics for an excluded column drops them.

Whether the setting should behave differently (exclude the columns on INSERT as well, or keep
existing statistics through merges that do not remove rows) is a design decision that is left
open here.
… a table

Like `exclude_materialize_skip_indexes_on_insert`, the session setting matches column names in
every MergeTree table the query writes to: excluding `id` for the source table of a materialized
view also excludes it in the view's target table, and in a settings profile the setting applies to
every INSERT of the profile's users. That follows from it being a query setting, so document it and
recommend setting it in the SETTINGS clause of the INSERTs it is meant for. 04661 pins the
behaviour for a materialized view target.
… column renames

The setting refers to columns by name and ALTER TABLE ... RENAME COLUMN / DROP COLUMN do not
change it: after `b` is renamed to `b2`, merges build statistics for `b2` again, and a column
added as `b` later is excluded. Document this and show how to change the setting in the same
ALTER TABLE query.

Rewriting the setting in `AlterCommand::apply` would not be replica-safe, because settings are
not part of the replicated table metadata, and rejecting such ALTERs is a policy decision; both
are left open here.
The first version of the change limited only the implicit statistics that `auto_statistics_types`
adds. `exclude_materialize_statistics_on_insert` and `exclude_materialize_statistics_on_merge`
also exclude statistics declared with `STATISTICS(...)` in the column definition. Document this
change in scope for both settings, and cover it with a test.
Mutations use neither `materialize_statistics_on_merge` nor
`exclude_materialize_statistics_on_merge`. A mutation that rewrites a column, such as
ALTER TABLE ... UPDATE, builds statistics for it, and one that rewrites a whole part, such as
ALTER TABLE ... DELETE or any mutation of a compact part (even a lightweight DELETE or
MATERIALIZE STATISTICS of another column), builds statistics for all columns, the excluded ones
included. Document this for both settings, and check in 04660 that rewriting an excluded column
builds its statistics.
…erges

Cover the merge paths the existing tests do not reach:

- 05318: a vertical merge, with an excluded column in each stage (the sorting key column in the
  horizontal stage, another column in the vertical one), and a ReplacingMergeTree merge, which can
  remove rows and therefore builds the statistics of the new part from the merged rows.
- 05319: two ReplicatedMergeTree replicas with the same list. Both end up with the merged part
  without statistics for the excluded column, whether a replica merges the part itself or fetches
  it.

Nested and dotted column names and explicitly declared statistics are covered by 05316 and 05317.
…n_merge

For ReplicatedMergeTree, ALTER TABLE ... MODIFY SETTING only changes the replica it runs on, and
so does the settings part of a query that also changes columns, such as the RENAME COLUMN
workaround documented for this setting. Replicas with different lists merge the same parts into
different parts: the replica whose part differs from the one another replica committed logs
"Data after merge is not byte-identical", discards its part and downloads the other one (seen as
`DataAfterMergeDiffersFromReplica`). Document this and recommend changing the setting with a
separate ALTER TABLE ... ON CLUSTER ... MODIFY SETTING.
…the size limit

The example claimed that only `a` "and possibly `c`" get statistics on INSERT, but the String
column `c` does get `basic` statistics. Also state that the setting has no effect on tables larger
than `materialize_statistics_on_insert_max_table_size`, into which INSERTs build no statistics.
The documentation of `exclude_materialize_statistics_on_merge` linked to
`exclude_materialize_statistics_on_insert` on the `materialize-statistics-on-insert` page of the
session settings, but the docs generator routes that setting to the `other` page. The generator
only rewrites links within a settings family, and the legacy-route redirect only resolves links
without a page, so the anchor was broken. Link to the page the setting is generated on.
…ning paths

An invalid value of exclude_materialize_statistics_on_insert or
exclude_materialize_statistics_on_merge was reported as "...write a name with
special characters in backquotes: (in the value of setting '...')". The setting
is now part of the main message: "Cannot parse the value 'a b' of setting '...'
as a comma-separated list of column names: unexpected 'b' at position 3. ...".
parseColumnNameList takes the setting name for that, which leaves nothing for
the parseExcludeStatisticsColumns wrapper to do, so it is removed.

parseColumnNameList returns at once for an empty string, so that INSERTs do not
tokenize the default value for every written part, and
collectStatisticsToMaterialize no longer creates the statistics of the excluded
columns only to erase them.

Tests:
- 05318 did not check that the merge is vertical, and it was not when the
  randomized min_bytes_for_full_part_storage made the parts packed. Disable
  packed parts and assert merge_algorithm = 'Vertical' from system.part_log.
- 05332 checks the error messages and the rejection of an invalid value by a
  full-definition ATTACH.
- test_exclude_materialize_statistics_config checks that merges use the value
  from the merge_tree config section and that the server does not start with
  an invalid one.
- Fix a comment in 05316 that described an earlier revision of the change.
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
Style check requires a history entry for the current version when a
setting is added. These settings were still tagged 26.10.

Co-authored-by: ylong2483 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-statistics Column statistics: statistics kept per part on a column (tdigest, uniq, count-min sketch, minmax)... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 19, 2026

This branch has not been deployed

No deployments
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-statistics Column statistics: statistics kept per part on a column (tdigest, uniq, count-min sketch, minmax)... external Author is not a member of the ClickHouse organization pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow include/exclude column lists for automatic column statistics (auto_statistics_types)

6 participants