Skip to content

Commit 3408fce

Browse files
committed
fix: apply feedback
Signed-off-by: Niels Pardon <par@zurich.ibm.com>
1 parent f6e7f5b commit 3408fce

1 file changed

Lines changed: 57 additions & 42 deletions

File tree

core/src/test/java/io/substrait/relation/AggregateRelTest.java

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import io.substrait.proto.ReadRel;
1313
import io.substrait.proto.Rel;
1414
import io.substrait.util.EmptyVisitationContext;
15+
import java.util.List;
16+
import java.util.stream.Collectors;
1517
import org.junit.jupiter.api.Test;
1618

1719
class AggregateRelTest extends TestBase {
@@ -58,6 +60,43 @@ public static io.substrait.proto.Expression createFieldReference(int col) {
5860
return Expression.newBuilder().setSelection(fieldRef1).build();
5961
}
6062

63+
/**
64+
* Helper method to extract expression references from an AggregateRel.
65+
*
66+
* @param aggregateRel the AggregateRel to extract expression references from
67+
* @return a list of lists, where each inner list contains the expression reference indices for a
68+
* grouping
69+
*/
70+
private static List<List<Integer>> getExpressionReferences(AggregateRel aggregateRel) {
71+
return aggregateRel.getGroupingsList().stream()
72+
.map(
73+
grouping ->
74+
grouping.getExpressionReferencesList().stream().collect(Collectors.toList()))
75+
.collect(Collectors.toList());
76+
}
77+
78+
/**
79+
* Helper method to extract deprecated grouping expressions from an AggregateRel.
80+
*
81+
* @param aggregateRel the AggregateRel to extract grouping expressions from
82+
* @return a list of lists, where each inner list contains the grouping expressions for a grouping
83+
*/
84+
private static List<List<Expression>> getGroupingExpressions(AggregateRel aggregateRel) {
85+
return aggregateRel.getGroupingsList().stream()
86+
.map(grouping -> grouping.getGroupingExpressionsList())
87+
.collect(Collectors.toList());
88+
}
89+
90+
/**
91+
* Helper method to extract aggregate-level grouping expressions from an AggregateRel.
92+
*
93+
* @param aggregateRel the AggregateRel to extract grouping expressions from
94+
* @return a list of expressions at the aggregate level
95+
*/
96+
private static List<Expression> getAggregateGroupingExpressions(AggregateRel aggregateRel) {
97+
return aggregateRel.getGroupingExpressionsList();
98+
}
99+
61100
@Test
62101
void testDeprecatedGroupingExpressionConversion() {
63102
Expression col1Ref = createFieldReference(0);
@@ -96,19 +135,12 @@ void testDeprecatedGroupingExpressionConversion() {
96135
assertTrue(roundtripRel.hasAggregate());
97136

98137
AggregateRel roundtripAgg = roundtripRel.getAggregate();
99-
assertEquals(1, roundtripAgg.getGroupingsCount(), "grouping count should be 1");
100-
assertEquals(
101-
2,
102-
roundtripAgg.getGroupingExpressionsCount(),
103-
"grouping expressions count of aggregate should be 2");
104-
assertEquals(
105-
2,
106-
roundtripAgg.getGroupings(0).getGroupingExpressionsCount(),
107-
"grouping expressions count of grouping should be 2");
108-
assertEquals(
109-
2,
110-
roundtripAgg.getGroupings(0).getExpressionReferencesCount(),
111-
"expression reference count of grouping should be 2");
138+
// Verify new expression_references structure
139+
assertEquals(List.of(List.of(0, 1)), getExpressionReferences(roundtripAgg));
140+
// Verify backward compatibility: deprecated grouping_expressions field is also populated
141+
assertEquals(List.of(List.of(col1Ref, col2Ref)), getGroupingExpressions(roundtripAgg));
142+
// Verify aggregate-level grouping_expressions field is populated
143+
assertEquals(List.of(col1Ref, col2Ref), getAggregateGroupingExpressions(roundtripAgg));
112144
}
113145

114146
@Test
@@ -151,19 +183,12 @@ void testAggregateWithSingleGrouping() {
151183
assertTrue(roundtripRel.hasAggregate());
152184

153185
AggregateRel roundtripAgg = roundtripRel.getAggregate();
154-
assertEquals(1, roundtripAgg.getGroupingsCount(), "grouping count should be 1");
155-
assertEquals(
156-
2,
157-
roundtripAgg.getGroupingExpressionsCount(),
158-
"grouping expressions count of aggregate should be 2");
159-
assertEquals(
160-
2,
161-
roundtripAgg.getGroupings(0).getGroupingExpressionsCount(),
162-
"grouping expressions count of grouping should be 2");
163-
assertEquals(
164-
2,
165-
roundtripAgg.getGroupings(0).getExpressionReferencesCount(),
166-
"expression reference count of grouping should be 2");
186+
// Verify new expression_references structure
187+
assertEquals(List.of(List.of(0, 1)), getExpressionReferences(roundtripAgg));
188+
// Verify backward compatibility: deprecated grouping_expressions field is also populated
189+
assertEquals(List.of(List.of(col1Ref, col2Ref)), getGroupingExpressions(roundtripAgg));
190+
// Verify aggregate-level grouping_expressions field is populated
191+
assertEquals(List.of(col1Ref, col2Ref), getAggregateGroupingExpressions(roundtripAgg));
167192
}
168193

169194
@Test
@@ -211,22 +236,12 @@ void testAggregateWithMultipleGroupings() {
211236
assertTrue(roundtripRel.hasAggregate());
212237

213238
AggregateRel roundtripAgg = roundtripRel.getAggregate();
214-
assertEquals(2, roundtripAgg.getGroupingsCount(), "grouping count should be 2");
215-
assertEquals(
216-
2,
217-
roundtripAgg.getGroupingExpressionsCount(),
218-
"grouping expressions count of aggregate should be 2");
219-
assertEquals(
220-
2,
221-
roundtripAgg.getGroupings(0).getGroupingExpressionsCount(),
222-
"grouping expressions count of grouping should be 2");
223-
assertEquals(
224-
2,
225-
roundtripAgg.getGroupings(0).getExpressionReferencesCount(),
226-
"expression reference count of grouping should be 2");
239+
// Verify new expression_references structure
240+
assertEquals(List.of(List.of(0, 1), List.of(1)), getExpressionReferences(roundtripAgg));
241+
// Verify backward compatibility: deprecated grouping_expressions field is also populated
227242
assertEquals(
228-
1,
229-
roundtripAgg.getGroupings(1).getExpressionReferencesCount(),
230-
"expression reference count of grouping should be 2");
243+
List.of(List.of(col1Ref, col2Ref), List.of(col2Ref)), getGroupingExpressions(roundtripAgg));
244+
// Verify aggregate-level grouping_expressions field is populated
245+
assertEquals(List.of(col1Ref, col2Ref), getAggregateGroupingExpressions(roundtripAgg));
231246
}
232247
}

0 commit comments

Comments
 (0)