Skip to content

feat: use spawned tasks to reduce call stack depth and avoid busy waiting #16319

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions datafusion/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ impl From<GenericError> for DataFusionError {
}
}

impl From<JoinError> for DataFusionError {
fn from(e: JoinError) -> Self {
DataFusionError::ExecutionJoin(e)
}
}

impl Display for DataFusionError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
let error_prefix = self.error_prefix();
Expand Down
56 changes: 22 additions & 34 deletions datafusion/physical-plan/src/aggregates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ use std::any::Any;
use std::sync::Arc;

use super::{DisplayAs, ExecutionPlanProperties, PlanProperties};
use crate::aggregates::{
no_grouping::AggregateStream, row_hash::GroupedHashAggregateStream,
topk_stream::GroupedTopKAggregateStream,
};
use crate::execution_plan::{CardinalityEffect, EmissionType};
use crate::metrics::{ExecutionPlanMetricsSet, MetricsSet};
use crate::windows::get_ordered_partition_by_indices;
Expand Down Expand Up @@ -358,21 +354,10 @@ impl PartialEq for PhysicalGroupBy {
}
}

#[allow(clippy::large_enum_variant)]
enum StreamType {
AggregateStream(AggregateStream),
GroupedHash(GroupedHashAggregateStream),
GroupedPriorityQueue(GroupedTopKAggregateStream),
}

impl From<StreamType> for SendableRecordBatchStream {
fn from(stream: StreamType) -> Self {
match stream {
StreamType::AggregateStream(stream) => Box::pin(stream),
StreamType::GroupedHash(stream) => Box::pin(stream),
StreamType::GroupedPriorityQueue(stream) => Box::pin(stream),
}
}
AggregateStream(SendableRecordBatchStream),
GroupedHash(SendableRecordBatchStream),
GroupedPriorityQueue(SendableRecordBatchStream),
}

/// Hash aggregate execution plan
Expand Down Expand Up @@ -608,7 +593,7 @@ impl AggregateExec {
) -> Result<StreamType> {
// no group by at all
if self.group_by.expr.is_empty() {
return Ok(StreamType::AggregateStream(AggregateStream::new(
return Ok(StreamType::AggregateStream(no_grouping::aggregate_stream(
self, context, partition,
)?));
}
Expand All @@ -617,13 +602,13 @@ impl AggregateExec {
if let Some(limit) = self.limit {
if !self.is_unordered_unfiltered_group_by_distinct() {
return Ok(StreamType::GroupedPriorityQueue(
GroupedTopKAggregateStream::new(self, context, partition, limit)?,
topk_stream::aggregate_stream(self, context, partition, limit)?,
));
}
}

// grouping by something else and we need to just materialize all results
Ok(StreamType::GroupedHash(GroupedHashAggregateStream::new(
Ok(StreamType::GroupedHash(row_hash::aggregate_stream(
self, context, partition,
)?))
}
Expand Down Expand Up @@ -998,8 +983,11 @@ impl ExecutionPlan for AggregateExec {
partition: usize,
context: Arc<TaskContext>,
) -> Result<SendableRecordBatchStream> {
self.execute_typed(partition, context)
.map(|stream| stream.into())
match self.execute_typed(partition, context)? {
StreamType::AggregateStream(s) => Ok(s),
StreamType::GroupedHash(s) => Ok(s),
StreamType::GroupedPriorityQueue(s) => Ok(s),
}
}

fn metrics(&self) -> Option<MetricsSet> {
Expand Down Expand Up @@ -1274,7 +1262,7 @@ pub fn create_accumulators(
/// final value (mode = Final, FinalPartitioned and Single) or states (mode = Partial)
pub fn finalize_aggregation(
accumulators: &mut [AccumulatorItem],
mode: &AggregateMode,
mode: AggregateMode,
) -> Result<Vec<ArrayRef>> {
match mode {
AggregateMode::Partial => {
Expand Down Expand Up @@ -2105,20 +2093,20 @@ mod tests {
let stream = partial_aggregate.execute_typed(0, Arc::clone(&task_ctx))?;

// ensure that we really got the version we wanted
match version {
0 => {
assert!(matches!(stream, StreamType::AggregateStream(_)));
let stream = match stream {
StreamType::AggregateStream(s) => {
assert_eq!(version, 0);
s
}
1 => {
assert!(matches!(stream, StreamType::GroupedHash(_)));
StreamType::GroupedHash(s) => {
assert!(version == 1 || version == 2);
s
}
2 => {
assert!(matches!(stream, StreamType::GroupedHash(_)));
StreamType::GroupedPriorityQueue(_) => {
panic!("Unexpected GroupedPriorityQueue stream type");
}
_ => panic!("Unknown version: {version}"),
}
};

let stream: SendableRecordBatchStream = stream.into();
let err = collect(stream).await.unwrap_err();

// error root cause traversal is a bit complicated, see #4172.
Expand Down
Loading
Loading