Search in sources :

Example 1 with JsonCastException

use of com.facebook.presto.util.JsonCastException in project presto by prestodb.

the class JsonToRowCast method toRow.

@UsedByGeneratedCode
public static Block toRow(RowType rowType, BlockBuilderAppender[] fieldAppenders, Optional<Map<String, Integer>> fieldNameToIndex, 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 && jsonParser.getCurrentToken() != START_OBJECT) {
            throw new JsonCastException(format("Expected a json array or object, but got %s", jsonParser.getText()));
        }
        BlockBuilder rowBlockBuilder = rowType.createBlockBuilder(null, 1);
        parseJsonToSingleRowBlock(jsonParser, (SingleRowBlockWriter) rowBlockBuilder.beginBlockEntry(), fieldAppenders, fieldNameToIndex);
        rowBlockBuilder.closeEntry();
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        return rowType.getObject(rowBlockBuilder, 0);
    } catch (PrestoException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", rowType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", rowType, truncateIfNecessaryForErrorMessage(json)), e);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) PrestoException(com.facebook.presto.spi.PrestoException) JsonCastException(com.facebook.presto.util.JsonCastException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 2 with JsonCastException

use of com.facebook.presto.util.JsonCastException in project presto by prestodb.

the class JsonOperators method castToInteger.

@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);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to INTEGER");
        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);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Example 3 with JsonCastException

use of com.facebook.presto.util.JsonCastException in project presto by prestodb.

the class JsonOperators method castToBoolean.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(BOOLEAN)
public static Boolean castToBoolean(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Boolean result = currentTokenAsBoolean(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BOOLEAN");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BOOLEAN), e);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.currentTokenAsBoolean(com.facebook.presto.util.JsonUtil.currentTokenAsBoolean) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Example 4 with JsonCastException

use of com.facebook.presto.util.JsonCastException in project presto by prestodb.

the class DecimalCasts method jsonToLongDecimal.

@UsedByGeneratedCode
public static Slice jsonToLongDecimal(Slice json, long precision, long scale, BigInteger tenToScale) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Slice result = currentTokenAsLongDecimal(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);
    }
}
Also used : Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 5 with JsonCastException

use of com.facebook.presto.util.JsonCastException in project presto by prestodb.

the class JsonToMapCast method toMap.

@UsedByGeneratedCode
public static Block toMap(MapType mapType, BlockBuilderAppender keyAppender, BlockBuilderAppender valueAppender, 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_OBJECT) {
            throw new JsonCastException(format("Expected a json object, but got %s", jsonParser.getText()));
        }
        BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1);
        BlockBuilder singleMapBlockBuilder = mapBlockBuilder.beginBlockEntry();
        HashTable hashTable = new HashTable(mapType.getKeyType(), singleMapBlockBuilder);
        int position = 0;
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            keyAppender.append(jsonParser, singleMapBlockBuilder);
            jsonParser.nextToken();
            valueAppender.append(jsonParser, singleMapBlockBuilder);
            // For example: CAST(JSON '{"1": 1, "01": 2}' AS MAP<INTEGER, INTEGER>).
            if (!hashTable.addIfAbsent(position)) {
                throw new JsonCastException("Duplicate keys are not allowed");
            }
            position += 2;
        }
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        mapBlockBuilder.closeEntry();
        return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
    } catch (PrestoException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", mapType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", mapType, truncateIfNecessaryForErrorMessage(json)), e);
    }
}
Also used : HashTable(com.facebook.presto.util.JsonUtil.HashTable) JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) PrestoException(com.facebook.presto.spi.PrestoException) JsonCastException(com.facebook.presto.util.JsonCastException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Aggregations

PrestoException (com.facebook.presto.spi.PrestoException)13 JsonCastException (com.facebook.presto.util.JsonCastException)13 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)13 JsonParser (com.fasterxml.jackson.core.JsonParser)13 IOException (java.io.IOException)10 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)8 SqlNullable (com.facebook.presto.spi.function.SqlNullable)8 SqlType (com.facebook.presto.spi.function.SqlType)8 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)5 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)3 Slice (io.airlift.slice.Slice)2 UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong (com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong)1 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)1 HashTable (com.facebook.presto.util.JsonUtil.HashTable)1 JsonUtil.currentTokenAsBoolean (com.facebook.presto.util.JsonUtil.currentTokenAsBoolean)1 JsonUtil.currentTokenAsDouble (com.facebook.presto.util.JsonUtil.currentTokenAsDouble)1 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)1