Skip to content

Apache DataFusion SQL Query Engine

ARROW-11606: [Rust] [DataFusion] Add input schema to HashAggregateExec

Andy Groveb71e3ee5b7b8
diff --git a/rust/datafusion/src/physical_plan/hash_aggregate.rs b/rust/datafusion/src/physical_plan/hash_aggregate.rs
index 54d25f17db..342ca320f8 100644
--- a/rust/datafusion/src/physical_plan/hash_aggregate.rs
+++ b/rust/datafusion/src/physical_plan/hash_aggregate.rs
@@ -77,10 +77,14 @@ pub struct HashAggregateExec {
group_expr: Vec<(Arc<dyn PhysicalExpr>, String)>,
/// Aggregate expressions
aggr_expr: Vec<Arc<dyn AggregateExpr>>,
- /// Input plan
+ /// Input plan, could be a partial aggregate or the input to the aggregate
input: Arc<dyn ExecutionPlan>,
/// Schema after the aggregate is applied
schema: SchemaRef,
+ /// Input schema before any aggregation is applied. For partial aggregate this will be the
+ /// same as input.schema() but for the final aggregate it will be the same as the input
+ /// to the partial aggregate
+ input_schema: SchemaRef,
}
fn create_schema(
@@ -123,6 +127,7 @@ impl HashAggregateExec {
group_expr: Vec<(Arc<dyn PhysicalExpr>, String)>,
aggr_expr: Vec<Arc<dyn AggregateExpr>>,
input: Arc<dyn ExecutionPlan>,
+ input_schema: SchemaRef,
) -> Result<Self> {
let schema = create_schema(&input.schema(), &group_expr, &aggr_expr, mode)?;
@@ -134,6 +139,7 @@ impl HashAggregateExec {
aggr_expr,
input,
schema,
+ input_schema,
})
}
@@ -156,6 +162,11 @@ impl HashAggregateExec {
pub fn input(&self) -> &Arc<dyn ExecutionPlan> {
&self.input
}
+
+ /// Get the input schema before any aggregates are applied
+ pub fn input_schema(&self) -> SchemaRef {
+ self.input_schema.clone()
+ }
}
#[async_trait]
@@ -217,6 +228,7 @@ impl ExecutionPlan for HashAggregateExec {
self.group_expr.clone(),
self.aggr_expr.clone(),
children[0].clone(),
+ self.input_schema.clone(),
)?)),
_ => Err(DataFusionError::Internal(
"HashAggregateExec wrong number of children".to_string(),
@@ -1048,11 +1060,13 @@ mod tests {
DataType::Float64,
))];
+ let input_schema = input.schema();
let partial_aggregate = Arc::new(HashAggregateExec::try_new(
AggregateMode::Partial,
groups.clone(),
aggregates.clone(),
input,
+ input_schema.clone(),
)?);
let result = common::collect(partial_aggregate.execute(0).await?).await?;
@@ -1082,6 +1096,7 @@ mod tests {
.collect(),
aggregates,
merge,
+ input_schema,
)?);
let result = common::collect(merged_aggregate.execute(0).await?).await?;
diff --git a/rust/datafusion/src/physical_plan/planner.rs b/rust/datafusion/src/physical_plan/planner.rs
index 93bb94327a..3f2df33d8f 100644
--- a/rust/datafusion/src/physical_plan/planner.rs
+++ b/rust/datafusion/src/physical_plan/planner.rs
@@ -186,6 +186,7 @@ impl DefaultPhysicalPlanner {
} => {
// Initially need to perform the aggregate and then merge the partitions
let input_exec = self.create_physical_plan(input, ctx_state)?;
+ let input_schema = input_exec.schema();
let physical_input_schema = input_exec.as_ref().schema();
let logical_input_schema = input.as_ref().schema();
@@ -219,6 +220,7 @@ impl DefaultPhysicalPlanner {
groups.clone(),
aggregates.clone(),
input_exec,
+ input_schema.clone(),
)?);
let final_group: Vec<Arc<dyn PhysicalExpr>> =
@@ -235,6 +237,7 @@ impl DefaultPhysicalPlanner {
.collect(),
aggregates,
initial_aggr,
+ input_schema,
)?))
}
LogicalPlan::Projection { input, expr, .. } => {
@@ -978,6 +981,29 @@ mod tests {
Ok(())
}
+ #[test]
+ fn hash_agg_input_schema() -> Result<()> {
+ let testdata = arrow::util::test_util::arrow_test_data();
+ let path = format!("{}/csv/aggregate_test_100.csv", testdata);
+
+ let options = CsvReadOptions::new().schema_infer_max_records(100);
+ let logical_plan = LogicalPlanBuilder::scan_csv(&path, options, None)?
+ .aggregate(&[col("c1")], &[sum(col("c2"))])?
+ .build()?;
+
+ let execution_plan = plan(&logical_plan)?;
+ let final_hash_agg = execution_plan
+ .as_any()
+ .downcast_ref::<HashAggregateExec>()
+ .expect("hash aggregate");
+ assert_eq!("SUM(c2)", final_hash_agg.schema().field(1).name());
+ // we need access to the input to the partial aggregate so that other projects can
+ // implement serde
+ assert_eq!("c2", final_hash_agg.input_schema().field(1).name());
+
+ Ok(())
+ }
+
/// An example extension node that doesn't do anything
struct NoOpExtensionNode {
schema: DFSchemaRef,