use of io.trino.spi.function.ScalarFunction 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.ScalarFunction 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.ScalarFunction in project trino by trinodb.
the class GeoFunctions method stCentroid.
@Description("Returns the Point value that is the mathematical centroid of a Geometry")
@ScalarFunction("ST_Centroid")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stCentroid(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
OGCGeometry geometry = deserialize(input);
validateType("ST_Centroid", geometry, EnumSet.of(POINT, MULTI_POINT, LINE_STRING, MULTI_LINE_STRING, POLYGON, MULTI_POLYGON));
GeometryType geometryType = GeometryType.getForEsriGeometryType(geometry.geometryType());
if (geometryType == GeometryType.POINT) {
return input;
}
int pointCount = ((MultiVertexGeometry) geometry.getEsriGeometry()).getPointCount();
if (pointCount == 0) {
return serialize(createFromEsriGeometry(new Point(), geometry.getEsriSpatialReference()));
}
Point centroid;
switch(geometryType) {
case MULTI_POINT:
centroid = computePointsCentroid((MultiVertexGeometry) geometry.getEsriGeometry());
break;
case LINE_STRING:
case MULTI_LINE_STRING:
centroid = computeLineCentroid((Polyline) geometry.getEsriGeometry());
break;
case POLYGON:
centroid = computePolygonCentroid((Polygon) geometry.getEsriGeometry());
break;
case MULTI_POLYGON:
centroid = computeMultiPolygonCentroid((OGCMultiPolygon) geometry);
break;
default:
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Unexpected geometry type: " + geometryType);
}
return serialize(createFromEsriGeometry(centroid, geometry.getEsriSpatialReference()));
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class GeoFunctions method stSymmetricDifference.
@Description("Returns the Geometry value that represents the point set symmetric difference of two Geometries")
@ScalarFunction("ST_SymDifference")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stSymmetricDifference(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right) {
OGCGeometry leftGeometry = deserialize(left);
OGCGeometry rightGeometry = deserialize(right);
verifySameSpatialReference(leftGeometry, rightGeometry);
return serialize(leftGeometry.symDifference(rightGeometry));
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class BenchmarkArrayDistinct method oldArrayDistinct.
@ScalarFunction
@SqlType("array(varchar)")
public static Block oldArrayDistinct(@SqlType("array(varchar)") Block array) {
if (array.getPositionCount() == 0) {
return array;
}
TypedSet typedSet = createEqualityTypedSet(VARCHAR, EQUAL_OPERATOR, HASH_CODE_OPERATOR, array.getPositionCount(), "old_array_distinct");
BlockBuilder distinctElementBlockBuilder = VARCHAR.createBlockBuilder(null, array.getPositionCount());
for (int i = 0; i < array.getPositionCount(); i++) {
if (!typedSet.contains(array, i)) {
typedSet.add(array, i);
VARCHAR.appendTo(array, i, distinctElementBlockBuilder);
}
}
return distinctElementBlockBuilder.build();
}
Aggregations