Search in sources :

Example 1 with ScalarOperator

use of io.prestosql.spi.function.ScalarOperator 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 2 with ScalarOperator

use of io.prestosql.spi.function.ScalarOperator 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 3 with ScalarOperator

use of io.prestosql.spi.function.ScalarOperator 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 4 with ScalarOperator

use of io.prestosql.spi.function.ScalarOperator 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)

Example 5 with ScalarOperator

use of io.prestosql.spi.function.ScalarOperator in project hetu-core by openlookeng.

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 PrestoException(INVALID_CAST_ARGUMENT, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), VARCHAR), e);
    }
}
Also used : Slice(io.airlift.slice.Slice) 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) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

ScalarOperator (io.prestosql.spi.function.ScalarOperator)17 SqlType (io.prestosql.spi.function.SqlType)16 PrestoException (io.prestosql.spi.PrestoException)10 JsonParser (com.fasterxml.jackson.core.JsonParser)8 SqlNullable (io.prestosql.spi.function.SqlNullable)8 JsonCastException (io.prestosql.util.JsonCastException)8 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)8 IOException (java.io.IOException)8 ISOChronology (org.joda.time.chrono.ISOChronology)5 LiteralParameters (io.prestosql.spi.function.LiteralParameters)2 ScalarFunction (io.prestosql.spi.function.ScalarFunction)2 ImmutableList (com.google.common.collect.ImmutableList)1 Slice (io.airlift.slice.Slice)1 HyperLogLog (io.airlift.stats.cardinality.HyperLogLog)1 ScalarHeader (io.prestosql.operator.scalar.ScalarHeader)1 TimeZoneKey (io.prestosql.spi.type.TimeZoneKey)1 JsonUtil.currentTokenAsBoolean (io.prestosql.util.JsonUtil.currentTokenAsBoolean)1 JsonUtil.currentTokenAsDouble (io.prestosql.util.JsonUtil.currentTokenAsDouble)1