use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.
the class MergeLimits method apply.
@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
if (!(node instanceof LimitNode)) {
return Optional.empty();
}
LimitNode parent = (LimitNode) node;
PlanNode source = lookup.resolve(parent.getSource());
if (!(source instanceof LimitNode)) {
return Optional.empty();
}
LimitNode child = (LimitNode) source;
return Optional.of(new LimitNode(parent.getId(), child.getSource(), Math.min(parent.getCount(), child.getCount()), parent.isPartial()));
}
use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.
the class PruneTableScanColumns method apply.
@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
if (!(node instanceof ProjectNode)) {
return Optional.empty();
}
ProjectNode parent = (ProjectNode) node;
PlanNode source = lookup.resolve(parent.getSource());
if (!(source instanceof TableScanNode)) {
return Optional.empty();
}
TableScanNode child = (TableScanNode) source;
Optional<List<Symbol>> dependencies = pruneInputs(child.getOutputSymbols(), parent.getAssignments().getExpressions());
if (!dependencies.isPresent()) {
return Optional.empty();
}
List<Symbol> newOutputs = dependencies.get();
return Optional.of(new ProjectNode(parent.getId(), new TableScanNode(child.getId(), child.getTable(), newOutputs, newOutputs.stream().collect(Collectors.toMap(Function.identity(), e -> child.getAssignments().get(e))), child.getLayout(), child.getCurrentConstraint(), child.getOriginalConstraint()), parent.getAssignments()));
}
use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.
the class PruneValuesColumns method apply.
@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
if (!(node instanceof ProjectNode)) {
return Optional.empty();
}
ProjectNode parent = (ProjectNode) node;
PlanNode child = lookup.resolve(parent.getSource());
if (!(child instanceof ValuesNode)) {
return Optional.empty();
}
ValuesNode values = (ValuesNode) child;
Optional<List<Symbol>> dependencies = pruneInputs(child.getOutputSymbols(), parent.getAssignments().getExpressions());
if (!dependencies.isPresent()) {
return Optional.empty();
}
List<Symbol> newOutputs = dependencies.get();
// for each output of project, the corresponding column in the values node
int[] mapping = new int[newOutputs.size()];
for (int i = 0; i < mapping.length; i++) {
mapping[i] = values.getOutputSymbols().indexOf(newOutputs.get(i));
}
ImmutableList.Builder<List<Expression>> rowsBuilder = ImmutableList.builder();
for (List<Expression> row : values.getRows()) {
rowsBuilder.add(Arrays.stream(mapping).mapToObj(row::get).collect(Collectors.toList()));
}
return Optional.of(new ProjectNode(parent.getId(), new ValuesNode(values.getId(), newOutputs, rowsBuilder.build()), parent.getAssignments()));
}
use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.
the class GraphvizPrinter method printFragmentNodes.
private static void printFragmentNodes(StringBuilder output, PlanFragment fragment, PlanNodeIdGenerator idGenerator) {
String clusterId = "cluster_" + fragment.getId();
output.append("subgraph ").append(clusterId).append(" {").append('\n');
output.append(format("label = \"%s\"", fragment.getPartitioning())).append('\n');
PlanNode plan = fragment.getRoot();
plan.accept(new NodePrinter(output, idGenerator), null);
output.append("}").append('\n');
}
use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.
the class EliminateCrossJoins method getJoinOrder.
/**
* Given JoinGraph determine the order of joins between graph nodes
* by traversing JoinGraph. Any graph traversal algorithm could be used
* here (like BFS or DFS), but we use PriorityQueue to preserve
* original JoinOrder as mush as it is possible. PriorityQueue returns
* next nodes to join in order of their occurrence in original Plan.
*/
public static List<Integer> getJoinOrder(JoinGraph graph) {
ImmutableList.Builder<PlanNode> joinOrder = ImmutableList.builder();
Map<PlanNodeId, Integer> priorities = new HashMap<>();
for (int i = 0; i < graph.size(); i++) {
priorities.put(graph.getNode(i).getId(), i);
}
PriorityQueue<PlanNode> nodesToVisit = new PriorityQueue<>(graph.size(), (Comparator<PlanNode>) (node1, node2) -> priorities.get(node1.getId()).compareTo(priorities.get(node2.getId())));
Set<PlanNode> visited = new HashSet<>();
nodesToVisit.add(graph.getNode(0));
while (!nodesToVisit.isEmpty()) {
PlanNode node = nodesToVisit.poll();
if (!visited.contains(node)) {
visited.add(node);
joinOrder.add(node);
for (JoinGraph.Edge edge : graph.getEdges(node)) {
nodesToVisit.add(edge.getTargetNode());
}
}
if (nodesToVisit.isEmpty() && visited.size() < graph.size()) {
// disconnected graph, find new starting point
Optional<PlanNode> firstNotVisitedNode = graph.getNodes().stream().filter(graphNode -> !visited.contains(graphNode)).findFirst();
if (firstNotVisitedNode.isPresent()) {
nodesToVisit.add(firstNotVisitedNode.get());
}
}
}
checkState(visited.size() == graph.size());
return joinOrder.build().stream().map(node -> priorities.get(node.getId())).collect(toImmutableList());
}
Aggregations