Repository navigation
fix(isthmus)!: preserve supported string function options #1390
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
Merged
nielspardon
merged 7 commits into
substrait-io:main
from
alexandrefimov:scalar-function-options-1173
Oct 9, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3880d2a
fix(isthmus)!: preserve concat null handling
alexandrefimov 50d3c0b
fix(isthmus)!: preserve supported string function options
alexandrefimov 15983c3
fix(isthmus): validate string options against the selected operator
alexandrefimov 4e90106
refactor(isthmus): share scalar option policies across both directions
alexandrefimov df6b505
style(isthmus): brace scalar option policy conditionals
alexandrefimov f97b88e
fix(isthmus): validate declared string preferences and defer initcap …
alexandrefimov 8fa8074
Merge main into scalar function options
alexandrefimov 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
20 changes: 20 additions & 0 deletions
20
isthmus/src/main/java/io/substrait/isthmus/expression/ScalarFunctionOptionPolicy.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,20 @@ | ||
| package io.substrait.isthmus.expression; | ||
|
|
||
| import io.substrait.expression.Expression; | ||
| import io.substrait.expression.FunctionOption; | ||
| import io.substrait.extension.SimpleExtension.ScalarFunctionVariant; | ||
| import java.util.List; | ||
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.sql.SqlOperator; | ||
|
|
||
| /** Option semantics for one family of scalar functions, in both conversion directions. */ | ||
| interface ScalarFunctionOptionPolicy { | ||
| SqlOperator resolve(Expression.ScalarFunctionInvocation expression, SqlOperator operator); | ||
|
|
||
| List<FunctionOption> forCall(RexCall call, ScalarFunctionVariant function); | ||
|
|
||
| /** Returns the operator whose signature also binds this call, such as an unchecked equivalent. */ | ||
| default SqlOperator signatureOperator(RexCall call) { | ||
| return call.getOperator(); | ||
| } | ||
| } |
140 changes: 140 additions & 0 deletions
140
isthmus/src/main/java/io/substrait/isthmus/expression/StringFunctionOptions.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,140 @@ | ||
| package io.substrait.isthmus.expression; | ||
|
|
||
| import io.substrait.expression.Expression; | ||
| import io.substrait.expression.FunctionOption; | ||
| import io.substrait.extension.DefaultExtensionCatalog; | ||
| import io.substrait.extension.SimpleExtension.ScalarFunctionVariant; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.sql.SqlOperator; | ||
| import org.apache.calcite.sql.fun.SqlLibraryOperators; | ||
| import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
|
|
||
| /** The spec v0.103.0 string options supported by the corresponding Calcite operators. */ | ||
| final class StringFunctionOptions implements ScalarFunctionOptionPolicy { | ||
| private static final Map<String, Binding> BINDINGS = | ||
| Map.of( | ||
| "concat", | ||
| new Binding( | ||
| "null_handling", | ||
| "ACCEPT_NULLS", | ||
| Set.of(SqlStdOperatorTable.CONCAT, SqlLibraryOperators.CONCAT_FUNCTION)), | ||
| "like", | ||
| new Binding("case_sensitivity", "CASE_SENSITIVE", Set.of(SqlStdOperatorTable.LIKE)), | ||
| "replace", | ||
| new Binding( | ||
| "case_sensitivity", "CASE_SENSITIVE", Set.of(SqlStdOperatorTable.REPLACE)), | ||
| "starts_with", | ||
| new Binding( | ||
| "case_sensitivity", "CASE_SENSITIVE", Set.of(SqlLibraryOperators.STARTS_WITH)), | ||
| "ends_with", | ||
| new Binding( | ||
| "case_sensitivity", "CASE_SENSITIVE", Set.of(SqlLibraryOperators.ENDS_WITH)), | ||
| "strpos", | ||
| new Binding( | ||
| "case_sensitivity", "CASE_SENSITIVE", Set.of(SqlStdOperatorTable.POSITION)), | ||
| "substring", | ||
| new Binding( | ||
| "negative_start", "LEFT_OF_BEGINNING", Set.of(SqlStdOperatorTable.SUBSTRING)), | ||
| "lower", new Binding("char_set", "UTF8", Set.of(SqlStdOperatorTable.LOWER)), | ||
| "upper", new Binding("char_set", "UTF8", Set.of(SqlStdOperatorTable.UPPER)), | ||
| "initcap", new Binding("char_set", null, Set.of(SqlStdOperatorTable.INITCAP))); | ||
|
|
||
| @Override | ||
| public List<FunctionOption> forCall(RexCall call, ScalarFunctionVariant function) { | ||
| Binding binding = binding(function); | ||
| if (binding == null | ||
| || binding.value() == null | ||
| || !binding.operators().contains(call.getOperator())) { | ||
| return List.of(); | ||
| } | ||
| // An omitted option lets a consumer choose any supported behavior. Emit the one | ||
| // the producer's Calcite operator actually implements instead of a YAML default. | ||
| return List.of( | ||
| FunctionOption.builder().name(binding.name()).addValues(binding.value()).build()); | ||
| } | ||
|
|
||
| @Override | ||
| public SqlOperator resolve(Expression.ScalarFunctionInvocation expression, SqlOperator operator) { | ||
| Binding binding = binding(expression.declaration()); | ||
| if (binding == null) { | ||
| return operator; | ||
| } | ||
| if (!expression.options().isEmpty() && !binding.operators().contains(operator)) { | ||
| throw new UnsupportedOperationException( | ||
| "No string option policy for Calcite operator " + operator.getName()); | ||
| } | ||
| for (FunctionOption option : expression.options()) { | ||
| if (!binding.name().equalsIgnoreCase(option.getName())) { | ||
| throw new UnsupportedOperationException( | ||
| "Unsupported " + expression.declaration().name() + " option: " + option.getName()); | ||
| } | ||
| for (String value : option.values()) { | ||
| boolean declared = | ||
| expression.declaration().options().entrySet().stream() | ||
| .filter(entry -> entry.getKey().equalsIgnoreCase(option.getName())) | ||
| .flatMap(entry -> entry.getValue().getValues().stream()) | ||
| .anyMatch(value::equalsIgnoreCase); | ||
| if (!declared) { | ||
| throw new UnsupportedOperationException( | ||
| expression.declaration().name() | ||
| + " option " | ||
| + option.getName() | ||
| + " does not declare value " | ||
| + value); | ||
| } | ||
| } | ||
| if (binding.value() == null) { | ||
| // The spec lists initcap's charsets without defining ASCII word boundaries. | ||
| throw new UnsupportedOperationException( | ||
| "No established Calcite initcap charset option policy"); | ||
| } | ||
| // Preferences name acceptable behaviors, in order. These operators implement one | ||
| // behavior each, so skip other preferences and reject when none is supported. | ||
| if (option.values().stream().noneMatch(binding.value()::equalsIgnoreCase)) { | ||
| throw new UnsupportedOperationException( | ||
| "Calcite " | ||
| + expression.declaration().name() | ||
| + " requires " | ||
| + binding.name() | ||
| + " " | ||
| + binding.value() | ||
| + "; preferences: " | ||
| + option.values()); | ||
| } | ||
| } | ||
| return operator; | ||
| } | ||
|
|
||
| private static Binding binding(ScalarFunctionVariant function) { | ||
| return DefaultExtensionCatalog.FUNCTIONS_STRING.equals(function.urn()) | ||
| ? BINDINGS.get(function.name()) | ||
| : null; | ||
| } | ||
|
|
||
| private static final class Binding { | ||
| private final String name; | ||
| private final String value; | ||
| private final Set<SqlOperator> operators; | ||
|
|
||
| private Binding(String name, String value, Set<SqlOperator> operators) { | ||
| this.name = name; | ||
| this.value = value; | ||
| this.operators = operators; | ||
| } | ||
|
|
||
| private String name() { | ||
| return name; | ||
| } | ||
|
|
||
| private String value() { | ||
| return value; | ||
| } | ||
|
|
||
| private Set<SqlOperator> operators() { | ||
| return operators; | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.