-
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 4 commits
e54b4f2
033f093
f8abdcd
718053a
1eeed90
0d8c1b6
e0ca93b
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,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 { | ||||||
|
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.
Suggested change
|
||||||
|
|
||||||
| /** | ||||||
| * 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 | ||||||
|
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. |
||||||
| 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) | ||||||
| int firstVariadicArgIdx = Math.max(variadicBehavior.getMin() - 1, 0); | ||||||
| for (int i = firstVariadicArgIdx; i < argumentTypes.size() - 1; i++) { | ||||||
| Type currentType = argumentTypes.get(i); | ||||||
Adam-Alani marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| Type nextType = argumentTypes.get(i + 1); | ||||||
| // Normalize both types to nullable for comparison (ignoring nullability) | ||||||
| if (!io.substrait.type.TypeCreator.asNullable(currentType) | ||||||
| .equals(io.substrait.type.TypeCreator.asNullable(nextType))) { | ||||||
Adam-Alani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| 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", | ||||||
| i, currentType, i + 1, nextType)); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| 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. |
||
| } | ||
|
|
||
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.
Lets clarify this to say something other than "have different types". Maybe, "have any type satisfying the type constraints" or something?