use of io.trino.plugin.iceberg.IcebergMaterializedViewDefinition in project trino by trinodb.
the class TrinoHiveCatalog method doGetMaterializedView.
private Optional<ConnectorMaterializedViewDefinition> doGetMaterializedView(ConnectorSession session, SchemaTableName schemaViewName) {
Optional<io.trino.plugin.hive.metastore.Table> tableOptional = metastore.getTable(schemaViewName.getSchemaName(), schemaViewName.getTableName());
if (tableOptional.isEmpty()) {
return Optional.empty();
}
io.trino.plugin.hive.metastore.Table table = tableOptional.get();
if (!isPrestoView(table) || !isHiveOrPrestoView(table) || !table.getParameters().containsKey(STORAGE_TABLE)) {
return Optional.empty();
}
io.trino.plugin.hive.metastore.Table materializedView = tableOptional.get();
String storageTable = materializedView.getParameters().get(STORAGE_TABLE);
checkState(storageTable != null, "Storage table missing in definition of materialized view " + schemaViewName);
IcebergMaterializedViewDefinition definition = decodeMaterializedViewData(materializedView.getViewOriginalText().orElseThrow(() -> new TrinoException(HIVE_INVALID_METADATA, "No view original text: " + schemaViewName)));
Table icebergTable;
try {
icebergTable = loadTable(session, new SchemaTableName(schemaViewName.getSchemaName(), storageTable));
} catch (RuntimeException e) {
// The materialized view could be removed concurrently. This may manifest in a number of ways, e.g.
// - io.trino.spi.connector.TableNotFoundException
// - org.apache.iceberg.exceptions.NotFoundException when accessing manifest file
// - other failures when reading storage table's metadata files
// Retry, as we're catching broadly.
metastore.invalidateTable(schemaViewName.getSchemaName(), schemaViewName.getTableName());
metastore.invalidateTable(schemaViewName.getSchemaName(), storageTable);
throw new MaterializedViewMayBeBeingRemovedException(e);
}
ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();
properties.put(FILE_FORMAT_PROPERTY, IcebergUtil.getFileFormat(icebergTable));
if (!icebergTable.spec().fields().isEmpty()) {
properties.put(PARTITIONING_PROPERTY, toPartitionFields(icebergTable.spec()));
}
return Optional.of(new ConnectorMaterializedViewDefinition(definition.getOriginalSql(), Optional.of(new CatalogSchemaTableName(catalogName.toString(), new SchemaTableName(schemaViewName.getSchemaName(), storageTable))), definition.getCatalog(), definition.getSchema(), definition.getColumns().stream().map(column -> new ConnectorMaterializedViewDefinition.Column(column.getName(), column.getType())).collect(toImmutableList()), definition.getComment(), materializedView.getOwner(), properties.buildOrThrow()));
}
Aggregations