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