Search in sources :

Example 1 with LocalQueryRunner

use of io.trino.testing.LocalQueryRunner in project trino by trinodb.

the class TestSpatialJoinPlanning method createLocalQueryRunner.

@Override
protected LocalQueryRunner createLocalQueryRunner() {
    LocalQueryRunner queryRunner = LocalQueryRunner.create(testSessionBuilder().setCatalog("memory").setSchema("default").build());
    queryRunner.installPlugin(new GeoPlugin());
    queryRunner.createCatalog("tpch", new TpchConnectorFactory(1), ImmutableMap.of());
    queryRunner.createCatalog("memory", new MemoryConnectorFactory(), ImmutableMap.of());
    queryRunner.execute(format("CREATE TABLE kdb_tree AS SELECT '%s' AS v", KDB_TREE_JSON));
    queryRunner.execute("CREATE TABLE points (lng, lat, name) AS (VALUES (2.1e0, 2.1e0, 'x'))");
    queryRunner.execute("CREATE TABLE polygons (wkt, name) AS (VALUES ('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))', 'a'))");
    return queryRunner;
}
Also used : TpchConnectorFactory(io.trino.plugin.tpch.TpchConnectorFactory) MemoryConnectorFactory(io.trino.plugin.memory.MemoryConnectorFactory) LocalQueryRunner(io.trino.testing.LocalQueryRunner)

Example 2 with LocalQueryRunner

use of io.trino.testing.LocalQueryRunner in project trino by trinodb.

the class TestAccessControlManager method testDenyCatalogAccessControl.

@Test
public void testDenyCatalogAccessControl() {
    try (LocalQueryRunner queryRunner = LocalQueryRunner.create(TEST_SESSION)) {
        TransactionManager transactionManager = queryRunner.getTransactionManager();
        AccessControlManager accessControlManager = createAccessControlManager(transactionManager);
        TestSystemAccessControlFactory accessControlFactory = new TestSystemAccessControlFactory("test");
        accessControlManager.addSystemAccessControlFactory(accessControlFactory);
        accessControlManager.setSystemAccessControl("test", ImmutableMap.of());
        queryRunner.createCatalog("catalog", MockConnectorFactory.create(), ImmutableMap.of());
        accessControlManager.addCatalogAccessControl(new CatalogName("catalog"), new DenyConnectorAccessControl());
        assertThatThrownBy(() -> transaction(transactionManager, accessControlManager).execute(transactionId -> {
            accessControlManager.checkCanSelectFromColumns(context(transactionId), new QualifiedObjectName("catalog", "schema", "table"), ImmutableSet.of("column"));
        })).isInstanceOf(TrinoException.class).hasMessageMatching("Access Denied: Cannot select from columns \\[column\\] in table or view schema.table");
    }
}
Also used : QueryId(io.trino.spi.QueryId) TransactionBuilder.transaction(io.trino.transaction.TransactionBuilder.transaction) TransactionManager(io.trino.transaction.TransactionManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) SystemSecurityContext(io.trino.spi.security.SystemSecurityContext) CatalogName(io.trino.connector.CatalogName) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Map(java.util.Map) CatalogSchemaName(io.trino.spi.connector.CatalogSchemaName) TEST_SESSION(io.trino.SessionTestUtils.TEST_SESSION) Path(java.nio.file.Path) WRITE(java.nio.file.StandardOpenOption.WRITE) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ViewExpression(io.trino.spi.security.ViewExpression) ConnectorAccessControl(io.trino.spi.connector.ConnectorAccessControl) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) SchemaTableName(io.trino.spi.connector.SchemaTableName) BasicPrincipal(io.trino.spi.security.BasicPrincipal) TestingEventListenerManager.emptyEventListenerManager(io.trino.testing.TestingEventListenerManager.emptyEventListenerManager) List(java.util.List) Principal(java.security.Principal) BIGINT(io.trino.spi.type.BigintType.BIGINT) ReadOnlySystemAccessControl(io.trino.plugin.base.security.ReadOnlySystemAccessControl) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Optional(java.util.Optional) SystemAccessControlFactory(io.trino.spi.security.SystemAccessControlFactory) AccessDeniedException(io.trino.spi.security.AccessDeniedException) TestingEventListenerManager(io.trino.testing.TestingEventListenerManager) TRUNCATE_EXISTING(java.nio.file.StandardOpenOption.TRUNCATE_EXISTING) Type(io.trino.spi.type.Type) Assert.assertEquals(org.testng.Assert.assertEquals) AllowAllAccessControl(io.trino.plugin.base.security.AllowAllAccessControl) SystemAccessControl(io.trino.spi.security.SystemAccessControl) InMemoryTransactionManager.createTestTransactionManager(io.trino.transaction.InMemoryTransactionManager.createTestTransactionManager) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Identity(io.trino.spi.security.Identity) LocalQueryRunner(io.trino.testing.LocalQueryRunner) Objects.requireNonNull(java.util.Objects.requireNonNull) ConnectorSecurityContext(io.trino.spi.connector.ConnectorSecurityContext) Files(java.nio.file.Files) AccessDeniedException.denySelectTable(io.trino.spi.security.AccessDeniedException.denySelectTable) IOException(java.io.IOException) Files.createTempFile(java.nio.file.Files.createTempFile) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) DefaultSystemAccessControl(io.trino.plugin.base.security.DefaultSystemAccessControl) EventListenerManager(io.trino.eventlistener.EventListenerManager) CatalogManager(io.trino.metadata.CatalogManager) CREATE(java.nio.file.StandardOpenOption.CREATE) TransactionId(io.trino.transaction.TransactionId) EventListener(io.trino.spi.eventlistener.EventListener) AllowAllSystemAccessControl(io.trino.plugin.base.security.AllowAllSystemAccessControl) TransactionManager(io.trino.transaction.TransactionManager) InMemoryTransactionManager.createTestTransactionManager(io.trino.transaction.InMemoryTransactionManager.createTestTransactionManager) TrinoException(io.trino.spi.TrinoException) CatalogName(io.trino.connector.CatalogName) LocalQueryRunner(io.trino.testing.LocalQueryRunner) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Test(org.testng.annotations.Test)

Example 3 with LocalQueryRunner

use of io.trino.testing.LocalQueryRunner in project trino by trinodb.

the class TestTableScanRedirectionWithPushdown method testRedirectionAfterPredicatePushdownIntoTableScan.

@Test
public void testRedirectionAfterPredicatePushdownIntoTableScan() {
    // the connector can detect a filter
    try (LocalQueryRunner queryRunner = createLocalQueryRunner(getMockApplyRedirectAfterPredicatePushdown(REDIRECTION_MAPPING_A, Optional.empty()), Optional.empty(), Optional.of(getMockApplyFilter(ImmutableSet.of(SOURCE_COLUMN_HANDLE_A, DESTINATION_COLUMN_HANDLE_A))))) {
        assertPlan(queryRunner, "SELECT source_col_a FROM test_table WHERE source_col_a = 1", output(ImmutableList.of("DEST_COL"), tableScan(new MockConnectorTableHandle(DESTINATION_TABLE, TupleDomain.withColumnDomains(ImmutableMap.of(DESTINATION_COLUMN_HANDLE_A, singleValue(INTEGER, 1L))), Optional.empty())::equals, TupleDomain.withColumnDomains(ImmutableMap.of(DESTINATION_COLUMN_HANDLE_A::equals, singleValue(INTEGER, 1L))), ImmutableMap.of("DEST_COL", DESTINATION_COLUMN_HANDLE_A::equals))));
        assertPlan(queryRunner, "SELECT source_col_a FROM test_table", output(ImmutableList.of("SOURCE_COL"), tableScan(TEST_TABLE, ImmutableMap.of("SOURCE_COL", SOURCE_COLUMN_NAME_A))));
    }
}
Also used : MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) LocalQueryRunner(io.trino.testing.LocalQueryRunner) Test(org.testng.annotations.Test)

Example 4 with LocalQueryRunner

use of io.trino.testing.LocalQueryRunner in project trino by trinodb.

the class TestTableScanRedirectionWithPushdown method createLocalQueryRunner.

private LocalQueryRunner createLocalQueryRunner(ApplyTableScanRedirect applyTableScanRedirect, Optional<ApplyProjection> applyProjection, Optional<ApplyFilter> applyFilter) {
    LocalQueryRunner queryRunner = LocalQueryRunner.create(MOCK_SESSION);
    MockConnectorFactory.Builder builder = MockConnectorFactory.builder().withGetTableHandle((session, schemaTableName) -> new MockConnectorTableHandle(schemaTableName)).withGetColumns(name -> {
        if (name.equals(SOURCE_TABLE)) {
            return ImmutableList.of(new ColumnMetadata(SOURCE_COLUMN_NAME_A, INTEGER), new ColumnMetadata(SOURCE_COLUMN_NAME_B, INTEGER), new ColumnMetadata(SOURCE_COLUMN_NAME_C, VARCHAR), new ColumnMetadata(SOURCE_COLUMN_NAME_D, ROW_TYPE));
        } else if (name.equals(DESTINATION_TABLE)) {
            return ImmutableList.of(new ColumnMetadata(DESTINATION_COLUMN_NAME_A, INTEGER), new ColumnMetadata(DESTINATION_COLUMN_NAME_B, INTEGER), new ColumnMetadata(DESTINATION_COLUMN_NAME_C, ROW_TYPE), new ColumnMetadata(DESTINATION_COLUMN_NAME_D, BOGUS));
        }
        throw new IllegalArgumentException();
    }).withApplyTableScanRedirect(applyTableScanRedirect);
    applyProjection.ifPresent(builder::withApplyProjection);
    applyFilter.ifPresent(builder::withApplyFilter);
    queryRunner.createCatalog(MOCK_CATALOG, builder.build(), ImmutableMap.of());
    return queryRunner;
}
Also used : ApplyTableScanRedirect(io.trino.connector.MockConnectorFactory.ApplyTableScanRedirect) TransactionBuilder.transaction(io.trino.transaction.TransactionBuilder.transaction) OPTIMIZED_AND_VALIDATED(io.trino.sql.planner.LogicalPlanner.Stage.OPTIMIZED_AND_VALIDATED) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) ApplyFilter(io.trino.connector.MockConnectorFactory.ApplyFilter) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Arrays.asList(java.util.Arrays.asList) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) Map(java.util.Map) ProjectionApplicationResult(io.trino.spi.connector.ProjectionApplicationResult) INTEGER(io.trino.spi.type.IntegerType.INTEGER) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) RowType(io.trino.spi.type.RowType) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) SchemaTableName(io.trino.spi.connector.SchemaTableName) List(java.util.List) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) BIGINT(io.trino.spi.type.BigintType.BIGINT) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Domain.singleValue(io.trino.spi.predicate.Domain.singleValue) Assignment(io.trino.spi.connector.Assignment) Optional(java.util.Optional) RowType.field(io.trino.spi.type.RowType.field) TableScanRedirectApplicationResult(io.trino.spi.connector.TableScanRedirectApplicationResult) PlanMatchPattern.output(io.trino.sql.planner.assertions.PlanMatchPattern.output) Session(io.trino.Session) ApplyProjection(io.trino.connector.MockConnectorFactory.ApplyProjection) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Type(io.trino.spi.type.Type) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) BOGUS(io.trino.tests.BogusType.BOGUS) Variable(io.trino.spi.expression.Variable) PlanAssert(io.trino.sql.planner.assertions.PlanAssert) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PlanOptimizer(io.trino.sql.planner.optimizations.PlanOptimizer) LocalQueryRunner(io.trino.testing.LocalQueryRunner) ColumnHandle(io.trino.spi.connector.ColumnHandle) ConstraintApplicationResult(io.trino.spi.connector.ConstraintApplicationResult) Language(org.intellij.lang.annotations.Language) MockConnectorColumnHandle(io.trino.connector.MockConnectorColumnHandle) FieldDereference(io.trino.spi.expression.FieldDereference) ConnectorSession(io.trino.spi.connector.ConnectorSession) TupleDomain(io.trino.spi.predicate.TupleDomain) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) WarningCollector(io.trino.execution.warnings.WarningCollector) PlanMatchPattern.tableScan(io.trino.sql.planner.assertions.PlanMatchPattern.tableScan) MockConnectorFactory(io.trino.connector.MockConnectorFactory) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) LocalQueryRunner(io.trino.testing.LocalQueryRunner)

Example 5 with LocalQueryRunner

use of io.trino.testing.LocalQueryRunner in project trino by trinodb.

the class TestTableScanRedirectionWithPushdown method testRedirectionBeforeDeferencePushdown.

@Test
public void testRedirectionBeforeDeferencePushdown() {
    // the connector can detect that source_col_a and source_col_d is projected
    try (LocalQueryRunner queryRunner = createLocalQueryRunner(mockApplyRedirectAfterProjectionPushdown(ROW_TYPE_REDIRECTION_MAPPING_AD, Optional.of(ImmutableSet.of(SOURCE_COLUMN_HANDLE_A, SOURCE_COLUMN_HANDLE_D))), Optional.of(this::mockApplyProjection), Optional.empty())) {
        // Pushdown of dereference for source_col_d.a into table scan results in a new column handle
        // Table scan redirection would not take place if dereference pushdown has already taken place before redirection
        ColumnHandle destinationColumnHandleC0 = new MockConnectorColumnHandle(DESTINATION_COLUMN_NAME_C + "#0", BIGINT);
        assertPlan(queryRunner, "SELECT source_col_a, source_col_d.a FROM test_table", output(ImmutableList.of("DEST_COL_A", "DEST_COL_C#0"), tableScan(new MockConnectorTableHandle(DESTINATION_TABLE, TupleDomain.all(), Optional.of(ImmutableList.of(DESTINATION_COLUMN_HANDLE_A, destinationColumnHandleC0)))::equals, TupleDomain.all(), ImmutableMap.of("DEST_COL_A", DESTINATION_COLUMN_HANDLE_A::equals, "DEST_COL_C#0", destinationColumnHandleC0::equals))));
    }
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) MockConnectorColumnHandle(io.trino.connector.MockConnectorColumnHandle) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) MockConnectorColumnHandle(io.trino.connector.MockConnectorColumnHandle) LocalQueryRunner(io.trino.testing.LocalQueryRunner) Test(org.testng.annotations.Test)

Aggregations

LocalQueryRunner (io.trino.testing.LocalQueryRunner)66 Session (io.trino.Session)28 TpchConnectorFactory (io.trino.plugin.tpch.TpchConnectorFactory)25 Test (org.testng.annotations.Test)22 ImmutableMap (com.google.common.collect.ImmutableMap)12 SchemaTableName (io.trino.spi.connector.SchemaTableName)12 ImmutableList (com.google.common.collect.ImmutableList)11 MockConnectorFactory (io.trino.connector.MockConnectorFactory)11 Optional (java.util.Optional)11 CatalogName (io.trino.connector.CatalogName)9 TestingSession.testSessionBuilder (io.trino.testing.TestingSession.testSessionBuilder)9 MockConnectorTableHandle (io.trino.connector.MockConnectorTableHandle)8 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)8 CatalogSchemaTableName (io.trino.spi.connector.CatalogSchemaTableName)7 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)7 TrinoException (io.trino.spi.TrinoException)6 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 ColumnHandle (io.trino.spi.connector.ColumnHandle)5 List (java.util.List)5