From 7e039d3a0528e00ef0e6162b9f966d41616f09d1 Mon Sep 17 00:00:00 2001 From: Yulei-Yang Date: Thu, 18 Jul 2024 20:16:53 +0800 Subject: [PATCH] update --- .../java/org/apache/doris/catalog/Env.java | 6 +++--- .../apache/doris/datasource/CatalogMgr.java | 18 ++++++++++++------ .../hive/test_external_catalog_hive.groovy | 18 ++++++++++++++++-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java index 120d8e25460266..b0c9c9761fd0dc 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java @@ -5369,7 +5369,7 @@ public void cancelAlterCluster(CancelAlterSystemStmt stmt) throws DdlException { this.alter.getClusterHandler().cancel(stmt); } - // Switch catalog of this sesseion. + // Switch catalog of this session public void changeCatalog(ConnectContext ctx, String catalogName) throws DdlException { CatalogIf catalogIf = catalogMgr.getCatalog(catalogName); if (catalogIf == null) { @@ -5381,11 +5381,11 @@ public void changeCatalog(ConnectContext ctx, String catalogName) throws DdlExce if (StringUtils.isNotEmpty(currentDB)) { // When dropped the current catalog in current context, the current catalog will be null. if (ctx.getCurrentCatalog() != null) { - ConnectContext.get().addLastDBOfCatalog(ctx.getCurrentCatalog().getName(), currentDB); + ctx.addLastDBOfCatalog(ctx.getCurrentCatalog().getName(), currentDB); } } ctx.changeDefaultCatalog(catalogName); - String lastDb = ConnectContext.get().getLastDBOfCatalog(catalogName); + String lastDb = ctx.getLastDBOfCatalog(catalogName); if (StringUtils.isNotEmpty(lastDb)) { ctx.setDatabase(lastDb); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java index 0c5a3be3e326be..2aec7655fe5f99 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java @@ -138,7 +138,9 @@ private CatalogIf removeCatalog(long catalogId) { if (catalog != null) { catalog.onClose(); nameToCatalog.remove(catalog.getName()); - ConnectContext.get().removeLastDBOfCatalog(catalog.getName()); + if (ConnectContext.get() != null) { + ConnectContext.get().removeLastDBOfCatalog(catalog.getName()); + } Env.getCurrentEnv().getExtMetaCacheMgr().removeCache(catalog.getId()); if (!Strings.isNullOrEmpty(catalog.getResource())) { Resource catalogResource = Env.getCurrentEnv().getResourceMgr().getResource(catalog.getResource()); @@ -272,7 +274,9 @@ public void dropCatalog(DropCatalogStmt stmt) throws UserException { replayDropCatalog(log); Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_DROP_CATALOG, log); - ConnectContext.get().removeLastDBOfCatalog(stmt.getCatalogName()); + if (ConnectContext.get() != null) { + ConnectContext.get().removeLastDBOfCatalog(stmt.getCatalogName()); + } Env.getCurrentEnv().getQueryStats().clear(catalog.getId()); } finally { writeUnlock(); @@ -297,10 +301,12 @@ public void alterCatalogName(AlterCatalogNameStmt stmt) throws UserException { Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_ALTER_CATALOG_NAME, log); ConnectContext ctx = ConnectContext.get(); - String db = ctx.getLastDBOfCatalog(stmt.getCatalogName()); - if (db != null) { - ctx.removeLastDBOfCatalog(stmt.getCatalogName()); - ctx.addLastDBOfCatalog(log.getNewCatalogName(), db); + if (ctx != null) { + String db = ctx.getLastDBOfCatalog(stmt.getCatalogName()); + if (db != null) { + ctx.removeLastDBOfCatalog(stmt.getCatalogName()); + ctx.addLastDBOfCatalog(log.getNewCatalogName(), db); + } } } finally { writeUnlock(); diff --git a/regression-test/suites/external_table_p0/hive/test_external_catalog_hive.groovy b/regression-test/suites/external_table_p0/hive/test_external_catalog_hive.groovy index 33c8feb2a5ade8..60880009dee64f 100644 --- a/regression-test/suites/external_table_p0/hive/test_external_catalog_hive.groovy +++ b/regression-test/suites/external_table_p0/hive/test_external_catalog_hive.groovy @@ -145,10 +145,24 @@ suite("test_external_catalog_hive", "p0,external,hive,external_docker,external_d sql """alter catalog ${catalog_name} rename hms;""" sql """switch hms;""" - - def res3 = sql """select count(*) from test.hive_test limit 10;""" + sql """use test;""" + def res3 = sql """select count(*) from hive_test limit 10;""" logger.info("recoding select: " + res3.toString()) + def user = 'account_user_test' + def pwd = 'C123_567p' + try_sql("DROP USER ${user}") + sql """CREATE USER '${user}' IDENTIFIED BY '${pwd}'""" + sql """GRANT SELECT_PRIV on *.*.* to '${user}'""" + def result1 = connect(user=user, password="${pwd}", url=context.config.jdbcUrl) { + sql """switch hms;""" + try { + sql """show tables;""" + } catch (Exception e) { + assertTrue(e.getMessage().contains("No database selected")) + } + } + sql """alter catalog hms rename ${catalog_name};""" // test wrong access controller