nastra commented on code in PR #6428:
URL: https://github.com/apache/iceberg/pull/6428#discussion_r1052987764


##########
snowflake/src/main/java/org/apache/iceberg/snowflake/NamespaceHelpers.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.snowflake;
+
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.snowflake.entities.SnowflakeIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class NamespaceHelpers {
+  private static final int MAX_NAMESPACE_DEPTH = 2;
+  private static final int NAMESPACE_ROOT_LEVEL = 0;
+  private static final int NAMESPACE_DB_LEVEL = 1;
+  private static final int NAMESPACE_SCHEMA_LEVEL = 2;
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(NamespaceHelpers.class);
+
+  /**
+   * Converts a Namespace into a SnowflakeIdentifier representing ROOT, a 
DATABASE, or a SCHEMA.
+   *
+   * @throws IllegalArgumentException if the namespace is not a supported 
depth.
+   */
+  public static SnowflakeIdentifier 
getSnowflakeIdentifierForNamespace(Namespace namespace) {
+    SnowflakeIdentifier identifier = null;
+    switch (namespace.length()) {
+      case NAMESPACE_ROOT_LEVEL:
+        identifier = SnowflakeIdentifier.ofRoot();
+        break;
+      case NAMESPACE_DB_LEVEL:
+        identifier = 
SnowflakeIdentifier.ofDatabase(namespace.level(NAMESPACE_DB_LEVEL - 1));
+        break;
+      case NAMESPACE_SCHEMA_LEVEL:
+        identifier =
+            SnowflakeIdentifier.ofSchema(
+                namespace.level(NAMESPACE_DB_LEVEL - 1),
+                namespace.level(NAMESPACE_SCHEMA_LEVEL - 1));
+        break;
+      default:
+        throw new IllegalArgumentException(
+            String.format(
+                "Snowflake max namespace level is %d, got namespace '%s'",
+                MAX_NAMESPACE_DEPTH, namespace));
+    }
+    LOG.debug("getSnowflakeIdentifierForNamespace({}) -> {}", namespace, 
identifier);
+    return identifier;
+  }
+
+  /**
+   * Converts a TableIdentifier into a SnowflakeIdentifier of type TABLE; the 
identifier must have
+   * exactly the right namespace depth to represent a fully-qualified 
Snowflake table identifier.
+   */
+  public static SnowflakeIdentifier getSnowflakeIdentifierForTableIdentifier(
+      TableIdentifier identifier) {
+    SnowflakeIdentifier namespaceScope = 
getSnowflakeIdentifierForNamespace(identifier.namespace());
+    Preconditions.checkArgument(
+        namespaceScope.getType() == SnowflakeIdentifier.Type.SCHEMA,
+        "Namespace portion of '%s' must be at the SCHEMA level, got 
namespaceScope '%s'",
+        identifier,
+        namespaceScope);
+    SnowflakeIdentifier ret =
+        SnowflakeIdentifier.ofTable(
+            namespaceScope.getDatabaseName(), namespaceScope.getSchemaName(), 
identifier.name());
+    LOG.debug("getSnowflakeIdentifierForTableIdentifier({}) -> {}", 
identifier, ret);
+    return ret;
+  }
+
+  private NamespaceHelpers() {}

Review Comment:
   nit: probably better to move this further up



##########
snowflake/src/test/java/org/apache/iceberg/snowflake/SnowflakeCatalogTest.java:
##########
@@ -0,0 +1,277 @@
+/*
+ * 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.snowflake;
+
+import java.io.IOException;
+import java.util.Map;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableMetadataParser;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.snowflake.entities.SnowflakeTableMetadata;
+import org.apache.iceberg.types.Types;
+import org.assertj.core.api.Assertions;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SnowflakeCatalogTest {
+
+  static final String TEST_CATALOG_NAME = "slushLog";
+  private SnowflakeCatalog catalog;
+  private FakeSnowflakeClient fakeClient;
+  private InMemoryFileIO fakeFileIO;
+  private Map<String, String> properties;
+
+  @Before
+  public void before() {
+    catalog = new SnowflakeCatalog();
+
+    fakeClient = new FakeSnowflakeClient();
+    fakeClient.addTable(
+        "DB_1",
+        "SCHEMA_1",
+        "TAB_1",
+        SnowflakeTableMetadata.parseJson(
+            
"{\"metadataLocation\":\"s3://tab1/metadata/v3.metadata.json\",\"status\":\"success\"}"));
+    fakeClient.addTable(
+        "DB_1",
+        "SCHEMA_1",
+        "TAB_2",
+        SnowflakeTableMetadata.parseJson(
+            
"{\"metadataLocation\":\"s3://tab2/metadata/v1.metadata.json\",\"status\":\"success\"}"));
+    fakeClient.addTable(
+        "DB_2",
+        "SCHEMA_2",
+        "TAB_3",
+        SnowflakeTableMetadata.parseJson(
+            
"{\"metadataLocation\":\"azure://myaccount.blob.core.windows.net/mycontainer/tab3/metadata/v334.metadata.json\",\"status\":\"success\"}"));
+    fakeClient.addTable(
+        "DB_2",
+        "SCHEMA_2",
+        "TAB_4",
+        SnowflakeTableMetadata.parseJson(
+            
"{\"metadataLocation\":\"azure://myaccount.blob.core.windows.net/mycontainer/tab4/metadata/v323.metadata.json\",\"status\":\"success\"}"));
+    fakeClient.addTable(
+        "DB_3",
+        "SCHEMA_3",
+        "TAB_5",
+        SnowflakeTableMetadata.parseJson(
+            
"{\"metadataLocation\":\"gcs://tab5/metadata/v793.metadata.json\",\"status\":\"success\"}"));
+    fakeClient.addTable(
+        "DB_3",
+        "SCHEMA_4",
+        "TAB_6",
+        SnowflakeTableMetadata.parseJson(
+            
"{\"metadataLocation\":\"gcs://tab6/metadata/v123.metadata.json\",\"status\":\"success\"}"));
+
+    fakeFileIO = new InMemoryFileIO();
+
+    Schema schema =
+        new Schema(
+            Types.NestedField.required(1, "x", Types.StringType.get(), 
"comment1"),
+            Types.NestedField.required(2, "y", Types.StringType.get(), 
"comment2"));
+    PartitionSpec partitionSpec =
+        
PartitionSpec.builderFor(schema).identity("x").withSpecId(1000).build();
+    fakeFileIO.addFile(
+        "s3://tab1/metadata/v3.metadata.json",
+        TableMetadataParser.toJson(
+                TableMetadata.newTableMetadata(
+                    schema, partitionSpec, "s3://tab1/", ImmutableMap.<String, 
String>of()))
+            .getBytes());
+    fakeFileIO.addFile(
+        
"wasbs://mycontai...@myaccount.blob.core.windows.net/tab3/metadata/v334.metadata.json",
+        TableMetadataParser.toJson(
+                TableMetadata.newTableMetadata(
+                    schema,
+                    partitionSpec,
+                    
"wasbs://mycontai...@myaccount.blob.core.windows.net/tab1/",
+                    ImmutableMap.<String, String>of()))
+            .getBytes());
+    fakeFileIO.addFile(
+        "gs://tab5/metadata/v793.metadata.json",
+        TableMetadataParser.toJson(
+                TableMetadata.newTableMetadata(
+                    schema, partitionSpec, "gs://tab5/", ImmutableMap.<String, 
String>of()))
+            .getBytes());
+
+    properties = Maps.newHashMap();
+    catalog.initialize(TEST_CATALOG_NAME, fakeClient, fakeFileIO, properties);
+  }
+
+  @Test
+  public void testInitializeNullClient() {
+    Assertions.assertThatExceptionOfType(IllegalArgumentException.class)
+        .isThrownBy(() -> catalog.initialize(TEST_CATALOG_NAME, null, 
fakeFileIO, properties))
+        .withMessageContaining("snowflakeClient must be non-null");
+  }
+
+  @Test
+  public void testInitializeNullFileIO() {
+    Assertions.assertThatExceptionOfType(IllegalArgumentException.class)
+        .isThrownBy(() -> catalog.initialize(TEST_CATALOG_NAME, fakeClient, 
null, properties))
+        .withMessageContaining("fileIO must be non-null");
+  }
+
+  @Test
+  public void testListNamespace() {
+    Assertions.assertThat(catalog.listNamespaces())
+        .containsExactly(
+            Namespace.of("DB_1", "SCHEMA_1"),
+            Namespace.of("DB_2", "SCHEMA_2"),
+            Namespace.of("DB_3", "SCHEMA_3"),
+            Namespace.of("DB_3", "SCHEMA_4"));
+  }
+
+  @Test
+  public void testListNamespaceWithinDB() {
+    String dbName = "DB_1";
+    Assertions.assertThat(catalog.listNamespaces(Namespace.of(dbName)))
+        .containsExactly(Namespace.of(dbName, "SCHEMA_1"));
+  }
+
+  @Test
+  public void testListNamespaceWithinNonExistentDB() {
+    // Existence check for nonexistent parent namespaces is optional in the 
SupportsNamespaces
+    // interface.
+    String dbName = "NONEXISTENT_DB";
+    Assertions.assertThatExceptionOfType(RuntimeException.class)
+        .isThrownBy(() -> catalog.listNamespaces(Namespace.of(dbName)))
+        .withMessageContaining("does not exist")
+        .withMessageContaining(dbName);
+  }
+
+  @Test
+  public void testListNamespaceWithinSchema() {
+    // No "sub-namespaces" beyond database.schema; invalid to try to list 
namespaces given
+    // a database.schema.
+    String dbName = "DB_3";
+    String schemaName = "SCHEMA_4";
+    Assertions.assertThatExceptionOfType(IllegalArgumentException.class)
+        .isThrownBy(() -> catalog.listNamespaces(Namespace.of(dbName, 
schemaName)))
+        .withMessageContaining("level")
+        .withMessageContaining("DB_3.SCHEMA_4");
+  }
+
+  @Test
+  public void testListTables() {
+    Assertions.assertThat(catalog.listTables(Namespace.empty()))
+        .containsExactly(
+            TableIdentifier.of("DB_1", "SCHEMA_1", "TAB_1"),
+            TableIdentifier.of("DB_1", "SCHEMA_1", "TAB_2"),
+            TableIdentifier.of("DB_2", "SCHEMA_2", "TAB_3"),
+            TableIdentifier.of("DB_2", "SCHEMA_2", "TAB_4"),
+            TableIdentifier.of("DB_3", "SCHEMA_3", "TAB_5"),
+            TableIdentifier.of("DB_3", "SCHEMA_4", "TAB_6"));
+  }
+
+  @Test
+  public void testListTablesWithinDB() {
+    String dbName = "DB_1";
+    Assertions.assertThat(catalog.listTables(Namespace.of(dbName)))
+        .containsExactly(
+            TableIdentifier.of("DB_1", "SCHEMA_1", "TAB_1"),
+            TableIdentifier.of("DB_1", "SCHEMA_1", "TAB_2"));
+  }
+
+  @Test
+  public void testListTablesWithinNonexistentDB() {
+    String dbName = "NONEXISTENT_DB";
+    Assertions.assertThatExceptionOfType(RuntimeException.class)
+        .isThrownBy(() -> catalog.listTables(Namespace.of(dbName)))
+        .withMessageContaining("does not exist")
+        .withMessageContaining(dbName);
+  }
+
+  @Test
+  public void testListTablesWithinSchema() {
+    String dbName = "DB_2";
+    String schemaName = "SCHEMA_2";
+    Assertions.assertThat(catalog.listTables(Namespace.of(dbName, schemaName)))
+        .containsExactly(
+            TableIdentifier.of("DB_2", "SCHEMA_2", "TAB_3"),
+            TableIdentifier.of("DB_2", "SCHEMA_2", "TAB_4"));
+  }
+
+  @Test
+  public void testListTablesWithinNonexistentSchema() {
+    String dbName = "DB_2";
+    String schemaName = "NONEXISTENT_SCHEMA";
+    Assertions.assertThatExceptionOfType(RuntimeException.class)
+        .isThrownBy(() -> catalog.listTables(Namespace.of(dbName, schemaName)))
+        .withMessageContaining("does not exist")
+        .withMessageContaining("DB_2.NONEXISTENT_SCHEMA");
+  }
+
+  @Test
+  public void testLoadS3Table() {
+    Table table = catalog.loadTable(TableIdentifier.of(Namespace.of("DB_1", 
"SCHEMA_1"), "TAB_1"));
+    Assertions.assertThat(table.location()).isEqualTo("s3://tab1/");
+  }
+
+  @Test
+  public void testLoadAzureTable() {
+    Table table = catalog.loadTable(TableIdentifier.of(Namespace.of("DB_2", 
"SCHEMA_2"), "TAB_3"));
+    Assertions.assertThat(table.location())
+        
.isEqualTo("wasbs://mycontai...@myaccount.blob.core.windows.net/tab1/");
+  }
+
+  @Test
+  public void testLoadGcsTable() {
+    Table table = catalog.loadTable(TableIdentifier.of(Namespace.of("DB_3", 
"SCHEMA_3"), "TAB_5"));
+    Assertions.assertThat(table.location()).isEqualTo("gs://tab5/");
+  }
+
+  @Test
+  public void testLoadTableWithMalformedTableIdentifier() {
+    Assertions.assertThatExceptionOfType(IllegalArgumentException.class)
+        .isThrownBy(
+            () ->
+                catalog.loadTable(
+                    TableIdentifier.of(Namespace.of("DB_1", "SCHEMA_1", 
"BAD_NS_LEVEL"), "TAB_1")))
+        .withMessageContaining("level")
+        .withMessageContaining("DB_1.SCHEMA_1.BAD_NS_LEVEL");
+    Assertions.assertThatExceptionOfType(IllegalArgumentException.class)
+        .isThrownBy(
+            () -> 
catalog.loadTable(TableIdentifier.of(Namespace.of("DB_WITHOUT_SCHEMA"), 
"TAB_1")))
+        .withMessageContaining("level")
+        .withMessageContaining("DB_WITHOUT_SCHEMA.TAB_1");
+  }
+
+  @Test
+  public void testCloseBeforeInitialize() throws IOException {
+    catalog = new SnowflakeCatalog();
+    catalog.close();

Review Comment:
   I think it would still be good to add some assertion here as this looks 
weird without any assertion



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to