You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2023/06/27 09:51:21 UTC

[doris] branch master updated: [opt](planner) support delete with a subquery in predicate by construct an insert. (#20983)

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 64a1eb77f0 [opt](planner) support delete with a subquery in predicate by construct an insert. (#20983)
64a1eb77f0 is described below

commit 64a1eb77f09f78622691f6a7a698756ffb4236c0
Author: mch_ucchi <41...@users.noreply.github.com>
AuthorDate: Tue Jun 27 17:51:13 2023 +0800

    [opt](planner) support delete with a subquery in predicate by construct an insert. (#20983)
    
    complex predicate in delete stmt like:
    ```sql
    delete from t1 where t1.id in (select id from t2);
    ```
    
    will be replaced to an insert stmt.
    ```sql
    insert into t1(id, __DORIS_DELETE_SIGN__) select id, 1 from t1 where id in (select id from t2);
    ```
---
 .../java/org/apache/doris/analysis/DeleteStmt.java |  9 ++++-
 .../java/org/apache/doris/qe/StmtExecutor.java     |  2 +-
 .../data/delete_p0/test_delete_where_in.out        |  7 +++-
 .../suites/delete_p0/test_delete_where_in.groovy   | 47 +++++++++++++++++++++-
 4 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/DeleteStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/DeleteStmt.java
index 1d733d1e2f..40162db227 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/DeleteStmt.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/DeleteStmt.java
@@ -118,7 +118,14 @@ public class DeleteStmt extends DdlStmt {
         if (fromClause == null) {
             ExprRewriter exprRewriter = new ExprRewriter(EXPR_NORMALIZE_RULES);
             wherePredicate = exprRewriter.rewrite(wherePredicate, analyzer);
-            analyzePredicate(wherePredicate);
+            try {
+                analyzePredicate(wherePredicate);
+            } catch (Exception e) {
+                if (!(((OlapTable) targetTable).getKeysType() == KeysType.UNIQUE_KEYS)) {
+                    throw new AnalysisException(e.getMessage(), e.getCause());
+                }
+                constructInsertStmt();
+            }
         } else {
             constructInsertStmt();
         }
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 9b5bc9e758..47f3460f20 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
@@ -720,7 +720,7 @@ public class StmtExecutor {
             } else if (parsedStmt instanceof UpdateStmt) {
                 handleUpdateStmt();
             } else if (parsedStmt instanceof DdlStmt) {
-                if (parsedStmt instanceof DeleteStmt && ((DeleteStmt) parsedStmt).getFromClause() != null) {
+                if (parsedStmt instanceof DeleteStmt && ((DeleteStmt) parsedStmt).getInsertStmt() != null) {
                     handleDeleteStmt();
                 } else {
                     handleDdlStmt();
diff --git a/regression-test/data/delete_p0/test_delete_where_in.out b/regression-test/data/delete_p0/test_delete_where_in.out
index b840a28984..37855fcdc3 100644
--- a/regression-test/data/delete_p0/test_delete_where_in.out
+++ b/regression-test/data/delete_p0/test_delete_where_in.out
@@ -18,4 +18,9 @@
 0
 
 -- !sql --
-0
\ No newline at end of file
+0
+
+-- !delete_in --
+2	20	2	2	2.0	2000-01-02
+3	30	3	3	3.0	2000-01-03
+
diff --git a/regression-test/suites/delete_p0/test_delete_where_in.groovy b/regression-test/suites/delete_p0/test_delete_where_in.groovy
index 021338fe3a..449647e37c 100644
--- a/regression-test/suites/delete_p0/test_delete_where_in.groovy
+++ b/regression-test/suites/delete_p0/test_delete_where_in.groovy
@@ -99,5 +99,50 @@ suite("test_delete_where_in", "delete_p0") {
         //drop table
         qt_sql """truncate table ${tb_name}"""
         qt_sql """drop table ${tb_name}"""
-        
+
+    sql 'drop table if exists t1'
+    sql 'drop table if exists t2'
+    sql 'drop table if exists t3'
+
+    sql '''
+        create table t1 (
+            id int,
+            id1 int,
+            c1 bigint,
+            c2 string,
+            c3 double,
+            c4 date
+        ) unique key (id, id1)
+        distributed by hash(id, id1) buckets 13
+        properties(
+            'replication_num'='1',
+            "function_column.sequence_col" = "c4"
+        );
+    '''
+
+    sql '''
+        create table t3 (
+            id int
+        ) distributed by hash(id) buckets 13
+        properties(
+            'replication_num'='1'
+        );
+    '''
+
+    sql '''
+        INSERT INTO t1 VALUES
+            (1, 10, 1, '1', 1.0, '2000-01-01'),
+            (2, 20, 2, '2', 2.0, '2000-01-02'),
+            (3, 30, 3, '3', 3.0, '2000-01-03');
+    '''
+
+    sql '''
+        INSERT INTO t3 VALUES
+            (1),
+            (4),
+            (5);
+    '''  
+    
+    sql 'delete from t1 where id in (select id from t3)'
+    qt_delete_in 'select * from t1 order by id'
 }


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