Search in sources :

Example 16 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

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) Comparator.nullsFirst(java.util.Comparator.nullsFirst) Iterator(java.util.Iterator) Comparator.naturalOrder(java.util.Comparator.naturalOrder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertEquals(org.testng.Assert.assertEquals) Page(io.prestosql.spi.Page) Test(org.testng.annotations.Test) INTEGER(io.prestosql.spi.type.IntegerType.INTEGER) PageBuilder(io.prestosql.spi.PageBuilder) SortOrder(io.prestosql.spi.block.SortOrder) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ImmutableList(com.google.common.collect.ImmutableList) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assert.assertTrue(org.testng.Assert.assertTrue) Type(io.prestosql.spi.type.Type) Assert.assertFalse(org.testng.Assert.assertFalse) ArrayList(java.util.ArrayList) SortOrder(io.prestosql.spi.block.SortOrder) Page(io.prestosql.spi.Page) PageBuilder(io.prestosql.spi.PageBuilder) Type(io.prestosql.spi.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 17 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class ArrayConcatFunction method concat.

@UsedByGeneratedCode
public static Block concat(Type elementType, Object state, Block[] blocks) {
    int resultPositionCount = 0;
    // fast path when there is at most one non empty block
    Block nonEmptyBlock = null;
    for (int i = 0; i < blocks.length; i++) {
        resultPositionCount += blocks[i].getPositionCount();
        if (blocks[i].getPositionCount() > 0) {
            nonEmptyBlock = blocks[i];
        }
    }
    if (nonEmptyBlock == null) {
        return blocks[0];
    }
    if (resultPositionCount == nonEmptyBlock.getPositionCount()) {
        return nonEmptyBlock;
    }
    PageBuilder pageBuilder = (PageBuilder) state;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    for (int blockIndex = 0; blockIndex < blocks.length; blockIndex++) {
        Block block = blocks[blockIndex];
        for (int i = 0; i < block.getPositionCount(); i++) {
            elementType.appendTo(block, i, blockBuilder);
        }
    }
    pageBuilder.declarePositions(resultPositionCount);
    return blockBuilder.getRegion(blockBuilder.getPositionCount() - resultPositionCount, resultPositionCount);
}
Also used : Block(io.prestosql.spi.block.Block) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 18 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class GenericPartitioningSpiller method flush.

private synchronized ListenableFuture<?> flush(int partition) {
    PageBuilder pageBuilder = pageBuilders.get(partition);
    if (pageBuilder.isEmpty()) {
        return Futures.immediateFuture(null);
    }
    Page page = pageBuilder.build();
    pageBuilder.reset();
    return getSpiller(partition).spill(page);
}
Also used : Page(io.prestosql.spi.Page) PageBuilder(io.prestosql.spi.PageBuilder)

Example 19 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class MultiChannelGroupBySort method startNewPage.

private void startNewPage() {
    if (currentPageBuilder != null) {
        completedPagesMemorySize += currentPageBuilder.getRetainedSizeInBytes();
        currentPageBuilder = currentPageBuilder.newPageBuilderLike();
        if (nextGroupIdStartingRange == Integer.MAX_VALUE) {
            nextGroupIdStartingRange = nextSortBasedGroupId;
        } else {
            maxGroupId.add(nextSortBasedGroupId);
        }
    } else {
        currentPageBuilder = new PageBuilder(types);
    }
    for (int i = 0; i < types.size(); i++) {
        channelBuilders.get(i).add(currentPageBuilder.getBlockBuilder(i));
    }
}
Also used : PageBuilder(io.prestosql.spi.PageBuilder)

Example 20 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class GroupAggregationOperator method getGlobalAggregationOutput.

protected Page getGlobalAggregationOutput() {
    List<Accumulator> accumulators = accumulatorFactories.stream().map(AccumulatorFactory::createAccumulator).collect(Collectors.toList());
    // global aggregation output page will only be constructed once,
    // so a new PageBuilder is constructed (instead of using PageBuilder.reset)
    PageBuilder output = new PageBuilder(globalAggregationGroupIds.size(), types);
    for (int groupId : globalAggregationGroupIds) {
        output.declarePosition();
        int channel = 0;
        for (; channel < groupByTypes.size(); channel++) {
            if (channel == groupIdChannel.get()) {
                output.getBlockBuilder(channel).writeLong(groupId);
            } else {
                output.getBlockBuilder(channel).appendNull();
            }
        }
        if (hashChannel.isPresent()) {
            long hashValue = calculateDefaultOutputHash(groupByTypes, groupIdChannel.get(), groupId);
            output.getBlockBuilder(channel++).writeLong(hashValue);
        }
        for (int j = 0; j < accumulators.size(); channel++, j++) {
            if (step.isOutputPartial()) {
                accumulators.get(j).evaluateIntermediate(output.getBlockBuilder(channel));
            } else {
                accumulators.get(j).evaluateFinal(output.getBlockBuilder(channel));
            }
        }
    }
    if (output.isEmpty()) {
        return null;
    }
    return output.build();
}
Also used : Accumulator(io.prestosql.operator.aggregation.Accumulator) PageBuilder(io.prestosql.spi.PageBuilder)

Aggregations

PageBuilder (io.prestosql.spi.PageBuilder)58 Page (io.prestosql.spi.Page)28 BlockBuilder (io.prestosql.spi.block.BlockBuilder)27 Type (io.prestosql.spi.type.Type)24 ImmutableList (com.google.common.collect.ImmutableList)18 Test (org.testng.annotations.Test)14 List (java.util.List)11 Block (io.prestosql.spi.block.Block)10 ArrayList (java.util.ArrayList)9 INTEGER (io.prestosql.spi.type.IntegerType.INTEGER)8 Collectors.toList (java.util.stream.Collectors.toList)8 Slice (io.airlift.slice.Slice)7 Benchmark (org.openjdk.jmh.annotations.Benchmark)7 Slices (io.airlift.slice.Slices)6 ConnectorPageSource (io.prestosql.spi.connector.ConnectorPageSource)6 ConnectorSession (io.prestosql.spi.connector.ConnectorSession)6 ArrayType (io.prestosql.spi.type.ArrayType)6 BIGINT (io.prestosql.spi.type.BigintType.BIGINT)6 DOUBLE (io.prestosql.spi.type.DoubleType.DOUBLE)6 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)6