Skip to content

Introduce statistics of type "number of distinct values"#59357

Merged
hanfei1991 merged 36 commits intoClickHouse:masterfrom
hanfei1991:hanfei/stats_uniq
Jun 5, 2024
Merged

Introduce statistics of type "number of distinct values"#59357
hanfei1991 merged 36 commits intoClickHouse:masterfrom
hanfei1991:hanfei/stats_uniq

Conversation

@hanfei1991
Copy link
Copy Markdown
Member

@hanfei1991 hanfei1991 commented Jan 29, 2024

This PR introduces the "number of distinct values" (NDV) as statistics type. To add such statistics:

ALTER TABLE tab ADD STATISTICS col1 TYPE Uniq;

Or to add NDV statistics plus other statistics kinds:

ALTER TABLE tab ADD STATISTICS col1 TYPE TDigest, Uniq;

NDV statistics are currently used to estimate the selectivity of equal (=) filters if the NDV is small (< 2048).

Changelog category (leave one):

  • New Feature

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

Introduce statistics of type "number of distinct values".

Documentation entry for user-facing changes

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

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-feature Pull request with new product feature label Jan 29, 2024
@robot-ch-test-poll4
Copy link
Copy Markdown
Contributor

robot-ch-test-poll4 commented Jan 29, 2024

This is an automated comment for commit c04e7e6 with description of existing statuses. It's updated for the latest CI running

⏳ Click here to open a full report in a separate page

Check nameDescriptionStatus
A SyncIf it fails, ask a maintainer for help⏳ pending
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR⏳ pending
Mergeable CheckChecks if all other necessary checks are successful⏳ pending
Successful checks
Check nameDescriptionStatus
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integration tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
PR CheckChecks correctness of the PR's body✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success

@alexey-milovidov alexey-milovidov changed the title Support NDV Statistics Support "number of distinct values" Statistics Jan 30, 2024
@hanfei1991 hanfei1991 mentioned this pull request Mar 11, 2024
1 task
@alexey-milovidov
Copy link
Copy Markdown
Member

uniq uses adaptive sampling - it is fast and precise, at the expense of memory usage.
For column statistics, even 2.5 KB HLL12 (with small object optimization) will work.

@rschu1ze rschu1ze self-assigned this Apr 5, 2024
The following operations are available:

- `ALTER TABLE [db].table ADD STATISTIC (columns list) TYPE type` - Adds statistic description to tables metadata.
- `ALTER TABLE [db].table ADD STATISTIC (columns list) TYPE (type list)` - Adds statistic description to tables metadata.
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.

I know it has been like that before this PR already but still: This page does not make it clear which statistics types exist and how they are different (e.g. with respect to size, usefulness, update cost, usefulness, etc.).

Also, I did not find syntax

CREATE TABLE t1
(
    a Float64 STATISTIC(tdigest),
    b Int64,
    pk String,
) Engine = MergeTree() ORDER BY pk;

documented anywhere (I only found it used in tests). EDIT: Sorry, that is documented (https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree).

It would also be great if this page could include a few examples.

Oh, and is there a system table which shows statistics objects on existing tables?


Stores distribution of values from numeric columns in [TDigest](https://github.com/tdunning/t-digest) sketch.

- `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.

No need for too much brevity. For readability, we can afford unique or unique_count as a statistic name during CREATE/ALTER TABLE.

(for comparison, consider secondary index type bloom_filter which is also not abbreviated as bf)

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.

Re l. 1071: As a user, I'd like to understand more here. What is actually stored?

  • a single value representing the exact distinct value count per column?
  • a single value representing an approximate distinct value count per column? ("Estimate", l. 1071 makes me think something is not exact)
  • a data structure (which one?) that allows to get the number of distinct values for a given value, either the exact count, or an approximate count, or some mixed form (such as top-k statistics which allow to get the exact count for the k contained values and approximate counts for all other values).

Please also mention here if the statistics exist per part, per partition, per shard, or per table.

@hanfei1991
Copy link
Copy Markdown
Member Author

@alexey-milovidov maybe I need your approval, otherwise it cannot be merged

@@ -0,0 +1,45 @@
DROP TABLE IF EXISTS t1;
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 test looks similar to 02864_statistic_operate.sql. What if we combine it with the other file? That will reduce the risk to change stuff in one file but then to forget changing the other file.

Copy link
Copy Markdown
Member Author

@hanfei1991 hanfei1991 May 22, 2024

Choose a reason for hiding this comment

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

well, I want to avoid a huge test file, so I made different statistics type in a different file

@hanfei1991 hanfei1991 enabled auto-merge May 26, 2024 14:50
@hanfei1991 hanfei1991 added this pull request to the merge queue Jun 5, 2024
Merged via the queue into ClickHouse:master with commit ac430bb Jun 5, 2024
@hanfei1991 hanfei1991 deleted the hanfei/stats_uniq branch June 5, 2024 13:23
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

8 participants