Search in sources :

Example 1 with TypedKeyValueHeap

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

the class AbstractMinMaxByNAggregationFunction method input.

public static void input(BlockComparator comparator, Type valueType, Type keyType, MinMaxByNState state, Block value, Block key, int blockIndex, long n) {
    TypedKeyValueHeap heap = state.getTypedKeyValueHeap();
    if (heap == null) {
        if (n <= 0) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "third argument of max_by/min_by must be a positive integer");
        }
        checkCondition(n <= MAX_NUMBER_OF_VALUES, INVALID_FUNCTION_ARGUMENT, "third argument of max_by/min_by must be less than or equal to %s; found %s", MAX_NUMBER_OF_VALUES, n);
        heap = new TypedKeyValueHeap(comparator, keyType, valueType, toIntExact(n));
        state.setTypedKeyValueHeap(heap);
    }
    long startSize = heap.getEstimatedSize();
    if (!key.isNull(blockIndex)) {
        heap.add(key, value, blockIndex);
    }
    state.addMemoryUsage(heap.getEstimatedSize() - startSize);
}
Also used : TypedKeyValueHeap(com.facebook.presto.operator.aggregation.TypedKeyValueHeap) PrestoException(com.facebook.presto.spi.PrestoException)

Example 2 with TypedKeyValueHeap

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

the class AbstractMinMaxByNAggregationFunction method output.

public static void output(ArrayType outputType, MinMaxByNState state, BlockBuilder out) {
    TypedKeyValueHeap heap = state.getTypedKeyValueHeap();
    if (heap == null || heap.isEmpty()) {
        out.appendNull();
        return;
    }
    Type elementType = outputType.getElementType();
    BlockBuilder arrayBlockBuilder = out.beginBlockEntry();
    BlockBuilder reversedBlockBuilder = elementType.createBlockBuilder(null, heap.getCapacity());
    long startSize = heap.getEstimatedSize();
    heap.popAll(reversedBlockBuilder);
    state.addMemoryUsage(heap.getEstimatedSize() - startSize);
    for (int i = reversedBlockBuilder.getPositionCount() - 1; i >= 0; i--) {
        elementType.appendTo(reversedBlockBuilder, i, arrayBlockBuilder);
    }
    out.closeEntry();
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) TypedKeyValueHeap(com.facebook.presto.operator.aggregation.TypedKeyValueHeap) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 3 with TypedKeyValueHeap

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

the class AbstractMinMaxByNAggregationFunction method combine.

public static void combine(MinMaxByNState state, MinMaxByNState otherState) {
    TypedKeyValueHeap otherHeap = otherState.getTypedKeyValueHeap();
    if (otherHeap == null) {
        return;
    }
    TypedKeyValueHeap heap = state.getTypedKeyValueHeap();
    if (heap == null) {
        state.setTypedKeyValueHeap(otherHeap);
        return;
    }
    long startSize = heap.getEstimatedSize();
    heap.addAll(otherHeap);
    state.addMemoryUsage(heap.getEstimatedSize() - startSize);
}
Also used : TypedKeyValueHeap(com.facebook.presto.operator.aggregation.TypedKeyValueHeap)

Example 4 with TypedKeyValueHeap

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

the class MinMaxByNStateSerializer method serialize.

@Override
public void serialize(MinMaxByNState state, BlockBuilder out) {
    TypedKeyValueHeap heap = state.getTypedKeyValueHeap();
    if (heap == null) {
        out.appendNull();
        return;
    }
    heap.serialize(out);
}
Also used : TypedKeyValueHeap(com.facebook.presto.operator.aggregation.TypedKeyValueHeap)

Example 5 with TypedKeyValueHeap

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

the class MinMaxByNStateSerializer method serialize.

@Override
public void serialize(MinMaxByNState state, BlockBuilder out) {
    TypedKeyValueHeap heap = state.getTypedKeyValueHeap();
    if (heap == null) {
        out.appendNull();
        return;
    }
    heap.serialize(out);
}
Also used : TypedKeyValueHeap(com.facebook.presto.operator.aggregation.TypedKeyValueHeap)

Aggregations

TypedKeyValueHeap (com.facebook.presto.operator.aggregation.TypedKeyValueHeap)5 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 Type (com.facebook.presto.common.type.Type)1 PrestoException (com.facebook.presto.spi.PrestoException)1