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;
}
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);
}
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;
}
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);
}
}
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();
}
Aggregations