Skip to content

Add basic statistics type - #106048

Merged
hanfei1991 merged 15 commits into
ClickHouse:masterfrom
hanfei1991:hanfei/basic-statistics
Jun 6, 2026
Merged

hanfei1991 merged 15 commits into
ClickHouse:masterfrom
hanfei1991:hanfei/basic-statistics

Conversation

@hanfei1991

@hanfei1991 hanfei1991 commented May 28, 2026 •

Copy link
Copy Markdown
Member

A compact, per-column bundle of single-value summaries:

  • numeric / temporal columns: min and max, used for range-filter selectivity and part pruning;
  • String / FixedString columns: total byte length of non-NULL values (i.e. average string length);
  • Nullable / LowCardinality(Nullable) columns: null count.

A single basic declaration on e.g. Nullable(UInt32) populates all applicable summaries at once, instead of stacking separate stat types.

basic is opt-in via STATISTICS(basic) or ALTER ... ADD STATISTICS ... TYPE basic; the default auto_statistics_types is unchanged.

This PR re-applies #102356 and #103949

Changelog category (leave one):

  • Improvement

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

Support basic statistics, a compact per-column statistic that stores numeric min/max, average string length, and NULL counts where applicable.

Version info

  • Merged into: 26.6.1.449

A compact, per-column bundle of single-value summaries:
- numeric / temporal columns: min and max, used for range-filter
  selectivity and part pruning;
- `String` / `FixedString` columns: total byte length of non-NULL
  values (i.e. average string length);
- `Nullable` / `LowCardinality(Nullable)` columns: null count.

A single `basic` declaration on e.g. `Nullable(UInt32)` populates all
applicable summaries at once, instead of stacking separate stat types.

`basic` is opt-in via `STATISTICS(basic)` or `ALTER ... ADD STATISTICS
... TYPE basic`; the default `auto_statistics_types` is unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bcdfe3a]

Summary: ✅


AI Review

Summary

This PR adds basic statistics as an opt-in per-column bundle for numeric min/max, string length, and NULL counts. Most earlier bot findings are fixed in the current code, but three material issues remain in areas the PR explicitly promises: string-length accounting, same-column nullable selectivity, and nullable part pruning. All three already have resolved threads and were dismissed or deferred by the author, so I did not post duplicate inline comments.

Findings

⚠️ Majors

  • [src/Storages/Statistics/StatisticsBasic.cpp:100] [dismissed by author -- https://github.com/Add basic statistics type #106048#discussion_r3357096971] Basic promises string_total_bytes is the total byte length of non-NULL string values, but ColumnString::byteSize counts storage bytes, including terminators and offsets. For values like 'a', 'bbb', the stored statistic is much larger than the actual payload length, so getStringLengthAvg reports storage overhead rather than average string length. Suggested fix: sum getDataAt(i).size for non-NULL rows, or use a payload-only API that excludes offsets and terminators.
  • [src/Storages/Statistics/ConditionSelectivityEstimator.cpp:651] [dismissed by author -- https://github.com/Add basic statistics type #106048#discussion_r3347962577] Same-column mixed IS [NOT] NULL plus range expressions can still be finalized and later combined with another predicate on the same column using the independent-column formula. For example, (a IS NOT NULL OR a > 5) AND a IS NULL is never TRUE, but the current trace estimates positive true rows. Suggested fix: keep same-column null/range predicates in one per-column representation, or conservatively stop using the independent-product path for such expressions.
  • [src/Storages/Statistics/StatisticsPartPruner.cpp:44] [dismissed by author -- https://github.com/Add basic statistics type #106048#discussion_r3347962574] Basic advertises part pruning for nullable numeric columns, but nullable estimates still construct [min, +inf], discarding the max bound even when estimated_null_count = 0. A Nullable(Int64) STATISTICS(basic) part with [0, 999] cannot be pruned for value > 5000, so the promised pruning support is not delivered for this supported shape. Suggested fix: use estimated_null_count to build a bounded range when there are no NULLs, or narrow the advertised contract.
Tests
  • ⚠️ Add focused regression coverage for the three remaining issues above: payload-only string length for String / Nullable(String), same-column nullable selectivity such as (a IS NOT NULL OR a > 5) AND a IS NULL, and nullable Basic part pruning when estimated_null_count = 0.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions: fix the three remaining contract violations above, or narrow the basic statistics contract so it no longer promises those behaviors.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label May 28, 2026
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
@CurtizJ

CurtizJ commented May 28, 2026 •

Copy link
Copy Markdown
Member

Does it make sense to add the number of default values to basic statistics? That will also automatically cover the number of nulls in nullable columns. I want to add statistics with the number of default values for refactoring of serializations, that is needed for automatic lowcardinality (#85145). So, maybe it's better to include this in basic statistics.

@hanfei1991

Copy link
Copy Markdown
Member Author

@CurtizJ It makes sense. I can add it in the next PR

@murphy-4o murphy-4o self-assigned this Jun 3, 2026
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
@hanfei1991
hanfei1991 force-pushed the hanfei/basic-statistics branch from 1334971 to d8de418 Compare June 4, 2026 16:36
@hanfei1991
hanfei1991 force-pushed the hanfei/basic-statistics branch from 22a4492 to 8ccb76c Compare June 5, 2026 12:21
@clickhouse-gh

clickhouse-gh Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.50% 84.50% +0.00%
Functions 92.40% 92.30% -0.10%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 566/665 (85.11%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 46 line(s) · Uncovered code

Full report · Diff report

@hanfei1991
hanfei1991 added this pull request to the merge queue Jun 6, 2026
Merged via the queue into ClickHouse:master with commit 378a25b Jun 6, 2026
166 checks passed
@hanfei1991
hanfei1991 deleted the hanfei/basic-statistics branch June 6, 2026 11:46
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 6, 2026
DECLARE(String, auto_statistics_types, "minmax, uniq", R"(
Comma-separated list of statistics types to calculate automatically on all suitable columns.
Supported statistics types: tdigest, countmin, minmax, uniq.
Supported statistics types: basic, tdigest, countmin, minmax, uniq.

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.

@hanfei1991 When you are back, let's not forget about changing to basic, uniqCombined(12).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

5 participants