Skip to content

Commit 885f9db

Browse files
committed
fix(core): use new aggregate grouping behavior in RelProtoConverter
Signed-off-by: Niels Pardon <par@zurich.ibm.com>
1 parent 824fc58 commit 885f9db

2 files changed

Lines changed: 76 additions & 4 deletions

File tree

core/src/main/java/io/substrait/relation/RelProtoConverter.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,21 @@ private io.substrait.proto.Expression.FieldReference toProto(FieldReference fiel
155155

156156
@Override
157157
public Rel visit(Aggregate aggregate, EmptyVisitationContext context) throws RuntimeException {
158-
AggregateRel.Builder builder =
158+
final List<Expression> uniqueGroupingExpressions =
159+
aggregate.getGroupings().stream()
160+
.flatMap(g -> g.getExpressions().stream())
161+
.distinct()
162+
.collect(Collectors.toList());
163+
164+
final AggregateRel.Builder builder =
159165
AggregateRel.newBuilder()
160166
.setInput(toProto(aggregate.getInput()))
161167
.setCommon(common(aggregate))
168+
.addAllGroupingExpressions(toProto(uniqueGroupingExpressions))
162169
.addAllGroupings(
163-
aggregate.getGroupings().stream().map(this::toProto).collect(Collectors.toList()))
170+
aggregate.getGroupings().stream()
171+
.map(g -> toProto(g, uniqueGroupingExpressions))
172+
.collect(Collectors.toList()))
164173
.addAllMeasures(
165174
aggregate.getMeasures().stream().map(this::toProto).collect(Collectors.toList()));
166175

@@ -203,9 +212,13 @@ private AggregateRel.Measure toProto(Aggregate.Measure measure) {
203212
return builder.build();
204213
}
205214

206-
private AggregateRel.Grouping toProto(Aggregate.Grouping grouping) {
215+
private AggregateRel.Grouping toProto(
216+
Aggregate.Grouping grouping, List<Expression> uniqueGroupingExpressions) {
207217
return AggregateRel.Grouping.newBuilder()
208-
.addAllGroupingExpressions(toProto(grouping.getExpressions()))
218+
.addAllExpressionReferences(
219+
grouping.getExpressions().stream()
220+
.map(e -> uniqueGroupingExpressions.indexOf(e))
221+
.collect(Collectors.toList()))
209222
.build();
210223
}
211224

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.substrait.proto.Plan;
1212
import io.substrait.proto.ReadRel;
1313
import io.substrait.proto.Rel;
14+
import io.substrait.util.EmptyVisitationContext;
1415
import org.junit.jupiter.api.Test;
1516

1617
class AggregateRelTest extends TestBase {
@@ -90,6 +91,24 @@ void testDeprecatedGroupingExpressionConversion() {
9091
Aggregate agg = (Aggregate) resultRel;
9192
assertEquals(1, agg.getGroupings().size());
9293
assertEquals(2, agg.getGroupings().get(0).getExpressions().size());
94+
95+
Rel roundtripRel = relProtoConverter.visit(agg, EmptyVisitationContext.INSTANCE);
96+
assertTrue(roundtripRel.hasAggregate());
97+
98+
AggregateRel roundtripAggr = roundtripRel.getAggregate();
99+
assertEquals(1, roundtripAggr.getGroupingsCount(), "grouping count should be 1");
100+
assertEquals(
101+
2,
102+
roundtripAggr.getGroupingExpressionsCount(),
103+
"grouping expressions count of aggregate should be 2");
104+
assertEquals(
105+
0,
106+
roundtripAggr.getGroupings(0).getGroupingExpressionsCount(),
107+
"grouping expressions count of grouping should be 0");
108+
assertEquals(
109+
2,
110+
roundtripAggr.getGroupings(0).getExpressionReferencesCount(),
111+
"expression reference count of grouping should be 2");
93112
}
94113

95114
@Test
@@ -127,6 +146,24 @@ void testAggregateWithSingleGrouping() {
127146
Aggregate agg = (Aggregate) resultRel;
128147
assertEquals(1, agg.getGroupings().size());
129148
assertEquals(2, agg.getGroupings().get(0).getExpressions().size());
149+
150+
Rel roundtripRel = relProtoConverter.visit(agg, EmptyVisitationContext.INSTANCE);
151+
assertTrue(roundtripRel.hasAggregate());
152+
153+
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+
0,
161+
roundtripAgg.getGroupings(0).getGroupingExpressionsCount(),
162+
"grouping expressions count of grouping should be 0");
163+
assertEquals(
164+
2,
165+
roundtripAgg.getGroupings(0).getExpressionReferencesCount(),
166+
"expression reference count of grouping should be 2");
130167
}
131168

132169
@Test
@@ -169,5 +206,27 @@ void testAggregateWithMultipleGroupings() {
169206
assertEquals(2, agg.getGroupings().size());
170207
assertEquals(2, agg.getGroupings().get(0).getExpressions().size());
171208
assertEquals(1, agg.getGroupings().get(1).getExpressions().size());
209+
210+
Rel roundtripRel = relProtoConverter.visit(agg, EmptyVisitationContext.INSTANCE);
211+
assertTrue(roundtripRel.hasAggregate());
212+
213+
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+
0,
221+
roundtripAgg.getGroupings(0).getGroupingExpressionsCount(),
222+
"grouping expressions count of grouping should be 0");
223+
assertEquals(
224+
2,
225+
roundtripAgg.getGroupings(0).getExpressionReferencesCount(),
226+
"expression reference count of grouping should be 2");
227+
assertEquals(
228+
1,
229+
roundtripAgg.getGroupings(1).getExpressionReferencesCount(),
230+
"expression reference count of grouping should be 2");
172231
}
173232
}

0 commit comments

Comments
 (0)