-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Optimize const grouping keys #17642
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
Merged
kaikalur
merged 1 commit into
prestodb:master
from
mlyublena:optimize-const-grouping-keys
Nov 4, 2022
Merged
Optimize const grouping keys #17642
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...rk/src/main/java/com/facebook/presto/benchmark/SqlPullConstantsAboveGroupByBenchmark.java
This file contains hidden or 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,48 @@ | ||
| /* | ||
| * 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.benchmark; | ||
|
|
||
| import com.facebook.airlift.log.Logger; | ||
| import com.facebook.presto.testing.LocalQueryRunner; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import org.intellij.lang.annotations.Language; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import static com.facebook.presto.benchmark.BenchmarkQueryRunner.createLocalQueryRunner; | ||
|
|
||
| public class SqlPullConstantsAboveGroupByBenchmark | ||
| extends AbstractSqlBenchmark | ||
| { | ||
| private static final Logger LOGGER = Logger.get(SqlRewriteConditionalAggregationBenchmarks.class); | ||
|
|
||
| public SqlPullConstantsAboveGroupByBenchmark(LocalQueryRunner localQueryRunner, @Language("SQL") String sql) | ||
| { | ||
| super(localQueryRunner, | ||
| "pull_constants_above_group_by", | ||
| 10, | ||
| 20, | ||
| sql); | ||
| } | ||
|
|
||
| public static void main(String[] args) | ||
| { | ||
| Map<String, String> disableOptimization = ImmutableMap.of("optimize_constant_grouping_keys", "false"); | ||
| String sql = "SELECT * FROM (SELECT regionkey, col, count(*) FROM (SELECT regionkey, 'bla' as col FROM nation) GROUP BY regionkey, col)"; | ||
| LOGGER.info("Without optimization"); | ||
| new SqlPullConstantsAboveGroupByBenchmark(createLocalQueryRunner(disableOptimization), sql).runBenchmark(new SimpleLineBenchmarkResultWriter(System.out)); | ||
| LOGGER.info("With optimization"); | ||
| new SqlPullConstantsAboveGroupByBenchmark(createLocalQueryRunner(), sql).runBenchmark(new SimpleLineBenchmarkResultWriter(System.out)); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
142 changes: 142 additions & 0 deletions
142
...c/main/java/com/facebook/presto/sql/planner/iterative/rule/PullConstantsAboveGroupBy.java
This file contains hidden or 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,142 @@ | ||
| /* | ||
| * 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.iterative.rule; | ||
|
|
||
| import com.facebook.presto.Session; | ||
| import com.facebook.presto.matching.Capture; | ||
| import com.facebook.presto.matching.Captures; | ||
| import com.facebook.presto.matching.Pattern; | ||
| import com.facebook.presto.spi.plan.AggregationNode; | ||
| import com.facebook.presto.spi.plan.Assignments; | ||
| import com.facebook.presto.spi.plan.ProjectNode; | ||
| import com.facebook.presto.spi.relation.ConstantExpression; | ||
| import com.facebook.presto.spi.relation.RowExpression; | ||
| import com.facebook.presto.spi.relation.VariableReferenceExpression; | ||
| import com.facebook.presto.sql.planner.iterative.Rule; | ||
| import com.google.common.collect.ImmutableList; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static com.facebook.presto.SystemSessionProperties.isOptimizeConstantGroupingKeys; | ||
| import static com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet; | ||
| import static com.facebook.presto.sql.analyzer.ExpressionTreeUtils.isConstant; | ||
| import static com.facebook.presto.sql.planner.plan.Patterns.aggregation; | ||
| import static com.facebook.presto.sql.planner.plan.Patterns.project; | ||
| import static com.facebook.presto.sql.planner.plan.Patterns.source; | ||
| import static com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression; | ||
| import static com.facebook.presto.sql.relational.OriginalExpressionUtils.isExpression; | ||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
| import static java.util.function.Function.identity; | ||
|
|
||
| /** | ||
| * Transforms: | ||
| * <pre> | ||
| * - GroupBy key1, <const_expr>, key2 | ||
| * </pre> | ||
| * Into: | ||
| * <pre> | ||
| * - Project <const_expr> | ||
| * - GroupBy key1, key2 | ||
| * </pre> | ||
| */ | ||
| public class PullConstantsAboveGroupBy | ||
| implements Rule<AggregationNode> | ||
| { | ||
| private static final Capture<ProjectNode> SOURCE = Capture.newCapture(); | ||
|
|
||
| private static final Pattern<AggregationNode> PATTERN = | ||
| aggregation() | ||
| .matching(agg -> agg.getGroupingSetCount() == 1) | ||
| .with(source().matching(project().capturedAs(SOURCE))); | ||
|
|
||
| @Override | ||
| public boolean isEnabled(Session session) | ||
| { | ||
| return isOptimizeConstantGroupingKeys(session); | ||
| } | ||
|
|
||
| @Override | ||
| public Pattern<AggregationNode> getPattern() | ||
| { | ||
| return PATTERN; | ||
| } | ||
|
|
||
| @Override | ||
| public Result apply(AggregationNode parent, Captures captures, Context context) | ||
| { | ||
| if (!isEnabled(context.getSession())) { | ||
| return Result.empty(); | ||
| } | ||
|
|
||
| // for each variable references in grouping keys, check if the source expression defines them as constants | ||
| ProjectNode source = captures.get(SOURCE); | ||
| List<VariableReferenceExpression> outputVariables = parent.getOutputVariables(); | ||
|
|
||
| Map<VariableReferenceExpression, RowExpression> constSourceVars = extractConstVars(source, outputVariables); | ||
|
|
||
| List<VariableReferenceExpression> groupingKeys = parent.getGroupingKeys(); | ||
| List<VariableReferenceExpression> newGroupingKeys = | ||
| groupingKeys.stream() | ||
| .filter(key -> !constSourceVars.containsKey(key)) | ||
| .collect(toImmutableList()); | ||
|
|
||
| if (constSourceVars.isEmpty() || newGroupingKeys.equals(groupingKeys)) { | ||
| return Result.empty(); | ||
| } | ||
|
|
||
| AggregationNode newAgg = new AggregationNode( | ||
| parent.getSourceLocation(), | ||
| parent.getId(), | ||
| source, | ||
| parent.getAggregations(), | ||
| singleGroupingSet(newGroupingKeys), | ||
| ImmutableList.of(), | ||
| parent.getStep(), | ||
| parent.getHashVariable(), | ||
| parent.getGroupIdVariable()); | ||
|
|
||
| Map<VariableReferenceExpression, RowExpression> remainingVars = | ||
| outputVariables.stream() | ||
| .filter(var -> !constSourceVars.containsKey(var)) | ||
| .collect(toImmutableMap(identity(), identity())); | ||
|
|
||
| Assignments.Builder assignments = Assignments.builder(); | ||
| assignments.putAll(constSourceVars); | ||
| assignments.putAll(remainingVars); | ||
| return Result.ofPlanNode( | ||
| new ProjectNode( | ||
| parent.getSourceLocation(), | ||
| context.getIdAllocator().getNextId(), | ||
| newAgg, | ||
| assignments.build(), | ||
| source.getLocality())); | ||
| } | ||
|
|
||
| private static Map<VariableReferenceExpression, RowExpression> extractConstVars(ProjectNode projectNode, List<VariableReferenceExpression> outputVariables) | ||
| { | ||
| return projectNode.getAssignments().entrySet().stream() | ||
| .filter((entry) -> isConstantRowExpr(entry.getValue()) && outputVariables.contains(entry.getKey())) | ||
| .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
| } | ||
|
|
||
| private static boolean isConstantRowExpr(RowExpression expr) | ||
| { | ||
| if (isExpression(expr)) { | ||
| return isConstant(castToExpression(expr)); | ||
| } | ||
| return expr instanceof ConstantExpression; | ||
| } | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Added a benchmark test @kaikalur: