tanelk commented on a change in pull request #32014:
URL: https://github.com/apache/spark/pull/32014#discussion_r604888377



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CostBasedJoinReorder.scala
##########
@@ -349,11 +349,14 @@ object JoinReorderDP extends PredicateHelper with Logging 
{
     }
 
     def betterThan(other: JoinPlan, conf: SQLConf): Boolean = {
-      val thisCost = BigDecimal(this.planCost.card) * 
conf.joinReorderCardWeight +
-        BigDecimal(this.planCost.size) * (1 - conf.joinReorderCardWeight)
-      val otherCost = BigDecimal(other.planCost.card) * 
conf.joinReorderCardWeight +
-        BigDecimal(other.planCost.size) * (1 - conf.joinReorderCardWeight)
-      thisCost < otherCost
+      if (other.planCost.card == 0 || other.planCost.size == 0) {
+        false
+      } else {
+        val relativeRows = BigDecimal(this.planCost.card) / 
BigDecimal(other.planCost.card)
+        val relativeSize = BigDecimal(this.planCost.size) / 
BigDecimal(other.planCost.size)
+        Math.pow(relativeRows.doubleValue(), conf.joinReorderCardWeight) *
+          Math.pow(relativeSize.doubleValue(), 1 - conf.joinReorderCardWeight) 
< 1

Review comment:
       If I'm not mistaken, then, when the left side is `x` for 
`A.betterThan(B)`, then for `B.betterThan(A)` it will be `1/x`. One of them 
will be greater than 1 and other smaller.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to