Repository navigation
Fix count_distinct_optimization on Nullable columns - #105439
Merged
Algunenano merged 2 commits intoMay 21, 2026
Merged
Algunenano merged 2 commits into
Algunenano merged 2 commits into
Conversation
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
Contributor
|
Workflow [PR], commit [a615cae] Summary: ❌
AI ReviewSummaryThis PR fixes wrong results from the Final VerdictStatus: ✅ Approve |
Contributor
LLVM Coverage Report
Changed lines: 83.33% (5/6) · Uncovered code |
antonio2368
approved these changes
May 21, 2026
Algunenano
enabled auto-merge
May 21, 2026 11:39
Merged
via the queue into
ClickHouse:master
with commit May 21, 2026
e941fe7
162 of 166 checks passed
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
This was referenced 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The analyzer pass for
count_distinct_optimizationrewritescountDistinct(x)/uniqExact(x)toSELECT count() FROM (SELECT x FROM t GROUP BY x). On aNullablecolumnGROUP BYproduces a NULL group thatcountincludes, whilecountDistinctanduniqExactskip NULLs by definition. The optimizationtherefore returned a value off-by-one (or
1instead of0for all-NULLcolumns).
Skip the rewrite when the argument is
NullableorLowCardinality(Nullable).The legacy planner is not affected.
Closes #103069
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong result of
countDistinct/uniqExactonNullablecolumns whencount_distinct_optimization = 1(the NULL group was incorrectly counted).Version info
26.5.1.933