Skip to content

[backport branch-53] perf(optimizer): EliminateCrossJoin fast-path for join-free plans (#22612)#59

Merged
zhuqi-lucas merged 1 commit into
branch-53from
qizhu/eliminate-cross-join-fastpath-branch-53
Jun 3, 2026
Merged

[backport branch-53] perf(optimizer): EliminateCrossJoin fast-path for join-free plans (#22612)#59
zhuqi-lucas merged 1 commit into
branch-53from
qizhu/eliminate-cross-join-fastpath-branch-53

Conversation

@zhuqi-lucas

Copy link
Copy Markdown
Collaborator

Summary

Backport of upstream apache#22612 (merged 2026-06-02) to branch-53.

EliminateCrossJoin::rewrite walks the full logical plan (clone-on-write map_children + recompute_schema) even when the tree contains no Join. This adds a read-only plan_has_joins scan up front and returns Transformed::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

Cherry-pick

Clean cherry-pick of upstream commit 5c92390921d8d667aa7cb7d56276a59ba36926f4 onto origin/branch-53 (auto-merge, no conflicts).

Affects

  • branch-53 only. atlas will pick this up via the next branch-53 rev bump in polygon/rust-app-atlas (separate PR). No prod impact until that lands.

Linear

X-2875

…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.
Copilot AI review requested due to automatic review settings June 3, 2026 06:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_joins pre-scan in EliminateCrossJoin::rewrite to short-circuit when the plan contains no Join nodes (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.

Comment thread datafusion/optimizer/src/eliminate_cross_join.rs
@zhuqi-lucas zhuqi-lucas merged commit ddb06d5 into branch-53 Jun 3, 2026
58 of 59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants