…opensearch-project#5660)
PPL queries that pass integer arithmetic as an argument to functions requiring Java int parameters fail when Calcite is enabled:
mvindex(arr, 1 + 1) -> CompileException: arrayItemOptional(List, long, ...)
left('abcdef', 1 + 1) -> Unable to implement: SqlFunctions.left(String, long)
round(123.456, 1 + 0) -> SqlFunctions.sround(BigDecimal, long)
Root cause: PPL widens INTEGER arithmetic to BIGINT for overflow safety (opensearch-project#5603), so expressions like `1 + 1` produce BIGINT. Many Calcite runtime methods (ITEM, LEFT, RIGHT, ROUND, TRUNCATE, SUBSTRING, CONV, SHA2, etc.) take Java int parameters. Since SqlTypeFamily.INTEGER contains BIGINT, the call passes type checking but fails at code generation because the JVM cannot auto-narrow long to int.
The bug surfaces on:
- Local execution: Calcite EnumerableCalc codegen -> Unable to implement
Fix:
- PPLFuncImpTable.resolve: for a known set of functions, narrow BIGINT arguments to INTEGER at the specific int-domain "control" positions (indices, lengths, precision, radix, bit-length, mode) via a per-function position map. Value/data positions are never narrowed, so e.g. round(bigint_value, 2) keeps its BIGINT first operand.
Overflow safety is preserved: the arithmetic itself still computes in BIGINT; only the final value handed to an int-domain parameter is narrowed. Arithmetic operators, comparisons, cast(x as long), aggregations, and long-field arithmetic are left untouched.
Also fixes the pre-existing case where an explicit cast(x as long) is passed to these functions.
Issue: opensearch-project#5660
Signed-off-by: Ajimelec Gonzalez <ajimelec@amazon.com>
Description
PPL queries that pass integer arithmetic as an argument to functions requiring Java
intparameters fail when Calcite is enabled:Root cause: PPL widens
INTEGERarithmetic toBIGINTfor overflow safety (#5603), so expressions like1 + 1produce BIGINT. Many Calcite runtime methods (ITEM,LEFT,RIGHT,ROUND,TRUNCATE,SUBSTRING,CONV,SHA2, etc.) take Javaintparameters. SinceSqlTypeFamily.INTEGERcontainsBIGINT, the call passes type checking but fails at code generation because the JVM cannot auto-narrowlongtoint.The bug surfaces on:
- Local execution: Calcite
EnumerableCalccodegen ->Unable to implementFix:
-
PPLFuncImpTable.resolve: for functions whose implementations requireint, narrowBIGINTarguments back toINTEGERat the plan layer.Overflow safety is preserved: the arithmetic itself still computes in
BIGINT; only the final value handed to an int-domain parameter is narrowed. Arithmetic operators, comparisons,cast(x as long), aggregations, and long-field arithmetic are left untouched.Also fixes the pre-existing case where an explicit
cast(x as long)is passed to these functions.Testing:
RelJsonSerializerTestcovering the serialization-layer type preservation.CalciteArrayFunctionIT,CalciteTextFunctionIT, andCalciteMathematicalFunctionITcoveringmvindex,left,right,substring,round,truncate,conv, andsha2witharithmetic and
cast(x as long)arguments.cast(x as long), and long-field arithmetic still return the correctBIGINTtypes.Related Issues
Resolves #5660
Related to #5603 (introduced the integer arithmetic widening that exposed this)
Check List
--signoffor-s.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.