Skip to content

Commit 9029ff1

Browse files
authored
refactor: merge CoalesceAsyncExecInput into CoalesceBatches (#18540)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #18155. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Merges the functionality of `CoalesceAsyncExecInput` into `CoalesceBatches` to remove redundant optimizer logic and simplify batch coalescing behavior. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Behavior is covered by existing ``CoalesceBatches and optimizer tests. ## Are there any user-facing changes? No <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 552dbe4 commit 9029ff1

File tree

5 files changed

+19
-81
lines changed

5 files changed

+19
-81
lines changed

datafusion/physical-optimizer/src/coalesce_async_exec_input.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.

datafusion/physical-optimizer/src/coalesce_batches.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ use crate::PhysicalOptimizerRule;
2222

2323
use std::sync::Arc;
2424

25-
use datafusion_common::config::ConfigOptions;
2625
use datafusion_common::error::Result;
26+
use datafusion_common::{config::ConfigOptions, internal_err};
2727
use datafusion_physical_expr::Partitioning;
2828
use datafusion_physical_plan::{
29-
coalesce_batches::CoalesceBatchesExec, filter::FilterExec, joins::HashJoinExec,
30-
repartition::RepartitionExec, ExecutionPlan,
29+
async_func::AsyncFuncExec, coalesce_batches::CoalesceBatchesExec, filter::FilterExec,
30+
joins::HashJoinExec, repartition::RepartitionExec, ExecutionPlan,
3131
};
3232

3333
use datafusion_common::tree_node::{Transformed, TransformedResult, TreeNode};
@@ -72,11 +72,27 @@ impl PhysicalOptimizerRule for CoalesceBatches {
7272
)
7373
})
7474
.unwrap_or(false);
75+
7576
if wrap_in_coalesce {
7677
Ok(Transformed::yes(Arc::new(CoalesceBatchesExec::new(
7778
plan,
7879
target_batch_size,
7980
))))
81+
} else if let Some(async_exec) = plan_any.downcast_ref::<AsyncFuncExec>() {
82+
// Coalesce inputs to async functions to reduce number of async function invocations
83+
let children = async_exec.children();
84+
if children.len() != 1 {
85+
return internal_err!(
86+
"Expected AsyncFuncExec to have exactly one child"
87+
);
88+
}
89+
90+
let coalesce_exec = Arc::new(CoalesceBatchesExec::new(
91+
Arc::clone(children[0]),
92+
target_batch_size,
93+
));
94+
let new_plan = plan.with_new_children(vec![coalesce_exec])?;
95+
Ok(Transformed::yes(new_plan))
8096
} else {
8197
Ok(Transformed::no(plan))
8298
}

datafusion/physical-optimizer/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#![cfg_attr(test, allow(clippy::needless_pass_by_value))]
2929

3030
pub mod aggregate_statistics;
31-
pub mod coalesce_async_exec_input;
3231
pub mod coalesce_batches;
3332
pub mod combine_partial_final_agg;
3433
pub mod enforce_distribution;

datafusion/physical-optimizer/src/optimizer.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use crate::sanity_checker::SanityCheckPlan;
3636
use crate::topk_aggregation::TopKAggregation;
3737
use crate::update_aggr_exprs::OptimizeAggregateOrder;
3838

39-
use crate::coalesce_async_exec_input::CoalesceAsyncExecInput;
4039
use crate::limit_pushdown_past_window::LimitPushPastWindows;
4140
use datafusion_common::config::ConfigOptions;
4241
use datafusion_common::Result;
@@ -123,7 +122,6 @@ impl PhysicalOptimizer {
123122
// The CoalesceBatches rule will not influence the distribution and ordering of the
124123
// whole plan tree. Therefore, to avoid influencing other rules, it should run last.
125124
Arc::new(CoalesceBatches::new()),
126-
Arc::new(CoalesceAsyncExecInput::new()),
127125
// Remove the ancillary output requirement operator since we are done with the planning
128126
// phase.
129127
Arc::new(OutputRequirements::new_remove_mode()),

datafusion/sqllogictest/test_files/explain.slt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ physical_plan after EnforceSorting SAME TEXT AS ABOVE
238238
physical_plan after OptimizeAggregateOrder SAME TEXT AS ABOVE
239239
physical_plan after ProjectionPushdown SAME TEXT AS ABOVE
240240
physical_plan after coalesce_batches SAME TEXT AS ABOVE
241-
physical_plan after coalesce_async_exec_input SAME TEXT AS ABOVE
242241
physical_plan after OutputRequirements DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/example.csv]]}, projection=[a, b, c], file_type=csv, has_header=true
243242
physical_plan after LimitAggregation SAME TEXT AS ABOVE
244243
physical_plan after LimitPushPastWindows SAME TEXT AS ABOVE
@@ -317,7 +316,6 @@ physical_plan after EnforceSorting SAME TEXT AS ABOVE
317316
physical_plan after OptimizeAggregateOrder SAME TEXT AS ABOVE
318317
physical_plan after ProjectionPushdown SAME TEXT AS ABOVE
319318
physical_plan after coalesce_batches SAME TEXT AS ABOVE
320-
physical_plan after coalesce_async_exec_input SAME TEXT AS ABOVE
321319
physical_plan after OutputRequirements
322320
01)GlobalLimitExec: skip=0, fetch=10, statistics=[Rows=Exact(8), Bytes=Exact(671), [(Col[0]:),(Col[1]:),(Col[2]:),(Col[3]:),(Col[4]:),(Col[5]:),(Col[6]:),(Col[7]:),(Col[8]:),(Col[9]:),(Col[10]:)]]
323321
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/parquet-testing/data/alltypes_plain.parquet]]}, projection=[id, bool_col, tinyint_col, smallint_col, int_col, bigint_col, float_col, double_col, date_string_col, string_col, timestamp_col], limit=10, file_type=parquet, statistics=[Rows=Exact(8), Bytes=Exact(671), [(Col[0]:),(Col[1]:),(Col[2]:),(Col[3]:),(Col[4]:),(Col[5]:),(Col[6]:),(Col[7]:),(Col[8]:),(Col[9]:),(Col[10]:)]]
@@ -362,7 +360,6 @@ physical_plan after EnforceSorting SAME TEXT AS ABOVE
362360
physical_plan after OptimizeAggregateOrder SAME TEXT AS ABOVE
363361
physical_plan after ProjectionPushdown SAME TEXT AS ABOVE
364362
physical_plan after coalesce_batches SAME TEXT AS ABOVE
365-
physical_plan after coalesce_async_exec_input SAME TEXT AS ABOVE
366363
physical_plan after OutputRequirements
367364
01)GlobalLimitExec: skip=0, fetch=10
368365
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/parquet-testing/data/alltypes_plain.parquet]]}, projection=[id, bool_col, tinyint_col, smallint_col, int_col, bigint_col, float_col, double_col, date_string_col, string_col, timestamp_col], limit=10, file_type=parquet
@@ -602,7 +599,6 @@ physical_plan after EnforceSorting SAME TEXT AS ABOVE
602599
physical_plan after OptimizeAggregateOrder SAME TEXT AS ABOVE
603600
physical_plan after ProjectionPushdown SAME TEXT AS ABOVE
604601
physical_plan after coalesce_batches SAME TEXT AS ABOVE
605-
physical_plan after coalesce_async_exec_input SAME TEXT AS ABOVE
606602
physical_plan after OutputRequirements DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/example.csv]]}, projection=[a, b, c], file_type=csv, has_header=true
607603
physical_plan after LimitAggregation SAME TEXT AS ABOVE
608604
physical_plan after LimitPushPastWindows SAME TEXT AS ABOVE

0 commit comments

Comments
 (0)