Search in sources :

Example 1 with ConnectorViewDefinition

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

the class MetadataManager method getViews.

@Override
public Map<QualifiedObjectName, ViewInfo> getViews(Session session, QualifiedTablePrefix prefix) {
    requireNonNull(prefix, "prefix is null");
    Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, prefix.getCatalogName());
    Map<QualifiedObjectName, ViewInfo> views = new LinkedHashMap<>();
    if (catalog.isPresent()) {
        CatalogMetadata catalogMetadata = catalog.get();
        SchemaTablePrefix tablePrefix = prefix.asSchemaTablePrefix();
        for (CatalogName catalogName : catalogMetadata.listConnectorIds()) {
            ConnectorMetadata metadata = catalogMetadata.getMetadataFor(session, catalogName);
            ConnectorSession connectorSession = session.toConnectorSession(catalogName);
            Map<SchemaTableName, ConnectorViewDefinition> viewMap;
            if (tablePrefix.getTable().isPresent()) {
                viewMap = metadata.getView(connectorSession, tablePrefix.toSchemaTableName()).map(view -> ImmutableMap.of(tablePrefix.toSchemaTableName(), view)).orElse(ImmutableMap.of());
            } else {
                viewMap = metadata.getViews(connectorSession, tablePrefix.getSchema());
            }
            for (Entry<SchemaTableName, ConnectorViewDefinition> entry : viewMap.entrySet()) {
                QualifiedObjectName viewName = new QualifiedObjectName(prefix.getCatalogName(), entry.getKey().getSchemaName(), entry.getKey().getTableName());
                views.put(viewName, new ViewInfo(entry.getValue()));
            }
        }
    }
    return ImmutableMap.copyOf(views);
}
Also used : SchemaTablePrefix(io.trino.spi.connector.SchemaTablePrefix) SchemaTableName(io.trino.spi.connector.SchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) QualifiedObjectName.convertFromSchemaTableName(io.trino.metadata.QualifiedObjectName.convertFromSchemaTableName) LinkedHashMap(java.util.LinkedHashMap) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition) CatalogName(io.trino.connector.CatalogName) ConnectorSession(io.trino.spi.connector.ConnectorSession) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata)

Example 2 with ConnectorViewDefinition

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

the class AbstractTestHive method doCreateView.

private void doCreateView(SchemaTableName viewName, boolean replace) {
    String viewData = "test data";
    ConnectorViewDefinition definition = new ConnectorViewDefinition(viewData, Optional.empty(), Optional.empty(), ImmutableList.of(new ViewColumn("test", BIGINT.getTypeId())), Optional.empty(), Optional.empty(), true);
    try (Transaction transaction = newTransaction()) {
        transaction.getMetadata().createView(newSession(), viewName, definition, replace);
        transaction.commit();
    }
    try (Transaction transaction = newTransaction()) {
        ConnectorMetadata metadata = transaction.getMetadata();
        assertThat(metadata.getView(newSession(), viewName)).map(ConnectorViewDefinition::getOriginalSql).contains(viewData);
        Map<SchemaTableName, ConnectorViewDefinition> views = metadata.getViews(newSession(), Optional.of(viewName.getSchemaName()));
        assertEquals(views.size(), 1);
        assertEquals(views.get(viewName).getOriginalSql(), definition.getOriginalSql());
        assertTrue(metadata.listViews(newSession(), Optional.of(viewName.getSchemaName())).contains(viewName));
    }
}
Also used : ViewColumn(io.trino.spi.connector.ConnectorViewDefinition.ViewColumn) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) SchemaTableName(io.trino.spi.connector.SchemaTableName) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition)

Example 3 with ConnectorViewDefinition

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

the class TestColumnMask method init.

@BeforeAll
public void init() {
    LocalQueryRunner runner = LocalQueryRunner.builder(SESSION).build();
    runner.createCatalog(CATALOG, new TpchConnectorFactory(1), ImmutableMap.of());
    ConnectorViewDefinition view = new ConnectorViewDefinition("SELECT nationkey, name FROM local.tiny.nation", Optional.empty(), Optional.empty(), ImmutableList.of(new ConnectorViewDefinition.ViewColumn("nationkey", BigintType.BIGINT.getTypeId()), new ConnectorViewDefinition.ViewColumn("name", VarcharType.createVarcharType(25).getTypeId())), Optional.empty(), Optional.of(VIEW_OWNER), false);
    ConnectorMaterializedViewDefinition materializedView = new ConnectorMaterializedViewDefinition("SELECT * FROM local.tiny.nation", Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(new ConnectorMaterializedViewDefinition.Column("nationkey", BigintType.BIGINT.getTypeId()), new ConnectorMaterializedViewDefinition.Column("name", VarcharType.createVarcharType(25).getTypeId()), new ConnectorMaterializedViewDefinition.Column("regionkey", BigintType.BIGINT.getTypeId()), new ConnectorMaterializedViewDefinition.Column("comment", VarcharType.createVarcharType(152).getTypeId())), Optional.empty(), Optional.of(VIEW_OWNER), ImmutableMap.of());
    ConnectorMaterializedViewDefinition freshMaterializedView = new ConnectorMaterializedViewDefinition("SELECT * FROM local.tiny.nation", Optional.of(new CatalogSchemaTableName("local", "tiny", "nation")), Optional.empty(), Optional.empty(), ImmutableList.of(new ConnectorMaterializedViewDefinition.Column("nationkey", BigintType.BIGINT.getTypeId()), new ConnectorMaterializedViewDefinition.Column("name", VarcharType.createVarcharType(25).getTypeId()), new ConnectorMaterializedViewDefinition.Column("regionkey", BigintType.BIGINT.getTypeId()), new ConnectorMaterializedViewDefinition.Column("comment", VarcharType.createVarcharType(152).getTypeId())), Optional.empty(), Optional.of(VIEW_OWNER), ImmutableMap.of());
    ConnectorMaterializedViewDefinition materializedViewWithCasts = new ConnectorMaterializedViewDefinition("SELECT nationkey, cast(name as varchar(1)) as name, regionkey, comment FROM local.tiny.nation", Optional.of(new CatalogSchemaTableName("local", "tiny", "nation")), Optional.empty(), Optional.empty(), ImmutableList.of(new ConnectorMaterializedViewDefinition.Column("nationkey", BigintType.BIGINT.getTypeId()), new ConnectorMaterializedViewDefinition.Column("name", VarcharType.createVarcharType(2).getTypeId()), new ConnectorMaterializedViewDefinition.Column("regionkey", BigintType.BIGINT.getTypeId()), new ConnectorMaterializedViewDefinition.Column("comment", VarcharType.createVarcharType(152).getTypeId())), Optional.empty(), Optional.of(VIEW_OWNER), ImmutableMap.of());
    MockConnectorFactory mock = MockConnectorFactory.builder().withGetColumns(schemaTableName -> {
        if (schemaTableName.equals(new SchemaTableName("tiny", "nation_with_hidden_column"))) {
            return TPCH_NATION_WITH_HIDDEN_COLUMN;
        }
        throw new UnsupportedOperationException();
    }).withData(schemaTableName -> {
        if (schemaTableName.equals(new SchemaTableName("tiny", "nation_with_hidden_column"))) {
            return TPCH_WITH_HIDDEN_COLUMN_DATA;
        }
        throw new UnsupportedOperationException();
    }).withGetViews((s, prefix) -> ImmutableMap.of(new SchemaTableName("default", "nation_view"), view)).withGetMaterializedViews((s, prefix) -> ImmutableMap.of(new SchemaTableName("default", "nation_materialized_view"), materializedView, new SchemaTableName("default", "nation_fresh_materialized_view"), freshMaterializedView, new SchemaTableName("default", "materialized_view_with_casts"), materializedViewWithCasts)).build();
    runner.createCatalog(MOCK_CATALOG, mock, ImmutableMap.of());
    assertions = new QueryAssertions(runner);
    accessControl = assertions.getQueryRunner().getAccessControl();
}
Also used : SELECT_COLUMN(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.SELECT_COLUMN) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) AfterAll(org.junit.jupiter.api.AfterAll) BigintType(io.trino.spi.type.BigintType) VarcharType(io.trino.spi.type.VarcharType) TestInstance(org.junit.jupiter.api.TestInstance) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BeforeAll(org.junit.jupiter.api.BeforeAll) TpchConnectorFactory(io.trino.plugin.tpch.TpchConnectorFactory) Identity(io.trino.spi.security.Identity) LocalQueryRunner(io.trino.testing.LocalQueryRunner) TestingAccessControlManager(io.trino.testing.TestingAccessControlManager) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition) TINY_SCHEMA_NAME(io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME) ImmutableMap(com.google.common.collect.ImmutableMap) ExecutionMode(org.junit.jupiter.api.parallel.ExecutionMode) ViewExpression(io.trino.spi.security.ViewExpression) PER_CLASS(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS) TPCH_WITH_HIDDEN_COLUMN_DATA(io.trino.connector.MockConnectorEntities.TPCH_WITH_HIDDEN_COLUMN_DATA) SchemaTableName(io.trino.spi.connector.SchemaTableName) Test(org.junit.jupiter.api.Test) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) TestingAccessControlManager.privilege(io.trino.testing.TestingAccessControlManager.privilege) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Optional(java.util.Optional) Execution(org.junit.jupiter.api.parallel.Execution) TPCH_NATION_WITH_HIDDEN_COLUMN(io.trino.connector.MockConnectorEntities.TPCH_NATION_WITH_HIDDEN_COLUMN) Session(io.trino.Session) TpchConnectorFactory(io.trino.plugin.tpch.TpchConnectorFactory) MockConnectorFactory(io.trino.connector.MockConnectorFactory) ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) SchemaTableName(io.trino.spi.connector.SchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) LocalQueryRunner(io.trino.testing.LocalQueryRunner) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 4 with ConnectorViewDefinition

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

the class TestRowFilter method init.

@BeforeAll
public void init() {
    LocalQueryRunner runner = LocalQueryRunner.builder(SESSION).build();
    runner.createCatalog(CATALOG, new TpchConnectorFactory(1), ImmutableMap.of());
    ConnectorViewDefinition view = new ConnectorViewDefinition("SELECT nationkey, name FROM local.tiny.nation", Optional.empty(), Optional.empty(), ImmutableList.of(new ConnectorViewDefinition.ViewColumn("nationkey", BigintType.BIGINT.getTypeId()), new ConnectorViewDefinition.ViewColumn("name", VarcharType.createVarcharType(25).getTypeId())), Optional.empty(), Optional.of(VIEW_OWNER), false);
    MockConnectorFactory mock = MockConnectorFactory.builder().withGetViews((s, prefix) -> ImmutableMap.<SchemaTableName, ConnectorViewDefinition>builder().put(new SchemaTableName("default", "nation_view"), view).buildOrThrow()).withGetColumns(schemaTableName -> {
        if (schemaTableName.equals(new SchemaTableName("tiny", "nation"))) {
            return TPCH_NATION_SCHEMA;
        }
        if (schemaTableName.equals(new SchemaTableName("tiny", "nation_with_hidden_column"))) {
            return TPCH_NATION_WITH_HIDDEN_COLUMN;
        }
        if (schemaTableName.equals(new SchemaTableName("tiny", "nation_with_optional_column"))) {
            return TPCH_NATION_WITH_OPTIONAL_COLUMN;
        }
        throw new UnsupportedOperationException();
    }).withData(schemaTableName -> {
        if (schemaTableName.equals(new SchemaTableName("tiny", "nation"))) {
            return TPCH_NATION_DATA;
        }
        if (schemaTableName.equals(new SchemaTableName("tiny", "nation_with_hidden_column"))) {
            return TPCH_WITH_HIDDEN_COLUMN_DATA;
        }
        if (schemaTableName.equals(new SchemaTableName("tiny", "nation_with_optional_column"))) {
            return TPCH_NATION_DATA;
        }
        throw new UnsupportedOperationException();
    }).build();
    runner.createCatalog(MOCK_CATALOG, mock, ImmutableMap.of());
    MockConnectorFactory mockMissingColumns = MockConnectorFactory.builder().withName("mockmissingcolumns").withGetViews((s, prefix) -> ImmutableMap.<SchemaTableName, ConnectorViewDefinition>builder().put(new SchemaTableName("default", "nation_view"), view).buildOrThrow()).withGetColumns(schemaTableName -> {
        if (schemaTableName.equals(new SchemaTableName("tiny", "nation_with_optional_column"))) {
            return TPCH_NATION_WITH_OPTIONAL_COLUMN;
        }
        throw new UnsupportedOperationException();
    }).withData(schemaTableName -> {
        if (schemaTableName.equals(new SchemaTableName("tiny", "nation_with_optional_column"))) {
            return TPCH_NATION_DATA;
        }
        throw new UnsupportedOperationException();
    }).withAllowMissingColumnsOnInsert(true).build();
    runner.createCatalog(MOCK_CATALOG_MISSING_COLUMNS, mockMissingColumns, ImmutableMap.of());
    assertions = new QueryAssertions(runner);
    accessControl = assertions.getQueryRunner().getAccessControl();
}
Also used : TPCH_NATION_WITH_OPTIONAL_COLUMN(io.trino.connector.MockConnectorEntities.TPCH_NATION_WITH_OPTIONAL_COLUMN) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SAME_THREAD(org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD) AfterAll(org.junit.jupiter.api.AfterAll) BigintType(io.trino.spi.type.BigintType) VarcharType(io.trino.spi.type.VarcharType) TestInstance(org.junit.jupiter.api.TestInstance) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BeforeAll(org.junit.jupiter.api.BeforeAll) TpchConnectorFactory(io.trino.plugin.tpch.TpchConnectorFactory) Identity(io.trino.spi.security.Identity) LocalQueryRunner(io.trino.testing.LocalQueryRunner) TestingAccessControlManager(io.trino.testing.TestingAccessControlManager) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition) TINY_SCHEMA_NAME(io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME) ImmutableMap(com.google.common.collect.ImmutableMap) ViewExpression(io.trino.spi.security.ViewExpression) TPCH_NATION_DATA(io.trino.connector.MockConnectorEntities.TPCH_NATION_DATA) PER_CLASS(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS) TrinoException(io.trino.spi.TrinoException) TPCH_NATION_SCHEMA(io.trino.connector.MockConnectorEntities.TPCH_NATION_SCHEMA) TPCH_WITH_HIDDEN_COLUMN_DATA(io.trino.connector.MockConnectorEntities.TPCH_WITH_HIDDEN_COLUMN_DATA) SchemaTableName(io.trino.spi.connector.SchemaTableName) Test(org.junit.jupiter.api.Test) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Optional(java.util.Optional) Execution(org.junit.jupiter.api.parallel.Execution) TPCH_NATION_WITH_HIDDEN_COLUMN(io.trino.connector.MockConnectorEntities.TPCH_NATION_WITH_HIDDEN_COLUMN) Session(io.trino.Session) TpchConnectorFactory(io.trino.plugin.tpch.TpchConnectorFactory) MockConnectorFactory(io.trino.connector.MockConnectorFactory) SchemaTableName(io.trino.spi.connector.SchemaTableName) LocalQueryRunner(io.trino.testing.LocalQueryRunner) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 5 with ConnectorViewDefinition

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

the class MetadataManager method getView.

@Override
public Optional<ViewDefinition> getView(Session session, QualifiedObjectName viewName) {
    Optional<ConnectorViewDefinition> connectorView = getViewInternal(session, viewName);
    if (connectorView.isEmpty() || connectorView.get().isRunAsInvoker() || isCatalogManagedSecurity(session, viewName.getCatalogName())) {
        return connectorView.map(view -> new ViewDefinition(viewName, view));
    }
    Identity runAsIdentity = systemSecurityMetadata.getViewRunAsIdentity(session, viewName.asCatalogSchemaTableName()).or(() -> connectorView.get().getOwner().map(Identity::ofUser)).orElseThrow(() -> new TrinoException(NOT_SUPPORTED, "Catalog does not support run-as DEFINER views: " + viewName));
    return Optional.of(new ViewDefinition(viewName, connectorView.get(), runAsIdentity));
}
Also used : ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition) TrinoException(io.trino.spi.TrinoException) Identity(io.trino.spi.security.Identity) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition)

Aggregations

ConnectorViewDefinition (io.trino.spi.connector.ConnectorViewDefinition)9 SchemaTableName (io.trino.spi.connector.SchemaTableName)8 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Session (io.trino.Session)4 MockConnectorFactory (io.trino.connector.MockConnectorFactory)4 Identity (io.trino.spi.security.Identity)4 TestingSession.testSessionBuilder (io.trino.testing.TestingSession.testSessionBuilder)4 Optional (java.util.Optional)4 Test (org.testng.annotations.Test)4 CatalogSchemaTableName (io.trino.spi.connector.CatalogSchemaTableName)3 ConnectorMaterializedViewDefinition (io.trino.spi.connector.ConnectorMaterializedViewDefinition)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 TPCH_NATION_WITH_HIDDEN_COLUMN (io.trino.connector.MockConnectorEntities.TPCH_NATION_WITH_HIDDEN_COLUMN)2 TPCH_WITH_HIDDEN_COLUMN_DATA (io.trino.connector.MockConnectorEntities.TPCH_WITH_HIDDEN_COLUMN_DATA)2 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)2 TpchConnectorFactory (io.trino.plugin.tpch.TpchConnectorFactory)2 TINY_SCHEMA_NAME (io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME)2 TpchPlugin (io.trino.plugin.tpch.TpchPlugin)2 TrinoException (io.trino.spi.TrinoException)2