|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.calcite.utils; |
| 7 | + |
| 8 | +import lombok.experimental.UtilityClass; |
| 9 | +import org.apache.calcite.rel.type.RelDataType; |
| 10 | +import org.apache.calcite.sql.type.SqlTypeName; |
| 11 | +import org.apache.calcite.sql.type.SqlTypeUtil; |
| 12 | +import org.opensearch.sql.calcite.type.AbstractExprRelDataType; |
| 13 | +import org.opensearch.sql.data.type.ExprCoreType; |
| 14 | +import org.opensearch.sql.data.type.ExprType; |
| 15 | + |
| 16 | +/** |
| 17 | + * Utility methods for to derive types, containing special handling logics for user-defined-types. |
| 18 | + * |
| 19 | + * @see SqlTypeUtil utilities used during SQL validation or type derivation. |
| 20 | + */ |
| 21 | +@UtilityClass |
| 22 | +public class OpenSearchTypeUtil { |
| 23 | + /** |
| 24 | + * Whether a given RelDataType is a user-defined type (UDT) |
| 25 | + * |
| 26 | + * @param type the RelDataType to check |
| 27 | + * @return true if the type is a user-defined type, false otherwise |
| 28 | + */ |
| 29 | + public static boolean isUserDefinedType(RelDataType type) { |
| 30 | + return type instanceof AbstractExprRelDataType<?>; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Checks if the RelDataType represents a numeric type. Supports standard SQL numeric types |
| 35 | + * (INTEGER, BIGINT, SMALLINT, TINYINT, FLOAT, DOUBLE, DECIMAL, REAL), OpenSearch UDT numeric |
| 36 | + * types, and string types (VARCHAR, CHAR). |
| 37 | + * |
| 38 | + * @param fieldType the RelDataType to check |
| 39 | + * @return true if the type is numeric or string, false otherwise |
| 40 | + */ |
| 41 | + public static boolean isNumericOrCharacter(RelDataType fieldType) { |
| 42 | + // Check for OpenSearch UDT numeric types |
| 43 | + if (isUserDefinedType(fieldType)) { |
| 44 | + AbstractExprRelDataType<?> exprType = (AbstractExprRelDataType<?>) fieldType; |
| 45 | + ExprType udtType = exprType.getExprType(); |
| 46 | + return ExprCoreType.numberTypes().contains(udtType); |
| 47 | + } |
| 48 | + |
| 49 | + // Check standard SQL numeric types & string types (VARCHAR, CHAR) |
| 50 | + if (SqlTypeUtil.isNumeric(fieldType) || SqlTypeUtil.isCharacter(fieldType)) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Checks if the RelDataType represents a time-based field (timestamp, date, or time). Supports |
| 59 | + * both standard SQL time types (including TIMESTAMP, TIMESTAMP_WITH_LOCAL_TIME_ZONE, DATE, TIME, |
| 60 | + * and their timezone variants) and OpenSearch UDT time types. |
| 61 | + * |
| 62 | + * @param fieldType the RelDataType to check |
| 63 | + * @return true if the type is time-based, false otherwise |
| 64 | + */ |
| 65 | + public static boolean isDatetime(RelDataType fieldType) { |
| 66 | + // Check standard SQL time types |
| 67 | + if (SqlTypeUtil.isDatetime(fieldType)) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + // Check for OpenSearch UDT types (EXPR_TIMESTAMP mapped to VARCHAR) |
| 72 | + if (isUserDefinedType(fieldType)) { |
| 73 | + AbstractExprRelDataType<?> exprType = (AbstractExprRelDataType<?>) fieldType; |
| 74 | + ExprType udtType = exprType.getExprType(); |
| 75 | + return udtType == ExprCoreType.TIMESTAMP |
| 76 | + || udtType == ExprCoreType.DATE |
| 77 | + || udtType == ExprCoreType.TIME; |
| 78 | + } |
| 79 | + |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Checks whether a {@link RelDataType} represents a date type. |
| 85 | + * |
| 86 | + * <p>This method returns true for both Calcite's built-in {@link SqlTypeName#DATE} type and |
| 87 | + * OpenSearch's user-defined date type {@link OpenSearchTypeFactory.ExprUDT#EXPR_DATE}. |
| 88 | + * |
| 89 | + * @param type the type to check |
| 90 | + * @return true if the type is a date type (built-in or user-defined), false otherwise |
| 91 | + */ |
| 92 | + public static boolean isDate(RelDataType type) { |
| 93 | + if (isUserDefinedType(type)) { |
| 94 | + if (((AbstractExprRelDataType<?>) type).getUdt() == OpenSearchTypeFactory.ExprUDT.EXPR_DATE) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + } |
| 98 | + return SqlTypeName.DATE.equals(type.getSqlTypeName()); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Checks whether a {@link RelDataType} represents a timestamp type. |
| 103 | + * |
| 104 | + * <p>This method returns true for both Calcite's built-in {@link SqlTypeName#TIMESTAMP} type and |
| 105 | + * OpenSearch's user-defined timestamp type {@link OpenSearchTypeFactory.ExprUDT#EXPR_TIMESTAMP}. |
| 106 | + * |
| 107 | + * @param type the type to check |
| 108 | + * @return true if the type is a timestamp type (built-in or user-defined), false otherwise |
| 109 | + */ |
| 110 | + public static boolean isTimestamp(RelDataType type) { |
| 111 | + if (isUserDefinedType(type)) { |
| 112 | + if (((AbstractExprRelDataType<?>) type).getUdt() |
| 113 | + == OpenSearchTypeFactory.ExprUDT.EXPR_TIMESTAMP) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + } |
| 117 | + return SqlTypeName.TIMESTAMP.equals(type.getSqlTypeName()); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Checks whether a {@link RelDataType} represents a time type. |
| 122 | + * |
| 123 | + * <p>This method returns true for both Calcite's built-in {@link SqlTypeName#TIME} type and |
| 124 | + * OpenSearch's user-defined time type {@link OpenSearchTypeFactory.ExprUDT#EXPR_TIME}. |
| 125 | + * |
| 126 | + * @param type the type to check |
| 127 | + * @return true if the type is a time type (built-in or user-defined), false otherwise |
| 128 | + */ |
| 129 | + public static boolean isTime(RelDataType type) { |
| 130 | + if (isUserDefinedType(type)) { |
| 131 | + if (((AbstractExprRelDataType<?>) type).getUdt() == OpenSearchTypeFactory.ExprUDT.EXPR_TIME) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + } |
| 135 | + return SqlTypeName.TIME.equals(type.getSqlTypeName()); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * This method should be used in place for {@link SqlTypeUtil#isCharacter(RelDataType)} because |
| 140 | + * user-defined types also have VARCHAR as their SqlTypeName. |
| 141 | + */ |
| 142 | + public static boolean isCharacter(RelDataType type) { |
| 143 | + return !isUserDefinedType(type) && SqlTypeUtil.isCharacter(type); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Checks whether a {@link RelDataType} represents an IP address type. |
| 148 | + * |
| 149 | + * <p>This method returns true only for OpenSearch's user-defined IP type {@link |
| 150 | + * OpenSearchTypeFactory.ExprUDT#EXPR_IP}. |
| 151 | + * |
| 152 | + * @param type the type to check |
| 153 | + * @return true if the type is an IP address type, false otherwise |
| 154 | + */ |
| 155 | + public static boolean isIp(RelDataType type) { |
| 156 | + return isIp(type, false); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Checks whether a {@link RelDataType} represents an IP address type. If {@code acceptOther} is |
| 161 | + * set, {@link SqlTypeName#OTHER} is also accepted as an IP type. |
| 162 | + * |
| 163 | + * <p>{@link SqlTypeName#OTHER} is "borrowed" to represent IP type during validation because |
| 164 | + * <i>SqlTypeName.IP</i> does not exist |
| 165 | + * |
| 166 | + * @param type the type to check |
| 167 | + * @param acceptOther whether to accept OTHER as a valid IP type |
| 168 | + * @return true if the type is an IP address type, false otherwise |
| 169 | + */ |
| 170 | + public static boolean isIp(RelDataType type, boolean acceptOther) { |
| 171 | + if (isUserDefinedType(type)) { |
| 172 | + return ((AbstractExprRelDataType<?>) type).getUdt() == OpenSearchTypeFactory.ExprUDT.EXPR_IP; |
| 173 | + } |
| 174 | + if (acceptOther) { |
| 175 | + return type.getSqlTypeName() == SqlTypeName.OTHER; |
| 176 | + } |
| 177 | + return false; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Checks whether a {@link RelDataType} represents a binary type. |
| 182 | + * |
| 183 | + * <p>This method returns true for both Calcite's built-in binary types (BINARY, VARBINARY) and |
| 184 | + * OpenSearch's user-defined binary type {@link OpenSearchTypeFactory.ExprUDT#EXPR_BINARY}. |
| 185 | + * |
| 186 | + * @param type the type to check |
| 187 | + * @return true if the type is a binary type (built-in or user-defined), false otherwise |
| 188 | + */ |
| 189 | + public static boolean isBinary(RelDataType type) { |
| 190 | + if (isUserDefinedType(type)) { |
| 191 | + return ((AbstractExprRelDataType<?>) type).getUdt() |
| 192 | + == OpenSearchTypeFactory.ExprUDT.EXPR_BINARY; |
| 193 | + } |
| 194 | + return SqlTypeName.BINARY_TYPES.contains(type.getSqlTypeName()); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Checks whether a {@link RelDataType} represents a scalar type. |
| 199 | + * |
| 200 | + * <p>Scalar types include all primitive and atomic types such as numeric types (INTEGER, BIGINT, |
| 201 | + * FLOAT, DOUBLE, DECIMAL), string types (VARCHAR, CHAR), boolean, temporal types (DATE, TIME, |
| 202 | + * TIMESTAMP), and special scalar types (IP, BINARY, UUID). |
| 203 | + * |
| 204 | + * <p>This method returns false for composite types including: |
| 205 | + * |
| 206 | + * <ul> |
| 207 | + * <li>STRUCT types (structured records with named fields) |
| 208 | + * <li>MAP types (key-value pairs) |
| 209 | + * <li>ARRAY and MULTISET types (collections) |
| 210 | + * <li>ROW types (tuples) |
| 211 | + * </ul> |
| 212 | + * |
| 213 | + * @param type the type to check; may be null |
| 214 | + * @return true if the type is a scalar type, false if it is a composite type or null |
| 215 | + */ |
| 216 | + public static boolean isScalar(RelDataType type) { |
| 217 | + if (type == null) { |
| 218 | + return false; |
| 219 | + } |
| 220 | + return !type.isStruct() |
| 221 | + && !SqlTypeUtil.isMap(type) |
| 222 | + && !SqlTypeUtil.isCollection(type) |
| 223 | + && !SqlTypeUtil.isRow(type); |
| 224 | + } |
| 225 | +} |
0 commit comments