Search in sources :

Example 21 with MockConnectorTableHandle

use of io.trino.connector.MockConnectorTableHandle in project trino by trinodb.

the class TestMetadataManager method setUp.

@BeforeClass
public void setUp() throws Exception {
    queryRunner = TpchQueryRunnerBuilder.builder().build();
    queryRunner.installPlugin(new Plugin() {

        @Override
        public Iterable<ConnectorFactory> getConnectorFactories() {
            SchemaTableName viewTableName = new SchemaTableName("UPPER_CASE_SCHEMA", "test_view");
            MockConnectorFactory connectorFactory = MockConnectorFactory.builder().withListSchemaNames(session -> ImmutableList.of("UPPER_CASE_SCHEMA")).withGetTableHandle((session, schemaTableName) -> {
                if (schemaTableName.equals(viewTableName)) {
                    return null;
                }
                return new MockConnectorTableHandle(schemaTableName);
            }).withListTables((session, schemaNameOrNull) -> ImmutableList.of(new SchemaTableName("UPPER_CASE_SCHEMA", "UPPER_CASE_TABLE"))).withGetViews((session, prefix) -> ImmutableMap.of(viewTableName, getConnectorViewDefinition())).build();
            return ImmutableList.of(connectorFactory);
        }
    });
    queryRunner.createCatalog("upper_case_schema_catalog", "mock");
    metadataManager = (MetadataManager) queryRunner.getMetadata();
}
Also used : QueryId(io.trino.spi.QueryId) BasicQueryInfo(io.trino.server.BasicQueryInfo) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RUNNING(io.trino.execution.QueryState.RUNNING) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) TpchQueryRunnerBuilder(io.trino.tests.tpch.TpchQueryRunnerBuilder) FAILED(io.trino.execution.QueryState.FAILED) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Slug(io.trino.server.protocol.Slug) TEST_SESSION(io.trino.SessionTestUtils.TEST_SESSION) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition) MetadataManager(io.trino.metadata.MetadataManager) QualifiedTablePrefix(io.trino.metadata.QualifiedTablePrefix) ConnectorFactory(io.trino.spi.connector.ConnectorFactory) AfterClass(org.testng.annotations.AfterClass) TestingSessionContext(io.trino.testing.TestingSessionContext) DispatchManager(io.trino.dispatcher.DispatchManager) ImmutableMap(com.google.common.collect.ImmutableMap) Language(org.intellij.lang.annotations.Language) BeforeClass(org.testng.annotations.BeforeClass) SchemaTableName(io.trino.spi.connector.SchemaTableName) TransactionBuilder(io.trino.transaction.TransactionBuilder) Plugin(io.trino.spi.Plugin) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) Optional(java.util.Optional) MockConnectorFactory(io.trino.connector.MockConnectorFactory) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) SchemaTableName(io.trino.spi.connector.SchemaTableName) Plugin(io.trino.spi.Plugin) BeforeClass(org.testng.annotations.BeforeClass)

Example 22 with MockConnectorTableHandle

use of io.trino.connector.MockConnectorTableHandle in project trino by trinodb.

the class TestApplyTableScanRedirection method testApplyTableScanRedirection.

@Test
public void testApplyTableScanRedirection() {
    try (RuleTester ruleTester = defaultRuleTester()) {
        // make the mock connector return a table scan on different table
        ApplyTableScanRedirect applyTableScanRedirect = getMockApplyRedirect(ImmutableMap.of(SOURCE_COLUMN_HANDLE_A, DESTINATION_COLUMN_NAME_A));
        MockConnectorFactory mockFactory = createMockFactory(Optional.of(applyTableScanRedirect));
        ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, mockFactory, ImmutableMap.of());
        ruleTester.assertThat(new ApplyTableScanRedirection(ruleTester.getPlannerContext())).on(p -> {
            Symbol column = p.symbol(SOURCE_COLUMN_NAME_A, VARCHAR);
            return p.tableScan(TEST_TABLE_HANDLE, ImmutableList.of(column), ImmutableMap.of(column, SOURCE_COLUMN_HANDLE_A));
        }).withSession(MOCK_SESSION).matches(tableScan(new MockConnectorTableHandle(DESTINATION_TABLE)::equals, TupleDomain.all(), ImmutableMap.of("DEST_COL", DESTINATION_COLUMN_HANDLE_A::equals)));
    }
}
Also used : MockConnectorFactory(io.trino.connector.MockConnectorFactory) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) RuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester) Symbol(io.trino.sql.planner.Symbol) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) ApplyTableScanRedirect(io.trino.connector.MockConnectorFactory.ApplyTableScanRedirect) Test(org.testng.annotations.Test)

Example 23 with MockConnectorTableHandle

use of io.trino.connector.MockConnectorTableHandle in project trino by trinodb.

the class TestApplyTableScanRedirection method testMismatchedTypesWithCoercion.

@Test
public void testMismatchedTypesWithCoercion() {
    try (RuleTester ruleTester = defaultRuleTester()) {
        // make the mock connector return a table scan on different table
        ApplyTableScanRedirect applyTableScanRedirect = getMockApplyRedirect(ImmutableMap.of(SOURCE_COLUMN_HANDLE_A, DESTINATION_COLUMN_NAME_C));
        MockConnectorFactory mockFactory = createMockFactory(Optional.of(applyTableScanRedirect));
        LocalQueryRunner runner = ruleTester.getQueryRunner();
        runner.createCatalog(MOCK_CATALOG, mockFactory, ImmutableMap.of());
        ruleTester.assertThat(new ApplyTableScanRedirection(ruleTester.getPlannerContext())).on(p -> {
            Symbol column = p.symbol(SOURCE_COLUMN_NAME_A, VARCHAR);
            return p.tableScan(TEST_TABLE_HANDLE, ImmutableList.of(column), ImmutableMap.of(column, SOURCE_COLUMN_HANDLE_A));
        }).withSession(MOCK_SESSION).matches(project(ImmutableMap.of("COL", expression("CAST(DEST_COL AS VARCHAR)")), tableScan(new MockConnectorTableHandle(DESTINATION_TABLE)::equals, TupleDomain.all(), ImmutableMap.of("DEST_COL", DESTINATION_COLUMN_HANDLE_C::equals))));
    }
}
Also used : MockConnectorFactory(io.trino.connector.MockConnectorFactory) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) RuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester) Symbol(io.trino.sql.planner.Symbol) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) ApplyTableScanRedirect(io.trino.connector.MockConnectorFactory.ApplyTableScanRedirect) LocalQueryRunner(io.trino.testing.LocalQueryRunner) Test(org.testng.annotations.Test)

Example 24 with MockConnectorTableHandle

use of io.trino.connector.MockConnectorTableHandle in project trino by trinodb.

the class TestInsert method createLocalQueryRunner.

@Override
protected LocalQueryRunner createLocalQueryRunner() {
    Session.SessionBuilder sessionBuilder = testSessionBuilder().setCatalog("mock").setSchema("schema");
    LocalQueryRunner queryRunner = LocalQueryRunner.create(sessionBuilder.build());
    queryRunner.createCatalog("mock", MockConnectorFactory.builder().withGetTableHandle((session, schemaTableName) -> {
        if (schemaTableName.getTableName().equals("test_table_preferred_partitioning")) {
            return new MockConnectorTableHandle(schemaTableName);
        }
        if (schemaTableName.getTableName().equals("test_table_required_partitioning")) {
            return new MockConnectorTableHandle(schemaTableName);
        }
        return null;
    }).withGetColumns(name -> ImmutableList.of(new ColumnMetadata("column1", INTEGER), new ColumnMetadata("column2", INTEGER))).withGetInsertLayout((session, tableName) -> {
        if (tableName.getTableName().equals("test_table_preferred_partitioning")) {
            return Optional.of(new ConnectorTableLayout(ImmutableList.of("column1")));
        }
        if (tableName.getTableName().equals("test_table_required_partitioning")) {
            return Optional.of(new ConnectorTableLayout(new TpchPartitioningHandle("orders", 10), ImmutableList.of("column1")));
        }
        return Optional.empty();
    }).withGetNewTableLayout((session, tableMetadata) -> {
        if (tableMetadata.getTable().getTableName().equals("new_test_table_preferred_partitioning")) {
            return Optional.of(new ConnectorTableLayout(ImmutableList.of("column1")));
        }
        if (tableMetadata.getTable().getTableName().equals("new_test_table_required_partitioning")) {
            return Optional.of(new ConnectorTableLayout(new TpchPartitioningHandle("orders", 10), ImmutableList.of("column1")));
        }
        if (tableMetadata.getTable().getTableName().equals("new_test_table_unpartitioned")) {
            return Optional.empty();
        }
        return Optional.empty();
    }).build(), ImmutableMap.of());
    return queryRunner;
}
Also used : ConnectorTableLayout(io.trino.spi.connector.ConnectorTableLayout) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) PREFERRED_WRITE_PARTITIONING_MIN_NUMBER_OF_PARTITIONS(io.trino.SystemSessionProperties.PREFERRED_WRITE_PARTITIONING_MIN_NUMBER_OF_PARTITIONS) TableWriterNode(io.trino.sql.planner.plan.TableWriterNode) Test(org.testng.annotations.Test) PlanNode(io.trino.sql.planner.plan.PlanNode) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) PlanMatchPattern.exchange(io.trino.sql.planner.assertions.PlanMatchPattern.exchange) LocalQueryRunner(io.trino.testing.LocalQueryRunner) LOCAL(io.trino.sql.planner.plan.ExchangeNode.Scope.LOCAL) REPARTITION(io.trino.sql.planner.plan.ExchangeNode.Type.REPARTITION) INTEGER(io.trino.spi.type.IntegerType.INTEGER) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) StatsProvider(io.trino.cost.StatsProvider) TASK_WRITER_COUNT(io.trino.SystemSessionProperties.TASK_WRITER_COUNT) PlanMatchPattern.node(io.trino.sql.planner.assertions.PlanMatchPattern.node) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) TpchPartitioningHandle(io.trino.plugin.tpch.TpchPartitioningHandle) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) PlanMatchPattern.anyTree(io.trino.sql.planner.assertions.PlanMatchPattern.anyTree) REMOTE(io.trino.sql.planner.plan.ExchangeNode.Scope.REMOTE) MatchResult(io.trino.sql.planner.assertions.MatchResult) USE_PREFERRED_WRITE_PARTITIONING(io.trino.SystemSessionProperties.USE_PREFERRED_WRITE_PARTITIONING) Metadata(io.trino.metadata.Metadata) Matcher(io.trino.sql.planner.assertions.Matcher) Optional(java.util.Optional) SymbolAliases(io.trino.sql.planner.assertions.SymbolAliases) ExchangeNode(io.trino.sql.planner.plan.ExchangeNode) Session(io.trino.Session) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ConnectorTableLayout(io.trino.spi.connector.ConnectorTableLayout) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) TpchPartitioningHandle(io.trino.plugin.tpch.TpchPartitioningHandle) LocalQueryRunner(io.trino.testing.LocalQueryRunner) Session(io.trino.Session)

Example 25 with MockConnectorTableHandle

use of io.trino.connector.MockConnectorTableHandle in project trino by trinodb.

the class TestTableScanRedirectionWithPushdown method testRedirectionAfterProjectionPushdown.

@Test
public void testRedirectionAfterProjectionPushdown() {
    // the connector can detect that source_col_a is projected
    try (LocalQueryRunner queryRunner = createLocalQueryRunner(mockApplyRedirectAfterProjectionPushdown(REDIRECTION_MAPPING_A, Optional.of(ImmutableSet.of(SOURCE_COLUMN_HANDLE_A))), Optional.of(this::mockApplyProjection), Optional.empty())) {
        assertPlan(queryRunner, "SELECT source_col_a FROM test_table", output(ImmutableList.of("DEST_COL"), tableScan("target_table", ImmutableMap.of("DEST_COL", DESTINATION_COLUMN_NAME_A))));
        assertPlan(queryRunner, "SELECT source_col_a, source_col_b FROM test_table", output(ImmutableList.of("SOURCE_COLA", "SOURCE_COLB"), tableScan(TEST_TABLE, ImmutableMap.of("SOURCE_COLA", SOURCE_COLUMN_NAME_A, "SOURCE_COLB", SOURCE_COLUMN_NAME_B))));
        assertPlan(queryRunner, "SELECT source_col_a FROM test_table WHERE source_col_a > 0", output(ImmutableList.of("DEST_COL"), filter("DEST_COL > 0", tableScan(new MockConnectorTableHandle(DESTINATION_TABLE)::equals, TupleDomain.all(), ImmutableMap.of("DEST_COL", DESTINATION_COLUMN_HANDLE_A::equals)))));
    }
}
Also used : MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) LocalQueryRunner(io.trino.testing.LocalQueryRunner) Test(org.testng.annotations.Test)

Aggregations

MockConnectorTableHandle (io.trino.connector.MockConnectorTableHandle)35 Test (org.testng.annotations.Test)32 MockConnectorFactory (io.trino.connector.MockConnectorFactory)29 ImmutableList (com.google.common.collect.ImmutableList)25 SchemaTableName (io.trino.spi.connector.SchemaTableName)25 TestingSession.testSessionBuilder (io.trino.testing.TestingSession.testSessionBuilder)21 Session (io.trino.Session)20 ImmutableMap (com.google.common.collect.ImmutableMap)19 Optional (java.util.Optional)18 ColumnHandle (io.trino.spi.connector.ColumnHandle)17 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)17 Symbol (io.trino.sql.planner.Symbol)16 BIGINT (io.trino.spi.type.BigintType.BIGINT)15 RuleTester (io.trino.sql.planner.iterative.rule.test.RuleTester)15 RuleTester.defaultRuleTester (io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester)15 List (java.util.List)14 TableHandle (io.trino.metadata.TableHandle)13 TupleDomain (io.trino.spi.predicate.TupleDomain)12 PlanMatchPattern.tableScan (io.trino.sql.planner.assertions.PlanMatchPattern.tableScan)12 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)12