-
Notifications
You must be signed in to change notification settings - Fork 93
feat: handle parameterConsistency option in YAML extensions #624
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
Adam-Alani
wants to merge
7
commits into
substrait-io:main
Choose a base branch
from
Adam-Alani:adam.alani/variadic-consistency
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.
+449
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e54b4f2
added parameterConsistency
Adam-Alani 033f093
fmt
Adam-Alani f8abdcd
applying comments
Adam-Alani 718053a
simplify to class
Adam-Alani 1eeed90
nit: comments
Adam-Alani 0d8c1b6
type comparison simplification
Adam-Alani e0ca93b
fix: lint and tests
Adam-Alani 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
99 changes: 99 additions & 0 deletions
99
core/src/main/java/io/substrait/expression/VariadicParameterConsistencyValidator.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,99 @@ | ||
| package io.substrait.expression; | ||
|
|
||
| import io.substrait.extension.SimpleExtension; | ||
| import io.substrait.type.Type; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Helper class for validating variadic parameter consistency in function invocations. Validates | ||
| * that when parameterConsistency is CONSISTENT, all variadic arguments have the same type (ignoring | ||
| * nullability). | ||
| */ | ||
| public class VariadicParameterConsistencyValidator { | ||
|
|
||
| /** | ||
| * Validates that variadic arguments satisfy the parameter consistency requirement. When | ||
| * CONSISTENT, all variadic arguments must have the same type (ignoring nullability). When | ||
| * INCONSISTENT, arguments can have different types. | ||
| * | ||
| * @param func the function declaration | ||
| * @param arguments the function arguments to validate | ||
| * @throws AssertionError if validation fails | ||
| */ | ||
| public static void validate(SimpleExtension.Function func, List<FunctionArg> arguments) { | ||
| Optional<SimpleExtension.VariadicBehavior> variadic = func.variadic(); | ||
| if (!variadic.isPresent()) { | ||
| return; | ||
| } | ||
|
|
||
| SimpleExtension.VariadicBehavior variadicBehavior = variadic.get(); | ||
| if (variadicBehavior.parameterConsistency() | ||
| != SimpleExtension.VariadicBehavior.ParameterConsistency.CONSISTENT) { | ||
| // INCONSISTENT allows different types, so validation passes | ||
| // TODO: Even when parameterConsistency is INCONSISTENT, there can be implicit constraints | ||
| // across variadic parameters due to type parameters. For example, consider a function with: | ||
| // args: [value: "decimal<P,S>", variadic: {min: 1, parameterConsistency: INCONSISTENT}] | ||
| // return: "decimal<38,S>" | ||
| // In this case, while the precision P can vary across variadic arguments, the scale S must | ||
| // be consistent across all variadic arguments (since it's used in the return type). The | ||
| // current implementation doesn't validate these type parameter constraints. According to | ||
| // the spec: "Each argument can be any possible concrete type afforded by the bounds of any | ||
| // parameter defined in the arguments specification." This means we need to check that type | ||
| // parameters that appear in the return type (or are otherwise constrained) are consistent | ||
| // across variadic arguments, even when parameterConsistency is INCONSISTENT. | ||
| return; | ||
| } | ||
|
|
||
| // Extract types from arguments (only Expression and Type have types, EnumArg doesn't) | ||
| List<Type> argumentTypes = | ||
| arguments.stream() | ||
| .filter(arg -> arg instanceof Expression || arg instanceof Type) | ||
| .map( | ||
| arg -> { | ||
| if (arg instanceof Expression) { | ||
| return ((Expression) arg).getType(); | ||
| } else { | ||
| return (Type) arg; | ||
| } | ||
| }) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| // Count how many Expression/Type arguments are in the fixed arguments (before variadic) | ||
| // Note: func.args() includes all argument types (Expression, Type, EnumArg), but we only | ||
| // care about Expression/Type arguments for type consistency checking | ||
| int fixedTypeArgCount = 0; | ||
| for (int i = 0; i < func.args().size() && i < arguments.size(); i++) { | ||
| FunctionArg arg = arguments.get(i); | ||
| if (arg instanceof Expression || arg instanceof Type) { | ||
| fixedTypeArgCount++; | ||
| } | ||
| } | ||
|
|
||
| if (argumentTypes.size() <= fixedTypeArgCount) { | ||
| // No variadic arguments, validation passes | ||
| return; | ||
| } | ||
|
|
||
| // For CONSISTENT, all variadic arguments must have the same type (ignoring nullability) | ||
| // Compare all variadic arguments to the first one for more informative error messages | ||
| // Variadic arguments start immediately after the fixed arguments | ||
| int firstVariadicArgIdx = fixedTypeArgCount; | ||
| if (firstVariadicArgIdx >= argumentTypes.size()) { | ||
| // Not enough variadic arguments provided, validation passes | ||
| return; | ||
| } | ||
| Type firstVariadicType = argumentTypes.get(firstVariadicArgIdx); | ||
| for (int i = firstVariadicArgIdx + 1; i < argumentTypes.size(); i++) { | ||
| Type currentType = argumentTypes.get(i); | ||
Adam-Alani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!firstVariadicType.equalsIgnoringNullability(currentType)) { | ||
| throw new AssertionError( | ||
| String.format( | ||
| "Variadic arguments must have consistent types when parameterConsistency is CONSISTENT. " | ||
| + "Argument at index %d has type %s but argument at index %d has type %s", | ||
| firstVariadicArgIdx, firstVariadicType, i, currentType)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -235,6 +235,7 @@ enum ParameterConsistency { | |
| INCONSISTENT | ||
| } | ||
|
|
||
| @Value.Default | ||
| default ParameterConsistency parameterConsistency() { | ||
| return ParameterConsistency.CONSISTENT; | ||
|
Member
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 think this is a reasonble default, because I think it's what most people expect in practice and it's also the first value in the enumeration. We can formalize this in the spec more concretely. |
||
| } | ||
|
|
||
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
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.
So unfortunately, this is going to be a bit more complicated than it initially seemed. From the docs:
Let me show you a (made-up) example:
This means that the following are valid:
inconsistent_sum(decimal<10, 2>, decimal<10, 2>, decimal<10, 2>)inconsistent_sum(decimal<10, 2>, decimal<10, 2>)inconsistent_sum(decimal<15, 8>, decimal<11, 8>, decimal<119, 8>)On the other hand, the following are all invalid:
inconsistent_sum(decimal<10, 2>, decimal<10, 3>, decimal<10, 4>)inconsistent_sum(decimal<10, 2>, i32)The above example is showing that there is the implicit constraint across the variadic parameters that the scale
Smust be the same as the output (and thus must all be the same across the variadic parameter). On the other hand, the precisionPis free to be anything. But all of the parameters do have to bedecimal.Not sure if that was instructive or not 😅
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.
For readability, it may make sense to have two private methods that implement each behavior, then have the public method just be a switch on the parameter consistency options. Just an idea!
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'll open a different issue for this and a follow up PR, and we can add a big TODO comment
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.
#633