Search in sources :

Example 16 with SqlType

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

the class JsonFunctions method jsonArrayContains.

@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BOOLEAN) boolean value) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        if (parser.nextToken() != START_ARRAY) {
            return null;
        }
        while (true) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                return null;
            }
            if (token == END_ARRAY) {
                return false;
            }
            parser.skipChildren();
            if (((token == VALUE_TRUE) && value) || ((token == VALUE_FALSE) && (!value))) {
                return true;
            }
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Example 17 with SqlType

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

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

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

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

SqlType (io.prestosql.spi.function.SqlType)138 ScalarFunction (io.prestosql.spi.function.ScalarFunction)96 Description (io.prestosql.spi.function.Description)76 SqlNullable (io.prestosql.spi.function.SqlNullable)52 BlockBuilder (io.prestosql.spi.block.BlockBuilder)42 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)40 PrestoException (io.prestosql.spi.PrestoException)27 Slice (io.airlift.slice.Slice)25 LiteralParameters (io.prestosql.spi.function.LiteralParameters)24 TypeParameter (io.prestosql.spi.function.TypeParameter)20 ScalarOperator (io.prestosql.spi.function.ScalarOperator)16 Constraint (io.prestosql.type.Constraint)16 JsonParser (com.fasterxml.jackson.core.JsonParser)15 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)15 IOException (java.io.IOException)15 Point (com.esri.core.geometry.Point)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 MultiPoint (com.esri.core.geometry.MultiPoint)12 Block (io.prestosql.spi.block.Block)9 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)8