Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optimizer to aggregate distinct and non-distinct inputs separately
- Loading branch information
1 parent
6071aa5
commit 4f06521
Showing
11 changed files
with
865 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
509 changes: 509 additions & 0 deletions
509
...java/com/facebook/presto/sql/planner/optimizations/OptimizeMixedDistinctAggregations.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
presto-main/src/test/java/com/facebook/presto/sql/planner/assertions/AggregationMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.sql.planner.assertions; | ||
|
||
import com.facebook.presto.Session; | ||
import com.facebook.presto.metadata.Metadata; | ||
import com.facebook.presto.sql.planner.Symbol; | ||
import com.facebook.presto.sql.planner.plan.AggregationNode; | ||
import com.facebook.presto.sql.planner.plan.PlanNode; | ||
import com.facebook.presto.sql.tree.FunctionCall; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.google.common.base.MoreObjects.toStringHelper; | ||
|
||
public class AggregationMatcher | ||
implements Matcher | ||
{ | ||
private final List<FunctionCall> aggregations; | ||
private final Map<Symbol, Symbol> masks; | ||
private final List<List<Symbol>> groupingSets; | ||
private final Optional<Symbol> groupId; | ||
|
||
public AggregationMatcher(List<List<Symbol>> groupingSets, List<FunctionCall> aggregations, Map<Symbol, Symbol> masks, Optional<Symbol> groupId) | ||
{ | ||
this.aggregations = aggregations; | ||
this.masks = masks; | ||
this.groupingSets = groupingSets; | ||
this.groupId = groupId; | ||
} | ||
|
||
@Override | ||
public boolean matches(PlanNode node, Session session, Metadata metadata, ExpressionAliases expressionAliases) | ||
{ | ||
if (!(node instanceof AggregationNode)) { | ||
return false; | ||
} | ||
|
||
AggregationNode aggregationNode = (AggregationNode) node; | ||
|
||
if (groupId.isPresent() != aggregationNode.getGroupIdSymbol().isPresent()) { | ||
return false; | ||
} | ||
|
||
if (groupingSets.size() != aggregationNode.getGroupingSets().size()) { | ||
return false; | ||
} | ||
|
||
List<Symbol> aggregationsWithMask = aggregationNode.getAggregations() | ||
.entrySet() | ||
.stream() | ||
.filter(entry -> entry.getValue().isDistinct()) | ||
.map(entry -> entry.getKey()) | ||
.collect(Collectors.toList()); | ||
|
||
if (aggregationsWithMask.size() != masks.keySet().size()) { | ||
return false; | ||
} | ||
|
||
for (Symbol symbol : aggregationsWithMask) { | ||
if (!masks.keySet().contains(symbol)) { | ||
return false; | ||
} | ||
} | ||
|
||
for (int i = 0; i < groupingSets.size(); i++) { | ||
if (!matches(groupingSets.get(i), aggregationNode.getGroupingSets().get(i))) { | ||
return false; | ||
} | ||
} | ||
|
||
if (!matches(aggregations, aggregationNode.getAggregations().values().stream().collect(Collectors.toList()))) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static <T> boolean matches(Collection<T> expected, Collection<T> actual) | ||
{ | ||
if (expected.size() != actual.size()) { | ||
return false; | ||
} | ||
|
||
for (T symbol : expected) { | ||
if (!actual.contains(symbol)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return toStringHelper(this) | ||
.add("groupingSets", groupingSets) | ||
.add("aggregations", aggregations) | ||
.add("masks", masks) | ||
.add("groudId", groupId) | ||
.toString(); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
presto-main/src/test/java/com/facebook/presto/sql/planner/assertions/GroupIdMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.sql.planner.assertions; | ||
|
||
import com.facebook.presto.Session; | ||
import com.facebook.presto.metadata.Metadata; | ||
import com.facebook.presto.sql.planner.Symbol; | ||
import com.facebook.presto.sql.planner.plan.GroupIdNode; | ||
import com.facebook.presto.sql.planner.plan.PlanNode; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.google.common.base.MoreObjects.toStringHelper; | ||
|
||
public class GroupIdMatcher | ||
implements Matcher | ||
{ | ||
private final List<List<Symbol>> groups; | ||
private final Map<Symbol, Symbol> identityMappings; | ||
|
||
public GroupIdMatcher(List<List<Symbol>> groups, Map<Symbol, Symbol> identityMappings) | ||
{ | ||
this.groups = groups; | ||
this.identityMappings = identityMappings; | ||
} | ||
|
||
@Override | ||
public boolean matches(PlanNode node, Session session, Metadata metadata, ExpressionAliases expressionAliases) | ||
{ | ||
if (!(node instanceof GroupIdNode)) { | ||
return false; | ||
} | ||
|
||
GroupIdNode groudIdNode = (GroupIdNode) node; | ||
List<List<Symbol>> actualGroups = groudIdNode.getGroupingSets(); | ||
Map<Symbol, Symbol> actualArgumentMappings = groudIdNode.getArgumentMappings(); | ||
|
||
if (actualGroups.size() != groups.size()) { | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < actualGroups.size(); i++) { | ||
if (!AggregationMatcher.matches(actualGroups.get(i), groups.get(i))) { | ||
return false; | ||
} | ||
} | ||
|
||
if (!AggregationMatcher.matches(identityMappings.keySet(), actualArgumentMappings.keySet())) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return toStringHelper(this) | ||
.add("groups", groups) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.