Skip to content

Disjunctions pushdown for JOINs - #84735

Merged
yariks5s merged 58 commits into
masterfrom
yarik/disjunction-push-down
Sep 24, 2025
Merged

yariks5s merged 58 commits into
masterfrom
yarik/disjunction-push-down

Conversation

@yariks5s

@yariks5s yariks5s commented Jul 30, 2025 •

Copy link
Copy Markdown
Member

A new logic, which pushes safe OR-branches of filters above a JOIN down to the left/right inputs so storages can pre-filter earlier. It works when every top-level OR branch contributes at least one deterministic predicate for the target side (e.g., (t1.k IN (1,2) AND t2.x=100) OR (t1.k IN (3,4) AND t2.x=200) pushes t1.k IN (1,2,3,4) to the left and t2.x IN (100,200) to the right). It does not apply if some branch lacks a predicate for that side (e.g., (t1.a=1 AND t2.b=2) OR (t1.a=3) can’t push to t2) or for non-AND/OR forms, and the original filter remains to preserve results. Used under the use_join_disjunctions_push_down setting.

Changelog category (leave one):

  • Performance Improvement

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

Provides a logic regarding pushing down the disjunction JOIN predicates. Example: in TPC-H Q7 for a condition on 2 tables n1 and n2 like (n1.n_name = 'FRANCE' AND n2.n_name = 'GERMANY') OR (n1.n_name = 'GERMANY' AND n2.n_name = 'FRANCE') we extract separate partial filters for each table n1.n_name = 'FRANCE' OR n1.n_name = 'GERMANY' for n1 and n2.n_name = 'GERMANY' OR n2.n_name = 'FRANCE' for n2.

@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9055c78]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_asan, old analyzer, 1/6) failure
test_storage_delta/test.py::test_replicated_database_and_unavailable_s3[1] FAIL
test_storage_delta/test.py::test_replicated_database_and_unavailable_s3[0] FAIL
Integration tests (amd_binary, 1/5) failure
test_storage_delta/test.py::test_replicated_database_and_unavailable_s3[1] FAIL
test_storage_delta/test.py::test_replicated_database_and_unavailable_s3[0] FAIL
Integration tests (arm_binary, distributed plan, 1/4) failure
test_storage_delta/test.py::test_replicated_database_and_unavailable_s3[0] FAIL
test_storage_delta/test.py::test_replicated_database_and_unavailable_s3[1] FAIL
Integration tests (amd_tsan, 1/6) failure
test_storage_delta/test.py::test_replicated_database_and_unavailable_s3[0] FAIL
Integration tests (amd_tsan, 3/6) failure
test_restore_db_replica/test.py::test_query_after_restore_db_replica[alter table-no exists table-with restart] FAIL

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 30, 2025
@yariks5s yariks5s changed the title Disjunctions pushdown in JOINS Disjunctions pushdown for JOINs Jul 30, 2025
@rschu1ze

This comment was marked as resolved.

@davenger davenger left a comment

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.

Please remove these debug prints

@yariks5s
yariks5s added this pull request to the merge queue Sep 24, 2025
Merged via the queue into master with commit 4368c53 Sep 24, 2025
@yariks5s
yariks5s deleted the yarik/disjunction-push-down branch September 24, 2025 15:22
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 24, 2025
@rschu1ze

Copy link
Copy Markdown
Member

@yariks5s Can we add the new setting in the SettingsRandomizer in clickhouse-test, so it at least runs on our tests? We should try to enable it in the mid run, otherwise it will remain dead code (from a user perspective).

@yariks5s

yariks5s commented Sep 24, 2025 •

Copy link
Copy Markdown
Member Author

@rschu1ze We definitely can because this setting should not change the result of queries, but we need to isolate this setting from the tests that perform DESCRIBE queries.

I will do it in the follow-up PR, thank you.

@yariks5s

Copy link
Copy Markdown
Member Author

PR for reference: #87697

Comment on lines +197 to +201
new_filter_node.step = std::make_unique<FilterStep>(
input_header,
std::move(filter_dag),
filter_column_name,
true);

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.

Here we have to make sure the order of outputs is the same as before, because in JoinStepLogical we have the assumption that the order of inputs in the dag are following the order of inputs in the input headers. Furthermore, int the test you added this optimization changes the order in the output header, thus the input header of the join step is different than the filter step, which is problematic.

alexey-milovidov added a commit that referenced this pull request Feb 15, 2026
The `onlyDependsOnAvailableColumns` function in the partial predicate
push-down for JOINs did not check for `ARRAY_JOIN` nodes. This caused
predicates containing `arrayJoin` to be incorrectly pushed down below
a JOIN as a partial (speculative) filter, while the original filter
above the JOIN was kept. The `arrayJoin` therefore executed twice --
once in the pushed-down filter and once in the original filter --
producing duplicate rows.

The fix rejects any subgraph containing `ARRAY_JOIN` from being
considered for partial predicate push-down.

The bug was introduced in #84735 and exposed when the optimization
was enabled by default in #89313.

Closes #96398

Co-Authored-By: Claude Opus 4.6 <[email protected]>
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 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.

5 participants