fix: cast mvindex index arithmetic to INTEGER type for Calcite ITEM compatibility (#5660) - #5670
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
|
Hello @waterWang, thank you for the contribution! A few observations after testing this change:
Suggestion: The fix needs to happen in the serialization layer (RexStandardizer.java) rather than MVIndexFunctionImp.java. Specifically, in visitCall(), after operands are widened, the result type should be cast back to the call's original declared type when it's narrower than what widening produced. This keeps the widening intact for doc-value compatibility while preserving the type contract for consumers like ITEM. |
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