Repository navigation
fix(isthmus)!: honor signed integer arithmetic options #1392
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 9 commits into
substrait-io:main
from
alexandrefimov:arithmetic-options-1173
Oct 9, 2026
Merged
Changes from all commits
Commits
Show all changes
9 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 b98bcb9
fix(isthmus)!: honor signed integer arithmetic options
alexandrefimov 4e90106
refactor(isthmus): share scalar option policies across both directions
alexandrefimov 9ae5c9a
Merge shared option policies and correct narrow integer overflow export
alexandrefimov 667c4ee
fix(isthmus): retain integer options after operand promotion
alexandrefimov 8e21205
Merge main and shared scalar option policies
alexandrefimov 8856a53
Merge main and address arithmetic option review
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
182 changes: 182 additions & 0 deletions
182
isthmus/src/main/java/io/substrait/isthmus/expression/IntegerFunctionOptions.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,182 @@ | ||
| 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.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.sql.SqlOperator; | ||
| import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
|
|
||
| /** Signed integer arithmetic options supported by Calcite. */ | ||
| final class IntegerFunctionOptions implements ScalarFunctionOptionPolicy { | ||
| @Override | ||
| public SqlOperator signatureOperator(RexCall call) { | ||
| return integerCall(call) ? unchecked(call.getOperator()) : call.getOperator(); | ||
| } | ||
|
|
||
| static SqlOperator unchecked(SqlOperator operator) { | ||
| if (operator == SqlStdOperatorTable.CHECKED_PLUS) { | ||
| return SqlStdOperatorTable.PLUS; | ||
| } | ||
| if (operator == SqlStdOperatorTable.CHECKED_MINUS) { | ||
| return SqlStdOperatorTable.MINUS; | ||
| } | ||
| if (operator == SqlStdOperatorTable.CHECKED_MULTIPLY) { | ||
| return SqlStdOperatorTable.MULTIPLY; | ||
| } | ||
| if (operator == SqlStdOperatorTable.CHECKED_DIVIDE) { | ||
| return SqlStdOperatorTable.DIVIDE; | ||
| } | ||
| return operator; | ||
| } | ||
|
|
||
| static boolean integerCall(RexCall call) { | ||
| return SqlTypeName.INT_TYPES.contains(call.getType().getSqlTypeName()) | ||
| && call.getOperands().stream() | ||
| .allMatch(arg -> SqlTypeName.INT_TYPES.contains(arg.getType().getSqlTypeName())); | ||
| } | ||
|
|
||
| private static Binding binding(ScalarFunctionVariant function) { | ||
| if (!DefaultExtensionCatalog.FUNCTIONS_ARITHMETIC.equals(function.urn())) { | ||
| return null; | ||
| } | ||
| String name = function.name(); | ||
| for (String type : List.of("i8", "i16", "i32", "i64")) { | ||
| if (!function.key().equals(name + ":" + type + "_" + type)) { | ||
| continue; | ||
| } | ||
| boolean narrow = type.equals("i8") || type.equals("i16"); | ||
| switch (name) { | ||
| case "add": | ||
| return new Binding( | ||
| SqlStdOperatorTable.PLUS, SqlStdOperatorTable.CHECKED_PLUS, narrow, false, false); | ||
| case "subtract": | ||
| return new Binding( | ||
| SqlStdOperatorTable.MINUS, SqlStdOperatorTable.CHECKED_MINUS, narrow, false, false); | ||
| case "multiply": | ||
| return new Binding( | ||
| SqlStdOperatorTable.MULTIPLY, | ||
| SqlStdOperatorTable.CHECKED_MULTIPLY, | ||
| narrow, | ||
| false, | ||
| false); | ||
| case "divide": | ||
| return new Binding( | ||
| SqlStdOperatorTable.DIVIDE, SqlStdOperatorTable.CHECKED_DIVIDE, narrow, true, false); | ||
| case "modulus": | ||
| return new Binding(SqlStdOperatorTable.MOD, SqlStdOperatorTable.MOD, false, false, true); | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public List<FunctionOption> forCall(RexCall call, ScalarFunctionVariant function) { | ||
| Binding binding = binding(function); | ||
| if (binding == null | ||
| || (call.getOperator() != binding.normal && call.getOperator() != binding.checked)) { | ||
| return List.of(); | ||
| } | ||
| List<FunctionOption> options = new ArrayList<>(); | ||
| // Export follows the actual Rex operator. Conformance-specific preparation may | ||
| // replace a plain operator with a checked equivalent before execution. | ||
| // Plain narrow arithmetic range-checks required results but wraps nullable ones. | ||
| // Planner nullability inference can change that choice, so do not promise a policy. | ||
| if (!binding.narrow || call.getOperator() == binding.checked) { | ||
| String overflow = | ||
|
nielspardon marked this conversation as resolved.
|
||
| call.getOperator() == binding.checked && !binding.modulus ? "ERROR" : "SILENT"; | ||
| options.add(option("overflow", overflow)); | ||
| } | ||
| if (binding.divide) { | ||
| options.add(option("on_domain_error", "ERROR")); | ||
| options.add(option("on_division_by_zero", "ERROR")); | ||
| } | ||
| if (binding.modulus) { | ||
| options.add(option("division_type", "TRUNCATE")); | ||
| options.add(option("on_domain_error", "ERROR")); | ||
| } | ||
| return options; | ||
| } | ||
|
|
||
| private static FunctionOption option(String name, String value) { | ||
| return FunctionOption.builder().name(name).addValues(value).build(); | ||
| } | ||
|
|
||
| @Override | ||
| public SqlOperator resolve(Expression.ScalarFunctionInvocation expression, SqlOperator operator) { | ||
| Binding binding = binding(expression.declaration()); | ||
| if (binding == null) { | ||
| return operator; | ||
| } | ||
| if (operator != binding.normal && operator != binding.checked) { | ||
| if (!expression.options().isEmpty()) { | ||
| throw new UnsupportedOperationException( | ||
| "No integer option policy for Calcite operator " + operator.getName()); | ||
| } | ||
| return operator; | ||
| } | ||
| Map<String, String> selected = new LinkedHashMap<>(); | ||
| for (FunctionOption option : expression.options()) { | ||
| ScalarFunctionOptionPolicy.requireDeclaredValues(expression, option); | ||
| String name = option.getName().toLowerCase(java.util.Locale.ROOT); | ||
| List<String> supported; | ||
| if (name.equals("overflow")) { | ||
| supported = binding.narrow ? List.of("ERROR") : List.of("SILENT", "ERROR"); | ||
| } else if ((binding.divide | ||
| && (name.equals("on_domain_error") || name.equals("on_division_by_zero"))) | ||
| || (binding.modulus && name.equals("on_domain_error"))) { | ||
| supported = List.of("ERROR"); | ||
| } else if (binding.modulus && name.equals("division_type")) { | ||
| supported = List.of("TRUNCATE"); | ||
| } else { | ||
| throw new UnsupportedOperationException("Unsupported integer arithmetic option: " + name); | ||
| } | ||
| String value = | ||
| option.values().stream() | ||
| .map(v -> v.toUpperCase(java.util.Locale.ROOT)) | ||
| .filter(supported::contains) | ||
| .findFirst() | ||
| .orElseThrow( | ||
| () -> | ||
| new UnsupportedOperationException( | ||
| "Unsupported integer arithmetic " | ||
| + name | ||
| + " preferences: " | ||
| + option.values())); | ||
| String previous = selected.putIfAbsent(name, value); | ||
| if (previous != null && !previous.equals(value)) { | ||
| throw new UnsupportedOperationException("Conflicting integer arithmetic option: " + name); | ||
| } | ||
| } | ||
| String overflow = selected.get("overflow"); | ||
| if (overflow == null) { | ||
| return operator; | ||
| } | ||
| return overflow.equals("ERROR") ? binding.checked : binding.normal; | ||
| } | ||
|
|
||
| private static final class Binding { | ||
| private final SqlOperator normal; | ||
| private final SqlOperator checked; | ||
| private final boolean narrow; | ||
| private final boolean divide; | ||
| private final boolean modulus; | ||
|
|
||
| private Binding( | ||
| SqlOperator normal, SqlOperator checked, boolean narrow, boolean divide, boolean modulus) { | ||
| this.normal = normal; | ||
| this.checked = checked; | ||
| this.narrow = narrow; | ||
| this.divide = divide; | ||
| this.modulus = modulus; | ||
| } | ||
| } | ||
| } | ||
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
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
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.