Search in sources :

Example 11 with TypeParameter

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

the class ArrayDistinctFunction method distinct.

@TypeParameter("E")
@SqlType("array(E)")
public static Block distinct(@TypeParameter("E") Type type, @SqlType("array(E)") Block array) {
    if (array.getPositionCount() < 2) {
        return array;
    }
    if (array.getPositionCount() == 2) {
        if (type.equalTo(array, 0, array, 1)) {
            return array.getSingleValueBlock(0);
        } else {
            return array;
        }
    }
    TypedSet typedSet = new TypedSet(type, array.getPositionCount());
    BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), array.getPositionCount());
    for (int i = 0; i < array.getPositionCount(); i++) {
        if (!typedSet.contains(array, i)) {
            typedSet.add(array, i);
            type.appendTo(array, i, distinctElementBlockBuilder);
        }
    }
    return distinctElementBlockBuilder.build();
}
Also used : TypedSet(com.facebook.presto.operator.aggregation.TypedSet) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 12 with TypeParameter

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

the class ArrayFilterFunction method filterBlock.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = Block.class)
@SqlType("array(T)")
public static Block filterBlock(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") MethodHandle function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), positionCount);
    for (int position = 0; position < positionCount; position++) {
        Block input = null;
        if (!arrayBlock.isNull(position)) {
            input = (Block) elementType.getObject(arrayBlock, position);
        }
        Boolean keep;
        try {
            keep = (Boolean) function.invokeExact(input);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        if (TRUE.equals(keep)) {
            elementType.appendTo(arrayBlock, position, resultBuilder);
        }
    }
    return resultBuilder.build();
}
Also used : Block(com.facebook.presto.spi.block.Block) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameterSpecialization(com.facebook.presto.spi.function.TypeParameterSpecialization) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 13 with TypeParameter

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

the class ArrayFilterFunction method filterSlice.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = Slice.class)
@SqlType("array(T)")
public static Block filterSlice(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") MethodHandle function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), positionCount);
    for (int position = 0; position < positionCount; position++) {
        Slice input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getSlice(arrayBlock, position);
        }
        Boolean keep;
        try {
            keep = (Boolean) function.invokeExact(input);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        if (TRUE.equals(keep)) {
            elementType.appendTo(arrayBlock, position, resultBuilder);
        }
    }
    return resultBuilder.build();
}
Also used : Slice(io.airlift.slice.Slice) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameterSpecialization(com.facebook.presto.spi.function.TypeParameterSpecialization) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 14 with TypeParameter

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

the class ArrayGreaterThanOperator method greaterThan.

@TypeParameter("T")
@SqlType(StandardTypes.BOOLEAN)
public static boolean greaterThan(@OperatorDependency(operator = GREATER_THAN, returnType = StandardTypes.BOOLEAN, argumentTypes = { "T", "T" }) MethodHandle greaterThanFunction, @TypeParameter("T") Type type, @SqlType("array(T)") Block leftArray, @SqlType("array(T)") Block rightArray) {
    int len = Math.min(leftArray.getPositionCount(), rightArray.getPositionCount());
    int index = 0;
    while (index < len) {
        checkElementNotNull(leftArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
        checkElementNotNull(rightArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
        Object leftElement = readNativeValue(type, leftArray, index);
        Object rightElement = readNativeValue(type, rightArray, index);
        try {
            if ((boolean) greaterThanFunction.invoke(leftElement, rightElement)) {
                return true;
            }
            if ((boolean) greaterThanFunction.invoke(rightElement, leftElement)) {
                return false;
            }
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, Error.class);
            Throwables.propagateIfInstanceOf(t, PrestoException.class);
            throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
        }
        index++;
    }
    return leftArray.getPositionCount() > rightArray.getPositionCount();
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 15 with TypeParameter

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

the class ArrayGreaterThanOperator method greaterThanLong.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType(StandardTypes.BOOLEAN)
public static boolean greaterThanLong(@OperatorDependency(operator = GREATER_THAN, returnType = StandardTypes.BOOLEAN, argumentTypes = { "T", "T" }) MethodHandle greaterThanFunction, @TypeParameter("T") Type type, @SqlType("array(T)") Block leftArray, @SqlType("array(T)") Block rightArray) {
    int len = Math.min(leftArray.getPositionCount(), rightArray.getPositionCount());
    int index = 0;
    while (index < len) {
        checkElementNotNull(leftArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
        checkElementNotNull(rightArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
        long leftElement = type.getLong(leftArray, index);
        long rightElement = type.getLong(rightArray, index);
        try {
            if ((boolean) greaterThanFunction.invokeExact(leftElement, rightElement)) {
                return true;
            }
            if ((boolean) greaterThanFunction.invokeExact(rightElement, leftElement)) {
                return false;
            }
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, Error.class);
            Throwables.propagateIfInstanceOf(t, PrestoException.class);
            throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
        }
        index++;
    }
    return leftArray.getPositionCount() > rightArray.getPositionCount();
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) TypeParameterSpecialization(com.facebook.presto.spi.function.TypeParameterSpecialization) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

SqlType (com.facebook.presto.spi.function.SqlType)24 TypeParameter (com.facebook.presto.spi.function.TypeParameter)24 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)15 PrestoException (com.facebook.presto.spi.PrestoException)11 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)11 TypeParameterSpecialization (com.facebook.presto.spi.function.TypeParameterSpecialization)9 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)4 Block (com.facebook.presto.spi.block.Block)2 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)1 Slice (io.airlift.slice.Slice)1 ArrayList (java.util.ArrayList)1