Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: integrate batch coalescer with repartition exec#19002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
6cca64e9b7f8ce0ee62efbde5cd44b4fd93File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -30,6 +30,7 @@ use super::metrics::{self, ExecutionPlanMetricsSet, MetricBuilder, MetricsSet}; | ||||||||||||||||||||||||||||||||||
| use super::{ | ||||||||||||||||||||||||||||||||||
| DisplayAs, ExecutionPlanProperties, RecordBatchStream, SendableRecordBatchStream, | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| use crate::coalesce::LimitedBatchCoalescer; | ||||||||||||||||||||||||||||||||||
| use crate::execution_plan::{CardinalityEffect, EvaluationType, SchedulingType}; | ||||||||||||||||||||||||||||||||||
| use crate::hash_utils::create_hashes; | ||||||||||||||||||||||||||||||||||
| use crate::metrics::{BaselineMetrics, SpillMetrics}; | ||||||||||||||||||||||||||||||||||
| @@ -62,7 +63,7 @@ use crate::filter_pushdown::{ | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| use datafusion_physical_expr_common::utils::evaluate_expressions_to_arrays; | ||||||||||||||||||||||||||||||||||
| use futures::stream::Stream; | ||||||||||||||||||||||||||||||||||
| use futures::{FutureExt, StreamExt, TryStreamExt}; | ||||||||||||||||||||||||||||||||||
| use futures::{ready, FutureExt, StreamExt, TryStreamExt}; | ||||||||||||||||||||||||||||||||||
| use log::trace; | ||||||||||||||||||||||||||||||||||
| use parking_lot::Mutex; | ||||||||||||||||||||||||||||||||||
| @@ -932,6 +933,7 @@ impl ExecutionPlan for RepartitionExec { | ||||||||||||||||||||||||||||||||||
| spill_stream, | ||||||||||||||||||||||||||||||||||
| 1, // Each receiver handles one input partition | ||||||||||||||||||||||||||||||||||
| BaselineMetrics::new(&metrics, partition), | ||||||||||||||||||||||||||||||||||
| None, // subsequent merge sort already does batching https://github.com/apache/datafusion/blob/e4dcf0c85611ad0bd291f03a8e03fe56d773eb16/datafusion/physical-plan/src/sorts/merge.rs#L286 | ||||||||||||||||||||||||||||||||||
| )) as SendableRecordBatchStream | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| .collect::<Vec<_>>(); | ||||||||||||||||||||||||||||||||||
| @@ -970,6 +972,7 @@ impl ExecutionPlan for RepartitionExec { | ||||||||||||||||||||||||||||||||||
| spill_stream, | ||||||||||||||||||||||||||||||||||
| num_input_partitions, | ||||||||||||||||||||||||||||||||||
| BaselineMetrics::new(&metrics, partition), | ||||||||||||||||||||||||||||||||||
| Some(context.session_config().batch_size()), | ||||||||||||||||||||||||||||||||||
| )) as SendableRecordBatchStream) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| @@ -1427,9 +1430,13 @@ struct PerPartitionStream { | ||||||||||||||||||||||||||||||||||
| /// Execution metrics | ||||||||||||||||||||||||||||||||||
| baseline_metrics: BaselineMetrics, | ||||||||||||||||||||||||||||||||||
| /// None for sort preserving variant (merge sort already does coalescing) | ||||||||||||||||||||||||||||||||||
| batch_coalescer: Option<LimitedBatchCoalescer>, | ||||||||||||||||||||||||||||||||||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend adding some context here about when this could be null -- something like
Suggested change
| ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| impl PerPartitionStream { | ||||||||||||||||||||||||||||||||||
| #[expect(clippy::too_many_arguments)] | ||||||||||||||||||||||||||||||||||
| fn new( | ||||||||||||||||||||||||||||||||||
| schema: SchemaRef, | ||||||||||||||||||||||||||||||||||
| receiver: DistributionReceiver<MaybeBatch>, | ||||||||||||||||||||||||||||||||||
| @@ -1438,7 +1445,10 @@ impl PerPartitionStream { | ||||||||||||||||||||||||||||||||||
| spill_stream: SendableRecordBatchStream, | ||||||||||||||||||||||||||||||||||
| num_input_partitions: usize, | ||||||||||||||||||||||||||||||||||
| baseline_metrics: BaselineMetrics, | ||||||||||||||||||||||||||||||||||
| batch_size: Option<usize>, | ||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||
| ifself.advance_cursors(stream_idx){ | |
| self.loser_tree_adjusted = false; | |
| self.in_progress.push_row(stream_idx); | |
| // stop sorting if fetch has been reached | |
| ifself.fetch_reached(){ | |
| self.done = true; | |
| }elseifself.in_progress.len() < self.batch_size{ | |
| continue; | |
| } | |
| } | |
| self.produced += self.in_progress.len(); | |
| returnPoll::Ready(self.in_progress.build_record_batch().transpose()); |
batch_coalescer and batch_size optional, e.g. only batch for non sort-preserving case. But please let me know if this makes sense to you. Thanks.
DandandanDec 15, 2025 •
edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we probably don't want to wrap the stream like this, as we will miss further optimization opportunities like apache/arrow-rs#8957
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you referring to maybe this method call
| let columns = take_arrays(batch.columns(),&indices,None)?; |
In the sort-preserving case, a BatchBuilder is used
| in_progress:BatchBuilder, |
push_row and build_record_batchdatafusion/datafusion/physical-plan/src/sorts/builder.rs
Lines 112 to 125 in fc88240
| pubfnbuild_record_batch(&mutself) -> Result<Option<RecordBatch>>{ | |
| ifself.is_empty(){ | |
| returnOk(None); | |
| } | |
| let columns = (0..self.schema.fields.len()) | |
| .map(|column_idx| { | |
| let arrays:Vec<_> = self | |
| .batches | |
| .iter() | |
| .map(|(_, batch)| batch.column(column_idx).as_ref()) | |
| .collect(); | |
| Ok(interleave(&arrays,&self.indices)?) | |
| }) |
interleave from arrow. Would this also be something to be replaced/improved with the optimization you mentioned, or that is different?Thanks!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see #18782 (comment) for my thoughts/reason on updating this test.