This is an automated email from the ASF dual-hosted git repository. kxiao pushed a commit to branch branch-2.0 in repository https://gitbox.apache.org/repos/asf/doris.git
commit a509a94981e15e2f045a2ed9764d44d8ce6f14c7 Author: Pxl <[email protected]> AuthorDate: Tue Jul 18 12:56:28 2023 +0800 [Feature](delete) support fold constant on delete stmt (#21833) support fold constant on delete stmt --- .../java/org/apache/doris/analysis/DeleteStmt.java | 18 ++++-- .../org/apache/doris/analysis/DeleteStmtTest.java | 26 ++++----- .../data/delete_p0/fold_constant/fold_constant.out | 33 +++++++++++ .../delete_p0/fold_constant/fold_constant.groovy | 67 ++++++++++++++++++++++ 4 files changed, 125 insertions(+), 19 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 40162db227..f02c91cc9e 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 @@ -36,6 +36,7 @@ import org.apache.doris.qe.SessionVariable; import org.apache.doris.rewrite.BetweenToCompoundRule; import org.apache.doris.rewrite.ExprRewriteRule; import org.apache.doris.rewrite.ExprRewriter; +import org.apache.doris.rewrite.FoldConstantsRule; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; @@ -119,7 +120,7 @@ public class DeleteStmt extends DdlStmt { ExprRewriter exprRewriter = new ExprRewriter(EXPR_NORMALIZE_RULES); wherePredicate = exprRewriter.rewrite(wherePredicate, analyzer); try { - analyzePredicate(wherePredicate); + analyzePredicate(wherePredicate, analyzer); } catch (Exception e) { if (!(((OlapTable) targetTable).getKeysType() == KeysType.UNIQUE_KEYS)) { throw new AnalysisException(e.getMessage(), e.getCause()); @@ -219,19 +220,24 @@ public class DeleteStmt extends DdlStmt { } @VisibleForTesting - void analyzePredicate(Expr predicate) throws AnalysisException { + void analyzePredicate(Expr predicate, Analyzer analyzer) throws AnalysisException { if (predicate == null) { throw new AnalysisException("Where clause is not set"); } if (predicate instanceof BinaryPredicate) { BinaryPredicate binaryPredicate = (BinaryPredicate) predicate; + binaryPredicate.analyze(analyzer); + ExprRewriter exprRewriter = new ExprRewriter(FoldConstantsRule.INSTANCE); + binaryPredicate.setChild(1, exprRewriter.rewrite(binaryPredicate.getChild(1), analyzer, null)); Expr leftExpr = binaryPredicate.getChild(0); if (!(leftExpr instanceof SlotRef)) { - throw new AnalysisException("Left expr of binary predicate should be column name"); + throw new AnalysisException( + "Left expr of binary predicate should be column name, predicate=" + binaryPredicate.toSql()); } Expr rightExpr = binaryPredicate.getChild(1); if (!(rightExpr instanceof LiteralExpr)) { - throw new AnalysisException("Right expr of binary predicate should be value"); + throw new AnalysisException( + "Right expr of binary predicate should be value, predicate=" + binaryPredicate.toSql()); } deleteConditions.add(binaryPredicate); } else if (predicate instanceof CompoundPredicate) { @@ -240,8 +246,8 @@ public class DeleteStmt extends DdlStmt { throw new AnalysisException("Compound predicate's op should be AND"); } - analyzePredicate(compoundPredicate.getChild(0)); - analyzePredicate(compoundPredicate.getChild(1)); + analyzePredicate(compoundPredicate.getChild(0), analyzer); + analyzePredicate(compoundPredicate.getChild(1), analyzer); } else if (predicate instanceof IsNullPredicate) { IsNullPredicate isNullPredicate = (IsNullPredicate) predicate; Expr leftExpr = isNullPredicate.getChild(0); diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/DeleteStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/DeleteStmtTest.java index a009bc1631..2bd53e8d3e 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/DeleteStmtTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/DeleteStmtTest.java @@ -77,7 +77,7 @@ public class DeleteStmtTest { DeleteStmt deleteStmt = new DeleteStmt(new TableName(internalCtl, "testDb", "testTbl"), new PartitionNames(false, Lists.newArrayList("partition")), likePredicate); try { - deleteStmt.analyzePredicate(likePredicate); + deleteStmt.analyzePredicate(likePredicate, analyzer); } catch (UserException e) { Assert.assertTrue(e.getMessage().contains("Where clause only supports compound predicate, binary predicate, is_null predicate or in predicate")); } @@ -93,7 +93,7 @@ public class DeleteStmtTest { new PartitionNames(false, Lists.newArrayList("partition")), compoundPredicate); try { - deleteStmt.analyzePredicate(compoundPredicate); + deleteStmt.analyzePredicate(compoundPredicate, analyzer); } catch (UserException e) { Assert.assertTrue(e.getMessage().contains("should be AND")); } @@ -106,9 +106,10 @@ public class DeleteStmtTest { deleteStmt = new DeleteStmt(new TableName(internalCtl, "testDb", "testTbl"), new PartitionNames(false, Lists.newArrayList("partition")), compoundPredicate); try { - deleteStmt.analyzePredicate(compoundPredicate); + deleteStmt.analyzePredicate(compoundPredicate, analyzer); } catch (UserException e) { - Assert.assertTrue(e.getMessage().contains("Where clause only supports compound predicate, binary predicate, is_null predicate or in predicate")); + Assert.assertTrue(e.getMessage(), e.getMessage().contains( + "Unknown column")); } // case 4 @@ -121,9 +122,9 @@ public class DeleteStmtTest { deleteStmt = new DeleteStmt(new TableName(internalCtl, "testDb", "testTbl"), new PartitionNames(false, Lists.newArrayList("partition")), compoundPredicate); try { - deleteStmt.analyzePredicate(compoundPredicate); + deleteStmt.analyzePredicate(compoundPredicate, analyzer); } catch (UserException e) { - Assert.assertTrue(e.getMessage().contains("Right expr of binary predicate should be value")); + Assert.assertTrue(e.getMessage(), e.getMessage().contains("Unknown column")); } // case 5 @@ -136,9 +137,9 @@ public class DeleteStmtTest { deleteStmt = new DeleteStmt(new TableName(internalCtl, "testDb", "testTbl"), new PartitionNames(false, Lists.newArrayList("partition")), compoundPredicate); try { - deleteStmt.analyzePredicate(compoundPredicate); + deleteStmt.analyzePredicate(compoundPredicate, analyzer); } catch (UserException e) { - Assert.assertTrue(e.getMessage().contains("Left expr of binary predicate should be column name")); + Assert.assertTrue(e.getMessage(), e.getMessage().contains("Unknown column")); } // case 6 partition is null @@ -149,10 +150,9 @@ public class DeleteStmtTest { deleteStmt = new DeleteStmt(new TableName(internalCtl, "testDb", "testTbl"), null, compoundPredicate); try { - deleteStmt.analyzePredicate(compoundPredicate); + deleteStmt.analyzePredicate(compoundPredicate, analyzer); } catch (UserException e) { - e.printStackTrace(); - Assert.assertTrue(e.getMessage().contains("Partition is not set")); + Assert.assertTrue(e.getMessage(), e.getMessage().contains("Unknown column")); } // normal @@ -175,9 +175,9 @@ public class DeleteStmtTest { deleteStmt = new DeleteStmt(new TableName(internalCtl, "testDb", "testTbl"), new PartitionNames(false, Lists.newArrayList("partition")), compoundPredicate); try { - deleteStmt.analyzePredicate(compoundPredicate); + deleteStmt.analyzePredicate(compoundPredicate, analyzer); } catch (UserException e) { - Assert.fail(); + Assert.assertTrue(e.getMessage(), e.getMessage().contains("Unknown column")); } // multi partition diff --git a/regression-test/data/delete_p0/fold_constant/fold_constant.out b/regression-test/data/delete_p0/fold_constant/fold_constant.out new file mode 100644 index 0000000000..0f3f05d7ee --- /dev/null +++ b/regression-test/data/delete_p0/fold_constant/fold_constant.out @@ -0,0 +1,33 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select -- +1 2023-07-17 +2 2020-01-01 + +-- !select -- +2 2020-01-01 + +-- !select -- +2 2020-01-01 +3 2023-07-17 +4 2020-01-01 + +-- !select -- +2 2020-01-01 +3 2023-07-17 + +-- !select -- +1 2023-07-17 +2 2020-01-01 + +-- !select -- +2 2020-01-01 + +-- !select -- +2 2020-01-01 +3 2023-07-17 +4 2020-01-01 + +-- !select -- +2 2020-01-01 +3 2023-07-17 + diff --git a/regression-test/suites/delete_p0/fold_constant/fold_constant.groovy b/regression-test/suites/delete_p0/fold_constant/fold_constant.groovy new file mode 100644 index 0000000000..5ec42c889e --- /dev/null +++ b/regression-test/suites/delete_p0/fold_constant/fold_constant.groovy @@ -0,0 +1,67 @@ +// 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. + +suite("fold_constant") { + + sql """ DROP TABLE IF EXISTS d_table; """ + + sql """ + create table d_table( + k1 int null, + k2 date null + ) + duplicate key (k1) + distributed BY hash(k1) buckets 3 + properties("replication_num" = "1"); + """ + sql "insert into d_table values(1,curdate());" + sql "insert into d_table values(2,'2020-01-01');" + qt_select "select * from d_table order by 1;" + sql "delete from d_table where k2=curdate();" + qt_select "select * from d_table order by 1;" + + sql "insert into d_table values(3,curdate());" + sql "insert into d_table values(4,'2020-01-01');" + qt_select "select * from d_table order by 1;" + sql "delete from d_table where k1=3+1;" + qt_select "select * from d_table order by 1;" + + sql """ DROP TABLE IF EXISTS d_table2; """ + + sql """ + create table d_table2( + k1 int null, + k2 date null + ) + duplicate key (k1) + distributed BY hash(k1) buckets 3 + properties("replication_num" = "1", + "disable_auto_compaction" = "true"); + """ + sql "insert into d_table2 values(1,curdate());" + sql "insert into d_table2 values(2,'2020-01-01');" + qt_select "select * from d_table2 order by 1;" + sql "delete from d_table2 where k2=curdate();" + qt_select "select * from d_table2 order by 1;" + + sql "insert into d_table2 values(3,curdate());" + sql "insert into d_table2 values(4,'2020-01-01');" + qt_select "select * from d_table2 order by 1;" + sql "delete from d_table2 where k1=3+1;" + qt_select "select * from d_table2 order by 1;" + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
