Search in sources :

Example 11 with ScalarFunction

use of io.prestosql.spi.function.ScalarFunction in project hetu-core by openlookeng.

the class TimestampWithTimeZoneOperators method castToDate.

@ScalarFunction("date")
@ScalarOperator(CAST)
@SqlType(StandardTypes.DATE)
public static long castToDate(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value) {
    // round down the current timestamp to days
    ISOChronology chronology = unpackChronology(value);
    long date = chronology.dayOfYear().roundFloor(unpackMillisUtc(value));
    // date is currently midnight in timezone of the original value
    // convert to UTC
    long millis = date + chronology.getZone().getOffset(date);
    return TimeUnit.MILLISECONDS.toDays(millis);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(io.prestosql.spi.function.ScalarOperator) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Example 12 with ScalarFunction

use of io.prestosql.spi.function.ScalarFunction in project hetu-core by openlookeng.

the class SetDigestFunctions method hashCounts.

@ScalarFunction
@SqlType("map(bigint,smallint)")
public static Block hashCounts(@TypeParameter("map<bigint,smallint>") Type mapType, @SqlType(SetDigestType.NAME) Slice slice) {
    SetDigest digest = SetDigest.newInstance(slice);
    // Maybe use static BlockBuilderStatus in order avoid `new`?
    BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 1);
    BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry();
    for (Map.Entry<Long, Short> entry : digest.getHashCounts().entrySet()) {
        BIGINT.writeLong(singleMapBlockBuilder, entry.getKey());
        SMALLINT.writeLong(singleMapBlockBuilder, entry.getValue());
    }
    blockBuilder.closeEntry();
    return (Block) mapType.getObject(blockBuilder, 0);
}
Also used : Block(io.prestosql.spi.block.Block) Map(java.util.Map) BlockBuilder(io.prestosql.spi.block.BlockBuilder) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Example 13 with ScalarFunction

use of io.prestosql.spi.function.ScalarFunction in project hetu-core by openlookeng.

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 = new TypedSet(VARCHAR, 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();
}
Also used : TypedSet(io.prestosql.operator.aggregation.TypedSet) BlockBuilder(io.prestosql.spi.block.BlockBuilder) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Example 14 with ScalarFunction

use of io.prestosql.spi.function.ScalarFunction in project hetu-core by openlookeng.

the class BenchmarkArraySort method oldArraySort.

@ScalarFunction
@SqlType("array(varchar)")
public static Block oldArraySort(@SqlType("array(varchar)") Block block) {
    List<Integer> positions = Ints.asList(new int[block.getPositionCount()]);
    for (int i = 0; i < block.getPositionCount(); i++) {
        positions.set(i, i);
    }
    positions.sort((p1, p2) -> {
        // TODO: This could be quite slow, it should use parametric equals
        return VARCHAR.compareTo(block, p1, block, p2);
    });
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, block.getPositionCount());
    for (int position : positions) {
        VARCHAR.appendTo(block, position, blockBuilder);
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.prestosql.spi.block.BlockBuilder) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Example 15 with ScalarFunction

use of io.prestosql.spi.function.ScalarFunction in project hetu-core by openlookeng.

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.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

ScalarFunction (io.prestosql.spi.function.ScalarFunction)97 SqlType (io.prestosql.spi.function.SqlType)96 Description (io.prestosql.spi.function.Description)76 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)40 SqlNullable (io.prestosql.spi.function.SqlNullable)40 LiteralParameters (io.prestosql.spi.function.LiteralParameters)20 Slice (io.airlift.slice.Slice)18 BlockBuilder (io.prestosql.spi.block.BlockBuilder)17 Constraint (io.prestosql.type.Constraint)16 Point (com.esri.core.geometry.Point)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 MultiPoint (com.esri.core.geometry.MultiPoint)12 PrestoException (io.prestosql.spi.PrestoException)12 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)8 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)8 JsonParser (com.fasterxml.jackson.core.JsonParser)7 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)7 IOException (java.io.IOException)7 JsonToken (com.fasterxml.jackson.core.JsonToken)6 GeometryType (io.prestosql.geospatial.GeometryType)6