use of io.trino.spi.function.SqlNullable in project trino by trinodb.
the class GeoFunctions method lineInterpolatePoint.
@SqlNullable
@Description("Returns a Point interpolated along a LineString at the fraction given.")
@ScalarFunction("line_interpolate_point")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice lineInterpolatePoint(@SqlType(GEOMETRY_TYPE_NAME) Slice input, @SqlType(StandardTypes.DOUBLE) double distanceFraction) {
OGCGeometry geometry = deserialize(input);
if (geometry.isEmpty()) {
return null;
}
List<Point> interpolatedPoints = interpolatePoints(geometry, distanceFraction, false);
return serialize(createFromEsriGeometry(interpolatedPoints.get(0), null));
}
use of io.trino.spi.function.SqlNullable in project trino by trinodb.
the class GeoFunctions method invalidReason.
@Description("Returns the reason for why the input geometry is not valid. Returns null if the input is valid.")
@ScalarFunction("geometry_invalid_reason")
@SqlType(VARCHAR)
@SqlNullable
public static Slice invalidReason(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
GeometryCursor cursor = deserialize(input).getEsriGeometryCursor();
NonSimpleResult result = new NonSimpleResult();
while (true) {
com.esri.core.geometry.Geometry geometry = cursor.next();
if (geometry == null) {
return null;
}
if (!OperatorSimplifyOGC.local().isSimpleOGC(geometry, null, true, result, null)) {
String reasonText = NON_SIMPLE_REASONS.getOrDefault(result.m_reason, result.m_reason.name());
if (!(geometry instanceof MultiVertexGeometry)) {
return utf8Slice(reasonText);
}
MultiVertexGeometry multiVertexGeometry = (MultiVertexGeometry) geometry;
if (result.m_vertexIndex1 >= 0 && result.m_vertexIndex2 >= 0) {
Point point1 = multiVertexGeometry.getPoint(result.m_vertexIndex1);
Point point2 = multiVertexGeometry.getPoint(result.m_vertexIndex2);
return utf8Slice(format("%s at or near (%s %s) and (%s %s)", reasonText, point1.getX(), point1.getY(), point2.getX(), point2.getY()));
}
if (result.m_vertexIndex1 >= 0) {
Point point = multiVertexGeometry.getPoint(result.m_vertexIndex1);
return utf8Slice(format("%s at or near (%s %s)", reasonText, point.getX(), point.getY()));
}
return utf8Slice(reasonText);
}
}
}
use of io.trino.spi.function.SqlNullable in project trino by trinodb.
the class MapFromEntriesFunction method mapFromEntries.
@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,V)")
@SqlNullable
public Block mapFromEntries(@OperatorDependency(operator = EQUAL, argumentTypes = { "K", "K" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = NULLABLE_RETURN)) BlockPositionEqual keyEqual, @OperatorDependency(operator = HASH_CODE, argumentTypes = "K", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode keyHashCode, @TypeParameter("map(K,V)") MapType mapType, ConnectorSession session, @SqlType("array(row(K,V))") Block mapEntries) {
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
RowType mapEntryType = RowType.anonymous(ImmutableList.of(keyType, valueType));
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
int entryCount = mapEntries.getPositionCount();
BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder resultBuilder = mapBlockBuilder.beginBlockEntry();
TypedSet uniqueKeys = createEqualityTypedSet(keyType, keyEqual, keyHashCode, entryCount, "map_from_entries");
for (int i = 0; i < entryCount; i++) {
if (mapEntries.isNull(i)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
}
Block mapEntryBlock = mapEntryType.getObject(mapEntries, i);
if (mapEntryBlock.isNull(0)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
if (!uniqueKeys.add(mapEntryBlock, 0)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", keyType.getObjectValue(session, mapEntryBlock, 0)));
}
keyType.appendTo(mapEntryBlock, 0, resultBuilder);
valueType.appendTo(mapEntryBlock, 1, resultBuilder);
}
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of io.trino.spi.function.SqlNullable in project trino by trinodb.
the class JoniRegexpFunctions method regexpExtract.
@SqlNullable
@Description("Returns regex group of extracted string with a pattern")
@ScalarFunction
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice regexpExtract(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern, @SqlType(StandardTypes.BIGINT) long groupIndex) {
Matcher matcher = pattern.matcher(source.getBytes());
validateGroup(groupIndex, matcher.getEagerRegion());
int group = toIntExact(groupIndex);
int offset = matcher.search(0, source.length(), Option.DEFAULT);
if (offset == -1) {
return null;
}
Region region = matcher.getEagerRegion();
int beg = region.beg[group];
int end = region.end[group];
if (beg == -1) {
// end == -1 must be true
return null;
}
return source.slice(beg, end - beg);
}
use of io.trino.spi.function.SqlNullable in project trino by trinodb.
the class JsonFunctions method jsonArrayContains.
@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BIGINT) long value) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
if (parser.nextToken() != START_ARRAY) {
return null;
}
while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
return null;
}
if (token == END_ARRAY) {
return false;
}
parser.skipChildren();
if ((token == VALUE_NUMBER_INT) && ((parser.getNumberType() == NumberType.INT) || (parser.getNumberType() == NumberType.LONG)) && (parser.getLongValue() == value)) {
return true;
}
}
} catch (IOException e) {
return null;
}
}
Aggregations