Search in sources :

Example 36 with Description

use of io.trino.spi.function.Description 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()));
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryType(io.trino.geospatial.GeometryType) MultiVertexGeometry(com.esri.core.geometry.MultiVertexGeometry) OGCMultiPolygon(com.esri.core.geometry.ogc.OGCMultiPolygon) Polyline(com.esri.core.geometry.Polyline) TrinoException(io.trino.spi.TrinoException) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Polygon(com.esri.core.geometry.Polygon) OGCMultiPolygon(com.esri.core.geometry.ogc.OGCMultiPolygon) OGCPolygon(com.esri.core.geometry.ogc.OGCPolygon) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 37 with Description

use of io.trino.spi.function.Description 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));
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 38 with Description

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

the class MathFunctions method inverseBetaCdf.

@Description("Inverse of Beta cdf given a, b parameters and probability")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double inverseBetaCdf(@SqlType(StandardTypes.DOUBLE) double a, @SqlType(StandardTypes.DOUBLE) double b, @SqlType(StandardTypes.DOUBLE) double p) {
    checkCondition(p >= 0 && p <= 1, INVALID_FUNCTION_ARGUMENT, "p must be 0 >= p >= 1");
    checkCondition(a > 0 && b > 0, INVALID_FUNCTION_ARGUMENT, "a, b must be > 0");
    BetaDistribution distribution = new BetaDistribution(null, a, b, BetaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    return distribution.inverseCumulativeProbability(p);
}
Also used : BetaDistribution(org.apache.commons.math3.distribution.BetaDistribution) ScalarFunction(io.trino.spi.function.ScalarFunction) DecimalOperators.modulusScalarFunction(io.trino.type.DecimalOperators.modulusScalarFunction) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 39 with Description

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

the class MathFunctions method widthBucket.

@Description("The bucket number of a value given an array of bins")
@ScalarFunction("width_bucket")
@SqlType(StandardTypes.BIGINT)
public static long widthBucket(@SqlType(StandardTypes.DOUBLE) double operand, @SqlType("array(double)") Block bins) {
    int numberOfBins = bins.getPositionCount();
    checkCondition(numberOfBins > 0, INVALID_FUNCTION_ARGUMENT, "Bins cannot be an empty array");
    checkCondition(!isNaN(operand), INVALID_FUNCTION_ARGUMENT, "Operand cannot be NaN");
    int lower = 0;
    int upper = numberOfBins;
    int index;
    double bin;
    while (lower < upper) {
        if (DOUBLE.getDouble(bins, lower) > DOUBLE.getDouble(bins, upper - 1)) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Bin values are not sorted in ascending order");
        }
        index = (lower + upper) / 2;
        bin = DOUBLE.getDouble(bins, index);
        if (!isFinite(bin)) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Bin value must be finite, got " + bin);
        }
        if (operand < bin) {
            upper = index;
        } else {
            lower = index + 1;
        }
    }
    return lower;
}
Also used : TrinoException(io.trino.spi.TrinoException) Constraint(io.trino.type.Constraint) ScalarFunction(io.trino.spi.function.ScalarFunction) DecimalOperators.modulusScalarFunction(io.trino.type.DecimalOperators.modulusScalarFunction) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 40 with Description

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

the class JoniRegexpFunctions method regexpPosition.

@ScalarFunction
@Description("Returns the index of the n-th matched substring starting from the specified position")
@LiteralParameters("x")
@SqlType(StandardTypes.INTEGER)
public static long regexpPosition(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern, @SqlType(StandardTypes.INTEGER) long start, @SqlType(StandardTypes.INTEGER) long occurrence) {
    // start position cannot be smaller than 1
    if (start < 1) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "start position cannot be smaller than 1");
    }
    // occurrence cannot be smaller than 1
    if (occurrence < 1) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "occurrence cannot be smaller than 1");
    }
    // returns -1 if start is greater than the length of source
    if (start > SliceUtf8.countCodePoints(source)) {
        return -1;
    }
    Matcher matcher = pattern.matcher(source.getBytes());
    long count = 0;
    // convert char position to byte position
    // subtract 1 because codePointCount starts from zero
    int nextStart = SliceUtf8.offsetOfCodePoint(source, (int) start - 1);
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        // Check whether offset is negative, offset is -1 if no pattern was found or -2 if process was interrupted
        if (offset < 0) {
            return -1;
        }
        if (++count == occurrence) {
            // Plus 1 because position returned start from 1
            return SliceUtf8.countCodePoints(source, 0, matcher.getBegin()) + 1;
        }
        nextStart = getNextStart(source, matcher);
    }
}
Also used : Matcher(io.airlift.joni.Matcher) TrinoException(io.trino.spi.TrinoException) Constraint(io.trino.type.Constraint) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Aggregations

Description (io.trino.spi.function.Description)93 ScalarFunction (io.trino.spi.function.ScalarFunction)91 SqlType (io.trino.spi.function.SqlType)91 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)46 SqlNullable (io.trino.spi.function.SqlNullable)38 LiteralParameters (io.trino.spi.function.LiteralParameters)19 Slice (io.airlift.slice.Slice)18 Constraint (io.trino.type.Constraint)18 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 BlockBuilder (io.trino.spi.block.BlockBuilder)17 MultiPoint (com.esri.core.geometry.MultiPoint)15 TrinoException (io.trino.spi.TrinoException)15 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)8 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)8 Matcher (io.airlift.joni.Matcher)7 GeometryType (io.trino.geospatial.GeometryType)7 Envelope (com.esri.core.geometry.Envelope)5 MultiPath (com.esri.core.geometry.MultiPath)5 MultiVertexGeometry (com.esri.core.geometry.MultiVertexGeometry)5