This is an automated email from the ASF dual-hosted git repository.

starocean999 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 43b7fa5eb4b [Enhancement] (nereids)implement showSqlBlockRuleCommand 
in nereids (#42979)
43b7fa5eb4b is described below

commit 43b7fa5eb4be442d1306edff6fe588382c2ad6fa
Author: Vallish Pai <vallish...@gmail.com>
AuthorDate: Tue Nov 19 14:29:17 2024 +0530

    [Enhancement] (nereids)implement showSqlBlockRuleCommand in nereids (#42979)
    
    Issue Number: close #42719
    
    implement showSqlBlockRuleCommand in nereids
---
 .../antlr4/org/apache/doris/nereids/DorisParser.g4 |  4 +-
 .../apache/doris/blockrule/SqlBlockRuleMgr.java    |  7 ++
 .../doris/nereids/parser/LogicalPlanBuilder.java   | 11 +++
 .../apache/doris/nereids/trees/plans/PlanType.java |  1 +
 .../plans/commands/ShowSqlBlockRuleCommand.java    | 80 ++++++++++++++++++++++
 .../trees/plans/visitor/CommandVisitor.java        |  5 ++
 .../data/sql_block_rule_p0/test_sql_block_rule.out |  8 ++-
 .../sql_block_rule_p0/test_sql_block_rule.groovy   | 12 +++-
 8 files changed, 124 insertions(+), 4 deletions(-)

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 c9958e68641..2f15ec1fd99 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
@@ -210,6 +210,7 @@ supportedShowStatement
     | SHOW PRIVILEGES                                                          
     #showPrivileges
     | 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 BACKENDS                                                            
     #showBackends
@@ -246,8 +247,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
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 13df2eb9377..c6dd0af4210 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
@@ -80,6 +80,13 @@ public class SqlBlockRuleMgr implements Writable {
      **/
     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);
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 6fc011d6926..d92e0608119 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
@@ -211,6 +211,7 @@ import org.apache.doris.nereids.DorisParser.ShowProcContext;
 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.ShowTableIdContext;
 import org.apache.doris.nereids.DorisParser.ShowVariablesContext;
@@ -458,6 +459,7 @@ 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.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.ShowTableIdCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
@@ -4127,6 +4129,15 @@ public class LogicalPlanBuilder extends 
DorisParserBaseVisitor<Object> {
         return new ShowPluginsCommand();
     }
 
+    @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();
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 e255ac32621..ab6f1dc79fc 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
@@ -178,6 +178,7 @@ public enum PlanType {
     PREPARED_COMMAND,
     EXECUTE_COMMAND,
     SHOW_BACKENDS_COMMAND,
+    SHOW_BLOCK_RULE_COMMAND,
     SHOW_CONFIG_COMMAND,
     SHOW_CREATE_MATERIALIZED_VIEW_COMMAND,
     SHOW_FRONTENDS_COMMAND,
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowSqlBlockRuleCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowSqlBlockRuleCommand.java
new file mode 100644
index 00000000000..e9a32cd091e
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowSqlBlockRuleCommand.java
@@ -0,0 +1,80 @@
+// 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 java.util.List;
+
+/**
+ * show sql block rule command
+ */
+public class ShowSqlBlockRuleCommand extends ShowCommand {
+    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);
+    }
+}
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 117c42b7bfc..c4d46c6cad1 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
@@ -66,6 +66,7 @@ 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.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.ShowTableIdCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
@@ -280,6 +281,10 @@ public interface CommandVisitor<R, C> {
         return visitCommand(showBackendsCommand, context);
     }
 
+    default R visitShowSqlBlockRuleCommand(ShowSqlBlockRuleCommand 
showblockruleCommand, C context) {
+        return visitCommand(showblockruleCommand, context);
+    }
+
     default R visitShowPluginsCommand(ShowPluginsCommand showPluginsCommand, C 
context) {
         return visitCommand(showPluginsCommand, 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 f958424e65c..373ec1348d1 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
@@ -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 --
 
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 651ee946bd9..e0243e35a09 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
@@ -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
               """
@@ -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
               """
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to