Skip to content

Commit

Permalink
[Command] [SQL] Add show database/table/partition id command (apache#…
Browse files Browse the repository at this point in the history
…5807)

In BE, when a problem happened, in the log, we can find the database id, table id, partition id,
but no database name, table name, partition name.

In FE, there also no way to find database name/table name/partition name accourding to
database id/table id/partition id. Therefore, this patch add 3 new commands:

1. show database id;
mysql> show database 10002;
+----------------------+
| DbName               |
+----------------------+
| default_cluster:test |
+----------------------+

2. show table id;
mysql> show table 11100;
+----------------------+-----------+-------+
| DbName               | TableName | DbId  |
+----------------------+-----------+-------+
| default_cluster:test | table2    | 10002 |
+----------------------+-----------+-------+

3. show partition id;
mysql> show partition 11099;
+----------------------+-----------+---------------+-------+---------+
| DbName               | TableName | PartitionName | DbId  | TableId |
+----------------------+-----------+---------------+-------+---------+
| default_cluster:test | table2    | p201708       | 10002 | 11100   |
+----------------------+-----------+---------------+-------+---------+
  • Loading branch information
xinghuayu007 authored May 26, 2021
1 parent a55b12d commit ba69f7a
Show file tree
Hide file tree
Showing 14 changed files with 708 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
{
"title": "SHOW DATABASE",
"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.
-->

# SHOW DATABASE
## Description
This statement is used to display database name according to database id (for administrators only)
Grammar:
SHOW DATABASE [database_id]

## example
1. Display database name according to database id
SHOW DATABASE 1001;

## keyword
SHOW,DATABASE
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
{
"title": "SHOW PARTITION",
"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.
-->

# SHOW PARTITION
## Description
This statement is used to display database name, table name, partition name according to partition id (for administrators only)
Grammar:
SHOW PARTITION [partition_id]

## example
1. Display database name, table name, partition name according to partition id
SHOW PARTITION 10002;

## keyword
SHOW,PARTITION
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
{
"title": "SHOW TABLE",
"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.
-->

# SHOW TABLE
## Description
This statement is used to display database name, table name according to table id (for administrators only)
Grammar:
SHOW TABLE [table_id]

## example
1. Display database name, table name according to table id
SHOW TABLE 10001;

## keyword
SHOW,TABLE
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
{
"title": "SHOW DATABASE",
"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.
-->

# SHOW DATABASE
## description
该语句用于根据 database id 查找对应的 database name(仅管理员使用)
语法:
SHOW DATABASE [database_id]

## example
1. 根据 database id 查找对应的 database name
SHOW DATABASE 1001;

## keyword
SHOW,DATABASE

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
{
"title": "SHOW PARTITION",
"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.
-->

# SHOW DATABASE
## description
该语句用于根据 partition id 查找对应的 database name, table name, partition name(仅管理员使用)
语法:
SHOW PARTITION [partition_id]

## example
1. 根据 partition id 查找对应的 database name, table name, partition name
SHOW PARTITION 10002;

## keyword
SHOW,PARTITION

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
{
"title": "SHOW TABLE",
"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.
-->

# SHOW DATABASE
## description
该语句用于根据 table id 查找对应的 database name, table name(仅管理员使用)
语法:
SHOW TABLE [table_id]

## example
1. 根据 table id 查找对应的 database name, table name
SHOW TABLE 10001;

## keyword
SHOW,TABLE

15 changes: 15 additions & 0 deletions fe/fe-core/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,11 @@ show_param ::=
{:
RESULT = new ShowTableStmt(db, parser.isVerbose, parser.wild, parser.where);
:}
/* show table id */
| KW_TABLE INTEGER_LITERAL:tableId
{:
RESULT = new ShowTableIdStmt(tableId);
:}
/* show processlist */
| opt_full KW_PROCESSLIST
{:
Expand Down Expand Up @@ -2383,6 +2388,11 @@ show_param ::=
{:
RESULT = new ShowDbStmt(parser.wild, parser.where);
:}
/* show database id */
| KW_DATABASE INTEGER_LITERAL:dbId
{:
RESULT = new ShowDbIdStmt(dbId);
:}
| KW_SCHEMAS opt_wild_where
{:
RESULT = new ShowDbStmt(parser.wild, parser.where);
Expand Down Expand Up @@ -2480,6 +2490,11 @@ show_param ::=
{:
RESULT = new ShowPartitionsStmt(tblName, parser.where, orderByClause, limitClause, tmp);
:}
/* show partition id */
| KW_PARTITION INTEGER_LITERAL:partitionId
{:
RESULT = new ShowPartitionIdStmt(partitionId);
:}
| KW_TABLET INTEGER_LITERAL:tabletId
{:
RESULT = new ShowTabletStmt(null, tabletId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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.Catalog;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.ShowResultSetMetaData;

// SHOW DATABASE ID
public class ShowDbIdStmt extends ShowStmt {
private long dbId;

public ShowDbIdStmt(long dbId) {
this.dbId = dbId;
}

public long getDbId() {
return dbId;
}

@Override
public void analyze(Analyzer analyzer) throws AnalysisException {
// check access first
if (!Catalog.getCurrentCatalog().getAuth().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "SHOW DATABASE");
}
}

@Override
public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("SHOW");
sb.append(" DATABASE ");
sb.append(this.dbId);
return sb.toString();
}

@Override
public String toString() {
return toSql();
}

@Override
public ShowResultSetMetaData getMetaData() {
ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder();
builder.addColumn(new Column("DbName", ScalarType.createVarchar(30)));
return builder.build();
}
}
Loading

0 comments on commit ba69f7a

Please sign in to comment.