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

morrysnow 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 8b5453296e [fix](optimizer) Fix sql block when new optimizer is 
enabled (#23804)
8b5453296e is described below

commit 8b5453296e6196e80ce50efdc11e62b473122ed7
Author: AKIRA <33112463+kikyou1...@users.noreply.github.com>
AuthorDate: Mon Sep 11 15:27:11 2023 +0900

    [fix](optimizer) Fix sql block when new optimizer is enabled (#23804)
    
    The check would skipped since when checkBlockPolicy get invoked, new 
optimizer doesn't do plan yet
---
 .../trees/plans/commands/ExplainCommand.java       |  1 +
 .../plans/commands/InsertIntoTableCommand.java     |  2 +-
 .../java/org/apache/doris/qe/StmtExecutor.java     | 46 ++++++++------
 .../sql_block_rule_p0/test_sql_block_rule.groovy   | 71 +++++++++++++++++++++-
 4 files changed, 99 insertions(+), 21 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExplainCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExplainCommand.java
index bfb0e82a17..9e02b8b4e9 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExplainCommand.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExplainCommand.java
@@ -79,6 +79,7 @@ public class ExplainCommand extends Command implements 
NoForward {
         NereidsPlanner planner = new NereidsPlanner(ctx.getStatementContext());
         planner.plan(logicalPlanAdapter, ctx.getSessionVariable().toThrift());
         executor.setPlanner(planner);
+        executor.checkBlockRules();
         executor.handleExplainStmt(planner.getExplainString(new 
ExplainOptions(level)));
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java
index feda9f9b0a..935d3867be 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java
@@ -123,7 +123,7 @@ public class InsertIntoTableCommand extends Command 
implements ForwardWithSync,
         LogicalPlanAdapter logicalPlanAdapter = new 
LogicalPlanAdapter(logicalQuery, ctx.getStatementContext());
         planner = new NereidsPlanner(ctx.getStatementContext());
         planner.plan(logicalPlanAdapter, ctx.getSessionVariable().toThrift());
-
+        executor.checkBlockRules();
         if (ctx.getMysqlChannel() != null) {
             ctx.getMysqlChannel().reset();
         }
diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java 
b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
index bd1d1a4043..9ea8805dd8 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
@@ -473,24 +473,32 @@ public class StmtExecutor {
         }
     }
 
-    private void checkBlockRules() throws AnalysisException {
-        if (originStmt != null) {
-            Env.getCurrentEnv().getSqlBlockRuleMgr().matchSql(
-                    originStmt.originStmt, context.getSqlHash(), 
context.getQualifiedUser());
-        }
-
-        // limitations: partition_num, tablet_num, cardinality
-        if (planner != null) {
-            List<ScanNode> scanNodeList = planner.getScanNodes();
-            for (ScanNode scanNode : scanNodeList) {
-                if (scanNode instanceof OlapScanNode) {
-                    OlapScanNode olapScanNode = (OlapScanNode) scanNode;
-                    Env.getCurrentEnv().getSqlBlockRuleMgr().checkLimitations(
-                            olapScanNode.getSelectedPartitionNum().longValue(),
-                            olapScanNode.getSelectedTabletsNum(),
-                            olapScanNode.getCardinality(),
-                            context.getQualifiedUser());
-                }
+    public void checkBlockRules() throws AnalysisException {
+        checkBlockRulesByRegex(originStmt);
+        checkBlockRulesByScan(planner);
+    }
+
+    public void checkBlockRulesByRegex(OriginStatement originStmt) throws 
AnalysisException {
+        if (originStmt == null) {
+            return;
+        }
+        Env.getCurrentEnv().getSqlBlockRuleMgr().matchSql(
+                originStmt.originStmt, context.getSqlHash(), 
context.getQualifiedUser());
+    }
+
+    public void checkBlockRulesByScan(Planner planner) throws 
AnalysisException {
+        if (planner == null) {
+            return;
+        }
+        List<ScanNode> scanNodeList = planner.getScanNodes();
+        for (ScanNode scanNode : scanNodeList) {
+            if (scanNode instanceof OlapScanNode) {
+                OlapScanNode olapScanNode = (OlapScanNode) scanNode;
+                Env.getCurrentEnv().getSqlBlockRuleMgr().checkLimitations(
+                        olapScanNode.getSelectedPartitionNum().longValue(),
+                        olapScanNode.getSelectedTabletsNum(),
+                        olapScanNode.getCardinality(),
+                        context.getQualifiedUser());
             }
         }
     }
@@ -502,7 +510,6 @@ public class StmtExecutor {
         profile.getSummaryProfile().setQueryBeginTime();
         context.setStmtId(STMT_ID_GENERATOR.incrementAndGet());
 
-        checkBlockRules();
         parseByNereids();
         Preconditions.checkState(parsedStmt instanceof LogicalPlanAdapter,
                 "Nereids only process LogicalPlanAdapter, but parsedStmt is " 
+ parsedStmt.getClass().getName());
@@ -556,6 +563,7 @@ public class StmtExecutor {
             planner = new NereidsPlanner(statementContext);
             try {
                 planner.plan(parsedStmt, 
context.getSessionVariable().toThrift());
+                checkBlockRules();
             } catch (Exception e) {
                 LOG.debug("Nereids plan query failed:\n{}", 
originStmt.originStmt);
                 throw new NereidsException(new 
AnalysisException(e.getMessage(), e));
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 fa45be92cd..582fa7c705 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
@@ -16,6 +16,15 @@
 // under the License.
 
 suite("test_sql_block_rule") {
+
+    sql """
+        DROP SQL_BLOCK_RULE if exists test_rule_partition
+    """
+
+    sql """
+        DROP SQL_BLOCK_RULE if exists test_rule_tablet
+    """
+
     sql """
                   DROP SQL_BLOCK_RULE if exists test_rule_num
                 """
@@ -27,7 +36,7 @@ suite("test_sql_block_rule") {
       `abcd` varchar(150) NULL COMMENT "",
       `create_time` datetime NULL COMMENT ""
     ) ENGINE=OLAP
-    UNIQUE KEY(`abcd`)
+    DUPLICATE KEY(`abcd`)
     COMMENT "OLAP"
     DISTRIBUTED BY HASH(`abcd`) BUCKETS 3
       PROPERTIES (
@@ -47,6 +56,15 @@ suite("test_sql_block_rule") {
         sql("SELECT * FROM table_2", false)
         exception "sql match regex sql block rule: test_rule_sql"
     }
+    test {
+        sql("EXPLAIN SELECT * FROM table_2", false)
+        exception "sql match regex sql block rule: test_rule_sql"
+    }
+
+    test {
+        sql("INSERT INTO table_2 SELECT * FROM table_2", false)
+        exception "sql match regex sql block rule: test_rule_sql"
+    }
 
     sql """
                 DROP SQL_BLOCK_RULE if exists test_rule_sql
@@ -126,4 +144,55 @@ suite("test_sql_block_rule") {
                 " PROPERTIES(\"sql\"=\"create\", \"global\"= \"true\", 
\"enable\"= \"true\")", false)
         exception "sql of SQL_BLOCK_RULE should not match its name"
     }
+
+    sql """DROP TABLE IF EXISTS a_partitioned_table_for_sql_block_rule"""
+
+    sql """
+        CREATE TABLE a_partitioned_table_for_sql_block_rule (
+            id BIGINT,
+            val BIGINT,
+            str VARCHAR(114)
+        ) DUPLICATE KEY(`id`)
+        PARTITION BY RANGE(`id`)
+        (
+            PARTITION `p1` VALUES LESS THAN ('5'),
+            PARTITION `p2` VALUES LESS THAN ('10'),
+            PARTITION `p3` VALUES LESS THAN ('15')
+        )
+        DISTRIBUTED BY HASH(`id`) BUCKETS 3
+        PROPERTIES (
+        "replication_num"="1"
+        );
+    """
+
+    sql """
+        INSERT INTO a_partitioned_table_for_sql_block_rule VALUES(1, 5, 
11),(6,1,5),(11,8,5);
+    """
+
+    sql """
+        CREATE SQL_BLOCK_RULE if not exists test_rule_partition PROPERTIES ( 
"partition_num" = "1", "global" = "true",
+        "enable"="true");
+    """
+
+    test {
+        sql("""SELECT * FROM a_partitioned_table_for_sql_block_rule;""", false)
+
+        exception """sql hits sql block rule"""
+
+    }
+
+    sql """
+        CREATE SQL_BLOCK_RULE if not exists test_rule_tablet PROPERTIES ( 
"tablet_num" = "3", "global" = "true",
+        "enable"="true");
+    """
+
+    test {
+        sql("""SELECT * FROM a_partitioned_table_for_sql_block_rule;""", false)
+
+        exception """sql hits 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