Skip to content

Commit

Permalink
Include root node LocalCostEstimate in PlanCostEstimate
Browse files Browse the repository at this point in the history
  • Loading branch information
sopel39 committed May 13, 2019
1 parent 798fc98 commit cb62759
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public PlanCostEstimate visitUnion(UnionNode node, Void context)
private PlanCostEstimate costForSource(PlanNode node, LocalCostEstimate localCost)
{
verify(node.getSources().isEmpty(), "Unexpected sources for %s: %s", node, node.getSources());
return new PlanCostEstimate(localCost.getCpuCost(), localCost.getMaxMemory(), localCost.getMaxMemory(), localCost.getNetworkCost());
return new PlanCostEstimate(localCost.getCpuCost(), localCost.getMaxMemory(), localCost.getMaxMemory(), localCost.getNetworkCost(), localCost);
}

private PlanCostEstimate costForAccumulation(PlanNode node, LocalCostEstimate localCost)
Expand All @@ -318,7 +318,8 @@ private PlanCostEstimate costForAccumulation(PlanNode node, LocalCostEstimate lo
sourcesCost.getMaxMemory(), // Accumulating operator allocates insignificant amount of memory (usually none) before first input page is received
sourcesCost.getMaxMemoryWhenOutputting() + localCost.getMaxMemory()),
localCost.getMaxMemory(), // Source freed its memory allocations when finished its output
sourcesCost.getNetworkCost() + localCost.getNetworkCost());
sourcesCost.getNetworkCost() + localCost.getNetworkCost(),
localCost);
}

private PlanCostEstimate costForStreaming(PlanNode node, LocalCostEstimate localCost)
Expand All @@ -331,7 +332,8 @@ private PlanCostEstimate costForStreaming(PlanNode node, LocalCostEstimate local
sourcesCost.getMaxMemory(), // Streaming operator allocates insignificant amount of memory (usually none) before first input page is received
sourcesCost.getMaxMemoryWhenOutputting() + localCost.getMaxMemory()),
sourcesCost.getMaxMemoryWhenOutputting() + localCost.getMaxMemory(),
sourcesCost.getNetworkCost() + localCost.getNetworkCost());
sourcesCost.getNetworkCost() + localCost.getNetworkCost(),
localCost);
}

private PlanCostEstimate costForLookupJoin(PlanNode node, LocalCostEstimate localCost)
Expand All @@ -348,7 +350,8 @@ private PlanCostEstimate costForLookupJoin(PlanNode node, LocalCostEstimate loca
probeCost.getMaxMemory() + buildCost.getMaxMemory(), // Probe and build execute independently, so their max memory allocations can be realized at the same time
probeCost.getMaxMemory() + buildCost.getMaxMemoryWhenOutputting() + localCost.getMaxMemory()),
probeCost.getMaxMemoryWhenOutputting() + localCost.getMaxMemory(), // Build side finished and freed its memory allocations
probeCost.getNetworkCost() + buildCost.getNetworkCost() + localCost.getNetworkCost());
probeCost.getNetworkCost() + buildCost.getNetworkCost() + localCost.getNetworkCost(),
localCost);
}

private PlanNodeStatsEstimate getStats(PlanNode node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ private static PlanCostEstimate addExchangeCost(PlanCostEstimate costEstimate, L
// exchange memory allocation will actually be freed before node is outputting. Conservatively we assume the exchanges can still
// hold the memory when the node is outputting.
costEstimate.getMaxMemoryWhenOutputting() + estimatedExchangeCost.getMaxMemory(),
costEstimate.getNetworkCost() + estimatedExchangeCost.getNetworkCost());
costEstimate.getNetworkCost() + estimatedExchangeCost.getNetworkCost(),
addPartialComponents(costEstimate.getRootNodeLocalCostEstimate(), estimatedExchangeCost));
}

private static class ExchangeCostEstimator
Expand Down
35 changes: 33 additions & 2 deletions presto-main/src/main/java/io/prestosql/cost/LocalCostEstimate.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
*/
package io.prestosql.cost;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.prestosql.sql.planner.plan.PlanNode;

import java.util.Objects;
import java.util.stream.Stream;

import static com.google.common.base.MoreObjects.toStringHelper;
Expand Down Expand Up @@ -54,23 +57,30 @@ public static LocalCostEstimate of(double cpuCost, double maxMemory, double netw
return new LocalCostEstimate(cpuCost, maxMemory, networkCost);
}

private LocalCostEstimate(double cpuCost, double maxMemory, double networkCost)
@JsonCreator
public LocalCostEstimate(
@JsonProperty("cpuCost") double cpuCost,
@JsonProperty("maxMemory") double maxMemory,
@JsonProperty("networkCost") double networkCost)
{
this.cpuCost = cpuCost;
this.maxMemory = maxMemory;
this.networkCost = networkCost;
}

@JsonProperty
public double getCpuCost()
{
return cpuCost;
}

@JsonProperty
public double getMaxMemory()
{
return maxMemory;
}

@JsonProperty
public double getNetworkCost()
{
return networkCost;
Expand All @@ -82,7 +92,7 @@ public double getNetworkCost()
@Deprecated
public PlanCostEstimate toPlanCost()
{
return new PlanCostEstimate(cpuCost, maxMemory, maxMemory, networkCost);
return new PlanCostEstimate(cpuCost, maxMemory, maxMemory, networkCost, this);
}

@Override
Expand All @@ -95,6 +105,27 @@ public String toString()
.toString();
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LocalCostEstimate that = (LocalCostEstimate) o;
return Double.compare(that.cpuCost, cpuCost) == 0 &&
Double.compare(that.maxMemory, maxMemory) == 0 &&
Double.compare(that.networkCost, networkCost) == 0;
}

@Override
public int hashCode()
{
return Objects.hash(cpuCost, maxMemory, networkCost);
}

/**
* Sums partial cost estimates of some (single) plan node.
*/
Expand Down
30 changes: 27 additions & 3 deletions presto-main/src/main/java/io/prestosql/cost/PlanCostEstimate.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static java.lang.Double.NaN;
import static java.lang.Double.POSITIVE_INFINITY;
import static java.lang.Double.isNaN;
import static java.util.Objects.requireNonNull;

public final class PlanCostEstimate
{
Expand All @@ -34,6 +35,7 @@ public final class PlanCostEstimate
private final double maxMemory;
private final double maxMemoryWhenOutputting;
private final double networkCost;
private final LocalCostEstimate rootNodeLocalCostEstimate;

public static PlanCostEstimate infinite()
{
Expand All @@ -50,12 +52,22 @@ public static PlanCostEstimate zero()
return ZERO;
}

public PlanCostEstimate(
double cpuCost,
double maxMemory,
double maxMemoryWhenOutputting,
double networkCost)
{
this(cpuCost, maxMemory, maxMemoryWhenOutputting, networkCost, LocalCostEstimate.unknown());
}

@JsonCreator
public PlanCostEstimate(
@JsonProperty("cpuCost") double cpuCost,
@JsonProperty("maxMemory") double maxMemory,
@JsonProperty("maxMemoryWhenOutputting") double maxMemoryWhenOutputting,
@JsonProperty("networkCost") double networkCost)
@JsonProperty("networkCost") double networkCost,
@JsonProperty("rootNodeLocalCostEstimate") LocalCostEstimate rootNodeLocalCostEstimate)
{
checkArgument(!(cpuCost < 0), "cpuCost cannot be negative: %s", cpuCost);
checkArgument(!(maxMemory < 0), "maxMemory cannot be negative: %s", maxMemory);
Expand All @@ -66,6 +78,7 @@ public PlanCostEstimate(
this.maxMemory = maxMemory;
this.maxMemoryWhenOutputting = maxMemoryWhenOutputting;
this.networkCost = networkCost;
this.rootNodeLocalCostEstimate = requireNonNull(rootNodeLocalCostEstimate, "rootNodeLocalCostEstimate is null");
}

/**
Expand Down Expand Up @@ -114,6 +127,15 @@ public double getNetworkCost()
return networkCost;
}

/**
* Returns local cost estimate for the root plan node.
*/
@JsonProperty
public LocalCostEstimate getRootNodeLocalCostEstimate()
{
return rootNodeLocalCostEstimate;
}

/**
* Returns true if this cost has unknown components.
*/
Expand All @@ -130,6 +152,7 @@ public String toString()
.add("memory", maxMemory)
// maxMemoryWhenOutputting is not that useful in toString
.add("network", networkCost)
.add("rootNodeLocalCostEstimate", rootNodeLocalCostEstimate)
.toString();
}

Expand All @@ -146,12 +169,13 @@ public boolean equals(Object o)
return Double.compare(that.cpuCost, cpuCost) == 0 &&
Double.compare(that.maxMemory, maxMemory) == 0 &&
Double.compare(that.maxMemoryWhenOutputting, maxMemoryWhenOutputting) == 0 &&
Double.compare(that.networkCost, networkCost) == 0;
Double.compare(that.networkCost, networkCost) == 0 &&
Objects.equals(rootNodeLocalCostEstimate, that.rootNodeLocalCostEstimate);
}

@Override
public int hashCode()
{
return Objects.hash(cpuCost, maxMemory, maxMemoryWhenOutputting, networkCost);
return Objects.hash(cpuCost, maxMemory, maxMemoryWhenOutputting, networkCost, rootNodeLocalCostEstimate);
}
}

0 comments on commit cb62759

Please sign in to comment.