Search in sources :

Example 1 with JsonCastException

use of io.prestosql.util.JsonCastException in project hetu-core by openlookeng.

the class JsonToArrayCast method toArray.

@UsedByGeneratedCode
public static Block toArray(ArrayType arrayType, BlockBuilderAppender elementAppender, ConnectorSession connectorSession, 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);
    }
}
Also used : JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) PrestoException(io.prestosql.spi.PrestoException) JsonCastException(io.prestosql.util.JsonCastException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) BlockBuilder(io.prestosql.spi.block.BlockBuilder) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 2 with JsonCastException

use of io.prestosql.util.JsonCastException in project hetu-core by openlookeng.

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, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), DOUBLE), e);
    }
}
Also used : JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) IOException(java.io.IOException) JsonUtil.currentTokenAsDouble(io.prestosql.util.JsonUtil.currentTokenAsDouble) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.prestosql.spi.function.ScalarOperator) SqlNullable(io.prestosql.spi.function.SqlNullable) SqlType(io.prestosql.spi.function.SqlType)

Example 3 with JsonCastException

use of io.prestosql.util.JsonCastException in project hetu-core by openlookeng.

the class JsonOperators method castToReal.

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

Example 4 with JsonCastException

use of io.prestosql.util.JsonCastException in project hetu-core by openlookeng.

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, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause());
        }
        throw e;
    } catch (ArithmeticException | IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e);
    }
}
Also used : JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.prestosql.spi.function.ScalarOperator) SqlNullable(io.prestosql.spi.function.SqlNullable) SqlType(io.prestosql.spi.function.SqlType)

Example 5 with JsonCastException

use of io.prestosql.util.JsonCastException in project hetu-core by openlookeng.

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, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), BOOLEAN), e);
    }
}
Also used : JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) IOException(java.io.IOException) JsonUtil.currentTokenAsBoolean(io.prestosql.util.JsonUtil.currentTokenAsBoolean) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.prestosql.spi.function.ScalarOperator) SqlNullable(io.prestosql.spi.function.SqlNullable) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

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