Skip to content

Support statistics uniqHLL12 - #95474

Closed
hanfei1991 wants to merge 9 commits into
ClickHouse:masterfrom
hanfei1991:hanfei/uniqhll12
Closed

hanfei1991 wants to merge 9 commits into
ClickHouse:masterfrom
hanfei1991:hanfei/uniqhll12

Conversation

@hanfei1991

Copy link
Copy Markdown
Member

I will test the perforcemance before this PR is ready to review.

Changelog category (leave one):

  • Improvement

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

uniqHLL12 will be lightweighter than uniq aggregator. It's an alternative of uniq if we want to enable auto statistics by default.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Jan 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e74e290]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug, targeted) FAIL
Logical error: Rows are not sorted with permutation, position A, previous_row index B, current_row index C (STID: 0250-2113) FAIL cidb

AI Review

Summary

This PR adds uniqHLL12 as a supported statistics type and wires it into cardinality/selectivity estimation and tests. I found one major correctness issue in merge behavior: on incompatible StatisticsUniq state representations, the code now skips state merge but still keeps merged row counts, creating inconsistent statistics that can mislead the optimizer.

PR Metadata
  • ⚠️ Changelog category: Improvement is acceptable for this change.
  • ⚠️ Changelog entry is required for this category and is present, but quality is weak (typo: "lightweighter", vague wording, no concrete user-facing behavior).
    • Suggested replacement: Added support for \uniqHLL12` in MergeTree column statistics (`STATISTICS` and `auto_statistics_types`), including cardinality/selectivity estimation paths and join-order planner tests.`
Missing context
  • ⚠️ No CI logs/results were provided in this review context.
Findings

⚠️ Majors

  • [src/Storages/Statistics/StatisticsUniq.cpp:63] Incompatible-state merge now returns early. But ColumnStatistics::merge already increments rows before per-stat merges, so this creates inconsistent stats (rows from both parts, uniq state from one side). This can significantly skew equality selectivity/cardinality estimates and join-order decisions.
    • Suggested fix: when representations are incompatible, invalidate this statistic in the merged result (fail-closed) instead of silently keeping partial state.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ✅
Core-area scrutiny ⚠️ Inconsistent merged statistics state in StatisticsUniq::merge.
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ⚠️ Changelog entry wording is vague and has a typo.
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Performance & Safety
  • ⚠️ The incompatible-merge path can produce optimizer inputs that are internally inconsistent (row count and cardinality derived from different subsets of data), increasing risk of poor plan selection.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    1. Fix StatisticsUniq incompatible-merge handling to avoid keeping inconsistent partial statistics.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jan 28, 2026
@hanfei1991 hanfei1991 changed the title [WIP] support statistics uniqHLL12 Support statistics uniqHLL12 Feb 20, 2026
@hanfei1991
hanfei1991 marked this pull request as ready for review February 20, 2026 11:39
@rschu1ze rschu1ze added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Feb 24, 2026
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
Comment thread src/Storages/StatisticsDescription.cpp Resolved
@hanfei1991 hanfei1991 removed the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label May 4, 2026
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
Comment thread src/Storages/StatisticsDescription.cpp Resolved
Comment thread src/Storages/Statistics/Statistics.cpp Resolved
/// function state layout depends on the argument type, so merging mismatched
/// states would read garbage. A subsequent MUTATE rebuilds statistics
/// consistently, so it is safe to skip the merge here.
if (!collector->haveSameStateRepresentation(*other->collector))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Returning early here keeps rows merged (done in ColumnStatistics::merge) but leaves uniq/uniqHLL12 state unmerged. That produces internally inconsistent statistics (rows from both parts, cardinality from one part), which can badly skew selectivity/cardinality estimates until a later rebuild.

Instead of silently skipping, please invalidate this statistic when representations differ (e.g. remove this type from the merged stats map or reset to an empty stat) so we fail closed and don't feed inconsistent estimates into the optimizer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's okay, smapling a small part of data will still helpful. For cardinality it's not likely too skew

@hanfei1991

Copy link
Copy Markdown
Member Author

Fuzzer found a LOGICAL ERROR #104376

But I will not fix it in this PR, because it's unrelate

@clickhouse-gh

clickhouse-gh Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

Changed lines: 92.50% (74/80) | lost baseline coverage: 8 line(s) · Uncovered code

Full report · Diff report

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants