Skip to content

Conversation

@hanfei1991
Copy link
Member

@hanfei1991 hanfei1991 commented Aug 9, 2023

What is statistic

an explaination from SQL Server https://learn.microsoft.com/en-us/sql/relational-databases/statistics/statistics?view=sql-server-ver16

We use tdigest as a pratical histogram

How to create / manipulate statistic in ClickHouse

we treat statistic as a property of a table, such as CODEC, TTL ...

but we store and manipulate statistic seperately, such as INDEX, PROJECTION

create

CREATE TABLE test (
    a Int64 STATISTIC type (tdigest),
    b Int64 STATISTIC type (tdigest)
) ...

manipulate

ALTER TABLE test ADD / DROP / CLEAR / MATERIALIZE STATISTIC a, b TYPE tdigest

How to store it in a part

we store a single file containing all types of statistics for every column which has statistic.

image

and

  • we write statistic automatically when inserting / merging to a new part

how do we use statistic in where optimizer

  • only work for integers
  • when prewhere condition is like a < 5 and b > 1 and c < 4.0, then we try to re-order them accroding to the selectivity
  • read statistic from file systems every time

more works need to do for this PR:

  • doc + rfc
  • tests
  • validate for statistic create
  • collect statistic by partition pruner
  • more comments
  • serialize statistic files as above described (will be implemented in the next PR)
  • manipulate statistic when column changed
    • drop/clear statistic when column is dropped/cleared
    • rename ...
    • do not modify column when the changed type conflicts with statistic
  • implement lessThan for TDigest

hope that I could control the + lines within 2000 😺

what to do in the future

#55065

Changelog category (leave one):

  • New Feature

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

use statistic to order prewhere conditions better

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

@hanfei1991 hanfei1991 added the pr-feature Pull request with new product feature label Aug 9, 2023
@robot-ch-test-poll1
Copy link
Contributor

robot-ch-test-poll1 commented Aug 9, 2023

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

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

Successful checks
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
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✅ success
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
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker image for serversThe check to build and optionally push the mentioned image to docker hub✅ 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
Install packagesChecks that the built packages are installable in a clear environment✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ success
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub✅ success
SQLTestThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool✅ success
SqllogicRun clickhouse on the sqllogic test set against sqlite and checks that all statements are passed✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ 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
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success
Check nameDescriptionStatus
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. Integrational 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❌ failure
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests❌ failure

@JackyWoo
Copy link
Contributor

JackyWoo commented Nov 17, 2023

hi @hanfei1991 I am interesting with your PR. I have some questions, expecting for your answers.

  1. The statistc is for prewhere conditions optimization, have you considered expanding it to other scenarios?
  2. As my understanding it works in local execution node, am I right?
  3. Why it is only for integer?

@hanfei1991
Copy link
Member Author

@JackyWoo

  1. yes, statistics can be used in any where if we define the interface properly.
  2. It works for local execution but also can be used in any other ways as long as we can collect statistics for every part we need
  3. It only supports integer for now because I want to keep this PR simple. Let's do it step by step.

@JackyWoo
Copy link
Contributor

thanks

Copy link
Member

@CurtizJ CurtizJ left a comment

Choose a reason for hiding this comment

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

LGTM as a first step. Let's continue working on tasks from the backlog.

@hanfei1991
Copy link
Member Author

00002_log_and_exception_messages_formatting will be okay if we run normal stateless tests

@hanfei1991 hanfei1991 merged commit 4c0efb0 into ClickHouse:master Nov 29, 2023
@JackyWoo
Copy link
Contributor

JackyWoo commented Mar 11, 2024

@hanfei1991 I am going to add more statistic types. There is a question:

How to support multi types of statistics such as hyperloglog, cm-sketch in SQL?
solution 1

CREATE TABLE test (
    a Int64 STATISTIC type (tdigest, hyperloglog)
) ...

ALTER TABLE test ADD / DROP / CLEAR / MATERIALIZE STATISTIC a, b TYPE tdigest, hyperloglog

solution 2(Incompatible with the current version)

CREATE TABLE test (
    a Int64 STATISTIC type (tdigest, hyperloglog)
) ...

ALTER TABLE test ADD / DROP / CLEAR / MATERIALIZE STATISTIC a TYPE tdigest, hyperloglog, b TYPE tdigest, hyperloglog

solution 3

CREATE TABLE test (
    a Int64 STATISTIC type (tdigest, hyperloglog)
) ...

ALTER TABLE test ADD / DROP / CLEAR / MATERIALIZE STATISTIC a TYPE tdigest, hyperloglog
ALTER TABLE test ADD / DROP / CLEAR / MATERIALIZE STATISTIC b TYPE tdigest, hyperloglog

I prefer the 3td one, which do you prefer or you have a better one?

@hanfei1991
Copy link
Member Author

the 1st one is a amendment for the 3rd one @JackyWoo

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants