Skip to content

Commit

Permalink
[feature](analyze_cmd) add show-tablets-belong stmt for analyzing a b…
Browse files Browse the repository at this point in the history
…atch of tablet-ids (apache#27994) (apache#31255)

bp apache#27994 apache#31230
  • Loading branch information
morningman authored and weixingyu12 committed Feb 26, 2024
1 parent f2fefe7 commit b1f0725
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
{
"title": "SHOW-TABLETS-BELONG",
"language": "en"
}
---

<!--
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.
-->

<version since="dev">

## SHOW-TABLETS-BELONG

</version>

### 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

1 change: 1 addition & 0 deletions docs/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
{
"title": "SHOW-TABLETS-BELONG",
"language": "zh-CN"
}
---

<!--
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.
-->

<version since="dev">

## SHOW-TABLETS-BELONG

</version>

### 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

Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ BACKEND: 'BACKEND';
BACKENDS: 'BACKENDS';
BACKUP: 'BACKUP';
BEGIN: 'BEGIN';
BELONG: 'BELONG';
BETWEEN: 'BETWEEN';
BIGINT: 'BIGINT';
BIN: 'BIN';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ nonReserved
| BACKENDS
| BACKUP
| BEGIN
| BELONG
| BIN
| BITAND
| BITMAP
Expand Down
9 changes: 8 additions & 1 deletion fe/fe-core/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,13 @@ terminal String
KW_BACKENDS,
KW_BACKUP,
KW_BEGIN,
KW_BELONG,
KW_BETWEEN,
KW_BIGINT,
KW_BIN,
KW_BINLOG,
KW_BITMAP,
KW_BITMAP_UNION,
KW_NGRAM_BF,
KW_BLOB,
KW_BOOLEAN,
KW_BRIEF,
Expand Down Expand Up @@ -481,6 +481,7 @@ terminal String
KW_NEGATIVE,
KW_NEVER,
KW_NEXT,
KW_NGRAM_BF,
KW_NO,
KW_NOT,
KW_NULL,
Expand Down Expand Up @@ -4007,6 +4008,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);
Expand Down Expand Up @@ -7780,6 +7785,8 @@ keyword ::=
{: RESULT = id; :}
| KW_PERCENT:id
{: RESULT = id; :}
| KW_BELONG:id
{: RESULT = id; :}
;

// Identifier that contain keyword
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Long> tabletIds;

private static final ImmutableList<String> TITLE_NAMES = new ImmutableList.Builder<String>()
.add("DbName")
.add("TableName")
.add("TableSize")
.add("PartitionNum")
.add("BucketNum")
.add("ReplicaCount")
.add("TabletIds")
.build();

public ShowTabletsBelongStmt(List<Long> tabletIds) {
this.tabletIds = tabletIds;
}

public List<Long> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,16 @@ public TableName getTableNameByTableId(Long tableId) {
return null;
}

public Table getTableByTableId(Long tableId) {
for (Database db : fullNameToDb.values()) {
Table table = db.getTableNullable(tableId);
if (table != null) {
return table;
}
}
return null;
}

// Use tryLock to avoid potential dead lock
private boolean tryLock(boolean mustLock) {
while (true) {
Expand Down
Loading

0 comments on commit b1f0725

Please sign in to comment.