-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Evaluate project node on values node #23245
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
ZacBlanco
merged 1 commit into
prestodb:master
from
jackychen718:EvaluateProjectOnValues
Aug 29, 2024
Merged
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
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
135 changes: 135 additions & 0 deletions
135
...c/main/java/com/facebook/presto/sql/planner/iterative/rule/InlineProjectionsOnValues.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,135 @@ | ||
| /* | ||
| * 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.metadata.FunctionAndTypeManager; | ||
| import com.facebook.presto.spi.plan.ProjectNode; | ||
| import com.facebook.presto.spi.plan.ValuesNode; | ||
| import com.facebook.presto.spi.relation.DeterminismEvaluator; | ||
| import com.facebook.presto.spi.relation.RowExpression; | ||
| import com.facebook.presto.spi.relation.VariableReferenceExpression; | ||
| import com.facebook.presto.sql.planner.iterative.Rule; | ||
| import com.facebook.presto.sql.relational.RowExpressionDeterminismEvaluator; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.Streams; | ||
|
|
||
| import java.util.AbstractMap.SimpleImmutableEntry; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import static com.facebook.presto.SystemSessionProperties.isInlineProjectionsOnValues; | ||
| import static com.facebook.presto.matching.Capture.newCapture; | ||
| import static com.facebook.presto.sql.planner.RowExpressionVariableInliner.inlineVariables; | ||
| 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.planner.plan.Patterns.values; | ||
| import static com.google.common.base.Verify.verify; | ||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * This optimizer looks for ProjectNode followed by a ValuesNode and get the ProjectNode Evaluated. | ||
| * When this rule is used on iterative optimizer, the rule could apply iteratively. | ||
| * <p/> | ||
| * Plan before optimizer: | ||
| * <pre> | ||
| * ProjectNode (outputVariables) | ||
| * - ValuesNode | ||
| * </pre> | ||
| * <p/> | ||
| * Plan after optimizer: | ||
| * <pre> | ||
| * ValuesNode (outputVariables) | ||
| * </pre> | ||
| */ | ||
| public class InlineProjectionsOnValues | ||
| implements Rule<ProjectNode> | ||
| { | ||
| private static final Capture<ValuesNode> CHILD = newCapture(); | ||
|
|
||
| private static final Pattern<ProjectNode> PATTERN = project() | ||
| .with(source().matching(values().capturedAs(CHILD))); | ||
|
|
||
| private final FunctionAndTypeManager functionAndTypeManager; | ||
|
|
||
| public InlineProjectionsOnValues(FunctionAndTypeManager functionAndTypeManager) | ||
| { | ||
| this.functionAndTypeManager = requireNonNull(functionAndTypeManager, "functionManager is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public Pattern<ProjectNode> getPattern() | ||
| { | ||
| return PATTERN; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled(Session session) | ||
| { | ||
| return isInlineProjectionsOnValues(session); | ||
| } | ||
|
|
||
| @Override | ||
| public Result apply(ProjectNode projectNode, Captures captures, Context context) | ||
| { | ||
| ValuesNode source = captures.get(CHILD); | ||
| List<List<RowExpression>> rows = source.getRows(); | ||
| List<VariableReferenceExpression> valuesOutputVariables = source.getOutputVariables(); | ||
| List<VariableReferenceExpression> projectOutputVariables = projectNode.getOutputVariables(); | ||
| List<RowExpression> projectRowExpressions = projectNode.getAssignments() | ||
| .getExpressions() | ||
| .stream() | ||
| .collect(toImmutableList()); | ||
|
|
||
| // exclude non-deterministic function | ||
| DeterminismEvaluator determinismEvaluator = new RowExpressionDeterminismEvaluator(functionAndTypeManager); | ||
| if (!projectRowExpressions.stream().allMatch(determinismEvaluator::isDeterministic)) { | ||
| return Result.empty(); | ||
| } | ||
| if (!rows.stream().allMatch(row -> row.stream() | ||
| .allMatch(determinismEvaluator::isDeterministic))) { | ||
| return Result.empty(); | ||
| } | ||
|
|
||
| //rewrite ProjectNode assignment expressions | ||
| ImmutableList.Builder<List<RowExpression>> rowExpressionsListBuilder = ImmutableList.builder(); | ||
| for (List<RowExpression> rowExpressions : rows) { | ||
| verify(rowExpressions.size() == valuesOutputVariables.size(), "Output variable does not match its value in ValuesNode"); | ||
| Map<VariableReferenceExpression, RowExpression> mapping = Streams.zip( | ||
| valuesOutputVariables.stream(), | ||
| rowExpressions.stream(), | ||
| SimpleImmutableEntry::new) | ||
| .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
| List<RowExpression> rowExpressionsInProject = projectRowExpressions.stream() | ||
| .map(expression -> inlineVariables(mapping, expression)) | ||
| .collect(toImmutableList()); | ||
| rowExpressionsListBuilder.add(rowExpressionsInProject); | ||
| } | ||
|
|
||
| ValuesNode updatedProject = new ValuesNode( | ||
| source.getSourceLocation(), | ||
| context.getIdAllocator().getNextId(), | ||
| projectOutputVariables, | ||
| rowExpressionsListBuilder.build(), | ||
| Optional.empty()); | ||
|
|
||
| return Result.ofPlanNode(updatedProject); | ||
| } | ||
| } |
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
61 changes: 61 additions & 0 deletions
61
...st/java/com/facebook/presto/sql/planner/iterative/rule/TestInlineProjectionsOnValues.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,61 @@ | ||
| /* | ||
| * 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.spi.plan.ValuesNode; | ||
| import com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static com.facebook.presto.SystemSessionProperties.INLINE_PROJECTIONS_ON_VALUES; | ||
| import static com.facebook.presto.common.type.BigintType.BIGINT; | ||
| import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.node; | ||
| import static com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder.assignment; | ||
| import static com.facebook.presto.sql.relational.Expressions.call; | ||
|
|
||
| public class TestInlineProjectionsOnValues | ||
| extends BaseRuleTest | ||
| { | ||
| @Test | ||
| public void testDoesNotFireOn() | ||
| { | ||
| tester().assertThat(new InlineProjectionsOnValues(tester.getMetadata().getFunctionAndTypeManager())) | ||
| .setSystemProperty(INLINE_PROJECTIONS_ON_VALUES, "true") | ||
| .on(p -> p.project(p.project(p.values(p.getIdAllocator().getNextId(), p.variable("a")), | ||
| assignment(p.variable("c"), p.variable("a"))), | ||
| assignment(p.variable("d"), p.variable("c")))) | ||
| .doesNotFire(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDoesNotFireOnWithNonDeterministicFunction() | ||
| { | ||
| tester().assertThat(new InlineProjectionsOnValues(tester.getMetadata().getFunctionAndTypeManager())) | ||
| .setSystemProperty(INLINE_PROJECTIONS_ON_VALUES, "true") | ||
| .on(p -> p.project(p.values(p.getIdAllocator().getNextId(), p.variable("a")), | ||
| assignment(p.variable("b"), call(tester.getMetadata().getFunctionAndTypeManager(), "random", BIGINT, p.variable("a"))))) | ||
| .doesNotFire(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFireOnProjectFollowedByValues() | ||
| { | ||
| tester().assertThat(new InlineProjectionsOnValues(tester.getMetadata().getFunctionAndTypeManager())) | ||
| .setSystemProperty(INLINE_PROJECTIONS_ON_VALUES, "true") | ||
| // Form the input planNode: ProjectNode -> ValuesNode | ||
| .on(p -> p.project(p.values(p.getIdAllocator().getNextId(), p.variable("a")), | ||
| assignment(p.variable("c"), p.variable("a")))) | ||
| // Ensure the PlanNode is optimized to a ValuesNode | ||
| .matches(node(ValuesNode.class)); | ||
| } | ||
| } |
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
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.