Github user cloud-fan commented on a diff in the pull request: https://github.com/apache/spark/pull/17240#discussion_r105823209 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CostBasedJoinReorder.scala --- @@ -122,46 +119,48 @@ case class CostBasedJoinReorder(conf: CatalystConf) extends Rule[LogicalPlan] wi * level 3: p({A, B, C, D}) * where p({A, B, C, D}) is the final output plan. * - * For cost evaluation, since physical costs for operators are not available currently, we use - * cardinalities and sizes to compute costs. + * To evaluate cost for a given plan, we calculate the sum of cardinalities for all intermediate + * joins in the plan. */ object JoinReorderDP extends PredicateHelper { def search( conf: CatalystConf, items: Seq[LogicalPlan], - conditions: Set[Expression], - topOutput: AttributeSet): Option[LogicalPlan] = { + conditions: Set[Expression]): Option[LogicalPlan] = { // Level i maintains all found plans for i + 1 items. // Create the initial plans: each plan is a single item with zero cost. - val itemIndex = items.zipWithIndex + val itemIndex = items.zipWithIndex.map(_.swap).toMap val foundPlans = mutable.Buffer[JoinPlanMap](itemIndex.map { - case (item, id) => Set(id) -> JoinPlan(Set(id), item, Set(), Cost(0, 0)) - }.toMap) + case (id, item) => Set(id) -> JoinPlan(Set(id), item, cost = 0) + }) - for (lev <- 1 until items.length) { + while (foundPlans.size < items.length && foundPlans.last.size > 1) { // Build plans for the next level. - foundPlans += searchLevel(foundPlans, conf, conditions, topOutput) + foundPlans += searchLevel(foundPlans, conf, conditions) } - val plansLastLevel = foundPlans(items.length - 1) - if (plansLastLevel.isEmpty) { - // Failed to find a plan, fall back to the original plan - None - } else { - // There must be only one plan at the last level, which contains all items. - assert(plansLastLevel.size == 1 && plansLastLevel.head._1.size == items.length) - Some(plansLastLevel.head._2.plan) + // Find the best plan + assert(foundPlans.last.size <= 1) + val bestJoinPlan = foundPlans.last.headOption --- End diff -- and what if the last level has more than one entries? shall we pick the best among them?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org