From a8de28c783aca6ffdaa68e041e149bbc0b0b3750 Mon Sep 17 00:00:00 2001 From: Vallish Pai Date: Wed, 13 Nov 2024 12:58:22 +0530 Subject: [PATCH] [Enhancement] (nereids)implement showProcCommand in nereids (#43146) Issue Number: close #42750 --- .../org/apache/doris/nereids/DorisParser.g4 | 4 +- .../nereids/parser/LogicalPlanBuilder.java | 8 ++ .../doris/nereids/trees/plans/PlanType.java | 1 + .../trees/plans/commands/ShowProcCommand.java | 106 ++++++++++++++++++ .../trees/plans/visitor/CommandVisitor.java | 5 + .../ddl/show/show_proc_nereids.groovy | 22 ++++ 6 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcCommand.java create mode 100644 regression-test/suites/nereids_p0/ddl/show/show_proc_nereids.groovy 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 4da0078ce1e6d6..8eedc739f28ac1 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 @@ -197,6 +197,7 @@ supportedShowStatement (FROM |IN) tableName=multipartIdentifier ((FROM | IN) database=identifier)? #showView | SHOW ROLES #showRoles + | SHOW PROC path=STRING_LITERAL #showProc ; unsupportedOtherStatement @@ -264,7 +265,6 @@ unsupportedShowStatement ((FROM | IN) database=multipartIdentifier)? wildWhere? #showColumns | SHOW COLLATION wildWhere? #showCollation | SHOW ((CHAR SET) | CHARSET) wildWhere? #showCharset - | SHOW PROC path=STRING_LITERAL #showProc | SHOW COUNT LEFT_PAREN ASTERISK RIGHT_PAREN (WARNINGS | ERRORS) #showWaringErrorCount | SHOW (WARNINGS | ERRORS) limitClause? #showWaringErrors | SHOW LOAD WARNINGS ((((FROM | IN) database=multipartIdentifier)? @@ -317,7 +317,7 @@ unsupportedShowStatement | SHOW ENCRYPTKEYS ((FROM | IN) database=multipartIdentifier)? wildWhere? #showEncryptKeys | SHOW SYNC JOB ((FROM | IN) database=multipartIdentifier)? #showSyncJob | SHOW TABLE CREATION ((FROM | IN) database=multipartIdentifier)? wildWhere? #showTableCreation - | SHOW LAST INSERT #showLastInsert + | SHOW LAST INSERT #showLastInsert | SHOW CREATE MATERIALIZED VIEW mvName=identifier ON tableName=multipartIdentifier #showCreateMaterializedView | SHOW CATALOG RECYCLE BIN wildWhere? #showCatalogRecycleBin 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 02cb23e4269632..156df4333d5f28 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 @@ -196,6 +196,7 @@ import org.apache.doris.nereids.DorisParser.ShowConstraintContext; import org.apache.doris.nereids.DorisParser.ShowCreateMTMVContext; import org.apache.doris.nereids.DorisParser.ShowCreateProcedureContext; +import org.apache.doris.nereids.DorisParser.ShowProcContext; import org.apache.doris.nereids.DorisParser.ShowProcedureStatusContext; import org.apache.doris.nereids.DorisParser.ShowRolesContext; import org.apache.doris.nereids.DorisParser.ShowVariablesContext; @@ -429,6 +430,7 @@ import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand; import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand; +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.ShowRolesCommand; import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand; @@ -4066,4 +4068,10 @@ public ShowViewCommand visitShowView(ShowViewContext ctx) { public LogicalPlan visitShowRoles(ShowRolesContext ctx) { return new ShowRolesCommand(); } + + @Override + public LogicalPlan visitShowProc(ShowProcContext ctx) { + String path = stripQuotes(ctx.path.getText()); + return new ShowProcCommand(path); + } } 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 dde7acedecdcec..b3c402daa9a1f2 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 { PREPARED_COMMAND, EXECUTE_COMMAND, SHOW_CONFIG_COMMAND, + SHOW_PROC_COMMAND, SHOW_ROLE_COMMAND, SHOW_VARIABLES_COMMAND, SHOW_AUTHORS_COMMAND, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcCommand.java new file mode 100644 index 00000000000000..da2fb38c4d8d80 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcCommand.java @@ -0,0 +1,106 @@ +// 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.AnalysisException; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; +import org.apache.doris.common.proc.ProcNodeInterface; +import org.apache.doris.common.proc.ProcResult; +import org.apache.doris.common.proc.ProcService; +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.List; + +/** + * show proc command + */ +public class ShowProcCommand extends ShowCommand { + public static final Logger LOG = LogManager.getLogger(ShowProcCommand.class); + private final String path; + + /** + * constructor + */ + public ShowProcCommand(String path) { + super(PlanType.SHOW_PROC_COMMAND); + this.path = path; + } + + /** + * get meta for show proc + */ + public ShowResultSetMetaData getMetaData(ProcNodeInterface procNode) { + ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder(); + ProcResult result = null; + try { + result = procNode.fetchResult(); + } catch (AnalysisException e) { + return builder.build(); + } + + for (String col : result.getColumnNames()) { + builder.addColumn(new Column(col, ScalarType.createVarchar(30))); + } + return builder.build(); + } + + private ShowResultSet handleShowProc(ConnectContext ctx, StmtExecutor executor) throws Exception { + if (!Env.getCurrentEnv().getAccessManager() + .checkGlobalPriv(ctx, PrivPredicate.ADMIN_OR_NODE)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN"); + } + + ProcNodeInterface procNode = ProcService.getInstance().open(path); + List> finalRows = procNode.fetchResult().getRows(); + ShowResultSet resultSet = new ShowResultSet(getMetaData(procNode), finalRows); + return resultSet; + } + + @Override + public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception { + return handleShowProc(ctx, executor); + } + + @Override + public R accept(PlanVisitor visitor, C context) { + return visitor.visitShowProcCommand(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 16e07e83589ee0..7d879a8efdf5a3 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 @@ -52,6 +52,7 @@ import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand; import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand; +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.ShowRolesCommand; import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand; @@ -252,4 +253,8 @@ default R visitShowViewCommand(ShowViewCommand showViewCommand, C context) { default R visitShowRolesCommand(ShowRolesCommand showRolesCommand, C context) { return visitCommand(showRolesCommand, context); } + + default R visitShowProcCommand(ShowProcCommand showProcCommand, C context) { + return visitCommand(showProcCommand, context); + } } diff --git a/regression-test/suites/nereids_p0/ddl/show/show_proc_nereids.groovy b/regression-test/suites/nereids_p0/ddl/show/show_proc_nereids.groovy new file mode 100644 index 00000000000000..7872835ed9b732 --- /dev/null +++ b/regression-test/suites/nereids_p0/ddl/show/show_proc_nereids.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("show_proc_nereids") { + // can not use qt command since the output change based on cluster and backend ip + checkNereidsExecute("""show proc '/diagnose/cluster_balance';""") + checkNereidsExecute("""show proc '/backends';""") +} \ No newline at end of file