Skip to content

Commit 095721c

Browse files
DarvenDuandataroaring
authored andcommitted
[bug](delete) fix delete random distributed tbl (#37985)
## Proposed changes Bug description: In PR #33630, Doris supports auto aggregation for random distributed table, but it not only effects query statements, so if we delete from a random distributed table, will get an error because of unexpectedly rewriting. ``` CREATE TABLE `test_tbl` ( `k` INT NULL, `v` BIGINT SUM NULL ) ENGINE=OLAP AGGREGATE KEY(`k`) DISTRIBUTED BY RANDOM BUCKETS AUTO; mysql > delete from test_tbl where k=1; ERROR 1105 (HY000): errCode = 2, detailMessage = Where clause only supports compound predicate, binary predicate, is_null predicate or in predicate. ``` fix: Check whether it is a query statement before rewriting.
1 parent d89c864 commit 095721c

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BuildAggForRandomDistributedTable.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
5252
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
5353
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
54+
import org.apache.doris.qe.ConnectContext;
5455

5556
import com.google.common.collect.ImmutableList;
5657

@@ -67,22 +68,29 @@ public class BuildAggForRandomDistributedTable implements AnalysisRuleFactory {
6768
public List<Rule> buildRules() {
6869
return ImmutableList.of(
6970
// Project(Scan) -> project(agg(scan))
70-
logicalProject(logicalOlapScan()).when(project -> isRandomDistributedTbl(project.child()))
71+
logicalProject(logicalOlapScan())
72+
.when(this::isQuery)
73+
.when(project -> isRandomDistributedTbl(project.child()))
7174
.then(project -> preAggForRandomDistribution(project, project.child()))
7275
.toRule(RuleType.BUILD_AGG_FOR_RANDOM_DISTRIBUTED_TABLE_PROJECT_SCAN),
7376
// agg(scan) -> agg(agg(scan)), agg(agg) may optimized by MergeAggregate
74-
logicalAggregate(logicalOlapScan()).when(agg -> isRandomDistributedTbl(agg.child())).whenNot(agg -> {
75-
Set<AggregateFunction> functions = agg.getAggregateFunctions();
76-
List<Expression> groupByExprs = agg.getGroupByExpressions();
77-
// check if need generate an inner agg plan or not
78-
// should not rewrite twice if we had rewritten olapScan to aggregate(olapScan)
79-
return functions.stream().allMatch(this::aggTypeMatch) && groupByExprs.stream()
77+
logicalAggregate(logicalOlapScan())
78+
.when(this::isQuery)
79+
.when(agg -> isRandomDistributedTbl(agg.child()))
80+
.whenNot(agg -> {
81+
Set<AggregateFunction> functions = agg.getAggregateFunctions();
82+
List<Expression> groupByExprs = agg.getGroupByExpressions();
83+
// check if need generate an inner agg plan or not
84+
// should not rewrite twice if we had rewritten olapScan to aggregate(olapScan)
85+
return functions.stream().allMatch(this::aggTypeMatch) && groupByExprs.stream()
8086
.allMatch(this::isKeyOrConstantExpr);
81-
})
87+
})
8288
.then(agg -> preAggForRandomDistribution(agg, agg.child()))
8389
.toRule(RuleType.BUILD_AGG_FOR_RANDOM_DISTRIBUTED_TABLE_AGG_SCAN),
8490
// filter(scan) -> filter(agg(scan))
85-
logicalFilter(logicalOlapScan()).when(filter -> isRandomDistributedTbl(filter.child()))
91+
logicalFilter(logicalOlapScan())
92+
.when(this::isQuery)
93+
.when(filter -> isRandomDistributedTbl(filter.child()))
8694
.then(filter -> preAggForRandomDistribution(filter, filter.child()))
8795
.toRule(RuleType.BUILD_AGG_FOR_RANDOM_DISTRIBUTED_TABLE_FILTER_SCAN));
8896

@@ -101,6 +109,12 @@ private boolean isRandomDistributedTbl(LogicalOlapScan olapScan) {
101109
return keysType == KeysType.AGG_KEYS && distributionInfo.getType() == DistributionInfoType.RANDOM;
102110
}
103111

112+
private boolean isQuery(LogicalPlan plan) {
113+
return ConnectContext.get() != null
114+
&& ConnectContext.get().getState() != null
115+
&& ConnectContext.get().getState().isQuery();
116+
}
117+
104118
/**
105119
* add LogicalAggregate above olapScan for preAgg
106120
*
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !sql --
3+
1 10
4+
2 30
5+
6+
-- !sql --
7+
2 30
8+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("test_delete_from_random_distributed_tbl") {
19+
def tableName = "test_delete_from_random_distributed_tbl"
20+
21+
sql """ DROP TABLE IF EXISTS ${tableName}; """
22+
sql """ CREATE TABLE ${tableName} (
23+
`k` INT NULL,
24+
`v` BIGINT SUM NULL
25+
) ENGINE=OLAP
26+
AGGREGATE KEY(`k`)
27+
DISTRIBUTED BY RANDOM BUCKETS 4
28+
PROPERTIES ("replication_num"="1")
29+
"""
30+
31+
sql """ insert into ${tableName} values(1, 10),(2,10),(2,20)"""
32+
qt_sql """ select * from ${tableName} order by k """
33+
34+
sql """ delete from ${tableName} where k=1 """
35+
qt_sql """ select * from ${tableName} order by k """
36+
37+
sql """ DROP TABLE IF EXISTS ${tableName}; """
38+
}

0 commit comments

Comments
 (0)