Skip to content
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

Properly encode STRING_AGG, NTH_VALUE in physical plan protobufs #9027

Merged
merged 2 commits into from
Jan 28, 2024
Merged
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
10 changes: 7 additions & 3 deletions datafusion/proto/src/physical_plan/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ use datafusion::physical_plan::expressions::{
CastExpr, Column, Correlation, Count, Covariance, CovariancePop, CumeDist,
DistinctArrayAgg, DistinctBitXor, DistinctCount, DistinctSum, FirstValue, Grouping,
InListExpr, IsNotNullExpr, IsNullExpr, LastValue, LikeExpr, Literal, Max, Median,
Min, NegativeExpr, NotExpr, NthValue, Ntile, OrderSensitiveArrayAgg, Rank, RankType,
Regr, RegrType, RowNumber, Stddev, StddevPop, Sum, TryCastExpr, Variance,
VariancePop, WindowShift,
Min, NegativeExpr, NotExpr, NthValue, NthValueAgg, Ntile, OrderSensitiveArrayAgg,
Rank, RankType, Regr, RegrType, RowNumber, Stddev, StddevPop, StringAgg, Sum,
TryCastExpr, Variance, VariancePop, WindowShift,
};
use datafusion::physical_plan::udaf::AggregateFunctionExpr;
use datafusion::physical_plan::windows::{BuiltInWindowExpr, PlainAggregateWindowExpr};
Expand Down Expand Up @@ -363,6 +363,10 @@ fn aggr_expr_to_aggr_fn(expr: &dyn AggregateExpr) -> Result<AggrFn> {
protobuf::AggregateFunction::FirstValueAgg
} else if aggr_expr.downcast_ref::<LastValue>().is_some() {
protobuf::AggregateFunction::LastValueAgg
} else if aggr_expr.downcast_ref::<StringAgg>().is_some() {
protobuf::AggregateFunction::StringAgg
} else if aggr_expr.downcast_ref::<NthValueAgg>().is_some() {
protobuf::AggregateFunction::NthValueAgg
} else {
return not_impl_err!("Aggregate function not supported: {expr:?}");
};
Expand Down
55 changes: 41 additions & 14 deletions datafusion/proto/tests/cases/roundtrip_physical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ use datafusion::physical_plan::analyze::AnalyzeExec;
use datafusion::physical_plan::empty::EmptyExec;
use datafusion::physical_plan::expressions::{
binary, cast, col, in_list, like, lit, Avg, BinaryExpr, Column, DistinctCount,
GetFieldAccessExpr, GetIndexedFieldExpr, NotExpr, NthValue, PhysicalSortExpr, Sum,
GetFieldAccessExpr, GetIndexedFieldExpr, NotExpr, NthValue, PhysicalSortExpr,
StringAgg, Sum,
};
use datafusion::physical_plan::filter::FilterExec;
use datafusion::physical_plan::insert::FileSinkExec;
Expand Down Expand Up @@ -328,20 +329,46 @@ fn rountrip_aggregate() -> Result<()> {
let groups: Vec<(Arc<dyn PhysicalExpr>, String)> =
vec![(col("a", &schema)?, "unused".to_string())];

let aggregates: Vec<Arc<dyn AggregateExpr>> = vec![Arc::new(Avg::new(
cast(col("b", &schema)?, &schema, DataType::Float64)?,
"AVG(b)".to_string(),
DataType::Float64,
))];
let test_cases: Vec<Vec<Arc<dyn AggregateExpr>>> = vec![
// AVG
vec![Arc::new(Avg::new(
cast(col("b", &schema)?, &schema, DataType::Float64)?,
"AVG(b)".to_string(),
DataType::Float64,
))],
// TODO: See <https://github.com/apache/arrow-datafusion/issues/9028>
// // NTH_VALUE
// vec![Arc::new(NthValueAgg::new(
// col("b", &schema)?,
// 1,
// "NTH_VALUE(b, 1)".to_string(),
// DataType::Int64,
// false,
// Vec::new(),
// Vec::new(),
// ))],
// STRING_AGG
vec![Arc::new(StringAgg::new(
cast(col("b", &schema)?, &schema, DataType::Utf8)?,
lit(ScalarValue::Utf8(Some(",".to_string()))),
"STRING_AGG(name, ',')".to_string(),
DataType::Utf8,
))],
];

roundtrip_test(Arc::new(AggregateExec::try_new(
AggregateMode::Final,
PhysicalGroupBy::new_single(groups.clone()),
aggregates.clone(),
vec![None],
Arc::new(EmptyExec::new(schema.clone())),
schema,
)?))
for aggregates in test_cases {
let schema = schema.clone();
roundtrip_test(Arc::new(AggregateExec::try_new(
AggregateMode::Final,
PhysicalGroupBy::new_single(groups.clone()),
aggregates,
vec![None],
Arc::new(EmptyExec::new(schema.clone())),
schema,
)?))?;
}

Ok(())
}

#[test]
Expand Down
Loading