LiBinfeng-01 commented on code in PR #44861:
URL: https://github.com/apache/doris/pull/44861#discussion_r1866978852


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminCompactTableCommand.java:
##########
@@ -0,0 +1,126 @@
+// 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.Env;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.util.Util;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.nereids.trees.expressions.EqualTo;
+import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
+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.StmtExecutor;
+
+import java.util.List;
+
+/**
+ * AdminCompactTableCommand
+ */
+public class AdminCompactTableCommand extends Command {

Review Comment:
   done



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminCompactTableCommand.java:
##########
@@ -0,0 +1,126 @@
+// 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.Env;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.util.Util;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.nereids.trees.expressions.EqualTo;
+import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
+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.StmtExecutor;
+
+import java.util.List;
+
+/**
+ * AdminCompactTableCommand
+ */
+public class AdminCompactTableCommand extends Command {
+
+    private TableRefInfo tableRefInfo;
+    private EqualTo where;
+
+    /**
+     * compact type
+     */
+    public enum CompactionType {
+        CUMULATIVE,
+        BASE
+    }
+
+    private CompactionType typeFilter;
+
+    public AdminCompactTableCommand(TableRefInfo tableRefInfo, EqualTo where) {
+        super(PlanType.ADD_CONSTRAINT_COMMAND);
+        this.tableRefInfo = tableRefInfo;
+        this.where = where;
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws 
Exception {
+        validate(ctx);
+        String dbName = tableRefInfo.getTableNameInfo().getDb();
+        String tableName = tableRefInfo.getTableNameInfo().getTbl();
+        String type = getCompactionType();
+        List<String> partitionNames = 
tableRefInfo.getPartitionNamesInfo().getPartitionNames();
+        ctx.getEnv().compactTable(dbName, tableName, type, partitionNames);
+    }
+
+    private void validate(ConnectContext ctx) throws UserException {
+        // 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<String> partitionNames = 
tableRefInfo.getPartitionNamesInfo().getPartitionNames();
+        if (partitionNames != null) {
+            if (partitionNames.size() != 1) {
+                throw new AnalysisException("Only support single partition for 
compaction");
+            }
+        } else {
+            throw new AnalysisException("No partition selected for 
compaction");
+        }
+
+        // analyze where clause if not null
+        if (where == null) {
+            throw new AnalysisException("Compaction type must be specified in"
+                + " Where clause like: type = 'BASE/CUMULATIVE'");
+        }
+
+        if (!analyzeWhere()) {
+            throw new AnalysisException(
+                "Where clause should looks like: type = 'BASE/CUMULATIVE'");
+        }
+    }
+
+    private boolean analyzeWhere() {
+        try {
+            typeFilter = CompactionType.valueOf(((StringLiteral) 
where.right()).getStringValue().toUpperCase());
+        } catch (Exception e) {
+            return false;
+        }
+
+        if (typeFilter == null || (typeFilter != CompactionType.CUMULATIVE && 
typeFilter != CompactionType.BASE)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
+        return visitor.visitAdminCompactTableCommand(this, context);
+    }
+
+    private String getCompactionType() {
+        if (typeFilter == CompactionType.CUMULATIVE) {
+            return "cumulative";
+        } else {
+            return "base";
+        }
+    }
+}

Review Comment:
   done



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminShowReplicaStatusCommand.java:
##########
@@ -0,0 +1,154 @@
+// 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.Env;
+import org.apache.doris.catalog.MetadataViewer;
+import org.apache.doris.catalog.Replica;
+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.UserException;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.trees.expressions.EqualTo;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.Not;
+import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral;
+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;
+
+/**
+ * ShowReplicaStatusCommand
+ */
+public class AdminShowReplicaStatusCommand extends ShowCommand {
+    public static final ImmutableList<String> TITLE_NAMES = new 
ImmutableList.Builder<String>()
+            
.add("TabletId").add("ReplicaId").add("BackendId").add("Version").add("LastFailedVersion")
+            
.add("LastSuccessVersion").add("CommittedVersion").add("SchemaHash").add("VersionNum")
+            .add("IsBad").add("IsUserDrop").add("State").add("Status")
+            .build();
+
+    private TableRefInfo tableRefInfo;
+    private Expression where;
+
+    private Replica.ReplicaStatus statusFilter;
+
+    public AdminShowReplicaStatusCommand(TableRefInfo tableRefInfo, Expression 
where) {
+        super(PlanType.SHOW_REPLICA_STATUS_COMMAND);
+        this.tableRefInfo = tableRefInfo;
+        this.where = where;
+    }
+
+    @Override
+    public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) 
throws Exception {
+        validate(ctx);
+        List<List<String>> results;
+        try {
+            results = 
MetadataViewer.getTabletStatus(tableRefInfo.getTableNameInfo().getDb(),
+                tableRefInfo.getTableNameInfo().getTbl(),
+                tableRefInfo.getPartitionNamesInfo().getPartitionNames(), 
statusFilter, where);
+        } catch (DdlException e) {
+            throw new AnalysisException(e.getMessage());
+        }
+        return new ShowResultSet(getMetaData(), results);
+    }
+
+    public ShowResultSetMetaData getMetaData() {
+        ShowResultSetMetaData.Builder builder = 
ShowResultSetMetaData.builder();
+        for (String title : TITLE_NAMES) {
+            builder.addColumn(new Column(title, ScalarType.createVarchar(30)));
+        }
+        return builder.build();
+    }
+
+    /**
+     * validate for show replica
+     */
+    public void validate(ConnectContext ctx) throws AnalysisException, 
UserException {
+        // check auth
+        if 
(!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), 
PrivPredicate.ADMIN)) {
+            
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, 
"ADMIN");
+        }
+
+        // bind relation
+        tableRefInfo.analyze(ctx);
+
+        if (!validateWhere()) {
+            throw new AnalysisException(
+                "Where clause should looks like: status =/!= 
'OK/DEAD/VERSION_ERROR/SCHEMA_ERROR/MISSING'");
+        }
+    }
+
+    private boolean validateWhere() throws AnalysisException {
+        // analyze where clause if not null
+        if (where == null) {
+            return true;
+        }
+
+        if (!(where instanceof EqualTo || (where instanceof Not && 
where.child(0) instanceof EqualTo))) {
+            return false;
+        }
+
+        EqualTo equalTo = where instanceof EqualTo ? (EqualTo) where : 
(EqualTo) ((Not) where).child();
+
+        Expression leftChild = equalTo.child(0);
+        if (!(leftChild instanceof UnboundSlot)) {
+            return false;
+        }
+
+        String leftKey = ((UnboundSlot) leftChild).getName();
+        if (!leftKey.equalsIgnoreCase("status")) {
+            return false;
+        }
+
+        Expression rightChild = equalTo.child(1);
+        if (!(rightChild instanceof StringLikeLiteral)) {
+            return false;
+        }
+
+        try {
+            statusFilter = Replica.ReplicaStatus
+                    .valueOf(((StringLikeLiteral) 
rightChild).getStringValue().toUpperCase());
+        } catch (Exception e) {
+            return false;
+        }
+
+        if (statusFilter == null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
+        return visitor.visitAdminShowReplicaStatusCommand(this, context);
+    }
+}

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to