Search in sources :

Example 1 with Description

use of com.facebook.presto.spi.function.Description in project presto by prestodb.

the class DateTimeFunctions method currentDate.

@Description("current date")
@ScalarFunction
@SqlType(StandardTypes.DATE)
public static long currentDate(ConnectorSession session) {
    ISOChronology chronology = getChronology(session.getTimeZoneKey());
    // It is ok for this method to use the Object interfaces because it is constant folded during
    // plan optimization
    DateTime currentDateTime = new DateTime(session.getStartTime(), chronology).withTimeAtStartOfDay();
    DateTime baseDateTime = new DateTime(1970, 1, 1, 0, 0, chronology).withTimeAtStartOfDay();
    return Days.daysBetween(baseDateTime, currentDateTime).getDays();
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) DateTime(org.joda.time.DateTime) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 2 with Description

use of com.facebook.presto.spi.function.Description in project presto by prestodb.

the class StringFunctions method splitToMap.

@Description("creates a map using entryDelimiter and keyValueDelimiter")
@ScalarFunction
@SqlType("map<varchar,varchar>")
public static Block splitToMap(@SqlType(StandardTypes.VARCHAR) Slice string, @SqlType(StandardTypes.VARCHAR) Slice entryDelimiter, @SqlType(StandardTypes.VARCHAR) Slice keyValueDelimiter) {
    checkCondition(entryDelimiter.length() > 0, INVALID_FUNCTION_ARGUMENT, "entryDelimiter is empty");
    checkCondition(keyValueDelimiter.length() > 0, INVALID_FUNCTION_ARGUMENT, "keyValueDelimiter is empty");
    checkCondition(!entryDelimiter.equals(keyValueDelimiter), INVALID_FUNCTION_ARGUMENT, "entryDelimiter and keyValueDelimiter must not be the same");
    Map<Slice, Slice> map = new HashMap<>();
    int entryStart = 0;
    while (entryStart < string.length()) {
        // Extract key-value pair based on current index
        // then add the pair if it can be split by keyValueDelimiter
        Slice keyValuePair;
        int entryEnd = string.indexOf(entryDelimiter, entryStart);
        if (entryEnd >= 0) {
            keyValuePair = string.slice(entryStart, entryEnd - entryStart);
        } else {
            // The rest of the string is the last possible pair.
            keyValuePair = string.slice(entryStart, string.length() - entryStart);
        }
        int keyEnd = keyValuePair.indexOf(keyValueDelimiter);
        if (keyEnd >= 0) {
            int valueStart = keyEnd + keyValueDelimiter.length();
            Slice key = keyValuePair.slice(0, keyEnd);
            Slice value = keyValuePair.slice(valueStart, keyValuePair.length() - valueStart);
            if (value.indexOf(keyValueDelimiter) >= 0) {
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Key-value delimiter must appear exactly once in each entry. Bad input: '" + keyValuePair.toStringUtf8() + "'");
            }
            if (map.containsKey(key)) {
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", key.toStringUtf8()));
            }
            map.put(key, value);
        } else {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Key-value delimiter must appear exactly once in each entry. Bad input: '" + keyValuePair.toStringUtf8() + "'");
        }
        if (entryEnd < 0) {
            // No more pairs to add
            break;
        }
        // Next possible pair is placed next to the current entryDelimiter
        entryStart = entryEnd + entryDelimiter.length();
    }
    BlockBuilder builder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), map.size());
    for (Map.Entry<Slice, Slice> entry : map.entrySet()) {
        VARCHAR.writeSlice(builder, entry.getKey());
        VARCHAR.writeSlice(builder, entry.getValue());
    }
    return builder.build();
}
Also used : HashMap(java.util.HashMap) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) PrestoException(com.facebook.presto.spi.PrestoException) HashMap(java.util.HashMap) Map(java.util.Map) Constraint(com.facebook.presto.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 3 with Description

use of com.facebook.presto.spi.function.Description in project presto by prestodb.

the class GeoFunctions method stLength.

@Description("Returns the length of a LineString or Multi-LineString using Euclidean measurement on a 2D plane (based on spatial ref) in projected units")
@ScalarFunction("ST_Length")
@SqlType(DOUBLE)
public static double stLength(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    Geometry geometry = deserialize(input);
    validateType("ST_Length", geometry, EnumSet.of(LINE_STRING, MULTI_LINE_STRING));
    return geometry.getLength();
}
Also used : GeometryUtils.wktFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry) OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCGeometry.createFromEsriGeometry(com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryUtils.jsonFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 4 with Description

use of com.facebook.presto.spi.function.Description in project presto by prestodb.

the class GeoFunctions method stGeometries.

@SqlNullable
@Description("Returns an array of geometries in the specified collection")
@ScalarFunction("ST_Geometries")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block stGeometries(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    Geometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }
    GeometryType type = GeometryType.getForJtsGeometryType(geometry.getGeometryType());
    if (!type.isMultitype()) {
        BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 1);
        GEOMETRY.writeSlice(blockBuilder, serialize(geometry));
        return blockBuilder.build();
    }
    GeometryCollection collection = (GeometryCollection) geometry;
    BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, collection.getNumGeometries());
    for (int i = 0; i < collection.getNumGeometries(); i++) {
        GEOMETRY.writeSlice(blockBuilder, serialize(collection.getGeometryN(i)));
    }
    return blockBuilder.build();
}
Also used : GeometryUtils.wktFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry) OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCGeometry.createFromEsriGeometry(com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryUtils.jsonFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) OGCConcreteGeometryCollection(com.esri.core.geometry.ogc.OGCConcreteGeometryCollection) GeometryType(com.facebook.presto.geospatial.GeometryType) GeometryUtils.createJtsMultiPoint(com.facebook.presto.geospatial.GeometryUtils.createJtsMultiPoint) Point(com.esri.core.geometry.Point) GeometryUtils.createJtsEmptyPoint(com.facebook.presto.geospatial.GeometryUtils.createJtsEmptyPoint) GeometryUtils.createJtsPoint(com.facebook.presto.geospatial.GeometryUtils.createJtsPoint) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 5 with Description

use of com.facebook.presto.spi.function.Description in project presto by prestodb.

the class GeoFunctions method flattenGeometryCollections.

@Description("Recursively flattens GeometryCollections")
@ScalarFunction("flatten_geometry_collections")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block flattenGeometryCollections(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = EsriGeometrySerde.deserialize(input);
    List<OGCGeometry> components = Streams.stream(flattenCollection(geometry)).collect(toImmutableList());
    BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, components.size());
    for (OGCGeometry component : components) {
        GEOMETRY.writeSlice(blockBuilder, EsriGeometrySerde.serialize(component));
    }
    return blockBuilder.build();
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

Description (com.facebook.presto.spi.function.Description)105 SqlType (com.facebook.presto.spi.function.SqlType)103 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)101 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)41 SqlNullable (com.facebook.presto.spi.function.SqlNullable)37 OGCGeometry.createFromEsriGeometry (com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry)20 GeometryUtils.jsonFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry)20 GeometryUtils.wktFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry)20 Geometry (org.locationtech.jts.geom.Geometry)20 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)19 PrestoException (com.facebook.presto.spi.PrestoException)18 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)15 Slice (io.airlift.slice.Slice)15 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)14 Constraint (com.facebook.presto.type.Constraint)14 DecimalOperators.modulusScalarFunction (com.facebook.presto.type.DecimalOperators.modulusScalarFunction)13 Point (com.esri.core.geometry.Point)10 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 Envelope (com.esri.core.geometry.Envelope)6