Skip to content

Add null_count statistics - #102356

Merged
hanfei1991 merged 24 commits into
ClickHouse:masterfrom
zoomxi:null_count
May 3, 2026
Merged

hanfei1991 merged 24 commits into
ClickHouse:masterfrom
zoomxi:null_count

Conversation

@zoomxi

@zoomxi zoomxi commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • New Feature

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

Added NullCount statistics type for accurate NULL value counting in Nullable columns. This enables improved PREWHERE optimization and better part pruning.

Documentation entry for user-facing changes

  • [*] Documentation is written (mandatory for new features)

@zoomxi

zoomxi commented Apr 10, 2026

Copy link
Copy Markdown
Contributor Author

cc @hanfei1991

@alesapin alesapin assigned hanfei1991 and unassigned hanfei1991 Apr 13, 2026
@alesapin alesapin added can be tested Allows running workflows for external contributors labels Apr 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [244fe17]

Summary: ❌

job_name test_name status info comment
Docs check FAIL
Generate system tables documentation FAIL
AST fuzzer (amd_debug, targeted) FAIL
Logical error: Bad cast from type A to B (STID: 1499-6033) FAIL
Upgrade check (amd_release) FAIL
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/check_report_messages.py FAIL

AI Review

Summary

This PR adds nullcount statistics (including collection, serialization, planner usage for IS NULL / IS NOT NULL, system-table exposure, and docs/tests), and also hardens per-statistics deserialization with size-prefixed V3 payloads. I did not find new correctness, safety, or compatibility issues in the current PR head.

Missing context
  • ⚠️ No CI report/log links were provided in this review request, so runtime signal was limited to code and tests in the diff.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ✅
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Apr 13, 2026
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
Comment thread src/Storages/MergeTree/KeyCondition.cpp Resolved
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
namespace
{
/// Helper to create ColumnStatistics with specified types for merge testing
ColumnStatisticsPtr createTestStats(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Add tests for one-sided NullCount merge behavior and V3 deserialization edge cases.

@zoomxi

zoomxi commented Apr 15, 2026

Copy link
Copy Markdown
Contributor Author

@hanfei1991 CI is green now. Could you please take some time to review this PR when you're free? Thanks!

@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Apr 17, 2026
@hanfei1991

Copy link
Copy Markdown
Member

added null_count to test randomization, let's monitor the status of CI again :)

@zoomxi

zoomxi commented Apr 17, 2026

Copy link
Copy Markdown
Contributor Author

@hanfei1991 I just resolved the conflicts, but since you recently updated clickhouse-test, it seems to have triggered a 'manual approve' label. My commits aren't running the CI automatically anymore.

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

This PR contains two changes:

  1. add null_count statistics
  2. use null_count stats to prune parts during read

Can we make two PRs? For the first PR we can only add null_count.
And in ConditionSelectivityEstimator.cpp We can use getNonNullRowCount instead of getNumRows() in some case.

Comment thread src/Storages/Statistics/Statistics.cpp Resolved
@zoomxi

zoomxi commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

Can we make two PRs? For the first PR we can only add null_count.

Got it. I'll undo the part pruning logic and only include the new null_count statistics here. I'll submit the pruning-related changes in a follow-up PR instead.

@zoomxi

zoomxi commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

Comment thread src/Storages/Statistics/Statistics.cpp Resolved
@hanfei1991

Copy link
Copy Markdown
Member

@zoomxi I modified some code. Feel free to change them again if you have other ideas

@clickhouse-gh

clickhouse-gh Bot commented May 3, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.10% +0.10%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 91.85% (597/650) | lost baseline coverage: 7 line(s) · Uncovered code

Full report · Diff report

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

We need three-valued logic in estimator, but I would make it in another PR ...

@hanfei1991
hanfei1991 added this pull request to the merge queue May 3, 2026
Merged via the queue into ClickHouse:master with commit 17fb5f9 May 3, 2026
160 of 165 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 3, 2026
@zoomxi
zoomxi deleted the null_count branch May 6, 2026 11:44
@hanfei1991

Copy link
Copy Markdown
Member

@zoomxi I just realized minmax actually records not_null_count. Do we actually need an extra null_count?

@zoomxi

zoomxi commented May 9, 2026

Copy link
Copy Markdown
Contributor Author

I just realized minmax actually records not_null_count. Do we actually need an extra null_count?

Yes, MinMax::row_count happens to count non-null rows, and we already use it as a fallback in getNonNullRowCount. But I'd suggest keeping NullCount as a first-class statistic:

  1. MinMax is only valid for numeric types. For non-numeric types (Nullable(String), etc.), MinMax cannot be built at all,so there is no way to derive a null count from it;
  • Uniq and CountMinSketch ignore NULLs by construction, so they need NullCount to produce a more accurate cardinality on nullable columns;
  • Without NullCount, all of these fall back to default factors on non-numeric nullable columns, which directly degrades part pruning, prewhere reordering, and join reordering;
  1. Semantic correctness. Users who only care about IS NULL selectivity shouldn't be forced to enable MinMax.
  2. NullCount is only 8 bytes per part, so keeping it as a separate statistic is not wasteful.

So I'd suggest keeping NullCount. WDYT?

@clickgapai

Copy link
Copy Markdown
Contributor

Hi @zoomxi @hanfei1991 — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request May 10, 2026
SELECT 'Fallback selectivity uses non-null row count (b before a)';
SELECT count() FROM (
EXPLAIN actions=1 SELECT count() FROM test_fallback_selectivity WHERE a = 1 AND b = 1
) WHERE explain LIKE '%Prewhere filter column%b%a%';

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.

@groeneai could you add a test with attach/detach and check everything goes well? Maybe an upgrade and downgrade integration test would be nice too.

@rschu1ze

rschu1ze commented May 18, 2026 •

Copy link
Copy Markdown
Member

@zoomxi @zoomxi Kindly revert this PR and the two follow-up PRs #103949 #104214 (I would have done it by myself but the revert already has lots of merge conflicts) - it must not land in 26.5. null_count as a separate statistics type is too specialized and inconvenient to use for users. Instead, please add a new statistics type Basic that stores these single-value statistics:

  • the smallest and biggest value if the column is numeric (= today's min-max statistics value),
  • the length of the shortest and the longest value if the column is of string type
  • the null count (if the column is nullable).

Setting auto_statistics_types will then change from minmax, uniq to basic, uniq. I think we shouldn't squeeze uniq into basic because it's only an estimation whereas the basic statistics` are exact.


- `NullCount`

Tracks the number of `NULL` values in `Nullable` columns. Used for accurate selectivity estimation of `IS NULL`/`IS NOT NULL` predicates in PREWHERE optimization.

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.

Not clear for readers if the value is exact or an estimation.

pull Bot pushed a commit to Spencerx/ClickHouse that referenced this pull request May 19, 2026
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 manual approve Manual approve required to run CI pr-feature Pull request with new product feature 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.

7 participants