nastra commented on code in PR #7004: URL: https://github.com/apache/iceberg/pull/7004#discussion_r1130642011
########## snowflake/src/integration/java/org/apache/iceberg/snowflake/NamespaceTests.java: ########## @@ -0,0 +1,268 @@ +/* + * 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.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.exceptions.NoSuchNamespaceException; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class NamespaceTests extends SnowTestBase { + + @Test + public void testListNamespacesAtRootLevel() throws SQLException, InterruptedException { + List<Namespace> namespaces = snowflakeCatalog.listNamespaces(Namespace.empty()); + int dbCount = + clientPool.run( + conn -> { + int databaseCount = 0; + ResultSet rs = conn.createStatement().executeQuery("show databases"); + while (rs.next()) { + databaseCount++; + } + return databaseCount; + }); + Assertions.assertThat(namespaces.stream().count()).isEqualTo(dbCount); + } + + @Test + public void testListNamespacesAtDatabaseLevel() throws SQLException, InterruptedException { + String schema1 = "Schema_1"; + String schema2 = "Schema_2"; + String dbName = TestConfigurations.getInstance().getDatabase().toUpperCase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema1); + createOrReplaceSchema(schema2); + List<Namespace> namespaces = snowflakeCatalog.listNamespaces(Namespace.of(dbName)); + Assertions.assertThat(namespaces) + .containsExactlyInAnyOrderElementsOf( + Arrays.asList( + Namespace.of(dbName, schema1.toUpperCase()), + Namespace.of(dbName, schema2.toUpperCase()), + Namespace.of(dbName, "PUBLIC"), + Namespace.of(dbName, "INFORMATION_SCHEMA"))); + } finally { + dropSchemaIfExists(schema1); + dropSchemaIfExists(schema2); + } + } + + @Test + public void testListNamespacesAtSchemaLevelIsNotAllowed() + throws SQLException, InterruptedException { + String schema = "Schema_1"; + String dbName = TestConfigurations.getInstance().getDatabase().toUpperCase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatThrownBy( + () -> { Review Comment: nit: no need for the {} here and all the places below ########## snowflake/src/integration/java/org/apache/iceberg/snowflake/NamespaceTests.java: ########## @@ -0,0 +1,268 @@ +/* + * 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.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.exceptions.NoSuchNamespaceException; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class NamespaceTests extends SnowTestBase { + + @Test + public void testListNamespacesAtRootLevel() throws SQLException, InterruptedException { + List<Namespace> namespaces = snowflakeCatalog.listNamespaces(Namespace.empty()); + int dbCount = + clientPool.run( + conn -> { + int databaseCount = 0; + ResultSet rs = conn.createStatement().executeQuery("show databases"); + while (rs.next()) { + databaseCount++; + } + return databaseCount; + }); + Assertions.assertThat(namespaces.stream().count()).isEqualTo(dbCount); + } + + @Test + public void testListNamespacesAtDatabaseLevel() throws SQLException, InterruptedException { + String schema1 = "Schema_1"; + String schema2 = "Schema_2"; + String dbName = TestConfigurations.getInstance().getDatabase().toUpperCase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema1); + createOrReplaceSchema(schema2); + List<Namespace> namespaces = snowflakeCatalog.listNamespaces(Namespace.of(dbName)); + Assertions.assertThat(namespaces) + .containsExactlyInAnyOrderElementsOf( + Arrays.asList( + Namespace.of(dbName, schema1.toUpperCase()), + Namespace.of(dbName, schema2.toUpperCase()), + Namespace.of(dbName, "PUBLIC"), + Namespace.of(dbName, "INFORMATION_SCHEMA"))); + } finally { + dropSchemaIfExists(schema1); + dropSchemaIfExists(schema2); + } + } + + @Test + public void testListNamespacesAtSchemaLevelIsNotAllowed() + throws SQLException, InterruptedException { + String schema = "Schema_1"; + String dbName = TestConfigurations.getInstance().getDatabase().toUpperCase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.listNamespaces(Namespace.of(dbName, schema)); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("listNamespaces must be at either ROOT or DATABASE level"); + } finally { + dropSchemaIfExists(schema); + } + } + + @Test + public void testLoadNonExistingRootLevelNamespace() { + String nonExistingDb = "IDontExist"; + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(nonExistingDb)); + }) + .isInstanceOf(NoSuchNamespaceException.class); Review Comment: it is generally good pratice to assert for a specific error message, so would be good to have `.hasMessageContaining` here and all the other places below ########## snowflake/src/integration/java/org/apache/iceberg/snowflake/NamespaceTests.java: ########## @@ -0,0 +1,268 @@ +/* + * 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.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.exceptions.NoSuchNamespaceException; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class NamespaceTests extends SnowTestBase { + + @Test + public void testListNamespacesAtRootLevel() throws SQLException, InterruptedException { + List<Namespace> namespaces = snowflakeCatalog.listNamespaces(Namespace.empty()); + int dbCount = + clientPool.run( + conn -> { + int databaseCount = 0; + ResultSet rs = conn.createStatement().executeQuery("show databases"); + while (rs.next()) { + databaseCount++; + } + return databaseCount; + }); + Assertions.assertThat(namespaces.stream().count()).isEqualTo(dbCount); + } + + @Test + public void testListNamespacesAtDatabaseLevel() throws SQLException, InterruptedException { + String schema1 = "Schema_1"; + String schema2 = "Schema_2"; + String dbName = TestConfigurations.getInstance().getDatabase().toUpperCase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema1); + createOrReplaceSchema(schema2); + List<Namespace> namespaces = snowflakeCatalog.listNamespaces(Namespace.of(dbName)); + Assertions.assertThat(namespaces) + .containsExactlyInAnyOrderElementsOf( + Arrays.asList( + Namespace.of(dbName, schema1.toUpperCase()), + Namespace.of(dbName, schema2.toUpperCase()), + Namespace.of(dbName, "PUBLIC"), + Namespace.of(dbName, "INFORMATION_SCHEMA"))); + } finally { + dropSchemaIfExists(schema1); + dropSchemaIfExists(schema2); + } + } + + @Test + public void testListNamespacesAtSchemaLevelIsNotAllowed() + throws SQLException, InterruptedException { + String schema = "Schema_1"; + String dbName = TestConfigurations.getInstance().getDatabase().toUpperCase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.listNamespaces(Namespace.of(dbName, schema)); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("listNamespaces must be at either ROOT or DATABASE level"); + } finally { + dropSchemaIfExists(schema); + } + } + + @Test + public void testLoadNonExistingRootLevelNamespace() { + String nonExistingDb = "IDontExist"; + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(nonExistingDb)); + }) + .isInstanceOf(NoSuchNamespaceException.class); + } + + @Test + public void testLoadNonExistingDBLevelNamespace() { + String nonExistingSchema = "IDontExist"; + String db = TestConfigurations.getInstance().getDatabase(); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(db, nonExistingSchema)); + }) + .isInstanceOf(NoSuchNamespaceException.class); + } + + @Test + public void testLoadNamespaceThatExceedMaxSupportedHierarchy() { + String nonExistingSchema = "IDontExist"; + String db = TestConfigurations.getInstance().getDatabase(); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata( + Namespace.of(db, nonExistingSchema, "UnSupportedLevel")); + }) + .isInstanceOf(IllegalArgumentException.class); + } + + @ParameterizedTest + @ValueSource(strings = {"Schema", "schema", "_schema", "Schema123", "schema$", "_Schema$_123"}) Review Comment: looks much better with this now :100: ########## snowflake/src/integration/java/org/apache/iceberg/snowflake/NamespaceTests.java: ########## @@ -0,0 +1,268 @@ +/* + * 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.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.exceptions.NoSuchNamespaceException; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class NamespaceTests extends SnowTestBase { + + @Test + public void testListNamespacesAtRootLevel() throws SQLException, InterruptedException { + List<Namespace> namespaces = snowflakeCatalog.listNamespaces(Namespace.empty()); + int dbCount = + clientPool.run( + conn -> { + int databaseCount = 0; + ResultSet rs = conn.createStatement().executeQuery("show databases"); + while (rs.next()) { + databaseCount++; + } + return databaseCount; + }); + Assertions.assertThat(namespaces.stream().count()).isEqualTo(dbCount); + } + + @Test + public void testListNamespacesAtDatabaseLevel() throws SQLException, InterruptedException { + String schema1 = "Schema_1"; + String schema2 = "Schema_2"; + String dbName = TestConfigurations.getInstance().getDatabase().toUpperCase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema1); + createOrReplaceSchema(schema2); + List<Namespace> namespaces = snowflakeCatalog.listNamespaces(Namespace.of(dbName)); + Assertions.assertThat(namespaces) + .containsExactlyInAnyOrderElementsOf( + Arrays.asList( + Namespace.of(dbName, schema1.toUpperCase()), + Namespace.of(dbName, schema2.toUpperCase()), + Namespace.of(dbName, "PUBLIC"), + Namespace.of(dbName, "INFORMATION_SCHEMA"))); + } finally { + dropSchemaIfExists(schema1); + dropSchemaIfExists(schema2); + } + } + + @Test + public void testListNamespacesAtSchemaLevelIsNotAllowed() + throws SQLException, InterruptedException { + String schema = "Schema_1"; + String dbName = TestConfigurations.getInstance().getDatabase().toUpperCase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.listNamespaces(Namespace.of(dbName, schema)); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("listNamespaces must be at either ROOT or DATABASE level"); + } finally { + dropSchemaIfExists(schema); + } + } + + @Test + public void testLoadNonExistingRootLevelNamespace() { + String nonExistingDb = "IDontExist"; + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(nonExistingDb)); + }) + .isInstanceOf(NoSuchNamespaceException.class); + } + + @Test + public void testLoadNonExistingDBLevelNamespace() { + String nonExistingSchema = "IDontExist"; + String db = TestConfigurations.getInstance().getDatabase(); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(db, nonExistingSchema)); + }) + .isInstanceOf(NoSuchNamespaceException.class); + } + + @Test + public void testLoadNamespaceThatExceedMaxSupportedHierarchy() { + String nonExistingSchema = "IDontExist"; + String db = TestConfigurations.getInstance().getDatabase(); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata( + Namespace.of(db, nonExistingSchema, "UnSupportedLevel")); + }) + .isInstanceOf(IllegalArgumentException.class); + } + + @ParameterizedTest + @ValueSource(strings = {"Schema", "schema", "_schema", "Schema123", "schema$", "_Schema$_123"}) + public void testLoadNamespaceWithValidUnquotedIdentifier(String schema) + throws SQLException, InterruptedException { + String dbName = TestConfigurations.getInstance().getDatabase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatNoException() + .isThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(dbName, schema)); + }); + } finally { + dropSchemaIfExists(schema); + } + } + + @Test + public void testLoadNamespaceWithUnquotedIdentifierIsCaseInsensitive() + throws SQLException, InterruptedException { + String schema = "Schema123"; + String dbName = TestConfigurations.getInstance().getDatabase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatNoException() + .isThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(dbName, schema)); + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(dbName, schema.toUpperCase())); + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(dbName, "schEmA123")); + }); + } finally { + dropSchemaIfExists(schema); + } + } + + @Test + public void testLoadNamespaceWithQuotedIdentifierIsCaseSensitive() + throws SQLException, InterruptedException { + String schema = "\"Schema123\""; + String dbName = TestConfigurations.getInstance().getDatabase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(dbName, schema.toUpperCase())); + }) + .isInstanceOf(NoSuchNamespaceException.class) + .hasMessageContainingAll("snowflake identifier", "doesn't exist"); + } finally { + dropSchemaIfExists(schema); + } + } + + @ParameterizedTest + @ValueSource( + strings = { + "\"Schema123\"", + "\"MySchema\"", + "\"my.schema\"", + "\"my schema\"", + "\"My 'Schema'\"", + "\"3rd_schema\"", + "\"$Ischema\"", + "\"H@!!.0-w*r()^'\"", + "\"идентификатор\"" + }) + public void testLoadNamespaceWithQuotedIdentifier(String schema) + throws SQLException, InterruptedException { + String dbName = TestConfigurations.getInstance().getDatabase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatNoException() + .isThrownBy( + () -> { + snowflakeCatalog.loadNamespaceMetadata(Namespace.of(dbName, schema)); + }); + } finally { + dropSchemaIfExists(schema); + } + } + + @Test + public void testDropNamespaceNotSupported() throws SQLException, InterruptedException { + String schema = "Schema123"; + String dbName = TestConfigurations.getInstance().getDatabase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.dropNamespace(Namespace.of(dbName, schema)); + }) + .isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("SnowflakeCatalog does not currently support dropNamespace"); + } finally { + dropSchemaIfExists(schema); + } + } + + @Test + public void testSetPropertiesNotSupported() throws SQLException, InterruptedException { + String schema = "Schema123"; + String dbName = TestConfigurations.getInstance().getDatabase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.setProperties( + Namespace.of(dbName, schema), new HashMap<String, String>()); + }) + .isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("SnowflakeCatalog does not currently support setProperties"); + } finally { + dropSchemaIfExists(schema); + } + } + + @Test + public void testRemovePropertiesNotSupported() throws SQLException, InterruptedException { + String schema = "Schema123"; + String dbName = TestConfigurations.getInstance().getDatabase(); + try { + clientPool.run(conn -> conn.createStatement().execute("use " + dbName)); + createOrReplaceSchema(schema); + Assertions.assertThatThrownBy( + () -> { + snowflakeCatalog.removeProperties( + Namespace.of(dbName, schema), new HashSet<String>()); Review Comment: nit (I think checkstyle should even complain about this): `Sets.newHashSet()` or `ImmutableSet.of()` ########## snowflake/src/integration/java/org/apache/iceberg/snowflake/SnowTestBase.java: ########## @@ -0,0 +1,110 @@ +/* + * 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.sql.PreparedStatement; +import java.sql.SQLException; +import org.apache.iceberg.jdbc.JdbcClientPool; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.junit.BeforeClass; +import org.junit.jupiter.api.AfterAll; + +@SuppressWarnings("VisibilityModifier") +class SnowTestBase { + + static SnowflakeCatalog snowflakeCatalog; + + static JdbcClientPool clientPool; + + protected SnowTestBase() {} + + @BeforeClass + public static void beforeAll() { + snowflakeCatalog = new SnowflakeCatalog(); + TestConfigurations configs = TestConfigurations.getInstance(); Review Comment: I believe pretty much all other tests across the codebase just set things up in a base test class (like the one you have here). I think moving things from `TestConfigurations` to this class makes the code cleaner, but I'm curious what other reviewers think -- 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]
