Search in sources :

Example 1 with OptimizedTypedSet

use of com.facebook.presto.operator.aggregation.OptimizedTypedSet in project presto by prestodb.

the class MapConcatFunction method mapConcat.

@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, Block[] maps) {
    int entries = 0;
    int lastMapIndex = maps.length - 1;
    int firstMapIndex = lastMapIndex;
    for (int i = 0; i < maps.length; i++) {
        entries += maps[i].getPositionCount();
        if (maps[i].getPositionCount() > 0) {
            lastMapIndex = i;
            firstMapIndex = min(firstMapIndex, i);
        }
    }
    if (lastMapIndex == firstMapIndex) {
        return maps[lastMapIndex];
    }
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    // We need to divide the entries by 2 because the maps array is SingleMapBlocks and it had the positionCount twice as large as a normal Block
    OptimizedTypedSet typedSet = new OptimizedTypedSet(keyType, maps.length, entries / 2);
    for (int i = lastMapIndex; i >= firstMapIndex; i--) {
        SingleMapBlock singleMapBlock = (SingleMapBlock) maps[i];
        Block keyBlock = singleMapBlock.getKeyBlock();
        typedSet.union(keyBlock);
    }
    List<SelectedPositions> selectedPositionsList = typedSet.getPositionsForBlocks();
    BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, selectedPositionsList.size());
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    for (int i = lastMapIndex; i >= firstMapIndex; i--) {
        SingleMapBlock singleMapBlock = (SingleMapBlock) maps[i];
        // selectedPositions was ordered by addUnion sequence therefore the order should be reversed.
        SelectedPositions selectedPositions = selectedPositionsList.get(lastMapIndex - i);
        assert selectedPositions.isList();
        int[] positions = selectedPositions.getPositions();
        for (int j = 0; j < selectedPositions.size(); j++) {
            int position = positions[j];
            keyType.appendTo(singleMapBlock, 2 * position, blockBuilder);
            valueType.appendTo(singleMapBlock, 2 * position + 1, blockBuilder);
        }
    }
    mapBlockBuilder.closeEntry();
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) OptimizedTypedSet(com.facebook.presto.operator.aggregation.OptimizedTypedSet) SelectedPositions(com.facebook.presto.operator.project.SelectedPositions) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) Block(com.facebook.presto.common.block.Block) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 2 with OptimizedTypedSet

use of com.facebook.presto.operator.aggregation.OptimizedTypedSet 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;
    }
    OptimizedTypedSet typedSet = new OptimizedTypedSet(type, max(leftPositionCount, rightPositionCount));
    typedSet.union(rightArray);
    typedSet.except(leftArray);
    return typedSet.getBlock();
}
Also used : OptimizedTypedSet(com.facebook.presto.operator.aggregation.OptimizedTypedSet) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 3 with OptimizedTypedSet

use of com.facebook.presto.operator.aggregation.OptimizedTypedSet in project presto by prestodb.

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();
    OptimizedTypedSet typedSet = new OptimizedTypedSet(type, leftArrayCount + rightArrayCount);
    typedSet.union(leftArray);
    typedSet.union(rightArray);
    return typedSet.getBlock();
}
Also used : OptimizedTypedSet(com.facebook.presto.operator.aggregation.OptimizedTypedSet) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 4 with OptimizedTypedSet

use of com.facebook.presto.operator.aggregation.OptimizedTypedSet in project presto by prestodb.

the class ArrayIntersectFunction method intersect.

@ScalarFunction("array_intersect")
@Description("Intersects elements of the two given arrays")
@TypeParameter("E")
@SqlType("array(E)")
public static Block intersect(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    if (leftArray.getPositionCount() < rightArray.getPositionCount()) {
        Block tempArray = leftArray;
        leftArray = rightArray;
        rightArray = tempArray;
    }
    int rightPositionCount = rightArray.getPositionCount();
    if (rightPositionCount == 0) {
        return rightArray;
    }
    OptimizedTypedSet typedSet = new OptimizedTypedSet(type, rightPositionCount);
    typedSet.union(rightArray);
    typedSet.intersect(leftArray);
    return typedSet.getBlock();
}
Also used : OptimizedTypedSet(com.facebook.presto.operator.aggregation.OptimizedTypedSet) Block(com.facebook.presto.common.block.Block) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlInvokedScalarFunction(com.facebook.presto.spi.function.SqlInvokedScalarFunction) TypeParameter(com.facebook.presto.spi.function.TypeParameter) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

OptimizedTypedSet (com.facebook.presto.operator.aggregation.OptimizedTypedSet)4 SqlType (com.facebook.presto.spi.function.SqlType)3 TypeParameter (com.facebook.presto.spi.function.TypeParameter)3 Block (com.facebook.presto.common.block.Block)2 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)1 MapType (com.facebook.presto.common.type.MapType)1 Type (com.facebook.presto.common.type.Type)1 SelectedPositions (com.facebook.presto.operator.project.SelectedPositions)1 Description (com.facebook.presto.spi.function.Description)1 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)1 SqlInvokedScalarFunction (com.facebook.presto.spi.function.SqlInvokedScalarFunction)1