Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin committed Aug 17, 2024
1 parent 9c07fda commit 66cdd72
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ public boolean isNotLike() {
return notLike;
}

public abstract String getOperationName();
abstract String getOperationName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public SqlOperator getOperator() {
}

@Override
public String getOperationName() {
String getOperationName() {
return "SHOW COLUMNS";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public SqlOperator getOperator() {
}

@Override
public String getOperationName() {
String getOperationName() {
return "SHOW DATABASES";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public boolean requireUser() {
}

@Override
public String getOperationName() {
String getOperationName() {
return requireUser ? "SHOW USER FUNCTIONS" : "SHOW FUNCTIONS";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public SqlOperator getOperator() {
}

@Override
public String getOperationName() {
String getOperationName() {
return "SHOW MODELS";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public SqlOperator getOperator() {
}

@Override
public String getOperationName() {
String getOperationName() {
return "SHOW PROCEDURES";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public SqlOperator getOperator() {
}

@Override
public String getOperationName() {
String getOperationName() {
return "SHOW TABLES";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public SqlOperator getOperator() {
}

@Override
public String getOperationName() {
String getOperationName() {
return "SHOW VIEWS";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -960,11 +960,17 @@ public ObjectIdentifier qualifyIdentifier(UnresolvedIdentifier identifier) {

/** Qualifies catalog name. Throws {@link ValidationException} if not set. */
public String qualifyCatalog(@Nullable String catalogName) {
if (StringUtils.isNullOrWhitespaceOnly(catalogName)) {
return validateCatalog(getCurrentCatalog());
}
return validateCatalog(catalogName);
}

/** Qualifies database name. Throws {@link ValidationException} if not set. */
public String qualifyDatabase(@Nullable String databaseName) {
if (StringUtils.isNullOrWhitespaceOnly(databaseName)) {
return validateDatabase(getCurrentDatabase());
}
return validateDatabase(databaseName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,22 @@
import static org.apache.flink.table.api.internal.TableResultUtils.buildStringArrayResult;

@Internal
public abstract class AbstractOneColumnShowOperation implements ShowOperation {
private final @Nullable String catalogName;
private final @Nullable String preposition;
private final @Nullable LikeOp likeOp;
public abstract class AbstractShowOperation implements ShowOperation {
protected final @Nullable String catalogName;
protected final @Nullable String preposition;
protected final @Nullable LikeOp likeOp;

public AbstractOneColumnShowOperation(String catalogName, String preposition, LikeOp likeOp) {
public AbstractShowOperation(String catalogName, String preposition, LikeOp likeOp) {
this.catalogName = catalogName;
this.preposition = preposition;
this.likeOp = likeOp;
}

public abstract Collection<String> retrieveDataForTableResult(Context ctx);
abstract Collection<String> retrieveDataForTableResult(Context ctx);

public abstract String getOperationName();
abstract String getOperationName();

public abstract String getColumnName();

String getCatalogName() {
return catalogName;
}

String getPreposition() {
return preposition;
}

LikeOp getLikeOp() {
return likeOp;
}
abstract String getColumnName();

@Override
public TableResultInternal execute(Context ctx) {
Expand Down Expand Up @@ -109,7 +97,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractOneColumnShowOperation that = (AbstractOneColumnShowOperation) o;
AbstractShowOperation that = (AbstractShowOperation) o;
return Objects.equals(catalogName, that.catalogName)
&& Objects.equals(preposition, that.preposition)
&& Objects.equals(likeOp, that.likeOp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@

import static org.apache.flink.table.api.internal.TableResultUtils.buildTableResult;

/** Show columns from [[catalog.]database.]table. */
/**
* SHOW COLUMNS sql call. The full syntax for SHOW COLUMNS is as followings:
*
* <pre>{@code
* SHOW COLUMNS [ ( FROM | IN ) [catalog_name.]database_name.]table [ [NOT] (LIKE | ILIKE)
* <sql_like_pattern> ] statement
* }</pre>
*/
@Internal
public class ShowColumnsOperation extends AbstractOneColumnShowOperation {
public class ShowColumnsOperation extends AbstractShowOperation {

private final ObjectIdentifier tableIdentifier;

Expand All @@ -73,7 +80,6 @@ public TableResultInternal execute(Context ctx) {

ResolvedSchema schema = result.get().getResolvedSchema();
Object[][] rows = generateTableColumnsRows(schema);
LikeOp likeOp = getLikeOp();
if (likeOp != null) {
rows =
Arrays.stream(rows)
Expand Down Expand Up @@ -163,18 +169,18 @@ private DataType[] generateTableColumnsDataTypes(boolean nonComments) {
}

@Override
public String getOperationName() {
String getOperationName() {
return "SHOW COLUMNS";
}

@Override
public String getColumnName() {
String getColumnName() {
// Dummy implementation since the main logic is overridden in execute method
return null;
}

@Override
public Collection<String> retrieveDataForTableResult(Context ctx) {
Collection<String> retrieveDataForTableResult(Context ctx) {
// Dummy implementation since the main logic is overridden in execute method
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@
package org.apache.flink.table.operations;

import org.apache.flink.annotation.Internal;
import org.apache.flink.table.catalog.CatalogManager;
import org.apache.flink.table.operations.utils.LikeOp;

import java.util.Collection;

/** Operation to describe a SHOW DATABASES statement. */
/**
* SHOW DATABASES sql call. The full syntax for SHOW DATABASES is as followings:
*
* <pre>{@code
* SHOW DATABASES [ ( FROM | IN ) catalog_name] [ [NOT] (LIKE | ILIKE) <sql_like_pattern> ]
* }</pre>
*/
@Internal
public class ShowDatabasesOperation extends AbstractOneColumnShowOperation {
public class ShowDatabasesOperation extends AbstractShowOperation {

public ShowDatabasesOperation(String catalogName, String preposition, LikeOp likeOp) {
super(catalogName, preposition, likeOp);
Expand All @@ -40,21 +47,20 @@ public ShowDatabasesOperation() {
}

@Override
public Collection<String> retrieveDataForTableResult(Context ctx) {
String cName =
getCatalogName() == null
? ctx.getCatalogManager().getCurrentCatalog()
: getCatalogName();
return ctx.getCatalogManager().getCatalogOrThrowException(cName).listDatabases();
Collection<String> retrieveDataForTableResult(Context ctx) {
final CatalogManager catalogManager = ctx.getCatalogManager();
final String catalogName = catalogManager.qualifyCatalog(this.catalogName);
String cName = catalogName == null ? catalogManager.getCurrentCatalog() : catalogName;
return catalogManager.getCatalogOrThrowException(cName).listDatabases();
}

@Override
public String getOperationName() {
String getOperationName() {
return "SHOW DATABASES";
}

@Override
public String getColumnName() {
String getColumnName() {
return "database name";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
import java.util.stream.Collectors;

/**
* Operation to describe a SHOW FUNCTIONS [ ( FROM | IN ) [catalog_name.]database_name ] [ [NOT]
* (LIKE | ILIKE) &lt;sql_like_pattern&gt; ] statement.
* SHOW FUNCTIONS sql call. The full syntax for SHOW FUNCTIONS is as followings:
*
* <pre>{@code
* SHOW [USER] FUNCTIONS [ ( FROM | IN ) [catalog_name.]database_name ] [ [NOT] (LIKE | ILIKE)
* <sql_like_pattern> ] statement
* }</pre>
*/
@Internal
public class ShowFunctionsOperation extends AbstractOneColumnShowOperation {
public class ShowFunctionsOperation extends AbstractShowOperation {

/**
* Represent scope of function.
Expand Down Expand Up @@ -73,9 +77,7 @@ public ShowFunctionsOperation(
}

@Override
public Collection<String> retrieveDataForTableResult(Context ctx) {
final String preposition = getPreposition();
final String catalogName = getCatalogName();
Collection<String> retrieveDataForTableResult(Context ctx) {
switch (functionScope) {
case USER:
if (preposition == null) {
Expand All @@ -94,24 +96,20 @@ public Collection<String> retrieveDataForTableResult(Context ctx) {
default:
throw new UnsupportedOperationException(
String.format(
"SHOW FUNCTIONS with %s scope is not supported.", functionScope));
"SHOW FUNCTIONS with %s scope is not supported.", databaseName));
}
}

@Override
public String getOperationName() {
String getOperationName() {
return functionScope == FunctionScope.ALL ? "SHOW FUNCTIONS" : "SHOW USER FUNCTIONS";
}

@Override
public String getColumnName() {
String getColumnName() {
return "function name";
}

public FunctionScope getFunctionScope() {
return functionScope;
}

@Override
public String getPrepositionSummaryString() {
if (databaseName == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
import java.util.Set;

/**
* Operation to describe a SHOW PROCEDURES [ ( FROM | IN ) [catalog_name.]database_name ] [ [NOT]
* (LIKE | ILIKE) &lt;sql_like_pattern&gt; ] statement.
* SHOW PROCEDURES sql call. The full syntax for SHOW PROCEDURES is as followings:
*
* <pre>{@code
* SHOW PROCEDURES [ ( FROM | IN ) [catalog_name.]database_name ] [ [NOT] (LIKE | ILIKE)
* <sql_like_pattern> ] statement
* }</pre>
*/
@Internal
public class ShowProceduresOperation extends AbstractOneColumnShowOperation {
public class ShowProceduresOperation extends AbstractShowOperation {

private final @Nullable String databaseName;

Expand All @@ -50,10 +54,10 @@ public ShowProceduresOperation(LikeOp likeOp) {
}

@Override
public Set<String> retrieveDataForTableResult(Context ctx) {
final String preposition = getPreposition();
final String catalogName = getCatalogName();
Set<String> retrieveDataForTableResult(Context ctx) {
final CatalogManager catalogManager = ctx.getCatalogManager();
final String catalogName = catalogManager.qualifyCatalog(this.catalogName);
final String dbName = catalogManager.qualifyDatabase(this.databaseName);
try {
if (preposition == null) {
// it's to show current_catalog.current_database
Expand All @@ -63,28 +67,26 @@ public Set<String> retrieveDataForTableResult(Context ctx) {
.listProcedures(catalogManager.getCurrentDatabase()));
} else {
Catalog catalog = catalogManager.getCatalogOrThrowException(catalogName);
return new HashSet<>(catalog.listProcedures(databaseName));
return new HashSet<>(catalog.listProcedures(dbName));
}
} catch (DatabaseNotExistException e) {
throw new TableException(
String.format(
"Fail to show procedures because the Database `%s` to show from/in does not exist in Catalog `%s`.",
preposition == null
? catalogManager.getCurrentDatabase()
: databaseName,
preposition == null ? catalogManager.getCurrentDatabase() : dbName,
preposition == null
? catalogManager.getCurrentCatalog()
: catalogName));
}
}

@Override
public String getOperationName() {
String getOperationName() {
return "SHOW PROCEDURES";
}

@Override
public String getColumnName() {
String getColumnName() {
return "procedure name";
}

Expand Down
Loading

0 comments on commit 66cdd72

Please sign in to comment.