Search in sources :

Example 11 with JsonCastException

use of io.trino.util.JsonCastException in project trino by trinodb.

the class JsonToMapCast method toMap.

@UsedByGeneratedCode
public static Block toMap(MapType mapType, BlockBuilderAppender mapAppender, ConnectorSession connectorSession, Slice json) {
    try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
        jsonParser.nextToken();
        if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
            return null;
        }
        BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 1);
        mapAppender.append(jsonParser, blockBuilder);
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        return mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
    } catch (TrinoException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", mapType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", mapType, truncateIfNecessaryForErrorMessage(json)), e);
    }
}
Also used : JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 12 with JsonCastException

use of io.trino.util.JsonCastException in project trino by trinodb.

the class DecimalCasts method jsonToLongDecimal.

@UsedByGeneratedCode
public static Int128 jsonToLongDecimal(Slice json, long precision, long scale, Int128 tenToScale) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Int128 result = currentTokenAsLongDecimal(parser, intPrecision(precision), DecimalConversions.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 TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DECIMAL(%s,%s)", json.toStringUtf8(), precision, scale), e);
    }
}
Also used : JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException) Int128(io.trino.spi.type.Int128) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 13 with JsonCastException

use of io.trino.util.JsonCastException in project trino by trinodb.

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), DecimalConversions.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 TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DECIMAL(%s,%s)", json.toStringUtf8(), precision, scale), e);
    }
}
Also used : JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)13 TrinoException (io.trino.spi.TrinoException)13 JsonCastException (io.trino.util.JsonCastException)13 JsonUtil.createJsonParser (io.trino.util.JsonUtil.createJsonParser)13 IOException (java.io.IOException)10 ScalarOperator (io.trino.spi.function.ScalarOperator)8 SqlNullable (io.trino.spi.function.SqlNullable)8 SqlType (io.trino.spi.function.SqlType)8 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)5 BlockBuilder (io.trino.spi.block.BlockBuilder)3 Slice (io.airlift.slice.Slice)1 LiteralParameters (io.trino.spi.function.LiteralParameters)1 Int128 (io.trino.spi.type.Int128)1 JsonUtil.currentTokenAsBoolean (io.trino.util.JsonUtil.currentTokenAsBoolean)1 JsonUtil.currentTokenAsDouble (io.trino.util.JsonUtil.currentTokenAsDouble)1