Search in sources :

Example 66 with ScalarFunction

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

the class JoniRegexpFunctions method regexpLike.

@Description("Returns whether the pattern is contained within the string")
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.BOOLEAN)
public static boolean regexpLike(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern) {
    Matcher matcher;
    int offset;
    if (source.hasByteArray()) {
        offset = source.byteArrayOffset();
        matcher = pattern.regex().matcher(source.byteArray(), offset, offset + source.length());
    } else {
        offset = 0;
        matcher = pattern.matcher(source.getBytes());
    }
    return matcher.search(offset, offset + source.length(), Option.DEFAULT) != -1;
}
Also used : Matcher(io.airlift.joni.Matcher) Constraint(io.trino.type.Constraint) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 67 with ScalarFunction

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

the class JsonFunctions method jsonArrayGet.

@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.JSON)
public static Slice jsonArrayGet(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BIGINT) long index) {
    // this value cannot be converted to positive number
    if (index == Long.MIN_VALUE) {
        return null;
    }
    try (JsonParser parser = createJsonParser(MAPPING_JSON_FACTORY, json)) {
        if (parser.nextToken() != START_ARRAY) {
            return null;
        }
        List<String> tokens = null;
        if (index < 0) {
            tokens = new LinkedList<>();
        }
        long count = 0;
        while (true) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                return null;
            }
            if (token == END_ARRAY) {
                if (tokens != null && count >= index * -1) {
                    return utf8Slice(tokens.get(0));
                }
                return null;
            }
            String arrayElement;
            if (token == START_OBJECT || token == START_ARRAY) {
                arrayElement = parser.readValueAsTree().toString();
            } else {
                arrayElement = parser.getValueAsString();
            }
            if (count == index) {
                return arrayElement == null ? null : utf8Slice(arrayElement);
            }
            if (tokens != null) {
                tokens.add(arrayElement);
                if (count >= index * -1) {
                    tokens.remove(0);
                }
            }
            count++;
        }
    } 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 68 with ScalarFunction

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

the class ColorFunctions method color.

@ScalarFunction
@LiteralParameters("x")
@SqlType(ColorType.NAME)
public static long color(@SqlType("varchar(x)") Slice color) {
    int rgb = parseRgb(color);
    if (rgb != -1) {
        return rgb;
    }
    // encode system colors (0-15) as negative values, offset by one
    try {
        SystemColor systemColor = SystemColor.valueOf(upper(color).toStringUtf8());
        int index = systemColor.getIndex();
        return -(index + 1);
    } catch (IllegalArgumentException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Invalid color: '%s'", color.toStringUtf8()), e);
    }
}
Also used : TrinoException(io.trino.spi.TrinoException) Constraint(io.trino.type.Constraint) ScalarFunction(io.trino.spi.function.ScalarFunction) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 69 with ScalarFunction

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

the class GeoFunctions method stSphericalLength.

@SqlNullable
@Description("Returns the great-circle length in meters of a linestring or multi-linestring on Earth's surface")
@ScalarFunction("ST_Length")
@SqlType(DOUBLE)
public static Double stSphericalLength(@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }
    validateSphericalType("ST_Length", geometry, EnumSet.of(LINE_STRING, MULTI_LINE_STRING));
    MultiPath lineString = (MultiPath) geometry.getEsriGeometry();
    double sum = 0;
    // sum up paths on (multi)linestring
    for (int path = 0; path < lineString.getPathCount(); path++) {
        if (lineString.getPathSize(path) < 2) {
            continue;
        }
        // sum up distances between adjacent points on this path
        int pathStart = lineString.getPathStart(path);
        Point previous = lineString.getPoint(pathStart);
        for (int i = pathStart + 1; i < lineString.getPathEnd(path); i++) {
            Point next = lineString.getPoint(i);
            sum += greatCircleDistance(previous.getY(), previous.getX(), next.getY(), next.getX());
            previous = next;
        }
    }
    return sum * 1000;
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) MultiPath(com.esri.core.geometry.MultiPath) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 70 with ScalarFunction

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

the class GeoFunctions method stWithin.

@SqlNullable
@Description("Returns TRUE if the geometry A is completely inside geometry B")
@ScalarFunction("ST_Within")
@SqlType(BOOLEAN)
public static Boolean stWithin(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right) {
    if (!envelopes(right, left, Envelope::contains)) {
        return false;
    }
    OGCGeometry leftGeometry = deserialize(left);
    OGCGeometry rightGeometry = deserialize(right);
    verifySameSpatialReference(leftGeometry, rightGeometry);
    return leftGeometry.within(rightGeometry);
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Aggregations

ScalarFunction (io.trino.spi.function.ScalarFunction)109 SqlType (io.trino.spi.function.SqlType)108 Description (io.trino.spi.function.Description)91 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)46 SqlNullable (io.trino.spi.function.SqlNullable)44 LiteralParameters (io.trino.spi.function.LiteralParameters)25 BlockBuilder (io.trino.spi.block.BlockBuilder)22 Constraint (io.trino.type.Constraint)21 Slice (io.airlift.slice.Slice)18 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 TrinoException (io.trino.spi.TrinoException)17 MultiPoint (com.esri.core.geometry.MultiPoint)15 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)10 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)9 Matcher (io.airlift.joni.Matcher)8 GeometryType (io.trino.geospatial.GeometryType)7 JsonParser (com.fasterxml.jackson.core.JsonParser)6 JsonToken (com.fasterxml.jackson.core.JsonToken)6 Envelope (com.esri.core.geometry.Envelope)5