|
| 1 | +package io.substrait.relation; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import io.substrait.TestBase; |
| 7 | +import io.substrait.extension.ExtensionLookup; |
| 8 | +import io.substrait.extension.ImmutableExtensionLookup; |
| 9 | +import io.substrait.proto.AggregateRel; |
| 10 | +import io.substrait.proto.Expression; |
| 11 | +import io.substrait.proto.Plan; |
| 12 | +import io.substrait.proto.Rel; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +class AggregateRelTest extends TestBase { |
| 16 | + |
| 17 | + protected static final Plan plan = Plan.newBuilder().build(); |
| 18 | + protected static final ExtensionLookup functionLookup = |
| 19 | + ImmutableExtensionLookup.builder().from(plan).build(); |
| 20 | + protected static final io.substrait.proto.NamedStruct namedStruct = createSchema(); |
| 21 | + |
| 22 | + public static io.substrait.proto.NamedStruct createSchema() { |
| 23 | + |
| 24 | + io.substrait.proto.Type i32Type = |
| 25 | + io.substrait.proto.Type.newBuilder() |
| 26 | + .setI32(io.substrait.proto.Type.I32.getDefaultInstance()) |
| 27 | + .build(); |
| 28 | + |
| 29 | + // Build a NamedStruct schema with two fields: col1, col2 |
| 30 | + io.substrait.proto.Type.Struct structType = |
| 31 | + io.substrait.proto.Type.Struct.newBuilder().addTypes(i32Type).addTypes(i32Type).build(); |
| 32 | + |
| 33 | + return io.substrait.proto.NamedStruct.newBuilder() |
| 34 | + .setStruct(structType) |
| 35 | + .addNames("col1") |
| 36 | + .addNames("col2") |
| 37 | + .build(); |
| 38 | + } |
| 39 | + |
| 40 | + public static io.substrait.proto.Expression createExpression(int col) { |
| 41 | + // Build a ReferenceSegment that refers to struct field col |
| 42 | + Expression.ReferenceSegment seg1 = |
| 43 | + Expression.ReferenceSegment.newBuilder() |
| 44 | + .setStructField( |
| 45 | + Expression.ReferenceSegment.StructField.newBuilder().setField(col).build()) |
| 46 | + .build(); |
| 47 | + |
| 48 | + // Build a FieldReference that uses the directReference and a rootReference |
| 49 | + Expression.FieldReference fieldRef1 = |
| 50 | + Expression.FieldReference.newBuilder() |
| 51 | + .setDirectReference(seg1) |
| 52 | + .setRootReference(Expression.FieldReference.RootReference.getDefaultInstance()) |
| 53 | + .build(); |
| 54 | + |
| 55 | + // Wrap the FieldReference in an Expression.selection |
| 56 | + return Expression.newBuilder().setSelection(fieldRef1).build(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testDeprecatedGroupingExpressionsAreMapped() { |
| 61 | + Expression col1Ref = createExpression(0); |
| 62 | + Expression col2Ref = createExpression(1); |
| 63 | + |
| 64 | + AggregateRel.Grouping grouping = |
| 65 | + AggregateRel.Grouping.newBuilder() |
| 66 | + .addGroupingExpressions(col1Ref) // deprecated proto form |
| 67 | + .addGroupingExpressions(col2Ref) |
| 68 | + .build(); |
| 69 | + |
| 70 | + // Build an input ReadRel |
| 71 | + io.substrait.proto.ReadRel readProto = |
| 72 | + io.substrait.proto.ReadRel.newBuilder().setBaseSchema(namedStruct).build(); |
| 73 | + |
| 74 | + // Build the AggregateRel with the new grouping_expressions field |
| 75 | + AggregateRel aggrProto = |
| 76 | + AggregateRel.newBuilder() |
| 77 | + .setInput(io.substrait.proto.Rel.newBuilder().setRead(readProto)) |
| 78 | + .addGroupings(grouping) |
| 79 | + .build(); |
| 80 | + |
| 81 | + Rel relProto = Rel.newBuilder().setAggregate(aggrProto).build(); |
| 82 | + ProtoRelConverter converter = new ProtoRelConverter(functionLookup); |
| 83 | + io.substrait.relation.Rel resultRel = converter.from(relProto); |
| 84 | + |
| 85 | + assertTrue(resultRel instanceof Aggregate); |
| 86 | + Aggregate agg = (Aggregate) resultRel; |
| 87 | + assertEquals(1, agg.getGroupings().size()); |
| 88 | + assertEquals(2, agg.getGroupings().get(0).getExpressions().size()); |
| 89 | + |
| 90 | + // Relation to Proto where both deprecated and new form are implemented |
| 91 | + RelProtoConverter relToProtoConverter = new RelProtoConverter(functionCollector); |
| 92 | + Rel newProto = relToProtoConverter.toProto(resultRel); |
| 93 | + |
| 94 | + assertEquals(2, newProto.getAggregate().getGroupings(0).getExpressionReferencesCount()); |
| 95 | + assertEquals(2, newProto.getAggregate().getGroupings(0).getGroupingExpressionsList().size()); |
| 96 | + assertEquals(2, newProto.getAggregate().getGroupingExpressionsList().size()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testNewAggregateProtoForm() { |
| 101 | + Expression col1Ref = createExpression(0); |
| 102 | + Expression col2Ref = createExpression(1); |
| 103 | + |
| 104 | + AggregateRel.Grouping grouping = |
| 105 | + AggregateRel.Grouping.newBuilder() |
| 106 | + .addExpressionReferences(0) // new proto form |
| 107 | + .addExpressionReferences(1) |
| 108 | + .build(); |
| 109 | + |
| 110 | + // Build an input ReadRel |
| 111 | + io.substrait.proto.ReadRel readProto = |
| 112 | + io.substrait.proto.ReadRel.newBuilder().setBaseSchema(namedStruct).build(); |
| 113 | + |
| 114 | + // Build the AggregateRel with the new grouping_expressions field |
| 115 | + AggregateRel aggrProto = |
| 116 | + AggregateRel.newBuilder() |
| 117 | + .setInput(io.substrait.proto.Rel.newBuilder().setRead(readProto)) |
| 118 | + .addGroupingExpressions(col1Ref) |
| 119 | + .addGroupingExpressions(col2Ref) |
| 120 | + .addGroupings(grouping) |
| 121 | + .build(); |
| 122 | + |
| 123 | + Rel relProto = Rel.newBuilder().setAggregate(aggrProto).build(); |
| 124 | + ProtoRelConverter converter = new ProtoRelConverter(functionLookup); |
| 125 | + io.substrait.relation.Rel resultRel = converter.from(relProto); |
| 126 | + |
| 127 | + assertTrue(resultRel instanceof Aggregate); |
| 128 | + Aggregate agg = (Aggregate) resultRel; |
| 129 | + assertEquals(1, agg.getGroupings().size()); |
| 130 | + assertEquals(2, agg.getGroupings().get(0).getExpressions().size()); |
| 131 | + |
| 132 | + // Relation to Proto where both deprecated and new form are implemented |
| 133 | + RelProtoConverter relToProtoConverter = new RelProtoConverter(functionCollector); |
| 134 | + Rel newProto = relToProtoConverter.toProto(resultRel); |
| 135 | + |
| 136 | + assertEquals(2, newProto.getAggregate().getGroupings(0).getExpressionReferencesCount()); |
| 137 | + assertEquals(2, newProto.getAggregate().getGroupings(0).getGroupingExpressionsList().size()); |
| 138 | + assertEquals(2, newProto.getAggregate().getGroupingExpressionsList().size()); |
| 139 | + } |
| 140 | +} |
0 commit comments