From e54c5de220478565e85abf43e18ffadd5223b215 Mon Sep 17 00:00:00 2001 From: Vallish Pai Date: Wed, 20 Nov 2024 15:00:27 +0530 Subject: [PATCH] [Enhancement] (nereids)implement DropSqlBlockRuleCommand in nereids (#44302) Issue Number: close #42624 --- .../org/apache/doris/nereids/DorisParser.g4 | 2 +- .../doris/blockrule/SqlBlockRuleMgr.java | 7 ++- .../nereids/parser/LogicalPlanBuilder.java | 7 +++ .../doris/nereids/trees/plans/PlanType.java | 1 + .../commands/DropSqlBlockRuleCommand.java | 60 +++++++++++++++++++ .../trees/plans/visitor/CommandVisitor.java | 5 ++ .../sql_block_rule_p0/test_sql_block_rule.out | 8 ++- .../test_sql_block_rule.groovy | 23 ++++++- 8 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropSqlBlockRuleCommand.java 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 948b0d5599f86d..97932c3bb2afd9 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 @@ -193,6 +193,7 @@ supportedAlterStatement supportedDropStatement : DROP CATALOG RECYCLE BIN WHERE idType=STRING_LITERAL EQ id=INTEGER_VALUE #dropCatalogRecycleBin | DROP ROLE (IF EXISTS)? name=identifier #dropRole + | DROP SQL_BLOCK_RULE (IF EXISTS)? identifierSeq #dropSqlBlockRule ; supportedShowStatement @@ -671,7 +672,6 @@ unsupportedDropStatement | DROP WORKLOAD GROUP (IF EXISTS)? name=identifierOrText #dropWorkloadGroup | DROP WORKLOAD POLICY (IF EXISTS)? name=identifierOrText #dropWorkloadPolicy | DROP ENCRYPTKEY (IF EXISTS)? name=multipartIdentifier #dropEncryptkey - | DROP SQL_BLOCK_RULE (IF EXISTS)? identifierSeq #dropSqlBlockRule | DROP ROW POLICY (IF EXISTS)? policyName=identifier ON tableName=multipartIdentifier (FOR (userIdentify | ROLE roleName=identifier))? #dropRowPolicy diff --git a/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java b/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java index c6dd0af42109eb..aa3b844f3e38ef 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java @@ -203,12 +203,15 @@ private void unprotectedAdd(SqlBlockRule sqlBlockRule) { * Drop SqlBlockRule for drop stmt. **/ public void dropSqlBlockRule(DropSqlBlockRuleStmt stmt) throws DdlException { + dropSqlBlockRule(stmt.getRuleNames(), stmt.isIfExists()); + } + + public void dropSqlBlockRule(List ruleNames, boolean isIfExists) throws DdlException { writeLock(); try { - List ruleNames = stmt.getRuleNames(); for (String ruleName : ruleNames) { if (!existRule(ruleName)) { - if (stmt.isIfExists()) { + if (isIfExists) { continue; } throw new DdlException("the sql block rule " + ruleName + " not exist"); 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 6b5e0b129c07e8..36b25e474df745 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 @@ -101,6 +101,7 @@ import org.apache.doris.nereids.DorisParser.DropMTMVContext; import org.apache.doris.nereids.DorisParser.DropProcedureContext; import org.apache.doris.nereids.DorisParser.DropRoleContext; +import org.apache.doris.nereids.DorisParser.DropSqlBlockRuleContext; import org.apache.doris.nereids.DorisParser.ElementAtContext; import org.apache.doris.nereids.DorisParser.ExceptContext; import org.apache.doris.nereids.DorisParser.ExceptOrReplaceContext; @@ -438,6 +439,7 @@ import org.apache.doris.nereids.trees.plans.commands.DropMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.DropProcedureCommand; import org.apache.doris.nereids.trees.plans.commands.DropRoleCommand; +import org.apache.doris.nereids.trees.plans.commands.DropSqlBlockRuleCommand; import org.apache.doris.nereids.trees.plans.commands.ExplainCommand; import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel; import org.apache.doris.nereids.trees.plans.commands.ExportCommand; @@ -4248,6 +4250,11 @@ public LogicalPlan visitDropRole(DropRoleContext ctx) { return new DropRoleCommand(ctx.name.getText(), ctx.EXISTS() != null); } + @Override + public LogicalPlan visitDropSqlBlockRule(DropSqlBlockRuleContext ctx) { + return new DropSqlBlockRuleCommand(visitIdentifierSeq(ctx.identifierSeq()), ctx.EXISTS() != null); + } + @Override public LogicalPlan visitShowTableId(ShowTableIdContext ctx) { long tableId = -1; 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 06a32bb31020c7..e040d18b82f4fa 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 @@ -177,6 +177,7 @@ public enum PlanType { REFRESH_CATALOG_COMMAND, PREPARED_COMMAND, EXECUTE_COMMAND, + DROP_SQL_BLOCK_RULE_COMMAND, SHOW_BACKENDS_COMMAND, SHOW_BLOCK_RULE_COMMAND, SHOW_CONFIG_COMMAND, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropSqlBlockRuleCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropSqlBlockRuleCommand.java new file mode 100644 index 00000000000000..37ce9104a44d07 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropSqlBlockRuleCommand.java @@ -0,0 +1,60 @@ +// 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.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.StmtExecutor; + +import java.util.List; + +/** + * drop sql block rule command + */ +public class DropSqlBlockRuleCommand extends DropCommand { + private final boolean ifExists; + private List ruleNames; + + /** + * constructor + */ + public DropSqlBlockRuleCommand(List ruleNames, boolean ifExists) { + super(PlanType.DROP_SQL_BLOCK_RULE_COMMAND); + this.ruleNames = ruleNames; + this.ifExists = ifExists; + } + + @Override + public void 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"); + } + Env.getCurrentEnv().getSqlBlockRuleMgr().dropSqlBlockRule(ruleNames, ifExists); + } + + @Override + public R accept(PlanVisitor visitor, C context) { + return visitor.visitDropSqlBlockRuleCommand(this, context); + } +} 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 5b77c5b5be019e..9bffb5a376e45e 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 @@ -38,6 +38,7 @@ import org.apache.doris.nereids.trees.plans.commands.DropMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.DropProcedureCommand; import org.apache.doris.nereids.trees.plans.commands.DropRoleCommand; +import org.apache.doris.nereids.trees.plans.commands.DropSqlBlockRuleCommand; import org.apache.doris.nereids.trees.plans.commands.ExplainCommand; import org.apache.doris.nereids.trees.plans.commands.ExportCommand; import org.apache.doris.nereids.trees.plans.commands.LoadCommand; @@ -357,6 +358,10 @@ default R visitDropRoleCommand(DropRoleCommand dropRoleCommand, C context) { return visitCommand(dropRoleCommand, context); } + default R visitDropSqlBlockRuleCommand(DropSqlBlockRuleCommand dropSqlBlockRuleCommand, C context) { + return visitCommand(dropSqlBlockRuleCommand, context); + } + default R visitShowTableIdCommand(ShowTableIdCommand showTableIdCommand, C context) { return visitCommand(showTableIdCommand, context); } diff --git a/regression-test/data/sql_block_rule_p0/test_sql_block_rule.out b/regression-test/data/sql_block_rule_p0/test_sql_block_rule.out index 373ec1348d1b39..c43c520177256b 100644 --- a/regression-test/data/sql_block_rule_p0/test_sql_block_rule.out +++ b/regression-test/data/sql_block_rule_p0/test_sql_block_rule.out @@ -5,5 +5,11 @@ 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 -- +-- !select3_notexist -- + +-- !select4_exist -- +test_rule_sql SELECT \\* FROM table_2 NULL 0 0 0 true true +test_rule_sql1 SELECT \\* FROM table_2 NULL 0 0 0 true true + +-- !select5_not_exist -- diff --git a/regression-test/suites/sql_block_rule_p0/test_sql_block_rule.groovy b/regression-test/suites/sql_block_rule_p0/test_sql_block_rule.groovy index e0243e35a090a7..13b83d272decdb 100644 --- a/regression-test/suites/sql_block_rule_p0/test_sql_block_rule.groovy +++ b/regression-test/suites/sql_block_rule_p0/test_sql_block_rule.groovy @@ -92,8 +92,27 @@ suite("test_sql_block_rule", "nonConcurrent") { SHOW SQL_BLOCK_RULE FOR test_rule_sql """ + checkNereidsExecute("DROP SQL_BLOCK_RULE if exists test_rule_sql") + + qt_select3_notexist """ + SHOW SQL_BLOCK_RULE + """ + sql """ - DROP SQL_BLOCK_RULE if exists test_rule_sql + CREATE SQL_BLOCK_RULE if not exists test_rule_sql + PROPERTIES("sql"="SELECT \\\\* FROM table_2", "global"= "true", "enable"= "true") + """ + sql """ + CREATE SQL_BLOCK_RULE if not exists test_rule_sql1 + PROPERTIES("sql"="SELECT \\\\* FROM table_2", "global"= "true", "enable"= "true") + """ + + qt_select4_exist """ + SHOW SQL_BLOCK_RULE + """ + + sql """ + DROP SQL_BLOCK_RULE if exists test_rule_sql,test_rule_sql1 """ sql """ @@ -110,7 +129,7 @@ suite("test_sql_block_rule", "nonConcurrent") { exception "sql hits sql block rule: test_rule_num, reach tablet_num : 1" } */ - qt_select3 """ + qt_select5_not_exist """ SHOW SQL_BLOCK_RULE """