Skip to content

Commit

Permalink
replace scan by empty relation when all partitions are pruned
Browse files Browse the repository at this point in the history
  • Loading branch information
englefly committed Nov 7, 2023
1 parent 99de6c7 commit a6cae36
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import org.apache.doris.nereids.rules.expression.rules.PartitionPruner;
import org.apache.doris.nereids.rules.expression.rules.PartitionPruner.PartitionTableType;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.plans.logical.LogicalEmptyRelation;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.nereids.util.Utils;

import com.google.common.collect.ImmutableList;
import org.apache.commons.collections.CollectionUtils;
import org.apache.doris.qe.ConnectContext;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -73,6 +75,11 @@ public Rule build() {
if (!CollectionUtils.isEmpty(manuallySpecifiedPartitions)) {
prunedPartitions.retainAll(manuallySpecifiedPartitions);
}
if (prunedPartitions.isEmpty()) {
return new LogicalEmptyRelation(
ConnectContext.get().getStatementContext().getNextRelationId(),
filter.getOutput());
}
LogicalOlapScan rewrittenScan = scan.withSelectedPartitionIds(ImmutableList.copyOf(prunedPartitions));
return new LogicalFilter<>(filter.getConjuncts(), rewrittenScan);
}).toRule(RuleType.OLAP_SCAN_PARTITION_PRUNE);
Expand Down
10 changes: 10 additions & 0 deletions regression-test/data/empty_relation/eliminate_empty.out
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ PhysicalResultSink

-- !except_empty_data --

-- !prune_partition1 --
PhysicalResultSink
--PhysicalProject
----PhysicalEmptyRelation

-- !prune_partition2 --
PhysicalResultSink
--PhysicalProject
----PhysicalEmptyRelation

36 changes: 36 additions & 0 deletions regression-test/suites/empty_relation/eliminate_empty.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,40 @@ suite("eliminate_empty") {
qt_except_empty_data """
select r_regionkey from region where false except select n_nationkey from nation
"""

sql """
drop table if exists eliminate_partition_prune;
"""
sql """
CREATE TABLE `eliminate_partition_prune` (
`k1` int(11) NULL COMMENT "",
`k2` int(11) NULL COMMENT "",
`k3` int(11) NULL COMMENT ""
)
PARTITION BY RANGE(`k1`, `k2`)
(PARTITION p1 VALUES LESS THAN ("3", "1"),
PARTITION p2 VALUES [("3", "1"), ("7", "10")),
PARTITION p3 VALUES [("7", "10"), ("10", "15")))
DISTRIBUTED BY HASH(`k1`) BUCKETS 10
PROPERTIES ('replication_num' = '1');
"""


qt_prune_partition1 """
explain shape plan
select sum(k2)
from
(select * from eliminate_partition_prune where k1=100) T
group by k3;
"""
sql """
insert into eliminate_partition_prune values (7, 0, 0)
"""
qt_prune_partition2 """
explain shape plan
select sum(k2)
from
(select * from eliminate_partition_prune where k1=100) T
group by k3;
"""
}

0 comments on commit a6cae36

Please sign in to comment.