|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.expression.function.CollectionUDF; |
| 7 | + |
| 8 | +import static org.opensearch.sql.expression.function.BuiltinFunctionName.ADDFUNCTION; |
| 9 | +import static org.opensearch.sql.expression.function.BuiltinFunctionName.ARRAY_LENGTH; |
| 10 | +import static org.opensearch.sql.expression.function.BuiltinFunctionName.ARRAY_SLICE; |
| 11 | +import static org.opensearch.sql.expression.function.BuiltinFunctionName.IF; |
| 12 | +import static org.opensearch.sql.expression.function.BuiltinFunctionName.INTERNAL_ITEM; |
| 13 | +import static org.opensearch.sql.expression.function.BuiltinFunctionName.LESS; |
| 14 | +import static org.opensearch.sql.expression.function.BuiltinFunctionName.SUBTRACT; |
| 15 | + |
| 16 | +import java.math.BigDecimal; |
| 17 | +import org.apache.calcite.rex.RexBuilder; |
| 18 | +import org.apache.calcite.rex.RexNode; |
| 19 | +import org.opensearch.sql.expression.function.PPLFuncImpTable; |
| 20 | + |
| 21 | +/** |
| 22 | + * MVINDEX function implementation that returns a subset of a multivalue array. |
| 23 | + * |
| 24 | + * <p>Usage: |
| 25 | + * |
| 26 | + * <ul> |
| 27 | + * <li>mvindex(array, start) - returns single element at index (0-based) |
| 28 | + * <li>mvindex(array, start, end) - returns array slice from start to end (inclusive, 0-based) |
| 29 | + * </ul> |
| 30 | + * |
| 31 | + * <p>Supports negative indexing where -1 refers to the last element. |
| 32 | + * |
| 33 | + * <p>Implementation notes: |
| 34 | + * |
| 35 | + * <ul> |
| 36 | + * <li>Single element access uses Calcite's ITEM operator (1-based indexing) |
| 37 | + * <li>Range access uses Calcite's ARRAY_SLICE operator (0-based indexing with length parameter) |
| 38 | + * <li>Index conversion handles the difference between PPL's 0-based indexing and Calcite's |
| 39 | + * conventions |
| 40 | + * </ul> |
| 41 | + */ |
| 42 | +public class MVIndexFunctionImp implements PPLFuncImpTable.FunctionImp { |
| 43 | + |
| 44 | + @Override |
| 45 | + public RexNode resolve(RexBuilder builder, RexNode... args) { |
| 46 | + RexNode array = args[0]; |
| 47 | + RexNode startIdx = args[1]; |
| 48 | + |
| 49 | + // Use resolve to get array length instead of direct makeCall |
| 50 | + RexNode arrayLen = PPLFuncImpTable.INSTANCE.resolve(builder, ARRAY_LENGTH, array); |
| 51 | + |
| 52 | + if (args.length == 2) { |
| 53 | + // Single element access using ITEM (1-based indexing) |
| 54 | + return resolveSingleElement(builder, array, startIdx, arrayLen); |
| 55 | + } else { |
| 56 | + // Range access using ARRAY_SLICE (0-based indexing) |
| 57 | + RexNode endIdx = args[2]; |
| 58 | + return resolveRange(builder, array, startIdx, endIdx, arrayLen); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Resolves single element access: mvindex(array, index) |
| 64 | + * |
| 65 | + * <p>Uses Calcite's ITEM operator which uses 1-based indexing. Converts PPL's 0-based index to |
| 66 | + * 1-based by adding 1. |
| 67 | + */ |
| 68 | + private RexNode resolveSingleElement( |
| 69 | + RexBuilder builder, RexNode array, RexNode startIdx, RexNode arrayLen) { |
| 70 | + // Convert 0-based PPL index to 1-based Calcite ITEM index |
| 71 | + RexNode zero = builder.makeExactLiteral(BigDecimal.ZERO); |
| 72 | + RexNode one = builder.makeExactLiteral(BigDecimal.ONE); |
| 73 | + |
| 74 | + RexNode isNegative = PPLFuncImpTable.INSTANCE.resolve(builder, LESS, startIdx, zero); |
| 75 | + RexNode sumArrayLenStart = |
| 76 | + PPLFuncImpTable.INSTANCE.resolve(builder, ADDFUNCTION, arrayLen, startIdx); |
| 77 | + RexNode negativeCase = |
| 78 | + PPLFuncImpTable.INSTANCE.resolve(builder, ADDFUNCTION, sumArrayLenStart, one); |
| 79 | + RexNode positiveCase = PPLFuncImpTable.INSTANCE.resolve(builder, ADDFUNCTION, startIdx, one); |
| 80 | + |
| 81 | + RexNode normalizedStart = |
| 82 | + PPLFuncImpTable.INSTANCE.resolve(builder, IF, isNegative, negativeCase, positiveCase); |
| 83 | + |
| 84 | + return PPLFuncImpTable.INSTANCE.resolve(builder, INTERNAL_ITEM, array, normalizedStart); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Resolves range access: mvindex(array, start, end) |
| 89 | + * |
| 90 | + * <p>Uses Calcite's ARRAY_SLICE operator which uses 0-based indexing and a length parameter. |
| 91 | + * PPL's end index is inclusive, so length = (end - start) + 1. |
| 92 | + */ |
| 93 | + private RexNode resolveRange( |
| 94 | + RexBuilder builder, RexNode array, RexNode startIdx, RexNode endIdx, RexNode arrayLen) { |
| 95 | + // Normalize negative indices for ARRAY_SLICE (0-based) |
| 96 | + RexNode zero = builder.makeExactLiteral(BigDecimal.ZERO); |
| 97 | + RexNode one = builder.makeExactLiteral(BigDecimal.ONE); |
| 98 | + |
| 99 | + RexNode isStartNegative = PPLFuncImpTable.INSTANCE.resolve(builder, LESS, startIdx, zero); |
| 100 | + RexNode startNegativeCase = |
| 101 | + PPLFuncImpTable.INSTANCE.resolve(builder, ADDFUNCTION, arrayLen, startIdx); |
| 102 | + RexNode normalizedStart = |
| 103 | + PPLFuncImpTable.INSTANCE.resolve(builder, IF, isStartNegative, startNegativeCase, startIdx); |
| 104 | + |
| 105 | + RexNode isEndNegative = PPLFuncImpTable.INSTANCE.resolve(builder, LESS, endIdx, zero); |
| 106 | + RexNode endNegativeCase = |
| 107 | + PPLFuncImpTable.INSTANCE.resolve(builder, ADDFUNCTION, arrayLen, endIdx); |
| 108 | + RexNode normalizedEnd = |
| 109 | + PPLFuncImpTable.INSTANCE.resolve(builder, IF, isEndNegative, endNegativeCase, endIdx); |
| 110 | + |
| 111 | + // Calculate length: (normalizedEnd - normalizedStart) + 1 |
| 112 | + RexNode diff = |
| 113 | + PPLFuncImpTable.INSTANCE.resolve(builder, SUBTRACT, normalizedEnd, normalizedStart); |
| 114 | + RexNode length = PPLFuncImpTable.INSTANCE.resolve(builder, ADDFUNCTION, diff, one); |
| 115 | + |
| 116 | + // Call ARRAY_SLICE(array, normalizedStart, length) |
| 117 | + return PPLFuncImpTable.INSTANCE.resolve(builder, ARRAY_SLICE, array, normalizedStart, length); |
| 118 | + } |
| 119 | +} |
0 commit comments