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

Optimize array element function when input is array(map)/array(array(num)/array(array(string))/array(bigint)/array(decimal) #56403

Merged
merged 7 commits into from Jan 10, 2024

Conversation

taiyang-li
Copy link
Contributor

@taiyang-li taiyang-li commented Nov 7, 2023

Changelog category (leave one):

  • Performance Improvement

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

Optimize array element function when input is array(map)/array(array(num)/array(array(string))/array(bigint)/array(decimal). Current implementation causes too many reallocs. The optimization speed up by ~6x especially when input type is array(map)

@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-performance Pull request with some performance improvements label Nov 7, 2023
@robot-ch-test-poll1
Copy link
Contributor

robot-ch-test-poll1 commented Nov 7, 2023

This is an automated comment for commit 231de4a 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
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
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
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
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
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
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ error

@taiyang-li
Copy link
Contributor Author

taiyang-li commented Nov 7, 2023

Some tests

Optimize when input type is array(array(num))

Q: select materialize(array(array(1,2,3,4)))[1] from numbers(10000000) format Null;

Before:

0 rows in set. Elapsed: 0.551 sec. Processed 10.01 million rows, 80.06 MB (18.18 million rows/s., 145.41 MB/s.)
Peak memory usage: 4.26 MiB.
0 rows in set. Elapsed: 0.528 sec. Processed 10.01 million rows, 80.06 MB (18.95 million rows/s., 151.63 MB/s.)
Peak memory usage: 4.26 MiB.
0 rows in set. Elapsed: 0.566 sec. Processed 10.01 million rows, 80.06 MB (17.67 million rows/s., 141.35 MB/s.)
Peak memory usage: 4.25 MiB.

After

0 rows in set. Elapsed: 0.464 sec. Processed 10.01 million rows, 80.06 MB (21.55 million rows/s., 172.41 MB/s.)
Peak memory usage: 4.25 MiB.
0 rows in set. Elapsed: 0.455 sec. Processed 10.01 million rows, 80.06 MB (21.99 million rows/s., 175.93 MB/s.)
Peak memory usage: 4.25 MiB.
0 rows in set. Elapsed: 0.470 sec. Processed 10.01 million rows, 80.06 MB (21.28 million rows/s., 170.22 MB/s.)
Peak memory usage: 4.25 MiB

Optimize when input type is array(array(string))

Q: select materialize(array(array('hello', 'world')))[1] from numbers(10000000) format Null

Before:

0 rows in set. Elapsed: 0.824 sec. Processed 10.01 million rows, 80.06 MB (12.14 million rows/s., 97.16 MB/s.)
Peak memory usage: 12.07 MiB.
0 rows in set. Elapsed: 0.827 sec. Processed 10.01 million rows, 80.06 MB (12.10 million rows/s., 96.78 MB/s.)
Peak memory usage: 12.07 MiB.
0 rows in set. Elapsed: 0.842 sec. Processed 10.01 million rows, 80.06 MB (11.88 million rows/s., 95.06 MB/s.)
Peak memory usage: 12.07 MiB. 

After:

0 rows in set. Elapsed: 0.722 sec. Processed 10.01 million rows, 80.06 MB (13.87 million rows/s., 110.94 MB/s.)
Peak memory usage: 12.06 MiB.
0 rows in set. Elapsed: 0.712 sec. Processed 10.01 million rows, 80.06 MB (14.05 million rows/s., 112.37 MB/s.)
Peak memory usage: 12.06 MiB.
0 rows in set. Elapsed: 0.722 sec. Processed 10.01 million rows, 80.06 MB (13.86 million rows/s., 110.86 MB/s.)
Peak memory usage: 12.06 MiB.

Optimize when input type is map with its key/value type string/num

Q: select materialize(array(map('hello', 1, 'world', 2)))[1] from numbers(10000000) format Null;

Before:

0 rows in set. Elapsed: 6.504 sec. Processed 10.01 million rows, 80.06 MB (1.54 million rows/s., 12.31 MB/s.)
Peak memory usage: 12.58 MiB.
0 rows in set. Elapsed: 6.394 sec. Processed 10.01 million rows, 80.06 MB (1.57 million rows/s., 12.52 MB/s.)
Peak memory usage: 12.58 MiB.
0 rows in set. Elapsed: 6.398 sec. Processed 10.01 million rows, 80.06 MB (1.56 million rows/s., 12.51 MB/s.)
Peak memory usage: 12.58 MiB.

After:

0 rows in set. Elapsed: 1.029 sec. Processed 10.01 million rows, 80.06 MB (9.72 million rows/s., 77.79 MB/s.)
Peak memory usage: 12.95 MiB.
0 rows in set. Elapsed: 1.010 sec. Processed 10.01 million rows, 80.06 MB (9.91 million rows/s., 79.26 MB/s.)
Peak memory usage: 12.95 MiB.
0 rows in set. Elapsed: 1.032 sec. Processed 10.01 million rows, 80.06 MB (9.70 million rows/s., 77.58 MB/s.)
Peak memory usage: 12.95 MiB.

@taiyang-li taiyang-li marked this pull request as ready for review November 7, 2023 08:37
@taiyang-li taiyang-li changed the title Optimize array element function when input is array(map)/array(array(num)/array(array(string)) Optimize array element function when input is array(map)/array(array(num)/array(array(string))/array(bigint)/array(decimal) Nov 8, 2023
@taiyang-li
Copy link
Contributor Author

@alexey-milovidov can you help review it, thanks very much!

@taiyang-li
Copy link
Contributor Author

@Avogar can you help review it, thanks very much !

@alexey-milovidov
Copy link
Member

It looks very promising; I didn't have time to review it yet.

@taiyang-li
Copy link
Contributor Author

taiyang-li commented Dec 8, 2023

It looks very promising; I didn't have time to review it yet.

It doesn't matter. I'll wait for you..

@taiyang-li
Copy link
Contributor Author

Do we have any progress recently ?

1 similar comment
@taiyang-li
Copy link
Contributor Author

Do we have any progress recently ?

@Avogar Avogar self-assigned this Jan 2, 2024
@Avogar
Copy link
Member

Avogar commented Jan 2, 2024

I will review this PR this week. Sorry for long wait.

@Avogar
Copy link
Member

Avogar commented Jan 3, 2024

Actually, I think we can generalize new implementations a bit to support any nested arrays, not only 2 level arrays. We will just need to use a vector of nested and result offsets. Not sure if there will be an overhead for 2 level arrays because of iterating through vector with single element. WDYT?
Current code LGTM, only one small comment.

Also need to make new perf tests faster, they are too slow: https://s3.amazonaws.com/clickhouse-test-reports/56403/4bbc0db1540bae020fecc1f1d8ff3bcc4eb191c2/performance_comparison_[1_4]/report.html

@taiyang-li
Copy link
Contributor Author

Actually, I think we can generalize new implementations a bit to support any nested arrays, not only 2 level arrays. We will just need to use a vector of nested and result offsets. Not sure if there will be an overhead for 2 level arrays because of iterating through vector with single element. WDYT? Current code LGTM, only one small comment.

Also need to make new perf tests faster, they are too slow: https://s3.amazonaws.com/clickhouse-test-reports/56403/4bbc0db1540bae020fecc1f1d8ff3bcc4eb191c2/performance_comparison_[1_4]/report.html

I'll commit again, and refersh the perf tests(current result seems not precise, similar situations happended in other prs before). You thoughts about support any nested arrsys is interesting, but I'm consused about how to implement it, do you have any example.

@Avogar
Copy link
Member

Avogar commented Jan 8, 2024

You thoughts about support any nested arrsys is interesting, but I'm consused about how to implement it, do you have any example.

I started to write an explanation, but it turned out to be difficult to explain without just writing this code :)
Let's merge current implementation, it's also really good. I will try to implement optimization for arrays with any level by myself.

@Avogar
Copy link
Member

Avogar commented Jan 8, 2024

Perf test still complains about array_element tests, they are too long, we should make them run under 1 second

Co-authored-by: Kruglov Pavel <48961922+Avogar@users.noreply.github.com>
@taiyang-li
Copy link
Contributor Author

image

Co-authored-by: Kruglov Pavel <48961922+Avogar@users.noreply.github.com>
@taiyang-li
Copy link
Contributor Author

@Avogar I don't known why there are always two errors about parallel_index in perf tests. I'll merge master and observe it again.

@Avogar
Copy link
Member

Avogar commented Jan 10, 2024

Error in parallel_index - #58593

@Avogar Avogar merged commit 3fb1df2 into ClickHouse:master Jan 10, 2024
265 of 271 checks passed
@Avogar
Copy link
Member

Avogar commented Jan 11, 2024

I tried to implement my idea for any nested arrays, and it's not worth it, almost no improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-performance Pull request with some performance improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants