kosiew commented on code in PR #27073:
URL: https://github.com/apache/datafusion/pull/27073#discussion_r4696380491


##########
datafusion/substrait/tests/cases/roundtrip_logical_plan.rs:
##########
@@ -432,6 +432,86 @@ async fn simple_aggregate() -> Result<()> {
     roundtrip("SELECT a, sum(b) FROM data GROUP BY a").await
 }
 
+#[tokio::test]
+async fn aggregate_duplicate_group_by_key_roundtrip() -> Result<()> {
+    // https://github.com/apache/datafusion/issues/27058
+    // Repeated GROUP BY keys are legal SQL and do not change the result. The
+    // eliminate_duplicated_expr optimizer removes them, so use the unoptimized
+    // plan to exercise the producer path that used to emit duplicate grouping
+    // expression references and shift projection column indices on round trip.
+    let ctx = create_context().await?;
+    let sql = "SELECT a, sum(b) AS s FROM data GROUP BY a, a ORDER BY a";
+    let plan = ctx.sql(sql).await?.into_unoptimized_plan();
+
+    let LogicalPlan::Sort(sort) = &plan else {
+        panic!("expected Sort, got {plan}");
+    };
+    let LogicalPlan::Projection(projection) = sort.input.as_ref() else {
+        panic!("expected Projection under Sort, got {}", sort.input);
+    };
+    let LogicalPlan::Aggregate(agg) = projection.input.as_ref() else {
+        panic!(
+            "expected Aggregate under Projection, got {}",
+            projection.input
+        );
+    };
+    assert_eq!(
+        agg.group_expr.len(),
+        2,
+        "fixture must keep duplicate group keys"
+    );
+    assert_eq!(
+        agg.schema.fields().len(),
+        2,
+        "Aggregate schema still has one column per unique group key plus 
measures"
+    );
+
+    let proto = to_substrait_plan(&plan, &ctx.state())?;
+    let aggregate = find_aggregate_rel(&proto).expect("plan should contain 
AggregateRel");
+    assert_eq!(
+        aggregate.grouping_expressions.len(),
+        1,
+        "producer should declare the repeated key once"
+    );
+    assert_eq!(
+        aggregate.groupings[0].expression_references,
+        vec![0],
+        "producer should not repeat the grouping expression reference"
+    );
+
+    let plan2 = from_substrait_plan(&ctx.state(), &proto).await?;
+    let expected = DataFrame::new(ctx.state(), plan.clone()).collect().await?;
+    let actual = DataFrame::new(ctx.state(), plan2).collect().await?;
+    assert_eq!(
+        format!("{expected:?}"),

Review Comment:
   Consider using `datafusion::assert_batches_eq!` with the expected rows `(1, 
2.00)` and `(3, 4.50)` for clearer failure diagnostics. Retain a separate 
schema equality check to verify types and nullability.



##########
datafusion/substrait/tests/cases/roundtrip_logical_plan.rs:
##########
@@ -432,6 +432,86 @@ async fn simple_aggregate() -> Result<()> {
     roundtrip("SELECT a, sum(b) FROM data GROUP BY a").await
 }
 
+#[tokio::test]
+async fn aggregate_duplicate_group_by_key_roundtrip() -> Result<()> {
+    // https://github.com/apache/datafusion/issues/27058
+    // Repeated GROUP BY keys are legal SQL and do not change the result. The
+    // eliminate_duplicated_expr optimizer removes them, so use the unoptimized
+    // plan to exercise the producer path that used to emit duplicate grouping
+    // expression references and shift projection column indices on round trip.
+    let ctx = create_context().await?;
+    let sql = "SELECT a, sum(b) AS s FROM data GROUP BY a, a ORDER BY a";
+    let plan = ctx.sql(sql).await?.into_unoptimized_plan();
+
+    let LogicalPlan::Sort(sort) = &plan else {
+        panic!("expected Sort, got {plan}");
+    };
+    let LogicalPlan::Projection(projection) = sort.input.as_ref() else {
+        panic!("expected Projection under Sort, got {}", sort.input);
+    };
+    let LogicalPlan::Aggregate(agg) = projection.input.as_ref() else {
+        panic!(
+            "expected Aggregate under Projection, got {}",
+            projection.input
+        );
+    };
+    assert_eq!(
+        agg.group_expr.len(),
+        2,
+        "fixture must keep duplicate group keys"
+    );
+    assert_eq!(
+        agg.schema.fields().len(),
+        2,
+        "Aggregate schema still has one column per unique group key plus 
measures"
+    );
+
+    let proto = to_substrait_plan(&plan, &ctx.state())?;
+    let aggregate = find_aggregate_rel(&proto).expect("plan should contain 
AggregateRel");
+    assert_eq!(
+        aggregate.grouping_expressions.len(),
+        1,
+        "producer should declare the repeated key once"
+    );
+    assert_eq!(
+        aggregate.groupings[0].expression_references,

Review Comment:
   Could we also assert that `aggregate.groupings[0].grouping_expressions.len() 
== 1`? This would directly verify the deprecated inline encoding, which the 
consumer prioritizes when present.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to