Search in sources :

Example 6 with FixedPageSource

use of io.trino.spi.connector.FixedPageSource in project trino by trinodb.

the class MemoryPageSourceProvider method createPageSource.

@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transaction, ConnectorSession session, ConnectorSplit split, ConnectorTableHandle table, List<ColumnHandle> columns, DynamicFilter dynamicFilter) {
    MemorySplit memorySplit = (MemorySplit) split;
    long tableId = memorySplit.getTable();
    int partNumber = memorySplit.getPartNumber();
    int totalParts = memorySplit.getTotalPartsPerWorker();
    long expectedRows = memorySplit.getExpectedRows();
    MemoryTableHandle memoryTable = (MemoryTableHandle) table;
    OptionalDouble sampleRatio = memoryTable.getSampleRatio();
    List<Integer> columnIndexes = columns.stream().map(MemoryColumnHandle.class::cast).map(MemoryColumnHandle::getColumnIndex).collect(toList());
    List<Page> pages = pagesStore.getPages(tableId, partNumber, totalParts, columnIndexes, expectedRows, memorySplit.getLimit(), sampleRatio);
    return new DynamicFilteringPageSource(new FixedPageSource(pages), columns, dynamicFilter, enableLazyDynamicFiltering);
}
Also used : Page(io.trino.spi.Page) FixedPageSource(io.trino.spi.connector.FixedPageSource) OptionalDouble(java.util.OptionalDouble)

Example 7 with FixedPageSource

use of io.trino.spi.connector.FixedPageSource in project trino by trinodb.

the class TestScanFilterAndProjectOperator method testPageYield.

@Test
public void testPageYield() {
    int totalRows = 1000;
    Page input = SequencePageBuilder.createSequencePage(ImmutableList.of(BIGINT), totalRows, 1);
    DriverContext driverContext = newDriverContext();
    // 20 columns; each column is associated with a function that will force yield per projection
    int totalColumns = 20;
    ImmutableList.Builder<SqlScalarFunction> functions = ImmutableList.builder();
    for (int i = 0; i < totalColumns; i++) {
        functions.add(new GenericLongFunction("page_col" + i, value -> {
            driverContext.getYieldSignal().forceYieldForTesting();
            return value;
        }));
    }
    functionAssertions.addFunctions(new InternalFunctionBundle(functions.build()));
    // match each column with a projection
    ExpressionCompiler expressionCompiler = new ExpressionCompiler(functionAssertions.getFunctionManager(), new PageFunctionCompiler(functionAssertions.getFunctionManager(), 0));
    ImmutableList.Builder<RowExpression> projections = ImmutableList.builder();
    for (int i = 0; i < totalColumns; i++) {
        projections.add(call(functionAssertions.getMetadata().resolveFunction(session, QualifiedName.of("generic_long_page_col" + i), fromTypes(BIGINT)), field(0, BIGINT)));
    }
    Supplier<CursorProcessor> cursorProcessor = expressionCompiler.compileCursorProcessor(Optional.empty(), projections.build(), "key");
    Supplier<PageProcessor> pageProcessor = expressionCompiler.compilePageProcessor(Optional.empty(), projections.build(), MAX_BATCH_SIZE);
    ScanFilterAndProjectOperator.ScanFilterAndProjectOperatorFactory factory = new ScanFilterAndProjectOperator.ScanFilterAndProjectOperatorFactory(0, new PlanNodeId("test"), new PlanNodeId("0"), (session, split, table, columns, dynamicFilter) -> new FixedPageSource(ImmutableList.of(input)), cursorProcessor, pageProcessor, TEST_TABLE_HANDLE, ImmutableList.of(), DynamicFilter.EMPTY, ImmutableList.of(BIGINT), DataSize.ofBytes(0), 0);
    SourceOperator operator = factory.createOperator(driverContext);
    operator.addSplit(new Split(new CatalogName("test"), TestingSplit.createLocalSplit(), Lifespan.taskWide()));
    operator.noMoreSplits();
    // exactly 20 blocks (one for each column) and the PageProcessor will be able to create a Page out of it.
    for (int i = 1; i <= totalRows * totalColumns; i++) {
        driverContext.getYieldSignal().setWithDelay(SECONDS.toNanos(1000), driverContext.getYieldExecutor());
        Page page = operator.getOutput();
        if (i == totalColumns) {
            assertNotNull(page);
            assertEquals(page.getPositionCount(), totalRows);
            assertEquals(page.getChannelCount(), totalColumns);
            for (int j = 0; j < totalColumns; j++) {
                assertEquals(toValues(BIGINT, page.getBlock(j)), toValues(BIGINT, input.getBlock(0)));
            }
        } else {
            assertNull(page);
        }
        driverContext.getYieldSignal().reset();
    }
}
Also used : MaterializedResult(io.trino.testing.MaterializedResult) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) BlockAssertions(io.trino.block.BlockAssertions) Test(org.testng.annotations.Test) Expressions.field(io.trino.sql.relational.Expressions.field) LazyPagePageProjection(io.trino.operator.project.TestPageProcessor.LazyPagePageProjection) SequencePageBuilder(io.trino.SequencePageBuilder) LazyBlock(io.trino.spi.block.LazyBlock) CatalogName(io.trino.connector.CatalogName) Block(io.trino.spi.block.Block) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) Executors.newScheduledThreadPool(java.util.concurrent.Executors.newScheduledThreadPool) CursorProcessor(io.trino.operator.project.CursorProcessor) TEST_SESSION(io.trino.SessionTestUtils.TEST_SESSION) PageRecordSet(io.trino.operator.index.PageRecordSet) RowPagesBuilder.rowPagesBuilder(io.trino.RowPagesBuilder.rowPagesBuilder) KILOBYTE(io.airlift.units.DataSize.Unit.KILOBYTE) EQUAL(io.trino.spi.function.OperatorType.EQUAL) ConnectorPageSource(io.trino.spi.connector.ConnectorPageSource) Assert.assertEquals(io.trino.testing.assertions.Assert.assertEquals) Expressions.constant(io.trino.sql.relational.Expressions.constant) PageFunctionCompiler(io.trino.sql.gen.PageFunctionCompiler) SelectAllFilter(io.trino.operator.project.TestPageProcessor.SelectAllFilter) Assert.assertNotNull(org.testng.Assert.assertNotNull) AbstractTestFunctions(io.trino.operator.scalar.AbstractTestFunctions) DataSize(io.airlift.units.DataSize) List(java.util.List) FixedPageSource(io.trino.spi.connector.FixedPageSource) BIGINT(io.trino.spi.type.BigintType.BIGINT) PageAssertions.assertPageEquals(io.trino.operator.PageAssertions.assertPageEquals) Split(io.trino.metadata.Split) DynamicFilter(io.trino.spi.connector.DynamicFilter) Optional(java.util.Optional) BlockAssertions.toValues(io.trino.block.BlockAssertions.toValues) Assert.assertNull(org.testng.Assert.assertNull) Page(io.trino.spi.Page) Supplier(java.util.function.Supplier) ExpressionCompiler(io.trino.sql.gen.ExpressionCompiler) MAX_BATCH_SIZE(io.trino.operator.project.PageProcessor.MAX_BATCH_SIZE) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) Threads.daemonThreadsNamed(io.airlift.concurrent.Threads.daemonThreadsNamed) RecordPageSource(io.trino.spi.connector.RecordPageSource) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PageProcessor(io.trino.operator.project.PageProcessor) TestingSplit(io.trino.testing.TestingSplit) Lifespan(io.trino.execution.Lifespan) ExecutorService(java.util.concurrent.ExecutorService) TestingTaskContext.createTaskContext(io.trino.testing.TestingTaskContext.createTaskContext) AfterClass(org.testng.annotations.AfterClass) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) FunctionManager(io.trino.metadata.FunctionManager) QualifiedName(io.trino.sql.tree.QualifiedName) OperatorAssertion.toMaterializedResult(io.trino.operator.OperatorAssertion.toMaterializedResult) Expressions.call(io.trino.sql.relational.Expressions.call) RowExpression(io.trino.sql.relational.RowExpression) InternalFunctionBundle(io.trino.metadata.InternalFunctionBundle) TEST_TABLE_HANDLE(io.trino.testing.TestingHandles.TEST_TABLE_HANDLE) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) Assert.assertTrue(org.testng.Assert.assertTrue) SECONDS(java.util.concurrent.TimeUnit.SECONDS) PageFunctionCompiler(io.trino.sql.gen.PageFunctionCompiler) CursorProcessor(io.trino.operator.project.CursorProcessor) ImmutableList(com.google.common.collect.ImmutableList) Page(io.trino.spi.Page) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) PageProcessor(io.trino.operator.project.PageProcessor) InternalFunctionBundle(io.trino.metadata.InternalFunctionBundle) RowExpression(io.trino.sql.relational.RowExpression) FixedPageSource(io.trino.spi.connector.FixedPageSource) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) ExpressionCompiler(io.trino.sql.gen.ExpressionCompiler) CatalogName(io.trino.connector.CatalogName) Split(io.trino.metadata.Split) TestingSplit(io.trino.testing.TestingSplit) Test(org.testng.annotations.Test)

Example 8 with FixedPageSource

use of io.trino.spi.connector.FixedPageSource in project trino by trinodb.

the class TestDriver method testMemoryRevocationRace.

@Test
public void testMemoryRevocationRace() {
    List<Type> types = ImmutableList.of(VARCHAR, BIGINT, BIGINT);
    TableScanOperator source = new AlwaysBlockedMemoryRevokingTableScanOperator(driverContext.addOperatorContext(99, new PlanNodeId("test"), "scan"), new PlanNodeId("source"), (session, split, table, columns, dynamicFilter) -> new FixedPageSource(rowPagesBuilder(types).addSequencePage(10, 20, 30, 40).build()), TEST_TABLE_HANDLE, ImmutableList.of());
    Driver driver = Driver.createDriver(driverContext, source, createSinkOperator(types));
    // the table scan operator will request memory revocation with requestMemoryRevoking()
    // while the driver is still not done with the processFor() method and before it moves to
    // updateDriverBlockedFuture() method.
    assertTrue(driver.processFor(new Duration(100, TimeUnit.MILLISECONDS)).isDone());
}
Also used : PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) Type(io.trino.spi.type.Type) Duration(io.airlift.units.Duration) FixedPageSource(io.trino.spi.connector.FixedPageSource) Test(org.testng.annotations.Test)

Example 9 with FixedPageSource

use of io.trino.spi.connector.FixedPageSource in project trino by trinodb.

the class TestDriver method testBrokenOperatorAddSource.

@Test
public void testBrokenOperatorAddSource() throws Exception {
    PlanNodeId sourceId = new PlanNodeId("source");
    List<Type> types = ImmutableList.of(VARCHAR, BIGINT, BIGINT);
    // create a table scan operator that does not block, which will cause the driver loop to busy wait
    TableScanOperator source = new NotBlockedTableScanOperator(driverContext.addOperatorContext(99, new PlanNodeId("test"), "values"), sourceId, (session, split, table, columns, dynamicFilter) -> new FixedPageSource(rowPagesBuilder(types).addSequencePage(10, 20, 30, 40).build()), TEST_TABLE_HANDLE, ImmutableList.of());
    BrokenOperator brokenOperator = new BrokenOperator(driverContext.addOperatorContext(0, new PlanNodeId("test"), "source"));
    Driver driver = Driver.createDriver(driverContext, source, brokenOperator);
    // block thread in operator processing
    Future<Boolean> driverProcessFor = executor.submit(() -> driver.processFor(new Duration(1, TimeUnit.MILLISECONDS)).isDone());
    brokenOperator.waitForLocked();
    assertSame(driver.getDriverContext(), driverContext);
    assertFalse(driver.isFinished());
    // processFor always returns NOT_BLOCKED, because DriveLockResult was not acquired
    assertTrue(driver.processFor(new Duration(1, TimeUnit.MILLISECONDS)).isDone());
    assertFalse(driver.isFinished());
    driver.updateSplitAssignment(new SplitAssignment(sourceId, ImmutableSet.of(new ScheduledSplit(0, sourceId, newMockSplit())), true));
    assertFalse(driver.isFinished());
    // processFor always returns NOT_BLOCKED, because DriveLockResult was not acquired
    assertTrue(driver.processFor(new Duration(1, TimeUnit.SECONDS)).isDone());
    assertFalse(driver.isFinished());
    driver.close();
    assertTrue(driver.isFinished());
    assertThatThrownBy(() -> driverProcessFor.get(1, TimeUnit.SECONDS)).isInstanceOf(ExecutionException.class).hasCause(new TrinoException(GENERIC_INTERNAL_ERROR, "Driver was interrupted"));
}
Also used : ScheduledSplit(io.trino.execution.ScheduledSplit) Duration(io.airlift.units.Duration) SplitAssignment(io.trino.execution.SplitAssignment) FixedPageSource(io.trino.spi.connector.FixedPageSource) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) Type(io.trino.spi.type.Type) TrinoException(io.trino.spi.TrinoException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 10 with FixedPageSource

use of io.trino.spi.connector.FixedPageSource in project trino by trinodb.

the class TestDriver method testAddSourceFinish.

@Test
public void testAddSourceFinish() {
    PlanNodeId sourceId = new PlanNodeId("source");
    List<Type> types = ImmutableList.of(VARCHAR, BIGINT, BIGINT);
    TableScanOperator source = new TableScanOperator(driverContext.addOperatorContext(99, new PlanNodeId("test"), "values"), sourceId, (session, split, table, columns, dynamicFilter) -> new FixedPageSource(rowPagesBuilder(types).addSequencePage(10, 20, 30, 40).build()), TEST_TABLE_HANDLE, ImmutableList.of(), DynamicFilter.EMPTY);
    PageConsumerOperator sink = createSinkOperator(types);
    Driver driver = Driver.createDriver(driverContext, source, sink);
    assertSame(driver.getDriverContext(), driverContext);
    assertFalse(driver.isFinished());
    assertFalse(driver.processFor(new Duration(1, TimeUnit.MILLISECONDS)).isDone());
    assertFalse(driver.isFinished());
    driver.updateSplitAssignment(new SplitAssignment(sourceId, ImmutableSet.of(new ScheduledSplit(0, sourceId, newMockSplit())), true));
    assertFalse(driver.isFinished());
    assertTrue(driver.processFor(new Duration(1, TimeUnit.SECONDS)).isDone());
    assertTrue(driver.isFinished());
    assertTrue(sink.isFinished());
    assertTrue(source.isFinished());
}
Also used : PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) PageConsumerOperator(io.trino.testing.PageConsumerOperator) Type(io.trino.spi.type.Type) ScheduledSplit(io.trino.execution.ScheduledSplit) Duration(io.airlift.units.Duration) SplitAssignment(io.trino.execution.SplitAssignment) FixedPageSource(io.trino.spi.connector.FixedPageSource) Test(org.testng.annotations.Test)

Aggregations

FixedPageSource (io.trino.spi.connector.FixedPageSource)11 PlanNodeId (io.trino.sql.planner.plan.PlanNodeId)7 Test (org.testng.annotations.Test)7 Page (io.trino.spi.Page)6 Type (io.trino.spi.type.Type)5 Duration (io.airlift.units.Duration)4 CatalogName (io.trino.connector.CatalogName)4 Split (io.trino.metadata.Split)4 ScheduledSplit (io.trino.execution.ScheduledSplit)3 SplitAssignment (io.trino.execution.SplitAssignment)3 CursorProcessor (io.trino.operator.project.CursorProcessor)3 PageProcessor (io.trino.operator.project.PageProcessor)3 BlockBuilder (io.trino.spi.block.BlockBuilder)3 ImmutableList (com.google.common.collect.ImmutableList)2 OperatorAssertion.toMaterializedResult (io.trino.operator.OperatorAssertion.toMaterializedResult)2 Block (io.trino.spi.block.Block)2 ConnectorPageSource (io.trino.spi.connector.ConnectorPageSource)2 RowExpression (io.trino.sql.relational.RowExpression)2 TestingSplit (io.trino.testing.TestingSplit)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1