Search in sources :

Example 96 with SqlType

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

the class DateTimeFunctions method addFieldValueTimeWithTimeZone.

@Description("add the specified amount of time to the given time")
@LiteralParameters("x")
@ScalarFunction("date_add")
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long addFieldValueTimeWithTimeZone(@SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone) {
    ISOChronology chronology = unpackChronology(timeWithTimeZone);
    long millis = modulo24Hour(chronology, getTimeField(chronology, unit).add(unpackMillisUtc(timeWithTimeZone), toIntExact(value)));
    return updateMillisUtc(millis, timeWithTimeZone);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 97 with SqlType

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

the class DateTimeFunctions method fromISO8601Date.

@ScalarFunction("from_iso8601_date")
@LiteralParameters("x")
@SqlType(StandardTypes.DATE)
public static long fromISO8601Date(ConnectorSession session, @SqlType("varchar(x)") Slice iso8601DateTime) {
    DateTimeFormatter formatter = ISODateTimeFormat.dateElementParser().withChronology(UTC_CHRONOLOGY);
    DateTime dateTime = parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8());
    return MILLISECONDS.toDays(dateTime.getMillis());
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime) ScalarFunction(io.prestosql.spi.function.ScalarFunction) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 98 with SqlType

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

the class ArrayRemoveFunction method remove.

@TypeParameter("E")
@SqlType("array(E)")
public Block remove(@OperatorDependency(operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle equalsFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block array, @SqlType("E") Object value) {
    List<Integer> positions = new ArrayList<>();
    for (int i = 0; i < array.getPositionCount(); i++) {
        Object element = readNativeValue(type, array, i);
        try {
            if (element == null) {
                positions.add(i);
                continue;
            }
            Boolean result = (Boolean) equalsFunction.invoke(element, value);
            if (result == null) {
                throw new PrestoException(NOT_SUPPORTED, "array_remove does not support arrays with elements that are null or contain null");
            }
            if (!result) {
                positions.add(i);
            }
        } catch (Throwable t) {
            throw internalError(t);
        }
    }
    if (array.getPositionCount() == positions.size()) {
        return array;
    }
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    for (int position : positions) {
        type.appendTo(array, position, blockBuilder);
    }
    pageBuilder.declarePositions(positions.size());
    return blockBuilder.getRegion(blockBuilder.getPositionCount() - positions.size(), positions.size());
}
Also used : ArrayList(java.util.ArrayList) PrestoException(io.prestosql.spi.PrestoException) BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 99 with SqlType

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

the class ArraySortFunction method sort.

@TypeParameter("E")
@SqlType("array(E)")
public Block sort(@OperatorDependency(operator = LESS_THAN, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle lessThanFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block block) {
    int arrayLength = block.getPositionCount();
    if (positions.size() < arrayLength) {
        positions = Ints.asList(new int[arrayLength]);
    }
    for (int i = 0; i < arrayLength; i++) {
        positions.set(i, i);
    }
    Collections.sort(positions.subList(0, arrayLength), new Comparator<Integer>() {

        @Override
        public int compare(Integer p1, Integer p2) {
            boolean nullLeft = block.isNull(p1);
            boolean nullRight = block.isNull(p2);
            if (nullLeft && nullRight) {
                return 0;
            }
            if (nullLeft) {
                return 1;
            }
            if (nullRight) {
                return -1;
            }
            // TODO: This could be quite slow, it should use parametric equals
            return type.compareTo(block, p1, block, p2);
        }
    });
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    for (int i = 0; i < arrayLength; i++) {
        type.appendTo(block, positions.get(i), blockBuilder);
    }
    pageBuilder.declarePositions(arrayLength);
    return blockBuilder.getRegion(blockBuilder.getPositionCount() - arrayLength, arrayLength);
}
Also used : BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 100 with SqlType

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

the class ArrayUnionFunction method union.

@TypeParameter("E")
@SqlType("array(E)")
public static Block union(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    int leftArrayCount = leftArray.getPositionCount();
    int rightArrayCount = rightArray.getPositionCount();
    TypedSet typedSet = new TypedSet(type, leftArrayCount + rightArrayCount, "array_union");
    BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(null, leftArrayCount + rightArrayCount);
    appendTypedArray(leftArray, type, typedSet, distinctElementBlockBuilder);
    appendTypedArray(rightArray, type, typedSet, distinctElementBlockBuilder);
    return distinctElementBlockBuilder.build();
}
Also used : TypedSet(io.prestosql.operator.aggregation.TypedSet) BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

SqlType (io.prestosql.spi.function.SqlType)138 ScalarFunction (io.prestosql.spi.function.ScalarFunction)96 Description (io.prestosql.spi.function.Description)76 SqlNullable (io.prestosql.spi.function.SqlNullable)52 BlockBuilder (io.prestosql.spi.block.BlockBuilder)42 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)40 PrestoException (io.prestosql.spi.PrestoException)27 Slice (io.airlift.slice.Slice)25 LiteralParameters (io.prestosql.spi.function.LiteralParameters)24 TypeParameter (io.prestosql.spi.function.TypeParameter)20 ScalarOperator (io.prestosql.spi.function.ScalarOperator)16 Constraint (io.prestosql.type.Constraint)16 JsonParser (com.fasterxml.jackson.core.JsonParser)15 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)15 IOException (java.io.IOException)15 Point (com.esri.core.geometry.Point)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 MultiPoint (com.esri.core.geometry.MultiPoint)12 Block (io.prestosql.spi.block.Block)9 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)8