Skip to content

Commit

Permalink
[Enhancement] (nereids)implement DropSqlBlockRuleCommand in nereids
Browse files Browse the repository at this point in the history
  • Loading branch information
Vallishp committed Nov 19, 2024
1 parent b3cb480 commit 94ec608
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,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
Expand Down Expand Up @@ -675,7 +676,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> ruleNames, boolean isIfExists) throws DdlException {
writeLock();
try {
List<String> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -433,6 +434,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;
Expand Down Expand Up @@ -4211,6 +4213,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> ruleNames;

/**
* constructor
*/
public DropSqlBlockRuleCommand(List<String> 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, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitDropSqlBlockRuleCommand(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -336,6 +337,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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 --

Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand All @@ -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
"""

Expand Down

0 comments on commit 94ec608

Please sign in to comment.