Skip to content

Commit

Permalink
[Enhancement] (nereids)implement showProcCommand in nereids (#43146)
Browse files Browse the repository at this point in the history
Issue Number: close #42750
  • Loading branch information
Vallishp authored Nov 13, 2024
1 parent 0d7b63f commit a8de28c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ supportedShowStatement
(FROM |IN) tableName=multipartIdentifier
((FROM | IN) database=identifier)? #showView
| SHOW ROLES #showRoles
| SHOW PROC path=STRING_LITERAL #showProc
;

unsupportedOtherStatement
Expand Down Expand Up @@ -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)?
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<String>> 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, C> R accept(PlanVisitor<R, C> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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';""")
}

0 comments on commit a8de28c

Please sign in to comment.