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));
}
}
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] });
}
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));
}
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());
}
}
Aggregations