Search in sources :

Example 21 with DictionaryBlock

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

the class TestArrayOfRowsUnnester method testArrayOfRowsUnnester.

/**
 * Test operations of ArrayOfRowUnnester incrementally on the input.
 * Output final blocks after the whole input has been processed.
 * <p>
 * Input 3d array {@code elements} stores values from a column with type <array<row<varchar, varchar, ... {@code fieldCount} times> >.
 * elements[i] corresponds to a position in this column, represents one array of row(....).
 * elements[i][j] represents one row(....) object in the array.
 * elements[i][j][k] represents value of kth field in row(...) object.
 */
private static Block[] testArrayOfRowsUnnester(int[] requiredOutputCounts, int[] unnestedLengths, Slice[][][] elements, int fieldCount) {
    validateTestInput(requiredOutputCounts, unnestedLengths, elements, fieldCount);
    int positionCount = requiredOutputCounts.length;
    // True if there is a null Row element inside the array at this position
    boolean[] containsNullRowElement = new boolean[positionCount];
    // Populate containsNullRowElement
    for (int i = 0; i < positionCount; i++) {
        containsNullRowElement[i] = false;
        if (elements[i] != null) {
            for (int j = 0; j < elements[i].length; j++) {
                if (elements[i][j] == null) {
                    containsNullRowElement[i] = true;
                }
            }
        }
    }
    // Check for null elements in individual input fields
    boolean[] nullsPresent = new boolean[fieldCount];
    for (int field = 0; field < fieldCount; field++) {
        nullsPresent[field] = nullExists(elements[field]);
    }
    // Create the unnester and input block
    RowType rowType = RowType.anonymous(Collections.nCopies(fieldCount, VARCHAR));
    Unnester arrayofRowsUnnester = new ArrayOfRowsUnnester(rowType);
    Block arrayBlockOfRows = createArrayBlockOfRowBlocks(elements, rowType);
    Block[] blocks = null;
    // Verify output being produced after processing every position. (quadratic)
    for (int inputTestCount = 1; inputTestCount <= elements.length; inputTestCount++) {
        // Reset input and prepare for new output
        PageBuilderStatus status = new PageBuilderStatus();
        arrayofRowsUnnester.resetInput(arrayBlockOfRows);
        assertEquals(arrayofRowsUnnester.getInputEntryCount(), elements.length);
        arrayofRowsUnnester.startNewOutput(status, 10);
        boolean misAligned = false;
        // Process inputTestCount positions
        for (int i = 0; i < inputTestCount; i++) {
            arrayofRowsUnnester.processCurrentAndAdvance(requiredOutputCounts[i]);
            int elementsSize = (elements[i] != null ? elements[i].length : 0);
            // (2) null Row element
            if ((requiredOutputCounts[i] > elementsSize) || containsNullRowElement[i]) {
                misAligned = true;
            }
        }
        // Build output block and verify
        blocks = arrayofRowsUnnester.buildOutputBlocksAndFlush();
        assertEquals(blocks.length, rowType.getFields().size());
        // Verify output blocks for individual fields
        for (int field = 0; field < blocks.length; field++) {
            assertTrue((blocks[field] instanceof DictionaryBlock) || (!nullsPresent[field] && misAligned));
            assertFalse((blocks[field] instanceof DictionaryBlock) && (!nullsPresent[field] && misAligned));
            Slice[][] fieldElements = getFieldElements(elements, field);
            Slice[] expectedOutput = computeExpectedUnnestedOutput(fieldElements, requiredOutputCounts, 0, inputTestCount);
            assertBlock(blocks[field], expectedOutput);
        }
    }
    return blocks;
}
Also used : DictionaryBlock(io.trino.spi.block.DictionaryBlock) RowType(io.trino.spi.type.RowType) PageBuilderStatus(io.trino.spi.block.PageBuilderStatus) Slice(io.airlift.slice.Slice) ColumnarTestUtils.assertBlock(io.trino.block.ColumnarTestUtils.assertBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) Block(io.trino.spi.block.Block)

Example 22 with DictionaryBlock

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

the class TestArrayUnnester method testArrayUnnester.

private static Block[] testArrayUnnester(int[] requiredOutputCounts, int[] unnestedLengths, Slice[][][] elements) {
    Slice[][] slices = getFieldElements(elements, 0);
    validateTestInput(requiredOutputCounts, unnestedLengths, elements, 1);
    // Check if there is a null element in the input
    boolean nullPresent = nullExists(slices);
    // Initialize unnester
    Unnester arrayUnnester = new ArrayUnnester(VARCHAR);
    Block arrayBlock = createArrayBlock(slices);
    Block[] blocks = null;
    // Verify output being produced after processing every position. (quadratic)
    for (int inputTestCount = 1; inputTestCount <= elements.length; inputTestCount++) {
        // Reset input
        arrayUnnester.resetInput(arrayBlock);
        assertEquals(arrayUnnester.getInputEntryCount(), elements.length);
        // Prepare for new output
        PageBuilderStatus status = new PageBuilderStatus();
        arrayUnnester.startNewOutput(status, 10);
        boolean misAligned = false;
        // Process inputTestCount positions
        for (int i = 0; i < inputTestCount; i++) {
            int elementsSize = (elements[i] != null ? elements[i].length : 0);
            assertEquals(arrayUnnester.getCurrentUnnestedLength(), elementsSize);
            arrayUnnester.processCurrentAndAdvance(requiredOutputCounts[i]);
            if (requiredOutputCounts[i] > elementsSize) {
                misAligned = true;
            }
        }
        // Verify output
        blocks = arrayUnnester.buildOutputBlocksAndFlush();
        assertEquals(blocks.length, 1);
        assertTrue((blocks[0] instanceof DictionaryBlock) || (!nullPresent && misAligned));
        assertFalse((blocks[0] instanceof DictionaryBlock) && (!nullPresent && misAligned));
        Slice[] expectedOutput = computeExpectedUnnestedOutput(slices, requiredOutputCounts, 0, inputTestCount);
        assertBlock(blocks[0], expectedOutput);
    }
    return blocks;
}
Also used : PageBuilderStatus(io.trino.spi.block.PageBuilderStatus) Slice(io.airlift.slice.Slice) DictionaryBlock(io.trino.spi.block.DictionaryBlock) ColumnarTestUtils.assertBlock(io.trino.block.ColumnarTestUtils.assertBlock) DictionaryBlock(io.trino.spi.block.DictionaryBlock) TestingUnnesterUtil.createArrayBlock(io.trino.operator.unnest.TestingUnnesterUtil.createArrayBlock) Block(io.trino.spi.block.Block)

Example 23 with DictionaryBlock

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

the class TestPageProcessorCompiler method testSanityColumnarDictionary.

@Test
public void testSanityColumnarDictionary() {
    PageProcessor processor = compiler.compilePageProcessor(Optional.empty(), ImmutableList.of(field(0, VARCHAR)), MAX_BATCH_SIZE).get();
    Page page = new Page(createDictionaryBlock(createExpectedValues(10), 100));
    Page outputPage = getOnlyElement(processor.process(null, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)).orElseThrow(() -> new AssertionError("page is not present"));
    assertEquals(outputPage.getPositionCount(), 100);
    assertTrue(outputPage.getBlock(0) instanceof DictionaryBlock);
    DictionaryBlock dictionaryBlock = (DictionaryBlock) outputPage.getBlock(0);
    assertEquals(dictionaryBlock.getDictionary().getPositionCount(), 10);
}
Also used : PageProcessor(io.trino.operator.project.PageProcessor) DictionaryBlock(io.trino.spi.block.DictionaryBlock) BlockAssertions.createLongDictionaryBlock(io.trino.block.BlockAssertions.createLongDictionaryBlock) DriverYieldSignal(io.trino.operator.DriverYieldSignal) Page(io.trino.spi.Page) Test(org.testng.annotations.Test)

Example 24 with DictionaryBlock

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

the class TestPageProcessorCompiler method testNonDeterministicProject.

@Test
public void testNonDeterministicProject() {
    ResolvedFunction lessThan = functionResolution.resolveOperator(LESS_THAN, ImmutableList.of(BIGINT, BIGINT));
    CallExpression random = new CallExpression(functionResolution.resolveFunction(QualifiedName.of("random"), fromTypes(BIGINT)), singletonList(constant(10L, BIGINT)));
    InputReferenceExpression col0 = field(0, BIGINT);
    CallExpression lessThanRandomExpression = new CallExpression(lessThan, ImmutableList.of(col0, random));
    PageProcessor processor = compiler.compilePageProcessor(Optional.empty(), ImmutableList.of(lessThanRandomExpression), MAX_BATCH_SIZE).get();
    assertFalse(isDeterministic(lessThanRandomExpression));
    Page page = new Page(createLongDictionaryBlock(1, 100));
    Page outputPage = getOnlyElement(processor.process(null, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)).orElseThrow(() -> new AssertionError("page is not present"));
    assertFalse(outputPage.getBlock(0) instanceof DictionaryBlock);
}
Also used : InputReferenceExpression(io.trino.sql.relational.InputReferenceExpression) PageProcessor(io.trino.operator.project.PageProcessor) ResolvedFunction(io.trino.metadata.ResolvedFunction) DictionaryBlock(io.trino.spi.block.DictionaryBlock) BlockAssertions.createLongDictionaryBlock(io.trino.block.BlockAssertions.createLongDictionaryBlock) DriverYieldSignal(io.trino.operator.DriverYieldSignal) Page(io.trino.spi.Page) CallExpression(io.trino.sql.relational.CallExpression) Test(org.testng.annotations.Test)

Example 25 with DictionaryBlock

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

the class TestReplicatedBlockBuilder method testReplication.

private static void testReplication(Slice[] values, int[] counts) {
    assertEquals(values.length, counts.length);
    ReplicatedBlockBuilder replicateBlockBuilder = new ReplicatedBlockBuilder();
    Block valuesBlock = createSimpleBlock(values);
    replicateBlockBuilder.resetInputBlock(valuesBlock);
    replicateBlockBuilder.startNewOutput(100);
    for (int i = 0; i < counts.length; i++) {
        replicateBlockBuilder.appendRepeated(i, counts[i]);
    }
    Block outputBlock = replicateBlockBuilder.buildOutputAndFlush();
    assertBlock(outputBlock, createReplicatedOutputSlice(values, counts));
    assertTrue(outputBlock instanceof DictionaryBlock);
}
Also used : 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)

Aggregations

DictionaryBlock (io.trino.spi.block.DictionaryBlock)71 Block (io.trino.spi.block.Block)39 Test (org.testng.annotations.Test)38 Slice (io.airlift.slice.Slice)21 RunLengthEncodedBlock (io.trino.spi.block.RunLengthEncodedBlock)16 Page (io.trino.spi.Page)15 LazyBlock (io.trino.spi.block.LazyBlock)10 BlockAssertions.createLongSequenceBlock (io.trino.block.BlockAssertions.createLongSequenceBlock)9 BlockAssertions.createLongsBlock (io.trino.block.BlockAssertions.createLongsBlock)9 LongArrayBlock (io.trino.spi.block.LongArrayBlock)9 ColumnarTestUtils.assertBlock (io.trino.block.ColumnarTestUtils.assertBlock)8 PageBuilderStatus (io.trino.spi.block.PageBuilderStatus)8 VariableWidthBlock (io.trino.spi.block.VariableWidthBlock)7 DictionaryId (io.trino.spi.block.DictionaryId)6 BlockAssertions.createLongDictionaryBlock (io.trino.block.BlockAssertions.createLongDictionaryBlock)5 BlockAssertions.createSlicesBlock (io.trino.block.BlockAssertions.createSlicesBlock)5 TestingUnnesterUtil.createSimpleBlock (io.trino.operator.unnest.TestingUnnesterUtil.createSimpleBlock)5 BlockBuilder (io.trino.spi.block.BlockBuilder)5 DictionaryId.randomDictionaryId (io.trino.spi.block.DictionaryId.randomDictionaryId)5 IntArrayBlock (io.trino.spi.block.IntArrayBlock)5