Search in sources :

Example 16 with SqlNullable

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

Example 17 with SqlNullable

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);
        }
    }
}
Also used : GeometryCursor(com.esri.core.geometry.GeometryCursor) ListeningGeometryCursor(com.esri.core.geometry.ListeningGeometryCursor) MultiVertexGeometry(com.esri.core.geometry.MultiVertexGeometry) NonSimpleResult(com.esri.core.geometry.NonSimpleResult) OGCLineString(com.esri.core.geometry.ogc.OGCLineString) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 18 with SqlNullable

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);
}
Also used : Type(io.trino.spi.type.Type) SqlType(io.trino.spi.function.SqlType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) RowType(io.trino.spi.type.RowType) TypedSet(io.trino.operator.aggregation.TypedSet) TypedSet.createEqualityTypedSet(io.trino.operator.aggregation.TypedSet.createEqualityTypedSet) TrinoException(io.trino.spi.TrinoException) Block(io.trino.spi.block.Block) BlockBuilder(io.trino.spi.block.BlockBuilder) SqlNullable(io.trino.spi.function.SqlNullable) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 19 with SqlNullable

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);
}
Also used : Matcher(io.airlift.joni.Matcher) Region(io.airlift.joni.Region) Constraint(io.trino.type.Constraint) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 20 with SqlNullable

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;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) SqlType(io.trino.spi.function.SqlType)

Aggregations

SqlNullable (io.trino.spi.function.SqlNullable)56 SqlType (io.trino.spi.function.SqlType)56 ScalarFunction (io.trino.spi.function.ScalarFunction)44 Description (io.trino.spi.function.Description)38 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)32 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 JsonParser (com.fasterxml.jackson.core.JsonParser)14 TrinoException (io.trino.spi.TrinoException)14 JsonUtil.createJsonParser (io.trino.util.JsonUtil.createJsonParser)14 IOException (java.io.IOException)14 MultiPoint (com.esri.core.geometry.MultiPoint)12 Point (com.esri.core.geometry.Point)12 BlockBuilder (io.trino.spi.block.BlockBuilder)10 ScalarOperator (io.trino.spi.function.ScalarOperator)8 JsonCastException (io.trino.util.JsonCastException)8 LiteralParameters (io.trino.spi.function.LiteralParameters)7 JsonToken (com.fasterxml.jackson.core.JsonToken)6 MultiPath (com.esri.core.geometry.MultiPath)4 MultiVertexGeometry (com.esri.core.geometry.MultiVertexGeometry)4 Slice (io.airlift.slice.Slice)4