Skip to content

Implement Query Condition Cache#69236

Merged
alexey-milovidov merged 66 commits intoClickHouse:masterfrom
zhongyuankai:query_condition_cache
Mar 3, 2025
Merged

Implement Query Condition Cache#69236
alexey-milovidov merged 66 commits intoClickHouse:masterfrom
zhongyuankai:query_condition_cache

Conversation

@zhongyuankai
Copy link
Copy Markdown
Contributor

@zhongyuankai zhongyuankai commented Sep 4, 2024

Implement query condition cache to improve query performance using repeated conditions. The range of the portion of data that does not meet the condition is remembered as a temporary index in memory. Subsequent queries will use this index. close #67768

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

@tavplubix tavplubix added the can be tested Allows running workflows for external contributors label Sep 6, 2024
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-performance Pull request with some performance improvements label Sep 6, 2024
@robot-ch-test-poll2
Copy link
Copy Markdown
Contributor

robot-ch-test-poll2 commented Sep 6, 2024

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

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

Check nameDescriptionStatus
Integration testsThe integration tests report. In parenthesis the package type is given, and 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❌ failure
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure
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
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns ClickBench with instant-attach table✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe 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
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. Integration 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
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
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

@nickitat nickitat self-assigned this Nov 21, 2024
@nickitat
Copy link
Copy Markdown
Member

pls merge with master and let's run CI with use_query_condition_cache=true to make sure it doesn't cause any issues

@zhongyuankai
Copy link
Copy Markdown
Contributor Author

@nickitat use_query_condition_cache=true will cause some tests to test the skip index to fail, https://s3.amazonaws.com/clickhouse-test-reports/69236/7858f72d3abefbfaf301f16e86b8ceaea4561c5d/fast_test.html, it seems normal, I set it to false, set to true if necessary.

@nickitat
Copy link
Copy Markdown
Member

nickitat commented Nov 25, 2024

@alexey-milovidov alexey-milovidov added this pull request to the merge queue Mar 3, 2025
@rschu1ze rschu1ze removed this pull request from the merge queue due to a manual request Mar 3, 2025
@rschu1ze
Copy link
Copy Markdown
Member

rschu1ze commented Mar 3, 2025

Sorry for removing this PR from the merge queue.

@zhongyuankai Could you please take another look at my comments from 28 Nov? I suppose a less fragile implementation needs to be a lot more encapsulated (i.e. restricted to ReadFromMergeTree) + without optimizer pass.

@alexey-milovidov alexey-milovidov added this pull request to the merge queue Mar 3, 2025
@alexey-milovidov
Copy link
Copy Markdown
Member

alexey-milovidov commented Mar 3, 2025

@rschu1ze, you didn't review this pull request since November, so I had to pick up.

Merged via the queue into ClickHouse:master with commit 0066dbb Mar 3, 2025
124 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 3, 2025
baibaichen added a commit to Kyligence/gluten that referenced this pull request Mar 4, 2025
@zhongyuankai
Copy link
Copy Markdown
Contributor Author

@zhongyuankai Could you please take another look at my comments from 28 Nov? I suppose a less fragile implementation needs to be a lot more encapsulated (i.e. restricted to ReadFromMergeTree) + without optimizer pass.

@rschu1ze OK, I've thought about this question, but it's a bit difficult to implement, I'll read the relevant code and if it works I'll resubmit a pull request, If you have more detailed ideas please let me know, I am willing to implement it.

baibaichen added a commit to Kyligence/gluten that referenced this pull request Mar 4, 2025
baibaichen added a commit to apache/gluten that referenced this pull request Mar 4, 2025
* [GLUTEN-1632][CH]Daily Update Clickhouse Version (20250304)

* Fix ut due to ClickHouse/ClickHouse#69236

---------

Co-authored-by: kyligence-git <gluten@kyligence.io>
Co-authored-by: Chang Chen <baibaichen@gmail.com>
@rschu1ze
Copy link
Copy Markdown
Member

@zhongyuankai With latest master (that contains #77280 and #77293 on top of this PR), when I run

CREATE TABLE tab (a Int64, b Int64) ENGINE = MergeTree ORDER BY a;
INSERT INTO tab SELECT number, number FROM numbers(1000000);
SELECT count(*) FROM tab WHERE b = 10000 SETTINGS use_query_condition_cache = true;
SELECT count(*) FROM tab WHERE b = 10000 SETTINGS use_query_condition_cache = true;

then the second SELECT prints this into the log:

2025.03.10 21:36:53.014580 [ 1817141 ] {df969aa2-8f01-410a-8f70-7df204eeb740} <Debug> QueryConditionCache: Read entry for table_uuid: d9ef87b2-d813-4dab-ba90-b218172dd7f6, part: all_1_1_0, predicate_hash: 5456494897146899690, ranges: [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Marks with values = 0 can be skipped.

One would expect something like

[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Can you explain this behavior?

@zhongyuankai
Copy link
Copy Markdown
Contributor Author

then the second SELECT prints this into the log:

2025.03.10 21:36:53.014580 [ 1817141 ] {df969aa2-8f01-410a-8f70-7df204eeb740} <Debug> QueryConditionCache: Read entry for table_uuid: d9ef87b2-d813-4dab-ba90-b218172dd7f6, part: all_1_1_0, predicate_hash: 5456494897146899690, ranges: [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Marks with values = 0 can be skipped.

One would expect something like

[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Can you explain this behavior?

@rschu1ze The reason for this behavior is that a single read call in the MergeTreeRangeReader will read multiple marks in succession, resulting in a Chunk containing multiple marks, and in the FilterTransform, the query condition cache will be updated only if all the data in the Chunk is filtered.
If you want to expect it, you have to read only one mark at a time, but this will certainly lead to performance degradation.

@zhanglistar
Copy link
Copy Markdown
Contributor

@rschu1ze Will you update result of clickbench after including this PR? I mean, maybe result of hot run is very good.

@rschu1ze
Copy link
Copy Markdown
Member

rschu1ze commented Apr 7, 2025

@zhanglistar Happy to do so but we should first enable the cache by default (--> PR). Before doing this, it would be nice to merge this other PR for better debugging first.


if (const auto & prewhere_info = select_query_info.prewhere_info)
{
for (const auto * dag : prewhere_info->prewhere_actions.getOutputs())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about PrewhereInfo::row_level_filter? It does not look ok to ignore it. Some marks might have been skipped because of it and the following queries that have the same prewhere condition will skip those marks even if the row policy is no longer there.

I just ran into this problem in one of my PRs: #85118. The test file can be used as a repro by removing the cache=0. Bear in mind this repro works only with a build produced by that PR

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 pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache For Filters