viirya commented on code in PR #60785:
URL: https://github.com/apache/spark/pull/60785#discussion_r4660476168


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala:
##########
@@ -191,13 +191,37 @@ object ExtractEquiJoinKeys extends Logging with 
PredicateHelper {
     (JoinType, Seq[Expression], Seq[Expression],
       Option[Expression], Option[Expression], LogicalPlan, LogicalPlan, 
JoinHint)
 
-  def unapply(join: Join): Option[ReturnType] = join match {
+  def unapply(join: Join): Option[ReturnType] = extract(join, allEqualities = 
false)
+
+  /**
+   * Returns the keys of `join`. If `allEqualities` is false, a conjunct that 
may not be evaluated
+   * ahead of the rest of the condition is not a key (see below), otherwise 
every equality
+   * between the two sides is.
+   */
+  private[planning] def extract(
+      join: Join,
+      allEqualities: Boolean): Option[ReturnType] = join match {
     case Join(left, right, joinType, condition, hint) =>
       logDebug(s"Considering join on: $condition")
       // Find equi-join predicates that can be evaluated before the join, and 
thus can be used
-      // as join keys.
+      // as join keys. A join computes its keys on every row of its inputs, 
ahead of the rest of
+      // its condition, so a conjunct that may not be evaluated ahead of the 
others (see
+      // `splitMovable`), e.g. one that can fail written after a throwable 
one, is not a key. It
+      // stays in the other condition, in its written order. The first 
conjunct is evaluated
+      // first on every pair anyway, so it may be a key unless it is 
non-deterministic, also when
+      // it is throwable. The columns of the inputs that can fail 
(`failableOutput`) do not
+      // count: a key over such a column reads the value that the projection 
below computes,
+      // while a filter pushed below the projection evaluates its expression 
again. This also
+      // keeps the keys the same when the inputs change, e.g. when AQE 
replaces them with query
+      // stages.
       val predicates = condition.map(splitConjunctivePredicates).getOrElse(Nil)
-      val joinKeys = predicates.flatMap {
+      val notMovable = predicates match {
+        case _ if allEqualities => Nil
+        case first +: rest if first.deterministic =>

Review Comment:
   `PushPredicateThroughJoin` puts the movable conjuncts first, so with `l.x = 
coalesce(r.w, raise_error('m')) AND l.w2 = r.w AND CAST(l.s AS INT) = r.z` the 
throwable equality is no longer first here, and the join is keyed on `w2` 
alone. This case then applies only when nothing after the throwable equality 
can move. Is it worth keeping, or should the comment say so?



-- 
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.

To unsubscribe, e-mail: [email protected]

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