8989import org .apache .iceberg .exceptions .NotFoundException ;
9090import org .apache .iceberg .io .FileIO ;
9191
92+ import java .io .IOException ;
9293import java .time .Duration ;
9394import java .util .HashMap ;
9495import java .util .Iterator ;
@@ -1129,12 +1130,28 @@ public void createMaterializedView(
11291130 encodeMaterializedViewData (fromConnectorMaterializedViewDefinition (definition )),
11301131 isUsingSystemSecurity ? null : session .getUser (),
11311132 createMaterializedViewProperties (session , storageMetadataLocation ));
1132- if (existing .isPresent ()) {
1133- updateTable (viewName .getSchemaName (), materializedViewTableInput );
1133+ try {
1134+ if (existing .isPresent ()) {
1135+ updateTable (viewName .getSchemaName (), materializedViewTableInput );
1136+ }
1137+ else {
1138+ createTable (viewName .getSchemaName (), materializedViewTableInput );
1139+ }
11341140 }
1135- else {
1136- createTable (viewName .getSchemaName (), materializedViewTableInput );
1141+ catch (RuntimeException e ) {
1142+ try {
1143+ dropMaterializedViewStorage (fileSystemFactory .create (session ), storageMetadataLocation .toString ());
1144+ }
1145+ catch (Exception suppressed ) {
1146+ LOG .warn (suppressed , "Failed to clean up metadata '%s' for materialized view '%s'" , storageMetadataLocation , viewName );
1147+ if (e != suppressed ) {
1148+ e .addSuppressed (suppressed );
1149+ }
1150+ }
1151+ throw e ;
11371152 }
1153+
1154+ existing .ifPresent (existingView -> dropMaterializedViewStorage (session , existingView ));
11381155 }
11391156 else {
11401157 createMaterializedViewWithStorageTable (session , viewName , definition , materializedViewProperties , existing );
@@ -1173,7 +1190,7 @@ private void createMaterializedViewWithStorageTable(
11731190 }
11741191 }
11751192 }
1176- dropStorageTable (session , existing .get ());
1193+ dropMaterializedViewStorage (session , existing .get ());
11771194 }
11781195 else {
11791196 createTable (viewName .getSchemaName (), materializedViewTableInput );
@@ -1227,11 +1244,11 @@ public void dropMaterializedView(ConnectorSession session, SchemaTableName viewN
12271244 throw new TrinoException (UNSUPPORTED_TABLE_TYPE , "Not a Materialized View: " + view .getDatabaseName () + "." + view .getName ());
12281245 }
12291246 materializedViewCache .invalidate (viewName );
1230- dropStorageTable (session , view );
1247+ dropMaterializedViewStorage (session , view );
12311248 deleteTable (view .getDatabaseName (), view .getName ());
12321249 }
12331250
1234- private void dropStorageTable (ConnectorSession session , com .amazonaws .services .glue .model .Table view )
1251+ private void dropMaterializedViewStorage (ConnectorSession session , com .amazonaws .services .glue .model .Table view )
12351252 {
12361253 Map <String , String > parameters = getTableParameters (view );
12371254 String storageTableName = parameters .get (STORAGE_TABLE );
@@ -1245,6 +1262,16 @@ private void dropStorageTable(ConnectorSession session, com.amazonaws.services.g
12451262 LOG .warn (e , "Failed to drop storage table '%s.%s' for materialized view '%s'" , storageSchema , storageTableName , view .getName ());
12461263 }
12471264 }
1265+ else {
1266+ String storageMetadataLocation = parameters .get (METADATA_LOCATION_PROP );
1267+ checkState (storageMetadataLocation != null , "Storage location missing in definition of materialized view " + view .getName ());
1268+ try {
1269+ dropMaterializedViewStorage (fileSystemFactory .create (session ), storageMetadataLocation );
1270+ }
1271+ catch (IOException e ) {
1272+ LOG .warn (e , "Failed to delete storage table metadata '%s' for materialized view '%s'" , storageMetadataLocation , view .getName ());
1273+ }
1274+ }
12481275 }
12491276
12501277 @ Override
0 commit comments