Skip to content

Commit

Permalink
feat(substrait): support order_by in aggregate functions (#13114)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvolpato authored Oct 29, 2024
1 parent 89e71ef commit d62f262
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 3 deletions.
17 changes: 15 additions & 2 deletions datafusion/substrait/src/logical_plan/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,14 +714,27 @@ pub async fn from_substrait_rel(
}
_ => false,
};
let order_by = if !f.sorts.is_empty() {
Some(
from_substrait_sorts(
ctx,
&f.sorts,
input.schema(),
extensions,
)
.await?,
)
} else {
None
};

from_substrait_agg_func(
ctx,
f,
input.schema(),
extensions,
filter,
// TODO: Add parsing of order_by also
None,
order_by,
distinct,
)
.await
Expand Down
16 changes: 15 additions & 1 deletion datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,19 @@ async fn aggregate_wo_projection_consume() -> Result<()> {
.await
}

#[tokio::test]
async fn aggregate_wo_projection_sorted_consume() -> Result<()> {
let proto_plan =
read_json("tests/testdata/test_plans/aggregate_sorted_no_project.substrait.json");

assert_expected_plan_substrait(
proto_plan,
"Aggregate: groupBy=[[data.a]], aggr=[[count(data.a) ORDER BY [data.a DESC NULLS FIRST] AS countA]]\
\n TableScan: data projection=[a]",
)
.await
}

#[tokio::test]
async fn simple_intersect_consume() -> Result<()> {
let proto_plan = read_json("tests/testdata/test_plans/intersect.substrait.json");
Expand Down Expand Up @@ -1025,8 +1038,9 @@ async fn roundtrip_aggregate_udf() -> Result<()> {

let ctx = create_context().await?;
ctx.register_udaf(dummy_agg);
roundtrip_with_ctx("select dummy_agg(a) from data", ctx.clone()).await?;
roundtrip_with_ctx("select dummy_agg(a order by a) from data", ctx.clone()).await?;

roundtrip_with_ctx("select dummy_agg(a) from data", ctx).await?;
Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"extensionUris": [
{
"uri": "https://github.com/substrait-io/substrait/blob/main/extensions/functions_aggregate_generic.yaml"
}
],
"extensions": [
{
"extensionFunction": {
"functionAnchor": 185,
"name": "count:any"
}
}
],
"relations": [
{
"root": {
"input": {
"aggregate": {
"input": {
"read": {
"common": {
"direct": {}
},
"baseSchema": {
"names": [
"a"
],
"struct": {
"types": [
{
"i64": {
"nullability": "NULLABILITY_NULLABLE"
}
}
],
"nullability": "NULLABILITY_NULLABLE"
}
},
"namedTable": {
"names": [
"data"
]
}
}
},
"groupings": [
{
"groupingExpressions": [
{
"selection": {
"directReference": {
"structField": {}
},
"rootReference": {}
}
}
]
}
],
"measures": [
{
"measure": {
"functionReference": 185,
"phase": "AGGREGATION_PHASE_INITIAL_TO_RESULT",
"outputType": {
"i64": {}
},
"arguments": [
{
"value": {
"selection": {
"directReference": {
"structField": {}
},
"rootReference": {}
}
}
}
],
"sorts": [
{
"expr": {
"selection": {
"directReference": {
"structField": {
"field": 0
}
},
"rootReference": {
}
}
},
"direction": "SORT_DIRECTION_DESC_NULLS_FIRST"
}
]
}
}
]
}
},
"names": [
"a",
"countA"
]
}
}
],
"version": {
"minorNumber": 54,
"producer": "manual"
}
}

0 comments on commit d62f262

Please sign in to comment.