Search in sources :

Example 6 with JsonCastException

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

the class JsonToRowCast method toRow.

@UsedByGeneratedCode
public static Block toRow(RowType rowType, BlockBuilderAppender rowAppender, ConnectorSession connectorSession, Slice json) {
    try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
        jsonParser.nextToken();
        if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
            return null;
        }
        BlockBuilder rowBlockBuilder = rowType.createBlockBuilder(null, 1);
        rowAppender.append(jsonParser, rowBlockBuilder);
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        return rowType.getObject(rowBlockBuilder, 0);
    } catch (TrinoException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", rowType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", rowType, 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 7 with JsonCastException

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

the class JsonOperators method castToVarchar.

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

Example 8 with JsonCastException

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

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 TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), DOUBLE), e);
    }
}
Also used : JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException) JsonUtil.currentTokenAsDouble(io.trino.util.JsonUtil.currentTokenAsDouble) 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 9 with JsonCastException

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

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 TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), REAL), 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 10 with JsonCastException

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

the class JsonToArrayCast method toArray.

@UsedByGeneratedCode
public static Block toArray(ArrayType arrayType, BlockBuilderAppender arrayAppender, ConnectorSession connectorSession, Slice json) {
    try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
        jsonParser.nextToken();
        if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
            return null;
        }
        BlockBuilder blockBuilder = arrayType.createBlockBuilder(null, 1);
        arrayAppender.append(jsonParser, blockBuilder);
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        return arrayType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
    } catch (TrinoException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", arrayType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", arrayType, 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)

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