-
Notifications
You must be signed in to change notification settings - Fork 74
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
feat: support output order remapping #132
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1aad1c1
refactor: expand access to SubstraitRelNodeConverter fields
vbarua 62cebe2
feat: add deriveRecordType to Cross
vbarua 5569dca
feat: substrait builder dsl
vbarua a360b00
feat: self-contained Substrait to Calcite converter
vbarua 9105d18
chore: bump junit-jupiter
vbarua 8fd4cd1
test: check for application of remappings
vbarua 9ef9fcd
feat: apply remaps
vbarua 0fa44ad
refactor: move RelOutputTest to SubstraitRelNodeConverterTest
vbarua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
288 changes: 288 additions & 0 deletions
288
core/src/main/java/io/substrait/dsl/SubstraitBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,288 @@ | ||
package io.substrait.dsl; | ||
|
||
import com.github.bsideup.jabel.Desugar; | ||
import io.substrait.expression.AggregateFunctionInvocation; | ||
import io.substrait.expression.Expression; | ||
import io.substrait.expression.FieldReference; | ||
import io.substrait.expression.ImmutableFieldReference; | ||
import io.substrait.function.SimpleExtension; | ||
import io.substrait.plan.ImmutableRoot; | ||
import io.substrait.plan.Plan; | ||
import io.substrait.proto.AggregateFunction; | ||
import io.substrait.relation.Aggregate; | ||
import io.substrait.relation.Cross; | ||
import io.substrait.relation.Fetch; | ||
import io.substrait.relation.Filter; | ||
import io.substrait.relation.Join; | ||
import io.substrait.relation.NamedScan; | ||
import io.substrait.relation.Project; | ||
import io.substrait.relation.Rel; | ||
import io.substrait.relation.Set; | ||
import io.substrait.relation.Sort; | ||
import io.substrait.type.NamedStruct; | ||
import io.substrait.type.Type; | ||
import io.substrait.type.TypeCreator; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class SubstraitBuilder { | ||
static final TypeCreator R = TypeCreator.of(false); | ||
static final TypeCreator N = TypeCreator.of(true); | ||
private final SimpleExtension.ExtensionCollection extensions; | ||
|
||
public SubstraitBuilder(SimpleExtension.ExtensionCollection extensions) { | ||
this.extensions = extensions; | ||
} | ||
|
||
// Relations | ||
public Aggregate aggregate( | ||
Function<Rel, Aggregate.Grouping> groupingFn, | ||
Function<Rel, List<AggregateFunctionInvocation>> measuresFn, | ||
Rel input) { | ||
Function<Rel, List<Aggregate.Grouping>> groupingsFn = | ||
groupingFn.andThen(g -> Stream.of(g).collect(Collectors.toList())); | ||
return aggregate(groupingsFn, measuresFn, Optional.empty(), input); | ||
} | ||
|
||
public Aggregate aggregate( | ||
Function<Rel, Aggregate.Grouping> groupingFn, | ||
Function<Rel, List<AggregateFunctionInvocation>> measuresFn, | ||
Rel.Remap remap, | ||
Rel input) { | ||
Function<Rel, List<Aggregate.Grouping>> groupingsFn = | ||
groupingFn.andThen(g -> Stream.of(g).collect(Collectors.toList())); | ||
return aggregate(groupingsFn, measuresFn, Optional.of(remap), input); | ||
} | ||
|
||
private Aggregate aggregate( | ||
Function<Rel, List<Aggregate.Grouping>> groupingsFn, | ||
Function<Rel, List<AggregateFunctionInvocation>> measuresFn, | ||
Optional<Rel.Remap> remap, | ||
Rel input) { | ||
var groupings = groupingsFn.apply(input); | ||
var measures = | ||
measuresFn.apply(input).stream() | ||
.map(m -> Aggregate.Measure.builder().function(m).build()) | ||
.collect(java.util.stream.Collectors.toList()); | ||
return Aggregate.builder() | ||
.groupings(groupings) | ||
.measures(measures) | ||
.remap(remap) | ||
.input(input) | ||
.build(); | ||
} | ||
|
||
public Cross cross(Rel left, Rel right) { | ||
return cross(left, right, Optional.empty()); | ||
} | ||
|
||
public Cross cross(Rel left, Rel right, Rel.Remap remap) { | ||
return cross(left, right, Optional.of(remap)); | ||
} | ||
|
||
private Cross cross(Rel left, Rel right, Optional<Rel.Remap> remap) { | ||
return Cross.builder().left(left).right(right).remap(remap).build(); | ||
} | ||
|
||
public Fetch fetch(long offset, long count, Rel input) { | ||
return fetch(offset, count, Optional.empty(), input); | ||
} | ||
|
||
public Fetch fetch(long offset, long count, Rel.Remap remap, Rel input) { | ||
return fetch(offset, count, Optional.of(remap), input); | ||
} | ||
|
||
private Fetch fetch(long offset, long count, Optional<Rel.Remap> remap, Rel input) { | ||
return Fetch.builder().offset(offset).count(count).input(input).remap(remap).build(); | ||
} | ||
|
||
public Filter filter(Function<Rel, Expression> conditionFn, Rel input) { | ||
return filter(conditionFn, Optional.empty(), input); | ||
} | ||
|
||
public Filter filter(Function<Rel, Expression> conditionFn, Rel.Remap remap, Rel input) { | ||
return filter(conditionFn, Optional.of(remap), input); | ||
} | ||
|
||
private Filter filter( | ||
Function<Rel, Expression> conditionFn, Optional<Rel.Remap> remap, Rel input) { | ||
var condition = conditionFn.apply(input); | ||
return Filter.builder().input(input).condition(condition).remap(remap).build(); | ||
} | ||
|
||
@Desugar | ||
public record JoinInput(Rel left, Rel right) {} | ||
|
||
public Join innerJoin(Function<JoinInput, Expression> conditionFn, Rel left, Rel right) { | ||
return join(conditionFn, Join.JoinType.INNER, left, right); | ||
} | ||
|
||
public Join innerJoin( | ||
Function<JoinInput, Expression> conditionFn, Rel.Remap remap, Rel left, Rel right) { | ||
return join(conditionFn, Join.JoinType.INNER, remap, left, right); | ||
} | ||
|
||
public Join join( | ||
Function<JoinInput, Expression> conditionFn, Join.JoinType joinType, Rel left, Rel right) { | ||
return join(conditionFn, joinType, Optional.empty(), left, right); | ||
} | ||
|
||
public Join join( | ||
Function<JoinInput, Expression> conditionFn, | ||
Join.JoinType joinType, | ||
Rel.Remap remap, | ||
Rel left, | ||
Rel right) { | ||
return join(conditionFn, joinType, Optional.of(remap), left, right); | ||
} | ||
|
||
private Join join( | ||
Function<JoinInput, Expression> conditionFn, | ||
Join.JoinType joinType, | ||
Optional<Rel.Remap> remap, | ||
Rel left, | ||
Rel right) { | ||
var condition = conditionFn.apply(new JoinInput(left, right)); | ||
return Join.builder() | ||
.left(left) | ||
.right(right) | ||
.condition(condition) | ||
.joinType(joinType) | ||
.remap(remap) | ||
.build(); | ||
} | ||
|
||
public NamedScan namedScan( | ||
Iterable<String> tableName, Iterable<String> columnNames, Iterable<Type> types) { | ||
return namedScan(tableName, columnNames, types, Optional.empty()); | ||
} | ||
|
||
public NamedScan namedScan( | ||
Iterable<String> tableName, | ||
Iterable<String> columnNames, | ||
Iterable<Type> types, | ||
Rel.Remap remap) { | ||
return namedScan(tableName, columnNames, types, Optional.of(remap)); | ||
} | ||
|
||
private NamedScan namedScan( | ||
Iterable<String> tableName, | ||
Iterable<String> columnNames, | ||
Iterable<Type> types, | ||
Optional<Rel.Remap> remap) { | ||
var struct = Type.Struct.builder().addAllFields(types).nullable(false).build(); | ||
var namedStruct = NamedStruct.of(columnNames, struct); | ||
return NamedScan.builder().names(tableName).initialSchema(namedStruct).remap(remap).build(); | ||
} | ||
|
||
public Project project(Function<Rel, Iterable<? extends Expression>> expressionsFn, Rel input) { | ||
return project(expressionsFn, Optional.empty(), input); | ||
} | ||
|
||
public Project project( | ||
Function<Rel, Iterable<? extends Expression>> expressionsFn, Rel.Remap remap, Rel input) { | ||
return project(expressionsFn, Optional.of(remap), input); | ||
} | ||
|
||
private Project project( | ||
Function<Rel, Iterable<? extends Expression>> expressionsFn, | ||
Optional<Rel.Remap> remap, | ||
Rel input) { | ||
var expressions = expressionsFn.apply(input); | ||
return Project.builder().input(input).expressions(expressions).remap(remap).build(); | ||
} | ||
|
||
public Set set(Set.SetOp op, Rel... inputs) { | ||
return set(op, Optional.empty(), inputs); | ||
} | ||
|
||
public Set set(Set.SetOp op, Rel.Remap remap, Rel... inputs) { | ||
return set(op, Optional.of(remap), inputs); | ||
} | ||
|
||
private Set set(Set.SetOp op, Optional<Rel.Remap> remap, Rel... inputs) { | ||
return Set.builder().setOp(op).remap(remap).addAllInputs(Arrays.asList(inputs)).build(); | ||
} | ||
|
||
public Sort sort(Function<Rel, Iterable<? extends Expression.SortField>> sortFieldFn, Rel input) { | ||
return sort(sortFieldFn, Optional.empty(), input); | ||
} | ||
|
||
public Sort sort( | ||
Function<Rel, Iterable<? extends Expression.SortField>> sortFieldFn, | ||
Rel.Remap remap, | ||
Rel input) { | ||
return sort(sortFieldFn, Optional.of(remap), input); | ||
} | ||
|
||
private Sort sort( | ||
Function<Rel, Iterable<? extends Expression.SortField>> sortFieldFn, | ||
Optional<Rel.Remap> remap, | ||
Rel input) { | ||
var condition = sortFieldFn.apply(input); | ||
return Sort.builder().input(input).sortFields(condition).remap(remap).build(); | ||
} | ||
|
||
// Expressions | ||
|
||
public Expression.BoolLiteral bool(boolean v) { | ||
return Expression.BoolLiteral.builder().value(v).build(); | ||
} | ||
|
||
public FieldReference fieldReference(Rel input, int index) { | ||
return ImmutableFieldReference.newInputRelReference(index, input); | ||
} | ||
|
||
public List<FieldReference> fieldReferences(Rel input, int... indexes) { | ||
return Arrays.stream(indexes) | ||
.mapToObj(index -> fieldReference(input, index)) | ||
.collect(java.util.stream.Collectors.toList()); | ||
} | ||
|
||
public List<Expression.SortField> sortFields(Rel input, int... indexes) { | ||
return Arrays.stream(indexes) | ||
.mapToObj( | ||
index -> | ||
Expression.SortField.builder() | ||
.expr(ImmutableFieldReference.newInputRelReference(index, input)) | ||
.direction(Expression.SortDirection.ASC_NULLS_LAST) | ||
.build()) | ||
.collect(java.util.stream.Collectors.toList()); | ||
} | ||
|
||
// Aggregate Functions | ||
|
||
public Aggregate.Grouping grouping(Rel input, int... indexes) { | ||
var columns = fieldReferences(input, indexes); | ||
return Aggregate.Grouping.builder().addAllExpressions(columns).build(); | ||
} | ||
|
||
public AggregateFunctionInvocation count(Rel input, int field) { | ||
var declaration = | ||
extensions.getAggregateFunction( | ||
SimpleExtension.FunctionAnchor.of("/functions_aggregate_generic.yaml", "count:any")); | ||
return AggregateFunctionInvocation.builder() | ||
.arguments(fieldReferences(input, field)) | ||
.outputType(R.I64) | ||
.declaration(declaration) | ||
.aggregationPhase(Expression.AggregationPhase.INITIAL_TO_RESULT) | ||
.invocation(AggregateFunction.AggregationInvocation.AGGREGATION_INVOCATION_ALL) | ||
.build(); | ||
} | ||
|
||
// Scalar Functions | ||
|
||
// Misc | ||
|
||
public Plan.Root root(Rel rel) { | ||
return ImmutableRoot.builder().input(rel).build(); | ||
} | ||
|
||
public Rel.Remap remap(Integer... fields) { | ||
return Rel.Remap.of(Arrays.asList(fields)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
package io.substrait.relation; | ||
|
||
import io.substrait.type.Type; | ||
import io.substrait.type.TypeCreator; | ||
import java.util.stream.Stream; | ||
import org.immutables.value.Value; | ||
|
||
@Value.Immutable | ||
public abstract class Cross extends BiRel { | ||
|
||
@Override | ||
protected Type.Struct deriveRecordType() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've realised that adding this removes the |
||
return TypeCreator.REQUIRED.struct( | ||
Stream.concat( | ||
getLeft().getRecordType().fields().stream(), | ||
getRight().getRecordType().fields().stream())); | ||
} | ||
|
||
@Override | ||
public <O, E extends Exception> O accept(RelVisitor<O, E> visitor) throws E { | ||
return visitor.visit(this); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See SubstraitRelNodeConverter for an example of how the methods in this class are used.
Could make this internal only as well to limit committing to an external API for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of moving towards an easier to write wrapper for generating plans, but any "usability" API would almost certainly need to come with proper docs, ideally containing short usage examples. An undocumented usability API, or one only documented by having to read the source code of other code, is going to be of limited use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This got merged before I circled back to hiding this.
Looking at https://substrait.io/spec/versioning/, because we're pre-1.0 I'm more comfortable with having it in. It is super useful for writing tests, both in substream-java and downstream.
100% something to have in place before 1.0.