@@ -423,6 +423,70 @@ public void testView()
423
423
}
424
424
}
425
425
426
+ @ Test
427
+ public void testViewUnique ()
428
+ throws IOException
429
+ {
430
+ TrinoCatalog catalog = createTrinoCatalog (true );
431
+ Path tmpDirectory = Files .createTempDirectory ("iceberg_catalog_test_create_view_" );
432
+ tmpDirectory .toFile ().deleteOnExit ();
433
+
434
+ String namespace = "test_create_view_" + randomNameSuffix ();
435
+ String viewName = "viewName" ;
436
+ String renamedViewName = "renamedViewName" ;
437
+ SchemaTableName schemaTableName = new SchemaTableName (namespace , viewName );
438
+ SchemaTableName renamedSchemaTableName = new SchemaTableName (namespace , renamedViewName );
439
+ ConnectorViewDefinition viewDefinition = new ConnectorViewDefinition (
440
+ "SELECT name FROM local.tiny.nation" ,
441
+ Optional .empty (),
442
+ Optional .empty (),
443
+ ImmutableList .of (
444
+ new ConnectorViewDefinition .ViewColumn ("name" , VarcharType .createUnboundedVarcharType ().getTypeId (), Optional .empty ())),
445
+ Optional .empty (),
446
+ Optional .of (SESSION .getUser ()),
447
+ false ,
448
+ ImmutableList .of ());
449
+
450
+ try {
451
+ catalog .createNamespace (SESSION , namespace , defaultNamespaceProperties (namespace ), new TrinoPrincipal (PrincipalType .USER , SESSION .getUser ()));
452
+ catalog .createView (SESSION , schemaTableName , viewDefinition , false );
453
+
454
+ assertThat (catalog .listTables (SESSION , Optional .of (namespace )).stream ()).contains (new TableInfo (schemaTableName , getViewType ()));
455
+
456
+ Map <SchemaTableName , ConnectorViewDefinition > views = catalog .getViews (SESSION , Optional .of (schemaTableName .getSchemaName ()));
457
+ assertThat (views ).hasSize (1 );
458
+ assertViewDefinition (views .get (schemaTableName ), viewDefinition );
459
+ assertViewDefinition (catalog .getView (SESSION , schemaTableName ).orElseThrow (), viewDefinition );
460
+
461
+ catalog .createView (SESSION , schemaTableName , viewDefinition , true );
462
+ assertThat (catalog .listTables (SESSION , Optional .of (namespace )).stream ()).contains (new TableInfo (schemaTableName , getViewType ()));
463
+ views = catalog .getViews (SESSION , Optional .of (schemaTableName .getSchemaName ()));
464
+ assertThat (views ).hasSize (1 );
465
+ assertViewDefinition (views .get (schemaTableName ), viewDefinition );
466
+ assertViewDefinition (catalog .getView (SESSION , schemaTableName ).orElseThrow (), viewDefinition );
467
+
468
+ catalog .renameView (SESSION , schemaTableName , renamedSchemaTableName );
469
+ assertThat (catalog .listTables (SESSION , Optional .of (namespace )).stream ().map (TableInfo ::tableName ).toList ()).doesNotContain (schemaTableName );
470
+ views = catalog .getViews (SESSION , Optional .of (schemaTableName .getSchemaName ()));
471
+ assertThat (views ).hasSize (1 );
472
+ assertViewDefinition (views .get (renamedSchemaTableName ), viewDefinition );
473
+ assertViewDefinition (catalog .getView (SESSION , renamedSchemaTableName ).orElseThrow (), viewDefinition );
474
+ assertThat (catalog .getView (SESSION , schemaTableName )).isEmpty ();
475
+
476
+ catalog .dropView (SESSION , renamedSchemaTableName );
477
+ assertThat (catalog .listTables (SESSION , Optional .empty ()).stream ().map (TableInfo ::tableName ).toList ())
478
+ .doesNotContain (renamedSchemaTableName );
479
+ }
480
+ finally {
481
+ try {
482
+ catalog .dropNamespace (SESSION , namespace );
483
+ }
484
+ catch (Exception e ) {
485
+ LOG .warn ("Failed to clean up namespace: %s" , namespace );
486
+ }
487
+ }
488
+ }
489
+
426
490
protected ExtendedRelationType getViewType ()
427
491
{
428
492
return TRINO_VIEW ;
0 commit comments