Search in sources :

Example 51 with Description

use of io.trino.spi.function.Description in project yauaa by nielsbasjes.

the class ParseUserAgentFunction method parseUserAgent.

@ScalarFunction("parse_user_agent")
@Description("Tries to parse and analyze the provided useragent string and extract as many attributes " + "as possible. Uses Yauaa (Yet Another UserAgent Analyzer) version " + Version.PROJECT_VERSION + ". " + "See https://yauaa.basjes.nl/udf/trino/ for documentation.")
@SqlType("map(varchar, varchar)")
public static Block parseUserAgent(@SqlType(StandardTypes.VARCHAR) Slice userAgentSlice) throws IllegalArgumentException {
    String userAgentStringToParse = null;
    if (userAgentSlice != null) {
        userAgentStringToParse = userAgentSlice.toStringUtf8();
    }
    UserAgentAnalyzer userAgentAnalyzer = threadLocalUserAgentAnalyzer.get();
    UserAgent userAgent = userAgentAnalyzer.parse(userAgentStringToParse);
    Map<String, String> resultMap = userAgent.toMap(userAgentAnalyzer.getAllPossibleFieldNamesSorted());
    MapType mapType = new MapType(VARCHAR, VARCHAR, new TypeOperators());
    BlockBuilder blockBuilder = mapType.createBlockBuilder(null, resultMap.size());
    BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry();
    for (Map.Entry<String, String> entry : resultMap.entrySet()) {
        VARCHAR.writeString(singleMapBlockBuilder, entry.getKey());
        VARCHAR.writeString(singleMapBlockBuilder, entry.getValue());
    }
    blockBuilder.closeEntry();
    return mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) UserAgent(nl.basjes.parse.useragent.UserAgent) Map(java.util.Map) MapType(io.trino.spi.type.MapType) TypeOperators(io.trino.spi.type.TypeOperators) BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 52 with Description

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

the class JoniRegexpFunctions method regexpSplit.

@ScalarFunction
@LiteralParameters("x")
@Description("Returns array of strings split by pattern")
@SqlType("array(varchar(x))")
public static Block regexpSplit(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern) {
    Matcher matcher = pattern.matcher(source.getBytes());
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
    int lastEnd = 0;
    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        nextStart = getNextStart(source, matcher);
        Slice slice = source.slice(lastEnd, matcher.getBegin() - lastEnd);
        lastEnd = matcher.getEnd();
        VARCHAR.writeSlice(blockBuilder, slice);
    }
    VARCHAR.writeSlice(blockBuilder, source.slice(lastEnd, source.length() - lastEnd));
    return blockBuilder.build();
}
Also used : Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) Constraint(io.trino.type.Constraint) BlockBuilder(io.trino.spi.block.BlockBuilder) 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 53 with Description

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

the class JoniRegexpFunctions method regexpCount.

@ScalarFunction
@Description("Returns the number of times that a pattern occurs in a string")
@LiteralParameters("x")
@SqlType(StandardTypes.BIGINT)
public static long regexpCount(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern) {
    Matcher matcher = pattern.matcher(source.getBytes());
    int count = 0;
    // Start from zero, implies the first byte
    int nextStart = 0;
    while (true) {
        // mather.search returns `source.length` if `nextStart` equals `source.length - 1`.
        // It should return -1 if `nextStart` is greater than `source.length - 1`.
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset < 0) {
            break;
        }
        nextStart = getNextStart(source, matcher);
        count++;
    }
    return count;
}
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 54 with Description

use of io.trino.spi.function.Description 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 55 with Description

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

Aggregations

Description (io.trino.spi.function.Description)93 ScalarFunction (io.trino.spi.function.ScalarFunction)91 SqlType (io.trino.spi.function.SqlType)91 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)46 SqlNullable (io.trino.spi.function.SqlNullable)38 LiteralParameters (io.trino.spi.function.LiteralParameters)19 Slice (io.airlift.slice.Slice)18 Constraint (io.trino.type.Constraint)18 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 BlockBuilder (io.trino.spi.block.BlockBuilder)17 MultiPoint (com.esri.core.geometry.MultiPoint)15 TrinoException (io.trino.spi.TrinoException)15 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)8 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)8 Matcher (io.airlift.joni.Matcher)7 GeometryType (io.trino.geospatial.GeometryType)7 Envelope (com.esri.core.geometry.Envelope)5 MultiPath (com.esri.core.geometry.MultiPath)5 MultiVertexGeometry (com.esri.core.geometry.MultiVertexGeometry)5