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

Make CostCalculator to calculate cumulatively #11689

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ private PlanNodeCostEstimate getGroupCost(GroupReference groupReference)

private PlanNodeCostEstimate calculateCumulativeCost(PlanNode node)
{
PlanNodeCostEstimate localCosts = costCalculator.calculateCost(node, statsProvider, session, types);

PlanNodeCostEstimate sourcesCost = node.getSources().stream()
.map(this::getCumulativeCost)
.reduce(PlanNodeCostEstimate.zero(), PlanNodeCostEstimate::add);

PlanNodeCostEstimate cumulativeCost = localCosts.add(sourcesCost);
return cumulativeCost;
return costCalculator.calculateCost(node, statsProvider, this, session, types);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
public interface CostCalculator
{
/**
* Calculates non-cumulative cost of a node.
* Calculates cumulative cost of a node.
*
* @param node The node to compute cost for.
* @param stats The stats provider for node's stats and child nodes' stats, to be used if stats are needed to compute cost for the {@code node}
*/
PlanNodeCostEstimate calculateCost(
PlanNode node,
StatsProvider stats,
CostProvider costs,
Session session,
TypeProvider types);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,16 @@ public CostCalculatorUsingExchanges(IntSupplier numberOfNodes)
}

@Override
public PlanNodeCostEstimate calculateCost(PlanNode node, StatsProvider stats, Session session, TypeProvider types)
public PlanNodeCostEstimate calculateCost(PlanNode node, StatsProvider stats, CostProvider costs, Session session, TypeProvider types)
{
CostEstimator costEstimator = new CostEstimator(numberOfNodes.getAsInt(), stats, types);
return node.accept(costEstimator, null);
PlanNodeCostEstimate localCosts = node.accept(costEstimator, null);

PlanNodeCostEstimate sourcesCost = node.getSources().stream()
.map(costs::getCumulativeCost)
.reduce(PlanNodeCostEstimate.zero(), PlanNodeCostEstimate::add);

return localCosts.add(sourcesCost);
}

private static class CostEstimator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public CostCalculatorWithEstimatedExchanges(CostCalculator costCalculator, IntSu
}

@Override
public PlanNodeCostEstimate calculateCost(PlanNode node, StatsProvider stats, Session session, TypeProvider types)
public PlanNodeCostEstimate calculateCost(PlanNode node, StatsProvider stats, CostProvider costs, Session session, TypeProvider types)
{
ExchangeCostEstimator exchangeCostEstimator = new ExchangeCostEstimator(numberOfNodes.getAsInt(), stats, types);
PlanNodeCostEstimate estimatedExchangeCost = node.accept(exchangeCostEstimator, null);
return costCalculator.calculateCost(node, stats, session, types).add(estimatedExchangeCost);
return costCalculator.calculateCost(node, stats, costs, session, types).add(estimatedExchangeCost);
}

private static class ExchangeCostEstimator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
import static com.facebook.presto.transaction.TransactionBuilder.transaction;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -442,11 +441,7 @@ public PlanNodeCostEstimate getCumulativeCost(PlanNode node)

private PlanNodeCostEstimate calculateCumulativeCost(PlanNode node)
{
PlanNodeCostEstimate sourcesCost = node.getSources().stream()
.map(this::getCumulativeCost)
.reduce(PlanNodeCostEstimate.zero(), PlanNodeCostEstimate::add);

return costCalculator.calculateCost(node, statsProvider, session, types).add(sourcesCost);
return costCalculator.calculateCost(node, statsProvider, this, session, types);
}
}

Expand Down Expand Up @@ -505,17 +500,15 @@ private PlanNodeCostEstimate calculateCumulativeCost(
Function<PlanNode, PlanNodeStatsEstimate> stats,
Map<String, Type> types)
{
PlanNodeCostEstimate localCost = costCalculator.calculateCost(
StatsProvider statsProvider = planNode -> requireNonNull(stats.apply(planNode), "no stats for node");
CostProvider costProvider = costs::apply;
return costCalculator.calculateCost(
node,
planNode -> requireNonNull(stats.apply(planNode), "no stats for node"),
statsProvider,
costProvider,
session,
TypeProvider.copyOf(types.entrySet().stream()
.collect(ImmutableMap.toImmutableMap(entry -> new Symbol(entry.getKey()), Map.Entry::getValue))));

PlanNodeCostEstimate sourcesCost = node.getSources().stream()
.map(source -> requireNonNull(costs.apply(source), format("no cost for source: %s", source.getId())))
.reduce(PlanNodeCostEstimate.zero(), PlanNodeCostEstimate::add);
return sourcesCost.add(localCost);
}

private PlanNodeCostEstimate calculateCumulativeCost(PlanNode node, CostCalculator costCalculator, StatsCalculator statsCalculator, Map<String, Type> types)
Expand Down