|
15 | 15 |
|
16 | 16 | import com.facebook.presto.common.block.Block;
|
17 | 17 | import com.facebook.presto.common.type.StandardTypes;
|
| 18 | +import com.facebook.presto.spi.PrestoException; |
18 | 19 | import com.facebook.presto.spi.function.BlockIndex;
|
19 | 20 | import com.facebook.presto.spi.function.BlockPosition;
|
20 | 21 | import com.facebook.presto.spi.function.IsNull;
|
|
26 | 27 | import io.airlift.slice.XxHash64;
|
27 | 28 |
|
28 | 29 | import static com.facebook.presto.common.function.OperatorType.BETWEEN;
|
| 30 | +import static com.facebook.presto.common.function.OperatorType.CAST; |
29 | 31 | import static com.facebook.presto.common.function.OperatorType.EQUAL;
|
30 | 32 | import static com.facebook.presto.common.function.OperatorType.GREATER_THAN;
|
31 | 33 | import static com.facebook.presto.common.function.OperatorType.GREATER_THAN_OR_EQUAL;
|
|
37 | 39 | import static com.facebook.presto.common.function.OperatorType.NOT_EQUAL;
|
38 | 40 | import static com.facebook.presto.common.function.OperatorType.XX_HASH_64;
|
39 | 41 | import static com.facebook.presto.common.type.Chars.compareChars;
|
| 42 | +import static com.facebook.presto.spi.StandardErrorCode.INVALID_CAST_ARGUMENT; |
| 43 | +import static java.lang.String.format; |
40 | 44 |
|
41 | 45 | public final class CharOperators
|
42 | 46 | {
|
@@ -167,4 +171,82 @@ public static boolean indeterminate(@SqlType("char(x)") Slice value, @IsNull boo
|
167 | 171 | {
|
168 | 172 | return isNull;
|
169 | 173 | }
|
| 174 | + |
| 175 | + @LiteralParameters("x") |
| 176 | + @ScalarOperator(CAST) |
| 177 | + @SqlType(StandardTypes.DOUBLE) |
| 178 | + public static double castToDouble(@SqlType("char(x)") Slice slice) |
| 179 | + { |
| 180 | + try { |
| 181 | + return Double.parseDouble(slice.toStringUtf8()); |
| 182 | + } |
| 183 | + catch (Exception e) { |
| 184 | + throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DOUBLE", slice.toStringUtf8())); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @LiteralParameters("x") |
| 189 | + @ScalarOperator(CAST) |
| 190 | + @SqlType(StandardTypes.REAL) |
| 191 | + public static long castToFloat(@SqlType("char(x)") Slice slice) |
| 192 | + { |
| 193 | + try { |
| 194 | + return Float.floatToIntBits(Float.parseFloat(slice.toStringUtf8())); |
| 195 | + } |
| 196 | + catch (Exception e) { |
| 197 | + throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to REAL", slice.toStringUtf8())); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @LiteralParameters("x") |
| 202 | + @ScalarOperator(CAST) |
| 203 | + @SqlType(StandardTypes.BIGINT) |
| 204 | + public static long castToBigint(@SqlType("char(x)") Slice slice) |
| 205 | + { |
| 206 | + try { |
| 207 | + return Long.parseLong(slice.toStringUtf8().trim()); |
| 208 | + } |
| 209 | + catch (Exception e) { |
| 210 | + throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to BIGINT", slice.toStringUtf8())); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + @LiteralParameters("x") |
| 215 | + @ScalarOperator(CAST) |
| 216 | + @SqlType(StandardTypes.INTEGER) |
| 217 | + public static long castToInteger(@SqlType("char(x)") Slice slice) |
| 218 | + { |
| 219 | + try { |
| 220 | + return Integer.parseInt(slice.toStringUtf8().trim()); |
| 221 | + } |
| 222 | + catch (Exception e) { |
| 223 | + throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to INT", slice.toStringUtf8())); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + @LiteralParameters("x") |
| 228 | + @ScalarOperator(CAST) |
| 229 | + @SqlType(StandardTypes.SMALLINT) |
| 230 | + public static long castToSmallint(@SqlType("char(x)") Slice slice) |
| 231 | + { |
| 232 | + try { |
| 233 | + return Short.parseShort(slice.toStringUtf8().trim()); |
| 234 | + } |
| 235 | + catch (Exception e) { |
| 236 | + throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to SMALLINT", slice.toStringUtf8())); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + @LiteralParameters("x") |
| 241 | + @ScalarOperator(CAST) |
| 242 | + @SqlType(StandardTypes.TINYINT) |
| 243 | + public static long castToTinyint(@SqlType("char(x)") Slice slice) |
| 244 | + { |
| 245 | + try { |
| 246 | + return Byte.parseByte(slice.toStringUtf8().trim()); |
| 247 | + } |
| 248 | + catch (Exception e) { |
| 249 | + throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to TINYINT", slice.toStringUtf8())); |
| 250 | + } |
| 251 | + } |
170 | 252 | }
|
0 commit comments