Skip to content

Commit

Permalink
[Enhancement] (nereids)implement showTabletBelongCommand in nereids
Browse files Browse the repository at this point in the history
  • Loading branch information
Vallishp committed Nov 18, 2024
1 parent f5bffd4 commit 7518327
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ supportedDropStatement
;

supportedShowStatement

: SHOW (GLOBAL | SESSION | LOCAL)? VARIABLES wildWhere? #showVariables
| SHOW AUTHORS #showAuthors
| SHOW LAST INSERT #showLastInsert
Expand All @@ -214,6 +215,8 @@ supportedShowStatement
| SHOW BACKENDS #showBackends
| SHOW FRONTENDS name=identifier? #showFrontends
| SHOW TABLE tableId=INTEGER_VALUE #showTableId
| SHOW TABLETS BELONG
tabletIds+=INTEGER_VALUE (COMMA tabletIds+=INTEGER_VALUE)* #showTabletsBelong
;

unsupportedOtherStatement
Expand Down Expand Up @@ -295,8 +298,6 @@ unsupportedShowStatement
| SHOW TEMPORARY? PARTITIONS FROM tableName=multipartIdentifier
wildWhere? sortClause? limitClause? #showPartitions
| SHOW TABLET tabletId=INTEGER_VALUE #showTabletId
| SHOW TABLETS BELONG
tabletIds+=INTEGER_VALUE (COMMA tabletIds+=INTEGER_VALUE)* #showTabletBelong
| SHOW TABLETS FROM tableName=multipartIdentifier partitionSpec?
wildWhere? sortClause? limitClause? #showTabletsFromTable
| SHOW PROPERTY (FOR user=identifierOrText)? wildWhere? #showUserProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
import org.apache.doris.nereids.DorisParser.ShowRolesContext;
import org.apache.doris.nereids.DorisParser.ShowStorageEnginesContext;
import org.apache.doris.nereids.DorisParser.ShowTableIdContext;
import org.apache.doris.nereids.DorisParser.ShowTabletsBelongContext;
import org.apache.doris.nereids.DorisParser.ShowVariablesContext;
import org.apache.doris.nereids.DorisParser.ShowViewContext;
import org.apache.doris.nereids.DorisParser.SimpleColumnDefContext;
Expand Down Expand Up @@ -459,6 +460,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowRolesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowStorageEnginesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTableIdCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTabletsBelongCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowViewCommand;
import org.apache.doris.nereids.trees.plans.commands.UnsetDefaultStorageVaultCommand;
Expand Down Expand Up @@ -4177,4 +4179,13 @@ public LogicalPlan visitShowTableId(ShowTableIdContext ctx) {
public LogicalPlan visitShowPrivileges(ShowPrivilegesContext ctx) {
return new ShowPrivilegesCommand();
}

@Override
public LogicalPlan visitShowTabletsBelong(ShowTabletsBelongContext ctx) {
List<Long> tabletIdLists = new ArrayList<>();
ctx.tabletIds.stream().forEach(tabletToken -> {
tabletIdLists.add(Long.parseLong(tabletToken.getText()));
});
return new ShowTabletsBelongCommand(tabletIdLists);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public enum PlanType {
SHOW_VARIABLES_COMMAND,
SHOW_AUTHORS_COMMAND,
SHOW_VIEW_COMMAND,
SHOW_TABLETS_BELONG_COMMAND,
RECOVER_DATABASE_COMMAND,
RECOVER_TABLE_COMMAND,
RECOVER_PARTITION_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// 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.nereids.trees.plans.commands;

import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.Database;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.Table;
import org.apache.doris.catalog.TabletInvertedIndex;
import org.apache.doris.catalog.TabletMeta;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.Pair;
import org.apache.doris.common.util.DebugUtil;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.ShowResultSet;
import org.apache.doris.qe.ShowResultSetMetaData;
import org.apache.doris.qe.StmtExecutor;

import com.google.common.collect.ImmutableList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* show tablet belong command
*/
public class ShowTabletsBelongCommand extends ShowCommand {
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();
private final List<Long> tabletIds;

/**
* constructor
*/
public ShowTabletsBelongCommand(List<Long> tabletIds) {
super(PlanType.SHOW_TABLETS_BELONG_COMMAND);
this.tabletIds = tabletIds;
}

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 ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
//validation logic
// check auth
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR,
PrivPredicate.ADMIN.getPrivs().toString());
}
if (tabletIds == null || tabletIds.isEmpty()) {
throw new Exception("Please supply at least one tablet id");
}

// main logic.
List<List<String>> rows = new ArrayList<>();
Env env = Env.getCurrentEnv();
TabletInvertedIndex invertedIndex = Env.getCurrentInvertedIndex();
Map<Long, HashSet<Long>> tableToTabletIdsMap = new HashMap<>();
for (long tabletId : tabletIds) {
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<String> line = new ArrayList<>();
line.add(table.getDatabase().getFullName());
line.add(table.getName());

OlapTable olapTable = (OlapTable) table;
Pair<Double, String> 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<String> 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);
}
return new ShowResultSet(getMetaData(), rows);
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitShowTabletsBelongCommand(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowRolesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowStorageEnginesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTableIdCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTabletsBelongCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowViewCommand;
import org.apache.doris.nereids.trees.plans.commands.UnsetDefaultStorageVaultCommand;
Expand Down Expand Up @@ -323,4 +324,8 @@ default R visitShowTableIdCommand(ShowTableIdCommand showTableIdCommand, C conte
default R visitShowPrivilegesCommand(ShowPrivilegesCommand showPrivilegesCommand, C context) {
return visitCommand(showPrivilegesCommand, context);
}

default R visitShowTabletsBelongCommand(ShowTabletsBelongCommand showTabletBelongCommand, C context) {
return visitCommand(showTabletBelongCommand, context);
}
}
3 changes: 3 additions & 0 deletions regression-test/suites/auth_call/test_show_tablet_auth.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ suite("test_show_tablet_auth","p0,auth_call") {

def tablet_res = sql """SHOW TABLET ${res[0][0]}"""
assertTrue(tablet_res.size() == 1)

checkNereidsExecute("SHOW TABLETS BELONG ${res[0][0]}")
checkNereidsExecute("SHOW TABLETS BELONG 100,101")

tablet_res = sql """SHOW TABLETS BELONG ${res[0][0]}"""
assertTrue(tablet_res.size() == 1)
Expand Down

0 comments on commit 7518327

Please sign in to comment.