@ScalarOperator(CAST) @SqlNullable @SqlType(TINYINT) public static Long castToTinyint(@SqlType(JSON) Slice json) { try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) { parser.nextToken(); Long result = currentTokenAsTinyint(parser); checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to TINYINT"); // check no trailing token return result; } catch (PrestoException e) { if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause()); } throw e; } catch (IllegalArgumentException | IOException | JsonCastException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), TINYINT), e); } }
@ScalarOperator(CAST) @SqlNullable @SqlType(SMALLINT) public static Long castToSmallint(@SqlType(JSON) Slice json) { try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) { parser.nextToken(); Long result = currentTokenAsSmallint(parser); checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to SMALLINT"); // check no trailing token return result; } catch (PrestoException e) { if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause()); } throw e; } catch (IllegalArgumentException | IOException | JsonCastException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), SMALLINT), e); } }
@ScalarOperator(CAST) @SqlNullable @SqlType(INTEGER) public static Long castToInteger(@SqlType(JSON) Slice json) { try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) { parser.nextToken(); Long result = currentTokenAsInteger(parser); checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to INTEGER"); // check no trailing token return result; } catch (PrestoException e) { if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause()); } throw e; } catch (ArithmeticException | IOException | JsonCastException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e); } }