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

Add a pass in Analyzer for time filter optimization with preimage #52091

Merged
merged 3 commits into from Aug 1, 2023

Conversation

ZhiguoZh
Copy link
Contributor

The preimage has been identified as the general solution to the
optimization of predicates with time converters (#15257). PR #50951
implemented this solution via the AST rewrite.

As a follow-up, this commit extends the optimization to the experi-
mental analyzer by replacing the sub-QueryTree of the time filter
with its preimage-transformed one. The optimization is implemented
as a new pass in the Analyzer.

Changelog category (leave one):

  • Performance Improvement

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

The performance experiments of SSB on the ICX device (Intel Xeon Platinum 8380 CPU, 80 cores, 160 threads) show that this change could bring an improvement of 8.5% to the geomean QPS when the experimental analyzer is enabled. The details are shown below:
image

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

robot-ch-test-poll commented Jul 14, 2023

This is an automated comment for commit 9ff409f with description of existing statuses. It's updated for the latest CI running
The full report is available here
The overall status of the commit is 🟢 success

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

@ZhiguoZh ZhiguoZh marked this pull request as draft July 19, 2023 13:52
@ZhiguoZh
Copy link
Contributor Author

A new issue is observed in the functional test 00515_enhanced_time_zones when the Analyzer is enabled by default. I've basically identified the root cause and will suspend the code review temporarily until the issue is fixed.

The preimage has been identified as the general solution to the
optimization of predicates with time converters (ClickHouse#15257). PR ClickHouse#50951
implemented this solution via the AST rewrite.

As a follow-up, this commit extends the optimization to the experi-
mental analyzer by replacing the sub-QueryTree of the time filter
with its preimage-transformed one. The optimization is implemented
as a new pass in the Analyzer.
@ZhiguoZh ZhiguoZh force-pushed the 20230713-analyzer-preimage branch 2 times, most recently from 2195b70 to 9ff409f Compare July 24, 2023 12:58
Date/Date32/DateTime/DateTime64 columns are required for arguments
of time converters, such as toYear and toYYYYMM.
@ZhiguoZh ZhiguoZh marked this pull request as ready for review July 24, 2023 14:20
@ZhiguoZh
Copy link
Contributor Author

ZhiguoZh commented Jul 25, 2023

Hi @hanfei1991, with the failed cases fixed, I think this PR is ready for the code review. This PR basically follows the implementation of the AST rewrite (#50951), and could you kindly help me review this one as well? Thanks a lot for your continuous help!

@hanfei1991 hanfei1991 self-assigned this Jul 25, 2023
@hanfei1991 hanfei1991 merged commit b25847e into ClickHouse:master Aug 1, 2023
272 checks passed
@ZhiguoZh
Copy link
Contributor Author

ZhiguoZh commented Aug 1, 2023

Thanks for your review and acceptance! @hanfei1991

hanfei1991 added a commit to hanfei1991/ClickHouse that referenced this pull request Aug 1, 2023
@hanfei1991 hanfei1991 mentioned this pull request Aug 1, 2023
1 task
tavplubix added a commit that referenced this pull request Aug 1, 2023
@novikd
Copy link
Member

novikd commented Aug 2, 2023

Why is this optimization not under the setting? There is no way to disable it. If it's a performance improvement why is there no new performance test?
Changing the predicate may prevent the application of read-in-order optimization.

Copy link
Member

@novikd novikd left a comment

Choose a reason for hiding this comment

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

The whole cpp file is written not like in the style guide. Very hard to read, especially because of huge amount of the copy-paste.

size_t literal_id = 1 - func_id;
const auto * literal = function->getArguments().getNodes()[literal_id]->as<ConstantNode>();

if (!literal || literal->getValue().getType() != Field::Types::UInt64) return;
Copy link
Member

Choose a reason for hiding this comment

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

Why is the field type used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The literal in a SQL query is parsed as one of UInt64, Int64, Float64, String, Null (as ParserLiteral described),
and in this case, where the literal is compared with results from toYear or toYYYYMM converters, we expect the type of the literal to be UInt64, and decide to return early elsewise.

Comment on lines +155 to +169
const auto lhs = std::make_shared<FunctionNode>("less");
lhs->getArguments().getNodes().push_back(std::make_shared<ColumnNode>(column_node.getColumn(), column_node.getColumnSource()));
lhs->getArguments().getNodes().push_back(std::make_shared<ConstantNode>(start_date_or_date_time));
resolveOrdinaryFunctionNode(*lhs, lhs->getFunctionName());

const auto rhs = std::make_shared<FunctionNode>("greaterOrEquals");
rhs->getArguments().getNodes().push_back(std::make_shared<ColumnNode>(column_node.getColumn(), column_node.getColumnSource()));
rhs->getArguments().getNodes().push_back(std::make_shared<ConstantNode>(end_date_or_date_time));
resolveOrdinaryFunctionNode(*rhs, rhs->getFunctionName());

const auto new_date_filter = std::make_shared<FunctionNode>("or");
new_date_filter->getArguments().getNodes() = {lhs, rhs};
resolveOrdinaryFunctionNode(*new_date_filter, new_date_filter->getFunctionName());

return new_date_filter;
Copy link
Member

Choose a reason for hiding this comment

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

Almost a complete copy-paste of equals case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing out this flaw! Maybe I should implement a utility function for constructing FunctionNode and have it build the subtrees of equals, notEquals, etc. instead, which would make the code neater.

@ZhiguoZh
Copy link
Contributor Author

ZhiguoZh commented Aug 4, 2023

Hi Dmitry,

I think it's required to add this optimization under the settings if it unintendedly breaks another optimization, and I will soon send out another PR for it. Meanwhile, I'd also like to explore if this preimage optimization could co-work with the read-in-order one.

And exactly, this optimization is supported more by the performance data of SSB than a CI performance test. I'm not sure if it is still necessary to add the performance test in this case, but I will try to add one if so.

The code style is also to be refined in the upcoming PR, where the code duplication problem will be further mitigated.

Thanks for your comments! @novikd

@Algunenano
Copy link
Member

The fuzzer is reporting a problem: https://s3.amazonaws.com/clickhouse-test-reports/0/f69bfa452633d2fcc2052a258f7202666053c787/ast_fuzzer__debug_.html

The comments from @novikid still stands by the way. The format of the file is bad, and there is no setting to disable this optimization. I'm having a look to see if it's something easy or I'll simply disable this completely until fixed

@ZhiguoZh
Copy link
Contributor Author

Extremely sorry for failing to make progress in this code refinement in the last months due to lots of urgent internal tasks. I will try to revise the code accordingly in the coming days to 1) fix the reported problem 2) refine the code style 3) add new setting to turn the optimization on optionally. But it's still OK if it needs to be disabled temporarily before I could completely resolve the known issues.

Thanks for your comments and picking up this thread.

@Algunenano
Copy link
Member

All 3 items should be covered by #60453

@ZhiguoZh
Copy link
Contributor Author

Thanks for the fixes from which I've learned a lot. And I also noticed that the implementation of generating FunctionNode is not as elegant as expected. I'm working on the code refactoring locally and plan to send it out for your review after your PR gets merged.

@ZhiguoZh
Copy link
Contributor Author

ZhiguoZh commented Mar 5, 2024

#60875 has been created for refactoring this part of code. Could you help me review it? Thanks in advance for your help!

@novikd @Algunenano

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