diff --git a/docs/en/docs/sql-manual/sql-reference/Show-Statements/SHOW-TABLETS-BELONG.md b/docs/en/docs/sql-manual/sql-reference/Show-Statements/SHOW-TABLETS-BELONG.md new file mode 100644 index 00000000000000..9321c90ff516fd --- /dev/null +++ b/docs/en/docs/sql-manual/sql-reference/Show-Statements/SHOW-TABLETS-BELONG.md @@ -0,0 +1,74 @@ +--- +{ + "title": "SHOW-TABLETS-BELONG", + "language": "en" +} +--- + + + + + +## SHOW-TABLETS-BELONG + + + +### Name + +SHOW TABLETS BELONG + +### Description + +Used to show tablets and information of their belonging table + +grammar: + +```sql +SHOW TABLETS BELONG tablet-ids; +``` + +illustrate: + +1. tablet-ids:one or more tablet-ids, with comma separated +2. Columns of result keep same with result of `SHOW-DATA` for the same table + +### Example + +1. show information of four tablet-ids (actually, three tablet-ids. Result will be deduplicated) + + ```sql + SHOW TABLETS BELONG 27028,78880,78382,27028; + ``` + + ``` ++---------------------+-----------+-----------+--------------+-----------+--------------+----------------+ +| DbName | TableName | TableSize | PartitionNum | BucketNum | ReplicaCount | TabletIds | ++---------------------+-----------+-----------+--------------+-----------+--------------+----------------+ +| default_cluster:db1 | kec | 613.000 B | 379 | 604 | 604 | [78880, 78382] | +| default_cluster:db1 | test | 1.874 KB | 1 | 1 | 1 | [27028] | ++---------------------+-----------+-----------+--------------+-----------+--------------+----------------+ + ``` + +### Keywords + + SHOW, TABLETS, BELONG + +### Best Practice + diff --git a/docs/sidebars.json b/docs/sidebars.json index 4db8f541361405..f81e84b71c2b40 100644 --- a/docs/sidebars.json +++ b/docs/sidebars.json @@ -867,7 +867,8 @@ "sql-manual/sql-reference/Database-Administration-Statements/RECOVER", "sql-manual/sql-reference/Database-Administration-Statements/KILL", "sql-manual/sql-reference/Database-Administration-Statements/ADMIN-REBALANCE-DISK", - "sql-manual/sql-reference/Database-Administration-Statements/ADMIN-CANCEL-REBALANCE-DISK" + "sql-manual/sql-reference/Database-Administration-Statements/ADMIN-CANCEL-REBALANCE-DISK", + "sql-manual/sql-reference/Database-Administration-Statements/UNSET-VARIABLE" ] }, { @@ -1068,6 +1069,7 @@ "sql-manual/sql-reference/Show-Statements/SHOW-WHITE-LIST", "sql-manual/sql-reference/Show-Statements/SHOW-WARNING", "sql-manual/sql-reference/Show-Statements/SHOW-TABLET", + "sql-manual/sql-reference/Show-Statements/SHOW-TABLETS-BELONG", "sql-manual/sql-reference/Show-Statements/SHOW-VARIABLES", "sql-manual/sql-reference/Show-Statements/SHOW-PLUGINS", "sql-manual/sql-reference/Show-Statements/SHOW-ROLES", diff --git a/docs/zh-CN/docs/sql-manual/sql-reference/Show-Statements/SHOW-TABLETS-BELONG.md b/docs/zh-CN/docs/sql-manual/sql-reference/Show-Statements/SHOW-TABLETS-BELONG.md new file mode 100644 index 00000000000000..492659634681ec --- /dev/null +++ b/docs/zh-CN/docs/sql-manual/sql-reference/Show-Statements/SHOW-TABLETS-BELONG.md @@ -0,0 +1,74 @@ +--- +{ + "title": "SHOW-TABLETS-BELONG", + "language": "zh-CN" +} +--- + + + + + +## SHOW-TABLETS-BELONG + + + +### Name + +SHOW TABLETS BELONG + +### Description + +该语句用于展示指定Tablets归属的表的信息 + +语法: + +```sql +SHOW TABLETS BELONG tablet-ids; +``` + +说明: + +1. tablet-ids:代表一到多个tablet-id构成的列表。如有多个,使用逗号分隔 +2. 结果中 table 相关的信息和 `SHOW-DATA` 语句的口径一致 + +### Example + +1. 展示3个tablet-id的相关信息(tablet-id可去重) + + ```sql + SHOW TABLETS BELONG 27028,78880,78382,27028; + ``` + + ``` ++---------------------+-----------+-----------+--------------+-----------+--------------+----------------+ +| DbName | TableName | TableSize | PartitionNum | BucketNum | ReplicaCount | TabletIds | ++---------------------+-----------+-----------+--------------+-----------+--------------+----------------+ +| default_cluster:db1 | kec | 613.000 B | 379 | 604 | 604 | [78880, 78382] | +| default_cluster:db1 | test | 1.874 KB | 1 | 1 | 1 | [27028] | ++---------------------+-----------+-----------+--------------+-----------+--------------+----------------+ + ``` + +### Keywords + + SHOW, TABLETS, BELONG + +### Best Practice + diff --git a/fe/fe-core/src/main/cup/sql_parser.cup b/fe/fe-core/src/main/cup/sql_parser.cup index 3a7b5b1b5fc5d4..47fa87dc4e4af0 100644 --- a/fe/fe-core/src/main/cup/sql_parser.cup +++ b/fe/fe-core/src/main/cup/sql_parser.cup @@ -267,6 +267,7 @@ terminal String KW_BACKENDS, KW_BACKUP, KW_BEGIN, + KW_BELONG, KW_BETWEEN, KW_BIGINT, KW_BIN, @@ -3981,6 +3982,10 @@ show_param ::= {: RESULT = new ShowTabletStmt(null, tabletId); :} + | KW_TABLETS KW_BELONG integer_list:tabletIds + {: + RESULT = new ShowTabletsBelongStmt(tabletIds); + :} | KW_TABLETS KW_FROM table_name:dbTblName opt_partition_names:partitionNames opt_wild_where order_by_clause:orderByClause limit_clause:limitClause {: RESULT = new ShowTabletStmt(dbTblName, -1L, partitionNames, parser.where, orderByClause, limitClause); diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTabletsBelongStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTabletsBelongStmt.java new file mode 100644 index 00000000000000..b321dace7f555e --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTabletsBelongStmt.java @@ -0,0 +1,94 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.analysis; + +import org.apache.doris.catalog.Column; +import org.apache.doris.catalog.ScalarType; +import org.apache.doris.common.UserException; +import org.apache.doris.qe.ShowResultSetMetaData; + +import com.google.common.collect.ImmutableList; + +import java.util.List; + +/** + * ShowTabletsBelongStmt is used to show information of tables which tablets are belonged to + * syntax: + * SHOW TABLETS BELONG tablet_ids + */ +public class ShowTabletsBelongStmt extends ShowStmt { + private List tabletIds; + + private static final ImmutableList TITLE_NAMES = new ImmutableList.Builder() + .add("DbName") + .add("TableName") + .add("TableSize") + .add("PartitionNum") + .add("BucketNum") + .add("ReplicaCount") + .add("TabletIds") + .build(); + + public ShowTabletsBelongStmt(List tabletIds) { + this.tabletIds = tabletIds; + } + + public List getTabletIds() { + return tabletIds; + } + + @Override + public void analyze(Analyzer analyzer) throws UserException { + if (tabletIds == null || tabletIds.isEmpty()) { + throw new UserException("Please supply at least one tablet id"); + } + } + + @Override + public ShowResultSetMetaData getMetaData() { + ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder(); + for (String title : TITLE_NAMES) { + builder.addColumn(new Column(title, ScalarType.createVarchar(128))); + } + return builder.build(); + } + + @Override + public RedirectStatus getRedirectStatus() { + return RedirectStatus.FORWARD_NO_SYNC; + } + + @Override + public String toSql() { + StringBuilder sb = new StringBuilder(); + sb.append("SHOW TABLETS BELONG "); + + for (long tabletId : tabletIds) { + sb.append(tabletId); + sb.append(", "); + } + + String tmp = sb.toString(); + return tmp.substring(tmp.length() - 1); + } + + @Override + public String toString() { + return toSql(); + } +} 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 430f790ecd3768..4b6679299f164e 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 @@ -97,6 +97,7 @@ import org.apache.doris.analysis.ShowTableStatusStmt; import org.apache.doris.analysis.ShowTableStmt; import org.apache.doris.analysis.ShowTabletStmt; +import org.apache.doris.analysis.ShowTabletsBelongStmt; import org.apache.doris.analysis.ShowTransactionStmt; import org.apache.doris.analysis.ShowTrashDiskStmt; import org.apache.doris.analysis.ShowTrashStmt; @@ -168,6 +169,7 @@ import org.apache.doris.common.proc.TrashProcNode; import org.apache.doris.common.profile.ProfileTreeNode; import org.apache.doris.common.profile.ProfileTreePrinter; +import org.apache.doris.common.util.DebugUtil; import org.apache.doris.common.util.ListComparator; import org.apache.doris.common.util.LogBuilder; import org.apache.doris.common.util.LogKey; @@ -237,6 +239,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -417,6 +420,8 @@ public ShowResultSet execute() throws AnalysisException { handleShowCreateCatalog(); } else if (stmt instanceof ShowAnalyzeStmt) { handleShowAnalyze(); + } else if (stmt instanceof ShowTabletsBelongStmt) { + handleShowTabletsBelong(); } else if (stmt instanceof AdminCopyTabletStmt) { handleCopyTablet(); } else if (stmt instanceof ShowCatalogRecycleBinStmt) { @@ -2704,6 +2709,62 @@ private void handleShowAnalyze() { resultSet = new ShowResultSet(showStmt.getMetaData(), resultRows); } + private void handleShowTabletsBelong() { + ShowTabletsBelongStmt showStmt = (ShowTabletsBelongStmt) stmt; + List> rows = new ArrayList<>(); + + Env env = Env.getCurrentEnv(); + + TabletInvertedIndex invertedIndex = Env.getCurrentInvertedIndex(); + Map> tableToTabletIdsMap = new HashMap<>(); + for (long tabletId : showStmt.getTabletIds()) { + TabletMeta tabletMeta = invertedIndex.getTabletMeta(tabletId); + if (tabletMeta == null) { + continue; + } + Database db = env.getInternalCatalog().getDbNullable(tabletMeta.getDbId()); + if (db == null) { + continue; + } + long tableId = tabletMeta.getTableId(); + Table table = db.getTableNullable(tableId); + if (table == null) { + continue; + } + + if (!tableToTabletIdsMap.containsKey(tableId)) { + tableToTabletIdsMap.put(tableId, new HashSet<>()); + } + tableToTabletIdsMap.get(tableId).add(tabletId); + } + + for (long tableId : tableToTabletIdsMap.keySet()) { + Table table = env.getInternalCatalog().getTableByTableId(tableId); + List line = new ArrayList<>(); + line.add(table.getDatabase().getFullName()); + line.add(table.getName()); + + OlapTable olapTable = (OlapTable) table; + Pair tableSizePair = DebugUtil.getByteUint((long) olapTable.getDataSize()); + String readableSize = DebugUtil.DECIMAL_FORMAT_SCALE_3.format(tableSizePair.first) + " " + + tableSizePair.second; + line.add(readableSize); + line.add(new Long(olapTable.getPartitionNum()).toString()); + int totalBucketNum = 0; + Set partitionNamesSet = table.getPartitionNames(); + for (String partitionName : partitionNamesSet) { + totalBucketNum += table.getPartition(partitionName).getDistributionInfo().getBucketNum(); + } + line.add(new Long(totalBucketNum).toString()); + line.add(new Long(olapTable.getReplicaCount()).toString()); + line.add(tableToTabletIdsMap.get(tableId).toString()); + + rows.add(line); + } + + resultSet = new ShowResultSet(showStmt.getMetaData(), rows); + } + private void handleCopyTablet() throws AnalysisException { AdminCopyTabletStmt copyStmt = (AdminCopyTabletStmt) stmt; long tabletId = copyStmt.getTabletId(); diff --git a/fe/fe-core/src/main/jflex/sql_scanner.flex b/fe/fe-core/src/main/jflex/sql_scanner.flex index 7ad9845701c813..ad98eacef2eebe 100644 --- a/fe/fe-core/src/main/jflex/sql_scanner.flex +++ b/fe/fe-core/src/main/jflex/sql_scanner.flex @@ -115,6 +115,7 @@ import org.apache.doris.qe.SqlModeHelper; keywordMap.put("backends", new Integer(SqlParserSymbols.KW_BACKENDS)); keywordMap.put("backup", new Integer(SqlParserSymbols.KW_BACKUP)); keywordMap.put("begin", new Integer(SqlParserSymbols.KW_BEGIN)); + keywordMap.put("belong", new Integer(SqlParserSymbols.KW_BELONG)); keywordMap.put("between", new Integer(SqlParserSymbols.KW_BETWEEN)); keywordMap.put("bigint", new Integer(SqlParserSymbols.KW_BIGINT)); keywordMap.put("bin", new Integer(SqlParserSymbols.KW_BIN));