Skip to content

Commit 1092024

Browse files
Yulei-Yangpull[bot]
authored andcommitted
[fix](multi-catalog) use last used database for catalog when switch back (#14793)
remember last used database of every catalog and use it when switch back
1 parent 5037ebd commit 1092024

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4410,7 +4410,16 @@ public void changeCatalog(ConnectContext ctx, String catalogName) throws DdlExce
44104410
throw new DdlException(ErrorCode.ERR_UNKNOWN_CATALOG.formatErrorMsg(catalogName),
44114411
ErrorCode.ERR_UNKNOWN_CATALOG);
44124412
}
4413+
4414+
String currentDB = ctx.getDatabase();
4415+
if (StringUtils.isNotEmpty(currentDB)) {
4416+
catalogMgr.addLastDBOfCatalog(ctx.getCurrentCatalog().getName(), currentDB);
4417+
}
44134418
ctx.changeDefaultCatalog(catalogName);
4419+
String lastDb = catalogMgr.getLastDB(catalogName);
4420+
if (StringUtils.isNotEmpty(lastDb)) {
4421+
ctx.setDatabase(lastDb);
4422+
}
44144423
if (catalogIf instanceof EsExternalCatalog) {
44154424
ctx.setDatabase(SystemInfoService.DEFAULT_CLUSTER + ClusterNamespace.CLUSTER_DELIMITER
44164425
+ EsExternalCatalog.DEFAULT_DB);

‎fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public class CatalogMgr implements Writable, GsonPostProcessable {
7676
private final Map<Long, CatalogIf> idToCatalog = Maps.newConcurrentMap();
7777
// this map will be regenerated from idToCatalog, so not need to persist.
7878
private final Map<String, CatalogIf> nameToCatalog = Maps.newConcurrentMap();
79+
// record last used database of every catalog
80+
private final Map<String, String> lastDBOfCatalog = Maps.newConcurrentMap();
7981

8082
// Use a separate instance to facilitate access.
8183
// internalDataSource still exists in idToDataSource and nameToDataSource
@@ -105,6 +107,7 @@ private CatalogIf removeCatalog(long catalogId) {
105107
if (catalog != null) {
106108
catalog.onClose();
107109
nameToCatalog.remove(catalog.getName());
110+
lastDBOfCatalog.remove(catalog.getName());
108111
Env.getCurrentEnv().getExtMetaCacheMgr().removeCache(catalog.getName());
109112
}
110113
return catalog;
@@ -146,6 +149,14 @@ public CatalogIf getCatalogOrAnalysisException(String name) throws AnalysisExcep
146149
ErrorCode.ERR_UNKNOWN_CATALOG));
147150
}
148151

152+
public void addLastDBOfCatalog(String catalog, String db) {
153+
lastDBOfCatalog.put(catalog, db);
154+
}
155+
156+
public String getLastDB(String catalog) {
157+
return lastDBOfCatalog.get(catalog);
158+
}
159+
149160
public List<Long> getCatalogIds() {
150161
return Lists.newArrayList(idToCatalog.keySet());
151162
}
@@ -241,6 +252,8 @@ public void dropCatalog(DropCatalogStmt stmt) throws UserException {
241252
CatalogLog log = CatalogFactory.constructorCatalogLog(catalog.getId(), stmt);
242253
replayDropCatalog(log);
243254
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_DROP_CATALOG, log);
255+
256+
lastDBOfCatalog.remove(stmt.getCatalogName());
244257
} finally {
245258
writeUnlock();
246259
}
@@ -262,6 +275,12 @@ public void alterCatalogName(AlterCatalogNameStmt stmt) throws UserException {
262275
CatalogLog log = CatalogFactory.constructorCatalogLog(catalog.getId(), stmt);
263276
replayAlterCatalogName(log);
264277
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_ALTER_CATALOG_NAME, log);
278+
279+
String db = lastDBOfCatalog.get(stmt.getCatalogName());
280+
if (db != null) {
281+
lastDBOfCatalog.remove(stmt.getCatalogName());
282+
lastDBOfCatalog.put(log.getNewCatalogName(), db);
283+
}
265284
} finally {
266285
writeUnlock();
267286
}

‎regression-test/suites/external_table_emr_p2/hive/test_external_catalog_hive.groovy‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ suite("test_external_catalog_hive", "p2") {
8989
sql """ use tpch_1000_orc; """
9090
q03()
9191

92+
93+
// test remember last used database after switch / rename catalog
94+
sql """switch ${catalog_name};"""
95+
96+
sql """use test;"""
97+
98+
def res2 = sql """select count(*) from hive_test limit 10;"""
99+
logger.info("recoding select: " + res2.toString())
100+
101+
sql """switch internal;"""
102+
103+
sql """alter catalog ${catalog_name} rename hms;"""
104+
105+
sql """switch hms;"""
106+
107+
def res3 = sql """select count(*) from hive_test limit 10;"""
108+
logger.info("recoding select: " + res3.toString())
109+
110+
sql """alter catalog hms rename ${catalog_name};"""
92111
} finally {
93112
// sql """admin set frontend config ("enable_multi_catalog" = "false")"""
94113
}

‎regression-test/suites/external_table_emr_p2/hive/test_external_hive.groovy‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,3 @@ suite("test_external_hive", "p2") {
5656

5757

5858

59-

0 commit comments

Comments
 (0)