Skip to content

Apache DataFusion SQL Query Engine

ARROW-10366: [Rust][DataFusion] Do not buffer intermediate results in merge or HashAggregate

Jorge C. Leitao905ddac28710
diff --git a/rust/datafusion/Cargo.toml b/rust/datafusion/Cargo.toml
index 380e04cf6b..e2df7e1697 100644
--- a/rust/datafusion/Cargo.toml
+++ b/rust/datafusion/Cargo.toml
@@ -56,6 +56,7 @@ num_cpus = "1.13.0"
chrono = "0.4"
async-trait = "0.1.41"
futures = "0.3"
+pin-project-lite= "^0.1.1"
tokio = { version = "0.2", features = ["macros", "rt-core", "rt-threaded"] }
[dev-dependencies]
diff --git a/rust/datafusion/src/physical_plan/hash_aggregate.rs b/rust/datafusion/src/physical_plan/hash_aggregate.rs
index 8d7beadb74..942a526867 100644
--- a/rust/datafusion/src/physical_plan/hash_aggregate.rs
+++ b/rust/datafusion/src/physical_plan/hash_aggregate.rs
@@ -21,15 +21,17 @@ use std::any::Any;
use std::sync::Arc;
use std::task::{Context, Poll};
-use futures::stream::{Stream, StreamExt, TryStreamExt};
-use futures::FutureExt;
+use futures::{
+ stream::{Stream, StreamExt},
+ Future,
+};
use crate::error::{DataFusionError, Result};
use crate::physical_plan::{Accumulator, AggregateExpr};
use crate::physical_plan::{Distribution, ExecutionPlan, Partitioning, PhysicalExpr};
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
-use arrow::error::Result as ArrowResult;
+use arrow::error::{ArrowError, Result as ArrowResult};
use arrow::record_batch::RecordBatch;
use arrow::{
array::{
@@ -38,6 +40,7 @@ use arrow::{
},
compute,
};
+use pin_project_lite::pin_project;
use fnv::FnvHashMap;
@@ -214,13 +217,13 @@ Example: average
* Once all N record batches arrive, `merge` is performed, which builds a RecordBatch with N rows and 2 columns.
* Finally, `get_value` returns an array with one entry computed from the state
*/
-struct GroupedHashAggregateStream {
- mode: AggregateMode,
- schema: SchemaRef,
- group_expr: Vec<Arc<dyn PhysicalExpr>>,
- aggr_expr: Vec<Arc<dyn AggregateExpr>>,
- input: SendableRecordBatchStream,
- finished: bool,
+pin_project! {
+ struct GroupedHashAggregateStream {
+ schema: SchemaRef,
+ #[pin]
+ output: futures::channel::oneshot::Receiver<ArrowResult<RecordBatch>>,
+ finished: bool,
+ }
}
fn group_aggregate_batch(
@@ -315,6 +318,41 @@ fn group_aggregate_batch(
Ok(accumulators)
}
+async fn compute_grouped_hash_aggregate(
+ mode: AggregateMode,
+ schema: SchemaRef,
+ group_expr: Vec<Arc<dyn PhysicalExpr>>,
+ aggr_expr: Vec<Arc<dyn AggregateExpr>>,
+ mut input: SendableRecordBatchStream,
+) -> ArrowResult<RecordBatch> {
+ // the expressions to evaluate the batch, one vec of expressions per aggregation
+ let aggregate_expressions = aggregate_expressions(&aggr_expr, &mode)
+ .map_err(DataFusionError::into_arrow_external_error)?;
+
+ // mapping key -> (set of accumulators, indices of the key in the batch)
+ // * the indexes are updated at each row
+ // * the accumulators are updated at the end of each batch
+ // * the indexes are `clear`ed at the end of each batch
+ //let mut accumulators: Accumulators = FnvHashMap::default();
+
+ // iterate over all input batches and update the accumulators
+ let mut accumulators = Accumulators::default();
+ while let Some(batch) = input.next().await {
+ let batch = batch?;
+ accumulators = group_aggregate_batch(
+ &mode,
+ &group_expr,
+ &aggr_expr,
+ batch,
+ accumulators,
+ &aggregate_expressions,
+ )
+ .map_err(DataFusionError::into_arrow_external_error)?;
+ }
+
+ create_batch_from_map(&mode, &accumulators, group_expr.len(), &schema)
+}
+
impl GroupedHashAggregateStream {
/// Create a new HashAggregateStream
pub fn new(
@@ -324,12 +362,24 @@ impl GroupedHashAggregateStream {
aggr_expr: Vec<Arc<dyn AggregateExpr>>,
input: SendableRecordBatchStream,
) -> Self {
+ let (tx, rx) = futures::channel::oneshot::channel();
+
+ let schema_clone = schema.clone();
+ tokio::spawn(async move {
+ let result = compute_grouped_hash_aggregate(
+ mode,
+ schema_clone,
+ group_expr,
+ aggr_expr,
+ input,
+ )
+ .await;
+ tx.send(result)
+ });
+
GroupedHashAggregateStream {
- mode,
schema,
- group_expr,
- aggr_expr,
- input,
+ output: rx,
finished: false,
}
}
@@ -342,63 +392,30 @@ impl Stream for GroupedHashAggregateStream {
type Item = ArrowResult<RecordBatch>;
fn poll_next(
- mut self: std::pin::Pin<&mut Self>,
+ self: std::pin::Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
if self.finished {
return Poll::Ready(None);
}
- // return single batch
- self.finished = true;
-
- let mode = self.mode.clone();
- let group_expr = self.group_expr.clone();
- let aggr_expr = self.aggr_expr.clone();
- let schema = self.schema.clone();
-
- // the expressions to evaluate the batch, one vec of expressions per aggregation
- let aggregate_expressions = match aggregate_expressions(&aggr_expr, &mode) {
- Ok(e) => e,
- Err(e) => {
- return Poll::Ready(Some(Err(
- DataFusionError::into_arrow_external_error(e),
- )))
- }
- };
-
- // mapping key -> (set of accumulators, indices of the key in the batch)
- // * the indexes are updated at each row
- // * the accumulators are updated at the end of each batch
- // * the indexes are `clear`ed at the end of each batch
- //let mut accumulators: Accumulators = FnvHashMap::default();
-
- // iterate over all input batches and update the accumulators
- let future = self.input.as_mut().try_fold(
- Accumulators::default(),
- |accumulators, batch| async {
- group_aggregate_batch(
- &mode,
- &group_expr,
- &aggr_expr,
- batch,
- accumulators,
- &aggregate_expressions,
- )
- .map_err(DataFusionError::into_arrow_external_error)
- },
- );
-
- let future = future.map(|maybe_accumulators| {
- maybe_accumulators.map(|accumulators| {
- create_batch_from_map(&mode, &accumulators, group_expr.len(), &schema)
- })?
- });
+ // is the output ready?
+ let this = self.project();
+ let output_poll = this.output.poll(cx);
- // send the stream to the heap, so that it outlives this function.
- let mut combined = Box::pin(future.into_stream());
+ match output_poll {
+ Poll::Ready(result) => {
+ *this.finished = true;
- combined.poll_next_unpin(cx)
+ // check for error in receiving channel and unwrap actual result
+ let result = match result {
+ Err(e) => Err(ArrowError::ExternalError(Box::new(e))), // error receiving
+ Ok(result) => result,
+ };
+ Poll::Ready(Some(result))
+ }
+ Poll::Pending => Poll::Pending,
+ }
}
}
@@ -462,12 +479,41 @@ fn aggregate_expressions(
}
}
-struct HashAggregateStream {
+pin_project! {
+ struct HashAggregateStream {
+ schema: SchemaRef,
+ #[pin]
+ output: futures::channel::oneshot::Receiver<ArrowResult<RecordBatch>>,
+ finished: bool,
+ }
+}
+
+async fn compute_hash_aggregate(
mode: AggregateMode,
schema: SchemaRef,
aggr_expr: Vec<Arc<dyn AggregateExpr>>,
- input: SendableRecordBatchStream,
- finished: bool,
+ mut input: SendableRecordBatchStream,
+) -> ArrowResult<RecordBatch> {
+ let mut accumulators = create_accumulators(&aggr_expr)
+ .map_err(DataFusionError::into_arrow_external_error)?;
+
+ let expressions = aggregate_expressions(&aggr_expr, &mode)
+ .map_err(DataFusionError::into_arrow_external_error)?;
+
+ let expressions = Arc::new(expressions);
+
+ // 1 for each batch, update / merge accumulators with the expressions' values
+ // future is ready when all batches are computed
+ while let Some(batch) = input.next().await {
+ let batch = batch?;
+ accumulators = aggregate_batch(&mode, &batch, accumulators, &expressions)
+ .map_err(DataFusionError::into_arrow_external_error)?;
+ }
+
+ // 2. convert values to a record batch
+ finalize_aggregation(&accumulators, &mode)
+ .map(|columns| RecordBatch::try_new(schema.clone(), columns))
+ .map_err(DataFusionError::into_arrow_external_error)?
}
impl HashAggregateStream {
@@ -478,11 +524,18 @@ impl HashAggregateStream {
aggr_expr: Vec<Arc<dyn AggregateExpr>>,
input: SendableRecordBatchStream,
) -> Self {
+ let (tx, rx) = futures::channel::oneshot::channel();
+
+ let schema_clone = schema.clone();
+ tokio::spawn(async move {
+ let result =
+ compute_hash_aggregate(mode, schema_clone, aggr_expr, input).await;
+ tx.send(result)
+ });
+
HashAggregateStream {
- mode,
schema,
- aggr_expr,
- input,
+ output: rx,
finished: false,
}
}
@@ -527,65 +580,31 @@ impl Stream for HashAggregateStream {
type Item = ArrowResult<RecordBatch>;
fn poll_next(
- mut self: std::pin::Pin<&mut Self>,
+ self: std::pin::Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
if self.finished {
return Poll::Ready(None);
}
- // return single batch
- self.finished = true;
+ // is the output ready?
+ let this = self.project();
+ let output_poll = this.output.poll(cx);
- let accumulators = match create_accumulators(&self.aggr_expr) {
- Ok(e) => e,
- Err(e) => {
- return Poll::Ready(Some(Err(
- DataFusionError::into_arrow_external_error(e),
- )))
- }
- };
-
- let expressions = match aggregate_expressions(&self.aggr_expr, &self.mode) {
- Ok(e) => e,
- Err(e) => {
- return Poll::Ready(Some(Err(
- DataFusionError::into_arrow_external_error(e),
- )))
- }
- };
- let expressions = Arc::new(expressions);
-
- let mode = self.mode;
- let schema = self.schema();
-
- // 1 for each batch, update / merge accumulators with the expressions' values
- // future is ready when all batches are computed
- let future = self
- .input
- .as_mut()
- .try_fold(
- // pass the expressions on every fold to handle closures' mutability
- (accumulators, expressions),
- |(acc, expr), batch| async move {
- aggregate_batch(&mode, &batch, acc, &expr)
- .map_err(DataFusionError::into_arrow_external_error)
- .map(|agg| (agg, expr))
- },
- )
- // pick the accumulators (disregard the expressions)
- .map(|e| e.map(|e| e.0));
-
- let future = future.map(|maybe_accumulators| {
- maybe_accumulators.map(|accumulators| {
- // 2. convert values to a record batch
- finalize_aggregation(&accumulators, &mode)
- .map_err(DataFusionError::into_arrow_external_error)
- .and_then(|columns| RecordBatch::try_new(schema.clone(), columns))
- })?
- });
+ match output_poll {
+ Poll::Ready(result) => {
+ *this.finished = true;
- Box::pin(future.into_stream()).poll_next_unpin(cx)
+ // check for error in receiving channel and unwrap actual result
+ let result = match result {
+ Err(e) => Err(ArrowError::ExternalError(Box::new(e))), // error receiving
+ Ok(result) => result,
+ };
+
+ Poll::Ready(Some(result))
+ }
+ Poll::Pending => Poll::Pending,
+ }
}
}
diff --git a/rust/datafusion/src/physical_plan/merge.rs b/rust/datafusion/src/physical_plan/merge.rs
index f50bfee92b..8c649b0d34 100644
--- a/rust/datafusion/src/physical_plan/merge.rs
+++ b/rust/datafusion/src/physical_plan/merge.rs
@@ -19,23 +19,28 @@
//! into a single partition
use std::any::Any;
-use std::iter::Iterator;
use std::sync::Arc;
-use futures::future;
+use futures::channel::mpsc;
+use futures::sink::SinkExt;
+use futures::stream::StreamExt;
+use futures::Stream;
-use super::common;
+use async_trait::async_trait;
+
+use arrow::record_batch::RecordBatch;
+use arrow::{
+ datatypes::SchemaRef,
+ error::{ArrowError, Result as ArrowResult},
+};
+
+use super::RecordBatchStream;
use crate::error::{DataFusionError, Result};
use crate::physical_plan::ExecutionPlan;
use crate::physical_plan::Partitioning;
-use arrow::record_batch::RecordBatch;
-use arrow::{datatypes::SchemaRef, error::ArrowError};
-
use super::SendableRecordBatchStream;
-
-use async_trait::async_trait;
-use tokio;
+use pin_project_lite::pin_project;
/// Merge execution plan executes partitions in parallel and combines them into a single
/// partition. No guarantees are made about the order of the resulting partition.
@@ -103,37 +108,72 @@ impl ExecutionPlan for MergeExec {
self.input.execute(0).await
}
_ => {
- let tasks = (0..input_partitions).map(|part_i| {
+ // use a stream that allows each sender to put in at
+ // least one result in an attempt to maximize
+ // parallelism.
+ let (sender, receiver) =
+ mpsc::channel::<ArrowResult<RecordBatch>>(input_partitions);
+
+ // spawn independent tasks whose resulting streams (of batches)
+ // are sent to the channel for consumption.
+ for part_i in (0..input_partitions) {
let input = self.input.clone();
+ let mut sender = sender.clone();
tokio::spawn(async move {
- let stream = input.execute(part_i).await?;
- common::collect(stream).await
- })
- });
-
- let results = future::try_join_all(tasks)
- .await
- .map_err(|e| ArrowError::from_external_error(Box::new(e)))?;
-
- let combined_results = results
- .into_iter()
- .try_fold(Vec::<RecordBatch>::new(), |mut acc, maybe_batches| {
- acc.append(&mut maybe_batches?);
- Result::Ok(acc)
- })?
- .into_iter()
- .map(|x| Arc::new(x))
- .collect::<Vec<_>>();
-
- Ok(Box::pin(common::SizedRecordBatchStream::new(
- self.input.schema(),
- combined_results,
- )))
+ let mut stream = match input.execute(part_i).await {
+ Err(e) => {
+ // If send fails, plan being torn
+ // down, no place to send the error
+ let arrow_error = ArrowError::ExternalError(Box::new(e));
+ sender.send(Err(arrow_error)).await.ok();
+ return;
+ }
+ Ok(stream) => stream,
+ };
+
+ while let Some(item) = stream.next().await {
+ // If send fails, plan being torn down,
+ // there is no place to send the error
+ sender.send(item).await.ok();
+ }
+ });
+ }
+
+ Ok(Box::pin(MergeStream {
+ input: receiver,
+ schema: self.schema().clone(),
+ }))
}
}
}
}
+pin_project! {
+ struct MergeStream {
+ schema: SchemaRef,
+ #[pin]
+ input: mpsc::Receiver<ArrowResult<RecordBatch>>,
+ }
+}
+
+impl Stream for MergeStream {
+ type Item = ArrowResult<RecordBatch>;
+
+ fn poll_next(
+ self: std::pin::Pin<&mut Self>,
+ cx: &mut std::task::Context<'_>,
+ ) -> std::task::Poll<Option<Self::Item>> {
+ let this = self.project();
+ this.input.poll_next(cx)
+ }
+}
+
+impl RecordBatchStream for MergeStream {
+ fn schema(&self) -> SchemaRef {
+ self.schema.clone()
+ }
+}
+
#[cfg(test)]
mod tests {