Skip to content
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

Iterative optimizer #6956

Merged
merged 2 commits into from Jan 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;

import javax.annotation.concurrent.Immutable;

Expand Down Expand Up @@ -204,4 +205,10 @@ public <C, R> R accept(PlanVisitor<C, R> visitor, C context)
{
return visitor.visitAggregation(this, context);
}

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren)
{
return new AggregationNode(getId(), Iterables.getOnlyElement(newChildren), aggregations, functions, masks, groupingSets, step, hashSymbol, groupIdSymbol);
}
}
Expand Up @@ -139,4 +139,11 @@ public <C, R> R accept(PlanVisitor<C, R> visitor, C context)
{
return visitor.visitApply(this, context);
}

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren)
{
checkArgument(newChildren.size() == 2, "expected newChildren to contain 2 nodes");
return new ApplyNode(getId(), newChildren.get(0), newChildren.get(1), subqueryAssignments, correlation);
}
}
Expand Up @@ -18,9 +18,11 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

public class AssignUniqueId
Expand Down Expand Up @@ -72,4 +74,11 @@ public <C, R> R accept(PlanVisitor<C, R> visitor, C context)
{
return visitor.visitAssignUniqueId(this, context);
}

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about renaming this to replaceSources? You have getSources() but replaceChildren, it sounds like they were referencing to two different things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"sources" is a legacy term. It used to be accurate a long time ago, before we even had support for "index joins". Then, with that feature, and now with Apply, it can mean either source or subplan. So "child" is more correct now.

{
checkArgument(newChildren.size() == 1, "expected newChildren to contain 1 node");
return new AssignUniqueId(getId(), Iterables.getOnlyElement(newChildren), idColumn);
}
}
Expand Up @@ -13,16 +13,13 @@
*/
package com.facebook.presto.sql.planner.plan;

import com.google.common.collect.Iterables;

import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;

public class ChildReplacer
extends PlanVisitor<List<PlanNode>, PlanNode>
{
private static final ChildReplacer INSTANCE = new ChildReplacer();
private ChildReplacer()
{
}

/**
* Return an identical copy of the given node with its children replaced
Expand All @@ -31,250 +28,9 @@ public static PlanNode replaceChildren(PlanNode node, List<PlanNode> children)
{
for (int i = 0; i < node.getSources().size(); i++) {
if (children.get(i) != node.getSources().get(i)) {
return node.accept(INSTANCE, children);
return node.replaceChildren(children);
}
}
return node;
}

@Override
public PlanNode visitPlan(PlanNode node, List<PlanNode> newChildren)
{
throw new UnsupportedOperationException("not yet implemented: " + node.getClass().getName());
}

@Override
public PlanNode visitExplainAnalyze(ExplainAnalyzeNode node, List<PlanNode> newChildren)
{
return new ExplainAnalyzeNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getOutputSymbol());
}

@Override
public PlanNode visitLimit(LimitNode node, List<PlanNode> newChildren)
{
return new LimitNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getCount(), node.isPartial());
}

@Override
public PlanNode visitDistinctLimit(DistinctLimitNode node, List<PlanNode> newChildren)
{
return new DistinctLimitNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getLimit(), node.isPartial(), node.getHashSymbol());
}

@Override
public PlanNode visitRemoteSource(RemoteSourceNode node, List<PlanNode> newChildren)
{
checkArgument(newChildren.isEmpty(), "newChildren is not empty");
return node;
}

@Override
public PlanNode visitExchange(ExchangeNode node, List<PlanNode> newChildren)
{
return new ExchangeNode(
node.getId(),
node.getType(),
node.getScope(),
node.getPartitioningScheme(),
newChildren,
node.getInputs());
}

@Override
public PlanNode visitTopN(TopNNode node, List<PlanNode> newChildren)
{
return new TopNNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getCount(), node.getOrderBy(), node.getOrderings(), node.isPartial());
}

@Override
public PlanNode visitTableScan(TableScanNode node, List<PlanNode> newChildren)
{
checkArgument(newChildren.isEmpty(), "newChildren is not empty");
return node;
}

@Override
public PlanNode visitValues(ValuesNode node, List<PlanNode> newChildren)
{
checkArgument(newChildren.isEmpty(), "newChildren is not empty");
return node;
}

@Override
public PlanNode visitUnnest(UnnestNode node, List<PlanNode> newChildren)
{
return new UnnestNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getReplicateSymbols(), node.getUnnestSymbols(), node.getOrdinalitySymbol());
}

@Override
public PlanNode visitProject(ProjectNode node, List<PlanNode> newChildren)
{
return new ProjectNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getAssignments());
}

@Override
public PlanNode visitFilter(FilterNode node, List<PlanNode> newChildren)
{
return new FilterNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getPredicate());
}

@Override
public PlanNode visitSample(SampleNode node, List<PlanNode> newChildren)
{
return new SampleNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getSampleRatio(), node.getSampleType());
}

@Override
public PlanNode visitIndexSource(IndexSourceNode node, List<PlanNode> newChildren)
{
return node;
}

@Override
public PlanNode visitJoin(JoinNode node, List<PlanNode> newChildren)
{
checkArgument(newChildren.size() == 2, "expected newChildren to contain 2 nodes");
return new JoinNode(node.getId(), node.getType(), newChildren.get(0), newChildren.get(1), node.getCriteria(), node.getFilter(), node.getLeftHashSymbol(), node.getRightHashSymbol());
}

@Override
public PlanNode visitSemiJoin(SemiJoinNode node, List<PlanNode> newChildren)
{
checkArgument(newChildren.size() == 2, "expected newChildren to contain 2 nodes");
return new SemiJoinNode(node.getId(), newChildren.get(0), newChildren.get(1), node.getSourceJoinSymbol(), node.getFilteringSourceJoinSymbol(), node.getSemiJoinOutput(), node.getSourceHashSymbol(), node.getFilteringSourceHashSymbol());
}

@Override
public PlanNode visitIndexJoin(IndexJoinNode node, List<PlanNode> newChildren)
{
checkArgument(newChildren.size() == 2, "expected newChildren to contain 2 nodes");
return new IndexJoinNode(node.getId(), node.getType(), newChildren.get(0), newChildren.get(1), node.getCriteria(), node.getProbeHashSymbol(), node.getIndexHashSymbol());
}

@Override
public PlanNode visitAggregation(AggregationNode node, List<PlanNode> newChildren)
{
return new AggregationNode(
node.getId(),
Iterables.getOnlyElement(newChildren),
node.getAggregations(),
node.getFunctions(),
node.getMasks(),
node.getGroupingSets(),
node.getStep(),
node.getHashSymbol(),
node.getGroupIdSymbol());
}

@Override
public PlanNode visitGroupId(GroupIdNode node, List<PlanNode> newChildren)
{
return new GroupIdNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getGroupingSets(), node.getGroupingSetMappings(), node.getArgumentMappings(), node.getGroupIdSymbol());
}

@Override
public PlanNode visitMarkDistinct(MarkDistinctNode node, List<PlanNode> newChildren)
{
return new MarkDistinctNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getMarkerSymbol(), node.getDistinctSymbols(), node.getHashSymbol());
}

@Override
public PlanNode visitWindow(WindowNode node, List<PlanNode> newChildren)
{
return new WindowNode(
node.getId(),
Iterables.getOnlyElement(newChildren),
node.getSpecification(),
node.getWindowFunctions(),
node.getHashSymbol(),
node.getPrePartitionedInputs(),
node.getPreSortedOrderPrefix());
}

@Override
public PlanNode visitTopNRowNumber(TopNRowNumberNode node, List<PlanNode> newChildren)
{
return new TopNRowNumberNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getSpecification(), node.getRowNumberSymbol(), node.getMaxRowCountPerPartition(), node.isPartial(), node.getHashSymbol());
}

@Override
public PlanNode visitRowNumber(RowNumberNode node, List<PlanNode> newChildren)
{
return new RowNumberNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getPartitionBy(), node.getRowNumberSymbol(), node.getMaxRowCountPerPartition(), node.getHashSymbol());
}

@Override
public PlanNode visitOutput(OutputNode node, List<PlanNode> newChildren)
{
return new OutputNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getColumnNames(), node.getOutputSymbols());
}

@Override
public PlanNode visitSort(SortNode node, List<PlanNode> newChildren)
{
return new SortNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getOrderBy(), node.getOrderings());
}

@Override
public PlanNode visitTableWriter(TableWriterNode node, List<PlanNode> newChildren)
{
return new TableWriterNode(
node.getId(),
Iterables.getOnlyElement(newChildren),
node.getTarget(),
node.getColumns(),
node.getColumnNames(),
node.getOutputSymbols(),
node.getPartitioningScheme());
}

@Override
public PlanNode visitTableFinish(TableFinishNode node, List<PlanNode> newChildren)
{
return new TableFinishNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getTarget(), node.getOutputSymbols());
}

@Override
public PlanNode visitUnion(UnionNode node, List<PlanNode> newChildren)
{
return new UnionNode(node.getId(), newChildren, node.getSymbolMapping(), node.getOutputSymbols());
}

@Override
public PlanNode visitIntersect(IntersectNode node, List<PlanNode> newChildren)
{
return new IntersectNode(node.getId(), newChildren, node.getSymbolMapping(), node.getOutputSymbols());
}

@Override
public PlanNode visitExcept(ExceptNode node, List<PlanNode> newChildren)
{
return new ExceptNode(node.getId(), newChildren, node.getSymbolMapping(), node.getOutputSymbols());
}

@Override
public PlanNode visitDelete(DeleteNode node, List<PlanNode> newChildren)
{
return new DeleteNode(node.getId(), Iterables.getOnlyElement(newChildren), node.getTarget(), node.getRowId(), node.getOutputSymbols());
}

@Override
public PlanNode visitEnforceSingleRow(EnforceSingleRowNode node, List<PlanNode> newChildren)
{
return new EnforceSingleRowNode(node.getId(), Iterables.getOnlyElement(newChildren));
}

@Override
public PlanNode visitApply(ApplyNode node, List<PlanNode> newChildren)
{
checkArgument(newChildren.size() == 2, "expected newChildren to contain 2 nodes");
return new ApplyNode(node.getId(), newChildren.get(0), newChildren.get(1), node.getSubqueryAssignments(), node.getCorrelation());
}

@Override
public PlanNode visitAssignUniqueId(AssignUniqueId node, List<PlanNode> newChildren)
{
checkArgument(newChildren.size() == 1, "expected newChildren to contain 1 node");
return new AssignUniqueId(node.getId(), Iterables.getOnlyElement(newChildren), node.getIdColumn());
}
}
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import javax.annotation.concurrent.Immutable;

Expand Down Expand Up @@ -86,4 +87,10 @@ public <C, R> R accept(PlanVisitor<C, R> visitor, C context)
{
return visitor.visitDelete(this, context);
}

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren)
{
return new DeleteNode(getId(), Iterables.getOnlyElement(newChildren), target, rowId, outputs);
}
}
Expand Up @@ -102,4 +102,10 @@ public <C, R> R accept(PlanVisitor<C, R> visitor, C context)
{
return visitor.visitDistinctLimit(this, context);
}

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren)
{
return new DistinctLimitNode(getId(), Iterables.getOnlyElement(newChildren), limit, partial, hashSymbol);
}
}
Expand Up @@ -17,6 +17,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import javax.annotation.concurrent.Immutable;

Expand Down Expand Up @@ -63,4 +64,10 @@ public <C, R> R accept(PlanVisitor<C, R> visitor, C context)
{
return visitor.visitEnforceSingleRow(this, context);
}

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren)
{
return new EnforceSingleRowNode(getId(), Iterables.getOnlyElement(newChildren));
}
}
Expand Up @@ -39,4 +39,10 @@ public <C, R> R accept(PlanVisitor<C, R> visitor, C context)
{
return visitor.visitExcept(this, context);
}

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren)
{
return new ExceptNode(getId(), newChildren, getSymbolMapping(), getOutputSymbols());
}
}
Expand Up @@ -208,4 +208,10 @@ public <C, R> R accept(PlanVisitor<C, R> visitor, C context)
{
return visitor.visitExchange(this, context);
}

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren)
{
return new ExchangeNode(getId(), type, scope, partitioningScheme, newChildren, inputs);
}
}