-
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
Changes from all commits
e54b4f2
033f093
f8abdcd
718053a
1eeed90
0d8c1b6
e0ca93b
250626f
33462dc
724476e
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,96 @@ | ||
| 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). | ||
| */ | ||
| 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 any type satisfying the type constraints | ||
| * | ||
| * @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 | ||
benbellick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // TODO (#633): 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 nonVariadicArgCount = 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) { | ||
| nonVariadicArgCount++; | ||
| } | ||
| } | ||
|
|
||
| if (argumentTypes.size() <= nonVariadicArgCount) { | ||
| // 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 = nonVariadicArgCount; | ||
| 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)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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.