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
11 changed files
with
687 additions
and
1 deletion.
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
52 changes: 52 additions & 0 deletions
52
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,52 @@ | ||
/* | ||
* 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; | ||
|
||
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; | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
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,129 @@ | ||
/* | ||
* 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, lookup, idAllocator, symbolAllocator, memo.getRootGroup()); | ||
|
||
return memo.extract(); | ||
} | ||
|
||
private boolean exploreGroup(Memo memo, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator, int group) | ||
{ | ||
// tracks whether this group or any children groups change as | ||
// this method executes | ||
boolean progress = exploreNode(memo, lookup, idAllocator, symbolAllocator, group); | ||
|
||
while (exploreChildren(memo, lookup, idAllocator, symbolAllocator, group)) { | ||
progress = true; | ||
|
||
// if children changed, try current group again | ||
// in case we can match additional rules | ||
if (!exploreNode(memo, lookup, idAllocator, symbolAllocator, group)) { | ||
// no additional matches, so bail out | ||
break; | ||
} | ||
} | ||
|
||
return progress; | ||
} | ||
|
||
private boolean exploreNode( | ||
Memo memo, | ||
Lookup lookup, | ||
PlanNodeIdAllocator idAllocator, | ||
SymbolAllocator symbolAllocator, | ||
int group) | ||
{ | ||
PlanNode node = memo.getNode(group); | ||
|
||
boolean done = false; | ||
boolean progress = false; | ||
|
||
while (!done) { | ||
done = true; | ||
for (Rule rule : rules) { | ||
Optional<PlanNode> transformed = rule.apply(node, lookup, idAllocator, symbolAllocator); | ||
|
||
if (transformed.isPresent()) { | ||
memo.replace(group, transformed.get(), rule.getClass().getName()); | ||
node = transformed.get(); | ||
|
||
done = false; | ||
progress = true; | ||
} | ||
} | ||
} | ||
|
||
return progress; | ||
} | ||
|
||
private boolean exploreChildren(Memo memo, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator, int group) | ||
{ | ||
boolean progress = false; | ||
|
||
PlanNode expression = memo.getNode(group); | ||
for (PlanNode child : expression.getSources()) { | ||
checkState(child instanceof GroupReference, "Expected child to be a group reference. Found: " + child.getClass().getName()); | ||
|
||
if (exploreGroup(memo, lookup, idAllocator, symbolAllocator, ((GroupReference) child).getGroupId())) { | ||
progress = true; | ||
} | ||
} | ||
|
||
return progress; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
/* | ||
* 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 | ||
{ | ||
PlanNode resolve(PlanNode node); | ||
} |
Oops, something went wrong.