[Command] [SQL] Add show database/table/partition id command (#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   |
+----------------------+-----------+---------------+-------+---------+
This commit is contained in:
xinghuayu007
2021-05-26 09:58:02 +08:00
committed by GitHub
parent a55b12da90
commit ba69f7a7c8
14 changed files with 708 additions and 0 deletions

View File

@ -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
{:
@ -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);
@ -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);

View File

@ -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();
}
}

View File

@ -0,0 +1,74 @@
// 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 PARTITION ID
public class ShowPartitionIdStmt extends ShowStmt {
private long partitionId;
public ShowPartitionIdStmt(long partitionId) {
this.partitionId = partitionId;
}
public long getPartitionId() {
return partitionId;
}
@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 PARTITION");
}
}
@Override
public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("SHOW");
sb.append(" PARTITION ");
sb.append(this.partitionId);
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)));
builder.addColumn(new Column("TableName", ScalarType.createVarchar(30)));
builder.addColumn(new Column("PartitionName", ScalarType.createVarchar(30)));
builder.addColumn(new Column("DbId", ScalarType.createVarchar(30)));
builder.addColumn(new Column("TableId", ScalarType.createVarchar(30)));
return builder.build();
}
}

View File

@ -0,0 +1,73 @@
// 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 TABLE ID
public class ShowTableIdStmt extends ShowStmt {
private long tableId;
public ShowTableIdStmt(long tableId) {
this.tableId = tableId;
}
public long getTableId() {
return tableId;
}
@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 TABLE");
}
}
@Override
public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("SHOW");
sb.append(" TABLE ");
sb.append(this.tableId);
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)));
builder.addColumn(new Column("TableName", ScalarType.createVarchar(30)));
builder.addColumn(new Column("DbId", ScalarType.createVarchar(30)));
return builder.build();
}
}

View File

@ -35,6 +35,7 @@ import org.apache.doris.analysis.ShowCreateDbStmt;
import org.apache.doris.analysis.ShowCreateFunctionStmt;
import org.apache.doris.analysis.ShowCreateTableStmt;
import org.apache.doris.analysis.ShowDataStmt;
import org.apache.doris.analysis.ShowDbIdStmt;
import org.apache.doris.analysis.ShowDbStmt;
import org.apache.doris.analysis.ShowDeleteStmt;
import org.apache.doris.analysis.ShowDynamicPartitionStmt;
@ -47,6 +48,7 @@ import org.apache.doris.analysis.ShowIndexStmt;
import org.apache.doris.analysis.ShowLoadStmt;
import org.apache.doris.analysis.ShowLoadWarningsStmt;
import org.apache.doris.analysis.ShowMigrationsStmt;
import org.apache.doris.analysis.ShowPartitionIdStmt;
import org.apache.doris.analysis.ShowPartitionsStmt;
import org.apache.doris.analysis.ShowPluginsStmt;
import org.apache.doris.analysis.ShowProcStmt;
@ -63,6 +65,7 @@ import org.apache.doris.analysis.ShowSmallFilesStmt;
import org.apache.doris.analysis.ShowSnapshotStmt;
import org.apache.doris.analysis.ShowStmt;
import org.apache.doris.analysis.ShowStreamLoadStmt;
import org.apache.doris.analysis.ShowTableIdStmt;
import org.apache.doris.analysis.ShowTableStatusStmt;
import org.apache.doris.analysis.ShowTableStmt;
import org.apache.doris.analysis.ShowTabletStmt;
@ -187,10 +190,14 @@ public class ShowExecutor {
handleHelp();
} else if (stmt instanceof ShowDbStmt) {
handleShowDb();
} else if (stmt instanceof ShowDbIdStmt) {
handleShowDbId();
} else if (stmt instanceof ShowTableStmt) {
handleShowTable();
} else if (stmt instanceof ShowTableStatusStmt) {
handleShowTableStatus();
} else if (stmt instanceof ShowTableIdStmt) {
handleShowTableId();
} else if (stmt instanceof DescribeStmt) {
handleDescribe();
} else if (stmt instanceof ShowCreateTableStmt) {
@ -231,6 +238,8 @@ public class ShowExecutor {
handleShowCollation();
} else if (stmt instanceof ShowPartitionsStmt) {
handleShowPartitions();
} else if (stmt instanceof ShowPartitionIdStmt) {
handleShowPartitionId();
} else if (stmt instanceof ShowTabletStmt) {
handleShowTablet();
} else if (stmt instanceof ShowBackupStmt) {
@ -452,6 +461,80 @@ public class ShowExecutor {
resultSet = new ShowResultSet(showStmt.getMetaData(), rows);
}
private void handleShowDbId() throws AnalysisException {
ShowDbIdStmt showStmt = (ShowDbIdStmt) stmt;
long dbId = showStmt.getDbId();
List<List<String>> rows = Lists.newArrayList();
Catalog catalog = ctx.getCatalog();
Database database = catalog.getDb(dbId);
if (database != null) {
List<String> row = new ArrayList<>();
row.add(database.getFullName());
rows.add(row);
}
resultSet = new ShowResultSet(showStmt.getMetaData(), rows);
}
private void handleShowTableId() throws AnalysisException {
ShowTableIdStmt showStmt = (ShowTableIdStmt) stmt;
long tableId = showStmt.getTableId();
List<List<String>> rows = Lists.newArrayList();
Catalog catalog = ctx.getCatalog();
List<Long> dbIds = catalog.getDbIds();
for (long dbId : dbIds) {
Database database = catalog.getDb(dbId);
if (database == null) {
continue;
}
Table table = database.getTable(tableId);
if (table != null) {
List<String> row = new ArrayList<>();
row.add(database.getFullName());
row.add(table.getName());
row.add(String.valueOf(database.getId()));
rows.add(row);
break;
}
}
resultSet = new ShowResultSet(showStmt.getMetaData(), rows);
}
private void handleShowPartitionId() throws AnalysisException {
ShowPartitionIdStmt showStmt = (ShowPartitionIdStmt) stmt;
long partitionId = showStmt.getPartitionId();
List<List<String>> rows = Lists.newArrayList();
Catalog catalog = ctx.getCatalog();
List<Long> dbIds = catalog.getDbIds();
for (long dbId : dbIds) {
Database database = catalog.getDb(dbId);
if (database == null) {
continue;
}
List<Table> tables = database.getTables();
for (Table tbl : tables) {
if (tbl instanceof OlapTable) {
tbl.readLock();
try {
Partition partition = ((OlapTable) tbl).getPartition(partitionId);
if (partition != null) {
List<String> row = new ArrayList<>();
row.add(database.getFullName());
row.add(tbl.getName());
row.add(partition.getName());
row.add(String.valueOf(database.getId()));
row.add(String.valueOf(tbl.getId()));
rows.add(row);
break;
}
} finally {
tbl.readUnlock();
}
}
}
}
resultSet = new ShowResultSet(showStmt.getMetaData(), rows);
}
// Show databases statement
private void handleShowDb() throws AnalysisException {
ShowDbStmt showDbStmt = (ShowDbStmt) stmt;

View File

@ -0,0 +1,53 @@
// 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 mockit.Mocked;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.UserException;
import org.apache.doris.mysql.privilege.MockedAuth;
import org.apache.doris.mysql.privilege.PaloAuth;
import org.apache.doris.qe.ConnectContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ShowDbIdStmtTest {
private Analyzer analyzer;
@Mocked
private PaloAuth auth;
@Mocked
private ConnectContext ctx;
@Before
public void setUp() {
analyzer = AccessTestUtil.fetchAdminAnalyzer(true);
MockedAuth.mockedAuth(auth);
MockedAuth.mockedConnectContext(ctx, "root", "192.168.1.1");
}
@Test
public void testNormal() throws UserException, AnalysisException {
ShowDbIdStmt stmt = new ShowDbIdStmt(123456);
stmt.analyze(analyzer);
Assert.assertEquals("SHOW DATABASE 123456", stmt.toString());
Assert.assertEquals(1, stmt.getMetaData().getColumnCount());
Assert.assertEquals("DbName", stmt.getMetaData().getColumn(0).getName());
}
}

View File

@ -0,0 +1,58 @@
// 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 mockit.Mocked;
import org.apache.doris.mysql.privilege.MockedAuth;
import org.apache.doris.mysql.privilege.PaloAuth;
import org.apache.doris.qe.ConnectContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.UserException;
public class ShowPartitionIdStmtTest {
private Analyzer analyzer;
@Mocked
private PaloAuth auth;
@Mocked
private ConnectContext ctx;
@Before
public void setUp() {
analyzer = AccessTestUtil.fetchAdminAnalyzer(true);
MockedAuth.mockedAuth(auth);
MockedAuth.mockedConnectContext(ctx, "root", "192.168.1.1");
}
@Test
public void testNormal() throws UserException, AnalysisException {
ShowPartitionIdStmt stmt = new ShowPartitionIdStmt(123456);
stmt.analyze(analyzer);
Assert.assertEquals("SHOW PARTITION 123456", stmt.toString());
Assert.assertEquals(5, stmt.getMetaData().getColumnCount());
Assert.assertEquals("DbName", stmt.getMetaData().getColumn(0).getName());
Assert.assertEquals("TableName", stmt.getMetaData().getColumn(1).getName());
Assert.assertEquals("PartitionName", stmt.getMetaData().getColumn(2).getName());
Assert.assertEquals("DbId", stmt.getMetaData().getColumn(3).getName());
Assert.assertEquals("TableId", stmt.getMetaData().getColumn(4).getName());
}
}

View File

@ -0,0 +1,51 @@
// 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 mockit.Mocked;
import org.apache.doris.common.*;
import org.apache.doris.mysql.privilege.*;
import org.apache.doris.qe.*;
import org.junit.*;
public class ShowTableIdStmtTest {
private Analyzer analyzer;
@Mocked
private PaloAuth auth;
@Mocked
private ConnectContext ctx;
@Before
public void setUp() {
analyzer = AccessTestUtil.fetchAdminAnalyzer(true);
MockedAuth.mockedAuth(auth);
MockedAuth.mockedConnectContext(ctx, "root", "192.168.1.1");
}
@Test
public void testNormal() throws UserException, AnalysisException {
ShowTableIdStmt stmt = new ShowTableIdStmt(123456);
stmt.analyze(analyzer);
Assert.assertEquals("SHOW TABLE 123456", stmt.toString());
Assert.assertEquals(3, stmt.getMetaData().getColumnCount());
Assert.assertEquals("DbName", stmt.getMetaData().getColumn(0).getName());
Assert.assertEquals("TableName", stmt.getMetaData().getColumn(1).getName());
Assert.assertEquals("DbId", stmt.getMetaData().getColumn(2).getName());
}
}