Skip to content

Enable merge filters optimization. - #71539

Merged
KochetovNicolai merged 16 commits into
masterfrom
enable-merge-filters-optimization
Nov 13, 2024
Merged

KochetovNicolai merged 16 commits into
masterfrom
enable-merge-filters-optimization

Conversation

@KochetovNicolai

@KochetovNicolai KochetovNicolai commented Nov 6, 2024 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Improve query_plan_merge_filters optimization. Fixes #71408

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/

CI Settings (Only check the boxes if you know what you are doing):

  • Allow: All Required Checks
  • Allow: Stateless tests
  • Allow: Stateful tests
  • Allow: Integration Tests
  • Allow: Performance tests
  • Allow: All Builds
  • Allow: batch 1, 2 for multi-batch jobs
  • Allow: batch 3, 4, 5, 6 for multi-batch jobs

  • Exclude: Style check
  • Exclude: Fast test
  • Exclude: All with ASAN
  • Exclude: All with TSAN, MSAN, UBSAN, Coverage
  • Exclude: All with aarch64, release, debug

  • Run only fuzzers related jobs (libFuzzer fuzzers, AST fuzzers, etc.)
  • Exclude: AST fuzzers

  • Do not test
  • Woolen Wolfdog
  • Upload binaries for special builds
  • Disable merge-commit
  • Disable CI cache

@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-improvement Pull request with some product improvements label Nov 6, 2024
@robot-clickhouse-ci-2

robot-clickhouse-ci-2 commented Nov 6, 2024 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit de41100 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
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](https://github.com/ClickHouse/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
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

@serxa serxa self-assigned this Nov 6, 2024
/// but contains values other than 0 and 1 (e.g. if it is (number%5) it contains 2,3,4)
/// 2. CAST the result to the exact type of the PREWHERE column from the original DAG
const auto & last_step_result_node_info = node_remap[steps.back().column_name];
//const auto & last_step_result_node_info = node_remap[steps.back().column_name];

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.

remove?

/// If PREWHERE has multiple conditions combined with AND, execute them in separate read/filtering steps.
bool enable_multiple_prewhere_read_steps = false;
/// In case of multiple prewhere steps, execute filtering earlier to support short-circuit properly.
bool force_shirt_circuit_execution = false;

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.

typo 👕 -> 🩳

@geldot

geldot commented Nov 12, 2024

Copy link
Copy Markdown
Contributor

@KochetovNicolai "shirt" still appears in some of the function param names, worth doing a grep

@KochetovNicolai
KochetovNicolai added this pull request to the merge queue Nov 13, 2024
Merged via the queue into master with commit 4d269bd Nov 13, 2024
@KochetovNicolai
KochetovNicolai deleted the enable-merge-filters-optimization branch November 13, 2024 12:32
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 13, 2024
@Algunenano Algunenano removed the pr-improvement Pull request with some product improvements label Nov 13, 2024
@Algunenano Algunenano added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 13, 2024
@Algunenano

Copy link
Copy Markdown
Member

Some bugs found by SQLancer. Disabling the setting by default again, but leaving the changes -> #71890

baibaichen added a commit to Kyligence/gluten that referenced this pull request Nov 14, 2024
Issue 1
BuildQueryPipelineSettings is created manually instead of calling  BuildQueryPipelineSettings::fromContext(); so even ClickHouse/ClickHouse#71890 disable 'query_plan_merge_filters', UTs are still failed.

To fix  this issue, we need set correct default parameters in CHUtil.cpp

Issue 2
If we set query_plan_merge_filters to true, then ClickHouse/ClickHouse#71539 will try to split the left most AND atom to a separate DAG and hence create FilterTransformer for each And atom, which cause collecting metrics failed.

I am not sure the benefits of setting it to true, let's keep it to false.

std::vector<ActionsAndName> and_atoms;
if (!actions_dag.hasStatefulFunctions())
and_atoms = splitAndChainIntoMultipleFilters(cloned_dag, filter_column_name);

@baibaichen baibaichen Nov 14, 2024 •

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.

Calling describeActions after building pipeline will failed at splitAndChainIntoMultipleFilters, due to action_dag is moved.

// ....
  auto expression = std::make_shared<ExpressionActions>(std::move(actions_dag), settings.getActionsSettings());
// ....

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is true for most other steps as well.
Afiak we really call explain after pipeline is build.
I was planning to make explain destructive as well; but it is not convenient while debugging.

baibaichen added a commit to baibaichen/gluten that referenced this pull request Nov 14, 2024
Issue 1
BuildQueryPipelineSettings is created manually instead of calling  BuildQueryPipelineSettings::fromContext(); so even ClickHouse/ClickHouse#71890 disable 'query_plan_merge_filters', UTs are still failed.

To fix  this issue, we need set correct default parameters in CHUtil.cpp

Issue 2
If we set query_plan_merge_filters to true, then ClickHouse/ClickHouse#71539 will try to split the left most AND atom to a separate DAG and hence create FilterTransformer for each And atom, which cause collecting metrics failed.

I am not sure the benefits of setting it to true, let's keep it to false.
baibaichen pushed a commit to apache/gluten that referenced this pull request Nov 14, 2024
* Fix issues due to ClickHouse/ClickHouse#71539.

Issue 1
BuildQueryPipelineSettings is created manually instead of calling  BuildQueryPipelineSettings::fromContext(); so even ClickHouse/ClickHouse#71890 disable 'query_plan_merge_filters', UTs are still failed.

To fix  this issue, we need set correct default parameters in CHUtil.cpp

Issue 2
If we set query_plan_merge_filters to true, then ClickHouse/ClickHouse#71539 will try to split the left most AND atom to a separate DAG and hence create FilterTransformer for each And atom, which cause collecting metrics failed.

I am not sure the benefits of setting it to true, let's keep it to false.

* Calling `QueryPlan::explainPlan` after building pipeline is not correct, due to `action_dag`  is [moved](https://github.com/ClickHouse/ClickHouse/blob/22d2c856a70dfb8b6e4c506fcb22ac03d59df9be/src/Processors/QueryPlan/FilterStep.cpp#L161).
OriginalToNewNodeMap & node_remap)
const DataTypePtr & to_type)
{
if (!node_to_cast.result_type->equals(*to_type))

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.

Should be if (node_to_cast.result_type->equals(*to_type)) (without negation) to fix test 03271_sqllancer_having_issue.sql? Didn't try, just a hunch.

Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Feb 22, 2025
…ers-optimization

Enable merge filters optimization.
Enmk added a commit to Altinity/ClickHouse that referenced this pull request Mar 13, 2025
…ge_filters_optimization

24.8 Backport of ClickHouse#71539 - Enable merge filters optimization.
alexey-milovidov pushed a commit to ylw510/ClickHouse that referenced this pull request Nov 2, 2026
…g later uses it

ClickHouse#71539 (8c2e541) replaced the by-name removal of intermediate filter columns with a check on
`Step::original_node`, but never assigned it. So every step but the last kept its condition result until the
end of the read, and each later step filtered it again.

Assign `original_node` for a single-condition step; the existing `node_to_step` / `all_outputs` check then
decides whether a later step or an output of the PREWHERE still uses it. The AND of several conditions of one
step is a node the steps create, nothing else references it, so it is always removed. A condition that is a
stored column stays: a later step may read it as the parent of a subcolumn missing in the part. Removing it
too returned `f.null = 1` for `SELECT f.null ... PREWHERE f AND x = 1` while `MODIFY COLUMN f Nullable(UInt8)`
is pending (0 with this change, as on master and with WHERE).

This also drops the condition columns of the split PREWHERE on `Memory` (ClickHouse#124498).

Tests: the pending-MODIFY query in `04628_multistep_prewhere_default_column`, and a step whose condition a
later step reuses in `02559_multiple_read_steps_in_prewhere_reuse_computation`. Both queries pass on master
as well; they fail if the stored-column or the later-use guard is dropped.

Found and analysed by @ snelheidai in ClickHouse#128432.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Nov 6, 2026
…ated on more rows

With `query_plan_merge_filters = 1` (the default since ClickHouse#71539) every PREWHERE
read step of a MergeTree read applied its filter to the whole block, to keep
short-circuit evaluation of merged filters. So a step that keeps almost all
rows still copied every column through its filter.

The forced filter is now skipped when the next step's conditions (and, before
the last step, the outputs computed in the last step) pass
`canBeMovedToTheFrontOfAndChain`, the reader neither converts nor fills a
PREWHERE column (`canReadPrewhereColumnsAhead`), and the last step filters.
The step filters are then combined and applied together by
`ReadResult::optimize`. `groups_requiring_filtered_input` is unchanged.

`system.predicate_statistics_log` now counts the rows that passed all steps so
far after `optimize`, so `passed_rows` and `total_passed_rows` stay the rows
surviving a step when an earlier filter was combined but not applied (also with
`query_plan_merge_filters = 0`).

Found, analysed and measured by @ snelheidai in ClickHouse#128442.

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

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

Analyzer does not push down filter condition and use index in View with subquery

8 participants