use of com.facebook.presto.util.JsonCastException in project presto by prestodb.
the class JsonOperators method castToDouble.
@ScalarOperator(CAST)
@SqlNullable
@SqlType(DOUBLE)
public static Double castToDouble(@SqlType(JSON) Slice json) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Double result = currentTokenAsDouble(parser);
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to DOUBLE");
return result;
} catch (IOException | JsonCastException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), DOUBLE), e);
}
}
use of com.facebook.presto.util.JsonCastException in project presto by prestodb.
the class JsonToArrayCast method toArray.
@UsedByGeneratedCode
public static Block toArray(ArrayType arrayType, BlockBuilderAppender elementAppender, SqlFunctionProperties properties, Slice json) {
try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
jsonParser.nextToken();
if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
return null;
}
if (jsonParser.getCurrentToken() != START_ARRAY) {
throw new JsonCastException(format("Expected a json array, but got %s", jsonParser.getText()));
}
BlockBuilder blockBuilder = arrayType.getElementType().createBlockBuilder(null, 20);
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
elementAppender.append(jsonParser, blockBuilder);
}
if (jsonParser.nextToken() != null) {
throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
}
return blockBuilder.build();
} catch (PrestoException | JsonCastException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", arrayType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
} catch (Exception e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", arrayType, truncateIfNecessaryForErrorMessage(json)), e);
}
}
use of com.facebook.presto.util.JsonCastException in project presto by prestodb.
the class DecimalCasts method jsonToShortDecimal.
@UsedByGeneratedCode
public static Long jsonToShortDecimal(Slice json, long precision, long scale, long tenToScale) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Long result = currentTokenAsShortDecimal(parser, intPrecision(precision), intScale(scale));
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to DECIMAL(%s,%s)", precision, scale);
return result;
} catch (IOException | NumberFormatException | JsonCastException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DECIMAL(%s,%s)", json.toStringUtf8(), precision, scale), e);
}
}
Aggregations