This is an automated email from the ASF dual-hosted git repository. morningman pushed a commit to branch branch-1.2-lts in repository https://gitbox.apache.org/repos/asf/doris.git
commit cba74c25ade0019f9fd0271230ef22621fd35096 Author: xueweizhang <zxw520bl...@163.com> AuthorDate: Thu Dec 22 09:22:40 2022 +0800 [feature](multi-catalog) support show tables/table status from catalog.db (#15180) support 'show tables from catalog.db' and 'show table status from catalog.db' --- fe/fe-core/src/main/cup/sql_parser.cup | 14 +++++++++-- .../apache/doris/analysis/ShowTableStatusStmt.java | 18 +++++++++++--- .../org/apache/doris/analysis/ShowTableStmt.java | 27 +++++++++++++++++---- .../java/org/apache/doris/qe/ShowExecutor.java | 12 ++++++---- .../apache/doris/analysis/ShowTableStmtTest.java | 14 +++++------ .../java/org/apache/doris/qe/ShowExecutorTest.java | 19 +++++++++++---- .../data/query_p0/show/test_show_where.out | 28 ++++++++++++++++++++++ .../query_p0/show/test_show_create_catalog.groovy | 1 - .../suites/query_p0/show/test_show_where.groovy | 19 +++++++++++++-- 9 files changed, 123 insertions(+), 29 deletions(-) diff --git a/fe/fe-core/src/main/cup/sql_parser.cup b/fe/fe-core/src/main/cup/sql_parser.cup index 5c209fe478..49ff734ccd 100644 --- a/fe/fe-core/src/main/cup/sql_parser.cup +++ b/fe/fe-core/src/main/cup/sql_parser.cup @@ -3349,12 +3349,22 @@ show_param ::= /* show table status */ | KW_TABLE KW_STATUS opt_db:db opt_wild_where {: - RESULT = new ShowTableStatusStmt(db, parser.wild, parser.where); + RESULT = new ShowTableStatusStmt(db, null, parser.wild, parser.where); :} /* show table status */ + | KW_TABLE KW_STATUS from_or_in ident:ctl DOT ident:db opt_wild_where + {: + RESULT = new ShowTableStatusStmt(db, ctl, parser.wild, parser.where); + :} + /* show tables */ | opt_full KW_TABLES opt_db:db opt_wild_where {: - RESULT = new ShowTableStmt(db, parser.isVerbose, parser.wild, parser.where); + RESULT = new ShowTableStmt(db, null, parser.isVerbose, parser.wild, parser.where); + :} + /* show tables */ + | opt_full KW_TABLES from_or_in ident:ctl DOT ident:db opt_wild_where + {: + RESULT = new ShowTableStmt(db, ctl, parser.isVerbose, parser.wild, parser.where); :} /* show table id */ | KW_TABLE INTEGER_LITERAL:tableId diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTableStatusStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTableStatusStmt.java index a4fe257cb3..3b661995b5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTableStatusStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTableStatusStmt.java @@ -60,20 +60,26 @@ public class ShowTableStatusStmt extends ShowStmt { .build(); private String db; + private String catalog; private String wild; private Expr where; private SelectStmt selectStmt; - public ShowTableStatusStmt(String db, String wild, Expr where) { + public ShowTableStatusStmt(String db, String catalog, String wild, Expr where) { this.db = db; this.wild = wild; this.where = where; + this.catalog = catalog; } public String getDb() { return db; } + public String getCatalog() { + return catalog; + } + public String getPattern() { return wild; } @@ -88,6 +94,13 @@ public class ShowTableStatusStmt extends ShowStmt { } else { db = ClusterNamespace.getFullName(analyzer.getClusterName(), db); } + if (Strings.isNullOrEmpty(catalog)) { + catalog = analyzer.getDefaultCatalog(); + if (Strings.isNullOrEmpty(catalog)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_WRONG_NAME_FOR_CATALOG); + } + } + if (!Env.getCurrentEnv().getAuth().checkDbPriv(ConnectContext.get(), db, PrivPredicate.SHOW)) { ErrorReport.reportAnalysisException(ErrorCode.ERR_DBACCESS_DENIED_ERROR, analyzer.getQualifiedUser(), db); } @@ -183,8 +196,7 @@ 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(ClusterNamespace.getNameFromFullName(db), null, null, - analyzer.getDefaultCatalog()); + analyzer.setSchemaInfo(ClusterNamespace.getNameFromFullName(db), null, null, catalog); return selectStmt; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTableStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTableStmt.java index 20971a0309..c14a05ea16 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTableStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTableStmt.java @@ -41,29 +41,36 @@ public class ShowTableStmt extends ShowStmt { private static final TableName TABLE_NAME = new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, InfoSchemaDb.DATABASE_NAME, "tables"); private String db; + private String catalog; private boolean isVerbose; private String pattern; private Expr where; private SelectStmt selectStmt; - public ShowTableStmt(String db, boolean isVerbose, String pattern) { + public ShowTableStmt(String db, String catalog, boolean isVerbose, String pattern) { this.db = db; this.isVerbose = isVerbose; this.pattern = pattern; this.where = null; + this.catalog = catalog; } - public ShowTableStmt(String db, boolean isVerbose, String pattern, Expr where) { + public ShowTableStmt(String db, String catalog, boolean isVerbose, String pattern, Expr where) { this.db = db; this.isVerbose = isVerbose; this.pattern = pattern; this.where = where; + this.catalog = catalog; } public String getDb() { return db; } + public String getCatalog() { + return catalog; + } + public boolean isVerbose() { return isVerbose; } @@ -82,6 +89,12 @@ public class ShowTableStmt extends ShowStmt { } else { db = ClusterNamespace.getFullName(analyzer.getClusterName(), db); } + if (Strings.isNullOrEmpty(catalog)) { + catalog = analyzer.getDefaultCatalog(); + if (Strings.isNullOrEmpty(catalog)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_WRONG_NAME_FOR_CATALOG); + } + } // we do not check db privs here. because user may not have any db privs, // but if it has privs of tbls inside this db,it should be allowed to see this db. @@ -114,8 +127,7 @@ 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.getDefaultCatalog()); + analyzer.setSchemaInfo(ClusterNamespace.getNameFromFullName(db), null, null, catalog); return selectStmt; } @@ -129,7 +141,12 @@ public class ShowTableStmt extends ShowStmt { } sb.append(" TABLES"); if (!Strings.isNullOrEmpty(db)) { - sb.append(" FROM ").append(db); + if (!Strings.isNullOrEmpty(catalog)) { + sb.append(" FROM ").append(catalog); + sb.append(".").append(ClusterNamespace.getNameFromFullName(db)); + } else { + sb.append(" FROM ").append(db); + } } if (pattern != null) { sb.append(" LIKE '").append(pattern).append("'"); diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java index 3969603186..a8c38c04b0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java @@ -726,9 +726,9 @@ public class ShowExecutor { private void handleShowTable() throws AnalysisException { ShowTableStmt showTableStmt = (ShowTableStmt) stmt; List<List<String>> rows = Lists.newArrayList(); - // TODO(gaoxin): Whether to support "show tables from `ctl.db`" syntax in show statement? - String catalogName = ctx.getDefaultCatalog(); - DatabaseIf<TableIf> db = ctx.getCurrentCatalog().getDbOrAnalysisException(showTableStmt.getDb()); + DatabaseIf<TableIf> db = ctx.getEnv().getCatalogMgr() + .getCatalogOrAnalysisException(showTableStmt.getCatalog()) + .getDbOrAnalysisException(showTableStmt.getDb()); PatternMatcher matcher = null; if (showTableStmt.getPattern() != null) { matcher = PatternMatcher.createMysqlPattern(showTableStmt.getPattern(), @@ -740,7 +740,7 @@ public class ShowExecutor { } // check tbl privs if (!Env.getCurrentEnv().getAuth() - .checkTblPriv(ConnectContext.get(), catalogName, db.getFullName(), tbl.getName(), + .checkTblPriv(ConnectContext.get(), showTableStmt.getCatalog(), db.getFullName(), tbl.getName(), PrivPredicate.SHOW)) { continue; } @@ -766,7 +766,9 @@ public class ShowExecutor { private void handleShowTableStatus() throws AnalysisException { ShowTableStatusStmt showStmt = (ShowTableStatusStmt) stmt; List<List<String>> rows = Lists.newArrayList(); - DatabaseIf<TableIf> db = ctx.getCurrentCatalog().getDbOrAnalysisException(showStmt.getDb()); + DatabaseIf<TableIf> db = ctx.getEnv().getCatalogMgr() + .getCatalogOrAnalysisException(showStmt.getCatalog()) + .getDbOrAnalysisException(showStmt.getDb()); if (db != null) { PatternMatcher matcher = null; if (showStmt.getPattern() != null) { diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowTableStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowTableStmtTest.java index 2b92a56a77..eac5a8b394 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowTableStmtTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowTableStmtTest.java @@ -44,25 +44,25 @@ public class ShowTableStmtTest { @Test public void testNormal() throws AnalysisException { - ShowTableStmt stmt = new ShowTableStmt("", false, null); + ShowTableStmt stmt = new ShowTableStmt("", null, false, null); stmt.analyze(analyzer); - Assert.assertEquals("SHOW TABLES FROM testCluster:testDb", stmt.toString()); + Assert.assertEquals("SHOW TABLES FROM internal.testDb", stmt.toString()); Assert.assertEquals("testCluster:testDb", stmt.getDb()); Assert.assertFalse(stmt.isVerbose()); Assert.assertEquals(1, stmt.getMetaData().getColumnCount()); Assert.assertEquals("Tables_in_testDb", stmt.getMetaData().getColumn(0).getName()); - stmt = new ShowTableStmt("abc", true, null); + stmt = new ShowTableStmt("abc", null, true, null); stmt.analyze(analyzer); - Assert.assertEquals("SHOW FULL TABLES FROM testCluster:abc", stmt.toString()); + Assert.assertEquals("SHOW FULL TABLES FROM internal.abc", stmt.toString()); Assert.assertEquals(3, stmt.getMetaData().getColumnCount()); Assert.assertEquals("Tables_in_abc", stmt.getMetaData().getColumn(0).getName()); Assert.assertEquals("Table_type", stmt.getMetaData().getColumn(1).getName()); - stmt = new ShowTableStmt("abc", true, "bcd"); + stmt = new ShowTableStmt("abc", null, true, "bcd"); stmt.analyze(analyzer); Assert.assertEquals("bcd", stmt.getPattern()); - Assert.assertEquals("SHOW FULL TABLES FROM testCluster:abc LIKE 'bcd'", stmt.toString()); + Assert.assertEquals("SHOW FULL TABLES FROM internal.abc LIKE 'bcd'", stmt.toString()); Assert.assertEquals(3, stmt.getMetaData().getColumnCount()); Assert.assertEquals("Tables_in_abc", stmt.getMetaData().getColumn(0).getName()); Assert.assertEquals("Table_type", stmt.getMetaData().getColumn(1).getName()); @@ -70,7 +70,7 @@ public class ShowTableStmtTest { @Test(expected = AnalysisException.class) public void testNoDb() throws AnalysisException { - ShowTableStmt stmt = new ShowTableStmt("", false, null); + ShowTableStmt stmt = new ShowTableStmt("", null, false, null); stmt.analyze(AccessTestUtil.fetchEmptyDbAnalyzer()); Assert.fail("No exception throws"); } diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/ShowExecutorTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/ShowExecutorTest.java index 1b4a3afd5a..e0dc10aaa5 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/ShowExecutorTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/ShowExecutorTest.java @@ -325,7 +325,18 @@ public class ShowExecutorTest { @Test public void testShowTable() throws AnalysisException { - ShowTableStmt stmt = new ShowTableStmt("testCluster:testDb", false, null); + ShowTableStmt stmt = new ShowTableStmt("testCluster:testDb", null, false, null); + ShowExecutor executor = new ShowExecutor(ctx, stmt); + ShowResultSet resultSet = executor.execute(); + + Assert.assertTrue(resultSet.next()); + Assert.assertEquals("testTbl", resultSet.getString(0)); + Assert.assertFalse(resultSet.next()); + } + + @Test + public void testShowTableFromCatalog() throws AnalysisException { + ShowTableStmt stmt = new ShowTableStmt("testCluster:testDb", "internal", false, null); ShowExecutor executor = new ShowExecutor(ctx, stmt); ShowResultSet resultSet = executor.execute(); @@ -336,7 +347,7 @@ public class ShowExecutorTest { @Test public void testShowTableFromUnknownDatabase() throws AnalysisException { - ShowTableStmt stmt = new ShowTableStmt("testCluster:emptyDb", false, null); + ShowTableStmt stmt = new ShowTableStmt("testCluster:emptyDb", null, false, null); ShowExecutor executor = new ShowExecutor(ctx, stmt); expectedEx.expect(AnalysisException.class); expectedEx.expectMessage("Unknown database 'testCluster:emptyDb'"); @@ -345,7 +356,7 @@ public class ShowExecutorTest { @Test public void testShowTablePattern() throws AnalysisException { - ShowTableStmt stmt = new ShowTableStmt("testCluster:testDb", false, "empty%"); + ShowTableStmt stmt = new ShowTableStmt("testCluster:testDb", null, false, "empty%"); ShowExecutor executor = new ShowExecutor(ctx, stmt); ShowResultSet resultSet = executor.execute(); @@ -432,7 +443,7 @@ public class ShowExecutorTest { @Test public void testShowTableVerbose() throws AnalysisException { - ShowTableStmt stmt = new ShowTableStmt("testCluster:testDb", true, null); + ShowTableStmt stmt = new ShowTableStmt("testCluster:testDb", null, true, null); ShowExecutor executor = new ShowExecutor(ctx, stmt); ShowResultSet resultSet = executor.execute(); diff --git a/regression-test/data/query_p0/show/test_show_where.out b/regression-test/data/query_p0/show/test_show_where.out index ac84d03efc..e03d3cd141 100644 --- a/regression-test/data/query_p0/show/test_show_where.out +++ b/regression-test/data/query_p0/show/test_show_where.out @@ -4,10 +4,19 @@ doris_test -- !select -- ex_tb0 +ex_tb1 -- !select -- ex_tb0 +-- !select -- +ex_tb0 +ex_tb1 + +-- !select -- +ex_tb0 +ex_tb1 + -- !select -- doris_test @@ -17,11 +26,30 @@ ex_tb0 -- !select -- ex_tb0 +-- !select -- +ex_tb0 + +-- !select -- +ex_tb0 +ex_tb1 + +-- !select -- +ex_tb0 + -- !select -- doris_test -- !select -- ex_tb0 +ex_tb1 + +-- !select -- +ex_tb1 + +-- !select -- +ex_tb0 +ex_tb1 -- !select -- ex_tb0 + diff --git a/regression-test/suites/query_p0/show/test_show_create_catalog.groovy b/regression-test/suites/query_p0/show/test_show_create_catalog.groovy index a30adab3c5..14dd568b40 100644 --- a/regression-test/suites/query_p0/show/test_show_create_catalog.groovy +++ b/regression-test/suites/query_p0/show/test_show_create_catalog.groovy @@ -21,7 +21,6 @@ suite("test_show_create_catalog", "query") { String enabled = context.config.otherConfigs.get("enableJdbcTest") String mysql_port = context.config.otherConfigs.get("mysql_57_port"); if (enabled != null && enabled.equalsIgnoreCase("true")) { - sql """admin set frontend config ("enable_multi_catalog" = "true")""" sql """drop catalog if exists ${catalog_name} """ diff --git a/regression-test/suites/query_p0/show/test_show_where.groovy b/regression-test/suites/query_p0/show/test_show_where.groovy index 4e47de43a5..921722d89b 100644 --- a/regression-test/suites/query_p0/show/test_show_where.groovy +++ b/regression-test/suites/query_p0/show/test_show_where.groovy @@ -19,6 +19,7 @@ suite("test_show_where", "query") { String ex_db_name = "doris_test"; String ex_tb0 = "ex_tb0"; + String ex_tb1 = "ex_tb1"; String catalog_name = "test_show_where_mysql_jdbc_catalog"; try { sql """ drop database if exists ${ex_db_name} """ @@ -32,17 +33,25 @@ suite("test_show_where", "query") { ) DISTRIBUTED BY HASH(id) BUCKETS 10 PROPERTIES("replication_num" = "1"); """ + sql """ + CREATE TABLE `${ex_db_name}`.`${ex_tb1}` ( + `id` INT NULL COMMENT "主键id", + `name` string NULL COMMENT "名字" + ) DISTRIBUTED BY HASH(id) BUCKETS 10 + PROPERTIES("replication_num" = "1"); + """ sql """ use ${ex_db_name}""" qt_select "show databases where schema_name= '${ex_db_name}'" qt_select "show tables" qt_select "show tables where table_name= '${ex_tb0}'" + qt_select "show tables from ${ex_db_name}" + qt_select "show tables from internal.${ex_db_name}" String enabled = context.config.otherConfigs.get("enableJdbcTest") String mysql_port = context.config.otherConfigs.get("mysql_57_port"); if (enabled != null && enabled.equalsIgnoreCase("true")) { - sql """admin set frontend config ("enable_multi_catalog" = "true")""" sql """drop catalog if exists ${catalog_name} """ @@ -62,6 +71,9 @@ suite("test_show_where", "query") { qt_select "show databases where schema_name= '${ex_db_name}'" qt_select "show tables" qt_select "show tables where table_name= '${ex_tb0}'" + qt_select "show tables from ${ex_db_name}" + qt_select "show tables from internal.${ex_db_name}" + qt_select "show tables from ${catalog_name}.${ex_db_name}" sql """switch internal""" @@ -69,7 +81,9 @@ suite("test_show_where", "query") { qt_select "show databases where schema_name= '${ex_db_name}'" qt_select "show tables" - qt_select "show tables where table_name= '${ex_tb0}'" + qt_select "show tables where table_name= '${ex_tb1}'" + qt_select "show tables from internal.${ex_db_name}" + qt_select "show tables from ${catalog_name}.${ex_db_name}" } @@ -79,3 +93,4 @@ suite("test_show_where", "query") { try_sql("DROP CATALOG IF EXISTS `${catalog_name}`") } } + --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org