-
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
e54b4f2
033f093
f8abdcd
718053a
1eeed90
0d8c1b6
e0ca93b
250626f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||||||||||||||||||||||||||||||
Adam-Alani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * 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 | ||||||||||||||||||||||||||||||
|
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. 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: urn: "urn:example:extension"
scalar_functions:
- name: "inconsistent_sum"
impls:
- args:
- value: "decimal<P,S>"
variadic:
min: 1
parameterConsistency: INCONSISTENT
return: "decimal<38,S>"
This means that the following are valid:
On the other hand, the following are all invalid:
The above example is showing that there is the implicit constraint across the variadic parameters that the scale Not sure if that was instructive or not 😅
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. 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!
Author
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'll open a different issue for this and a follow up PR, and we can add a big TODO comment
Author
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.
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. You might as well put the ticket number in the comment as well. |
||||||||||||||||||||||||||||||
| // 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()); | ||||||||||||||||||||||||||||||
|
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 wonder if all of this filtering logic could be simplified if we just introduced an
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 its fine to leave this for now, but just making note of it. |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // 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; | ||||||||||||||||||||||||||||||
Adam-Alani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
| 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++; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| 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++; | |
| } | |
| } | |
| int fixedTypeArgCount = | |
| (int) | |
| arguments.stream() | |
| .limit(func.args().size()) | |
| .filter(arg -> arg instanceof Expression || arg instanceof Type) | |
| .count(); | |
Adam-Alani marked this conversation as resolved.
Show resolved
Hide resolved
| 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. |
||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.