|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.calcite.sql.fun; |
| 19 | + |
| 20 | +import org.apache.calcite.rel.type.RelDataType; |
| 21 | +import org.apache.calcite.rel.type.RelDataTypeFactory; |
| 22 | +import org.apache.calcite.rel.type.RelDataTypeField; |
| 23 | +import org.apache.calcite.sql.SqlCall; |
| 24 | +import org.apache.calcite.sql.SqlCallBinding; |
| 25 | +import org.apache.calcite.sql.SqlKind; |
| 26 | +import org.apache.calcite.sql.SqlNode; |
| 27 | +import org.apache.calcite.sql.SqlOperandCountRange; |
| 28 | +import org.apache.calcite.sql.SqlOperatorBinding; |
| 29 | +import org.apache.calcite.sql.SqlSpecialOperator; |
| 30 | +import org.apache.calcite.sql.SqlWriter; |
| 31 | +import org.apache.calcite.sql.parser.SqlParserPos; |
| 32 | +import org.apache.calcite.sql.type.OperandTypes; |
| 33 | +import org.apache.calcite.sql.type.SqlOperandCountRanges; |
| 34 | +import org.apache.calcite.sql.type.SqlSingleOperandTypeChecker; |
| 35 | +import org.apache.calcite.sql.type.SqlTypeFamily; |
| 36 | +import org.apache.calcite.sql.type.SqlTypeName; |
| 37 | +import org.apache.calcite.sql.type.SqlTypeUtil; |
| 38 | + |
| 39 | +import java.util.Arrays; |
| 40 | + |
| 41 | +import static java.util.Objects.requireNonNull; |
| 42 | +import static org.apache.calcite.sql.type.NonNullableAccessors.getComponentTypeOrThrow; |
| 43 | +import static org.apache.calcite.sql.validate.SqlNonNullableAccessors.getOperandLiteralValueOrThrow; |
| 44 | + |
| 45 | +/** |
| 46 | + * The item operator {@code [ ... ]}, used to access a given element of an array, map or struct. For |
| 47 | + * example, {@code myArray[3]}, {@code "myMap['foo']"}, {@code myStruct[2]} or {@code |
| 48 | + * myStruct['fieldName']}. |
| 49 | + * |
| 50 | + * <p>This class was copied over from Calcite 1.39.0 version to support access variant |
| 51 | + * (FLINK-37924). |
| 52 | + * |
| 53 | + * <p>Line 148 ~ 153, CALCITE-7325, should be removed after upgrading Calcite to 1.42.0. |
| 54 | + */ |
| 55 | +public class SqlItemOperator extends SqlSpecialOperator { |
| 56 | + public final int offset; |
| 57 | + public final boolean safe; |
| 58 | + |
| 59 | + public SqlItemOperator( |
| 60 | + String name, SqlSingleOperandTypeChecker operandTypeChecker, int offset, boolean safe) { |
| 61 | + super(name, SqlKind.ITEM, 100, true, null, null, operandTypeChecker); |
| 62 | + this.offset = offset; |
| 63 | + this.safe = safe; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public ReduceResult reduceExpr(int ordinal, TokenSequence list) { |
| 68 | + SqlNode left = list.node(ordinal - 1); |
| 69 | + SqlNode right = list.node(ordinal + 1); |
| 70 | + return new ReduceResult( |
| 71 | + ordinal - 1, |
| 72 | + ordinal + 2, |
| 73 | + createCall( |
| 74 | + SqlParserPos.sum( |
| 75 | + Arrays.asList( |
| 76 | + requireNonNull(left, "left").getParserPosition(), |
| 77 | + requireNonNull(right, "right").getParserPosition(), |
| 78 | + list.pos(ordinal))), |
| 79 | + left, |
| 80 | + right)); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { |
| 85 | + call.operand(0).unparse(writer, leftPrec, 0); |
| 86 | + final SqlWriter.Frame frame = writer.startList("[", "]"); |
| 87 | + if (!this.getName().equals("ITEM")) { |
| 88 | + final SqlWriter.Frame offsetFrame = writer.startFunCall(this.getName()); |
| 89 | + call.operand(1).unparse(writer, 0, 0); |
| 90 | + writer.endFunCall(offsetFrame); |
| 91 | + } else { |
| 92 | + call.operand(1).unparse(writer, 0, 0); |
| 93 | + } |
| 94 | + writer.endList(frame); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public SqlOperandCountRange getOperandCountRange() { |
| 99 | + return SqlOperandCountRanges.of(2); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) { |
| 104 | + final SqlNode left = callBinding.operand(0); |
| 105 | + final SqlNode right = callBinding.operand(1); |
| 106 | + if (!getOperandTypeChecker().checkSingleOperandType(callBinding, left, 0, throwOnFailure)) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + final SqlSingleOperandTypeChecker checker = getChecker(callBinding); |
| 110 | + return checker.checkSingleOperandType(callBinding, right, 0, throwOnFailure); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public SqlSingleOperandTypeChecker getOperandTypeChecker() { |
| 115 | + return (SqlSingleOperandTypeChecker) |
| 116 | + requireNonNull(super.getOperandTypeChecker(), "operandTypeChecker"); |
| 117 | + } |
| 118 | + |
| 119 | + private static SqlSingleOperandTypeChecker getChecker(SqlCallBinding callBinding) { |
| 120 | + final RelDataType operandType = callBinding.getOperandType(0); |
| 121 | + switch (operandType.getSqlTypeName()) { |
| 122 | + case ARRAY: |
| 123 | + return OperandTypes.family(SqlTypeFamily.INTEGER); |
| 124 | + case MAP: |
| 125 | + RelDataType keyType = |
| 126 | + requireNonNull(operandType.getKeyType(), "operandType.getKeyType()"); |
| 127 | + SqlTypeName sqlTypeName = keyType.getSqlTypeName(); |
| 128 | + return OperandTypes.family( |
| 129 | + requireNonNull( |
| 130 | + sqlTypeName.getFamily(), |
| 131 | + () -> |
| 132 | + "keyType.getSqlTypeName().getFamily() null, type is " |
| 133 | + + sqlTypeName)); |
| 134 | + case ROW: |
| 135 | + case ANY: |
| 136 | + case DYNAMIC_STAR: |
| 137 | + case VARIANT: |
| 138 | + return OperandTypes.family(SqlTypeFamily.INTEGER) |
| 139 | + .or(OperandTypes.family(SqlTypeFamily.CHARACTER)); |
| 140 | + default: |
| 141 | + throw callBinding.newValidationSignatureError(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public String getAllowedSignatures(String name) { |
| 147 | + if (name.equals("ITEM")) { |
| 148 | + // FLINK MODIFICATION BEGIN |
| 149 | + return "<ARRAY>[<INTEGER>]\n" |
| 150 | + + "<MAP>[<ANY>]\n" |
| 151 | + + "<ROW>[<CHARACTER>|<INTEGER>]\n" |
| 152 | + + "<VARIANT>[<CHARACTER>|<INTEGER>]"; |
| 153 | + // FLINK MODIFICATION END |
| 154 | + } else { |
| 155 | + return "<ARRAY>[" + name + "(<INTEGER>)]"; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public RelDataType inferReturnType(SqlOperatorBinding opBinding) { |
| 161 | + final RelDataTypeFactory typeFactory = opBinding.getTypeFactory(); |
| 162 | + final RelDataType operandType = opBinding.getOperandType(0); |
| 163 | + switch (operandType.getSqlTypeName()) { |
| 164 | + case VARIANT: |
| 165 | + // Return type is always nullable VARIANT |
| 166 | + return typeFactory.createTypeWithNullability(operandType, true); |
| 167 | + case ARRAY: |
| 168 | + return typeFactory.createTypeWithNullability( |
| 169 | + getComponentTypeOrThrow(operandType), true); |
| 170 | + case MAP: |
| 171 | + return typeFactory.createTypeWithNullability( |
| 172 | + requireNonNull( |
| 173 | + operandType.getValueType(), |
| 174 | + () -> "operandType.getValueType() is null for " + operandType), |
| 175 | + true); |
| 176 | + case ROW: |
| 177 | + RelDataType fieldType; |
| 178 | + RelDataType indexType = opBinding.getOperandType(1); |
| 179 | + |
| 180 | + if (SqlTypeUtil.isString(indexType)) { |
| 181 | + final String fieldName = |
| 182 | + getOperandLiteralValueOrThrow(opBinding, 1, String.class); |
| 183 | + RelDataTypeField field = operandType.getField(fieldName, false, false); |
| 184 | + if (field == null) { |
| 185 | + throw new AssertionError( |
| 186 | + "Cannot infer type of field '" |
| 187 | + + fieldName |
| 188 | + + "' within ROW type: " |
| 189 | + + operandType); |
| 190 | + } else { |
| 191 | + fieldType = field.getType(); |
| 192 | + } |
| 193 | + } else if (SqlTypeUtil.isIntType(indexType)) { |
| 194 | + Integer index = opBinding.getOperandLiteralValue(1, Integer.class); |
| 195 | + if (index == null || index < 1 || index > operandType.getFieldCount()) { |
| 196 | + throw new AssertionError( |
| 197 | + "Cannot infer type of field at position " |
| 198 | + + index |
| 199 | + + " within ROW type: " |
| 200 | + + operandType); |
| 201 | + } else { |
| 202 | + fieldType = |
| 203 | + operandType.getFieldList().get(index - 1).getType(); // 1 indexed |
| 204 | + } |
| 205 | + } else { |
| 206 | + throw new AssertionError( |
| 207 | + "Unsupported field identifier type: '" + indexType + "'"); |
| 208 | + } |
| 209 | + if (operandType.isNullable()) { |
| 210 | + fieldType = typeFactory.createTypeWithNullability(fieldType, true); |
| 211 | + } |
| 212 | + return fieldType; |
| 213 | + case ANY: |
| 214 | + case DYNAMIC_STAR: |
| 215 | + return typeFactory.createTypeWithNullability( |
| 216 | + typeFactory.createSqlType(SqlTypeName.ANY), true); |
| 217 | + default: |
| 218 | + throw new AssertionError(); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments