@Override public long getEstimatedSize() { long estimatedSize = INSTANCE_SIZE; if (typedKeyValueHeap != null) { estimatedSize += typedKeyValueHeap.getEstimatedSize(); } return estimatedSize; }
@Override public void setTypedKeyValueHeap(TypedKeyValueHeap value) { TypedKeyValueHeap previous = getTypedKeyValueHeap(); if (previous != null) { size -= previous.getEstimatedSize(); } heaps.set(getGroupId(), value); size += value.getEstimatedSize(); }
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); }
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); }
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(); }
@Override public long getEstimatedSize() { if (typedKeyValueHeap == null) { return 0; } return typedKeyValueHeap.getEstimatedSize(); }
@Override public void setTypedKeyValueHeap(TypedKeyValueHeap value) { TypedKeyValueHeap previous = getTypedKeyValueHeap(); if (previous != null) { size -= previous.getEstimatedSize(); } heaps.set(getGroupId(), value); size += value.getEstimatedSize(); }
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); }
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"); } heap = new TypedKeyValueHeap(comparator, keyType, valueType, Ints.checkedCast(n)); state.setTypedKeyValueHeap(heap); } long startSize = heap.getEstimatedSize(); if (!key.isNull(blockIndex)) { heap.add(key, value, blockIndex); } state.addMemoryUsage(heap.getEstimatedSize() - startSize); }
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(new BlockBuilderStatus(), 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(); }