Search in sources :

Example 1 with PageBuilderStatus

use of com.facebook.presto.common.block.PageBuilderStatus in project presto by prestodb.

the class ArrayCombinationsFunction method combinations.

@TypeParameter("T")
@SqlType("array(array(T))")
public static Block combinations(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block array, @SqlType(StandardTypes.INTEGER) long n) {
    int arrayLength = array.getPositionCount();
    int combinationLength = toIntExact(n);
    checkCondition(combinationLength >= 0, INVALID_FUNCTION_ARGUMENT, "combination size must not be negative: %s", combinationLength);
    checkCondition(combinationLength <= MAX_COMBINATION_LENGTH, INVALID_FUNCTION_ARGUMENT, "combination size must not exceed %s: %s", MAX_COMBINATION_LENGTH, combinationLength);
    ArrayType arrayType = new ArrayType(elementType);
    if (combinationLength > arrayLength) {
        return arrayType.createBlockBuilder(new PageBuilderStatus().createBlockBuilderStatus(), 0).build();
    }
    int combinationCount = combinationCount(arrayLength, combinationLength);
    checkCondition(combinationCount * (long) combinationLength <= MAX_RESULT_ELEMENTS, INVALID_FUNCTION_ARGUMENT, "combinations exceed max size");
    int[] ids = new int[combinationCount * combinationLength];
    int idsPosition = 0;
    int[] combination = firstCombination(combinationLength);
    do {
        arraycopy(combination, 0, ids, idsPosition, combinationLength);
        idsPosition += combinationLength;
    } while (nextCombination(combination, arrayLength));
    verify(idsPosition == ids.length, "idsPosition != ids.length, %s and %s respectively", idsPosition, ids.length);
    int[] offsets = new int[combinationCount + 1];
    setAll(offsets, i -> i * combinationLength);
    return ArrayBlock.fromElementBlock(combinationCount, Optional.empty(), offsets, new DictionaryBlock(array, ids));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) PageBuilderStatus(com.facebook.presto.common.block.PageBuilderStatus) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 2 with PageBuilderStatus

use of com.facebook.presto.common.block.PageBuilderStatus in project presto by prestodb.

the class PageBuilder method reset.

public void reset() {
    if (isEmpty()) {
        return;
    }
    pageBuilderStatus = new PageBuilderStatus(pageBuilderStatus.getMaxPageSizeInBytes());
    declaredPositions = 0;
    for (int i = 0; i < blockBuilders.length; i++) {
        blockBuilders[i] = blockBuilders[i].newBlockBuilderLike(pageBuilderStatus.createBlockBuilderStatus());
    }
}
Also used : PageBuilderStatus(com.facebook.presto.common.block.PageBuilderStatus)

Aggregations

PageBuilderStatus (com.facebook.presto.common.block.PageBuilderStatus)2 DictionaryBlock (com.facebook.presto.common.block.DictionaryBlock)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 SqlType (com.facebook.presto.spi.function.SqlType)1 TypeParameter (com.facebook.presto.spi.function.TypeParameter)1