-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Custom Calcite Rule to remove redundant references #16402
Changes from 7 commits
63208f4
9d5561d
76d0c47
f972b16
824ab85
26fad89
a649acc
e82b4a9
b174754
4152d44
190d7ef
0c47a08
500db4c
73f52e5
78e55a3
43f8744
a244686
343f158
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
* 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.druid.sql.calcite.rule.logical; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Sets; | ||
import org.apache.calcite.plan.RelOptRuleCall; | ||
import org.apache.calcite.plan.RelOptUtil; | ||
import org.apache.calcite.plan.RelRule; | ||
import org.apache.calcite.rel.RelNode; | ||
import org.apache.calcite.rel.core.Aggregate; | ||
import org.apache.calcite.rel.core.Aggregate.Group; | ||
import org.apache.calcite.rel.core.AggregateCall; | ||
import org.apache.calcite.rel.core.Project; | ||
import org.apache.calcite.rel.rules.TransformationRule; | ||
import org.apache.calcite.rex.RexInputRef; | ||
import org.apache.calcite.rex.RexNode; | ||
import org.apache.calcite.tools.RelBuilder; | ||
import org.apache.calcite.util.ImmutableBitSet; | ||
import org.apache.calcite.util.Util; | ||
import org.apache.calcite.util.mapping.Mappings; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.immutables.value.Value; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* Planner rule that recognizes a {@link Aggregate} | ||
* on top of a {@link Project} and if possible | ||
* aggregate through the project or removes the project. | ||
* | ||
* This is updated version of {@link org.apache.calcite.rel.rules.AggregateProjectMergeRule} | ||
* to be able to handle expressions. | ||
*/ | ||
@Value.Enclosing | ||
public class DruidAggregateRemoveRedundancyRule | ||
extends RelRule<DruidAggregateRemoveRedundancyRule.Config> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could possibly extend or optionally move that immutables generated stuff inside this class somewhere - so its co-located There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. made use of RelOptRule and got rid of the Immutable class |
||
implements TransformationRule | ||
{ | ||
|
||
/** | ||
* Creates a DruidAggregateRemoveRedundancyRule. | ||
*/ | ||
protected DruidAggregateRemoveRedundancyRule(Config config) | ||
{ | ||
super(config); | ||
} | ||
|
||
@Override | ||
public void onMatch(RelOptRuleCall call) | ||
{ | ||
final Aggregate aggregate = call.rel(0); | ||
final Project project = call.rel(1); | ||
RelNode x = apply(call, aggregate, project); | ||
if (x != null) { | ||
call.transformTo(x); | ||
call.getPlanner().prune(aggregate); | ||
} | ||
} | ||
|
||
public static @Nullable RelNode apply(RelOptRuleCall call, Aggregate aggregate, Project project) | ||
{ | ||
final Set<Integer> interestingFields = RelOptUtil.getAllFields(aggregate); | ||
if (interestingFields.isEmpty()) { | ||
return null; | ||
} | ||
final Map<Integer, Integer> map = new HashMap<>(); | ||
final Map<RexNode, Integer> assignedNodeForExpr = new HashMap<>(); | ||
List<RexNode> newRexNodes = new ArrayList<>(); | ||
for (int source : interestingFields) { | ||
final RexNode rex = project.getProjects().get(source); | ||
if (!assignedNodeForExpr.containsKey(rex)) { | ||
RexNode newNode = new RexInputRef(source, rex.getType()); | ||
assignedNodeForExpr.put(rex, newRexNodes.size()); | ||
newRexNodes.add(newNode); | ||
} | ||
map.put(source, assignedNodeForExpr.get(rex)); | ||
} | ||
|
||
if (newRexNodes.size() == project.getProjects().size()) { | ||
return null; | ||
} | ||
|
||
final ImmutableBitSet newGroupSet = aggregate.getGroupSet().permute(map); | ||
ImmutableList<ImmutableBitSet> newGroupingSets = null; | ||
if (aggregate.getGroupType() != Group.SIMPLE) { | ||
newGroupingSets = | ||
ImmutableBitSet.ORDERING.immutableSortedCopy( | ||
Sets.newTreeSet(ImmutableBitSet.permute(aggregate.getGroupSets(), map))); | ||
} | ||
|
||
final ImmutableList.Builder<AggregateCall> aggCalls = ImmutableList.builder(); | ||
final int sourceCount = aggregate.getInput().getRowType().getFieldCount(); | ||
final int targetCount = newRexNodes.size(); | ||
final Mappings.TargetMapping targetMapping = Mappings.target(map, sourceCount, targetCount); | ||
for (AggregateCall aggregateCall : aggregate.getAggCallList()) { | ||
aggCalls.add(aggregateCall.transform(targetMapping)); | ||
} | ||
|
||
final RelBuilder relBuilder = call.builder(); | ||
relBuilder.push(project); | ||
relBuilder.project(newRexNodes); | ||
|
||
final Aggregate newAggregate = | ||
aggregate.copy(aggregate.getTraitSet(), relBuilder.build(), | ||
newGroupSet, newGroupingSets, aggCalls.build() | ||
); | ||
relBuilder.push(newAggregate); | ||
|
||
final List<Integer> newKeys = | ||
Util.transform( | ||
aggregate.getGroupSet().asList(), | ||
key -> Objects.requireNonNull( | ||
map.get(key), | ||
() -> "no value found for key " + key + " in " + map | ||
) | ||
); | ||
|
||
// Add a project if the group set is not in the same order or | ||
// contains duplicates. | ||
if (!newKeys.equals(newGroupSet.asList())) { | ||
final List<Integer> posList = new ArrayList<>(); | ||
for (int newKey : newKeys) { | ||
posList.add(newGroupSet.indexOf(newKey)); | ||
} | ||
for (int i = newAggregate.getGroupCount(); | ||
i < newAggregate.getRowType().getFieldCount(); i++) { | ||
posList.add(i); | ||
} | ||
relBuilder.project(relBuilder.fields(posList)); | ||
} | ||
|
||
return relBuilder.build(); | ||
} | ||
|
||
/** | ||
* Rule configuration. | ||
*/ | ||
@Value.Immutable | ||
public interface Config extends RelRule.Config | ||
|
||
{ | ||
Config DEFAULT = DruidImmutableAggregateRemoveRedundancyRule.Config.of() | ||
.withOperandFor(Aggregate.class, Project.class); | ||
|
||
@Override | ||
default DruidAggregateRemoveRedundancyRule toRule() | ||
{ | ||
return new DruidAggregateRemoveRedundancyRule(this); | ||
} | ||
|
||
/** | ||
* Defines an operand tree for the given classes. | ||
*/ | ||
default Config withOperandFor( | ||
Class<? extends Aggregate> aggregateClass, | ||
Class<? extends Project> projectClass | ||
) | ||
{ | ||
return withOperandSupplier(b0 -> | ||
b0.operand(aggregateClass).oneInput(b1 -> | ||
b1.operand(projectClass).anyInputs())).as( | ||
Config.class); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't this will mask the newly added check in
GroupingAggregatorFactory
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made this a soft exception and removed this distinct masking here