Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement][nereids]implement showTabletIdCommand in nereids #46321

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ supportedShowStatement
| SHOW REPLICA DISTRIBUTION FROM baseTableRef #showReplicaDistribution
| SHOW FULL? TRIGGERS ((FROM | IN) database=multipartIdentifier)? wildWhere? #showTriggers
| SHOW TABLET DIAGNOSIS tabletId=INTEGER_VALUE #showDiagnoseTablet
| SHOW TABLET tabletId=INTEGER_VALUE #showTabletId
| SHOW FRONTENDS name=identifier? #showFrontends
| SHOW DATABASE databaseId=INTEGER_VALUE #showDatabaseId
| SHOW TABLE tableId=INTEGER_VALUE #showTableId
Expand Down Expand Up @@ -368,7 +369,6 @@ unsupportedShowStatement
sortClause? propertyClause? #showData
| SHOW TEMPORARY? PARTITIONS FROM tableName=multipartIdentifier
wildWhere? sortClause? limitClause? #showPartitions
| SHOW TABLET tabletId=INTEGER_VALUE #showTabletId
| SHOW TABLETS FROM tableName=multipartIdentifier partitionSpec?
wildWhere? sortClause? limitClause? #showTabletsFromTable
| SHOW BACKUP ((FROM | IN) database=multipartIdentifier)? wildWhere? #showBackup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@
import org.apache.doris.nereids.DorisParser.SortClauseContext;
import org.apache.doris.nereids.DorisParser.SortItemContext;
import org.apache.doris.nereids.DorisParser.SpecifiedPartitionContext;
import org.apache.doris.nereids.DorisParser.ShowTabletIdContext;
import org.apache.doris.nereids.DorisParser.StarContext;
import org.apache.doris.nereids.DorisParser.StatementDefaultContext;
import org.apache.doris.nereids.DorisParser.StepPartitionDefContext;
Expand Down Expand Up @@ -499,6 +500,7 @@
import org.apache.doris.nereids.trees.plans.commands.AlterWorkloadPolicyCommand;
import org.apache.doris.nereids.trees.plans.commands.CallCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelExportCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTabletIdCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelJobTaskCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelLoadCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelMTMVTaskCommand;
Expand Down Expand Up @@ -4921,6 +4923,12 @@ public LogicalPlan visitAdminDiagnoseTablet(AdminDiagnoseTabletContext ctx) {
return new ShowDiagnoseTabletCommand(tabletId);
}

@Override
public LogicalPlan visitShowTabletId(DorisParser.ShowTabletIdContext ctx) {
long tabletId = Long.parseLong(ctx.INTEGER_VALUE().getText());
return new ShowTabletIdCommand(tabletId);
}

@Override
public LogicalPlan visitShowCreateTable(ShowCreateTableContext ctx) {
List<String> nameParts = visitMultipartIdentifier(ctx.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public enum PlanType {
SHOW_TABLE_ID_COMMAND,
SHOW_TRASH_COMMAND,
SHOW_TABLET_STORAGE_FORMAT_COMMAND,
SHOW_TABLET_ID_COMMAND,
SHOW_TRIGGERS_COMMAND,
SHOW_VARIABLES_COMMAND,
SHOW_AUTHORS_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// 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 com.google.common.collect.Lists;
import org.apache.doris.analysis.RedirectStatus;
import org.apache.doris.catalog.*;
import org.apache.doris.common.Config;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.FeConstants;
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 org.apache.doris.statistics.query.QueryStatsUtil;

import java.util.List;

/**
* show tablet id command
*/
public class ShowTabletIdCommand extends ShowCommand {
private final long tabletId;

/**
* constructor
*/
public ShowTabletIdCommand(long tabletId) {
super(PlanType.SHOW_TABLET_ID_COMMAND);
this.tabletId = tabletId;
}

/**
* get meta for show tabletId
*/
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("IndexName", ScalarType.createVarchar(30)));
builder.addColumn(new Column("DbId", ScalarType.createVarchar(30)));
builder.addColumn(new Column("TableId", ScalarType.createVarchar(30)));
builder.addColumn(new Column("PartitionId", ScalarType.createVarchar(30)));
builder.addColumn(new Column("IndexId", ScalarType.createVarchar(30)));
builder.addColumn(new Column("IsSync", ScalarType.createVarchar(30)));
builder.addColumn(new Column("Order", ScalarType.createVarchar(30)));
builder.addColumn(new Column("QueryHits", ScalarType.createVarchar(30)));
builder.addColumn(new Column("DetailCmd", ScalarType.createVarchar(30)));
return builder.build();
}

public List<List<String>> handleShowTablet() {

List<List<String>> rows = Lists.newArrayList();
TabletInvertedIndex invertedIndex = Env.getCurrentInvertedIndex();
TabletMeta tabletMeta = invertedIndex.getTabletMeta(tabletId);
Long dbId = tabletMeta != null ? tabletMeta.getDbId() : TabletInvertedIndex.NOT_EXIST_VALUE;
String dbName = FeConstants.null_string;
Long tableId = tabletMeta != null ? tabletMeta.getTableId() : TabletInvertedIndex.NOT_EXIST_VALUE;
String tableName = FeConstants.null_string;
Long partitionId = tabletMeta != null ? tabletMeta.getPartitionId() : TabletInvertedIndex.NOT_EXIST_VALUE;
String partitionName = FeConstants.null_string;
Long indexId = tabletMeta != null ? tabletMeta.getIndexId() : TabletInvertedIndex.NOT_EXIST_VALUE;
String indexName = FeConstants.null_string;
Boolean isSync = true;
long queryHits = 0L;

int tabletIdx = -1;
// check real meta
do {
Database db = Env.getCurrentEnv().getInternalCatalog().getDbNullable(dbId);
if (db == null) {
isSync = false;
break;
}
dbName = db.getFullName();
Table table = db.getTableNullable(tableId);
if (!(table instanceof OlapTable)) {
isSync = false;
break;
}
if (Config.enable_query_hit_stats) {
MaterializedIndex mi = ((OlapTable) table).getPartition(partitionId).getIndex(indexId);
if (mi != null) {
Tablet t = mi.getTablet(tabletId);
for (Replica r : t.getReplicas()) {
queryHits += QueryStatsUtil.getMergedReplicaStats(r.getId());
}
}
}

table.readLock();
try {
tableName = table.getName();
OlapTable olapTable = (OlapTable) table;
Partition partition = olapTable.getPartition(partitionId);
if (partition == null) {
isSync = false;
break;
}
partitionName = partition.getName();

MaterializedIndex index = partition.getIndex(indexId);
if (index == null) {
isSync = false;
break;
}
indexName = olapTable.getIndexNameById(indexId);

Tablet tablet = index.getTablet(tabletId);
if (tablet == null) {
isSync = false;
break;
}

tabletIdx = index.getTabletOrderIdx(tablet.getId());

List<Replica> replicas = tablet.getReplicas();
for (Replica replica : replicas) {
Replica tmp = invertedIndex.getReplica(tabletId, replica.getBackendIdWithoutException());
if (tmp == null) {
isSync = false;
break;
}
// use !=, not equals(), because this should be the same object.
if (tmp != replica) {
isSync = false;
break;
}
}

} finally {
table.readUnlock();
}
} while (false);

String detailCmd = String.format("SHOW PROC '/dbs/%d/%d/partitions/%d/%d/%d';",
dbId, tableId, partitionId, indexId, tabletId);
rows.add(Lists.newArrayList(dbName, tableName, partitionName, indexName,
dbId.toString(), tableId.toString(),
partitionId.toString(), indexId.toString(),
isSync.toString(), String.valueOf(tabletIdx), String.valueOf(queryHits), detailCmd));
return rows;
}


@Override
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
// check auth
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN");
deng-jeffer marked this conversation as resolved.
Show resolved Hide resolved
}

// Set the result set and send it using the executor
return new ShowResultSet(getMetaData(), handleShowTablet());
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitShowTabletIdCommand(this, context);
}

@Override
public RedirectStatus toRedirectStatus() {
if (ConnectContext.get().getSessionVariable().getForwardToMaster()) {
return RedirectStatus.FORWARD_NO_SYNC;
} else {
return RedirectStatus.NO_FORWARD;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowCreateRepositoryCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateTableCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateViewCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTabletIdCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDataSkewCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDataTypesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDatabaseIdCommand;
Expand Down Expand Up @@ -726,6 +727,10 @@ default R visitShowTabletStorageFormatCommand(ShowTabletStorageFormatCommand sho
return visitCommand(showTabletStorageFormatCommand, context);
}

default R visitShowTabletIdCommand(ShowTabletIdCommand showTabletIdCommand, C context) {
return visitCommand(showTabletIdCommand, context);
}

default R visitShowQueryProfileCommand(ShowQueryProfileCommand showQueryProfileCommand,
C context) {
return visitCommand(showQueryProfileCommand, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.

suite("test_nereids_show_tablet_id") {
String tableName = "test_tablet_id";
String tabletId = "";
try {
// Create a new table to test the SHOW TABLET command
sql "CREATE TABLE IF NOT EXISTS ${tableName} (id INT, name STRING) DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 3 PROPERTIES('replication_num'='1');"

// Extract tablet ID from the created table
def showTabletsResult = sql "SHOW TABLETS FROM ${tableName}"
assert showTabletsResult.size() > 0
tabletId = showTabletsResult[0][0] // Assuming the first tablet ID is used

// Execute the SHOW TABLET command and verify the output
checkNereidsExecute("SHOW TABLET ${tabletId}")
} catch (Exception e) {
log.error("Failed to execute SHOW TABLET command", e)
throw e
} finally {
try_sql("DROP TABLE IF EXISTS ${tableName}")
}
}

Loading