use of io.trino.spi.block.PageBuilderStatus in project trino by trinodb.
the class TestMapUnnester method testTrimmedBlocks.
@Test
public void testTrimmedBlocks() {
int[] unnestedLengths = { 1, 2, 1 };
Slice[][][] elements = column(array(toSlices("0.0.0", "0.0.1")), array(toSlices("1.0.0", "1.0.1"), toSlices("1.1.0", "1.1.1")), array(toSlices("2.0.0", "2.0.1")));
Block mapBlock = createBlockBuilderWithValues(elements).build();
// Remove the first element from the arrayBlock for testing
int startElement = 1;
Slice[][][] truncatedSlices = Arrays.copyOfRange(elements, startElement, elements.length - startElement + 1);
int[] truncatedUnnestedLengths = Arrays.copyOfRange(unnestedLengths, startElement, elements.length - startElement + 1);
Block truncatedBlock = mapBlock.getRegion(startElement, elements.length - startElement);
assertBlock(truncatedBlock, truncatedSlices);
Unnester mapUnnester = new MapUnnester(VARCHAR, VARCHAR);
mapUnnester.resetInput(truncatedBlock);
mapUnnester.startNewOutput(new PageBuilderStatus(), 20);
// Process all input entries in the truncated block
for (int i = 0; i < truncatedBlock.getPositionCount(); i++) {
mapUnnester.processCurrentAndAdvance(truncatedUnnestedLengths[i]);
}
Block[] output = mapUnnester.buildOutputBlocksAndFlush();
assertEquals(Arrays.asList(truncatedSlices).stream().mapToInt(slice -> slice.length).sum(), output[0].getPositionCount());
Slice[] expectedOutput0 = computeExpectedUnnestedOutput(getFieldElements(truncatedSlices, 0), truncatedUnnestedLengths, 0, truncatedSlices.length);
assertBlock(output[0], expectedOutput0);
Slice[] expectedOutput1 = computeExpectedUnnestedOutput(getFieldElements(truncatedSlices, 1), truncatedUnnestedLengths, 0, truncatedSlices.length);
assertBlock(output[1], expectedOutput1);
}
use of io.trino.spi.block.PageBuilderStatus 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;
}
use of io.trino.spi.block.PageBuilderStatus in project trino by trinodb.
the class TestArrayOfRowsUnnester method testTrimmedBlocks.
@Test
public void testTrimmedBlocks() {
int fieldCount = 3;
int[] unnestedLengths = { 1, 2, 1 };
Slice[][][] elements = column(array(toSlices("0.0.0", "0.0.1", "0.0.2")), array(toSlices("1.0.0", "1.0.1", "1.0.2"), toSlices("1.1.0", "1.1.1", "1.1.2")), array(toSlices("2.0.0", "2.0.1", "2.0.2")));
RowType rowType = RowType.anonymous(Collections.nCopies(fieldCount, VARCHAR));
Block arrayOfRowBlock = createArrayBlockOfRowBlocks(elements, rowType);
// Remove the first element from the arrayBlock for testing
int startElement = 1;
Slice[][][] truncatedSlices = Arrays.copyOfRange(elements, startElement, elements.length - startElement + 1);
int[] truncatedUnnestedLengths = Arrays.copyOfRange(unnestedLengths, startElement, elements.length - startElement + 1);
Block truncatedBlock = arrayOfRowBlock.getRegion(startElement, truncatedSlices.length);
Unnester arrayOfRowsUnnester = new ArrayOfRowsUnnester(rowType);
arrayOfRowsUnnester.resetInput(truncatedBlock);
arrayOfRowsUnnester.startNewOutput(new PageBuilderStatus(), 20);
// Process all input entries in the truncated block
for (int i = 0; i < truncatedBlock.getPositionCount(); i++) {
arrayOfRowsUnnester.processCurrentAndAdvance(truncatedUnnestedLengths[i]);
}
Block[] output = arrayOfRowsUnnester.buildOutputBlocksAndFlush();
assertEquals(Arrays.asList(truncatedSlices).stream().mapToInt(slice -> slice.length).sum(), output[0].getPositionCount());
for (int i = 0; i < fieldCount; i++) {
Slice[] expectedOutput = computeExpectedUnnestedOutput(getFieldElements(truncatedSlices, i), truncatedUnnestedLengths, 0, truncatedSlices.length);
assertBlock(output[i], expectedOutput);
}
}
use of io.trino.spi.block.PageBuilderStatus 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;
}
use of io.trino.spi.block.PageBuilderStatus in project trino by trinodb.
the class TestUnnestBlockBuilder method testAppendRange.
private static void testAppendRange(UnnestBlockBuilder unnestBlockBuilder, Slice[] values) {
unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 10);
assertTrue(values.length >= 3, "test requires at least 3 elements in values");
int startIndex = 1;
int length = values.length - 2;
unnestBlockBuilder.appendRange(startIndex, length);
Block block = unnestBlockBuilder.buildOutputAndFlush();
assertTrue(block instanceof DictionaryBlock);
assertBlock(block, Arrays.copyOfRange(values, startIndex, startIndex + length));
}
Aggregations