private JoinEnumerationResult(Optional<PlanNode> planNode, PlanNodeCostEstimate cost) { this.planNode = requireNonNull(planNode, "planNode is null"); this.cost = requireNonNull(cost, "cost is null"); checkArgument((cost.hasUnknownComponents() || cost.equals(PlanNodeCostEstimate.infinite())) && !planNode.isPresent() || (!cost.hasUnknownComponents() || !cost.equals(PlanNodeCostEstimate.infinite())) && planNode.isPresent(), "planNode should be present if and only if cost is known"); }
CostAssertionBuilder hasUnknownComponents() { assertTrue(actual.hasUnknownComponents()); return this; } }
public int compare(Session session, PlanNodeCostEstimate left, PlanNodeCostEstimate right) { requireNonNull(session, "session is null"); requireNonNull(left, "left is null"); requireNonNull(right, "right is null"); checkArgument(!left.hasUnknownComponents() && !right.hasUnknownComponents(), "cannot compare unknown costs"); double leftCost = left.getCpuCost() * cpuWeight + left.getMemoryCost() * memoryWeight + left.getNetworkCost() * networkWeight; double rightCost = right.getCpuCost() * cpuWeight + right.getMemoryCost() * memoryWeight + right.getNetworkCost() * networkWeight; return Double.compare(leftCost, rightCost); } }
static JoinEnumerationResult createJoinEnumerationResult(Optional<PlanNode> planNode, PlanNodeCostEstimate cost) { if (cost.hasUnknownComponents()) { return UNKNOWN_COST_RESULT; } if (cost.equals(PlanNodeCostEstimate.infinite())) { return INFINITE_COST_RESULT; } return new JoinEnumerationResult(planNode, cost); } }
private PlanNode getCostBasedJoin(JoinNode joinNode, Context context) { List<PlanNodeWithCost> possibleJoinNodes = new ArrayList<>(); addJoinsWithDifferentDistributions(joinNode, possibleJoinNodes, context); addJoinsWithDifferentDistributions(joinNode.flipChildren(), possibleJoinNodes, context); if (possibleJoinNodes.stream().anyMatch(result -> result.getCost().hasUnknownComponents()) || possibleJoinNodes.isEmpty()) { return getSyntacticOrderJoin(joinNode, context, AUTOMATIC); } // Using Ordering to facilitate rule determinism Ordering<PlanNodeWithCost> planNodeOrderings = costComparator.forSession(context.getSession()).onResultOf(PlanNodeWithCost::getCost); return planNodeOrderings.min(possibleJoinNodes).getPlanNode(); }
private PlanNode getCostBasedDistributionType(SemiJoinNode node, Context context) { if (!canReplicate(node, context)) { return node.withDistributionType(PARTITIONED); } List<PlanNodeWithCost> possibleJoinNodes = new ArrayList<>(); possibleJoinNodes.add(getSemiJoinNodeWithCost(node.withDistributionType(REPLICATED), context)); possibleJoinNodes.add(getSemiJoinNodeWithCost(node.withDistributionType(PARTITIONED), context)); if (possibleJoinNodes.stream().anyMatch(result -> result.getCost().hasUnknownComponents())) { return node.withDistributionType(PARTITIONED); } // Using Ordering to facilitate rule determinism Ordering<PlanNodeWithCost> planNodeOrderings = costComparator.forSession(context.getSession()).onResultOf(PlanNodeWithCost::getCost); return planNodeOrderings.min(possibleJoinNodes).getPlanNode(); }