Skip to content

Commit

Permalink
[Enhancement] (nereids)implement showSqlBlockRuleCommand in nereids
Browse files Browse the repository at this point in the history
  • Loading branch information
Vallishp committed Nov 17, 2024
1 parent 13ced20 commit b2d9d17
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ supportedShowStatement
| SHOW ROLES #showRoles
| SHOW PROC path=STRING_LITERAL #showProc
| SHOW STORAGE? ENGINES #showStorageEngines
| SHOW SQL_BLOCK_RULE (FOR ruleName=identifier)? #showSqlBlockRule
| SHOW CREATE MATERIALIZED VIEW mvName=identifier
ON tableName=multipartIdentifier #showCreateMaterializedView
| SHOW FRONTENDS name=identifier? #showFrontends
Expand Down Expand Up @@ -240,8 +241,7 @@ lockTable


unsupportedShowStatement
: SHOW SQL_BLOCK_RULE (FOR ruleName=identifier)? #showSqlBlockRule
| SHOW ROW POLICY (FOR (userIdentify | (ROLE role=identifier)))? #showRowPolicy
: SHOW ROW POLICY (FOR (userIdentify | (ROLE role=identifier)))? #showRowPolicy
| SHOW STORAGE POLICY (USING (FOR policy=identifierOrText)?)? #showStoragePolicy
| SHOW STAGES #showStages
| SHOW STORAGE (VAULT | VAULTS) #showStorageVault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public boolean existRule(String name) {
**/
public List<SqlBlockRule> getSqlBlockRule(ShowSqlBlockRuleStmt stmt) throws AnalysisException {
String ruleName = stmt.getRuleName();
return getSqlBlockRule(ruleName);
}

/**
* Get SqlBlockRule by rulename.
**/
public List<SqlBlockRule> getSqlBlockRule(String ruleName) throws AnalysisException {
if (StringUtils.isNotEmpty(ruleName)) {
if (nameToSqlBlockRuleMap.containsKey(ruleName)) {
SqlBlockRule sqlBlockRule = nameToSqlBlockRuleMap.get(ruleName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
import org.apache.doris.nereids.DorisParser.ShowProcedureStatusContext;
import org.apache.doris.nereids.DorisParser.ShowRepositoriesContext;
import org.apache.doris.nereids.DorisParser.ShowRolesContext;
import org.apache.doris.nereids.DorisParser.ShowSqlBlockRuleContext;
import org.apache.doris.nereids.DorisParser.ShowStorageEnginesContext;
import org.apache.doris.nereids.DorisParser.ShowVariablesContext;
import org.apache.doris.nereids.DorisParser.ShowViewContext;
Expand Down Expand Up @@ -448,6 +449,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowProcedureStatusCommand;
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;
import org.apache.doris.nereids.trees.plans.commands.ShowStorageEnginesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowViewCommand;
Expand Down Expand Up @@ -4085,6 +4087,15 @@ public ShowViewCommand visitShowView(ShowViewContext ctx) {
return new ShowViewCommand(databaseName, new TableNameInfo(tableNameParts));
}

@Override
public LogicalPlan visitShowSqlBlockRule(ShowSqlBlockRuleContext ctx) {
String ruleName = null;
if (ctx.ruleName != null) {
ruleName = ctx.ruleName.getText();
}
return new ShowSqlBlockRuleCommand(ruleName);
}

@Override
public LogicalPlan visitShowRepositories(ShowRepositoriesContext ctx) {
return new ShowRepositoriesCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public enum PlanType {
SET_DEFAULT_STORAGE_VAULT_COMMAND,
PREPARED_COMMAND,
EXECUTE_COMMAND,
SHOW_BLOCK_RULE_COMMAND,
SHOW_CONFIG_COMMAND,
SHOW_CREATE_MATERIALIZED_VIEW_COMMAND,
SHOW_FRONTENDS_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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.blockrule.SqlBlockRule;
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.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 com.google.common.collect.Lists;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

/**
* show sql block rule command
*/
public class ShowSqlBlockRuleCommand extends ShowCommand {
public static final Logger LOG = LogManager.getLogger(ShowSqlBlockRuleCommand.class);
private static final ShowResultSetMetaData META_DATA =
ShowResultSetMetaData.builder()
.addColumn(new Column("Name", ScalarType.createVarchar(50)))
.addColumn(new Column("Sql", ScalarType.createVarchar(65535)))
.addColumn(new Column("SqlHash", ScalarType.createVarchar(65535)))
.addColumn(new Column("PartitionNum", ScalarType.createVarchar(10)))
.addColumn(new Column("TabletNum", ScalarType.createVarchar(10)))
.addColumn(new Column("Cardinality", ScalarType.createVarchar(20)))
.addColumn(new Column("Global", ScalarType.createVarchar(4)))
.addColumn(new Column("Enable", ScalarType.createVarchar(4)))
.build();
private final String ruleName; // optional

/**
* constructor
*/
public ShowSqlBlockRuleCommand(String ruleName) {
super(PlanType.SHOW_BLOCK_RULE_COMMAND);
this.ruleName = ruleName;
}

@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");
}

List<List<String>> rows = Lists.newArrayList();
List<SqlBlockRule> sqlBlockRules = Env.getCurrentEnv().getSqlBlockRuleMgr().getSqlBlockRule(ruleName);
sqlBlockRules.forEach(rule -> rows.add(rule.getShowInfo()));
return new ShowResultSet(META_DATA, rows);
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitShowSqlBlockRuleCommand(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowProcedureStatusCommand;
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;
import org.apache.doris.nereids.trees.plans.commands.ShowStorageEnginesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowViewCommand;
Expand Down Expand Up @@ -262,6 +263,10 @@ default R visitShowViewCommand(ShowViewCommand showViewCommand, C context) {
return visitCommand(showViewCommand, context);
}

default R visitShowSqlBlockRuleCommand(ShowSqlBlockRuleCommand showblockruleCommand, C context) {
return visitCommand(showblockruleCommand, context);
}

default R visitShowRepositoriesCommand(ShowRepositoriesCommand showRepositoriesCommand, C context) {
return visitCommand(showRepositoriesCommand, context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select --
-- !select1 --
test_rule_sql SELECT abcd FROM table_2 NULL 0 0 0 true true

-- !select2 --
test_rule_sql SELECT abcd FROM table_2 NULL 0 0 0 true true

-- !select3 --

Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ suite("test_sql_block_rule", "nonConcurrent") {
exception "sql match regex sql block rule: test_rule_sql"
}

checkNereidsExecute("SHOW SQL_BLOCK_RULE")

qt_select1 """
SHOW SQL_BLOCK_RULE
"""

qt_select2 """
SHOW SQL_BLOCK_RULE FOR test_rule_sql
"""

sql """
DROP SQL_BLOCK_RULE if exists test_rule_sql
"""
Expand All @@ -100,7 +110,7 @@ suite("test_sql_block_rule", "nonConcurrent") {
exception "sql hits sql block rule: test_rule_num, reach tablet_num : 1"
}
*/
qt_select """
qt_select3 """
SHOW SQL_BLOCK_RULE
"""

Expand Down

0 comments on commit b2d9d17

Please sign in to comment.