Skip to content

Commit

Permalink
add test for rule EliminateGroupByKeyByUniform and change computeUnif…
Browse files Browse the repository at this point in the history
…orm in LogicalProject
  • Loading branch information
feiniaofeiafei committed Nov 12, 2024
1 parent 452bb5f commit 0c01e45
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ public boolean isUniformAndNotNull(ImmutableSet<Slot> slotSet) {
return true;
}

public boolean isUniformAndHasConstValue(Slot slot) {
return uniformSet.isUniformAndHasConstValue(slot);
}

public Optional<Expression> getUniformValue(Slot slot) {
return uniformSet.slotUniformValue.get(slot);
}

public boolean isNullSafeEqual(Slot l, Slot r) {
return equalSet.isEqual(l, r);
}
Expand Down Expand Up @@ -565,6 +573,10 @@ public boolean isUniformAndNotNull(Slot slot) {
&& !slotUniformValue.get(slot).get().nullable());
}

public boolean isUniformAndHasConstValue(Slot slot) {
return slotUniformValue.containsKey(slot) && slotUniformValue.get(slot).isPresent();
}

@Override
public String toString() {
return "{" + slotUniformValue + "}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
* +--aggregate(group by a,b output a,b,max(c))
Expand All @@ -41,7 +43,7 @@
public class EliminateGroupByKeyByUniform extends OneRewriteRuleFactory {
@Override
public Rule build() {
return logicalAggregate().when(agg -> !agg.getSourceRepeat().isPresent())
return logicalAggregate().whenNot(agg -> agg.getSourceRepeat().isPresent())
.whenNot(agg -> agg.getGroupByExpressions().isEmpty())
.then(EliminateGroupByKeyByUniform::eliminate)
.toRule(RuleType.ELIMINATE_GROUP_BY_KEY_BY_UNIFORM);
Expand All @@ -51,7 +53,7 @@ public Rule build() {
private static Plan eliminate(LogicalAggregate<Plan> agg) {
DataTrait aggChildTrait = agg.child().getLogicalProperties().getTrait();
// Get the Group by column of agg. If there is a uniform one, delete the group by key.
List<Expression> removedExpression = new ArrayList<>();
Set<Expression> removedExpression = new LinkedHashSet<>();
List<Expression> newGroupBy = new ArrayList<>();
for (Expression groupBy : agg.getGroupByExpressions()) {
if (!(groupBy instanceof Slot)) {
Expand All @@ -64,13 +66,20 @@ private static Plan eliminate(LogicalAggregate<Plan> agg) {
newGroupBy.add(groupBy);
}
}
// TODO Consider whether there are other opportunities for optimization when newGroupBy is empty
if (removedExpression.isEmpty()) {
return null;
}
// when newGroupBy is empty, need retain one expr in group by, otherwise the result may be wrong in empty table
if (newGroupBy.isEmpty()) {
Expression expr = removedExpression.iterator().next();
newGroupBy.add(expr);
removedExpression.remove(expr);
}
if (removedExpression.isEmpty()) {
return null;
}

List<NamedExpression> newOutputs = new ArrayList<>();
// If this output appears in the removedExpression column, replace it with anyvalue
// If this output appears in the removedExpression column, replace it with any_value
for (NamedExpression output : agg.getOutputExpressions()) {
if (output instanceof Slot) {
if (removedExpression.contains(output)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,16 @@ public void computeUnique(DataTrait.Builder builder) {
public void computeUniform(DataTrait.Builder builder) {
builder.addUniformSlot(child(0).getLogicalProperties().getTrait());
for (NamedExpression proj : getProjects()) {
if (proj.children().isEmpty()) {
if (!(proj instanceof Alias)) {
continue;
}
if (proj.child(0).isConstant()) {
builder.addUniformSlotAndLiteral(proj.toSlot(), proj.child(0));
} else if (ExpressionUtils.isInjective(proj.child(0))) {
ImmutableSet<Slot> inputs = ImmutableSet.copyOf(proj.getInputSlots());
if (child(0).getLogicalProperties().getTrait().isUniform(inputs)) {
builder.addUniformSlot(proj.toSlot());
} else if (proj.child(0) instanceof Slot) {
Slot slot = (Slot) proj.child(0);
if (child(0).getLogicalProperties().getTrait().isUniformAndHasConstValue(slot)) {
builder.addUniformSlotAndLiteral(proj.toSlot(),
child(0).getLogicalProperties().getTrait().getUniformValue(slot).get());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
// 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.

package org.apache.doris.nereids.rules.rewrite;

import org.apache.doris.nereids.util.MemoPatternMatchSupported;
Expand Down

0 comments on commit 0c01e45

Please sign in to comment.