Search in sources :

Example 1 with TypeParameter

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

the class ArrayLessThanOrEqualOperator method lessThanOrEqualLong.

@TypeParameter("E")
@TypeParameterSpecialization(name = "E", nativeContainerType = long.class)
@SqlType(StandardTypes.BOOLEAN)
public static boolean lessThanOrEqualLong(@OperatorDependency(operator = LESS_THAN, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle lessThanFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") 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) lessThanFunction.invokeExact(leftElement, rightElement)) {
                return true;
            }
            if ((boolean) lessThanFunction.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)

Example 2 with TypeParameter

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

the class ArrayDistinctFromOperator method isDistinctFrom.

@TypeParameter("E")
@SqlType(StandardTypes.BOOLEAN)
public static boolean isDistinctFrom(@OperatorDependency(operator = IS_DISTINCT_FROM, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle function, @TypeParameter("E") Type type, @SqlType("array(E)") Block left, @IsNull boolean leftNull, @SqlType("array(E)") Block right, @IsNull boolean rightNull) {
    if (leftNull != rightNull) {
        return true;
    }
    if (leftNull) {
        return false;
    }
    if (left.getPositionCount() != right.getPositionCount()) {
        return true;
    }
    for (int i = 0; i < left.getPositionCount(); i++) {
        Object leftValue = readNativeValue(type, left, i);
        boolean leftValueNull = leftValue == null;
        if (leftValueNull) {
            leftValue = defaultValue(type.getJavaType());
        }
        Object rightValue = readNativeValue(type, right, i);
        boolean rightValueNull = rightValue == null;
        if (rightValueNull) {
            rightValue = defaultValue(type.getJavaType());
        }
        try {
            if ((boolean) function.invoke(leftValue, leftValueNull, rightValue, rightValueNull)) {
                return true;
            }
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, Error.class);
            Throwables.propagateIfInstanceOf(t, PrestoException.class);
            throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
        }
    }
    return false;
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 3 with TypeParameter

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

the class ArrayEqualOperator method equals.

@TypeParameter("E")
@SqlType(StandardTypes.BOOLEAN)
public static boolean equals(@OperatorDependency(operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle equalsFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    if (leftArray.getPositionCount() != rightArray.getPositionCount()) {
        return false;
    }
    for (int i = 0; i < leftArray.getPositionCount(); i++) {
        checkElementNotNull(leftArray.isNull(i), ARRAY_NULL_ELEMENT_MSG);
        checkElementNotNull(rightArray.isNull(i), ARRAY_NULL_ELEMENT_MSG);
        Object leftElement = readNativeValue(type, leftArray, i);
        Object rightElement = readNativeValue(type, rightArray, i);
        try {
            if (!(boolean) equalsFunction.invoke(leftElement, rightElement)) {
                return false;
            }
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, Error.class);
            Throwables.propagateIfInstanceOf(t, PrestoException.class);
            throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
        }
    }
    return true;
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 4 with TypeParameter

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

the class ArrayExceptFunction method except.

@TypeParameter("E")
@SqlType("array(E)")
public static Block except(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    int leftPositionCount = leftArray.getPositionCount();
    int rightPositionCount = rightArray.getPositionCount();
    if (leftPositionCount == 0) {
        return leftArray;
    }
    TypedSet typedSet = new TypedSet(type, leftPositionCount + rightPositionCount);
    BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), leftPositionCount);
    for (int i = 0; i < rightPositionCount; i++) {
        typedSet.add(rightArray, i);
    }
    for (int i = 0; i < leftPositionCount; i++) {
        if (!typedSet.contains(leftArray, i)) {
            typedSet.add(leftArray, i);
            type.appendTo(leftArray, 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 5 with TypeParameter

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

the class ArrayFilterFunction method filterLong.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType("array(T)")
public static Block filterLong(@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++) {
        Long input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getLong(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 : 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)

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