Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the delete stmt #668

Merged
merged 4 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/help/Contents/Data Manipulation/manipulation_stmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,22 +293,22 @@
该语句用于按条件删除指定 table(base index) partition 中的数据。
该操作会同时删除和此 base index 相关的 rollup index 的数据。
语法:
DELETE FROM table_name PARTITION partition_name
DELETE FROM table_name [PARTITION partition_name]
WHERE
column_name1 op value[ AND column_name2 op value ...];

说明:
1) op 的可选类型包括:=, >, <, >=, <=, !=
2) 只能指定 key 列上的条件。
2) 当选定的 key 列不存在于某个 rollup 中时,无法进行 delete。
3) 条件之间只能是“与”的关系。
若希望达成“或”的关系,需要将条件分写在两个 DELETE语句中
4) 如果为单分区表,partition_name 同 table_name
若希望达成“或”的关系,需要将条件分写在两个 DELETE 语句中
4) 如果为RANGE分区表,则必须指定 PARTITION。如果是单分区表,可以不指定

注意:
该语句可能会降低执行后一段时间内的查询效率。
影响程度取决于语句中指定的删除条件的数量。
指定的条件越多,影响越大。


## example

Expand Down
5 changes: 2 additions & 3 deletions fe/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -2063,10 +2063,9 @@ cancel_param ::=

// Delete stmt
delete_stmt ::=
KW_DELETE KW_FROM table_name:table KW_PARTITION ident:partition where_clause:wherePredicate
opt_properties:properties
KW_DELETE KW_FROM table_name:table opt_partition_name:partition where_clause:wherePredicate
{:
RESULT = new DeleteStmt(table, partition, wherePredicate, properties);
RESULT = new DeleteStmt(table, partition, wherePredicate);
:}
;

Expand Down
26 changes: 5 additions & 21 deletions fe/src/main/java/org/apache/doris/analysis/DeleteStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,26 @@
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.UserException;
import org.apache.doris.common.util.PrintableMap;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.qe.ConnectContext;

import com.google.common.base.Strings;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class DeleteStmt extends DdlStmt {
private final TableName tbl;
private final String partitionName;
private Expr wherePredicate;

private List<Predicate> deleteConditions;
private Map<String, String> properties;

public DeleteStmt(TableName tableName, String partitionName, Expr wherePredicate, Map<String, String> properties) {
public DeleteStmt(TableName tableName, String partitionName, Expr wherePredicate) {
this.tbl = tableName;
this.partitionName = partitionName;
this.partitionName = Strings.emptyToNull(partitionName);
this.wherePredicate = wherePredicate;
this.deleteConditions = new LinkedList<Predicate>();

this.properties = properties;
}

public String getTableName() {
Expand All @@ -66,10 +61,6 @@ public List<Predicate> getDeleteConditions() {
return deleteConditions;
}

public Map<String, String> getProperties() {
return properties;
}

@Override
public void analyze(Analyzer analyzer) throws AnalysisException, UserException {
super.analyze(analyzer);
Expand All @@ -79,10 +70,6 @@ public void analyze(Analyzer analyzer) throws AnalysisException, UserException {
}

tbl.analyze(analyzer);

if (Strings.isNullOrEmpty(partitionName)) {
throw new AnalysisException("Partition is not set");
}

if (wherePredicate == null) {
throw new AnalysisException("Where clause is not set");
Expand Down Expand Up @@ -136,13 +123,10 @@ private void analyzePredicate(Expr predicate) throws AnalysisException {
public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("DELETE FROM ").append(tbl.toSql());
sb.append(" PARTITION ").append(partitionName);
sb.append(" WHERE ").append(wherePredicate.toSql());
if (properties != null && !properties.isEmpty()) {
sb.append("\nPROPERTIES (");
sb.append(new PrintableMap<String, String>(properties, "=", true, false));
sb.append(")");
if (!Strings.isNullOrEmpty(partitionName)) {
sb.append(" PARTITION ").append(partitionName);
}
sb.append(" WHERE ").append(wherePredicate.toSql());
return sb.toString();
}

Expand Down
12 changes: 12 additions & 0 deletions fe/src/main/java/org/apache/doris/load/Load.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.doris.catalog.OlapTable.OlapTableState;
import org.apache.doris.catalog.Partition;
import org.apache.doris.catalog.Partition.PartitionState;
import org.apache.doris.catalog.PartitionType;
import org.apache.doris.catalog.PrimitiveType;
import org.apache.doris.catalog.Replica;
import org.apache.doris.catalog.Table;
Expand Down Expand Up @@ -3089,6 +3090,16 @@ public void delete(DeleteStmt stmt) throws DdlException {
}

tableId = olapTable.getId();
if (partitionName == null) {
if (olapTable.getPartitionInfo().getType() == PartitionType.RANGE) {
throw new DdlException("This is a range partitioned table."
+ " You should specify partition in delete stmt");
} else {
// this is a unpartitioned table, use table name as partition name
partitionName = olapTable.getName();
}
}

Partition partition = olapTable.getPartition(partitionName);
if (partition == null) {
throw new DdlException("Partition does not exist. name: " + partitionName);
Expand Down Expand Up @@ -3170,6 +3181,7 @@ public void delete(DeleteStmt stmt) throws DdlException {
}
}

@Deprecated
public void deleteOld(DeleteStmt stmt) throws DdlException {
String dbName = stmt.getDbName();
String tableName = stmt.getTableName();
Expand Down
20 changes: 10 additions & 10 deletions fe/src/test/java/org/apache/doris/analysis/DeleteStmtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setUp() {
public void getMethodTest() {
BinaryPredicate wherePredicate = new BinaryPredicate(Operator.EQ, new SlotRef(null, "k1"),
new StringLiteral("abc"));
DeleteStmt deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", wherePredicate, null);
DeleteStmt deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", wherePredicate);

Assert.assertEquals("testDb", deleteStmt.getDbName());
Assert.assertEquals("testTbl", deleteStmt.getTableName());
Expand All @@ -70,7 +70,7 @@ public void testAnalyze() {
LikePredicate likePredicate = new LikePredicate(org.apache.doris.analysis.LikePredicate.Operator.LIKE,
new SlotRef(null, "k1"),
new StringLiteral("abc"));
DeleteStmt deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", likePredicate, null);
DeleteStmt deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", likePredicate);
try {
deleteStmt.analyze(analyzer);
} catch (UserException e) {
Expand All @@ -84,7 +84,7 @@ public void testAnalyze() {
new CompoundPredicate(org.apache.doris.analysis.CompoundPredicate.Operator.OR, binaryPredicate,
binaryPredicate);

deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", compoundPredicate, null);
deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", compoundPredicate);

try {
deleteStmt.analyze(analyzer);
Expand All @@ -97,7 +97,7 @@ public void testAnalyze() {
binaryPredicate,
likePredicate);

deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", compoundPredicate, null);
deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", compoundPredicate);
try {
deleteStmt.analyze(analyzer);
} catch (UserException e) {
Expand All @@ -111,7 +111,7 @@ public void testAnalyze() {
binaryPredicate,
binaryPredicate);

deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", compoundPredicate, null);
deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", compoundPredicate);
try {
deleteStmt.analyze(analyzer);
} catch (UserException e) {
Expand All @@ -125,24 +125,24 @@ public void testAnalyze() {
binaryPredicate,
binaryPredicate);

deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", compoundPredicate, null);
deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", compoundPredicate);
try {
deleteStmt.analyze(analyzer);
} catch (UserException e) {
Assert.assertTrue(e.getMessage().contains("Left expr should be column name"));
}

// case 6 partition is null
binaryPredicate = new BinaryPredicate(Operator.EQ, new StringLiteral("abc"),
new SlotRef(null, "k1"));
binaryPredicate = new BinaryPredicate(Operator.EQ, new SlotRef(null, "k1"), new StringLiteral("abc"));
compoundPredicate = new CompoundPredicate(org.apache.doris.analysis.CompoundPredicate.Operator.AND,
binaryPredicate,
binaryPredicate);

deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), null, compoundPredicate, null);
deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), null, compoundPredicate);
try {
deleteStmt.analyze(analyzer);
} catch (UserException e) {
e.printStackTrace();
Assert.assertTrue(e.getMessage().contains("Partition is not set"));
}

Expand All @@ -157,7 +157,7 @@ public void testAnalyze() {
binaryPredicate,
compoundPredicate2);

deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", compoundPredicate, null);
deleteStmt = new DeleteStmt(new TableName("testDb", "testTbl"), "partition", compoundPredicate);
try {
deleteStmt.analyze(analyzer);
} catch (UserException e) {
Expand Down