Skip to content

perf: Use agg DistinctHandling in join optimization - #25385

Open
neilconway wants to merge 3 commits into
apache:mainfrom
neilconway:neilc/perf-semi-join-distinct
Open

neilconway wants to merge 3 commits into
apache:mainfrom
neilconway:neilc/perf-semi-join-distinct

Conversation

@neilconway

@neilconway neilconway commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

If we can prove that parts of a query are insensitive to duplicates, the optimizer apply various simplifications, like replacing inner joins with semi-joins and removing unused outer-join inputs.

The join analysis was previously conservative and assumed that all aggregate expressions are duplicate sensitive. Since #25288 added a framework for classifying how an aggregate treats duplicate values, we can now apply that framework to optimize joins more effectively.

What changes are included in this PR?

  • Extend EliminateJoin to recognize Aggregate plan nodes whose aggregate expressions all declare DistinctHandling::Insensitive
  • Guard duplicate-insensitivity propagation against volatile expressions and subqueries, to avoid changing query results
  • Refactor code to share the existing volatility/subquery check with UnionsToFilter.
  • Add tests

What is the testing strategy for this PR?

Existing tests pass; new tests added.

Are there any user-facing changes?

Some query plans might change (usually for the better).

@github-actions github-actions Bot added the optimizer Optimizer rules label Sep 16, 2026
@neilconway

Copy link
Copy Markdown
Contributor Author

FYI @mkleen @adriangb @jayzhan211

@codecov-commenter

codecov-commenter commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.10619% with 81 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.92%. Comparing base (b0b5471) to head (ab9e061).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/optimizer/src/eliminate_join.rs 73.24% 14 Missing and 66 partials ⚠️
datafusion/optimizer/src/utils.rs 97.29% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25385      +/-   ##
==========================================
- Coverage   81.93%   81.92%   -0.01%     
==========================================
  Files        1136     1136              
  Lines      428779   429449     +670     
  Branches   428779   429449     +670     
==========================================
+ Hits       351319   351836     +517     
- Misses      56446    56503      +57     
- Partials    21014    21110      +96     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions Bot added the sqllogictest SQL Logic Tests (.slt) label Sep 16, 2026
@github-actions github-actions Bot added the core Core DataFusion crate label Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants