-
Notifications
You must be signed in to change notification settings - Fork 95
feat(isthmus): observe scalar function types #1015
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
Open
alexandrefimov
wants to merge
1
commit into
substrait-io:main
Choose a base branch
from
alexandrefimov:issue-379-scalar-type-observer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+447
−2
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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
110 changes: 110 additions & 0 deletions
110
isthmus/src/main/java/io/substrait/isthmus/expression/TypeObservation.java
This file contains hidden or 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,110 @@ | ||
| package io.substrait.isthmus.expression; | ||
|
|
||
| import io.substrait.expression.Expression; | ||
| import io.substrait.type.Type; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
|
|
||
| /** | ||
| * The result of attempting to independently infer a Calcite type during Substrait-to-Calcite | ||
| * expression conversion. Exactly one of {@link #inferredType()} and {@link #inferenceFailure()} is | ||
| * present. | ||
| */ | ||
| public final class TypeObservation { | ||
| /** The expression category that produced an observation. */ | ||
| public enum Source { | ||
| /** A scalar function invocation. */ | ||
| SCALAR_FUNCTION | ||
| } | ||
|
|
||
| private final Source source; | ||
| private final Expression expression; | ||
| private final RelDataType inferredType; | ||
| private final RuntimeException inferenceFailure; | ||
|
|
||
| /** | ||
| * Creates a successful type observation. | ||
| * | ||
| * @param source expression category that produced the observation | ||
| * @param expression expression that produced the observation | ||
| * @param inferredType type independently inferred by Calcite | ||
| * @return a successful type observation | ||
| */ | ||
| static TypeObservation success(Source source, Expression expression, RelDataType inferredType) { | ||
| return new TypeObservation(source, expression, inferredType, null); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a failed type observation. | ||
| * | ||
| * @param source expression category that produced the observation | ||
| * @param expression expression that produced the observation | ||
| * @param inferenceFailure failure to independently infer a Calcite type | ||
| * @return a failed type observation | ||
| */ | ||
| static TypeObservation failure( | ||
| Source source, Expression expression, RuntimeException inferenceFailure) { | ||
| return new TypeObservation(source, expression, null, inferenceFailure); | ||
| } | ||
|
|
||
| private TypeObservation( | ||
| Source source, | ||
| Expression expression, | ||
| RelDataType inferredType, | ||
| RuntimeException inferenceFailure) { | ||
| this.source = Objects.requireNonNull(source, "source"); | ||
| this.expression = Objects.requireNonNull(expression, "expression"); | ||
| if ((inferredType == null) == (inferenceFailure == null)) { | ||
| throw new IllegalArgumentException( | ||
| "Exactly one of inferredType and inferenceFailure must be present"); | ||
| } | ||
| this.inferredType = inferredType; | ||
| this.inferenceFailure = inferenceFailure; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the expression category that produced this observation. | ||
| * | ||
| * @return the expression category | ||
| */ | ||
| public Source source() { | ||
| return source; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the expression that produced this observation. | ||
| * | ||
| * @return the observed expression | ||
| */ | ||
| public Expression expression() { | ||
| return expression; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the type supplied by Substrait. | ||
| * | ||
| * @return the supplied type | ||
| */ | ||
| public Type suppliedType() { | ||
| return expression.getType(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the type independently inferred by Calcite. | ||
| * | ||
| * @return the inferred type, or empty if inference failed | ||
| */ | ||
| public Optional<RelDataType> inferredType() { | ||
| return Optional.ofNullable(inferredType); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the Calcite inference failure. | ||
| * | ||
| * @return the inference failure, or empty if inference succeeded | ||
| */ | ||
| public Optional<RuntimeException> inferenceFailure() { | ||
| return Optional.ofNullable(inferenceFailure); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
isthmus/src/main/java/io/substrait/isthmus/expression/TypeObserver.java
This file contains hidden or 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,17 @@ | ||
| package io.substrait.isthmus.expression; | ||
|
|
||
| /** Receives type observations while converting Substrait expressions to Calcite. */ | ||
| @FunctionalInterface | ||
| public interface TypeObserver { | ||
| /** Observer that disables type inference and discards all observations. */ | ||
| TypeObserver NOOP = observation -> {}; | ||
|
|
||
| /** | ||
| * Receives the result of attempting to observe an expression's inferred type. | ||
| * | ||
| * <p>Exceptions thrown by an observer are propagated to the conversion caller. | ||
| * | ||
| * @param observation supplied type and either an inferred type or inference failure | ||
| */ | ||
| void observe(TypeObservation observation); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.