gemini-code-assist[bot] commented on code in PR #38952:
URL: https://github.com/apache/beam/pull/38952#discussion_r3406142640
##########
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/catalog/InMemoryCatalog.java:
##########
@@ -111,13 +110,20 @@ public Collection<String> databases() {
@Override
public boolean dropDatabase(String database, boolean cascade) {
- checkState(!cascade, "%s does not support CASCADE.",
getClass().getSimpleName());
+ MetaStore metaStore = metaStores.get(database);
+ if (!cascade && metaStore != null && !metaStore.getTables().isEmpty()) {
+ throw new IllegalStateException("Database '" + database + "' is not
empty.");
+ }
boolean removed = databases.remove(database);
+ if (!removed) {
+ return false;
+ }
if (database.equals(currentDatabase)) {
currentDatabase = null;
}
- return removed;
+ metaStores.remove(database);
+ return true;
}
Review Comment:

To ensure robust defensive programming and prevent potential
`NullPointerException`s, we should add a null check for the `database`
parameter at the beginning of the `dropDatabase` method. If `database` is null,
calling `metaStores.get(database)` or `database.equals(currentDatabase)` will
throw an exception (especially if `metaStores` is backed by a
`ConcurrentHashMap` which does not permit null keys).
```java
public boolean dropDatabase(String database, boolean cascade) {
if (database == null) {
return false;
}
MetaStore metaStore = metaStores.get(database);
if (!cascade && metaStore != null && !metaStore.getTables().isEmpty()) {
throw new IllegalStateException("Database '" + database + "' is not
empty.");
}
boolean removed = databases.remove(database);
if (!removed) {
return false;
}
if (database.equals(currentDatabase)) {
currentDatabase = null;
}
metaStores.remove(database);
return true;
}
```
--
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]