Search in sources :

Example 21 with SqlNullable

use of io.trino.spi.function.SqlNullable in project trino by trinodb.

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.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) SqlType(io.trino.spi.function.SqlType)

Example 22 with SqlNullable

use of io.trino.spi.function.SqlNullable in project trino by trinodb.

the class JsonFunctions method jsonArrayContains.

@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.DOUBLE) double value) {
    if (!Doubles.isFinite(value)) {
        return false;
    }
    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();
            // noinspection FloatingPointEquality
            if ((token == VALUE_NUMBER_FLOAT) && (parser.getDoubleValue() == value) && (Doubles.isFinite(parser.getDoubleValue()))) {
                return true;
            }
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) SqlType(io.trino.spi.function.SqlType)

Example 23 with SqlNullable

use of io.trino.spi.function.SqlNullable in project trino by trinodb.

the class JsonFunctions method jsonArrayContains.

@SqlNullable
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType("varchar(x)") Slice value) {
    String valueString = value.toStringUtf8();
    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_STRING && valueString.equals(parser.getValueAsString())) {
                return true;
            }
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 24 with SqlNullable

use of io.trino.spi.function.SqlNullable in project trino by trinodb.

the class JsonFunctions method jsonArrayLength.

@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BIGINT)
public static Long jsonArrayLength(@SqlType(StandardTypes.JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        if (parser.nextToken() != START_ARRAY) {
            return null;
        }
        long length = 0;
        while (true) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                return null;
            }
            if (token == END_ARRAY) {
                return length;
            }
            parser.skipChildren();
            length++;
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) SqlType(io.trino.spi.function.SqlType)

Example 25 with SqlNullable

use of io.trino.spi.function.SqlNullable 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)

Aggregations

SqlNullable (io.trino.spi.function.SqlNullable)56 SqlType (io.trino.spi.function.SqlType)56 ScalarFunction (io.trino.spi.function.ScalarFunction)44 Description (io.trino.spi.function.Description)38 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)32 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 JsonParser (com.fasterxml.jackson.core.JsonParser)14 TrinoException (io.trino.spi.TrinoException)14 JsonUtil.createJsonParser (io.trino.util.JsonUtil.createJsonParser)14 IOException (java.io.IOException)14 MultiPoint (com.esri.core.geometry.MultiPoint)12 Point (com.esri.core.geometry.Point)12 BlockBuilder (io.trino.spi.block.BlockBuilder)10 ScalarOperator (io.trino.spi.function.ScalarOperator)8 JsonCastException (io.trino.util.JsonCastException)8 LiteralParameters (io.trino.spi.function.LiteralParameters)7 JsonToken (com.fasterxml.jackson.core.JsonToken)6 MultiPath (com.esri.core.geometry.MultiPath)4 MultiVertexGeometry (com.esri.core.geometry.MultiVertexGeometry)4 Slice (io.airlift.slice.Slice)4