Skip to content

Commit

Permalink
[Feature](delete) support fold constant on delete stmt (apache#21833)
Browse files Browse the repository at this point in the history
support fold constant on delete stmt
  • Loading branch information
BiteTheDDDDt authored Jul 18, 2023
1 parent 19492b0 commit 417e3e5
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 19 deletions.
18 changes: 12 additions & 6 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/DeleteStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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;
Expand Down Expand Up @@ -119,7 +120,7 @@ public void analyze(Analyzer analyzer) throws UserException {
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());
Expand Down Expand Up @@ -219,19 +220,24 @@ private void analyzeTargetTable(Analyzer analyzer) throws UserException {
}

@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) {
Expand All @@ -240,8 +246,8 @@ void analyzePredicate(Expr predicate) throws AnalysisException {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testAnalyze() {
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"));
}
Expand All @@ -93,7 +93,7 @@ public void testAnalyze() {
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"));
}
Expand All @@ -106,9 +106,10 @@ public void testAnalyze() {
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
Expand All @@ -121,9 +122,9 @@ public void testAnalyze() {
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
Expand All @@ -136,9 +137,9 @@ public void testAnalyze() {
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
Expand All @@ -149,10 +150,9 @@ public void testAnalyze() {

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
Expand All @@ -175,9 +175,9 @@ public void testAnalyze() {
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
Expand Down
33 changes: 33 additions & 0 deletions regression-test/data/delete_p0/fold_constant/fold_constant.out
Original file line number Diff line number Diff line change
@@ -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

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

}

0 comments on commit 417e3e5

Please sign in to comment.