Search in sources :

Example 96 with ScalarFunction

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

the class VarbinaryFunctions method spookyHashV2_64.

@Description("Compute SpookyHashV2 64-bit hash")
@ScalarFunction
@SqlType(StandardTypes.VARBINARY)
public static Slice spookyHashV2_64(@SqlType(StandardTypes.VARBINARY) Slice slice) {
    Slice hash = Slices.allocate(Long.BYTES);
    hash.setLong(0, Long.reverseBytes(SpookyHashV2.hash64(slice, 0, slice.length(), 0)));
    return hash;
}
Also used : Slice(io.airlift.slice.Slice) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 97 with ScalarFunction

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

the class SetDigestFunctions method hashCounts.

@ScalarFunction
@SqlType("map(bigint,smallint)")
public static Block hashCounts(@TypeParameter("map(bigint,smallint)") Type mapType, @SqlType(SetDigestType.NAME) Slice slice) {
    SetDigest digest = SetDigest.newInstance(slice);
    // Maybe use static BlockBuilderStatus in order avoid `new`?
    BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 1);
    BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry();
    for (Map.Entry<Long, Short> entry : digest.getHashCounts().entrySet()) {
        BIGINT.writeLong(singleMapBlockBuilder, entry.getKey());
        SMALLINT.writeLong(singleMapBlockBuilder, entry.getValue());
    }
    blockBuilder.closeEntry();
    return (Block) mapType.getObject(blockBuilder, 0);
}
Also used : Block(io.trino.spi.block.Block) Map(java.util.Map) BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) SqlType(io.trino.spi.function.SqlType)

Example 98 with ScalarFunction

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

the class LikeFunctions method likeVarchar.

// TODO: this should not be callable from SQL
@ScalarFunction(value = "like", hidden = true)
@LiteralParameters("x")
@SqlType(StandardTypes.BOOLEAN)
public static boolean likeVarchar(@SqlType("varchar(x)") Slice value, @SqlType(LikePatternType.NAME) JoniRegexp pattern) {
    // Joni can infinite loop with UTF8Encoding when invalid UTF-8 is encountered.
    // NonStrictUTF8Encoding must be used to avoid this issue.
    Matcher matcher;
    int offset;
    if (value.hasByteArray()) {
        offset = value.byteArrayOffset();
        matcher = pattern.regex().matcher(value.byteArray(), offset, offset + value.length());
    } else {
        offset = 0;
        matcher = pattern.matcher(value.getBytes());
    }
    return matcher.match(offset, offset + value.length(), Option.NONE) != -1;
}
Also used : Matcher(io.airlift.joni.Matcher) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) ScalarFunction(io.trino.spi.function.ScalarFunction) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 99 with ScalarFunction

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

the class EncodedPolylineFunctions method toEncodedPolyline.

@Description("Encodes a linestring or multipoint geometry to a polyline")
@ScalarFunction("to_encoded_polyline")
@SqlType(StandardTypes.VARCHAR)
public static Slice toEncodedPolyline(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    validateType("encode_polyline", geometry, EnumSet.of(LINE_STRING, MULTI_POINT));
    GeometryType geometryType = GeometryType.getForEsriGeometryType(geometry.geometryType());
    switch(geometryType) {
        case LINE_STRING:
        case MULTI_POINT:
            return encodePolyline((MultiVertexGeometry) geometry.getEsriGeometry());
        default:
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Unexpected geometry type: " + geometryType);
    }
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryType(io.trino.geospatial.GeometryType) TrinoException(io.trino.spi.TrinoException) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 100 with ScalarFunction

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

the class GeoFunctions method stX.

@SqlNullable
@Description("Return the X coordinate of the point")
@ScalarFunction("ST_X")
@SqlType(DOUBLE)
public static Double stX(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    validateType("ST_X", geometry, EnumSet.of(POINT));
    if (geometry.isEmpty()) {
        return null;
    }
    return ((OGCPoint) geometry).X();
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) 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

ScalarFunction (io.trino.spi.function.ScalarFunction)108 SqlType (io.trino.spi.function.SqlType)107 Description (io.trino.spi.function.Description)90 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)21 Constraint (io.trino.type.Constraint)21 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 Slice (io.airlift.slice.Slice)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