Search in sources :

Example 1 with PageBuilder

use of com.facebook.presto.common.PageBuilder in project presto by prestodb.

the class PrestoSparkShufflePageInput method createPage.

private static Page createPage(int rowCount, BasicSliceInput input, List<Type> types) {
    checkArgument(rowCount > 0, "rowCount must be greater than zero: %s", rowCount);
    if (input.length() == 0) {
        // zero column page
        verify(types.isEmpty(), "types is expected to be empty");
        return new Page(rowCount);
    }
    PageBuilder pageBuilder = new PageBuilder(types);
    while (input.isReadable()) {
        pageBuilder.declarePosition();
        for (int channel = 0; channel < types.size(); channel++) {
            BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(channel);
            blockBuilder.readPositionFrom(input);
        }
    }
    Page page = pageBuilder.build();
    verify(page.getPositionCount() == rowCount, "unexpected row count: %s != %s", page.getPositionCount(), rowCount);
    return page;
}
Also used : Page(com.facebook.presto.common.Page) PageBuilder(com.facebook.presto.common.PageBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 2 with PageBuilder

use of com.facebook.presto.common.PageBuilder in project presto by prestodb.

the class TestMergingPageIterator method testMerging.

@Test
public void testMerging() {
    List<Type> types = ImmutableList.of(INTEGER, INTEGER);
    List<Integer> sortIndexes = ImmutableList.of(1);
    List<SortOrder> sortOrders = ImmutableList.of(SortOrder.ASC_NULLS_FIRST);
    List<List<Page>> pageLists = new ArrayList<>();
    PageBuilder pageBuilder = new PageBuilder(types);
    for (int i = 0; i < 10; i++) {
        Iterator<Integer> values = IntStream.range(0, 1000).map(ignored -> ThreadLocalRandom.current().nextInt(100_000)).mapToObj(n -> ((n % 100) == 0) ? null : n).sorted(nullsFirst(naturalOrder())).iterator();
        List<Page> pages = new ArrayList<>();
        for (int j = 0; j < 10; j++) {
            for (int k = 0; k < 100; k++) {
                Integer n = values.next();
                pageBuilder.declarePosition();
                if (n == null) {
                    pageBuilder.getBlockBuilder(0).appendNull();
                    pageBuilder.getBlockBuilder(1).appendNull();
                } else {
                    INTEGER.writeLong(pageBuilder.getBlockBuilder(0), n);
                    INTEGER.writeLong(pageBuilder.getBlockBuilder(1), n * 22L);
                }
            }
            pages.add(pageBuilder.build());
            pageBuilder.reset();
        }
        pageLists.add(pages);
        assertFalse(values.hasNext());
    }
    List<Iterator<Page>> pages = pageLists.stream().map(List::iterator).collect(toList());
    Iterator<Page> iterator = new MergingPageIterator(pages, types, sortIndexes, sortOrders);
    List<Long> values = new ArrayList<>();
    while (iterator.hasNext()) {
        Page page = iterator.next();
        for (int i = 0; i < page.getPositionCount(); i++) {
            if (page.getBlock(0).isNull(i)) {
                assertTrue(page.getBlock(1).isNull(i));
                values.add(null);
            } else {
                long x = INTEGER.getLong(page.getBlock(0), i);
                long y = INTEGER.getLong(page.getBlock(1), i);
                assertEquals(y, x * 22);
                values.add(x);
            }
        }
    }
    assertThat(values).isSortedAccordingTo(nullsFirst(naturalOrder()));
}
Also used : IntStream(java.util.stream.IntStream) Page(com.facebook.presto.common.Page) Comparator.nullsFirst(java.util.Comparator.nullsFirst) Iterator(java.util.Iterator) Comparator.naturalOrder(java.util.Comparator.naturalOrder) SortOrder(com.facebook.presto.common.block.SortOrder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) PageBuilder(com.facebook.presto.common.PageBuilder) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ImmutableList(com.google.common.collect.ImmutableList) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assert.assertTrue(org.testng.Assert.assertTrue) Assert.assertFalse(org.testng.Assert.assertFalse) Type(com.facebook.presto.common.type.Type) ArrayList(java.util.ArrayList) SortOrder(com.facebook.presto.common.block.SortOrder) Page(com.facebook.presto.common.Page) PageBuilder(com.facebook.presto.common.PageBuilder) Type(com.facebook.presto.common.type.Type) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ImmutableList(com.google.common.collect.ImmutableList) Test(org.testng.annotations.Test)

Example 3 with PageBuilder

use of com.facebook.presto.common.PageBuilder in project presto by prestodb.

the class TestGroupByHash method testAppendToMultipleTuplesPerGroup.

@Test
public void testAppendToMultipleTuplesPerGroup() {
    List<Long> values = new ArrayList<>();
    for (long i = 0; i < 100; i++) {
        values.add(i % 50);
    }
    Block valuesBlock = BlockAssertions.createLongsBlock(values);
    Block hashBlock = TypeUtils.getHashBlock(ImmutableList.of(BIGINT), valuesBlock);
    GroupByHash groupByHash = createGroupByHash(TEST_SESSION, ImmutableList.of(BIGINT), new int[] { 0 }, Optional.of(1), 100, JOIN_COMPILER);
    groupByHash.getGroupIds(new Page(valuesBlock, hashBlock)).process();
    assertEquals(groupByHash.getGroupCount(), 50);
    PageBuilder pageBuilder = new PageBuilder(groupByHash.getTypes());
    for (int i = 0; i < groupByHash.getGroupCount(); i++) {
        pageBuilder.declarePosition();
        groupByHash.appendValuesTo(i, pageBuilder, 0);
    }
    Page outputPage = pageBuilder.build();
    assertEquals(outputPage.getPositionCount(), 50);
    BlockAssertions.assertBlockEquals(BIGINT, outputPage.getBlock(0), BlockAssertions.createLongSequenceBlock(0, 50));
}
Also used : ArrayList(java.util.ArrayList) GroupByHash.createGroupByHash(com.facebook.presto.operator.GroupByHash.createGroupByHash) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) BlockAssertions.createLongSequenceBlock(com.facebook.presto.block.BlockAssertions.createLongSequenceBlock) BlockAssertions.createStringSequenceBlock(com.facebook.presto.block.BlockAssertions.createStringSequenceBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) TypeUtils.getHashBlock(com.facebook.presto.type.TypeUtils.getHashBlock) Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page) PageBuilder(com.facebook.presto.common.PageBuilder) Test(org.testng.annotations.Test)

Example 4 with PageBuilder

use of com.facebook.presto.common.PageBuilder in project presto by prestodb.

the class BenchmarkGroupedTopNBuilder method createInputPages.

private static List<Page> createInputPages(int positions, List<Type> types, int positionsPerPage, int groupCount, int seed) {
    Random random = new Random(seed);
    List<Page> pages = new ArrayList<>();
    PageBuilder pageBuilder = new PageBuilder(types);
    LineItemGenerator lineItemGenerator = new LineItemGenerator(1, 1, 1);
    Iterator<LineItem> iterator = lineItemGenerator.iterator();
    for (int i = 0; i < positions; i++) {
        pageBuilder.declarePosition();
        LineItem lineItem = iterator.next();
        BIGINT.writeLong(pageBuilder.getBlockBuilder(HASH_GROUP), groupCount > 1 ? random.nextInt(groupCount) : 1);
        DOUBLE.writeDouble(pageBuilder.getBlockBuilder(EXTENDED_PRICE), lineItem.getExtendedPrice());
        DOUBLE.writeDouble(pageBuilder.getBlockBuilder(DISCOUNT), lineItem.getDiscount());
        DATE.writeLong(pageBuilder.getBlockBuilder(SHIP_DATE), lineItem.getShipDate());
        DOUBLE.writeDouble(pageBuilder.getBlockBuilder(QUANTITY), lineItem.getQuantity());
        if (pageBuilder.getPositionCount() >= positionsPerPage) {
            pages.add(pageBuilder.build());
            pageBuilder.reset();
        }
    }
    if (!pageBuilder.isEmpty()) {
        pages.add(pageBuilder.build());
    }
    return pages;
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) LineItem(io.airlift.tpch.LineItem) Page(com.facebook.presto.common.Page) PageBuilder(com.facebook.presto.common.PageBuilder) LineItemGenerator(io.airlift.tpch.LineItemGenerator)

Example 5 with PageBuilder

use of com.facebook.presto.common.PageBuilder in project presto by prestodb.

the class PinotBrokerPageSourcePql method buildBlockAndTypeBuilder.

@Override
@VisibleForTesting
public BlockAndTypeBuilder buildBlockAndTypeBuilder(List<PinotColumnHandle> columnHandles, PinotQueryGenerator.GeneratedPinotQuery brokerPql) {
    // When we created the PQL, we came up with some column handles
    // however other optimizers post-pushdown can come in and prune/re-order the required column handles
    // so we need to map from the column handles the PQL corresponds to, to the actual column handles
    // needed in the scan.
    List<Type> expectedTypes = columnHandles.stream().map(PinotColumnHandle::getDataType).collect(Collectors.toList());
    PageBuilder pageBuilder = new PageBuilder(expectedTypes);
    checkState(brokerPql.getExpectedColumnIndices().size() >= expectedHandles.size());
    checkState(expectedHandles.size() >= columnHandles.size());
    // The expectedColumnHandles are the handles corresponding to the generated PQL
    // However, the engine could end up requesting only a permutation/subset of those handles
    // during the actual scan
    // Map the handles from planning time to the handles asked in the scan
    // so that we know which columns to discard.
    int[] handleMapping = new int[expectedHandles.size()];
    for (int i = 0; i < handleMapping.length; ++i) {
        handleMapping[i] = columnHandles.indexOf(expectedHandles.get(i));
    }
    ArrayList<BlockBuilder> columnBlockBuilders = new ArrayList<>();
    ArrayList<Type> columnTypes = new ArrayList<>();
    for (int expectedColumnIndex : brokerPql.getExpectedColumnIndices()) {
        // columnIndex is the index of this column in the current scan
        // It is obtained from the mapping and can be -ve, which means that the
        // expectedColumnIndex'th column returned by Pinot can be discarded.
        int columnIndex = -1;
        if (expectedColumnIndex >= 0) {
            columnIndex = handleMapping[expectedColumnIndex];
        }
        columnBlockBuilders.add(columnIndex >= 0 ? pageBuilder.getBlockBuilder(columnIndex) : null);
        columnTypes.add(columnIndex >= 0 ? expectedTypes.get(columnIndex) : null);
    }
    return new BlockAndTypeBuilder(pageBuilder, columnBlockBuilders, columnTypes);
}
Also used : Type(com.facebook.presto.common.type.Type) ArrayList(java.util.ArrayList) PageBuilder(com.facebook.presto.common.PageBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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