nastra commented on code in PR #14868:
URL: https://github.com/apache/iceberg/pull/14868#discussion_r2676150710
##########
core/src/test/java/org/apache/iceberg/view/ViewCatalogTests.java:
##########
@@ -1968,4 +1973,145 @@ public void dropNonEmptyNamespace() {
.as("Namespace should not exist")
.isFalse();
}
+
+ @Test
+ public void registerView() {
+ C catalog = catalog();
+
+ // Register view is not yet supported for REST catalog
+ assumeThat(catalog).isNotInstanceOf(RESTCatalog.class);
+
+ TableIdentifier identifier = TableIdentifier.of("ns", "view");
+
+ if (requiresNamespaceCreate()) {
+ catalog.createNamespace(identifier.namespace());
+ }
+
+ View originalView =
+ catalog()
+ .buildView(identifier)
+ .withSchema(SCHEMA)
+ .withDefaultNamespace(identifier.namespace())
+ .withDefaultCatalog(catalog().name())
+ .withQuery("spark", "select * from ns.tbl")
+ .withProperty(GC_ENABLED, "false")
+ .create();
+
+ ViewOperations ops = ((BaseView) originalView).operations();
+ String metadataLocation = ops.current().metadataFileLocation();
+
+ assertThat(catalog.dropView(identifier)).isTrue();
+ assertThat(catalog.viewExists(identifier)).as("View must not
exist").isFalse();
+
+ // view metadata should still exist after dropping the view as gc is
disabled
+ assertThat(((BaseViewOperations)
ops).io().newInputFile(metadataLocation).exists()).isTrue();
+
+ View registeredView = catalog.registerView(identifier, metadataLocation);
+
+ assertThat(registeredView).isNotNull();
+ assertThat(catalog.viewExists(identifier)).as("View must exist").isTrue();
+ assertThat(registeredView.name())
+ .isEqualTo(ViewUtil.fullViewName(catalog().name(), identifier));
+ assertThat(registeredView.history())
+ .hasSize(1)
+ .first()
+ .extracting(ViewHistoryEntry::versionId)
+ .isEqualTo(1);
+ assertThat(registeredView.schemas()).hasSize(1).containsKey(0);
+
assertThat(registeredView.schema().asStruct()).isEqualTo(SCHEMA.asStruct());
+
assertThat(registeredView.currentVersion().operation()).isEqualTo("create");
+ assertThat(registeredView.versions())
+ .hasSize(1)
+ .containsExactly(registeredView.currentVersion());
+ assertThat(registeredView.versions()).isEqualTo(originalView.versions());
+ assertThat(registeredView.currentVersion())
+ .isEqualTo(
+ ImmutableViewVersion.builder()
+
.timestampMillis(registeredView.currentVersion().timestampMillis())
+ .versionId(1)
+ .schemaId(0)
+ .summary(registeredView.currentVersion().summary())
+ .defaultNamespace(identifier.namespace())
+ .defaultCatalog(catalog().name())
+ .addRepresentations(
+ ImmutableSQLViewRepresentation.builder()
+ .sql("select * from ns.tbl")
+ .dialect("spark")
+ .build())
+ .build());
+
+ assertThat(catalog.loadView(identifier)).isNotNull();
+ assertThat(catalog.dropView(identifier)).isTrue();
+ assertThat(catalog.viewExists(identifier)).isFalse();
+ }
+
+ @Test
+ public void registerExistingView() {
+ C catalog = catalog();
+
+ // Register view is not yet supported for REST catalog
+ assumeThat(catalog).isNotInstanceOf(RESTCatalog.class);
+
+ TableIdentifier identifier = TableIdentifier.of("ns", "view");
+
+ if (requiresNamespaceCreate()) {
+ catalog.createNamespace(identifier.namespace());
+ }
+
+ View view =
+ catalog()
+ .buildView(identifier)
+ .withSchema(SCHEMA)
+ .withDefaultNamespace(identifier.namespace())
+ .withDefaultCatalog(catalog().name())
+ .withQuery("spark", "select * from ns.tbl")
+ .create();
+
+ ViewOperations ops = ((BaseView) view).operations();
+ String metadataLocation = ops.current().metadataFileLocation();
+
+ assertThatThrownBy(() -> catalog.registerView(identifier,
metadataLocation))
+ .isInstanceOf(AlreadyExistsException.class)
+ .hasMessageStartingWith("View already exists: ns.view");
+ assertThat(catalog.dropView(identifier)).isTrue();
+ }
+
+ @Test
+ public void registerViewWithExistingTable() {
+ C catalog = catalog();
+
+ // Register view is not yet supported for REST catalog
+ assumeThat(catalog).isNotInstanceOf(RESTCatalog.class);
+
+ TableIdentifier identifier = TableIdentifier.of("ns", "view");
+
+ if (requiresNamespaceCreate()) {
+ catalog.createNamespace(identifier.namespace());
+ }
+
+ // create a table with the same name as the view
+ tableCatalog().createTable(identifier, SCHEMA);
+
+ ViewBuilder viewBuilder =
+ catalog()
+ .buildView(identifier)
+ .withSchema(SCHEMA)
+ .withDefaultNamespace(identifier.namespace())
+ .withDefaultCatalog(catalog().name())
+ .withQuery("spark", "select * from ns.tbl");
+
+ assertThatThrownBy(viewBuilder::create)
Review Comment:
I think what we really want to test here is to:
* first create the view and drop it but keep its metadata
* create a table with the same identifier (this should succeed)
* now try to register the original view metadata under the table's
identifier (this should fail, saying that a table with the same name already
exists)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]