Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multithreading after window functions #50771

Merged

Conversation

frinkr
Copy link
Contributor

@frinkr frinkr commented Jun 9, 2023

Changelog category (leave one):

  • Performance Improvement

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

Add option query_plan_preserve_num_streams_after_window_functions to preserve the number of streams after evaluating window functions to allow parallel stream processing.

Detail

The WindowTransform needs to merge streams into one, but the new option will split the result from WindowTransform to multiple streams, which enables the parallel processing of down-streams functions/aggregates.

Benchmark

Test data

CREATE TABLE window_test(id Int64, value Int64, partition Int64, msg String) Engine=MergeTree ORDER BY id;

INSERT INTO window_test SELECT number, rand(1) % 500, number % 3000,randomPrintableASCII(2) FROM numbers(50000000);

Test queries

-- query0: sipHash64 over window
SELECT id, AVG(value) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS frame, sipHash64(frame) FROM window_test FORMAT Null;

-- query1: aggregate (by id) over window
SELECT id AS key, sipHash64(sum(frame)) AS value FROM (SELECT id, AVG(value) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS frame FROM window_test) GROUP BY key ORDER BY key, value FORMAT Null;

-- query2: aggregate (by id % 100000) over window
SELECT id % 100000 AS key, sipHash64(sum(frame)) AS value FROM (SELECT id, AVG(value) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS frame FROM window_test) GROUP BY key ORDER BY key, value FORMAT Null;

-- query3: aes enc/dec over window
WITH 'xxxxyyyyxxxxyyyyxxxxyyyyxxxxyyyy' AS cipherKey SELECT id, AVG(value) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS frame, toString(frame) AS str,encrypt('aes-256-ofb', str, cipherKey) AS enc, decrypt('aes-256-ofb', str, cipherKey) AS dec FROM window_test FORMAT Null;

-- query4: window with partitions
SELECT id, AVG(value) OVER (PARTITION by partition ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS frame FROM window_test ORDER BY id FORMAT Null;

-- query5: window with partitions
SELECT DISTINCT AVG(value) OVER (PARTITION by partition ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS frame FROM window_test ORDER BY frame FORMAT Null;

Test Env

  • CPU: AMD EPYC 7K62 8 Cores
  • Memory: 32G
  • Disk: 512G SSD
  • OS: Ubuntu 22.04.2 LTS

Test Result

Query OPTION Elapsed Memory RPS
0 OFF 3.07s 257.98M 16.27M
0 ON 2.38s 359.06M 20.99M
1 OFF 10.67s 463.51M 4.78M
1 ON 6.618s 592.35M 7.56M
2 OFF 3.20s 15.99M 15.62M
2 ON 2.52s 18.98M 19.78M
3 OFF 13.918s 164.49M 3.59M
3 ON 3.902s 554.46M 12.81M
4 OFF 9.602s 138.74M 5.21M
4 ON 5.404s 102.53M 9.25M
5 OFF 3.89s 20.17M 12.85M
5 ON 3.55s 88.29M 14.07M

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 9, 2023
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-performance Pull request with some performance improvements label Jun 9, 2023
@robot-ch-test-poll2
Copy link
Contributor

robot-ch-test-poll2 commented Jun 9, 2023

This is an automated comment for commit ac07b34 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
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
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
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
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
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
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure

@nickitat nickitat self-assigned this Jun 13, 2023
src/Core/Settings.h Outdated Show resolved Hide resolved
@nickitat
Copy link
Member

also would be good to add perf tests

@novikd novikd self-assigned this Jun 14, 2023
@nickitat
Copy link
Member

01568_window_functions_distributed - I suppose we need similar logic to what we have for aggregation, when remote nodes pass buckets to initiator in order through a single stream.

@nickitat
Copy link
Member

@frinkr a few tests left to fix. take a look pls

…between WindowTransforms (and WindowTransform works on single stream anyway).
…m:frinkr/ClickHouse into feat-preserve-num-streams-window-function
@frinkr
Copy link
Contributor Author

frinkr commented Jul 31, 2023

@nickitat I didn't see how the performance failures (zeros_mt, hasAll, etc.) related to the PR.

src/Planner/Planner.cpp Outdated Show resolved Hide resolved
@@ -60,6 +62,8 @@ WindowStep::WindowStep(

void WindowStep::transformPipeline(QueryPipelineBuilder & pipeline, const BuildQueryPipelineSettings &)
{
auto num_streams = pipeline.getNumThreads();
Copy link
Member

Choose a reason for hiding this comment

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

maybe getNumStreams()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

getNumStreams() return 1 if there is a ORDER BY in the window,

Copy link
Member

Choose a reason for hiding this comment

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

so, we don't preserve the number of stream, but rather always fan out after the WindowStep

Copy link
Contributor Author

Choose a reason for hiding this comment

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

exactly.

Copy link
Member

Choose a reason for hiding this comment

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

then I think we should name the setting accordingly

@nickitat
Copy link
Member

pls create a perf test with the queries you've provided in the description

@frinkr
Copy link
Contributor Author

frinkr commented Sep 28, 2023

@nickitat a new perf test 'window_functions_downstream_multithreading' was added, but the CI reports run errors. How do I know the error details? I run the perf with command performance-comparison/perf.py in my localhost, and no error. Maybe the test dataset is too large (50M rows)?

@ucasfl
Copy link
Collaborator

ucasfl commented Oct 8, 2023

LGTM. @nickitat How is it going on?

@ucasfl ucasfl requested a review from nickitat October 9, 2023 07:05
@nickitat
Copy link
Member

apart from the setting name lgtm

@frinkr
Copy link
Contributor Author

frinkr commented Oct 12, 2023

How about query_plan_enable_multithreading_after_window_functions? @nickitat could you propose a proper name, please?

@nickitat
Copy link
Member

How about query_plan_enable_multithreading_after_window_functions? @nickitat could you propose a proper name, please?

I'm ok with query_plan_enable_multithreading_after_window_functions

@frinkr
Copy link
Contributor Author

frinkr commented Oct 13, 2023

@nickitat the setting was renamed.

@alexey-milovidov
Copy link
Member

Ok, let's fix clang-tidy:

Oct 17 09:20:17 /build/src/Processors/QueryPlan/WindowStep.h:17:14: error: function 'DB::WindowStep::WindowStep' has a definition with different parameter names [readability-inconsistent-declaration-parameter-name,-warnings-as-errors]
Oct 17 09:20:17    17 |     explicit WindowStep(const DataStream & input_stream_,
Oct 17 09:20:17       |              ^
Oct 17 09:20:17 /build/src/Processors/QueryPlan/WindowStep.cpp:46:13: note: the definition seen here
Oct 17 09:20:17    46 | WindowStep::WindowStep(
Oct 17 09:20:17       |             ^
Oct 17 09:20:17 /build/src/Processors/QueryPlan/WindowStep.h:17:14: note: differing parameters are named here: ('streams_fan_out_'), in definition: ('preserve_num_streams_')
Oct 17 09:20:17    17 |     explicit WindowStep(const DataStream & input_stream_,
Oct 17 09:20:17       |              ^
Oct 17 09:20:17    18 |             const WindowDescription & window_description_,
Oct 17 09:20:17    19 |             const std::vector<WindowFunctionDescription> & window_functions_,
Oct 17 09:20:17    20 |             bool streams_fan_out_);
Oct 17 09:20:17       |                  ~~~~~~~~~~~~~~~~
Oct 17 09:20:17       |                  preserve_num_streams_

and it will be ready for merging.

@frinkr
Copy link
Contributor Author

frinkr commented Oct 25, 2023

@alexey-milovidov the clang-tidy was fixed.

@nickitat nickitat merged commit 18c50c1 into ClickHouse:master Oct 27, 2023
271 of 274 checks passed
baibaichen added a commit to Kyligence/gluten that referenced this pull request Oct 28, 2023
zzcclp pushed a commit to apache/incubator-gluten that referenced this pull request Oct 28, 2023
* [GLUTEN-1632][CH]Daily Update Clickhouse Version (20231028)

* fix build due to ClickHouse/ClickHouse#50771

---------

Co-authored-by: kyligence-git <gluten@kyligence.io>
Co-authored-by: Chang Chen <baibaichen@gmail.com>
@frinkr frinkr deleted the feat-preserve-num-streams-window-function branch November 1, 2023 13:19
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.

None yet

6 participants