Search in sources :

Example 81 with ConnectorTableMetadata

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

the class TrinoHiveCatalog method createMaterializedView.

@Override
public void createMaterializedView(ConnectorSession session, SchemaTableName schemaViewName, ConnectorMaterializedViewDefinition definition, boolean replace, boolean ignoreExisting) {
    Optional<io.trino.plugin.hive.metastore.Table> existing = metastore.getTable(schemaViewName.getSchemaName(), schemaViewName.getTableName());
    // It's a create command where the materialized view already exists and 'if not exists' clause is not specified
    if (!replace && existing.isPresent()) {
        if (ignoreExisting) {
            return;
        }
        throw new TrinoException(ALREADY_EXISTS, "Materialized view already exists: " + schemaViewName);
    }
    // Generate a storage table name and create a storage table. The properties in the definition are table properties for the
    // storage table as indicated in the materialized view definition.
    String storageTableName = "st_" + randomUUID().toString().replace("-", "");
    Map<String, Object> storageTableProperties = new HashMap<>(definition.getProperties());
    storageTableProperties.putIfAbsent(FILE_FORMAT_PROPERTY, DEFAULT_FILE_FORMAT_DEFAULT);
    SchemaTableName storageTable = new SchemaTableName(schemaViewName.getSchemaName(), storageTableName);
    List<ColumnMetadata> columns = definition.getColumns().stream().map(column -> new ColumnMetadata(column.getName(), typeManager.getType(column.getType()))).collect(toImmutableList());
    ConnectorTableMetadata tableMetadata = new ConnectorTableMetadata(storageTable, columns, storageTableProperties, Optional.empty());
    Transaction transaction = IcebergUtil.newCreateTableTransaction(this, tableMetadata, session);
    transaction.newAppend().commit();
    transaction.commitTransaction();
    // Create a view indicating the storage table
    Map<String, String> viewProperties = ImmutableMap.<String, String>builder().put(PRESTO_QUERY_ID_NAME, session.getQueryId()).put(STORAGE_TABLE, storageTableName).put(PRESTO_VIEW_FLAG, "true").put(TRINO_CREATED_BY, TRINO_CREATED_BY_VALUE).put(TABLE_COMMENT, ICEBERG_MATERIALIZED_VIEW_COMMENT).buildOrThrow();
    Column dummyColumn = new Column("dummy", HIVE_STRING, Optional.empty());
    io.trino.plugin.hive.metastore.Table.Builder tableBuilder = io.trino.plugin.hive.metastore.Table.builder().setDatabaseName(schemaViewName.getSchemaName()).setTableName(schemaViewName.getTableName()).setOwner(isUsingSystemSecurity ? Optional.empty() : Optional.of(session.getUser())).setTableType(VIRTUAL_VIEW.name()).setDataColumns(ImmutableList.of(dummyColumn)).setPartitionColumns(ImmutableList.of()).setParameters(viewProperties).withStorage(storage -> storage.setStorageFormat(VIEW_STORAGE_FORMAT)).withStorage(storage -> storage.setLocation("")).setViewOriginalText(Optional.of(encodeMaterializedViewData(fromConnectorMaterializedViewDefinition(definition)))).setViewExpandedText(Optional.of("/* Presto Materialized View */"));
    io.trino.plugin.hive.metastore.Table table = tableBuilder.build();
    PrincipalPrivileges principalPrivileges = isUsingSystemSecurity ? NO_PRIVILEGES : buildInitialPrivilegeSet(session.getUser());
    if (existing.isPresent() && replace) {
        // drop the current storage table
        String oldStorageTable = existing.get().getParameters().get(STORAGE_TABLE);
        if (oldStorageTable != null) {
            metastore.dropTable(schemaViewName.getSchemaName(), oldStorageTable, true);
        }
        // Replace the existing view definition
        metastore.replaceTable(schemaViewName.getSchemaName(), schemaViewName.getTableName(), table, principalPrivileges);
        return;
    }
    // create the view definition
    metastore.createTable(table, principalPrivileges);
}
Also used : FileSystem(org.apache.hadoop.fs.FileSystem) Throwables.throwIfUnchecked(com.google.common.base.Throwables.throwIfUnchecked) ICEBERG_TABLE_TYPE_VALUE(org.apache.iceberg.BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE) TABLE_TYPE_PROP(org.apache.iceberg.BaseMetastoreTableOperations.TABLE_TYPE_PROP) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Column(io.trino.plugin.hive.metastore.Column) TABLE_NOT_FOUND(io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND) Duration(java.time.Duration) Map(java.util.Map) ViewNotFoundException(io.trino.spi.connector.ViewNotFoundException) TABLE_COMMENT(io.trino.plugin.hive.HiveMetadata.TABLE_COMMENT) HdfsEnvironment(io.trino.plugin.hive.HdfsEnvironment) BaseTable(org.apache.iceberg.BaseTable) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Schema(org.apache.iceberg.Schema) IcebergUtil.validateTableCanBeDropped(io.trino.plugin.iceberg.IcebergUtil.validateTableCanBeDropped) SchemaTableName(io.trino.spi.connector.SchemaTableName) HiveMetadata(io.trino.plugin.hive.HiveMetadata) Stream(java.util.stream.Stream) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) STORAGE_TABLE(io.trino.plugin.hive.HiveMetadata.STORAGE_TABLE) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) CatalogUtil.dropTableData(org.apache.iceberg.CatalogUtil.dropTableData) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal) HiveUtil(io.trino.plugin.hive.util.HiveUtil) HiveViewNotSupportedException(io.trino.plugin.hive.HiveViewNotSupportedException) IcebergMaterializedViewDefinition(io.trino.plugin.iceberg.IcebergMaterializedViewDefinition) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) HIVE_INVALID_METADATA(io.trino.plugin.hive.HiveErrorCode.HIVE_INVALID_METADATA) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) SCHEMA_NOT_EMPTY(io.trino.spi.StandardErrorCode.SCHEMA_NOT_EMPTY) FILE_FORMAT_PROPERTY(io.trino.plugin.iceberg.IcebergTableProperties.FILE_FORMAT_PROPERTY) AbstractTrinoCatalog(io.trino.plugin.iceberg.catalog.AbstractTrinoCatalog) ViewReaderUtil.isPrestoView(io.trino.plugin.hive.ViewReaderUtil.isPrestoView) Table(org.apache.iceberg.Table) IOException(java.io.IOException) ConnectorSession(io.trino.spi.connector.ConnectorSession) CatalogName(io.trino.plugin.base.CatalogName) Failsafe(net.jodah.failsafe.Failsafe) HiveUtil.isHiveSystemSchema(io.trino.plugin.hive.util.HiveUtil.isHiveSystemSchema) ChronoUnit(java.time.temporal.ChronoUnit) ViewReaderUtil.isHiveOrPrestoView(io.trino.plugin.hive.ViewReaderUtil.isHiveOrPrestoView) MaterializedViewNotFoundException(io.trino.spi.connector.MaterializedViewNotFoundException) IcebergSchemaProperties.getSchemaLocation(io.trino.plugin.iceberg.IcebergSchemaProperties.getSchemaLocation) IcebergMaterializedViewDefinition.decodeMaterializedViewData(io.trino.plugin.iceberg.IcebergMaterializedViewDefinition.decodeMaterializedViewData) Database(io.trino.plugin.hive.metastore.Database) HiveSchemaProperties(io.trino.plugin.hive.HiveSchemaProperties) ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) SchemaNotFoundException(io.trino.spi.connector.SchemaNotFoundException) TableMetadata(org.apache.iceberg.TableMetadata) IcebergUtil(io.trino.plugin.iceberg.IcebergUtil) Locale(java.util.Locale) ALREADY_EXISTS(io.trino.spi.StandardErrorCode.ALREADY_EXISTS) Path(org.apache.hadoop.fs.Path) ViewReaderUtil.encodeViewData(io.trino.plugin.hive.ViewReaderUtil.encodeViewData) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition) ImmutableSet(com.google.common.collect.ImmutableSet) HiveWriteUtils.getTableDefaultLocation(io.trino.plugin.hive.util.HiveWriteUtils.getTableDefaultLocation) ImmutableMap(com.google.common.collect.ImmutableMap) IcebergTableOperationsProvider(io.trino.plugin.iceberg.catalog.IcebergTableOperationsProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VIEW_STORAGE_FORMAT(io.trino.plugin.hive.metastore.StorageFormat.VIEW_STORAGE_FORMAT) TableAlreadyExistsException(io.trino.plugin.hive.TableAlreadyExistsException) TrinoException(io.trino.spi.TrinoException) RetryPolicy(net.jodah.failsafe.RetryPolicy) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) PRESTO_VIEW_FLAG(io.trino.plugin.hive.ViewReaderUtil.PRESTO_VIEW_FLAG) INVALID_SCHEMA_PROPERTY(io.trino.spi.StandardErrorCode.INVALID_SCHEMA_PROPERTY) HdfsContext(io.trino.plugin.hive.HdfsEnvironment.HdfsContext) List(java.util.List) IcebergUtil.loadIcebergTable(io.trino.plugin.iceberg.IcebergUtil.loadIcebergTable) MetastoreUtil.buildInitialPrivilegeSet(io.trino.plugin.hive.metastore.MetastoreUtil.buildInitialPrivilegeSet) PartitionSpec(org.apache.iceberg.PartitionSpec) Optional(java.util.Optional) Logger(io.airlift.log.Logger) PartitionFields.toPartitionFields(io.trino.plugin.iceberg.PartitionFields.toPartitionFields) ColumnIdentity(io.trino.plugin.iceberg.ColumnIdentity) ViewReaderUtil(io.trino.plugin.hive.ViewReaderUtil) HashMap(java.util.HashMap) IcebergUtil.getIcebergTableWithMetadata(io.trino.plugin.iceberg.IcebergUtil.getIcebergTableWithMetadata) PARTITIONING_PROPERTY(io.trino.plugin.iceberg.IcebergTableProperties.PARTITIONING_PROPERTY) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) CachingHiveMetastore(io.trino.plugin.hive.metastore.cache.CachingHiveMetastore) VIRTUAL_VIEW(org.apache.hadoop.hive.metastore.TableType.VIRTUAL_VIEW) IcebergMaterializedViewDefinition.encodeMaterializedViewData(io.trino.plugin.iceberg.IcebergMaterializedViewDefinition.encodeMaterializedViewData) NO_PRIVILEGES(io.trino.plugin.hive.metastore.PrincipalPrivileges.NO_PRIVILEGES) ViewAlreadyExistsException(io.trino.plugin.hive.ViewAlreadyExistsException) HIVE_STRING(io.trino.plugin.hive.HiveType.HIVE_STRING) IcebergMaterializedViewDefinition.fromConnectorMaterializedViewDefinition(io.trino.plugin.iceberg.IcebergMaterializedViewDefinition.fromConnectorMaterializedViewDefinition) UUID.randomUUID(java.util.UUID.randomUUID) Transaction(org.apache.iceberg.Transaction) DEFAULT_FILE_FORMAT_DEFAULT(org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT_DEFAULT) PrincipalPrivileges(io.trino.plugin.hive.metastore.PrincipalPrivileges) TypeManager(io.trino.spi.type.TypeManager) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) BaseTable(org.apache.iceberg.BaseTable) Table(org.apache.iceberg.Table) IcebergUtil.loadIcebergTable(io.trino.plugin.iceberg.IcebergUtil.loadIcebergTable) PrincipalPrivileges(io.trino.plugin.hive.metastore.PrincipalPrivileges) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SchemaTableName(io.trino.spi.connector.SchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Transaction(org.apache.iceberg.Transaction) Column(io.trino.plugin.hive.metastore.Column) TrinoException(io.trino.spi.TrinoException) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata)

Example 82 with ConnectorTableMetadata

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

the class TestingMetadata method renameColumn.

@Override
public void renameColumn(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle source, String target) {
    ConnectorTableMetadata tableMetadata = getTableMetadata(session, tableHandle);
    SchemaTableName tableName = getTableName(tableHandle);
    ColumnMetadata columnMetadata = getColumnMetadata(session, tableHandle, source);
    List<ColumnMetadata> columns = new ArrayList<>(tableMetadata.getColumns());
    columns.set(columns.indexOf(columnMetadata), ColumnMetadata.builderFrom(columnMetadata).setName(target).build());
    tables.put(tableName, new ConnectorTableMetadata(tableName, ImmutableList.copyOf(columns), tableMetadata.getProperties(), tableMetadata.getComment()));
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ArrayList(java.util.ArrayList) SchemaTableName(io.trino.spi.connector.SchemaTableName) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata)

Example 83 with ConnectorTableMetadata

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

the class TestingMetadata method renameTable.

@Override
public void renameTable(ConnectorSession session, ConnectorTableHandle tableHandle, SchemaTableName newTableName) {
    // TODO: use locking to do this properly
    ConnectorTableMetadata table = getTableMetadata(session, tableHandle);
    if (tables.putIfAbsent(newTableName, table) != null) {
        throw new IllegalArgumentException("Target table already exists: " + newTableName);
    }
    tables.remove(table.getTable(), table);
}
Also used : ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata)

Example 84 with ConnectorTableMetadata

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

the class TestingMetadata method getTableMetadata.

@Override
public ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle tableHandle) {
    requireNonNull(tableHandle, "tableHandle is null");
    SchemaTableName tableName = getTableName(tableHandle);
    ConnectorTableMetadata tableMetadata = tables.get(tableName);
    checkArgument(tableMetadata != null, "Table '%s' does not exist", tableName);
    return tableMetadata;
}
Also used : SchemaTableName(io.trino.spi.connector.SchemaTableName) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata)

Example 85 with ConnectorTableMetadata

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

the class TestMaterializedViews method createLocalQueryRunner.

@Override
protected LocalQueryRunner createLocalQueryRunner() {
    Session.SessionBuilder sessionBuilder = testSessionBuilder().setCatalog(CATALOG).setSchema(SCHEMA).setSystemProperty("task_concurrency", // these tests don't handle exchanges from local parallel
    "1");
    TestingMetadata testingConnectorMetadata = new TestingMetadata();
    LocalQueryRunner queryRunner = LocalQueryRunner.create(sessionBuilder.build());
    queryRunner.createCatalog(CATALOG, new StaticConnectorFactory("test", new TestMaterializedViewConnector(testingConnectorMetadata)), ImmutableMap.of());
    Metadata metadata = queryRunner.getMetadata();
    SchemaTableName testTable = new SchemaTableName(SCHEMA, "test_table");
    queryRunner.inTransaction(session -> {
        metadata.createTable(session, CATALOG, new ConnectorTableMetadata(testTable, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT))), false);
        return null;
    });
    SchemaTableName storageTable = new SchemaTableName(SCHEMA, "storage_table");
    queryRunner.inTransaction(session -> {
        metadata.createTable(session, CATALOG, new ConnectorTableMetadata(storageTable, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT))), false);
        return null;
    });
    SchemaTableName storageTableWithCasts = new SchemaTableName(SCHEMA, "storage_table_with_casts");
    queryRunner.inTransaction(session -> {
        metadata.createTable(session, CATALOG, new ConnectorTableMetadata(storageTableWithCasts, ImmutableList.of(new ColumnMetadata("a", TINYINT), new ColumnMetadata("b", VARCHAR))), false);
        return null;
    });
    QualifiedObjectName freshMaterializedView = new QualifiedObjectName(CATALOG, SCHEMA, "fresh_materialized_view");
    MaterializedViewDefinition materializedViewDefinition = new MaterializedViewDefinition("SELECT a, b FROM test_table", Optional.of(CATALOG), Optional.of(SCHEMA), ImmutableList.of(new ViewColumn("a", BIGINT.getTypeId()), new ViewColumn("b", BIGINT.getTypeId())), Optional.empty(), Identity.ofUser("some user"), Optional.of(new CatalogSchemaTableName(CATALOG, SCHEMA, "storage_table")), ImmutableMap.of());
    queryRunner.inTransaction(session -> {
        metadata.createMaterializedView(session, freshMaterializedView, materializedViewDefinition, false, false);
        return null;
    });
    testingConnectorMetadata.markMaterializedViewIsFresh(freshMaterializedView.asSchemaTableName());
    QualifiedObjectName notFreshMaterializedView = new QualifiedObjectName(CATALOG, SCHEMA, "not_fresh_materialized_view");
    queryRunner.inTransaction(session -> {
        metadata.createMaterializedView(session, notFreshMaterializedView, materializedViewDefinition, false, false);
        return null;
    });
    MaterializedViewDefinition materializedViewDefinitionWithCasts = new MaterializedViewDefinition("SELECT a, b FROM test_table", Optional.of(CATALOG), Optional.of(SCHEMA), ImmutableList.of(new ViewColumn("a", BIGINT.getTypeId()), new ViewColumn("b", BIGINT.getTypeId())), Optional.empty(), Identity.ofUser("some user"), Optional.of(new CatalogSchemaTableName(CATALOG, SCHEMA, "storage_table_with_casts")), ImmutableMap.of());
    QualifiedObjectName materializedViewWithCasts = new QualifiedObjectName(CATALOG, SCHEMA, "materialized_view_with_casts");
    queryRunner.inTransaction(session -> {
        metadata.createMaterializedView(session, materializedViewWithCasts, materializedViewDefinitionWithCasts, false, false);
        return null;
    });
    testingConnectorMetadata.markMaterializedViewIsFresh(materializedViewWithCasts.asSchemaTableName());
    return queryRunner;
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ViewColumn(io.trino.metadata.ViewColumn) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TestingMetadata(io.trino.testing.TestingMetadata) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) Metadata(io.trino.metadata.Metadata) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) SchemaTableName(io.trino.spi.connector.SchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) LocalQueryRunner(io.trino.testing.LocalQueryRunner) StaticConnectorFactory(io.trino.connector.StaticConnectorFactory) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) MaterializedViewDefinition(io.trino.metadata.MaterializedViewDefinition) TestingMetadata(io.trino.testing.TestingMetadata) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) ConnectorSession(io.trino.spi.connector.ConnectorSession) Session(io.trino.Session)

Aggregations

ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)113 SchemaTableName (io.trino.spi.connector.SchemaTableName)69 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)62 Test (org.testng.annotations.Test)48 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)39 ConnectorSession (io.trino.spi.connector.ConnectorSession)35 ImmutableList (com.google.common.collect.ImmutableList)34 List (java.util.List)32 ImmutableMap (com.google.common.collect.ImmutableMap)31 ConnectorMetadata (io.trino.spi.connector.ConnectorMetadata)30 ColumnHandle (io.trino.spi.connector.ColumnHandle)25 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)24 ConnectorOutputTableHandle (io.trino.spi.connector.ConnectorOutputTableHandle)20 Optional (java.util.Optional)20 TrinoException (io.trino.spi.TrinoException)19 Map (java.util.Map)19 Type (io.trino.spi.type.Type)18 CatalogSchemaTableName (io.trino.spi.connector.CatalogSchemaTableName)17 Constraint (io.trino.spi.connector.Constraint)17 ImmutableSet (com.google.common.collect.ImmutableSet)16