@Override public PlanNodeCostEstimate visitEnforceSingleRow(EnforceSingleRowNode node, Void context) { return PlanNodeCostEstimate.zero(); }
@Override public PlanNodeCostEstimate visitUnion(UnionNode node, Void context) { // Cost will be accounted either in CostCalculatorUsingExchanges#CostEstimator#visitExchanged // or in CostCalculatorWithEstimatedExchanges#CostEstimator#visitUnion // This stub is needed just to avoid the cumulative cost being set to unknown return PlanNodeCostEstimate.zero(); }
@Override protected PlanNodeCostEstimate visitPlan(PlanNode node, Void context) { // TODO implement logic for other node types and return PlanNodeCostEstimate.unknown() here (or throw) return PlanNodeCostEstimate.zero(); }
@Override public PlanNodeCostEstimate visitOutput(OutputNode node, Void context) { return PlanNodeCostEstimate.zero(); }
@Override public PlanNodeCostEstimate visitValues(ValuesNode node, Void context) { return PlanNodeCostEstimate.zero(); }
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; } }
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); } }
private PlanNodeCostEstimate calculateCumulativeCost( CostCalculator costCalculator, PlanNode node, Function<PlanNode, PlanNodeCostEstimate> costs, Function<PlanNode, PlanNodeStatsEstimate> stats, Map<String, Type> types) { PlanNodeCostEstimate localCost = costCalculator.calculateCost( node, planNode -> requireNonNull(stats.apply(planNode), "no stats for node"), 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); }
@Test public void testUnknownCost() { CostComparator costComparator = new CostComparator(1.0, 1.0, 1.0); Session session = testSessionBuilder().build(); assertThrows(IllegalArgumentException.class, () -> costComparator.compare(session, PlanNodeCostEstimate.zero(), PlanNodeCostEstimate.unknown())); assertThrows(IllegalArgumentException.class, () -> costComparator.compare(session, PlanNodeCostEstimate.unknown(), PlanNodeCostEstimate.zero())); assertThrows(IllegalArgumentException.class, () -> costComparator.compare(session, PlanNodeCostEstimate.unknown(), PlanNodeCostEstimate.unknown())); }