Search in sources :

Example 31 with CatalogSchemaTableName

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

the class DeltaLakeMetadata method redirectTable.

@Override
public Optional<CatalogSchemaTableName> redirectTable(ConnectorSession session, SchemaTableName tableName) {
    requireNonNull(session, "session is null");
    requireNonNull(tableName, "tableName is null");
    Optional<String> targetCatalogName = DeltaLakeSessionProperties.getHiveCatalogName(session);
    if (targetCatalogName.isEmpty()) {
        return Optional.empty();
    }
    if (isHiveSystemSchema(tableName.getSchemaName())) {
        return Optional.empty();
    }
    // we need to chop off any "$partitions" and similar suffixes from table name while querying the metastore for the Table object
    int metadataMarkerIndex = tableName.getTableName().lastIndexOf('$');
    SchemaTableName tableNameBase = (metadataMarkerIndex == -1) ? tableName : schemaTableName(tableName.getSchemaName(), tableName.getTableName().substring(0, metadataMarkerIndex));
    Optional<Table> table = metastore.getHiveMetastore().getTable(tableNameBase.getSchemaName(), tableNameBase.getTableName());
    if (table.isEmpty() || VIRTUAL_VIEW.name().equals(table.get().getTableType())) {
        return Optional.empty();
    }
    if (isHiveTable(table.get())) {
        // After redirecting, use the original table name, with "$partitions" and similar suffixes
        return targetCatalogName.map(catalog -> new CatalogSchemaTableName(catalog, tableName));
    }
    return Optional.empty();
}
Also used : Table(io.trino.plugin.hive.metastore.Table) HiveUtil.isDeltaLakeTable(io.trino.plugin.hive.util.HiveUtil.isDeltaLakeTable) SchemaTableName(io.trino.spi.connector.SchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Constraint(io.trino.spi.connector.Constraint) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName)

Example 32 with CatalogSchemaTableName

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

the class IcebergMetadata method getMaterializedViewToken.

private Map<String, Optional<TableToken>> getMaterializedViewToken(ConnectorSession session, SchemaTableName name) {
    Map<String, Optional<TableToken>> viewToken = new HashMap<>();
    Optional<ConnectorMaterializedViewDefinition> materializedViewDefinition = getMaterializedView(session, name);
    if (materializedViewDefinition.isEmpty()) {
        return viewToken;
    }
    SchemaTableName storageTableName = materializedViewDefinition.get().getStorageTable().map(CatalogSchemaTableName::getSchemaTableName).orElseThrow(() -> new IllegalStateException("Storage table missing in definition of materialized view " + name));
    Table icebergTable = catalog.loadTable(session, storageTableName);
    String dependsOnTables = icebergTable.currentSnapshot().summary().getOrDefault(DEPENDS_ON_TABLES, "");
    if (!dependsOnTables.isEmpty()) {
        Map<String, String> tableToSnapshotIdMap = Splitter.on(',').withKeyValueSeparator('=').split(dependsOnTables);
        for (Map.Entry<String, String> entry : tableToSnapshotIdMap.entrySet()) {
            viewToken.put(entry.getKey(), Optional.of(new TableToken(Long.parseLong(entry.getValue()))));
        }
    }
    return viewToken;
}
Also used : Table(org.apache.iceberg.Table) ClassLoaderSafeSystemTable(io.trino.plugin.base.classloader.ClassLoaderSafeSystemTable) SystemTable(io.trino.spi.connector.SystemTable) Optional(java.util.Optional) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) SchemaTableName(io.trino.spi.connector.SchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 33 with CatalogSchemaTableName

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

the class TestRefreshMaterializedView method createQueryRunner.

@Override
protected QueryRunner createQueryRunner() throws Exception {
    Session session = testSessionBuilder().setCatalog("mock").setSchema("default").build();
    DistributedQueryRunner queryRunner = DistributedQueryRunner.builder(session).build();
    queryRunner.installPlugin(new MockConnectorPlugin(MockConnectorFactory.builder().withListSchemaNames(connectionSession -> ImmutableList.of("default")).withGetColumns(schemaTableName -> ImmutableList.of(new ColumnMetadata("nationkey", BIGINT))).withGetTableHandle((connectorSession, tableName) -> new MockConnectorTableHandle(tableName)).withGetMaterializedViews((connectorSession, schemaTablePrefix) -> ImmutableMap.of(new SchemaTableName("default", "delegate_refresh_to_connector"), new ConnectorMaterializedViewDefinition("SELECT nationkey FROM mock.default.test_table", Optional.of(new CatalogSchemaTableName("mock", "default", "test_storage")), Optional.of("mock"), Optional.of("default"), ImmutableList.of(new ConnectorMaterializedViewDefinition.Column("nationkey", BIGINT.getTypeId())), Optional.empty(), Optional.of("alice"), ImmutableMap.of()))).withDelegateMaterializedViewRefreshToConnector((connectorSession, schemaTableName) -> true).withRefreshMaterializedView(((connectorSession, schemaTableName) -> {
        startRefreshMaterializedView.set(null);
        SettableFuture<Void> refreshMaterializedView = SettableFuture.create();
        finishRefreshMaterializedView.addListener(() -> refreshMaterializedView.set(null), directExecutor());
        addExceptionCallback(refreshMaterializedView, () -> refreshInterrupted.set(null));
        return toCompletableFuture(refreshMaterializedView);
    })).build()));
    queryRunner.createCatalog("mock", "mock");
    return queryRunner;
}
Also used : MoreExecutors.listeningDecorator(com.google.common.util.concurrent.MoreExecutors.listeningDecorator) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RUNNING(io.trino.execution.QueryState.RUNNING) Test(org.testng.annotations.Test) SettableFuture(com.google.common.util.concurrent.SettableFuture) ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) AbstractTestQueryFramework(io.trino.testing.AbstractTestQueryFramework) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) MoreFutures.toCompletableFuture(io.airlift.concurrent.MoreFutures.toCompletableFuture) AfterClass(org.testng.annotations.AfterClass) ImmutableMap(com.google.common.collect.ImmutableMap) Language(org.intellij.lang.annotations.Language) BeforeClass(org.testng.annotations.BeforeClass) BeforeMethod(org.testng.annotations.BeforeMethod) MoreFutures.getFutureValue(io.airlift.concurrent.MoreFutures.getFutureValue) MoreFutures.addExceptionCallback(io.airlift.concurrent.MoreFutures.addExceptionCallback) SchemaTableName(io.trino.spi.connector.SchemaTableName) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) Futures(com.google.common.util.concurrent.Futures) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) BIGINT(io.trino.spi.type.BigintType.BIGINT) Assert.assertEventually(io.trino.testing.assertions.Assert.assertEventually) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) QueryRunner(io.trino.testing.QueryRunner) Optional(java.util.Optional) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Session(io.trino.Session) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) SchemaTableName(io.trino.spi.connector.SchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Session(io.trino.Session)

Example 34 with CatalogSchemaTableName

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

the class TestTableRedirection method testShowColumns.

@Test
public void testShowColumns() {
    assertQuery(format("SHOW COLUMNS FROM %s.%s", SCHEMA_ONE, VALID_REDIRECTION_SRC), "VALUES " + row(C2, BIGINT.getDisplayName(), "", "") + "," + row(C3, BIGINT.getDisplayName(), "", ""));
    assertThatThrownBy(() -> query((format("SHOW COLUMNS FROM %s.%s", SCHEMA_ONE, BAD_REDIRECTION_SRC)))).hasMessageContaining("Table '%s' redirected to '%s', but the target table '%s' does not exist", new CatalogSchemaTableName(CATALOG_NAME, SCHEMA_ONE, BAD_REDIRECTION_SRC), new CatalogSchemaTableName(CATALOG_NAME, SCHEMA_TWO, NON_EXISTENT_TABLE), new CatalogSchemaTableName(CATALOG_NAME, SCHEMA_TWO, NON_EXISTENT_TABLE));
    assertThatThrownBy(() -> query((format("SHOW COLUMNS FROM %s.%s", SCHEMA_ONE, REDIRECTION_LOOP_PING)))).hasMessageContaining("Table redirections form a loop");
}
Also used : CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Test(org.testng.annotations.Test)

Example 35 with CatalogSchemaTableName

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

the class TestTableRedirection method testShowCreate.

@Test
public void testShowCreate() {
    String showCreateValidSource = (String) computeScalar(format("SHOW CREATE TABLE %s.%s", SCHEMA_ONE, VALID_REDIRECTION_SRC));
    String showCreateValidTarget = (String) computeScalar(format("SHOW CREATE TABLE %s.%s", SCHEMA_TWO, VALID_REDIRECTION_TARGET));
    assertEquals(showCreateValidTarget, showCreateValidSource.replace(SCHEMA_ONE + "." + VALID_REDIRECTION_SRC, SCHEMA_TWO + "." + VALID_REDIRECTION_TARGET));
    assertThatThrownBy(() -> query((format("SHOW CREATE TABLE %s.%s", SCHEMA_ONE, BAD_REDIRECTION_SRC)))).hasMessageContaining("Table '%s' redirected to '%s', but the target table '%s' does not exist", new CatalogSchemaTableName(CATALOG_NAME, SCHEMA_ONE, BAD_REDIRECTION_SRC), new CatalogSchemaTableName(CATALOG_NAME, SCHEMA_TWO, NON_EXISTENT_TABLE), new CatalogSchemaTableName(CATALOG_NAME, SCHEMA_TWO, NON_EXISTENT_TABLE));
    assertThatThrownBy(() -> query((format("SHOW CREATE TABLE %s.%s", SCHEMA_ONE, REDIRECTION_LOOP_PING)))).hasMessageContaining("Table redirections form a loop");
}
Also used : CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Test(org.testng.annotations.Test)

Aggregations

CatalogSchemaTableName (io.trino.spi.connector.CatalogSchemaTableName)68 Test (org.testng.annotations.Test)48 SystemAccessControl (io.trino.spi.security.SystemAccessControl)38 SchemaTableName (io.trino.spi.connector.SchemaTableName)18 Optional (java.util.Optional)13 TrinoPrincipal (io.trino.spi.security.TrinoPrincipal)12 Type (io.trino.spi.type.Type)11 Map (java.util.Map)11 ImmutableList (com.google.common.collect.ImmutableList)9 ImmutableMap (com.google.common.collect.ImmutableMap)9 TrinoException (io.trino.spi.TrinoException)9 List (java.util.List)8 Objects.requireNonNull (java.util.Objects.requireNonNull)8 ImmutableSet (com.google.common.collect.ImmutableSet)7 Session (io.trino.Session)7 Logger (io.airlift.log.Logger)6 ViewExpression (io.trino.spi.security.ViewExpression)6 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)5 CatalogSchemaName (io.trino.spi.connector.CatalogSchemaName)5 ConnectorMetadata (io.trino.spi.connector.ConnectorMetadata)5