Skip to content

Commit

Permalink
[Feature](inverted index) push count on index down to scan node
Browse files Browse the repository at this point in the history
  • Loading branch information
airborne12 committed Aug 8, 2023
1 parent 5b880c9 commit 9099aeb
Showing 1 changed file with 11 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import org.apache.doris.nereids.trees.expressions.Match;
import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
Expand All @@ -34,7 +32,6 @@

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Rewriter of pushing down count on index.
Expand All @@ -47,37 +44,22 @@ public Rule build() {
logicalProject(
logicalFilter(
logicalOlapScan().whenNot(LogicalOlapScan::isCountOnIndexPushedDown)
)
).when(filter -> containsMatchExpression(filter.getExpressions())
&& filter.getExpressions().size() == 1)
))
.when(agg -> enablePushDownCountOnIndex())
.thenApply(ctx -> {
LogicalAggregate<LogicalProject<LogicalFilter<LogicalOlapScan>>> aggregate = ctx.root;
final LogicalAggregate<? extends Plan> canNotPush = aggregate;

LogicalProject<LogicalFilter<LogicalOlapScan>> project = aggregate.child();
.when(agg -> agg.getGroupByExpressions().size() == 0)
.when(agg -> {
Set<AggregateFunction> funcs = agg.getAggregateFunctions();
return !funcs.isEmpty() && funcs.stream()
.allMatch(f -> f instanceof Count && !f.isDistinct());
})
.then(agg -> {
LogicalProject<LogicalFilter<LogicalOlapScan>> project = agg.child();
LogicalFilter<LogicalOlapScan> filter = project.child();
LogicalOlapScan olapScan = filter.child();
Set<AggregateFunction> aggregateFunctions = aggregate.getAggregateFunctions();
Set<Class<? extends AggregateFunction>> functionClasses = aggregateFunctions
.stream()
.map(AggregateFunction::getClass)
.collect(Collectors.toSet());

//if and only if there is a count on agg
if (!(functionClasses.size() == 1 && functionClasses.contains(Count.class))) {
return canNotPush;
}
if (aggregateFunctions.stream().anyMatch(fun -> fun.arity() > 1)) {
return canNotPush;
}
List<Expression> expressions = filter.getExpressions();
if (expressions.size() > 1) {
return canNotPush;
}
if (!containsMatchExpression(expressions)) {
return canNotPush;
}
return aggregate.withChildren(ImmutableList.of(
return agg.withChildren(ImmutableList.of(
project.withChildren(
ImmutableList.of(
filter.withChildren(olapScan.withPushDownCountOnIndex(true))
Expand Down

0 comments on commit 9099aeb

Please sign in to comment.