Search in sources :

Example 26 with PageBuilder

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;
}
Also used : Field(java.lang.reflect.Field) BlockAssertions.createIntsBlock(com.facebook.presto.block.BlockAssertions.createIntsBlock) BlockAssertions.createRowBlock(com.facebook.presto.block.BlockAssertions.createRowBlock) BlockAssertions.createBooleansBlock(com.facebook.presto.block.BlockAssertions.createBooleansBlock) BlockAssertions.createDoublesBlock(com.facebook.presto.block.BlockAssertions.createDoublesBlock) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) BlockAssertions.createSlicesBlock(com.facebook.presto.block.BlockAssertions.createSlicesBlock) BlockAssertions.createTimestampsWithTimezoneBlock(com.facebook.presto.block.BlockAssertions.createTimestampsWithTimezoneBlock) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) Block(com.facebook.presto.common.block.Block) PageBuilder(com.facebook.presto.common.PageBuilder)

Example 27 with PageBuilder

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);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) PageBuilder(com.facebook.presto.common.PageBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 28 with PageBuilder

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();
}
Also used : Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page) PageBuilder(com.facebook.presto.common.PageBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 29 with PageBuilder

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());
}
Also used : Type(com.facebook.presto.common.type.Type) Page(com.facebook.presto.common.Page) PageBuilder(com.facebook.presto.common.PageBuilder)

Example 30 with PageBuilder

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);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) Block(com.facebook.presto.common.block.Block) PageBuilder(com.facebook.presto.common.PageBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Aggregations

PageBuilder (com.facebook.presto.common.PageBuilder)57 Page (com.facebook.presto.common.Page)27 Type (com.facebook.presto.common.type.Type)25 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)23 ImmutableList (com.google.common.collect.ImmutableList)14 Block (com.facebook.presto.common.block.Block)11 Test (org.testng.annotations.Test)11 ArrayType (com.facebook.presto.common.type.ArrayType)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Benchmark (org.openjdk.jmh.annotations.Benchmark)7 INTEGER (com.facebook.presto.common.type.IntegerType.INTEGER)4 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)3 DOUBLE (com.facebook.presto.common.type.DoubleType.DOUBLE)3 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)3 ConnectorPageSource (com.facebook.presto.spi.ConnectorPageSource)3 ConnectorSession (com.facebook.presto.spi.ConnectorSession)3 MaterializedResult (com.facebook.presto.testing.MaterializedResult)3 Slices (io.airlift.slice.Slices)3 File (java.io.File)3