Search in sources :

Example 66 with SqlType

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

the class ArrayIntersectFunction method intersect.

@TypeParameter("E")
@SqlType("array(E)")
public Block intersect(@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 leftPositionCount = leftArray.getPositionCount();
    int rightPositionCount = rightArray.getPositionCount();
    if (leftPositionCount == 0) {
        return leftArray;
    }
    if (rightPositionCount == 0) {
        return rightArray;
    }
    if (leftPositions.length < leftPositionCount) {
        leftPositions = new int[leftPositionCount];
    }
    if (rightPositions.length < rightPositionCount) {
        rightPositions = new int[rightPositionCount];
    }
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    for (int i = 0; i < leftPositionCount; i++) {
        leftPositions[i] = i;
    }
    for (int i = 0; i < rightPositionCount; i++) {
        rightPositions[i] = i;
    }
    IntArrays.quickSort(leftPositions, 0, leftPositionCount, intBlockCompare(type, leftArray));
    IntArrays.quickSort(rightPositions, 0, rightPositionCount, intBlockCompare(type, rightArray));
    BlockBuilder resultBlockBuilder = pageBuilder.getBlockBuilder(0);
    int leftCurrentPosition = 0;
    int rightCurrentPosition = 0;
    int leftBasePosition;
    int rightBasePosition;
    int totalCount = 0;
    while (leftCurrentPosition < leftPositionCount && rightCurrentPosition < rightPositionCount) {
        leftBasePosition = leftCurrentPosition;
        rightBasePosition = rightCurrentPosition;
        int compareValue = type.compareTo(leftArray, leftPositions[leftCurrentPosition], rightArray, rightPositions[rightCurrentPosition]);
        if (compareValue > 0) {
            rightCurrentPosition++;
        } else if (compareValue < 0) {
            leftCurrentPosition++;
        } else {
            type.appendTo(leftArray, leftPositions[leftCurrentPosition], resultBlockBuilder);
            leftCurrentPosition++;
            rightCurrentPosition++;
            totalCount++;
            while (leftCurrentPosition < leftPositionCount && type.equalTo(leftArray, leftPositions[leftBasePosition], leftArray, leftPositions[leftCurrentPosition])) {
                leftCurrentPosition++;
            }
            while (rightCurrentPosition < rightPositionCount && type.equalTo(rightArray, rightPositions[rightBasePosition], rightArray, rightPositions[rightCurrentPosition])) {
                rightCurrentPosition++;
            }
        }
    }
    pageBuilder.declarePositions(totalCount);
    return resultBlockBuilder.getRegion(resultBlockBuilder.getPositionCount() - totalCount, totalCount);
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 67 with SqlType

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

the class ArrayLessThanOperator method lessThan.

@TypeParameter("E")
@SqlType(StandardTypes.BOOLEAN)
public static boolean lessThan(@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);
        Object leftElement = readNativeValue(type, leftArray, index);
        Object rightElement = readNativeValue(type, rightArray, index);
        try {
            if ((boolean) lessThanFunction.invoke(leftElement, rightElement)) {
                return true;
            }
            if ((boolean) lessThanFunction.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 68 with SqlType

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

the class ArrayLessThanOperator method lessThanLong.

@TypeParameter("E")
@TypeParameterSpecialization(name = "E", nativeContainerType = long.class)
@SqlType(StandardTypes.BOOLEAN)
public static boolean lessThanLong(@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 69 with SqlType

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

the class ArrayLessThanOrEqualOperator method lessThanOrEqual.

@TypeParameter("E")
@SqlType(StandardTypes.BOOLEAN)
public static boolean lessThanOrEqual(@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);
        Object leftElement = readNativeValue(type, leftArray, index);
        Object rightElement = readNativeValue(type, rightArray, index);
        try {
            if ((boolean) lessThanFunction.invoke(leftElement, rightElement)) {
                return true;
            }
            if ((boolean) lessThanFunction.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 70 with SqlType

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

the class ArrayRemoveFunction method remove.

@TypeParameter("E")
@SqlType("array(E)")
public static 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) {
    int sizeAfterRemove = 0;
    List<Integer> positions = new ArrayList<>();
    for (int i = 0; i < array.getPositionCount(); i++) {
        Object element = readNativeValue(type, array, i);
        try {
            if (element == null || !(boolean) equalsFunction.invoke(element, value)) {
                positions.add(i);
                sizeAfterRemove += array.getRegionSizeInBytes(i, 1);
            }
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, Error.class);
            Throwables.propagateIfInstanceOf(t, PrestoException.class);
            throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
        }
    }
    if (array.getPositionCount() == positions.size()) {
        return array;
    }
    int entrySize = 0;
    if (!positions.isEmpty()) {
        entrySize = (int) Math.ceil(sizeAfterRemove / (double) positions.size());
    }
    BlockBuilder blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), positions.size(), entrySize);
    for (int position : positions) {
        type.appendTo(array, position, blockBuilder);
    }
    return blockBuilder.build();
}
Also used : ArrayList(java.util.ArrayList) PrestoException(com.facebook.presto.spi.PrestoException) 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)

Aggregations

SqlType (com.facebook.presto.spi.function.SqlType)74 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)36 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)24 TypeParameter (com.facebook.presto.spi.function.TypeParameter)24 PrestoException (com.facebook.presto.spi.PrestoException)22 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)20 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)19 Description (com.facebook.presto.spi.function.Description)18 Constraint (com.facebook.presto.type.Constraint)15 SqlNullable (com.facebook.presto.spi.function.SqlNullable)14 Slice (io.airlift.slice.Slice)13 IOException (java.io.IOException)12 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)11 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)11 JsonParser (com.fasterxml.jackson.core.JsonParser)11 TypeParameterSpecialization (com.facebook.presto.spi.function.TypeParameterSpecialization)9 JsonToken (com.fasterxml.jackson.core.JsonToken)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)6 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)5