Skip to content

MergeTree FINAL to not compare rows from same non-L0 part - #58142

Merged
robot-ch-test-poll3 merged 5 commits into
ClickHouse:masterfrom
canhld94:final_less_compare
Dec 29, 2023
Merged

robot-ch-test-poll3 merged 5 commits into
ClickHouse:masterfrom
canhld94:final_less_compare

Conversation

@canhld94

@canhld94 canhld94 commented Dec 22, 2023 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Performance Improvement

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

  • MergeTree FINAL to not compare rows from same non-L0 part

Currently, merging FINAL comparing current row to next row in sorting queue to check if the key has changed. It means total number of comparisons == number of read rows.
However, if two rows are from same non-L0 part, their keys must be distinct, and we can skip comparing them. This optimisation can significantly reduce number of comparisons in some case, e.g. we read from one big part and and some other small parts.

It means to complement #54366 but I create a new PR because this optimisation will apply to any non-ordinary merge tree.
It can also complement #58120 in case where parts have intersect ranges but almost no intersect rows.

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/

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Dec 22, 2023
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-performance Pull request with some performance improvements label Dec 22, 2023
@robot-clickhouse-ci-2

robot-clickhouse-ci-2 commented Dec 22, 2023 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit 91a87d6 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
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ 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 checkThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ 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. 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✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ 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

@UnamedRus

Copy link
Copy Markdown
Contributor

However, if two rows are from same non-L0 part, their keys must be distinct, and we can skip comparing them. This optimisation can significantly reduce number of comparisons in some case, e.g. we read from one big part and and some other small parts.

It should be under setting, because it's possible to get some parts with _0 level with duplicates in ReplacingMergeTree (ATTACH PART, insert with optimize_on_insert=0)

@canhld94

Copy link
Copy Markdown
Contributor Author

@UnamedRus By non-L0 parts I mean this optimization only applies to streams with part level > 0

@UnamedRus

Copy link
Copy Markdown
Contributor

My mistake then.

@jorisgio

Copy link
Copy Markdown
Contributor

Funnily i made the same mistake while reading @canhld94 proposal but i think the assumption indeeds holds for level > 0, as there is no other source for level increase than merge as far as i know. But maybe we miss some corner cases.

@UnamedRus

Copy link
Copy Markdown
Contributor

We have another optimization, which rely on that assumption for level > 0 #19375,
And it seems there wasn't a lot of complaint about it.

@UnamedRus UnamedRus mentioned this pull request Dec 22, 2023
1 task
Comment thread src/Processors/Chunk.cpp Resolved
Comment thread src/Processors/Chunk.h Resolved
Comment thread src/Processors/Chunk.cpp Resolved
size_t num_columns = 0;
UInt64 row_num = 0;

int source_stream_index = -1;

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.

std::optional?

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.

Yes, I think we can just use UInt64, because when hasEqualSortColumnsWith is called the object surely has been initialised.

@alexey-milovidov alexey-milovidov 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.

The idea is good, but a few changes are needed.

@alexey-milovidov alexey-milovidov self-assigned this Dec 27, 2023
Comment thread src/Processors/Chunk.h Resolved
Comment thread src/Processors/Chunk.h Resolved
Comment thread src/Processors/Chunk.h Resolved
Signed-off-by: Duc Canh Le <[email protected]>
@canhld94

Copy link
Copy Markdown
Contributor Author

I update the implementation, for each chunk IMergeTreeAlgorithm will only calculate the part level once.

PedroTadim pushed a commit to lazerg/ClickHouse that referenced this pull request Nov 17, 2026
…te row of one key from a merged part

`IMergingAlgorithmWithSharedChunks::rowsHaveDifferentSortColumns` skipped the
key comparison for two rows of one source whose part level is non-zero
(added by ClickHouse#58142 in 24.1), on the premise that a merged part holds one row per
sort key. That holds for ReplacingMergeTree, but a CollapsingMergeTree merge
keeps the first cancel row and the last state row of a key when their counts
are equal and the last row is a state row. When such a part was the oldest
source of a key in a later merge or FINAL, the pair was split into two key
groups: the group head (the cancel row) was collapsed on its own. A merge then
kept the stale cancel row next to the newer state row (`sum(sign)` 1 -> 0,
written to disk), and FINAL could return a row of an object whose rows all
cancel.

The shortcut is now opt-in per algorithm: `ReplacingSortedAlgorithm` sets
`nonzero_level_parts_have_unique_keys`, so `CollapsingSortedAlgorithm` compares
the sort columns of every pair of rows again (the 23.8 behaviour) in merges,
vertical merges and FINAL. Summing, Aggregating and Coalescing use
`IMergingAlgorithmWithDelayedChunk` and keep one row per key, so their
shortcut is unchanged; VersionedCollapsing compares keys directly.

Parts already written by affected versions are not repaired.

Closes ClickHouse#120649

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants