Skip to content

Commit

Permalink
Rename TrinoCatalog materialized view parameters
Browse files Browse the repository at this point in the history
`viewName` matches the parameter name used in ConnectorMetadata.
  • Loading branch information
alexjo2144 authored and findepi committed Apr 13, 2022
1 parent 79d2d7a commit 9c2e0ff
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ Transaction newCreateTableTransaction(

void createMaterializedView(
ConnectorSession session,
SchemaTableName schemaViewName,
SchemaTableName viewName,
ConnectorMaterializedViewDefinition definition,
boolean replace,
boolean ignoreExisting);

void dropMaterializedView(ConnectorSession session, SchemaTableName schemaViewName);
void dropMaterializedView(ConnectorSession session, SchemaTableName viewName);

Optional<ConnectorMaterializedViewDefinition> getMaterializedView(ConnectorSession session, SchemaTableName schemaViewName);
Optional<ConnectorMaterializedViewDefinition> getMaterializedView(ConnectorSession session, SchemaTableName viewName);

void renameMaterializedView(ConnectorSession session, SchemaTableName source, SchemaTableName target);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,19 +560,19 @@ public List<SchemaTableName> listMaterializedViews(ConnectorSession session, Opt
}

@Override
public void createMaterializedView(ConnectorSession session, SchemaTableName schemaViewName, ConnectorMaterializedViewDefinition definition, boolean replace, boolean ignoreExisting)
public void createMaterializedView(ConnectorSession session, SchemaTableName viewName, ConnectorMaterializedViewDefinition definition, boolean replace, boolean ignoreExisting)
{
throw new TrinoException(NOT_SUPPORTED, "createMaterializedView is not supported for Iceberg Glue catalogs");
}

@Override
public void dropMaterializedView(ConnectorSession session, SchemaTableName schemaViewName)
public void dropMaterializedView(ConnectorSession session, SchemaTableName viewName)
{
throw new TrinoException(NOT_SUPPORTED, "dropMaterializedView is not supported for Iceberg Glue catalogs");
}

@Override
public Optional<ConnectorMaterializedViewDefinition> getMaterializedView(ConnectorSession session, SchemaTableName schemaViewName)
public Optional<ConnectorMaterializedViewDefinition> getMaterializedView(ConnectorSession session, SchemaTableName viewName)
{
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,17 @@ public List<SchemaTableName> listMaterializedViews(ConnectorSession session, Opt
}

@Override
public void createMaterializedView(ConnectorSession session, SchemaTableName schemaViewName, ConnectorMaterializedViewDefinition definition,
public void createMaterializedView(ConnectorSession session, SchemaTableName viewName, ConnectorMaterializedViewDefinition definition,
boolean replace, boolean ignoreExisting)
{
Optional<io.trino.plugin.hive.metastore.Table> existing = metastore.getTable(schemaViewName.getSchemaName(), schemaViewName.getTableName());
Optional<io.trino.plugin.hive.metastore.Table> existing = metastore.getTable(viewName.getSchemaName(), viewName.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);
throw new TrinoException(ALREADY_EXISTS, "Materialized view already exists: " + viewName);
}

// Generate a storage table name and create a storage table. The properties in the definition are table properties for the
Expand All @@ -471,7 +471,7 @@ public void createMaterializedView(ConnectorSession session, SchemaTableName sch
Map<String, Object> storageTableProperties = new HashMap<>(definition.getProperties());
storageTableProperties.putIfAbsent(FILE_FORMAT_PROPERTY, DEFAULT_FILE_FORMAT_DEFAULT);

SchemaTableName storageTable = new SchemaTableName(schemaViewName.getSchemaName(), storageTableName);
SchemaTableName storageTable = new SchemaTableName(viewName.getSchemaName(), storageTableName);
List<ColumnMetadata> columns = definition.getColumns().stream()
.map(column -> new ColumnMetadata(column.getName(), typeManager.getType(column.getType())))
.collect(toImmutableList());
Expand All @@ -493,8 +493,8 @@ public void createMaterializedView(ConnectorSession session, SchemaTableName sch
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())
.setDatabaseName(viewName.getSchemaName())
.setTableName(viewName.getTableName())
.setOwner(isUsingSystemSecurity ? Optional.empty() : Optional.of(session.getUser()))
.setTableType(VIRTUAL_VIEW.name())
.setDataColumns(ImmutableList.of(dummyColumn))
Expand All @@ -511,44 +511,44 @@ public void createMaterializedView(ConnectorSession session, SchemaTableName sch
// drop the current storage table
String oldStorageTable = existing.get().getParameters().get(STORAGE_TABLE);
if (oldStorageTable != null) {
metastore.dropTable(schemaViewName.getSchemaName(), oldStorageTable, true);
metastore.dropTable(viewName.getSchemaName(), oldStorageTable, true);
}
// Replace the existing view definition
metastore.replaceTable(schemaViewName.getSchemaName(), schemaViewName.getTableName(), table, principalPrivileges);
metastore.replaceTable(viewName.getSchemaName(), viewName.getTableName(), table, principalPrivileges);
return;
}
// create the view definition
metastore.createTable(table, principalPrivileges);
}

@Override
public void dropMaterializedView(ConnectorSession session, SchemaTableName schemaViewName)
public void dropMaterializedView(ConnectorSession session, SchemaTableName viewName)
{
io.trino.plugin.hive.metastore.Table view = metastore.getTable(schemaViewName.getSchemaName(), schemaViewName.getTableName())
.orElseThrow(() -> new MaterializedViewNotFoundException(schemaViewName));
io.trino.plugin.hive.metastore.Table view = metastore.getTable(viewName.getSchemaName(), viewName.getTableName())
.orElseThrow(() -> new MaterializedViewNotFoundException(viewName));

String storageTableName = view.getParameters().get(STORAGE_TABLE);
if (storageTableName != null) {
try {
metastore.dropTable(schemaViewName.getSchemaName(), storageTableName, true);
metastore.dropTable(viewName.getSchemaName(), storageTableName, true);
}
catch (TrinoException e) {
log.warn(e, "Failed to drop storage table '%s' for materialized view '%s'", storageTableName, schemaViewName);
log.warn(e, "Failed to drop storage table '%s' for materialized view '%s'", storageTableName, viewName);
}
}
metastore.dropTable(schemaViewName.getSchemaName(), schemaViewName.getTableName(), true);
metastore.dropTable(viewName.getSchemaName(), viewName.getTableName(), true);
}

@Override
public Optional<ConnectorMaterializedViewDefinition> getMaterializedView(ConnectorSession session, SchemaTableName schemaViewName)
public Optional<ConnectorMaterializedViewDefinition> getMaterializedView(ConnectorSession session, SchemaTableName viewName)
{
try {
return Failsafe.with(new RetryPolicy<>()
.withMaxAttempts(10)
.withBackoff(1, 5_000, ChronoUnit.MILLIS, 4)
.withMaxDuration(Duration.ofSeconds(30))
.abortOn(failure -> !(failure instanceof MaterializedViewMayBeBeingRemovedException)))
.get(() -> doGetMaterializedView(session, schemaViewName));
.get(() -> doGetMaterializedView(session, viewName));
}
catch (MaterializedViewMayBeBeingRemovedException e) {
throwIfUnchecked(e.getCause());
Expand Down

0 comments on commit 9c2e0ff

Please sign in to comment.