use of com.facebook.presto.common.PageBuilder in project presto by prestodb.
the class FunctionAssertions method getRetainedSizeOf.
private long getRetainedSizeOf(Object object) {
if (object instanceof PageBuilder) {
return ((PageBuilder) object).getRetainedSizeInBytes();
}
if (object instanceof Block) {
return ((Block) object).getRetainedSizeInBytes();
}
Class type = object.getClass();
if (type.isArray()) {
if (type == int[].class) {
return sizeOf((int[]) object);
} else if (type == boolean[].class) {
return sizeOf((boolean[]) object);
} else if (type == byte[].class) {
return sizeOf((byte[]) object);
} else if (type == long[].class) {
return sizeOf((long[]) object);
} else if (type == short[].class) {
return sizeOf((short[]) object);
} else if (type == Block[].class) {
Object[] objects = (Object[]) object;
return Arrays.stream(objects).mapToLong(this::getRetainedSizeOf).sum();
} else {
throw new IllegalArgumentException(format("Unknown type encountered: %s", type));
}
}
long retainedSize = ClassLayout.parseClass(type).instanceSize();
Field[] fields = type.getDeclaredFields();
for (Field field : fields) {
try {
if (field.getType().isPrimitive() || Modifier.isStatic(field.getModifiers())) {
continue;
}
field.setAccessible(true);
retainedSize += getRetainedSizeOf(field.get(object));
} catch (IllegalAccessException t) {
throw new RuntimeException(t);
}
}
return retainedSize;
}
use of com.facebook.presto.common.PageBuilder in project presto by prestodb.
the class ZipWithFunction method zipWith.
public static Block zipWith(Type leftElementType, Type rightElementType, ArrayType outputArrayType, Object state, Block leftBlock, Block rightBlock, BinaryFunctionInterface function) {
Type outputElementType = outputArrayType.getElementType();
int leftPositionCount = leftBlock.getPositionCount();
int rightPositionCount = rightBlock.getPositionCount();
int outputPositionCount = max(leftPositionCount, rightPositionCount);
PageBuilder pageBuilder = (PageBuilder) state;
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder arrayBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder blockBuilder = arrayBlockBuilder.beginBlockEntry();
for (int position = 0; position < outputPositionCount; position++) {
Object left = position < leftPositionCount ? readNativeValue(leftElementType, leftBlock, position) : null;
Object right = position < rightPositionCount ? readNativeValue(rightElementType, rightBlock, position) : null;
Object output;
try {
output = function.apply(left, right);
} catch (Throwable throwable) {
// Restore pageBuilder into a consistent state.
arrayBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throwIfUnchecked(throwable);
throw new RuntimeException(throwable);
}
writeNativeValue(outputElementType, blockBuilder, output);
}
arrayBlockBuilder.closeEntry();
pageBuilder.declarePosition();
return outputArrayType.getObject(arrayBlockBuilder, arrayBlockBuilder.getPositionCount() - 1);
}
use of com.facebook.presto.common.PageBuilder in project presto by prestodb.
the class TestUnnesterUtil method mergePages.
static Page mergePages(List<Type> types, List<Page> pages) {
PageBuilder pageBuilder = new PageBuilder(types);
int totalPositionCount = 0;
for (Page page : pages) {
verify(page.getChannelCount() == types.size(), format("Number of channels in page %d is not equal to number of types %d", page.getChannelCount(), types.size()));
for (int i = 0; i < types.size(); i++) {
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(i);
Block block = page.getBlock(i);
for (int position = 0; position < page.getPositionCount(); position++) {
if (block.isNull(position)) {
blockBuilder.appendNull();
} else {
block.writePositionTo(position, blockBuilder);
}
}
}
totalPositionCount += page.getPositionCount();
}
pageBuilder.declarePositions(totalPositionCount);
return pageBuilder.build();
}
use of com.facebook.presto.common.PageBuilder in project presto by prestodb.
the class TestPagesIndexPageSorter method createOutputPages.
private static List<Page> createOutputPages(List<Type> types, List<Page> inputPages, long[] sortedAddresses) {
PageBuilder pageBuilder = new PageBuilder(types);
pageBuilder.reset();
for (long address : sortedAddresses) {
int index = sorter.decodePageIndex(address);
int position = sorter.decodePositionIndex(address);
Page page = inputPages.get(index);
for (int i = 0; i < types.size(); i++) {
Type type = types.get(i);
type.appendTo(page.getBlock(i), position, pageBuilder.getBlockBuilder(i));
}
pageBuilder.declarePosition();
}
return ImmutableList.of(pageBuilder.build());
}
use of com.facebook.presto.common.PageBuilder in project presto by prestodb.
the class TestBlockBuilder method testNewBlockBuilderLike.
@Test
public void testNewBlockBuilderLike() {
ArrayType longArrayType = new ArrayType(BIGINT);
ArrayType arrayType = new ArrayType(longArrayType);
List<Type> channels = ImmutableList.of(BIGINT, VARCHAR, arrayType);
PageBuilder pageBuilder = new PageBuilder(channels);
BlockBuilder bigintBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder varcharBlockBuilder = pageBuilder.getBlockBuilder(1);
BlockBuilder arrayBlockBuilder = pageBuilder.getBlockBuilder(2);
for (int i = 0; i < 100; i++) {
BIGINT.writeLong(bigintBlockBuilder, i);
VARCHAR.writeSlice(varcharBlockBuilder, Slices.utf8Slice("test" + i));
Block longArrayBlock = new ArrayType(BIGINT).createBlockBuilder(null, 1).appendStructure(BIGINT.createBlockBuilder(null, 2).writeLong(i).closeEntry().writeLong(i * 2).closeEntry().build());
arrayBlockBuilder.appendStructure(longArrayBlock);
pageBuilder.declarePosition();
}
PageBuilder newPageBuilder = pageBuilder.newPageBuilderLike();
for (int i = 0; i < channels.size(); i++) {
assertEquals(newPageBuilder.getType(i), pageBuilder.getType(i));
// we should get new block builder instances
assertNotEquals(pageBuilder.getBlockBuilder(i), newPageBuilder.getBlockBuilder(i));
assertEquals(newPageBuilder.getBlockBuilder(i).getPositionCount(), 0);
assertTrue(newPageBuilder.getBlockBuilder(i).getRetainedSizeInBytes() < pageBuilder.getBlockBuilder(i).getRetainedSizeInBytes());
}
// Test newBlockBuilderLike with expectedEntries
BlockBuilder newBigintBlockBuilder = bigintBlockBuilder.newBlockBuilderLike(null, 200);
assertEquals(newBigintBlockBuilder.getPositionCount(), 0);
assertEquals(newBigintBlockBuilder.getRetainedSizeInBytes(), 80);
newBigintBlockBuilder.writeLong(0);
assertEquals(newBigintBlockBuilder.getPositionCount(), 1);
// Reserved 200 longs and booleans for nulls array
assertEquals(newBigintBlockBuilder.getRetainedSizeInBytes(), 1880);
BlockBuilder newVarcharBlockBuilder = varcharBlockBuilder.newBlockBuilderLike(null, 200);
assertEquals(newVarcharBlockBuilder.getPositionCount(), 0);
assertEquals(newVarcharBlockBuilder.getRetainedSizeInBytes(), 164);
newVarcharBlockBuilder.writeLong(0);
newVarcharBlockBuilder.closeEntry();
assertEquals(newVarcharBlockBuilder.getPositionCount(), 1);
// Reserved 200 varchars of average length 5.9, and 201 ints for offsets and 200 booleans for nulls
assertEquals(newVarcharBlockBuilder.getRetainedSizeInBytes(), 2360);
BlockBuilder newArrayBlockBuilder = arrayBlockBuilder.newBlockBuilderLike(null, 200);
assertEquals(newArrayBlockBuilder.getPositionCount(), 0);
assertEquals(newArrayBlockBuilder.getRetainedSizeInBytes(), 248);
newArrayBlockBuilder.appendStructure(fromElementBlock(1, Optional.empty(), IntStream.range(0, 2).toArray(), newBigintBlockBuilder.build()));
assertEquals(newArrayBlockBuilder.getPositionCount(), 1);
// Reserved 200 ARRAY(ARRAY(BIGINT)), and 201 ints for offsets and 200 booleans for nulls
assertEquals(newArrayBlockBuilder.getRetainedSizeInBytes(), 5848);
}
Aggregations