Search in sources :

Example 1 with JsonCastException

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

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 (TrinoException e) {
        if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) {
            throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause());
        }
        throw e;
    } catch (ArithmeticException | IOException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), 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) ScalarOperator(io.trino.spi.function.ScalarOperator) SqlNullable(io.trino.spi.function.SqlNullable) SqlType(io.trino.spi.function.SqlType)

Example 2 with JsonCastException

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

the class JsonOperators method castToBigint.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(BIGINT)
public static Long castToBigint(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result = currentTokenAsBigint(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BIGINT");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BIGINT), 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) ScalarOperator(io.trino.spi.function.ScalarOperator) SqlNullable(io.trino.spi.function.SqlNullable) SqlType(io.trino.spi.function.SqlType)

Example 3 with JsonCastException

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

the class JsonOperators method castToTinyint.

@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);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to TINYINT");
        return result;
    } catch (TrinoException e) {
        if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) {
            throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause());
        }
        throw e;
    } catch (IllegalArgumentException | IOException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), TINYINT), 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) ScalarOperator(io.trino.spi.function.ScalarOperator) SqlNullable(io.trino.spi.function.SqlNullable) SqlType(io.trino.spi.function.SqlType)

Example 4 with JsonCastException

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

the class JsonOperators method castToSmallint.

@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);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to SMALLINT");
        return result;
    } catch (TrinoException e) {
        if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) {
            throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause());
        }
        throw e;
    } catch (IllegalArgumentException | IOException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), SMALLINT), 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) ScalarOperator(io.trino.spi.function.ScalarOperator) SqlNullable(io.trino.spi.function.SqlNullable) SqlType(io.trino.spi.function.SqlType)

Example 5 with JsonCastException

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

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

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