{"record":{"id":"49c00bc10eb054ec","repo":"apache/iceberg","slug":"failed-to-check-if-database-s-exists","errorCode":null,"errorMessage":"Failed to check if database '%s' exists","messagePattern":"Failed to check if database '(.+?)' exists","errorType":"exception","errorClass":"UncheckedSQLException","httpStatus":null,"severity":"error","filePath":"snowflake/src/main/java/org/apache/iceberg/snowflake/JdbcSnowflakeClient.java","lineNumber":172,"sourceCode":"    Preconditions.checkArgument(\n        database.type() == SnowflakeIdentifier.Type.DATABASE,\n        \"databaseExists requires a DATABASE identifier, got '%s'\",\n        database);\n\n    final String finalQuery = \"SHOW SCHEMAS IN DATABASE IDENTIFIER(?) LIMIT 1\";\n\n    List<SnowflakeIdentifier> schemas;\n    try {\n      schemas =\n          connectionPool.run(\n              conn ->\n                  queryHarness.query(\n                      conn, finalQuery, SCHEMA_RESULT_SET_HANDLER, database.databaseName()));\n    } catch (SQLException e) {\n      if (DATABASE_NOT_FOUND_ERROR_CODES.contains(e.getErrorCode())) {\n        return false;\n      }\n      throw new UncheckedSQLException(e, \"Failed to check if database '%s' exists\", database);\n    } catch (InterruptedException e) {\n      throw new UncheckedInterruptedException(\n          e, \"Interrupted while checking if database '%s' exists\", database);\n    }\n\n    return !schemas.isEmpty();\n  }\n\n  @Override\n  public boolean schemaExists(SnowflakeIdentifier schema) {\n    Preconditions.checkArgument(\n        schema.type() == SnowflakeIdentifier.Type.SCHEMA,\n        \"schemaExists requires a SCHEMA identifier, got '%s'\",\n        schema);\n\n    if (!databaseExists(SnowflakeIdentifier.ofDatabase(schema.databaseName()))) {\n      return false;\n    }","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/snowflake/src/main/java/org/apache/iceberg/snowflake/JdbcSnowflakeClient.java#L154-L190","documentation":"JdbcSnowflakeClient.databaseExists wraps unexpected SQLExceptions from querying Snowflake's INFORMATION_SCHEMA/SHOW command into UncheckedSQLException. Snowflake 'database does not exist' error codes are handled gracefully (returns false); this error means the existence check failed for another reason — connection, permission, or SQL failure. It surfaces as an unchecked exception from the catalog's schemaExists path.","triggerScenarios":"Calling schemaExists (or databaseExists) on a SnowflakeCatalog backed by JdbcSnowflakeClient when the JDBC query to check the database throws a SQLException whose error code is not in DATABASE_NOT_FOUND_ERROR_CODES — e.g. broken connection, insufficient privileges, or driver error.","commonSituations":"Snowflake JDBC connection dropped or expired mid-query, the configured role lacks privileges to run SHOW DATABASES, malformed JDBC URL, or network interruption to the Snowflake account.","solutions":["Verify the Snowflake JDBC connection (URL, credentials, role) is valid and test connectivity with a simple query","Grant the connecting role privileges to list databases in the account (e.g. SHOW DATABASES permission)","Retry the operation in case of transient network/connection failures","Inspect the wrapped SQLException (getCause) for the underlying Snowflake error code"],"exampleFix":"// before\nboolean exists = snowflakeCatalog.schemaExists(SnowflakeIdentifier.ofDatabase(\"db\"));\n// after\ntry {\n  boolean exists = snowflakeCatalog.schemaExists(SnowflakeIdentifier.ofDatabase(\"db\"));\n} catch (UncheckedSQLException e) {\n  LOG.error(\"Database existence check failed: {}\", e.getCause().getMessage());\n  throw new RuntimeException(\"Snowflake connectivity/permission issue checking database\", e);\n}","handlingStrategy":"try-catch","validationCode":"// verify connectivity before catalog calls\ntry (Connection c = DriverManager.getConnection(jdbcUrl, user, pass)) {\n  c.createStatement().executeQuery(\"SELECT 1\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  boolean exists = client.databaseExists(SnowflakeIdentifier.ofDatabase(name));\n} catch (UncheckedSQLException e) {\n  LOG.error(\"Snowflake query failed checking database {}: {}\", name, e.getCause().getMessage());\n  throw new IcebergRuntimeException(\"Database existence check failed; check connectivity/permissions\", e);\n}","preventionTips":["Validate Snowflake JDBC credentials and URL before initializing the catalog","Grant the role SHOW DATABASES / USAGE privileges needed for account-level lookups","Use connection pools with validation queries to catch stale connections","Log the cause SQLException's error code to distinguish permission vs connectivity issues"],"tags":["snowflake","jdbc","sql"],"backgroundTag":"database-query-failed","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}