rdblue commented on code in PR #7880:
URL: https://github.com/apache/iceberg/pull/7880#discussion_r1337888441


##########
core/src/test/java/org/apache/iceberg/view/ViewCatalogTests.java:
##########
@@ -0,0 +1,1283 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.view;
+
+import static org.apache.iceberg.types.Types.NestedField.required;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Transaction;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.catalog.ViewCatalog;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.NoSuchViewException;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.types.Types;
+import org.assertj.core.api.Assumptions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+public abstract class ViewCatalogTests<C extends ViewCatalog & 
SupportsNamespaces> {
+  // the schema ID of SCHEMA / OTHER_SCHEMA are by default set to 0. The 
schema id of SCHEMA will
+  // stay 0, but the schema id of OTHER_SCHEMA will be re-assigned to 1
+  private static final int EXPECTED_SCHEMA_ID = 0;
+  private static final int EXPECTED_OTHER_SCHEMA_ID = 1;
+
+  protected static final Schema SCHEMA =
+      new Schema(
+          required(3, "id", Types.IntegerType.get(), "unique ID"),
+          required(4, "data", Types.StringType.get()));
+
+  private static final Schema OTHER_SCHEMA =
+      new Schema(required(1, "some_id", Types.IntegerType.get()));
+
+  protected abstract C catalog();
+
+  protected abstract Catalog tableCatalog();
+
+  protected boolean requiresNamespaceCreate() {
+    return false;
+  }
+
+  @Test
+  public void basicCreateView() {
+    TableIdentifier identifier = TableIdentifier.of("ns", "view");
+
+    if (requiresNamespaceCreate()) {
+      catalog().createNamespace(identifier.namespace());
+    }
+
+    assertThat(catalog().viewExists(identifier)).as("View should not 
exist").isFalse();
+
+    View view =
+        catalog()
+            .buildView(identifier)
+            .withSchema(SCHEMA)
+            .withDefaultNamespace(identifier.namespace())
+            .withQuery("spark", "select * from ns.tbl")
+            .create();
+
+    assertThat(view).isNotNull();
+    assertThat(catalog().viewExists(identifier)).as("View should 
exist").isTrue();
+
+    // validate view settings
+    assertThat(view.name()).isEqualTo(ViewUtil.fullViewName(catalog().name(), 
identifier));
+    assertThat(view.history())
+        .hasSize(1)
+        .first()
+        .extracting(ViewHistoryEntry::versionId)
+        .isEqualTo(1);
+    assertThat(view.schema().schemaId()).isEqualTo(EXPECTED_SCHEMA_ID);
+    assertThat(view.schema().asStruct()).isEqualTo(SCHEMA.asStruct());
+    assertThat(view.schemas()).hasSize(1).containsKey(EXPECTED_SCHEMA_ID);
+    
assertThat(view.versions()).hasSize(1).containsExactly(view.currentVersion());
+
+    assertThat(view.currentVersion())
+        .isEqualTo(
+            ImmutableViewVersion.builder()
+                .timestampMillis(view.currentVersion().timestampMillis())
+                .versionId(1)
+                .schemaId(EXPECTED_SCHEMA_ID)
+                .putSummary("operation", "create")
+                .defaultNamespace(identifier.namespace())
+                .addRepresentations(
+                    ImmutableSQLViewRepresentation.builder()
+                        .sql("select * from ns.tbl")
+                        .dialect("spark")
+                        .build())
+                .build());
+
+    assertThat(catalog().dropView(identifier)).isTrue();
+    assertThat(catalog().viewExists(identifier)).as("View should not 
exist").isFalse();
+  }
+
+  @Test
+  public void completeCreateView() {
+    TableIdentifier identifier = TableIdentifier.of("ns", "view");
+
+    if (requiresNamespaceCreate()) {
+      catalog().createNamespace(identifier.namespace());
+    }
+
+    assertThat(catalog().viewExists(identifier)).as("View should not 
exist").isFalse();
+
+    View view =
+        catalog()
+            .buildView(identifier)
+            .withSchema(SCHEMA)
+            .withDefaultNamespace(identifier.namespace())
+            .withQuery("spark", "select * from ns.tbl")
+            .withQuery("trino", "select * from ns.tbl using X")
+            .withProperty("prop1", "val1")
+            .withProperty("prop2", "val2")
+            .create();
+
+    assertThat(view).isNotNull();
+    assertThat(catalog().viewExists(identifier)).as("View should 
exist").isTrue();
+
+    // validate view settings
+    assertThat(view.name()).isEqualTo(ViewUtil.fullViewName(catalog().name(), 
identifier));
+    assertThat(view.properties()).containsEntry("prop1", 
"val1").containsEntry("prop2", "val2");
+    assertThat(view.history())
+        .hasSize(1)
+        .first()
+        .extracting(ViewHistoryEntry::versionId)
+        .isEqualTo(1);
+    assertThat(view.schema().schemaId()).isEqualTo(EXPECTED_SCHEMA_ID);
+    assertThat(view.schema().asStruct()).isEqualTo(SCHEMA.asStruct());
+    assertThat(view.schemas()).hasSize(1).containsKey(EXPECTED_SCHEMA_ID);
+    
assertThat(view.versions()).hasSize(1).containsExactly(view.currentVersion());
+
+    assertThat(view.currentVersion())
+        .isEqualTo(
+            ImmutableViewVersion.builder()
+                .timestampMillis(view.currentVersion().timestampMillis())
+                .versionId(1)
+                .schemaId(EXPECTED_SCHEMA_ID)
+                .putSummary("operation", "create")
+                .defaultNamespace(identifier.namespace())
+                .addRepresentations(
+                    ImmutableSQLViewRepresentation.builder()
+                        .sql("select * from ns.tbl")
+                        .dialect("spark")
+                        .build())
+                .addRepresentations(
+                    ImmutableSQLViewRepresentation.builder()
+                        .sql("select * from ns.tbl using X")
+                        .dialect("trino")
+                        .build())
+                .build());
+
+    assertThat(catalog().dropView(identifier)).isTrue();
+    assertThat(catalog().viewExists(identifier)).as("View should not 
exist").isFalse();
+  }
+
+  @Test
+  public void createViewErrorCases() {
+    TableIdentifier identifier = TableIdentifier.of("ns", "view");
+
+    if (requiresNamespaceCreate()) {
+      catalog().createNamespace(identifier.namespace());
+    }
+
+    assertThat(catalog().viewExists(identifier)).as("View should not 
exist").isFalse();
+
+    SQLViewRepresentation trino =
+        ImmutableSQLViewRepresentation.builder()
+            .sql("select * from ns.tbl")
+            .dialect("trino")
+            .build();
+
+    // query is required
+    assertThatThrownBy(() -> catalog().buildView(identifier).create())
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessage("Cannot create view without specifying a query");
+
+    // schema is required
+    assertThatThrownBy(
+            () -> catalog().buildView(identifier).withQuery(trino.dialect(), 
trino.sql()).create())
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessage("Cannot create view without specifying schema");
+
+    // default namespace is required
+    assertThatThrownBy(
+            () ->
+                catalog()
+                    .buildView(identifier)
+                    .withQuery(trino.dialect(), trino.sql())
+                    .withSchema(SCHEMA)
+                    .create())
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessage("Cannot create view without specifying a default 
namespace");
+
+    // cannot define multiple SQLs for same dialect
+    assertThatThrownBy(
+            () ->
+                catalog()
+                    .buildView(identifier)
+                    .withSchema(SCHEMA)
+                    .withDefaultNamespace(identifier.namespace())
+                    .withQuery(trino.dialect(), trino.sql())
+                    .withQuery(trino.dialect(), trino.sql())
+                    .create())
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot add multiple SQLs for dialect: trino");
+  }
+
+  @Test
+  public void createViewThatAlreadyExists() {
+    TableIdentifier identifier = TableIdentifier.of("ns", "view");
+
+    if (requiresNamespaceCreate()) {
+      catalog().createNamespace(identifier.namespace());
+    }
+
+    assertThat(catalog().viewExists(identifier)).as("View should not 
exist").isFalse();
+
+    View view =
+        catalog()
+            .buildView(identifier)
+            .withSchema(SCHEMA)
+            .withDefaultNamespace(identifier.namespace())
+            .withQuery("spark", "select * from ns.tbl")
+            .create();
+
+    assertThat(view).isNotNull();
+    assertThat(catalog().viewExists(identifier)).as("View should 
exist").isTrue();
+
+    assertThatThrownBy(
+            () ->
+                catalog()
+                    .buildView(identifier)
+                    .withSchema(OTHER_SCHEMA)
+                    .withQuery("spark", "select * from ns.tbl")
+                    .withDefaultNamespace(identifier.namespace())
+                    .create())
+        .isInstanceOf(AlreadyExistsException.class)
+        .hasMessageStartingWith("View already exists: ns.view");
+  }
+
+  @Test
+  public void createViewThatAlreadyExistsAsTable() {
+    Assumptions.assumeThat(tableCatalog())
+        .as("Only valid for catalogs that support tables")
+        .isNotNull();
+
+    TableIdentifier tableIdentifier = TableIdentifier.of("ns", "table");
+
+    if (requiresNamespaceCreate()) {
+      catalog().createNamespace(tableIdentifier.namespace());
+    }
+
+    assertThat(tableCatalog().tableExists(tableIdentifier)).as("Table should 
not exist").isFalse();
+
+    tableCatalog().buildTable(tableIdentifier, SCHEMA).create();
+
+    assertThat(tableCatalog().tableExists(tableIdentifier)).as("Table should 
exist").isTrue();
+
+    assertThatThrownBy(
+            () ->
+                catalog()
+                    .buildView(tableIdentifier)
+                    .withSchema(OTHER_SCHEMA)
+                    .withDefaultNamespace(tableIdentifier.namespace())
+                    .withQuery("spark", "select * from ns.tbl")
+                    .create())
+        .isInstanceOf(AlreadyExistsException.class)
+        .hasMessageStartingWith("Table with same name already exists: 
ns.table");
+  }
+
+  @Test
+  public void createTableThatAlreadyExistsAsView() {
+    Assumptions.assumeThat(tableCatalog())
+        .as("Only valid for catalogs that support tables")
+        .isNotNull();
+
+    TableIdentifier viewIdentifier = TableIdentifier.of("ns", "view");
+
+    if (requiresNamespaceCreate()) {
+      catalog().createNamespace(viewIdentifier.namespace());
+    }
+
+    assertThat(catalog().viewExists(viewIdentifier)).as("View should not 
exist").isFalse();
+
+    catalog()
+        .buildView(viewIdentifier)
+        .withSchema(SCHEMA)
+        .withDefaultNamespace(viewIdentifier.namespace())
+        .withQuery("spark", "select * from ns.tbl")
+        .create();
+
+    assertThat(catalog().viewExists(viewIdentifier)).as("View should 
exist").isTrue();
+
+    assertThatThrownBy(() -> tableCatalog().buildTable(viewIdentifier, 
SCHEMA).create())
+        .isInstanceOf(AlreadyExistsException.class)
+        .hasMessageStartingWith("View with same name already exists: ns.view");
+  }
+
+  @Test
+  public void createTableViaTransactionThatAlreadyExistsAsView() {
+    Assumptions.assumeThat(tableCatalog())
+        .as("Only valid for catalogs that support tables")
+        .isNotNull();
+
+    TableIdentifier viewIdentifier = TableIdentifier.of("ns", "view");
+
+    if (requiresNamespaceCreate()) {
+      catalog().createNamespace(viewIdentifier.namespace());
+    }
+
+    assertThat(catalog().viewExists(viewIdentifier)).as("View should not 
exist").isFalse();
+
+    Transaction transaction = tableCatalog().buildTable(viewIdentifier, 
SCHEMA).createTransaction();
+
+    catalog()
+        .buildView(viewIdentifier)
+        .withSchema(SCHEMA)
+        .withDefaultNamespace(viewIdentifier.namespace())
+        .withQuery("spark", "select * from ns.tbl")
+        .create();
+
+    assertThat(catalog().viewExists(viewIdentifier)).as("View should 
exist").isTrue();
+
+    assertThatThrownBy(transaction::commitTransaction)
+        .isInstanceOf(AlreadyExistsException.class)
+        .hasMessageStartingWith("View with same name already exists: ns.view");
+  }
+
+  @Test
+  public void replaceTableViaTransactionThatAlreadyExistsAsView() {
+    Assumptions.assumeThat(tableCatalog())
+        .as("Only valid for catalogs that support tables")
+        .isNotNull();
+
+    TableIdentifier viewIdentifier = TableIdentifier.of("ns", "view");
+
+    if (requiresNamespaceCreate()) {
+      catalog().createNamespace(viewIdentifier.namespace());
+    }
+
+    assertThat(catalog().viewExists(viewIdentifier)).as("View should not 
exist").isFalse();
+
+    catalog()
+        .buildView(viewIdentifier)
+        .withSchema(SCHEMA)
+        .withDefaultNamespace(viewIdentifier.namespace())
+        .withQuery("spark", "select * from ns.tbl")
+        .create();
+
+    assertThat(catalog().viewExists(viewIdentifier)).as("View should 
exist").isTrue();
+
+    // replace transaction requires table existence
+    assertThatThrownBy(
+            () ->
+                tableCatalog()
+                    .buildTable(viewIdentifier, SCHEMA)
+                    .replaceTransaction()
+                    .commitTransaction())
+        .isInstanceOf(NoSuchTableException.class)
+        .hasMessageStartingWith("Table does not exist: ns.view");
+  }
+
+  @Test
+  public void replaceViewThatAlreadyExistsAsTable() {
+    Assumptions.assumeThat(tableCatalog())
+        .as("Only valid for catalogs that support tables")
+        .isNotNull();
+
+    TableIdentifier tableIdentifier = TableIdentifier.of("ns", "table");
+
+    if (requiresNamespaceCreate()) {
+      catalog().createNamespace(tableIdentifier.namespace());
+    }
+
+    assertThat(tableCatalog().tableExists(tableIdentifier)).as("Table should 
not exist").isFalse();
+
+    tableCatalog().buildTable(tableIdentifier, SCHEMA).create();
+
+    assertThat(tableCatalog().tableExists(tableIdentifier)).as("Table should 
exist").isTrue();
+
+    // replace view requires the view to exist
+    assertThatThrownBy(
+            () ->
+                catalog()
+                    .buildView(tableIdentifier)
+                    .withSchema(OTHER_SCHEMA)
+                    .withDefaultNamespace(tableIdentifier.namespace())
+                    .withQuery("spark", "select * from ns.tbl")
+                    .replace())
+        .isInstanceOf(NoSuchViewException.class)
+        .hasMessageStartingWith("View does not exist: ns.table");

Review Comment:
   Same idea as above.



-- 
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]

Reply via email to