This is an automated email from the ASF dual-hosted git repository.

starocean999 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new cf5dd303275 [Enhancement] (nereids)implement showBackendsCommand in 
nereids (#43121)
cf5dd303275 is described below

commit cf5dd303275d212e26721efdb1c77120631fa5ed
Author: Vallish Pai <vallish...@gmail.com>
AuthorDate: Mon Nov 18 14:31:51 2024 +0530

    [Enhancement] (nereids)implement showBackendsCommand in nereids (#43121)
    
    Issue Number: close #42762
---
 .../antlr4/org/apache/doris/nereids/DorisParser.g4 |  2 +-
 .../doris/nereids/parser/LogicalPlanBuilder.java   |  7 ++
 .../apache/doris/nereids/trees/plans/PlanType.java |  1 +
 .../trees/plans/commands/ShowBackendsCommand.java  | 94 ++++++++++++++++++++++
 .../trees/plans/visitor/CommandVisitor.java        |  5 ++
 .../show/test_nereids_showbackends.groovy          | 22 +++++
 6 files changed, 130 insertions(+), 1 deletion(-)

diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 
b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
index c92afb1c6ea..b816b312194 100644
--- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
+++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
@@ -208,6 +208,7 @@ supportedShowStatement
     | SHOW STORAGE? ENGINES                                                    
     #showStorageEngines
     | SHOW CREATE MATERIALIZED VIEW mvName=identifier
         ON tableName=multipartIdentifier                                       
     #showCreateMaterializedView   
+    | SHOW BACKENDS                                                            
     #showBackends
     | SHOW FRONTENDS name=identifier?                                          
     #showFrontends 
     ;
 
@@ -304,7 +305,6 @@ unsupportedShowStatement
     | SHOW BROKER                                                              
     #showBroker
     | SHOW RESOURCES wildWhere? sortClause? limitClause?                       
     #showResources
     | SHOW WORKLOAD GROUPS wildWhere?                                          
     #showWorkloadGroups
-    | SHOW BACKENDS                                                            
     #showBackends
     | SHOW TRASH (ON backend=STRING_LITERAL)?                                  
     #showTrash
     | SHOW SNAPSHOT ON repo=identifier wildWhere?                              
     #showSnapshot
     | SHOW ALL? GRANTS                                                         
     #showGrants
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
index 4fe061b5468..59ae207a696 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
@@ -195,6 +195,7 @@ import 
org.apache.doris.nereids.DorisParser.SetUserPropertiesContext;
 import org.apache.doris.nereids.DorisParser.SetUserVariableContext;
 import org.apache.doris.nereids.DorisParser.SetVariableWithTypeContext;
 import org.apache.doris.nereids.DorisParser.ShowAuthorsContext;
+import org.apache.doris.nereids.DorisParser.ShowBackendsContext;
 import org.apache.doris.nereids.DorisParser.ShowConfigContext;
 import org.apache.doris.nereids.DorisParser.ShowConstraintContext;
 import org.apache.doris.nereids.DorisParser.ShowCreateMTMVContext;
@@ -437,6 +438,7 @@ import 
org.apache.doris.nereids.trees.plans.commands.SetOptionsCommand;
 import org.apache.doris.nereids.trees.plans.commands.SetTransactionCommand;
 import org.apache.doris.nereids.trees.plans.commands.SetUserPropertiesCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowAuthorsCommand;
+import org.apache.doris.nereids.trees.plans.commands.ShowBackendsCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowConfigCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand;
@@ -4085,6 +4087,11 @@ public class LogicalPlanBuilder extends 
DorisParserBaseVisitor<Object> {
         return new ShowViewCommand(databaseName, new 
TableNameInfo(tableNameParts));
     }
 
+    @Override
+    public LogicalPlan visitShowBackends(ShowBackendsContext ctx) {
+        return new ShowBackendsCommand();
+    }
+
     @Override
     public LogicalPlan visitShowRepositories(ShowRepositoriesContext ctx) {
         return new ShowRepositoriesCommand();
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java
index 576bd734a04..768ef8ca723 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java
@@ -176,6 +176,7 @@ public enum PlanType {
     SET_DEFAULT_STORAGE_VAULT_COMMAND,
     PREPARED_COMMAND,
     EXECUTE_COMMAND,
+    SHOW_BACKENDS_COMMAND,
     SHOW_CONFIG_COMMAND,
     SHOW_CREATE_MATERIALIZED_VIEW_COMMAND,
     SHOW_FRONTENDS_COMMAND,
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowBackendsCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowBackendsCommand.java
new file mode 100644
index 00000000000..9119e8490f4
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowBackendsCommand.java
@@ -0,0 +1,94 @@
+// 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.ScalarType;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.proc.BackendsProcDir;
+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.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * show backends command
+ */
+public class ShowBackendsCommand extends ShowCommand {
+    public static final Logger LOG = 
LogManager.getLogger(ShowBackendsCommand.class);
+
+    /**
+     * constructor
+     */
+    public ShowBackendsCommand() {
+        super(PlanType.SHOW_BACKENDS_COMMAND);
+    }
+
+    public ShowResultSetMetaData getMetaData() {
+        ShowResultSetMetaData.Builder builder = 
ShowResultSetMetaData.builder();
+        for (String title : BackendsProcDir.TITLE_NAMES) {
+            builder.addColumn(new Column(title, ScalarType.createVarchar(30)));
+        }
+        return builder.build();
+    }
+
+    @Override
+    public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) 
throws Exception {
+        if 
(!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), 
PrivPredicate.ADMIN)
+                && 
!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(),
+                                                                          
PrivPredicate.OPERATOR)) {
+            
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, 
"ADMIN/OPERATOR");
+        }
+
+        List<List<String>> backendInfos = BackendsProcDir.getBackendInfos();
+        backendInfos.sort(new Comparator<List<String>>() {
+            @Override
+            public int compare(List<String> o1, List<String> o2) {
+                return Long.compare(Long.parseLong(o1.get(0)), 
Long.parseLong(o2.get(0)));
+            }
+        });
+
+        return new ShowResultSet(getMetaData(), backendInfos);
+    }
+
+    @Override
+    public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
+        return visitor.visitShowBackendsCommand(this, context);
+    }
+
+    @Override
+    public RedirectStatus toRedirectStatus() {
+        if (ConnectContext.get().getSessionVariable().getForwardToMaster()) {
+            return RedirectStatus.FORWARD_NO_SYNC;
+        } else {
+            return RedirectStatus.NO_FORWARD;
+        }
+    }
+}
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java
index 421fdeb4c24..3b5ee877e8b 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java
@@ -51,6 +51,7 @@ import 
org.apache.doris.nereids.trees.plans.commands.SetOptionsCommand;
 import org.apache.doris.nereids.trees.plans.commands.SetTransactionCommand;
 import org.apache.doris.nereids.trees.plans.commands.SetUserPropertiesCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowAuthorsCommand;
+import org.apache.doris.nereids.trees.plans.commands.ShowBackendsCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowConfigCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand;
@@ -262,6 +263,10 @@ public interface CommandVisitor<R, C> {
         return visitCommand(showViewCommand, context);
     }
 
+    default R visitShowBackendsCommand(ShowBackendsCommand 
showBackendsCommand, C context) {
+        return visitCommand(showBackendsCommand, context);
+    }
+
     default R visitShowRepositoriesCommand(ShowRepositoriesCommand 
showRepositoriesCommand, C context) {
         return visitCommand(showRepositoriesCommand, context);
     }
diff --git 
a/regression-test/suites/nereids_p0/show/test_nereids_showbackends.groovy 
b/regression-test/suites/nereids_p0/show/test_nereids_showbackends.groovy
new file mode 100644
index 00000000000..66980ab0e61
--- /dev/null
+++ b/regression-test/suites/nereids_p0/show/test_nereids_showbackends.groovy
@@ -0,0 +1,22 @@
+// 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_showbackends") {
+    checkNereidsExecute("""show backends;""")
+}
+


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to