Skip to content

Commit

Permalink
[FLINK-36126] Qualification of current catalog and current database s…
Browse files Browse the repository at this point in the history
…hould happen during execution, not parsing
  • Loading branch information
snuyanzin committed Aug 21, 2024
1 parent 9399e66 commit 2d7a7c0
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Objects;

/** Abstract class for SHOW sql call. */
public abstract class SqlShowCall extends SqlCall {
private final String preposition;
private final SqlIdentifier sqlIdentifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
*/
@Internal
public abstract class AbstractShowOperation implements ShowOperation {
protected final String catalogName;
protected final @Nullable String preposition;
protected final @Nullable ShowLikeOperator likeOp;
private final @Nullable String catalogName;
private final @Nullable String preposition;
private final @Nullable ShowLikeOperator likeOp;

public AbstractShowOperation(
String catalogName, @Nullable String preposition, @Nullable ShowLikeOperator likeOp) {
@Nullable String catalogName,
@Nullable String preposition,
@Nullable ShowLikeOperator likeOp) {
this.catalogName = catalogName;
this.preposition = preposition;
this.likeOp = likeOp;
Expand All @@ -55,6 +57,18 @@ public AbstractShowOperation(

protected abstract String getColumnName();

public String getCatalogName() {
return catalogName;
}

public ShowLikeOperator getLikeOp() {
return likeOp;
}

public String getPreposition() {
return preposition;
}

@Override
public TableResultInternal execute(Context ctx) {
final Collection<String> views = retrieveDataForTableResult(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public TableResultInternal execute(Context ctx) {

ResolvedSchema schema = result.get().getResolvedSchema();
Object[][] rows = generateTableColumnsRows(schema);
ShowLikeOperator likeOp = getLikeOp();
if (likeOp != null) {
rows =
Arrays.stream(rows)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@
public class ShowDatabasesOperation extends AbstractShowOperation {

public ShowDatabasesOperation(
String catalogName, @Nullable String preposition, @Nullable ShowLikeOperator likeOp) {
@Nullable String catalogName,
@Nullable String preposition,
@Nullable ShowLikeOperator likeOp) {
super(catalogName, preposition, likeOp);
}

public ShowDatabasesOperation(String catalogName, ShowLikeOperator likeOp) {
public ShowDatabasesOperation(@Nullable String catalogName, @Nullable ShowLikeOperator likeOp) {
this(catalogName, null, likeOp);
}

public ShowDatabasesOperation(String catalogName) {
public ShowDatabasesOperation(@Nullable String catalogName) {
this(catalogName, null, null);
}

@Override
protected Collection<String> retrieveDataForTableResult(Context ctx) {
final CatalogManager catalogManager = ctx.getCatalogManager();
return catalogManager.getCatalogOrThrowException(catalogName).listDatabases();
final String qualifiedCatalogName = catalogManager.qualifyCatalog(getCatalogName());
return catalogManager.getCatalogOrThrowException(qualifiedCatalogName).listDatabases();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public enum FunctionScope {
private final FunctionScope functionScope;
private final @Nullable String databaseName;

public ShowFunctionsOperation(String catalogName, String databaseName) {
public ShowFunctionsOperation(@Nullable String catalogName, @Nullable String databaseName) {
// "SHOW FUNCTIONS" default is ALL scope
this(FunctionScope.ALL, catalogName, databaseName, null);
}
Expand All @@ -73,8 +73,8 @@ public ShowFunctionsOperation(
public ShowFunctionsOperation(
FunctionScope functionScope,
@Nullable String preposition,
String catalogName,
String databaseName,
@Nullable String catalogName,
@Nullable String databaseName,
@Nullable ShowLikeOperator likeOp) {
super(catalogName, preposition, likeOp);
this.functionScope = functionScope;
Expand All @@ -83,6 +83,8 @@ public ShowFunctionsOperation(

@Override
protected Collection<String> retrieveDataForTableResult(Context ctx) {
final String preposition = getPreposition();
final String catalogName = getCatalogName();
switch (functionScope) {
case USER:
if (preposition == null) {
Expand All @@ -105,6 +107,14 @@ protected Collection<String> retrieveDataForTableResult(Context ctx) {
}
}

public String getDatabaseName() {
return databaseName;
}

public FunctionScope getFunctionScope() {
return functionScope;
}

@Override
protected String getOperationName() {
return functionScope == FunctionScope.ALL ? "SHOW FUNCTIONS" : "SHOW USER FUNCTIONS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,52 @@
@Internal
public class ShowProceduresOperation extends AbstractShowOperation {

private final String databaseName;
private final @Nullable String databaseName;

public ShowProceduresOperation(
String catalogName,
String databaseName,
@Nullable String catalogName,
@Nullable String databaseName,
@Nullable String preposition,
@Nullable ShowLikeOperator likeOp) {
super(catalogName, preposition, likeOp);
this.databaseName = databaseName;
}

public ShowProceduresOperation(
String catalogName, String databaseName, @Nullable ShowLikeOperator likeOp) {
@Nullable String catalogName,
@Nullable String databaseName,
@Nullable ShowLikeOperator likeOp) {
this(catalogName, databaseName, null, likeOp);
}

@Override
protected Collection<String> retrieveDataForTableResult(Context ctx) {
final CatalogManager catalogManager = ctx.getCatalogManager();
final String qualifiedCatalogName = catalogManager.qualifyCatalog(getCatalogName());
final String qualifiedDatabaseName = catalogManager.qualifyDatabase(databaseName);
try {
if (preposition == null) {
if (getPreposition() == null) {
// it's to show current_catalog.current_database
return catalogManager.getCatalogOrError(catalogName).listProcedures(databaseName);
return catalogManager
.getCatalogOrError(qualifiedCatalogName)
.listProcedures(qualifiedDatabaseName);
} else {
Catalog catalog = catalogManager.getCatalogOrThrowException(catalogName);
return catalog.listProcedures(databaseName);
Catalog catalog = catalogManager.getCatalogOrThrowException(qualifiedCatalogName);
return catalog.listProcedures(qualifiedDatabaseName);
}
} 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`.",
databaseName, catalogName));
qualifiedDatabaseName, qualifiedCatalogName),
e);
}
}

public String getDatabaseName() {
return databaseName;
}

@Override
protected String getOperationName() {
return "SHOW PROCEDURES";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,52 @@
@Internal
public class ShowTablesOperation extends AbstractShowOperation {

private final String databaseName;
private final @Nullable String databaseName;

public ShowTablesOperation(
String catalogName,
String databaseName,
@Nullable String catalogName,
@Nullable String databaseName,
@Nullable String preposition,
@Nullable ShowLikeOperator likeOp) {
super(catalogName, preposition, likeOp);
this.databaseName = databaseName;
}

public ShowTablesOperation(
String catalogName, String databaseName, @Nullable ShowLikeOperator likeOp) {
@Nullable String catalogName,
@Nullable String databaseName,
@Nullable ShowLikeOperator likeOp) {
this(catalogName, databaseName, null, likeOp);
}

public ShowTablesOperation(String catalogName, String databaseName) {
public ShowTablesOperation(@Nullable String catalogName, @Nullable String databaseName) {
this(catalogName, databaseName, null);
}

@Override
protected Set<String> retrieveDataForTableResult(Context ctx) {
final CatalogManager catalogManager = ctx.getCatalogManager();
if (preposition == null) {
final String qualifiedCatalogName = catalogManager.qualifyCatalog(getCatalogName());
final String qualifiedDatabaseName = catalogManager.qualifyDatabase(databaseName);
if (getPreposition() == null) {
return catalogManager.listTables();
} else {
Catalog catalog = catalogManager.getCatalogOrThrowException(catalogName);
if (catalog.databaseExists(databaseName)) {
return catalogManager.listTables(catalogName, databaseName);
Catalog catalog = catalogManager.getCatalogOrThrowException(qualifiedCatalogName);
if (catalog.databaseExists(qualifiedDatabaseName)) {
return catalogManager.listTables(qualifiedCatalogName, qualifiedDatabaseName);
} else {
throw new ValidationException(
String.format(
"Database '%s'.'%s' doesn't exist.", catalogName, databaseName));
"Database '%s'.'%s' doesn't exist.",
qualifiedCatalogName, qualifiedDatabaseName));
}
}
}

public String getDatabaseName() {
return databaseName;
}

@Override
protected String getOperationName() {
return "SHOW TABLES";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,25 @@
@Internal
public class ShowViewsOperation extends AbstractShowOperation {

private final String databaseName;
private final @Nullable String databaseName;

public ShowViewsOperation(
String catalogName,
String databaseName,
@Nullable String catalogName,
@Nullable String databaseName,
@Nullable String preposition,
@Nullable ShowLikeOperator likeOp) {
super(catalogName, preposition, likeOp);
this.databaseName = databaseName;
}

public ShowViewsOperation(
String catalogName, String databaseName, @Nullable ShowLikeOperator likeOp) {
@Nullable String catalogName,
@Nullable String databaseName,
@Nullable ShowLikeOperator likeOp) {
this(catalogName, databaseName, null, likeOp);
}

public ShowViewsOperation(String catalogName, String databaseName) {
public ShowViewsOperation(@Nullable String catalogName, @Nullable String databaseName) {
this(catalogName, databaseName, null);
}

Expand All @@ -66,20 +68,27 @@ protected String getOperationName() {

protected Set<String> retrieveDataForTableResult(Context ctx) {
final CatalogManager catalogManager = ctx.getCatalogManager();
if (preposition == null) {
final String qualifiedCatalogName = catalogManager.qualifyCatalog(getCatalogName());
final String qualifiedDatabaseName = catalogManager.qualifyDatabase(databaseName);
if (getPreposition() == null) {
return catalogManager.listViews();
} else {
Catalog catalog = catalogManager.getCatalogOrThrowException(catalogName);
if (catalog.databaseExists(databaseName)) {
return catalogManager.listViews(catalogName, databaseName);
Catalog catalog = catalogManager.getCatalogOrThrowException(qualifiedCatalogName);
if (catalog.databaseExists(qualifiedDatabaseName)) {
return catalogManager.listViews(qualifiedCatalogName, qualifiedDatabaseName);
} else {
throw new ValidationException(
String.format(
"Database '%s'.'%s' doesn't exist.", catalogName, databaseName));
"Database '%s'.'%s' doesn't exist.",
qualifiedCatalogName, qualifiedDatabaseName));
}
}
}

public String getDatabaseName() {
return databaseName;
}

@Override
protected String getColumnName() {
return "view name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.util.List;

/** An abstract class for SHOW converters. */
public abstract class AbstractSqlShowConverter<T extends SqlShowCall>
implements SqlNodeConverter<T> {

Expand All @@ -38,13 +39,8 @@ protected Operation convertShowOperation(T sqlShowCall, ConvertContext context)
final CatalogManager catalogManager = context.getCatalogManager();
final String currentCatalogName = catalogManager.getCurrentCatalog();
final String currentDatabaseName = catalogManager.getCurrentDatabase();
if (skipQualifyingDefaultCatalogAndDatabase()) {
return getOperationWithoutPrep(
currentCatalogName, currentDatabaseName, sqlShowCall, likeOp);
}
final String catalogName = catalogManager.qualifyCatalog(currentCatalogName);
final String databaseName = catalogManager.qualifyDatabase(currentDatabaseName);
return getOperationWithoutPrep(catalogName, databaseName, sqlShowCall, likeOp);
return getOperationWithoutPrep(
sqlShowCall, currentCatalogName, currentDatabaseName, likeOp);
}
final List<String> sqlIdentifierNameList = sqlShowCall.getSqlIdentifierNameList();
if (sqlIdentifierNameList.size() > 2) {
Expand All @@ -64,14 +60,8 @@ protected Operation convertShowOperation(T sqlShowCall, ConvertContext context)
sqlIdentifierNameList.size() == 1
? sqlIdentifierNameList.get(0)
: sqlIdentifierNameList.get(1);
final String qualifiedCatalogName = catalogManager.qualifyCatalog(catalogName);
final String qualifiedDatabaseName = catalogManager.qualifyDatabase(databaseName);
return getOperation(
sqlShowCall,
qualifiedCatalogName,
qualifiedDatabaseName,
sqlShowCall.getPreposition(),
likeOp);
sqlShowCall, catalogName, databaseName, sqlShowCall.getPreposition(), likeOp);
}

public ShowLikeOperator getLikeOp(SqlShowCall sqlShowCall) {
Expand All @@ -81,22 +71,18 @@ public ShowLikeOperator getLikeOp(SqlShowCall sqlShowCall) {
}

public abstract Operation getOperationWithoutPrep(
String catalogName,
String databaseName,
T sqlShowCall,
@Nullable String catalogName,
@Nullable String databaseName,
@Nullable ShowLikeOperator likeOp);

public abstract Operation getOperation(
T sqlShowCall,
String catalogName,
String databaseName,
@Nullable String catalogName,
@Nullable String databaseName,
@Nullable String prep,
@Nullable ShowLikeOperator likeOp);

@Override
public abstract Operation convertSqlNode(T node, ConvertContext context);

protected boolean skipQualifyingDefaultCatalogAndDatabase() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public Operation convertSqlNode(SqlShowDatabases sqlShowDatabases, ConvertContex
if (sqlShowDatabases.getPreposition() == null) {
final CatalogManager catalogManager = context.getCatalogManager();
final String currentCatalogName = catalogManager.getCurrentCatalog();
final String qualifiedCatalogName = catalogManager.qualifyCatalog(currentCatalogName);
return new ShowDatabasesOperation(qualifiedCatalogName, likeOp);
return new ShowDatabasesOperation(currentCatalogName, likeOp);
} else {
return new ShowDatabasesOperation(
sqlShowDatabases.getCatalogName(), sqlShowDatabases.getPreposition(), likeOp);
Expand Down
Loading

0 comments on commit 2d7a7c0

Please sign in to comment.