You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When mvindex() is used in a PPL query with Calcite pushdown enabled, the index conversion +(index, 1) produces a BIGINT (long) value instead of INTEGER (int). The INTERNAL_ITEM operator expects SqlTypeFamily.INTEGER for the index parameter, causing a CompileException:
No applicable constructor/method found for actual parameters "java.util.List, long, int, boolean";
candidates are: "public static java.lang.Object org.apache.calcite.runtime.SqlFunctions.arrayItemOptional(java.util.List, int, int, boolean)"
Root Cause
The add and subtract methods in MVIndexFunctionImp.java use Calcite's SqlStdOperatorTable.PLUS/MINUS, which widen the result type to BIGINT when both operands are INTEGER. The INTERNAL_ITEM operator's type checker expects SqlTypeFamily.INTEGER, but receives BIGINT (part of SqlTypeFamily.NUMERIC).
Fix
Wrap the PLUS/MINUS results in a CAST(INTEGER) to ensure the index arithmetic always produces INTEGER-typed values for the ITEM/ARRAY_SLICE operators.
Testing
Existing CalcitePPLArrayFunctionTest covers mvindex with positive and negative indices
The fix ensures the +(1, 1) expression is cast to INTEGER before being passed to ITEM
The cast to INTEGER may cause overflow or data loss if the addition result exceeds INTEGER range. Consider validating that operands are within safe bounds before casting, or use a larger type like BIGINT if the domain allows values beyond INTEGER limits.
Why: While the suggestion correctly identifies the location and raises a valid concern about potential overflow, the context indicates this is for "internal, int-domain array-index math" as stated in the method comment. Array indices are typically within INTEGER range, making BIGINT unnecessary and potentially inconsistent with the intended design. The suggestion's improved code also doesn't match its own recommendation of "validating operands" but instead just changes the type.
Low
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
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.
Description
Fixes #5660
When
mvindex()is used in a PPL query with Calcite pushdown enabled, the index conversion+(index, 1)produces aBIGINT(long) value instead ofINTEGER(int). TheINTERNAL_ITEMoperator expectsSqlTypeFamily.INTEGERfor the index parameter, causing aCompileException:Root Cause
The
addandsubtractmethods inMVIndexFunctionImp.javause Calcite'sSqlStdOperatorTable.PLUS/MINUS, which widen the result type toBIGINTwhen both operands areINTEGER. TheINTERNAL_ITEMoperator's type checker expectsSqlTypeFamily.INTEGER, but receivesBIGINT(part ofSqlTypeFamily.NUMERIC).Fix
Wrap the
PLUS/MINUSresults in aCAST(INTEGER)to ensure the index arithmetic always producesINTEGER-typed values for theITEM/ARRAY_SLICEoperators.Testing
CalcitePPLArrayFunctionTestcoversmvindexwith positive and negative indices+(1, 1)expression is cast toINTEGERbefore being passed toITEM