jnh5y commented on code in PR #23612: URL: https://github.com/apache/flink/pull/23612#discussion_r1430167319
########## flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/ShowDatabasesOperation.java: ########## @@ -20,26 +20,108 @@ import org.apache.flink.annotation.Internal; import org.apache.flink.table.api.internal.TableResultInternal; +import org.apache.flink.table.functions.SqlLikeUtils; +import java.util.List; +import java.util.stream.Collectors; + +import static java.util.Objects.requireNonNull; import static org.apache.flink.table.api.internal.TableResultUtils.buildStringArrayResult; /** Operation to describe a SHOW DATABASES statement. */ @Internal public class ShowDatabasesOperation implements ShowOperation { + private final String preposition; + private final String catalogName; + private final LikeType likeType; + private final String likePattern; + private final boolean notLike; + + public ShowDatabasesOperation() { + // "SHOW DATABASES" command with all options being default + this.preposition = null; + this.catalogName = null; + this.likeType = null; + this.likePattern = null; + this.notLike = false; + } + + public ShowDatabasesOperation(String likeType, String likePattern, boolean notLike) { + this.preposition = null; + this.catalogName = null; + if (likeType != null) { + this.likeType = LikeType.of(likeType); + this.likePattern = requireNonNull(likePattern, "Like pattern must not be null"); + this.notLike = notLike; + } else { + this.likeType = null; + this.likePattern = null; + this.notLike = false; + } + } + + public ShowDatabasesOperation( + String preposition, + String catalogName, + String likeType, + String likePattern, + boolean notLike) { + this.preposition = preposition; + this.catalogName = catalogName; + if (likeType != null) { + this.likeType = LikeType.of(likeType); + this.likePattern = requireNonNull(likePattern, "Like pattern must not be null"); + this.notLike = notLike; + } else { + this.likeType = null; + this.likePattern = null; + this.notLike = false; + } + } + @Override public String asSummaryString() { - return "SHOW DATABASES"; + StringBuilder builder = new StringBuilder(); + builder.append("SHOW DATABASES"); + if (preposition != null) { + builder.append(String.format(" %s %s", preposition, catalogName)); + } + if (likeType != null) { + if (notLike) { + builder.append(String.format(" NOT %s '%s'", likeType.name(), likePattern)); + } else { + builder.append(String.format(" %s '%s'", likeType.name(), likePattern)); + } + } + return builder.toString(); } @Override public TableResultInternal execute(Context ctx) { - String[] databases = - ctx.getCatalogManager() - .getCatalogOrThrowException(ctx.getCatalogManager().getCurrentCatalog()) - .listDatabases().stream() - .sorted() - .toArray(String[]::new); - return buildStringArrayResult("database name", databases); + String cName = + catalogName == null ? ctx.getCatalogManager().getCurrentCatalog() : catalogName; + List<String> databases = + ctx.getCatalogManager().getCatalogOrThrowException(cName).listDatabases(); + + if (likeType != null) { + databases = + databases.stream() + .filter( + row -> { + if (likeType == LikeType.ILIKE) { + return notLike + != SqlLikeUtils.ilike(row, likePattern, "\\"); + } else if (likeType == LikeType.LIKE) { + return notLike + != SqlLikeUtils.like(row, likePattern, "\\"); + } + return false; + }) + .collect(Collectors.toList()); + } + + return buildStringArrayResult( + "database name", databases.stream().sorted().toArray(String[]::new)); Review Comment: Thanks for improving this! I'll leave it up to @dawidwys if he has any other ideas here. I'm tempted to suggest something that doesn't collect the intermediate list. (On one hand, this code isn't in an inner loop, so optimization may not be strictly necessary. On another hand, it is useful to think through minimizing intermediate work!) -- 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...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org