Skip to content

Do not answer min/max from statistics of a column a part stores with another type - #126076

Open
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-116627-statistics-stale-part-type
Open

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-116627-statistics-stale-part-type

Conversation

@groeneai

@groeneai groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #116627
Related: #119337
Related: #117320

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):

Fixed wrong min/max answered from column statistics (use_statistics_for_min_max_aggregation) for a part that still stores the column with its type from before an ALTER TABLE ... MODIFY COLUMN, for example after the mutation was killed. Such parts are now read instead.

Description

On 26.9.16.11, after KILL MUTATION of ALTER TABLE t MODIFY COLUMN x UInt8 (x was UInt64 with values 250 and 260), SELECT min(x), max(x) FROM t returns 250 4 instead of 4 250.

A part's statistics hold values of the column type the part was written with. After a killed MODIFY COLUMN, or while it is pending with apply_mutations_on_fly = 0, apply_patch_parts = 0, the part keeps the old type and a read converts its values (260 reads as 4). makeBlockWithMinMaxFromStatistics still took the part's old min/max and inserted them as the new type: its only per-part type check compares the Field type, which a narrowing inside one Field type passes (UInt64 -> UInt8, UInt32 -> UInt16, or a Decimal scale change, where Decimal64(3) -> Decimal64(1) returned 123.4 567.8 instead of 1.2 5.6).

Now a part is answered from statistics only when its type of the column reads back unchanged as the current type, using the predicate skip indexes already use for the same state (isRepresentationPreservingConversion, #112484). Metadata-only changes such as an enum widening keep the shortcut; other parts are read.

#119337 covers statistics part pruning for this state; this PR covers the min/max shortcut, which #119337 does not touch.

Test 05401_min_max_from_statistics_stale_part_type: pending with both settings off, killed, and an enum widening that keeps the shortcut. It fails on master and passes with the fix, including 50 randomized runs.


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

…another type

A part's statistics hold values of the column type the part was written
with. After KILL MUTATION of an ALTER MODIFY COLUMN, or while it is pending
with apply_mutations_on_fly = 0, the part keeps the old type and a read
converts its values (UInt64 260 reads as UInt8 4). The min/max shortcut took
the part's old min/max and inserted them as the new type, because its only
per-part type check compares the Field type, which a narrowing inside one
Field type passes. SELECT min(x), max(x) returned 250 4 instead of 4 250, and
a Decimal64(3) -> Decimal64(1) change returned 123.4 567.8 instead of 1.2 5.6.

Answer a part from statistics only when its type of the column reads back
unchanged as the current type, using the predicate skip indexes use for the
same state (isRepresentationPreservingConversion), so metadata-only changes
such as an enum widening keep the shortcut. Other parts are read.

Statistics part pruning for this state is handled by ClickHouse#119337.

Related: ClickHouse#116627

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

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

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

# Sev Finding Verdict Evidence / action
1 💡 A query-plan optimization includes MergeTreeIndices.h only to reach a type predicate DISAGREE Exporting the existing predicate is the smallest change; one .cpp includes it, no high-fan-out header is touched
2 💡 The predicate's existing doc comment talks only about skip-index granules DISAGREE The header declaration states the general contract; for statistics the semantics coincide (equals() types are not converted on read, allow-listed pairs keep the numeric value)

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

Session id: cron:clickhouse-review-slot-11:20261015-165500

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new test on master: SELECT min(x), max(x) returns 250 4 (truth 4 250) for a killed UInt64 -> UInt8 MODIFY COLUMN and for the pending one with apply_mutations_on_fly = 0, apply_patch_parts = 0, every run (10/10 randomized runs fail).
b Root cause explained? Yes. Part statistics hold values of the part's own column type; makeBlockWithMinMaxFromStatistics inserted them as the current type after a Field-type check that a narrowing inside one Field type passes, while the read converts the part's values.
c Fix matches root cause? Yes. The part is answered from statistics only when its column type reads back unchanged as the current type (isRepresentationPreservingConversion, the skip-index predicate for the same state); otherwise it is read.
d Test intent preserved / new tests added? Yes. New 05401_min_max_from_statistics_stale_part_type: pending, killed, and an enum widening that must keep the shortcut.
e Both directions demonstrated? Yes. Fails on master (the two min/max lines), passes with the fix; 50/50 randomized runs. Removing the check reddens both lines; a plain equals() reddens only the enum shortcut line.
f Fix is general across code paths? Yes. count() uses the part row count; statistics part pruning is #119337; skip indexes already use the same predicate; the selectivity estimator only orders filters.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Checked UInt32 -> UInt16 and Decimal64(3) -> Decimal64(1) (both wrong on master), Int, Float, widening, enum and DateTime/Date pairs: all equal a normal read. Nullable and LowCardinality columns are not answered from statistics at all.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or stored data change; only more parts are read.
i Invariants and contracts preserved? Yes. A refused part returns before any value is folded, so the folded min/max and the covered-part counters only include accepted parts.

Session id: cron:clickhouse-impl-slot-7:20261015-161900

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [69d4926]

Summary: ❌

job_name test_name status info comment
Unit tests (asan_ubsan, function_prop_fuzzer) FAIL
FunctionsStress.stress FAIL cidb
AllTests FAIL cidb
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL
Build (amd_darwin) DROPPED
Build (arm_darwin) DROPPED
Build (arm_v80compat) DROPPED
Build (amd_freebsd) DROPPED
Build (amd_illumos) DROPPED
Build (ppc64le) DROPPED
Build (amd_compat) DROPPED
Build (amd_musl) DROPPED

AI Review

Summary

The PR prevents min/max from using statistics when a part's stored column type requires a value-changing conversion to the queried type, while preserving the shortcut for compatible changes such as enum widening. No actionable findings. The new stateless regression test covers pending and killed type-changing mutations and compatible enum widening, and passed in Fast test CI for commit 69d49267ec98e580e6e9b13cf9b3eb34a0a86a77.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Michicosun @CurtizJ, could you review this, and add the can be tested label so CI runs? After a killed (or pending, with apply_mutations_on_fly = 0) ALTER ... MODIFY COLUMN, a part keeps the old column type, and makeBlockWithMinMaxFromStatistics answered min/max from its old-type statistics (UInt64 -> UInt8: 250 4 instead of 4 250); it now uses those statistics only when isRepresentationPreservingConversion says the part type reads back unchanged, and reads the part otherwise.

@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 15, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 69d4926

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
Unit tests (asan_ubsan, function_prop_fuzzer) / FunctionsStress.stress stuck thread, not PR-caused: Thread is stuck while executing: SELECT range(CAST(7579902 AS Int64)). This diff does not touch src/Functions; the same range stuck-thread shape has failed on several unrelated PRs and on master since 2026-10-15 a fix task is created (investigating at full effort, fixing-PR link to follow here)
Finish Workflow / Post Hooks cascade of the row above: new_tests_check.py reports both Bugfix validation (functional tests, amd64/aarch64) jobs as DROPPED (Dropped due to previous failure [Unit tests (asan_ubsan, function_prop_fuzzer)]), so no per-arch job could report OK or XFAIL a fix task is created (the one for the first row)
56 dropped jobs (stateless, builds, integration, install, performance, stress, upgrade, compatibility) not run: praktika dropped them because the first row failed, so this head has no results for them a fix task is created (the one for the first row)
Mergeable Check, PR rollups of the rows above a fix task is created (the one for the first row)

Session id: cron:our-pr-ci-monitor:20261015-213010

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the FunctionsStress.stress range stuck thread: #126210

@fm4v

fm4v commented Oct 27, 2026

Copy link
Copy Markdown
Member

#119337 now contains the same fix (commit d7adf71). makeBlockWithMinMaxFromStatistics refuses a part unless isRepresentationPreservingConversion holds for the part's column type against the current type, and it adds a per-part pending-mutation check. The two PRs conflict in optimizeUseAggregateProjection.cpp. #119337 is the one proposed to land for #116627, so this PR becomes redundant once it merges. @groeneai, please check whether 05401_min_max_from_statistics_stale_part_type covers a case that 119337's 05200/05427/05448 tests do not (for example the Decimal scale change), and report it on 119337 if so.

fm4v added a commit that referenced this pull request Oct 27, 2026
…istics of a part with a stale column type

`05453_statistics_pruning_pending_rename` covers the repro of #117323: with a pending `DROP COLUMN x, RENAME COLUMN y TO x`, statistics-based part pruning and `min`/`max` from statistics must not use the estimate of the dropped column stored under `x`.

`05454_statistics_min_max_stale_part_type` ports the cases of #126076 that the existing tests do not cover: `min`/`max` from statistics while a narrowing `MODIFY COLUMN` is pending with `apply_mutations_on_fly = 0` (so it is not in the mutations snapshot), after it is killed, and a `Decimal` scale change, which keeps the type of `Field`.

Both tests pass with the CI build of 4f7649a and fail with a build without this PR.
@fm4v

fm4v commented Oct 27, 2026

Copy link
Copy Markdown
Member

Follow-up to my previous comment: please disregard my request. I ported the cases of 05401_min_max_from_statistics_stale_part_type that #119337 did not cover into it myself, as 05454_statistics_min_max_stale_part_type (commit e260ffd). Those cases are the pending narrowing MODIFY COLUMN with apply_mutations_on_fly = 0 and the killed one. I also added a Decimal64(2) to Decimal64(1) scale change. 119337 already has the code fix (the isRepresentationPreservingConversion check in makeBlockWithMinMaxFromStatistics), so nothing is needed from this PR.

@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)... pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants