Skip to content

Deprecate the minmax column statistics type - #108680

Merged
hanfei1991 merged 14 commits into
ClickHouse:masterfrom
hanfei1991:deprecate-statistics-minmax
Jul 16, 2026
Merged

hanfei1991 merged 14 commits into
ClickHouse:masterfrom
hanfei1991:deprecate-statistics-minmax

Conversation

@hanfei1991

@hanfei1991 hanfei1991 commented Jun 27, 2026 •

Copy link
Copy Markdown
Member

The minmax statistics type is a subset of basic (which tracks the same numeric min/max plus string-length and null-count summaries). This deprecates minmax in favor of basic:

  • Creating minmax anew is rejected with INCORRECT_QUERY: this covers CREATE ... STATISTICS(minmax), ALTER ... ADD/MODIFY STATISTICS ... TYPE minmax, and auto_statistics_types values containing minmax. The check lives in MergeTreeStatisticsFactory::validate.
  • Existing tables and parts keep working: MergeTreeData::checkProperties skips statistics validation on attach, so loading, reading, and merging parts that still reference minmax continue to work.
  • The default of the auto_statistics_types MergeTree setting changes from minmax, uniq to basic, uniq.

Tests that used minmax are migrated to basic, and the random auto_statistics_types generator in clickhouse-test no longer emits minmax (otherwise random injection would fail unrelated tests). Adds a stateless test for the rejection and an integration test (test_statistics_minmax_upgrade) that creates a minmax table on an old server, upgrades, and verifies it still attaches, reads, and merges while new minmax statistics are rejected.

Changelog category (leave one):

  • Backward Incompatible Change

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

Deprecate the minmax column statistics type and change the default value of auto_statistics_types to 'basic, uniq'

The `minmax` statistics type is a subset of `basic` (which tracks the same
numeric min/max plus string-length and null-count summaries). This deprecates
`minmax` in favor of `basic`:

- Creating `minmax` anew is rejected with `INCORRECT_QUERY`: this covers
  `CREATE ... STATISTICS(minmax)`, `ALTER ... ADD/MODIFY STATISTICS ... TYPE
  minmax`, and `auto_statistics_types` values containing `minmax`. The check
  lives in `MergeTreeStatisticsFactory::validate`.
- Existing tables and parts keep working: `MergeTreeData::checkProperties`
  skips statistics validation on `attach`, so loading, reading, and merging
  parts that still reference `minmax` continue to work.
- The default of the `auto_statistics_types` MergeTree setting changes from
  `minmax, uniq` to `basic, uniq`.

Tests that used `minmax` are migrated to `basic`, and the random
`auto_statistics_types` generator in `clickhouse-test` no longer emits
`minmax` (otherwise random injection would fail unrelated tests). Adds a
stateless test for the rejection and an integration test
(`test_statistics_minmax_upgrade`) that creates a `minmax` table on an old
server, upgrades, and verifies it still attaches, reads, and merges while new
`minmax` statistics are rejected.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cc6586d]

Summary: ✅


AI Review

Summary

This PR deprecates the minmax column-statistics type and switches the default auto_statistics_types to basic, uniq, but the current implementation still has one backward-compatibility regression and several ALTER/ATTACH paths that either keep minting new minmax metadata or reject valid upgraded metadata. It is not ready to merge in the current state.

Findings

❌ Blockers

  • [src/Storages/MergeTree/registerStorageMergeTree.cpp:907-915] The new validateAutoStatisticsTypes call now runs on the effective MergeTree default, not just on explicit SETTINGS auto_statistics_types = .... Context::getMergeTreeSettings applies compatibility first, and this PR also adds auto_statistics_types = 'minmax, uniq' as the pre-26.7 compatibility value in src/Core/SettingsChangesHistory.cpp, so SET compatibility = '26.6'; CREATE TABLE ... ENGINE = MergeTree ... will now fail with INCORRECT_QUERY even when the query never mentioned auto_statistics_types. That breaks the compatibility contract this PR is supposed to preserve.
    Suggested fix: validate only an explicit auto_statistics_types clause from the CREATE AST (or otherwise skip compatibility/default-derived values), and add a regression test for plain CREATE TABLE under compatibility = '26.6'.

⚠️ Majors

  • [src/Storages/MergeTree/MergeTreeData.cpp:4714-4715, 5489-5497] Upgraded tables whose old explicit auto_statistics_types still contains minmax can still create fresh deprecated metadata on unrelated ALTERs. getNewImplicitStatisticsTypes treats any existing override in new_metadata.settings_changes as a fresh change, so ALTER TABLE ... ADD COLUMN x UInt64 reuses the legacy minmax setting and addImplicitStatistics attaches implicit MinMax to the new column. That violates the PR contract that the current version can no longer create new minmax statistics.
    Suggested fix: grandfather old implicit minmax only for columns that already existed before the ALTER, or rewrite legacy auto_statistics_types away from minmax before reapplying implicit statistics to new columns.

  • [src/Storages/MergeTree/MergeTreeData.cpp:1255-1260] allow_deprecated_minmax keys off old_metadata.columns.get(col.name).statistics.contains("minmax"), which matches both explicit and implicit legacy statistics. On an upgraded table whose old minmax came only from auto_statistics_types, ALTER TABLE ... ADD STATISTICS a TYPE minmax or MODIFY COLUMN a ... STATISTICS(minmax) is still accepted because the old implicit minmax makes allow_deprecated_minmax true. That still lets the current version mint a fresh explicit STATISTICS(minmax).
    Suggested fix: grandfather only columns whose previous minmax descriptor was already explicit (is_implicit == false), not every column whose old metadata merely contained MinMax.

  • [src/Storages/MergeTree/MergeTreeData.cpp:1255-1260] Column renames on upgraded tables with explicit legacy STATISTICS(minmax) are still broken. ALTER TABLE ... RENAME COLUMN value TO value2 moves the existing ColumnDescription to value2, but allow_deprecated_minmax only checks the post-rename name against old_metadata, so validation treats the inherited explicit minmax as newly introduced and throws. That contradicts the PR’s promise that already-loaded minmax metadata remains alterable.
    Suggested fix: carry rename mappings into the grandfathering check so a renamed column can still be matched to its pre-ALTER explicit statistics, and add a regression test for RENAME COLUMN after upgrade.

  • [src/Storages/MergeTree/MergeTreeData.cpp:4714-4715, 5489-5497; src/Storages/StorageMergeTree.cpp:484-493; src/Storages/StorageReplicatedMergeTree.cpp:6803-6822] RESET SETTING auto_statistics_types on an upgraded table with an old explicit auto_statistics_types = 'minmax, uniq' still leaves the in-memory metadata on the deprecated implicit statistics until restart. commands.apply removes the AST entry, getNewImplicitStatisticsTypes falls back to old_settings[auto_statistics_types], and statistics_changed becomes false, so the settings-only ALTER path updates the runtime setting to the new default but skips setInMemoryMetadata(new_metadata). Inserts and merges then keep using the old implicit MinMax metadata until another metadata refresh happens.
    Suggested fix: treat RESET SETTING auto_statistics_types as an effective-value change and refresh implicit statistics immediately for both ordinary and replicated tables; add a regression covering both engines.

  • [dismissed by author -- https://github.com/Deprecate the minmax column statistics type #108680#discussion_r3485624958] [src/Storages/MergeTree/registerStorageMergeTree.cpp:914-916; src/Storages/MergeTree/MergeTreeData.cpp:1255-1260] Full user-supplied ATTACH TABLE ... (columns) ENGINE = MergeTree ... definitions can still introduce new deprecated minmax metadata. User ATTACH runs with LoadingStrictnessLevel::ATTACH, so validateAutoStatisticsTypes is skipped, and checkProperties(..., attach = true, ...) also grandfathers explicit STATISTICS(minmax) unconditionally. That means a fresh table can still be created on the current version through full ATTACH (including SETTINGS auto_statistics_types = 'minmax, uniq'), which contradicts the PR’s stated “creating minmax anew is rejected” contract.
    Suggested fix: keep the grandfathering only for real metadata-reload paths (FORCE_ATTACH / startup / restore of existing metadata), but validate full user ATTACH definitions like CREATE.

Final Verdict

Request changes: one new blocker remains, and the existing ALTER/ATTACH gaps still leave the deprecation incomplete.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 27, 2026
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
@EmeraldShift

Copy link
Copy Markdown
Contributor

Is basic just as cheap to compute as minmax? This makes sense as long as it's not more expensive and removing a cheaper option.

Comment thread src/Storages/Statistics/Statistics.cpp Resolved
@hanfei1991
hanfei1991 marked this pull request as ready for review July 6, 2026 09:29
@hanfei1991

Copy link
Copy Markdown
Member Author

Is basic just as cheap to compute as minmax? This makes sense as long as it's not more expensive and removing a cheaper option.

Yes, both of them are quite lightweight

hanfei1991 and others added 2 commits July 8, 2026 13:18
Reject deprecated `minmax` only when a statement newly introduces it, so an
unrelated `ALTER` of an old table that still carries explicit `minmax` is not
rejected. Fresh `CREATE` with `minmax` is still rejected.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 37 queries analysed

This PR deprecates the minmax column-statistics type (rejecting it at CREATE/ALTER) and switches the default auto_statistics_types from minmax, uniq to basic, uniq; it is DDL-validation and default-setting plumbing, not a query-execution change. The two flagged ClickBench improvements (Q4 -17.1%, Q15 -19.5%) were correctly suppressed as within master's current variance band: both deltas are wall-clock only with flat CPU, and neither query exercises the changed code path, so they are run-to-run variance rather than a real effect of this change. No genuine per-query regressions or improvements are attributable to this PR.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: a25b8635-d2cf-4fae-9394-e1ea373415d7
  • MIRAI run: 4f3a84d5-e9e3-48a7-94ff-33e68d2bf613
  • PR check IDs:
    • clickbench_625182_1784121288
    • clickbench_625195_1784121288
    • clickbench_625200_1784121288
    • tpch_adapted_1_official_625203_1784121288
    • tpch_adapted_1_official_625213_1784121288
    • tpch_adapted_1_official_625220_1784121288

@murphy-4o murphy-4o self-assigned this Jul 13, 2026

@murphy-4o murphy-4o 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.

LGTM

Comment thread src/Storages/Statistics/Statistics.cpp Outdated
/// The `minmax` statistics type is deprecated: it is a subset of `basic`, which should be used instead.
/// Reject it only when it is declared explicitly (`STATISTICS(minmax)` in CREATE / ALTER) and newly
/// introduced by the current statement:
/// - Implicit `minmax` produced by the `auto_statistics_types` setting is tolerated here (including on

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This explicitly grandfathers implicit minmax on freshly added columns of tables whose old auto_statistics_types still contains it. After upgrading such a table, ALTER TABLE ... ADD COLUMN x UInt64 takes the unchanged setting from getNewImplicitStatisticsTypes, addImplicitStatistics re-attaches implicit MinMax to x, and validate accepts it because desc.is_implicit is exempt here. That still creates new deprecated minmax metadata on the current version without any setting change, which contradicts the PR contract and the new 04411 test comments that new minmax can no longer be created.

Please grandfather existing implicit minmax only for columns that were already present before the ALTER, or rewrite grandfathered auto_statistics_types away from minmax before it can be applied to newly added columns.

/// blocked. The rest of the statistics validation (unknown type, data-type support) still runs.
bool allow_deprecated_minmax = attach
|| (is_alter
&& old_metadata.columns.has(col.name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow_deprecated_minmax is keyed only by the post-ALTER column name. On an upgraded table that still carries explicit STATISTICS(minmax), ALTER TABLE ... RENAME COLUMN value TO value2 moves the existing ColumnDescription (including the explicit minmax) to value2, but old_metadata.columns.has(col.name) is now false, so MergeTreeStatisticsFactory::validate treats the inherited minmax as newly introduced and throws.

That breaks the PR contract that already-loaded old minmax metadata remains alterable. Please grandfather explicit minmax through renames as well, e.g. by tracing the pre-rename column identity instead of matching only the post-ALTER name, and add a regression test for RENAME COLUMN on an upgraded table.

/// setting is unaffected.
for (const auto & command : commands)
{
if (command.type == AlterCommand::MODIFY_SETTING)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still misses RESET SETTING auto_statistics_types on upgraded tables that have an old explicit auto_statistics_types = 'minmax, uniq'. After commands.apply, the reset removes the AST entry entirely, so getNewImplicitStatisticsTypes falls back to old_settings[auto_statistics_types], returns the deprecated value, and reports statistics_changed = false.

In the settings-only ALTER branch that means changeSettings switches storage_settings to the new default basic, uniq, but setInMemoryMetadata(new_metadata) is skipped, so inserts and merges continue using the old implicit MinMax metadata until restart or another ALTER rebuilds the columns. Please treat RESET SETTING auto_statistics_types as a real effective-value change and refresh implicit statistics immediately for both ordinary and replicated tables.

@rschu1ze

Copy link
Copy Markdown
Member

@hanfei1991 Could you please change the new default to basic,uniq_v2?

@hanfei1991

Copy link
Copy Markdown
Member Author

@hanfei1991 Could you please change the new default to basic,uniq_v2?

I prefer to change uniq to uniq_v2 in another PR. This PR focuses on basic

@rschu1ze rschu1ze self-assigned this Jul 15, 2026
bool allow_deprecated_minmax = attach
|| (is_alter
&& old_metadata.columns.has(col.name)
&& old_metadata.columns.get(col.name).statistics.contains("minmax"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow_deprecated_minmax currently keys off old_metadata.columns.get(col.name).statistics.contains("minmax"), which matches both explicit and implicit old minmax. That lets an upgraded table whose legacy minmax came only from auto_statistics_types mint a fresh explicit minmax on the current version, for example with ALTER TABLE t ADD STATISTICS a TYPE minmax or ALTER TABLE t MODIFY COLUMN a UInt64 STATISTICS(minmax).

removeImplicitStatistics strips the inherited implicit stats from new_metadata, but old_metadata still makes allow_deprecated_minmax true here, so validate accepts the newly explicit declaration. That violates the PR contract that fresh STATISTICS(minmax) declarations are rejected after upgrade.

Please grandfather only columns whose previous StatisticsType::MinMax descriptor was already explicit (is_implicit == false), instead of using a plain contains("minmax") check.

@rschu1ze rschu1ze 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.

@hanfei1991 Question: This PR deprecates min-max statistics. Normally, deprecation of a feature is followed by the removal of the feature. I don't see any possibility to remove minmax statistics from ClickHouse in the long run after deprecation. Do you have a good idea how this can be achieved?

EDIT: Maybe the query execution can simply ignore minmax statistics at some point in future (perhaps in 12 months from now)?

@@ -0,0 +1,37 @@
-- Tags: no-fasttest

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 is this no-fasttest?

@@ -0,0 +1,151 @@
import pytest

from helpers.cluster import CLICKHOUSE_CI_MIN_TESTED_VERSION, ClickHouseCluster

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.

CLICKHOUSE_CI_MIN_TESTED_VERSION is 25.3, but it is really a moving target. Consider hard-coding this to e.g. 26.5.

Comment thread src/Storages/Statistics/Statistics.cpp Outdated
if (statistics_types_str.empty())
return;

/// Runs only on the setting-change path (CREATE / ALTER ... MODIFY SETTING auto_statistics_types),

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.

Please remove l. 865-866. Function validateAutoStatisticsType has no authority about the places from which it is called. It cannot make any such claims.

Comment thread src/Storages/Statistics/Statistics.cpp Outdated
{
for (const auto & [type, desc] : stats.types_to_desc)
{
/// The `minmax` statistics type is deprecated: it is a subset of `basic`, which should be used instead.

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.

L. 714-724 are way too much bla bla and it is also wrong to talk about other code parts (ALTER TABLE) in this file. Let's remove.

validate_complex_projection(projection.name, {"_block_offset"});
}

/// On CREATE the constructor passes the very same metadata object as both `new` and `old` (there is no

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.

See my other comment about "bla bla" below, the same applies here. Can you try to compress the comment into a one-liner?

/// with them in `auto_statistics_types`. Skipped for ATTACH and replicated propagation so that
/// existing tables which still carry `minmax` in the setting keep loading.
if (args.mode <= LoadingStrictnessLevel::CREATE)
validateAutoStatisticsTypes(statistics_types_str);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context::getMergeTreeSettings already applies the session/profile compatibility defaults before this code copies the effective MergeTree settings. With the new 26.7 history entry for auto_statistics_types, compatibility = '26.6' makes the effective default here minmax, uniq even for a plain CREATE TABLE ... ENGINE = MergeTree with no explicit SETTINGS clause, so this unconditional validation now throws INCORRECT_QUERY for a statement that previously worked.

That breaks the backward-compatibility contract the PR just encoded in SettingsChangesHistory.cpp. Please validate only an explicit auto_statistics_types clause from the CREATE AST (or otherwise skip the check when the value comes only from compatibility/defaults), and add a regression test for SET compatibility = '26.6'; CREATE TABLE ... ENGINE = MergeTree ....

@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 28.60% 85.80% +57.20%
Functions 39.40% 92.70% +53.30%
Branches 20.60% 78.00% +57.40%

Changed lines: Changed C/C++ lines covered: 56/57 (98.25%) · Uncovered code

Full report · Diff report

@hanfei1991
hanfei1991 added this pull request to the merge queue Jul 16, 2026
Merged via the queue into ClickHouse:master with commit e7228ab Jul 16, 2026
176 checks passed
@hanfei1991
hanfei1991 deleted the deprecate-statistics-minmax branch July 16, 2026 15:49
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 16, 2026
@fm4v fm4v removed the pr-improvement Pull request with some product improvements label Jul 23, 2026
@fm4v

fm4v commented Jul 23, 2026

Copy link
Copy Markdown
Member

@hanfei1991 @rschu1ze
Please make this backward compatible - otherwise, existing tables will get an error on upgrade::

Code: 80. DB::Exception: Received from localhost:9000. DB::Exception: Statistics type 'minmax' is deprecated. Use 'basic' instead, which is a superset of 'minmax'.. (INCORRECT_QUERY)

@hanfei1991

Copy link
Copy Markdown
Member Author

@fm4v It's supposed to be backward compatible, and we added an integration test in this PR.
Could you give me a test case to reproduce?

@rschu1ze

Copy link
Copy Markdown
Member

The PR goes great length that existing tables with minmax statistics continue to load, there is even an integration test for it. We'll need a counterexample that proves that upgrades can break.

(There is a philosophical argument that "deprecating X" (PR title) and "rejecting new instances of X" (what the PR actually does) are different things, but that's not relevant to the discussion.)

@fm4v

fm4v commented Jul 23, 2026

Copy link
Copy Markdown
Member

Sorry for the missing info, I thought it would be an easier case. Created the issue with repros #111687

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…tics-minmax

Deprecate the minmax column statistics type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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