|
12 | 12 | import io.substrait.proto.ReadRel; |
13 | 13 | import io.substrait.proto.Rel; |
14 | 14 | import io.substrait.util.EmptyVisitationContext; |
| 15 | +import java.util.List; |
| 16 | +import java.util.stream.Collectors; |
15 | 17 | import org.junit.jupiter.api.Test; |
16 | 18 |
|
17 | 19 | class AggregateRelTest extends TestBase { |
@@ -58,6 +60,43 @@ public static io.substrait.proto.Expression createFieldReference(int col) { |
58 | 60 | return Expression.newBuilder().setSelection(fieldRef1).build(); |
59 | 61 | } |
60 | 62 |
|
| 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 | + |
61 | 100 | @Test |
62 | 101 | void testDeprecatedGroupingExpressionConversion() { |
63 | 102 | Expression col1Ref = createFieldReference(0); |
@@ -96,19 +135,12 @@ void testDeprecatedGroupingExpressionConversion() { |
96 | 135 | assertTrue(roundtripRel.hasAggregate()); |
97 | 136 |
|
98 | 137 | 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)); |
112 | 144 | } |
113 | 145 |
|
114 | 146 | @Test |
@@ -151,19 +183,12 @@ void testAggregateWithSingleGrouping() { |
151 | 183 | assertTrue(roundtripRel.hasAggregate()); |
152 | 184 |
|
153 | 185 | 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)); |
167 | 192 | } |
168 | 193 |
|
169 | 194 | @Test |
@@ -211,22 +236,12 @@ void testAggregateWithMultipleGroupings() { |
211 | 236 | assertTrue(roundtripRel.hasAggregate()); |
212 | 237 |
|
213 | 238 | 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 |
227 | 242 | 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)); |
231 | 246 | } |
232 | 247 | } |
0 commit comments