Search in sources :

Example 11 with PageBuilderStatus

use of io.trino.spi.block.PageBuilderStatus in project trino by trinodb.

the class TestUnnestBlockBuilder method testAppendSingleElement.

private static void testAppendSingleElement(UnnestBlockBuilder unnestBlockBuilder, Slice[] values) {
    // Test unnestBlockBuilder output over the course of building output
    for (int testCount = 1; testCount <= values.length; testCount++) {
        unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 10);
        for (int i = 0; i < testCount; i++) {
            unnestBlockBuilder.appendElement(i);
        }
        Block block = unnestBlockBuilder.buildOutputAndFlush();
        assertTrue(block instanceof DictionaryBlock);
        assertBlock(block, Arrays.copyOf(values, testCount));
    }
}
Also used : PageBuilderStatus(io.trino.spi.block.PageBuilderStatus) DictionaryBlock(io.trino.spi.block.DictionaryBlock) TestingUnnesterUtil.createSimpleBlock(io.trino.operator.unnest.TestingUnnesterUtil.createSimpleBlock) ColumnarTestUtils.assertBlock(io.trino.block.ColumnarTestUtils.assertBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) Block(io.trino.spi.block.Block)

Example 12 with PageBuilderStatus

use of io.trino.spi.block.PageBuilderStatus in project trino by trinodb.

the class TestUnnestBlockBuilder method testAppendNull.

private static void testAppendNull(UnnestBlockBuilder unnestBlockBuilder, Slice[] values) {
    assertTrue(values.length >= 1, "values should have at least one element");
    int nullIndex = -1;
    for (int i = 0; i < values.length; i++) {
        if (values[i] == null) {
            nullIndex = i;
            break;
        }
    }
    // Test-1: appending a non-null element
    unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 10);
    unnestBlockBuilder.appendElement(0);
    Block block = unnestBlockBuilder.buildOutputAndFlush();
    assertTrue(block instanceof DictionaryBlock);
    assertBlock(block, new Slice[] { values[0] });
    // Test-2: appending a non-null element and a null.
    unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 10);
    unnestBlockBuilder.appendElement(0);
    unnestBlockBuilder.appendNull();
    block = unnestBlockBuilder.buildOutputAndFlush();
    assertTrue((block instanceof DictionaryBlock) || (nullIndex == -1));
    assertFalse((block instanceof DictionaryBlock) && (nullIndex == -1));
    assertBlock(block, new Slice[] { values[0], null });
    // Test-3: appending a non-null element, a null, and another non-null element.
    unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 10);
    unnestBlockBuilder.appendElement(0);
    unnestBlockBuilder.appendNull();
    unnestBlockBuilder.appendElement(0);
    block = unnestBlockBuilder.buildOutputAndFlush();
    assertTrue((block instanceof DictionaryBlock) || (nullIndex == -1));
    assertFalse((block instanceof DictionaryBlock) && (nullIndex == -1));
    assertBlock(block, new Slice[] { values[0], null, values[0] });
}
Also used : PageBuilderStatus(io.trino.spi.block.PageBuilderStatus) DictionaryBlock(io.trino.spi.block.DictionaryBlock) TestingUnnesterUtil.createSimpleBlock(io.trino.operator.unnest.TestingUnnesterUtil.createSimpleBlock) ColumnarTestUtils.assertBlock(io.trino.block.ColumnarTestUtils.assertBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) Block(io.trino.spi.block.Block)

Example 13 with PageBuilderStatus

use of io.trino.spi.block.PageBuilderStatus in project trino by trinodb.

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(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(arrayLength, combinationLength);
    do {
        arraycopy(combination, 0, ids, idsPosition, combinationLength);
        idsPosition += combinationLength;
    } while (nextCombination(combination, combinationLength));
    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(io.trino.spi.type.ArrayType) PageBuilderStatus(io.trino.spi.block.PageBuilderStatus) DictionaryBlock(io.trino.spi.block.DictionaryBlock) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 14 with PageBuilderStatus

use of io.trino.spi.block.PageBuilderStatus in project trino by trinodb.

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(io.trino.spi.block.PageBuilderStatus)

Aggregations

PageBuilderStatus (io.trino.spi.block.PageBuilderStatus)14 Block (io.trino.spi.block.Block)12 DictionaryBlock (io.trino.spi.block.DictionaryBlock)12 ColumnarTestUtils.assertBlock (io.trino.block.ColumnarTestUtils.assertBlock)11 Slice (io.airlift.slice.Slice)8 TestingUnnesterUtil.createSimpleBlock (io.trino.operator.unnest.TestingUnnesterUtil.createSimpleBlock)5 Test (org.testng.annotations.Test)5 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)2 TestingUnnesterUtil.createArrayBlock (io.trino.operator.unnest.TestingUnnesterUtil.createArrayBlock)2 RowType (io.trino.spi.type.RowType)2 Page (io.trino.spi.Page)1 SqlType (io.trino.spi.function.SqlType)1 TypeParameter (io.trino.spi.function.TypeParameter)1 ArrayType (io.trino.spi.type.ArrayType)1