Skip to content

Make setting count_distinct_optimization obsolete - #129307

Merged
Blargian merged 1 commit into
ClickHouse:masterfrom
Algunenano:remove-count-distinct-optimization
Nov 9, 2026
Merged

Blargian merged 1 commit into
ClickHouse:masterfrom
Algunenano:remove-count-distinct-optimization

Conversation

@Algunenano

@Algunenano Algunenano commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

The setting count_distinct_optimization rewrote countDistinct(x) / uniqExact(x) of a query with only a SELECT clause into count() over a GROUP BY x subquery. After radix partitioning of a single uniqExact without GROUP BY (which covers every argument type and spills to disk), the rewrite no longer helps, so CountDistinctPass is removed and the setting is obsolete.

The only case it still covered was a table with an aggregate projection GROUP BY x: the rewritten query read the projection, while uniqExact(x) without GROUP BY reads the table. Supporting it would need aggregation of raw key values in the merge-only aggregation over a projection, which we decided not to add for such a corner case.

Tests that only checked the rewrite are removed; tests of other components that were reached through the rewrite (04066, 04605) now run the rewritten query directly.

Related: #123552
Related: #127252

Changelog category (leave one):

  • Backward Incompatible Change

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

Setting count_distinct_optimization is obsolete and has no effect. It rewrote count(DISTINCT x) into count() over a GROUP BY x subquery, which is no longer faster than the radix partitioned uniqExact used for count(DISTINCT x) without GROUP BY (optimize_uniq_exact_radix_partitioning).


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

Version info

  • Merged into: 26.11.1.1760-master (included in 26.11 and later)

A single `uniqExact` or `count(DISTINCT)` without `GROUP BY` is now computed
by radix partitioning (`optimize_uniq_exact_radix_partitioning`), which covers
every argument type and spills to disk, so the rewrite of `countDistinct(x)`
into `count()` over a `GROUP BY x` subquery is no longer useful. Remove
`CountDistinctPass` and the tests that only checked the rewrite. The tests of
other components that were reached through the rewrite now run the rewritten
query directly.

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

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [37314ff]

Summary: ✅


AI Review

Summary

Removes CountDistinctPass, makes count_distinct_optimization ineffective, and adapts tests that still need the explicit GROUP BY query shape. Request changes: radix partitioning does not replace the bounded-memory execution available through aggregation in sorting-key order.

Findings

⚠️ Majors

  • [P1] Preserve bounded-memory ordered distinct counts — src/Core/Settings.cpp:11771. With both count_distinct_optimization = 1 and optimize_aggregation_in_order = 1, counting a non-null sorting-key column uses a streaming GROUP BY. Removing the rewrite makes it retain distinct keys in the radix path, so memory grows with cardinality. On a MergeTree with x UInt64, ORDER BY x, and 20,000,000 distinct values, the existing rewrite returns 20000000 at 8.36 MiB peak memory; disabling it raises MEMORY_LIMIT_EXCEEDED under the same 128 MiB limit. With a larger limit, radix uses 157.65 MiB. The explicit rewritten query succeeds under the original limit. Preserve an ordered execution path before obsoleting the setting; spilling adds disk I/O to a previously streaming query.
Tests
  • ⚠️ Add a focused stateless regression covering an ordered distinct count under a memory limit that excludes retaining every key. The existing radix tests do not demonstrate preservation of the ordered aggregation's memory bound.
Missing context / blind spots
  • ⚠️ The resource comparison used official ARM master 26.11.1.1332, toggling the existing rewrite to emulate its removal, rather than a PR-head executable. Pipeline inspection confirmed AggregatingInOrderTransform with the rewrite and radix transforms without it. Run the reproducer on the updated PR build to verify the eventual fix; release and performance jobs were still pending when inspected.
Final Verdict

⚠️ Request changes. Preserve bounded-memory execution for ordered distinct counts and add focused coverage before making count_distinct_optimization ineffective.

@clickhouse-gh clickhouse-gh Bot added the pr-backward-incompatible Pull request with backwards incompatible changes label Nov 4, 2026
Comment thread src/Core/Settings.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Nov 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 37314ffb5 with master e1c9e2684 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 968f28c23; 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

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

Job report

@Algunenano
Algunenano added this pull request to the merge queue Nov 9, 2026
@Blargian
Blargian removed this pull request from the merge queue due to a manual request Nov 9, 2026
@Blargian
Blargian added this pull request to the merge queue Nov 9, 2026
Merged via the queue into ClickHouse:master with commit e4c26e6 Nov 9, 2026
186 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 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-backward-incompatible Pull request with backwards incompatible changes 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.

3 participants