From 2e2e174804c9a8ac6947ccc1c380c4e896f478bc Mon Sep 17 00:00:00 2001 From: Jibing-Li <64681310+Jibing-Li@users.noreply.github.com> Date: Tue, 12 Sep 2023 14:12:18 +0800 Subject: [PATCH] [fix](forward master op)Set default catalog and db only when they exist in master FE while executing forwarded stmt (#24212) In this case, forward to master will throw catalog or db not found exception: Connect to a follower: 1. create database test 2. use test 3. drop database test 4. create database test This is because after step 2, the default db in follower has been set to `test`, drop database will not change the default db. In step 4, the default db `test` is set and forwarded to master, and master will fail to find it because it is already dropped. This pr is to set the default catalog and db only when they exist. The actual reason is that, when Follower handle the `drop db` stmt, it will forward to master to execute it, but can not unset its own "current db" --- .../org/apache/doris/qe/ConnectProcessor.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java index de3acfc8e8..296cac55bc 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java @@ -665,11 +665,19 @@ public class ConnectProcessor { int idx = request.isSetStmtIdx() ? request.getStmtIdx() : 0; executor = new StmtExecutor(ctx, new OriginStatement(request.getSql(), idx), true); ctx.setExecutor(executor); + // Set default catalog only if the catalog exists. if (request.isSetDefaultCatalog()) { - ctx.getEnv().changeCatalog(ctx, request.getDefaultCatalog()); - } - if (request.isSetDefaultDatabase() && !request.getDefaultDatabase().isEmpty()) { - ctx.getEnv().changeDb(ctx, request.getDefaultDatabase()); + CatalogIf catalog = ctx.getEnv().getCatalogMgr().getCatalog(request.getDefaultCatalog()); + if (catalog != null) { + ctx.getEnv().changeCatalog(ctx, request.getDefaultCatalog()); + // Set default db only when the default catalog is set and the dbname exists in default catalog. + if (request.isSetDefaultDatabase()) { + DatabaseIf db = ctx.getCurrentCatalog().getDbNullable(request.getDefaultDatabase()); + if (db != null) { + ctx.getEnv().changeDb(ctx, request.getDefaultDatabase()); + } + } + } } TUniqueId queryId; // This query id will be set in ctx if (request.isSetQueryId()) {