Skip to content

Fix count_distinct_optimization on Nullable columns - #105439

Merged
Algunenano merged 2 commits into
ClickHouse:masterfrom
Algunenano:fix-count-distinct-optimization-nullable
May 21, 2026
Merged

Algunenano merged 2 commits into
ClickHouse:masterfrom
Algunenano:fix-count-distinct-optimization-nullable

Conversation

@Algunenano

@Algunenano Algunenano commented May 20, 2026 •

Copy link
Copy Markdown
Member

The analyzer pass for count_distinct_optimization rewrites countDistinct(x) /
uniqExact(x) to SELECT count() FROM (SELECT x FROM t GROUP BY x). On a
Nullable column GROUP BY produces a NULL group that count includes, while
countDistinct and uniqExact skip NULLs by definition. The optimization
therefore returned a value off-by-one (or 1 instead of 0 for all-NULL
columns).

Skip the rewrite when the argument is Nullable or LowCardinality(Nullable).
The legacy planner is not affected.

Closes #103069

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

Fix wrong result of countDistinct / uniqExact on Nullable columns when count_distinct_optimization = 1 (the NULL group was incorrectly counted).

Version info

  • Merged into: 26.5.1.933

The analyzer pass rewrote `countDistinct(x)` to `count() FROM (SELECT x GROUP BY x)`.
On a `Nullable` column `GROUP BY` produces a NULL group that `count` includes,
while `countDistinct`/`uniqExact` skip NULLs. Skip the rewrite when the argument
is `Nullable` (or `LowCardinality(Nullable)`).

Fixes ClickHouse#103069
@clickhouse-gh

clickhouse-gh Bot commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a615cae]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_tsan, 6/6) FAIL
test_grpc_protocol/test.py::test_cancel_while_generating_output FAIL cidb, issue ISSUE EXISTS
AST fuzzer (arm_asan_ubsan) FAIL
Logical error: AggregateFunctionArray: parameters mismatch between Array wrapper 'A' and nested function 'B'. Wrapper has C parameter(s): [D], nested function has E parameter(s): [F] (STID: 4870-4d73) FAIL cidb IGNORED
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/check_report_messages.py FAIL IGNORED

AI Review

Summary

This PR fixes wrong results from the count_distinct_optimization rewrite on Nullable and LowCardinality(Nullable) arguments by skipping the rewrite in those cases, and it adds focused stateless coverage (plus one corrected expected result in an existing test). I did not find unresolved correctness, safety, or performance issues in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 83.33% (5/6) · Uncovered code

Full report · Diff report

@antonio2368 antonio2368 self-assigned this May 21, 2026
@Algunenano
Algunenano enabled auto-merge May 21, 2026 11:39
@Algunenano
Algunenano added this pull request to the merge queue May 21, 2026
Merged via the queue into ClickHouse:master with commit e941fe7 May 21, 2026
162 of 166 checks passed
@Algunenano
Algunenano deleted the fix-count-distinct-optimization-nullable branch May 21, 2026 11:52
groeneai added a commit to groeneai/ClickHouse that referenced this pull request May 21, 2026
@azat: the inline Python proxy in the .sh was hacky. Moved it to
`tests/queries/0_stateless/helpers/04260_client_segfault_proxy.py`
and compacted the .sh comments. Made the proxy single-shot (one
connection, then exits) so no external `kill -9` is needed, which
also removes the "Killed" bash job-control notification that the
runner reported as test stderr noise on `amd_tsan, s3` and `amd_tsan`
(this is what was actually breaking the test in CI).

The C++ fix in `ClientBase::processOrdinaryQuery` is unchanged.

Renumbered to 04260 to avoid the collision with ClickHouse#105439's new
`04259_count_distinct_optimization_nullable.sql` that just landed
on master.

Verified locally: pre-fix master binary deterministically SIGSEGVs
at Address 0x108 in `ReadBufferFromPocoSocketChunked::poll`; the
rebuilt binary with the fix passes 10/10 with empty stderr.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105386
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 21, 2026
DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
…ct-optimization-nullable

Fix count_distinct_optimization on Nullable columns
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
@azat: the inline Python proxy in the .sh was hacky. Moved it to
`tests/queries/0_stateless/helpers/04260_client_segfault_proxy.py`
and compacted the .sh comments. Made the proxy single-shot (one
connection, then exits) so no external `kill -9` is needed, which
also removes the "Killed" bash job-control notification that the
runner reported as test stderr noise on `amd_tsan, s3` and `amd_tsan`
(this is what was actually breaking the test in CI).

The C++ fix in `ClientBase::processOrdinaryQuery` is unchanged.

Renumbered to 04260 to avoid the collision with ClickHouse#105439's new
`04259_count_distinct_optimization_nullable.sql` that just landed
on master.

Verified locally: pre-fix master binary deterministically SIGSEGVs
at Address 0x108 in `ReadBufferFromPocoSocketChunked::poll`; the
rebuilt binary with the fix passes 10/10 with empty stderr.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105386
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

count_distinct_optimization overcounts by including NULL group when column is Nullable

3 participants