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


##########
snowflake/src/test/java/org/apache/iceberg/snowflake/JdbcSnowflakeClientTest.java:
##########
@@ -79,46 +79,24 @@ public void testNullClientPoolInConstructor() {
   @Test
   public void testDatabaseExists() throws SQLException {
     when(mockResultSet.next()).thenReturn(true).thenReturn(false);
-    when(mockResultSet.getString("name")).thenReturn("DB_1");
+    when(mockResultSet.getString("database_name")).thenReturn("DB_1");
+    when(mockResultSet.getString("name")).thenReturn("SCHEMA_1");
 
     
Assertions.assertThat(snowflakeClient.databaseExists(SnowflakeIdentifier.ofDatabase("DB_1")))
         .isTrue();
 
     verify(mockQueryHarness)
         .query(
             eq(mockConnection),
-            eq("SHOW DATABASES LIKE 'DB_1' IN ACCOUNT"),
-            any(JdbcSnowflakeClient.ResultSetParser.class));
-  }
-
-  @Test
-  public void testDatabaseExistsSpecialCharacters() throws SQLException {
-    when(mockResultSet.next()).thenReturn(true).thenReturn(false);
-    when(mockResultSet.getString("name")).thenReturn("$DB_1$.'!@#%^&*");
-
-    Assertions.assertThat(
-            
snowflakeClient.databaseExists(SnowflakeIdentifier.ofDatabase("$DB_1$.'!@#%^&*")))
-        .isTrue();
-
-    verify(mockQueryHarness)
-        .query(
-            eq(mockConnection),
-            eq("SHOW DATABASES LIKE '_DB_1$_________' IN ACCOUNT"),
-            any(JdbcSnowflakeClient.ResultSetParser.class));
-  }
-
-  @Test
-  public void testDatabaseDoesntExistNoResults() throws SQLException {
-    when(mockResultSet.next()).thenReturn(false);
-
-    
Assertions.assertThat(snowflakeClient.databaseExists(SnowflakeIdentifier.ofDatabase("DB_1")))
-        .isFalse();
+            eq("SHOW SCHEMAS IN DATABASE IDENTIFIER(?) LIMIT 1"),
+            any(JdbcSnowflakeClient.ResultSetParser.class),
+            eq("DB_1"));
   }
 
   @Test
-  public void testDatabaseDoesntExistMismatchedResults() throws SQLException {
-    when(mockResultSet.next()).thenReturn(true).thenReturn(false);
-    when(mockResultSet.getString("name")).thenReturn("DBZ1");
+  public void testDatabaseDoesntExist() throws SQLException {
+    when(mockResultSet.next())
+        .thenThrow(new SQLException("Database does not exists", "2000", 2003, 
null));

Review Comment:
   ```suggestion
           .thenThrow(new SQLException("Database does not exist", "2000", 2003, 
null));
   ```



##########
snowflake/src/main/java/org/apache/iceberg/snowflake/JdbcSnowflakeClient.java:
##########
@@ -203,31 +176,26 @@ public boolean schemaExists(SnowflakeIdentifier schema) {
       return false;
     }
 
-    // Due to current limitations in PreparedStatement parameters for the LIKE 
clause in
-    // SHOW SCHEMAS queries, we'll use a fairly limited allowlist for 
identifier characters,
-    // using wildcards for non-allowed characters, and post-filter for 
matching.
-    final String finalQuery =
-        String.format(
-            "SHOW SCHEMAS LIKE '%s' IN DATABASE IDENTIFIER(?)",
-            sanitizeIdentifierWithWildcardForLikeClause(schema.schemaName()));
+    final String finalQuery = "SHOW TABLES IN SCHEMA IDENTIFIER(?) LIMIT 1";
+
     List<SnowflakeIdentifier> schemas;
     try {
       schemas =
           connectionPool.run(
               conn ->
                   queryHarness.query(
-                      conn, finalQuery, SCHEMA_RESULT_SET_HANDLER, 
schema.databaseName()));
+                      conn, finalQuery, TABLE_RESULT_SET_HANDLER, 
schema.toIdentifierString()));
     } catch (SQLException e) {
+      if (e.getErrorCode() == 2003 && e.getMessage().contains("does not 
exist")) {
+        return false;
+      }
       throw new UncheckedSQLException(e, "Failed to check if schema '%s' 
exists", schema);
     } catch (InterruptedException e) {
       throw new UncheckedInterruptedException(
           e, "Interrupted while checking if schema '%s' exists", schema);
     }
 
-    // Filter to handle the edge case of '_' appearing as a wildcard that 
can't be remapped the way
-    // it can for predicates in SELECT statements.
-    schemas.removeIf(sc -> 
!schema.schemaName().equalsIgnoreCase(sc.schemaName()));
-    return !schemas.isEmpty();
+    return true;

Review Comment:
   should this maybe return `!schemas.isEmpty()`?



##########
snowflake/src/test/java/org/apache/iceberg/snowflake/JdbcSnowflakeClientTest.java:
##########
@@ -421,7 +357,7 @@ public void testListIcebergTablesInSchema() throws 
SQLException {
             eq(mockConnection),
             eq("SHOW ICEBERG TABLES IN SCHEMA IDENTIFIER(?)"),
             any(JdbcSnowflakeClient.ResultSetParser.class),
-            eq("DB_1.SCHEMA_1"));
+            eq("" + "DB_1.SCHEMA_1"));

Review Comment:
   ```suggestion
               eq("DB_1.SCHEMA_1"));
   ```



##########
snowflake/src/test/java/org/apache/iceberg/snowflake/JdbcSnowflakeClientTest.java:
##########
@@ -79,46 +79,24 @@ public void testNullClientPoolInConstructor() {
   @Test
   public void testDatabaseExists() throws SQLException {
     when(mockResultSet.next()).thenReturn(true).thenReturn(false);
-    when(mockResultSet.getString("name")).thenReturn("DB_1");
+    when(mockResultSet.getString("database_name")).thenReturn("DB_1");
+    when(mockResultSet.getString("name")).thenReturn("SCHEMA_1");
 
     
Assertions.assertThat(snowflakeClient.databaseExists(SnowflakeIdentifier.ofDatabase("DB_1")))
         .isTrue();
 
     verify(mockQueryHarness)
         .query(
             eq(mockConnection),
-            eq("SHOW DATABASES LIKE 'DB_1' IN ACCOUNT"),
-            any(JdbcSnowflakeClient.ResultSetParser.class));
-  }
-
-  @Test
-  public void testDatabaseExistsSpecialCharacters() throws SQLException {

Review Comment:
   why are we removing tests with special characters?



##########
snowflake/src/test/java/org/apache/iceberg/snowflake/JdbcSnowflakeClientTest.java:
##########
@@ -131,87 +109,45 @@ public void testSchemaExists() throws SQLException {
         .thenReturn(false)
         .thenReturn(true)
         .thenReturn(false);
-    
when(mockResultSet.getString("name")).thenReturn("DB_1").thenReturn("SCHEMA_1");
-    when(mockResultSet.getString("database_name")).thenReturn("DB_1");
+    
when(mockResultSet.getString("name")).thenReturn("DB1").thenReturn("SCHEMA1");
+    when(mockResultSet.getString("database_name")).thenReturn("DB1");
+    when(mockResultSet.getString("schema_name")).thenReturn("SCHEMA1");
 
     Assertions.assertThat(
-            snowflakeClient.schemaExists(SnowflakeIdentifier.ofSchema("DB_1", 
"SCHEMA_1")))
+            snowflakeClient.schemaExists(SnowflakeIdentifier.ofSchema("DB1", 
"SCHEMA1")))
         .isTrue();
 
     verify(mockQueryHarness)
         .query(
             eq(mockConnection),
-            eq("SHOW DATABASES LIKE 'DB_1' IN ACCOUNT"),
-            any(JdbcSnowflakeClient.ResultSetParser.class));
-    verify(mockQueryHarness)
-        .query(
-            eq(mockConnection),
-            eq("SHOW SCHEMAS LIKE 'SCHEMA_1' IN DATABASE IDENTIFIER(?)"),
+            eq("SHOW SCHEMAS IN DATABASE IDENTIFIER(?) LIMIT 1"),
             any(JdbcSnowflakeClient.ResultSetParser.class),
-            eq("DB_1"));
-  }
-
-  @Test
-  public void testSchemaExistsSpecialCharacters() throws SQLException {
-    when(mockResultSet.next())
-        .thenReturn(true)
-        .thenReturn(false)
-        .thenReturn(true)
-        .thenReturn(false);
-    
when(mockResultSet.getString("name")).thenReturn("DB_1").thenReturn("$SCHEMA_1$.'!@#%^&*");
-    when(mockResultSet.getString("database_name")).thenReturn("DB_1");
-
-    Assertions.assertThat(
-            snowflakeClient.schemaExists(
-                SnowflakeIdentifier.ofSchema("DB_1", "$SCHEMA_1$.'!@#%^&*")))
-        .isTrue();
-
+            eq("DB1"));
     verify(mockQueryHarness)
         .query(
             eq(mockConnection),
-            eq("SHOW DATABASES LIKE 'DB_1' IN ACCOUNT"),
-            any(JdbcSnowflakeClient.ResultSetParser.class));
-    verify(mockQueryHarness)
-        .query(
-            eq(mockConnection),
-            eq("SHOW SCHEMAS LIKE '_SCHEMA_1$_________' IN DATABASE 
IDENTIFIER(?)"),
+            eq("SHOW TABLES IN SCHEMA IDENTIFIER(?) LIMIT 1"),
             any(JdbcSnowflakeClient.ResultSetParser.class),
-            eq("DB_1"));
-  }
-
-  @Test
-  public void testSchemaDoesntExistMismatchDatabase() throws SQLException {
-    when(mockResultSet.next()).thenReturn(true).thenReturn(false);
-    when(mockResultSet.getString("name")).thenReturn("DBZ1");
-
-    Assertions.assertThat(
-            snowflakeClient.schemaExists(SnowflakeIdentifier.ofSchema("DB_1", 
"SCHEMA_1")))
-        .isFalse();
+            eq("DB1.SCHEMA1"));
   }
 
   @Test
-  public void testSchemaDoesntExistNoSchemaFound() throws SQLException {
-    
when(mockResultSet.next()).thenReturn(true).thenReturn(false).thenReturn(false);
-    when(mockResultSet.getString("name")).thenReturn("DB_1");
+  public void testSchemaDoesntExistNoSchemaFoundException() throws 
SQLException {
+    when(mockResultSet.next())
+        .thenThrow(new SQLException("Schema does not exists", "2000", 2003, 
null));

Review Comment:
   ```suggestion
           .thenThrow(new SQLException("Schema does not exist", "2000", 2003, 
null));
   ```



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