-
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
6
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
| package io.substrait.expression; | ||
|
|
||
| import io.substrait.extension.SimpleExtension; | ||
| import io.substrait.type.Type; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * 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) { | ||
| java.util.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 | ||
| 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(java.util.stream.Collectors.toList()); | ||
|
|
||
| int fixedArgCount = func.args().size(); | ||
| if (argumentTypes.size() <= fixedArgCount) { | ||
| // 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 after the fixed arguments | ||
| int firstVariadicArgIdx = fixedArgCount + Math.max(variadicBehavior.getMin() - 1, 0); | ||
| 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.
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!