[backport branch-53] perf(optimizer): EliminateCrossJoin fast-path for join-free plans (#22612)#59
Merged
zhuqi-lucas merged 1 commit intoJun 3, 2026
Conversation
…ache#22612) ## Which issue does this PR close? Closes apache#22583. ## Rationale for this change `EliminateCrossJoin::rewrite` is called on every plan during logical optimization. The rule's body only does real work when the root (or its `Filter` child) is an inner `Join`; in every other case it falls through to `rewrite_children`, which recurses into the plan, processes uncorrelated subqueries, and rewrites every direct child via `map_children` (clone-on-write), then calls `recompute_schema` on the way back. This is paid by every query in the logical optimizer pipeline — including simple point queries with no joins anywhere in the tree. ## Discussion on the issue @neilconway raised the valid concern that a fast-path scan still does *some* up-front work in the case where the rewrite does fire, and that the deeper fix is mutable tree rewrites (avoiding the clone-on-write of `TreeNode::rewrite` entirely). @alamb agreed and pointed at the in-place `map_children_mut` / `plan_has_subqueries` infrastructure adriangb landed in apache#22298 as the existing precedent. This PR follows that precedent directly: - **Same shape as `plan_has_subqueries`** — a read-only `apply` scan, early-stops on the first matching node, allocates nothing. - The scan cost on a query that *does* have joins is O(depth-to-first-join) — typically a handful of nodes, well below the cost of even one `map_children` clone-on-write the rewrite would otherwise do. - For the deeper "in-place mutable rewrite" direction, `rewrite_children` here recurses via `optimizer.rewrite(input, config)` per child — a different shape from `map_children_mut`'s `&mut` traversal. Adapting that is a larger refactor and worth its own follow-up; this PR doesn't block it. ## What changes are included in this PR? - New `plan_has_joins(&LogicalPlan) -> bool` helper in `eliminate_cross_join.rs` — `apply` walk that returns `true` on the first `LogicalPlan::Join` it sees. - Fast-path at the top of `EliminateCrossJoin::rewrite`: `if !plan_has_joins(&plan) { return Ok(Transformed::no(plan)); }`. Everything else is unchanged. ## Are these changes tested? Four new unit tests in the existing `mod tests`: - `plan_has_joins_detects_root_join` - `plan_has_joins_detects_nested_join` (Join under Filter/Projection) - `plan_has_joins_returns_false_for_join_free_plan` - `rewrite_short_circuits_when_plan_has_no_joins` — end-to-end: rule's `rewrite` returns `Transformed::no` and the plan comes back identical (schema + display) on join-free input. The existing 20 `EliminateCrossJoin` tests + the full 708-test `datafusion-optimizer --lib` suite still pass. `cargo clippy -p datafusion-optimizer --all-targets -- -D warnings` clean. ## Are there any user-facing changes? No semantic change. Pure perf optimization, no new config knobs. ## Follow-ups - A deeper architectural improvement (mutable tree rewrites following the `map_children_mut` pattern from apache#22298) is worth considering — see issue apache#22583 comments for discussion. Out of scope here.
There was a problem hiding this comment.
Pull request overview
Backports an optimizer performance improvement to branch-53 by adding a fast-path to EliminateCrossJoin so join-free logical plans skip the full recursive rewrite (including subquery traversal), reducing overhead in the logical optimizer pipeline.
Changes:
- Add a
plan_has_joinspre-scan inEliminateCrossJoin::rewriteto short-circuit when the plan contains noJoinnodes (including within embedded subqueries). - Introduce targeted unit tests covering join detection (root, nested, and inside subqueries) and the rewrite short-circuit behavior.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Backport of upstream apache#22612 (merged 2026-06-02) to branch-53.
EliminateCrossJoin::rewritewalks the full logical plan (clone-on-writemap_children+recompute_schema) even when the tree contains noJoin. This adds a read-onlyplan_has_joinsscan up front and returnsTransformed::no(plan)immediately when there are none. Identical shape to the entry-level fast-paths in upstream apache#22521 (physical ensure_distribution) and atlas's X-2823 (PruneViewMatchingBase) / X-2836 (ReverseOrder) optimizations.Why backport
Reference-cluster query servers run mostly no-join point queries. Spotted on the plan-time hot path in the X-2757 hot pod CPU profile. Pays its way on every query in that workload.
Validation on branch-53
eliminate_cross_jointests pass (including the new short-circuit +plan_has_joinscoverage from perf(optimizer): EliminateCrossJoin fast-path for join-free plans apache/datafusion#22612)datafusion-optimizer --libtests passcargo clippy -p datafusion-optimizer --all-targets -- -D warnings: cleancargo fmt --check: cleanCherry-pick
Clean cherry-pick of upstream commit
5c92390921d8d667aa7cb7d56276a59ba36926f4ontoorigin/branch-53(auto-merge, no conflicts).Affects
polygon/rust-app-atlas(separate PR). No prod impact until that lands.Linear
X-2875