Search in sources :

Example 76 with Page

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

the class ThriftTpchService method getRowsInternal.

private static PrestoThriftPageResult getRowsInternal(ConnectorPageSource pageSource, String tableName, List<String> columnNames, @Nullable PrestoThriftId nextToken) {
    // very inefficient implementation as it needs to re-generate all previous results to get the next page
    int skipPages = nextToken != null ? Ints.fromByteArray(nextToken.getId()) : 0;
    skipPages(pageSource, skipPages);
    Page page = null;
    while (!pageSource.isFinished() && page == null) {
        page = pageSource.getNextPage();
        skipPages++;
    }
    PrestoThriftId newNextToken = pageSource.isFinished() ? null : new PrestoThriftId(Ints.toByteArray(skipPages));
    return toThriftPage(page, types(tableName, columnNames), newNextToken);
}
Also used : PrestoThriftId(com.facebook.presto.thrift.api.connector.PrestoThriftId) Page(com.facebook.presto.common.Page)

Example 77 with Page

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

the class TestThriftIndexPageSource method testGetNextPageTwoConcurrentRequests.

@Test
public void testGetNextPageTwoConcurrentRequests() throws Exception {
    final int splits = 3;
    final int lookupRequestsConcurrency = 2;
    final int rowsPerSplit = 1;
    List<SettableFuture<PrestoThriftPageResult>> futures = IntStream.range(0, splits).mapToObj(i -> SettableFuture.<PrestoThriftPageResult>create()).collect(toImmutableList());
    List<CountDownLatch> signals = IntStream.range(0, splits).mapToObj(i -> new CountDownLatch(1)).collect(toImmutableList());
    TestingThriftService client = new TestingThriftService(rowsPerSplit, false, false) {

        @Override
        public ListenableFuture<PrestoThriftPageResult> getRows(PrestoThriftId splitId, List<String> columns, long maxBytes, PrestoThriftNullableToken nextToken) {
            int key = Ints.fromByteArray(splitId.getId());
            signals.get(key).countDown();
            return futures.get(key);
        }
    };
    ThriftConnectorStats stats = new ThriftConnectorStats();
    long pageSizeReceived = 0;
    ThriftIndexPageSource pageSource = new ThriftIndexPageSource((context, headers) -> client, ImmutableMap.of(), stats, new ThriftIndexHandle(new SchemaTableName("default", "table1"), TupleDomain.all()), ImmutableList.of(column("a", INTEGER)), ImmutableList.of(column("b", INTEGER)), new InMemoryRecordSet(ImmutableList.of(INTEGER), generateKeys(0, splits)), MAX_BYTES_PER_RESPONSE, lookupRequestsConcurrency);
    assertNull(pageSource.getNextPage());
    assertEquals((long) stats.getIndexPageSize().getAllTime().getTotal(), 0);
    signals.get(0).await(1, SECONDS);
    signals.get(1).await(1, SECONDS);
    signals.get(2).await(1, SECONDS);
    assertEquals(signals.get(0).getCount(), 0, "first request wasn't sent");
    assertEquals(signals.get(1).getCount(), 0, "second request wasn't sent");
    assertEquals(signals.get(2).getCount(), 1, "third request shouldn't be sent");
    // at this point first two requests were sent
    assertFalse(pageSource.isFinished());
    assertNull(pageSource.getNextPage());
    assertEquals((long) stats.getIndexPageSize().getAllTime().getTotal(), 0);
    // completing the second request
    futures.get(1).set(pageResult(20, null));
    Page page = pageSource.getNextPage();
    pageSizeReceived += page.getSizeInBytes();
    assertEquals((long) stats.getIndexPageSize().getAllTime().getTotal(), pageSizeReceived);
    assertNotNull(page);
    assertEquals(page.getPositionCount(), 1);
    assertEquals(page.getBlock(0).getInt(0), 20);
    // not complete yet
    assertFalse(pageSource.isFinished());
    // once one of the requests completes the next one should be sent
    signals.get(2).await(1, SECONDS);
    assertEquals(signals.get(2).getCount(), 0, "third request wasn't sent");
    // completing the first request
    futures.get(0).set(pageResult(10, null));
    page = pageSource.getNextPage();
    assertNotNull(page);
    pageSizeReceived += page.getSizeInBytes();
    assertEquals((long) stats.getIndexPageSize().getAllTime().getTotal(), pageSizeReceived);
    assertEquals(page.getPositionCount(), 1);
    assertEquals(page.getBlock(0).getInt(0), 10);
    // still not complete
    assertFalse(pageSource.isFinished());
    // completing the third request
    futures.get(2).set(pageResult(30, null));
    page = pageSource.getNextPage();
    assertNotNull(page);
    pageSizeReceived += page.getSizeInBytes();
    assertEquals((long) stats.getIndexPageSize().getAllTime().getTotal(), pageSizeReceived);
    assertEquals(page.getPositionCount(), 1);
    assertEquals(page.getBlock(0).getInt(0), 30);
    // finished now
    assertTrue(pageSource.isFinished());
    // after completion
    assertNull(pageSource.getNextPage());
    pageSource.close();
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) IntStream(java.util.stream.IntStream) Collections.shuffle(java.util.Collections.shuffle) PrestoThriftNullableSchemaName(com.facebook.presto.thrift.api.connector.PrestoThriftNullableSchemaName) Page(com.facebook.presto.common.Page) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Assert.assertNull(org.testng.Assert.assertNull) PrestoThriftNullableTableMetadata(com.facebook.presto.thrift.api.connector.PrestoThriftNullableTableMetadata) PrestoThriftNullableColumnSet(com.facebook.presto.thrift.api.connector.PrestoThriftNullableColumnSet) PrestoThriftBlock.integerData(com.facebook.presto.thrift.api.datatypes.PrestoThriftBlock.integerData) Assert.assertEquals(org.testng.Assert.assertEquals) PrestoThriftSplitBatch(com.facebook.presto.thrift.api.connector.PrestoThriftSplitBatch) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) SettableFuture(com.google.common.util.concurrent.SettableFuture) PrestoThriftPageResult(com.facebook.presto.thrift.api.connector.PrestoThriftPageResult) InMemoryRecordSet(com.facebook.presto.spi.InMemoryRecordSet) ArrayList(java.util.ArrayList) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ImmutableList(com.google.common.collect.ImmutableList) PrestoThriftSchemaTableName(com.facebook.presto.thrift.api.connector.PrestoThriftSchemaTableName) PrestoThriftNullableToken(com.facebook.presto.thrift.api.connector.PrestoThriftNullableToken) PrestoThriftTupleDomain(com.facebook.presto.thrift.api.connector.PrestoThriftTupleDomain) Assert.assertFalse(org.testng.Assert.assertFalse) Type(com.facebook.presto.common.type.Type) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) PrestoThriftService(com.facebook.presto.thrift.api.connector.PrestoThriftService) PrestoThriftServiceException(com.facebook.presto.thrift.api.connector.PrestoThriftServiceException) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Assert.assertNotNull(org.testng.Assert.assertNotNull) PrestoThriftId(com.facebook.presto.thrift.api.connector.PrestoThriftId) PrestoThriftSplit(com.facebook.presto.thrift.api.connector.PrestoThriftSplit) Ints(com.google.common.primitives.Ints) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) PrestoThriftInteger(com.facebook.presto.thrift.api.datatypes.PrestoThriftInteger) Assert.assertTrue(org.testng.Assert.assertTrue) Block(com.facebook.presto.common.block.Block) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) PrestoThriftId(com.facebook.presto.thrift.api.connector.PrestoThriftId) PrestoThriftNullableToken(com.facebook.presto.thrift.api.connector.PrestoThriftNullableToken) PrestoThriftPageResult(com.facebook.presto.thrift.api.connector.PrestoThriftPageResult) Page(com.facebook.presto.common.Page) CountDownLatch(java.util.concurrent.CountDownLatch) SchemaTableName(com.facebook.presto.spi.SchemaTableName) PrestoThriftSchemaTableName(com.facebook.presto.thrift.api.connector.PrestoThriftSchemaTableName) InMemoryRecordSet(com.facebook.presto.spi.InMemoryRecordSet) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Example 78 with Page

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

the class ThriftIndexPageSource method getNextPage.

@Override
public Page getNextPage() {
    if (finished) {
        return null;
    }
    if (!loadAllSplits()) {
        return null;
    }
    // check if any data requests were started
    if (dataSignalFuture == null) {
        // no data requests were started, start a number of initial requests
        checkState(contexts.isEmpty() && dataRequests.isEmpty(), "some splits are already started");
        if (splits.isEmpty()) {
            // all done: no splits
            finished = true;
            return null;
        }
        for (int i = 0; i < min(lookupRequestsConcurrency, splits.size()); i++) {
            startDataFetchForNextSplit();
        }
        updateSignalAndStatusFutures();
    }
    // check if any data request is finished
    if (!dataSignalFuture.isDone()) {
        // not finished yet
        return null;
    }
    // at least one of data requests completed
    ListenableFuture<PrestoThriftPageResult> resultFuture = getAndRemoveNextCompletedRequest();
    RunningSplitContext resultContext = contexts.remove(resultFuture);
    checkState(resultContext != null, "no associated context for the request");
    PrestoThriftPageResult pageResult = getFutureValue(resultFuture);
    Page page = pageResult.toPage(outputColumnTypes);
    if (page != null) {
        long pageSize = page.getSizeInBytes();
        completedBytes += pageSize;
        completedPositions += page.getPositionCount();
        stats.addIndexPageSize(pageSize);
    } else {
        stats.addIndexPageSize(0);
    }
    if (pageResult.getNextToken() != null) {
        // can get more data
        sendDataRequest(resultContext, pageResult.getNextToken());
        updateSignalAndStatusFutures();
        return page;
    }
    // are there more splits available
    if (splitIndex < splits.size()) {
        // can send data request for a new split
        startDataFetchForNextSplit();
        updateSignalAndStatusFutures();
    } else if (!dataRequests.isEmpty()) {
        // no more new splits, but some requests are still in progress, wait for them
        updateSignalAndStatusFutures();
    } else {
        // all done: no more new splits, no requests in progress
        dataSignalFuture = null;
        statusFuture = null;
        finished = true;
    }
    return page;
}
Also used : PrestoThriftPageResult(com.facebook.presto.thrift.api.connector.PrestoThriftPageResult) Page(com.facebook.presto.common.Page)

Example 79 with Page

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

the class TempFileReader method computeNext.

@Override
protected Page computeNext() {
    try {
        if (Thread.currentThread().isInterrupted()) {
            throw new InterruptedIOException();
        }
        int batchSize = reader.nextBatch();
        if (batchSize <= 0) {
            return endOfData();
        }
        Block[] blocks = new Block[columnCount];
        for (int i = 0; i < columnCount; i++) {
            blocks[i] = reader.readBlock(i).getLoadedBlock();
        }
        return new Page(batchSize, blocks);
    } catch (IOException e) {
        throw new PrestoException(HIVE_WRITER_DATA_ERROR, "Failed to read temporary data");
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page) PrestoException(com.facebook.presto.spi.PrestoException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 80 with Page

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

the class AbstractTestHiveFileFormats method assertFileStatistics.

private static void assertFileStatistics(Optional<Page> fileStatistics, long writtenBytes, HiveStorageFormat storageFormat) {
    if (storageFormat == ORC || storageFormat == DWRF) {
        assertTrue(fileStatistics.isPresent());
        Page statisticsPage = fileStatistics.get();
        assertEquals(statisticsPage.getPositionCount(), 1);
        assertEquals(writtenBytes, getFileSize(statisticsPage, 0));
    }
}
Also used : Page(com.facebook.presto.common.Page)

Aggregations

Page (com.facebook.presto.common.Page)545 Test (org.testng.annotations.Test)273 Block (com.facebook.presto.common.block.Block)146 Type (com.facebook.presto.common.type.Type)129 MaterializedResult (com.facebook.presto.testing.MaterializedResult)102 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)89 ImmutableList (com.google.common.collect.ImmutableList)73 DataSize (io.airlift.units.DataSize)69 RowPagesBuilder (com.facebook.presto.RowPagesBuilder)65 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)52 ArrayList (java.util.ArrayList)50 List (java.util.List)48 Optional (java.util.Optional)44 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)43 OperatorAssertion.toMaterializedResult (com.facebook.presto.operator.OperatorAssertion.toMaterializedResult)38 PrestoException (com.facebook.presto.spi.PrestoException)38 TestingTaskContext (com.facebook.presto.testing.TestingTaskContext)36 ArrayType (com.facebook.presto.common.type.ArrayType)35 IOException (java.io.IOException)31 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)29