[fix](mutil-catalog) fix get many same name db/table when show where (#15076)

when show databases/tables/table status where xxx, it will change a selectStmt to select result from 
information_schema, it need catalog info to scan schema table, otherwise may get many
database or table info from multi catalog.

for example
mysql> show databases where schema_name='test';
+----------+
| Database |
+----------+
| test |
| test |
+----------+

MySQL [internal.test]> show tables from test where table_name='test_dc';
+----------------+
| Tables_in_test |
+----------------+
| test_dc |
| test_dc |
+----------------+
This commit is contained in:
xueweizhang
2022-12-19 14:27:48 +08:00
committed by GitHub
parent 000972ae17
commit 1597afcd67
21 changed files with 169 additions and 8 deletions

View File

@ -139,6 +139,7 @@ public class Analyzer {
private final Map<TupleId, Integer> currentOutputColumn = Maps.newHashMap();
// used for Information Schema Table Scan
private String schemaDb;
private String schemaCatalog;
private String schemaWild;
private String schemaTable; // table used in DESCRIBE Table
@ -2025,6 +2026,10 @@ public class Analyzer {
return schemaDb;
}
public String getSchemaCatalog() {
return schemaCatalog;
}
public String getSchemaTable() {
return schemaTable;
}
@ -2044,10 +2049,11 @@ public class Analyzer {
}
// for Schema Table Schema like SHOW TABLES LIKE "abc%"
public void setSchemaInfo(String db, String table, String wild) {
public void setSchemaInfo(String db, String table, String wild, String catalog) {
schemaDb = db;
schemaTable = table;
schemaWild = wild;
schemaCatalog = catalog;
}
public String getTargetDbName(FunctionName fnName) {

View File

@ -164,7 +164,7 @@ public class ShowColumnStmt extends ShowStmt {
selectStmt = new SelectStmt(selectList,
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
where, null, null, null, LimitElement.NO_LIMIT);
analyzer.setSchemaInfo(tableName.getDb(), tableName.getTbl(), null);
analyzer.setSchemaInfo(tableName.getDb(), tableName.getTbl(), null, tableName.getCtl());
return selectStmt;
}

View File

@ -88,7 +88,11 @@ public class ShowDbStmt extends ShowStmt {
selectStmt = new SelectStmt(selectList,
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
where, null, null, null, LimitElement.NO_LIMIT);
if (catalogName != null) {
analyzer.setSchemaInfo(null, null, null, catalogName);
} else {
analyzer.setSchemaInfo(null, null, null, analyzer.getDefaultCatalog());
}
return selectStmt;
}

View File

@ -183,7 +183,8 @@ public class ShowTableStatusStmt extends ShowStmt {
selectStmt = new SelectStmt(selectList,
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
where, null, null, null, LimitElement.NO_LIMIT);
analyzer.setSchemaInfo(db, null, null);
analyzer.setSchemaInfo(ClusterNamespace.getNameFromFullName(db), null, null,
analyzer.getDefaultCatalog());
return selectStmt;
}

View File

@ -114,7 +114,8 @@ public class ShowTableStmt extends ShowStmt {
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
where, null, null, null, LimitElement.NO_LIMIT);
analyzer.setSchemaInfo(ClusterNamespace.getNameFromFullName(db), null, null);
analyzer.setSchemaInfo(ClusterNamespace.getNameFromFullName(db), null, null,
analyzer.getDefaultCatalog());
return selectStmt;
}

View File

@ -106,7 +106,7 @@ public class ShowVariablesStmt extends ShowStmt {
// DB: type
// table: thread id
analyzer.setSchemaInfo(type.toSql(), null, null);
analyzer.setSchemaInfo(type.toSql(), null, null, null);
return selectStmt;
}

View File

@ -52,6 +52,7 @@ public class SchemaScanNode extends ScanNode {
private String userIp;
private String frontendIP;
private int frontendPort;
private String schemaCatalog;
/**
* Constructs node to scan given data files of table 'tbl'.
@ -77,6 +78,7 @@ public class SchemaScanNode extends ScanNode {
userIp = analyzer.getContext().getRemoteIP();
frontendIP = FrontendOptions.getLocalHostAddress();
frontendPort = Config.rpc_port;
schemaCatalog = analyzer.getSchemaCatalog();
}
@Override
@ -92,6 +94,9 @@ public class SchemaScanNode extends ScanNode {
msg.schema_scan_node.setDb("SESSION");
}
}
if (schemaCatalog != null) {
msg.schema_scan_node.setCatalog(schemaCatalog);
}
msg.schema_scan_node.show_hidden_cloumns = Util.showHiddenColumns();
if (schemaTable != null) {

View File

@ -172,7 +172,13 @@ public class FrontendServiceImpl implements FrontendService.Iface {
}
Env env = Env.getCurrentEnv();
List<CatalogIf> catalogIfs = env.getCatalogMgr().listCatalogs();
List<CatalogIf> catalogIfs = Lists.newArrayList();
if (Strings.isNullOrEmpty(params.catalog)) {
catalogIfs = env.getCatalogMgr().listCatalogs();
} else {
catalogIfs.add(env.getCatalogMgr()
.getCatalogOrException(params.catalog, catalog -> new TException("Unknown catalog " + catalog)));
}
for (CatalogIf catalog : catalogIfs) {
List<String> dbNames = catalog.getDbNames();
LOG.debug("get db names: {}, in catalog: {}", dbNames, catalog.getName());