Skip to content

fix(core): row type should output the window function aggregation results #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected Type.Struct deriveRecordType() {
.struct(
Stream.concat(
initial.fields().stream(),
getPartitionExpressions().stream().map(Expression::getType)));
getWindowFunctions().stream().map(WindowRelFunctionInvocation::outputType)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is consistent with the language in the spec

Same as Project operator (input followed by each window expression).

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class ConsistentPartitionWindowRelRoundtripTest extends TestBase {

@Test
void consistentPartitionWindowRoundtrip() {
void consistentPartitionWindowRoundtripSingle() {
var windowFunctionDeclaration =
defaultExtensionCollection.getWindowFunction(
SimpleExtension.FunctionAnchor.of(
Expand Down Expand Up @@ -63,5 +63,83 @@ void consistentPartitionWindowRoundtrip() {
io.substrait.proto.Rel protoRel = relProtoConverter.toProto(rel1);
io.substrait.relation.Rel rel2 = protoRelConverter.from(protoRel);
assertEquals(rel1, rel2);

// Make sure that the record types match I64, I16, I32 and then the I64 from the window
// function.
assertEquals(rel2.getRecordType().fields(), Arrays.asList(R.I64, R.I16, R.I32, R.I64));
}

@Test
void consistentPartitionWindowRoundtripMulti() {
var windowFunctionLeadDeclaration =
defaultExtensionCollection.getWindowFunction(
SimpleExtension.FunctionAnchor.of(
DefaultExtensionCatalog.FUNCTIONS_ARITHMETIC, "lead:any"));
var windowFunctionLagDeclaration =
defaultExtensionCollection.getWindowFunction(
SimpleExtension.FunctionAnchor.of(
DefaultExtensionCatalog.FUNCTIONS_ARITHMETIC, "lead:any"));
Rel input =
b.namedScan(
Arrays.asList("test"),
Arrays.asList("a", "b", "c"),
Arrays.asList(R.I64, R.I16, R.I32));
Rel rel1 =
ImmutableConsistentPartitionWindow.builder()
.input(input)
.windowFunctions(
Arrays.asList(
ConsistentPartitionWindow.WindowRelFunctionInvocation.builder()
.declaration(windowFunctionLeadDeclaration)
// lead(a)
.arguments(Arrays.asList(b.fieldReference(input, 0)))
.options(
Arrays.asList(
FunctionOption.builder()
.name("option")
.addValues("VALUE1", "VALUE2")
.build()))
.outputType(R.I64)
.aggregationPhase(Expression.AggregationPhase.INITIAL_TO_RESULT)
.invocation(Expression.AggregationInvocation.ALL)
.lowerBound(ImmutableWindowBound.Unbounded.UNBOUNDED)
.upperBound(ImmutableWindowBound.Following.CURRENT_ROW)
.boundsType(Expression.WindowBoundsType.RANGE)
.build(),
ConsistentPartitionWindow.WindowRelFunctionInvocation.builder()
.declaration(windowFunctionLagDeclaration)
// lag(a)
.arguments(Arrays.asList(b.fieldReference(input, 0)))
.options(
Arrays.asList(
FunctionOption.builder()
.name("option")
.addValues("VALUE1", "VALUE2")
.build()))
.outputType(R.I64)
.aggregationPhase(Expression.AggregationPhase.INITIAL_TO_RESULT)
.invocation(Expression.AggregationInvocation.ALL)
.lowerBound(ImmutableWindowBound.Unbounded.UNBOUNDED)
.upperBound(ImmutableWindowBound.Following.CURRENT_ROW)
.boundsType(Expression.WindowBoundsType.RANGE)
.build()))
// PARTITION BY b
.partitionExpressions(Arrays.asList(b.fieldReference(input, 1)))
.sorts(
Arrays.asList(
Expression.SortField.builder()
// SORT BY c
.expr(b.fieldReference(input, 2))
.direction(Expression.SortDirection.ASC_NULLS_FIRST)
.build()))
.build();

io.substrait.proto.Rel protoRel = relProtoConverter.toProto(rel1);
io.substrait.relation.Rel rel2 = protoRelConverter.from(protoRel);
assertEquals(rel1, rel2);

// Make sure that the record types match I64, I16, I32 and then the I64 and I64 from the window
// functions.
assertEquals(rel2.getRecordType().fields(), Arrays.asList(R.I64, R.I16, R.I32, R.I64, R.I64));
}
}
Loading