Skip to content

Commit 28a92a5

Browse files
committed
refactor: use existing with_new_children_if_necessary helper
Per reviewer suggestion in #22521, replace the inline Arc::ptr_eq + with_new_children branch with a call to the existing datafusion_physical_plan::with_new_children_if_necessary helper. Same behavior, smaller surface area, keeps the optimization in one canonical place so future call sites elsewhere in the optimizer pick it up automatically.
1 parent b5beced commit 28a92a5

1 file changed

Lines changed: 13 additions & 22 deletions

File tree

‎datafusion/physical-optimizer/src/ensure_requirements/enforce_distribution.rs‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ use datafusion_physical_plan::tree_node::PlanContext;
6565
use datafusion_physical_plan::union::{InterleaveExec, UnionExec, can_interleave};
6666
use datafusion_physical_plan::windows::WindowAggExec;
6767
use datafusion_physical_plan::windows::{BoundedWindowAggExec, get_best_fitting_window};
68-
use datafusion_physical_plan::{Distribution, ExecutionPlan, Partitioning};
68+
use datafusion_physical_plan::{
69+
Distribution, ExecutionPlan, Partitioning, with_new_children_if_necessary,
70+
};
6971

7072
use itertools::izip;
7173

@@ -1333,22 +1335,6 @@ pub fn ensure_distribution(
13331335
.map(|c| Arc::clone(&c.plan))
13341336
.collect::<Vec<_>>();
13351337

1336-
// Skip the (often expensive) `with_new_children` rebuild when none of
1337-
// the children were actually replaced above. For nodes like
1338-
// `ProjectionExec`, `with_new_children` calls `try_new` and recomputes
1339-
// schema / equivalence properties / output ordering even when the
1340-
// input Arcs are identical. Profiling on a representative deep
1341-
// ProjectionExec stack showed `with_new_children` dominating
1342-
// `ensure_distribution` time for plans where no distribution change
1343-
// applies (point queries with no join / aggregate / unmet ordering),
1344-
// so the rebuild is wasted on the common case.
1345-
let original_children = plan.children();
1346-
let children_unchanged = children_plans.len() == original_children.len()
1347-
&& children_plans
1348-
.iter()
1349-
.zip(original_children.iter())
1350-
.all(|(new, old)| Arc::ptr_eq(new, *old));
1351-
13521338
plan = if plan.is::<UnionExec>()
13531339
&& !config.optimizer.prefer_existing_union
13541340
&& can_interleave(children_plans.iter())
@@ -1377,12 +1363,17 @@ pub fn ensure_distribution(
13771363
// Repartition (hash):
13781364
// Data
13791365
Arc::new(InterleaveExec::try_new(children_plans)?)
1380-
} else if children_unchanged {
1381-
// Children are byte-identical Arcs as before; reuse the existing
1382-
// plan node and skip the schema/ordering recomputation.
1383-
plan
13841366
} else {
1385-
plan.with_new_children(children_plans)?
1367+
// Route through `with_new_children_if_necessary` so the common
1368+
// case where no child was replaced above skips the expensive
1369+
// `with_new_children` rebuild. For nodes like `ProjectionExec`,
1370+
// `with_new_children` recomputes schema / equivalence properties /
1371+
// output ordering via `try_new` even when the input Arcs are
1372+
// identical, which dominates `ensure_distribution` time on deep
1373+
// projection stacks over plans where no distribution change
1374+
// applies (point queries with no join / aggregate / unmet
1375+
// ordering).
1376+
with_new_children_if_necessary(plan, children_plans)?
13861377
};
13871378

13881379
Ok(Transformed::yes(DistributionContext::new(

0 commit comments

Comments
 (0)