Skip to content

Commit

Permalink
[Enhancement] (nereids)implement showReplicaDistributionCommand in ne…
Browse files Browse the repository at this point in the history
…reids
  • Loading branch information
Vallishp committed Nov 23, 2024
1 parent 362efda commit 4b5b7ce
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ statementBase
| supportedShowStatement #supportedShowStatementAlias
| supportedRecoverStatement #supportedRecoverStatementAlias
| supportedLoadStatement #supportedLoadfStatementAlias
| supportedAdminStatement #supportedAdminStatementAlias
| unsupportedStatement #unsupported
;

Expand Down Expand Up @@ -227,6 +228,7 @@ supportedShowStatement
| SHOW CREATE MATERIALIZED VIEW mvName=identifier
ON tableName=multipartIdentifier #showCreateMaterializedView
| SHOW BACKENDS #showBackends
| SHOW REPLICA DISTRIBUTION FROM baseTableRef #showReplicaDistribution
| SHOW FULL? TRIGGERS ((FROM | IN) database=multipartIdentifier)? wildWhere? #showTriggers
| SHOW FRONTENDS name=identifier? #showFrontends
| SHOW TABLE tableId=INTEGER_VALUE #showTableId
Expand Down Expand Up @@ -343,7 +345,6 @@ unsupportedShowStatement
| SHOW (CLUSTERS | (COMPUTE GROUPS)) #showClusters
| SHOW CONVERT_LSC ((FROM | IN) database=multipartIdentifier)? #showConvertLsc
| SHOW REPLICA STATUS FROM baseTableRef wildWhere? #showReplicaStatus
| SHOW REPLICA DISTRIBUTION FROM baseTableRef #showREplicaDistribution
| SHOW TABLET STORAGE FORMAT VERBOSE? #showTabletStorageFormat
| SHOW TABLET DIAGNOSIS tabletId=INTEGER_VALUE #showDiagnoseTablet
| SHOW COPY ((FROM | IN) database=multipartIdentifier)?
Expand Down Expand Up @@ -464,6 +465,10 @@ unsupportedCancelStatement
| CANCEL WARM UP JOB wildWhere? #cancelWarmUp
;

supportedAdminStatement
: ADMIN SHOW REPLICA DISTRIBUTION FROM baseTableRef #adminShowReplicaDistribution
;

supportedRecoverStatement
: RECOVER DATABASE name=identifier id=INTEGER_VALUE? (AS alias=identifier)? #recoverDatabase
| RECOVER TABLE name=multipartIdentifier
Expand All @@ -474,7 +479,6 @@ supportedRecoverStatement

unsupportedAdminStatement
: ADMIN SHOW REPLICA STATUS FROM baseTableRef wildWhere? #adminShowReplicaStatus
| ADMIN SHOW REPLICA DISTRIBUTION FROM baseTableRef #adminShowReplicaDistribution
| ADMIN SET REPLICA STATUS PROPERTIES LEFT_PAREN propertyItemList RIGHT_PAREN #adminSetReplicaStatus
| ADMIN SET REPLICA VERSION PROPERTIES LEFT_PAREN propertyItemList RIGHT_PAREN #adminSetReplicaVersion
| ADMIN REPAIR TABLE baseTableRef #adminRepairTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public static List<List<String>> getTabletDistribution(ShowReplicaDistributionSt
return getTabletDistribution(stmt.getDbName(), stmt.getTblName(), stmt.getPartitionNames());
}

private static List<List<String>> getTabletDistribution(
public static List<List<String>> getTabletDistribution(
String dbName, String tblName, PartitionNames partitionNames)
throws DdlException {
DecimalFormat df = new DecimalFormat("00.00 %");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.analysis.ArithmeticExpr.Operator;
import org.apache.doris.analysis.BrokerDesc;
import org.apache.doris.analysis.ColumnNullableType;
import org.apache.doris.analysis.PartitionNames;
import org.apache.doris.analysis.PassVar;
import org.apache.doris.analysis.SetType;
import org.apache.doris.analysis.StorageBackend;
Expand Down Expand Up @@ -48,6 +49,7 @@
import org.apache.doris.mtmv.MTMVRefreshTriggerInfo;
import org.apache.doris.nereids.DorisParser;
import org.apache.doris.nereids.DorisParser.AddConstraintContext;
import org.apache.doris.nereids.DorisParser.AdminShowReplicaDistributionContext;
import org.apache.doris.nereids.DorisParser.AggClauseContext;
import org.apache.doris.nereids.DorisParser.AggStateDataTypeContext;
import org.apache.doris.nereids.DorisParser.AliasQueryContext;
Expand All @@ -61,6 +63,7 @@
import org.apache.doris.nereids.DorisParser.ArrayLiteralContext;
import org.apache.doris.nereids.DorisParser.ArrayRangeContext;
import org.apache.doris.nereids.DorisParser.ArraySliceContext;
import org.apache.doris.nereids.DorisParser.BaseTableRefContext;
import org.apache.doris.nereids.DorisParser.BitOperationContext;
import org.apache.doris.nereids.DorisParser.BooleanExpressionContext;
import org.apache.doris.nereids.DorisParser.BooleanLiteralContext;
Expand Down Expand Up @@ -144,6 +147,7 @@
import org.apache.doris.nereids.DorisParser.NamedExpressionContext;
import org.apache.doris.nereids.DorisParser.NamedExpressionSeqContext;
import org.apache.doris.nereids.DorisParser.NullLiteralContext;
import org.apache.doris.nereids.DorisParser.OptScanParamsContext;
import org.apache.doris.nereids.DorisParser.OutFileClauseContext;
import org.apache.doris.nereids.DorisParser.ParenthesizedExpressionContext;
import org.apache.doris.nereids.DorisParser.PartitionSpecContext;
Expand Down Expand Up @@ -175,6 +179,7 @@
import org.apache.doris.nereids.DorisParser.RefreshTriggerContext;
import org.apache.doris.nereids.DorisParser.RegularQuerySpecificationContext;
import org.apache.doris.nereids.DorisParser.RelationContext;
import org.apache.doris.nereids.DorisParser.RelationHintContext;
import org.apache.doris.nereids.DorisParser.ReplaceContext;
import org.apache.doris.nereids.DorisParser.ResumeMTMVContext;
import org.apache.doris.nereids.DorisParser.RollupDefContext;
Expand Down Expand Up @@ -221,6 +226,7 @@
import org.apache.doris.nereids.DorisParser.ShowPrivilegesContext;
import org.apache.doris.nereids.DorisParser.ShowProcContext;
import org.apache.doris.nereids.DorisParser.ShowProcedureStatusContext;
import org.apache.doris.nereids.DorisParser.ShowReplicaDistributionContext;
import org.apache.doris.nereids.DorisParser.ShowRepositoriesContext;
import org.apache.doris.nereids.DorisParser.ShowRolesContext;
import org.apache.doris.nereids.DorisParser.ShowSqlBlockRuleContext;
Expand All @@ -237,6 +243,7 @@
import org.apache.doris.nereids.DorisParser.SingleStatementContext;
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.StarContext;
import org.apache.doris.nereids.DorisParser.StatementDefaultContext;
import org.apache.doris.nereids.DorisParser.StepPartitionDefContext;
Expand All @@ -249,7 +256,9 @@
import org.apache.doris.nereids.DorisParser.SystemVariableContext;
import org.apache.doris.nereids.DorisParser.TableAliasContext;
import org.apache.doris.nereids.DorisParser.TableNameContext;
import org.apache.doris.nereids.DorisParser.TableSnapshotContext;
import org.apache.doris.nereids.DorisParser.TableValuedFunctionContext;
import org.apache.doris.nereids.DorisParser.TabletListContext;
import org.apache.doris.nereids.DorisParser.TimestampaddContext;
import org.apache.doris.nereids.DorisParser.TimestampdiffContext;
import org.apache.doris.nereids.DorisParser.TypeConstructorContext;
Expand Down Expand Up @@ -489,6 +498,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowPrivilegesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowProcCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowProcedureStatusCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowReplicaDistributionCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowRepositoriesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowRolesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowSqlBlockRuleCommand;
Expand Down Expand Up @@ -550,6 +560,7 @@
import org.apache.doris.nereids.trees.plans.commands.info.SimpleColumnDefinition;
import org.apache.doris.nereids.trees.plans.commands.info.StepPartition;
import org.apache.doris.nereids.trees.plans.commands.info.TableNameInfo;
import org.apache.doris.nereids.trees.plans.commands.info.TableRefInfo;
import org.apache.doris.nereids.trees.plans.commands.insert.BatchInsertIntoTableCommand;
import org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand;
import org.apache.doris.nereids.trees.plans.commands.insert.InsertOverwriteTableCommand;
Expand Down Expand Up @@ -4261,6 +4272,88 @@ public LogicalPlan visitShowProc(ShowProcContext ctx) {
return new ShowProcCommand(path);
}

private TableScanParams visitOptScanParamsContex(OptScanParamsContext ctx) {
if (ctx != null) {
Map<String, String> map = visitPropertyItemList(ctx.properties);
return new TableScanParams(ctx.funcName.getText(), map);
}
return null;
}

private TableSnapshot visitTableSnapshotContext(TableSnapshotContext ctx) {
if (ctx != null) {
if (ctx.TIME() != null) {
return new TableSnapshot(stripQuotes(ctx.time.getText()));
} else {
return new TableSnapshot(Long.parseLong(ctx.version.getText()));
}
}
return null;
}

private List<String> visitRelationHintContext(RelationHintContext ctx) {
final List<String> relationHints;
if (ctx != null) {
relationHints = typedVisit(ctx);
} else {
relationHints = ImmutableList.of();
}
return relationHints;
}

private PartitionNames visitSpecifiedPartitionContext(SpecifiedPartitionContext ctx) {
if (ctx != null) {
List<String> partitions = new ArrayList<>();
boolean isTempPart = ctx.TEMPORARY() != null;
if (ctx.identifier() != null) {
partitions.add(ctx.identifier().getText());
} else {
partitions.addAll(visitIdentifierList(ctx.identifierList()));
}
return new PartitionNames(isTempPart, partitions);
}
return null;
}

private List<Long> visitTabletListContext(TabletListContext ctx) {
List<Long> tabletIdList = new ArrayList<>();
if (ctx != null && ctx.tabletIdList != null) {
ctx.tabletIdList.stream().forEach(tabletToken -> {
tabletIdList.add(Long.parseLong(tabletToken.getText()));
});
}
return tabletIdList;
}

private TableRefInfo visitBaseTableRefContext(BaseTableRefContext ctx) {
List<String> nameParts = visitMultipartIdentifier(ctx.multipartIdentifier());
TableScanParams scanParams = visitOptScanParamsContex(ctx.optScanParams());
TableSnapshot tableSnapShot = visitTableSnapshotContext(ctx.tableSnapshot());
PartitionNames partitionNames = visitSpecifiedPartitionContext(ctx.specifiedPartition());
List<Long> tabletIdList = visitTabletListContext(ctx.tabletList());

String tableAlias = null;
if (ctx.tableAlias().strictIdentifier() != null) {
tableAlias = ctx.tableAlias().getText();
}
TableSample tableSample = ctx.sample() == null ? null : (TableSample) visit(ctx.sample());
List<String> hints = visitRelationHintContext(ctx.relationHint());
return new TableRefInfo(new TableNameInfo(nameParts), scanParams, tableSnapShot, partitionNames,
tabletIdList, tableAlias, tableSample, hints);
}

@Override
public LogicalPlan visitShowReplicaDistribution(ShowReplicaDistributionContext ctx) {
TableRefInfo tableRefInfo = visitBaseTableRefContext(ctx.baseTableRef());
return new ShowReplicaDistributionCommand(tableRefInfo);
}

@Override
public LogicalPlan visitAdminShowReplicaDistribution(AdminShowReplicaDistributionContext ctx) {
TableRefInfo tableRefInfo = visitBaseTableRefContext(ctx.baseTableRef());
return new ShowReplicaDistributionCommand(tableRefInfo);
}

@Override
public LogicalPlan visitShowCreateCatalog(ShowCreateCatalogContext ctx) {
return new ShowCreateCatalogCommand(ctx.identifier().getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public enum PlanType {
SHOW_PROC_COMMAND,
SHOW_PLUGINS_COMMAND,
SHOW_PRIVILEGES_COMMAND,
SHOW_REPLICA_DISTRIBUTION_COMMAND,
SHOW_REPOSITORIES_COMMAND,
SHOW_ROLE_COMMAND,
SHOW_STORAGE_ENGINES_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// 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.analysis.RedirectStatus;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.MetadataViewer;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.DdlException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.util.Util;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.commands.info.TableRefInfo;
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.List;

/**
* show replica distribution command
*/
public class ShowReplicaDistributionCommand extends ShowCommand {
public static final ImmutableList<String> TITLE_NAMES = new ImmutableList.Builder<String>()
.add("BackendId").add("ReplicaNum").add("ReplicaSize")
.add("NumGraph").add("NumPercent")
.add("SizeGraph").add("SizePercent")
.add("CloudClusterName").add("CloudClusterId")
.build();
private final TableRefInfo tableRefInfo;

/**
* constructor
*/

public ShowReplicaDistributionCommand(TableRefInfo tableRefInfo) {
super(PlanType.SHOW_REPLICA_DISTRIBUTION_COMMAND);
this.tableRefInfo = tableRefInfo;
}

@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");
}
tableRefInfo.analyze(ctx);
Util.prohibitExternalCatalog(tableRefInfo.getTableNameInfo().getCtl(), this.getClass().getSimpleName());

List<List<String>> results;
try {
results = MetadataViewer.getTabletDistribution(tableRefInfo.getTableNameInfo().getDb(),
tableRefInfo.getTableNameInfo().getTbl(), tableRefInfo.getPartitionNames());
} catch (DdlException e) {
throw new AnalysisException(e.getMessage());
}
return new ShowResultSet(getMetaData(), results);
}

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

public ShowResultSetMetaData getMetaData() {
ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder();
for (String title : TITLE_NAMES) {
builder.addColumn(new Column(title, ScalarType.createVarchar(30)));
}
return builder.build();
}

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

0 comments on commit 4b5b7ce

Please sign in to comment.