Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This optimizer decouples the traversal of the plan tree (IterativeOptimizer) from the transformation logic (Rule). The optimization loop applies rules recursively until a fixpoint is reached. It's implemented as PlanOptimizer so that it fits right into the existing framework.
- Loading branch information
Showing
12 changed files
with
755 additions
and
2 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
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
61 changes: 61 additions & 0 deletions
61
presto-main/src/main/java/com/facebook/presto/sql/planner/iterative/GroupReference.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,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; | ||
|
||
import com.facebook.presto.sql.planner.Symbol; | ||
import com.facebook.presto.sql.planner.plan.PlanNode; | ||
import com.facebook.presto.sql.planner.plan.PlanNodeId; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.List; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
public class GroupReference | ||
extends PlanNode | ||
{ | ||
private final int groupId; | ||
private final List<Symbol> outputs; | ||
|
||
public GroupReference(PlanNodeId id, int groupId, List<Symbol> outputs) | ||
{ | ||
super(id); | ||
this.groupId = groupId; | ||
this.outputs = ImmutableList.copyOf(outputs); | ||
} | ||
|
||
public int getGroupId() | ||
{ | ||
return groupId; | ||
} | ||
|
||
@Override | ||
public List<PlanNode> getSources() | ||
{ | ||
return ImmutableList.of(); | ||
} | ||
|
||
@Override | ||
public List<Symbol> getOutputSymbols() | ||
{ | ||
return outputs; | ||
} | ||
|
||
@Override | ||
public PlanNode replaceChildren(List<PlanNode> newChildren) | ||
{ | ||
checkArgument(newChildren.isEmpty(), "newChildren is not empty"); | ||
return this; | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
presto-main/src/main/java/com/facebook/presto/sql/planner/iterative/IterativeOptimizer.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,160 @@ | ||
/* | ||
* 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; | ||
|
||
import com.facebook.presto.Session; | ||
import com.facebook.presto.SystemSessionProperties; | ||
import com.facebook.presto.spi.type.Type; | ||
import com.facebook.presto.sql.planner.PlanNodeIdAllocator; | ||
import com.facebook.presto.sql.planner.Symbol; | ||
import com.facebook.presto.sql.planner.SymbolAllocator; | ||
import com.facebook.presto.sql.planner.optimizations.PlanOptimizer; | ||
import com.facebook.presto.sql.planner.plan.PlanNode; | ||
import com.google.common.collect.ImmutableSet; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
|
||
public class IterativeOptimizer | ||
implements PlanOptimizer | ||
{ | ||
private final Set<Rule> rules; | ||
|
||
public IterativeOptimizer(Set<Rule> rules) | ||
{ | ||
this.rules = ImmutableSet.copyOf(rules); | ||
} | ||
|
||
@Override | ||
public PlanNode optimize(PlanNode plan, Session session, Map<Symbol, Type> types, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator) | ||
{ | ||
if (!SystemSessionProperties.isNewOptimizerEnabled(session)) { | ||
return plan; | ||
} | ||
|
||
Memo memo = new Memo(idAllocator, plan); | ||
|
||
Lookup lookup = node -> { | ||
if (node instanceof GroupReference) { | ||
return memo.getNode(((GroupReference) node).getGroupId()); | ||
} | ||
|
||
return node; | ||
}; | ||
|
||
exploreGroup(memo.getRootGroup(), new Context(memo, lookup, idAllocator, symbolAllocator)); | ||
|
||
return memo.extract(); | ||
} | ||
|
||
private boolean exploreGroup(int group, Context context) | ||
{ | ||
// tracks whether this group or any children groups change as | ||
// this method executes | ||
boolean progress = exploreNode(group, context); | ||
|
||
while (exploreChildren(group, context)) { | ||
progress = true; | ||
|
||
// if children changed, try current group again | ||
// in case we can match additional rules | ||
if (!exploreNode(group, context)) { | ||
// no additional matches, so bail out | ||
break; | ||
} | ||
} | ||
|
||
return progress; | ||
} | ||
|
||
private boolean exploreNode(int group, Context context) | ||
{ | ||
PlanNode node = context.getMemo().getNode(group); | ||
|
||
boolean done = false; | ||
boolean progress = false; | ||
|
||
while (!done) { | ||
done = true; | ||
for (Rule rule : rules) { | ||
Optional<PlanNode> transformed = rule.apply(node, context.getLookup(), context.getIdAllocator(), context.getSymbolAllocator()); | ||
|
||
if (transformed.isPresent()) { | ||
context.getMemo().replace(group, transformed.get(), rule.getClass().getName()); | ||
node = transformed.get(); | ||
|
||
done = false; | ||
progress = true; | ||
} | ||
} | ||
} | ||
|
||
return progress; | ||
} | ||
|
||
private boolean exploreChildren(int group, Context context) | ||
{ | ||
boolean progress = false; | ||
|
||
PlanNode expression = context.getMemo().getNode(group); | ||
for (PlanNode child : expression.getSources()) { | ||
checkState(child instanceof GroupReference, "Expected child to be a group reference. Found: " + child.getClass().getName()); | ||
|
||
if (exploreGroup(((GroupReference) child).getGroupId(), context)) { | ||
progress = true; | ||
} | ||
} | ||
|
||
return progress; | ||
} | ||
|
||
private static class Context | ||
{ | ||
private final Memo memo; | ||
private final Lookup lookup; | ||
private final PlanNodeIdAllocator idAllocator; | ||
private final SymbolAllocator symbolAllocator; | ||
|
||
public Context(Memo memo, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) | ||
{ | ||
this.memo = memo; | ||
this.lookup = lookup; | ||
this.idAllocator = idAllocator; | ||
this.symbolAllocator = symbolAllocator; | ||
} | ||
|
||
public Memo getMemo() | ||
{ | ||
return memo; | ||
} | ||
|
||
public Lookup getLookup() | ||
{ | ||
return lookup; | ||
} | ||
|
||
public PlanNodeIdAllocator getIdAllocator() | ||
{ | ||
return idAllocator; | ||
} | ||
|
||
public SymbolAllocator getSymbolAllocator() | ||
{ | ||
return symbolAllocator; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
presto-main/src/main/java/com/facebook/presto/sql/planner/iterative/Lookup.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,28 @@ | ||
/* | ||
* 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; | ||
|
||
import com.facebook.presto.sql.planner.plan.PlanNode; | ||
|
||
public interface Lookup | ||
{ | ||
/** | ||
* Resolves a node by materializing GroupReference nodes | ||
* representing symbolic references to other nodes. | ||
* | ||
* If the node is not a GroupReference, it returns the | ||
* argument as is. | ||
*/ | ||
PlanNode resolve(PlanNode node); | ||
} |
Oops, something went wrong.