ulysses-you commented on code in PR #60118:
URL: https://github.com/apache/spark/pull/60118#discussion_r4509217821


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledJoin.scala:
##########
@@ -129,6 +129,107 @@ trait ShuffledJoin extends JoinCodegenSupport {
     }
   }
 
+  /**
+   * The output partitioning of a FULL OUTER join, rebuilt on the coalesced 
join keys per
+   * partition expression, or `UnknownPartitioning` where that is not sound.
+   *
+   * Neither side's partitioning describes the output: a NULL-padded row of 
one side was placed by
+   * the other side's key. The coalesced key does describe every row. A 
matched row has equal keys
+   * on both sides, and an unmatched row has its own key next to NULLs, so it 
sits in the partition
+   * its coalesced key hashes to. That takes three things:
+   *  - the two sides are co-partitioned hash layouts, so the i-th expressions 
of the two sides are
+   *    the two sides of one join key pair and hash into the same partition 
space;
+   *  - every key evaluates to NULL on a NULL-padded row (see 
`isNullOnNullPadding`);
+   *  - every key is deterministic, which holds as a join condition cannot be 
non-deterministic.
+   * A null-aware side may have spread its NULL-keyed rows, and those are 
exactly the rows whose
+   * coalesced key contains a NULL, so the output is null-aware whenever 
either side is.
+   *
+   * Every sound pair of the children's members is reported, each in the two 
argument orders of
+   * `coalescedKeys`, in a `PartitioningCollection` when there is more than 
one.
+   */
+  private lazy val fullOuterOutputPartitioning: Partitioning = {
+    val leftPartitioning = left.outputPartitioning
+    // Spreading is allowed here rather than read from 
`requiredChildDistribution`, which follows
+    // the live `spark.sql.shuffle.spreadNullJoinKeys.enabled`. A null-aware 
side then pairs with a
+    // strict one however the conf has changed since planning, and the output 
takes the null-aware
+    // shape, which is sound either way.
+    val leftDist = ClusteredDistribution(leftKeys, allowNullKeySpreading = 
true)
+    val rightDist = ClusteredDistribution(rightKeys, allowNullKeySpreading = 
true)
+    val coalesced = for {
+      l <- hashPartitionings(leftPartitioning)
+      r <- hashPartitionings(right.outputPartitioning)
+      p <- coalescedPartitionings(l, leftDist, r, rightDist)
+    } yield p
+    coalesced match {
+      case Seq() => UnknownPartitioning(leftPartitioning.numPartitions)
+      case Seq(single) => single

Review Comment:
   Thanks @peter-toth! Removed in 7dd9f36cbdc; a non-empty result is always a 
`PartitioningCollection`. The scaladoc no longer says "when there is more than 
one".
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledJoin.scala:
##########
@@ -129,6 +129,107 @@ trait ShuffledJoin extends JoinCodegenSupport {
     }
   }
 
+  /**
+   * The output partitioning of a FULL OUTER join, rebuilt on the coalesced 
join keys per
+   * partition expression, or `UnknownPartitioning` where that is not sound.
+   *
+   * Neither side's partitioning describes the output: a NULL-padded row of 
one side was placed by
+   * the other side's key. The coalesced key does describe every row. A 
matched row has equal keys
+   * on both sides, and an unmatched row has its own key next to NULLs, so it 
sits in the partition
+   * its coalesced key hashes to. That takes three things:
+   *  - the two sides are co-partitioned hash layouts, so the i-th expressions 
of the two sides are
+   *    the two sides of one join key pair and hash into the same partition 
space;
+   *  - every key evaluates to NULL on a NULL-padded row (see 
`isNullOnNullPadding`);
+   *  - every key is deterministic, which holds as a join condition cannot be 
non-deterministic.
+   * A null-aware side may have spread its NULL-keyed rows, and those are 
exactly the rows whose
+   * coalesced key contains a NULL, so the output is null-aware whenever 
either side is.
+   *
+   * Every sound pair of the children's members is reported, each in the two 
argument orders of
+   * `coalescedKeys`, in a `PartitioningCollection` when there is more than 
one.
+   */
+  private lazy val fullOuterOutputPartitioning: Partitioning = {
+    val leftPartitioning = left.outputPartitioning
+    // Spreading is allowed here rather than read from 
`requiredChildDistribution`, which follows
+    // the live `spark.sql.shuffle.spreadNullJoinKeys.enabled`. A null-aware 
side then pairs with a
+    // strict one however the conf has changed since planning, and the output 
takes the null-aware
+    // shape, which is sound either way.
+    val leftDist = ClusteredDistribution(leftKeys, allowNullKeySpreading = 
true)
+    val rightDist = ClusteredDistribution(rightKeys, allowNullKeySpreading = 
true)
+    val coalesced = for {
+      l <- hashPartitionings(leftPartitioning)
+      r <- hashPartitionings(right.outputPartitioning)
+      p <- coalescedPartitionings(l, leftDist, r, rightDist)
+    } yield p
+    coalesced match {
+      case Seq() => UnknownPartitioning(leftPartitioning.numPartitions)
+      case Seq(single) => single
+      case multiple => PartitioningCollection.fromPartitionings(multiple)
+    }
+  }
+
+  private def hashPartitionings(partitioning: Partitioning): 
Seq[HashPartitioningLike] =
+    PartitioningCollection.flatten(partitioning).collect { case h: 
HashPartitioningLike => h }
+
+  private def coalescedPartitionings(
+      leftPartitioning: HashPartitioningLike,
+      leftDist: ClusteredDistribution,
+      rightPartitioning: HashPartitioningLike,
+      rightDist: ClusteredDistribution): Seq[Partitioning] = {
+    // Compatibility pairs the i-th expressions of the two sides by join key 
position, and for a
+    // coalesced pair also requires the same reducer boundaries.
+    val compatible = leftPartitioning.createShuffleSpec(leftDist)
+      .isCompatibleWith(rightPartitioning.createShuffleSpec(rightDist))
+    if (!compatible) return Nil
+
+    val keyPairs = 
leftPartitioning.expressions.zip(rightPartitioning.expressions)
+    val allowCoalesced = keyPairs.forall { case (l, r) => canCoalesce(l, r) }
+    if (!allowCoalesced) return Nil
+
+    // A compatible pair has the same number of partitions and, if coalesced, 
the same boundaries,
+    // so either side fits, and a null-aware side is the one to keep.
+    val candidatePartitioning = rightPartitioning match {
+      case _: NullAwareHashPartitioning | _: 
CoalescedNullAwareHashPartitioning => rightPartitioning
+      case _ => leftPartitioning
+    }
+    coalescedKeys(keyPairs).map { keys =>
+      candidatePartitioning.withNewChildren(keys).asInstanceOf[Partitioning]
+    }
+  }
+
+  /**
+   * The coalesced keys of `keyPairs` with every key left first, and with 
every key right first.
+   * For key pairs that pass `canCoalesce`, `Coalesce(l, r)` and `Coalesce(r, 
l)` agree on every
+   * output row: a matched row has equal keys, and an unmatched row has a NULL 
on the other side.
+   * `USING` gives the left-first order, and a query usually writes all of its 
keys in one order,
+   * so the mixed orders are not reported.
+   */
+  private def coalescedKeys(keyPairs: Seq[(Expression, Expression)]): 
Seq[Seq[Expression]] = {
+    val leftFirst = keyPairs.map { case (l, r) => Coalesce(Seq(l, r)) }

Review Comment:
   Thanks @dongjoon-hyun! Fixed in 7dd9f36cbdc: the claim's attributes are 
nullable, as in `output`. Your self-join shape returns 150 rows on the previous 
head, but a simpler shape over the same checkpoint does fail there: with AQE 
off, `cp.groupBy($"ak").count()` returns 105 rows with five NULL groups instead 
of 101 with one, because the recorded `coalesce(ak, bk)` folds to `ak`. The new 
test "a checkpointed join output keeps the claim on nullable keys" covers it 
with plain keys and with `ak + 1`, which fails if only top-level attributes are 
made nullable. This was a regression of this change.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledJoin.scala:
##########
@@ -129,6 +129,107 @@ trait ShuffledJoin extends JoinCodegenSupport {
     }
   }
 
+  /**
+   * The output partitioning of a FULL OUTER join, rebuilt on the coalesced 
join keys per
+   * partition expression, or `UnknownPartitioning` where that is not sound.
+   *
+   * Neither side's partitioning describes the output: a NULL-padded row of 
one side was placed by
+   * the other side's key. The coalesced key does describe every row. A 
matched row has equal keys
+   * on both sides, and an unmatched row has its own key next to NULLs, so it 
sits in the partition
+   * its coalesced key hashes to. That takes three things:
+   *  - the two sides are co-partitioned hash layouts, so the i-th expressions 
of the two sides are
+   *    the two sides of one join key pair and hash into the same partition 
space;
+   *  - every key evaluates to NULL on a NULL-padded row (see 
`isNullOnNullPadding`);
+   *  - every key is deterministic, which holds as a join condition cannot be 
non-deterministic.
+   * A null-aware side may have spread its NULL-keyed rows, and those are 
exactly the rows whose
+   * coalesced key contains a NULL, so the output is null-aware whenever 
either side is.
+   *
+   * Every sound pair of the children's members is reported, each in the two 
argument orders of
+   * `coalescedKeys`, in a `PartitioningCollection` when there is more than 
one.
+   */
+  private lazy val fullOuterOutputPartitioning: Partitioning = {

Review Comment:
   Thanks @dongjoon-hyun! Filed SPARK-61209 for the per-preparation snapshot, 
listing this shape, the inner `JOIN USING` union on master, and the BHJ expand 
limit. Reproduced here: 19 rows before the change, 28 after. The root cause 
predates this PR, so this PR leaves it to that ticket.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledJoin.scala:
##########
@@ -129,6 +129,107 @@ trait ShuffledJoin extends JoinCodegenSupport {
     }
   }
 
+  /**
+   * The output partitioning of a FULL OUTER join, rebuilt on the coalesced 
join keys per
+   * partition expression, or `UnknownPartitioning` where that is not sound.
+   *
+   * Neither side's partitioning describes the output: a NULL-padded row of 
one side was placed by
+   * the other side's key. The coalesced key does describe every row. A 
matched row has equal keys
+   * on both sides, and an unmatched row has its own key next to NULLs, so it 
sits in the partition
+   * its coalesced key hashes to. That takes three things:
+   *  - the two sides are co-partitioned hash layouts, so the i-th expressions 
of the two sides are
+   *    the two sides of one join key pair and hash into the same partition 
space;
+   *  - every key evaluates to NULL on a NULL-padded row (see 
`isNullOnNullPadding`);
+   *  - every key is deterministic, which holds as a join condition cannot be 
non-deterministic.
+   * A null-aware side may have spread its NULL-keyed rows, and those are 
exactly the rows whose
+   * coalesced key contains a NULL, so the output is null-aware whenever 
either side is.
+   *
+   * Every sound pair of the children's members is reported, each in the two 
argument orders of
+   * `coalescedKeys`, in a `PartitioningCollection` when there is more than 
one.
+   */
+  private lazy val fullOuterOutputPartitioning: Partitioning = {
+    val leftPartitioning = left.outputPartitioning
+    // Spreading is allowed here rather than read from 
`requiredChildDistribution`, which follows
+    // the live `spark.sql.shuffle.spreadNullJoinKeys.enabled`. A null-aware 
side then pairs with a
+    // strict one however the conf has changed since planning, and the output 
takes the null-aware
+    // shape, which is sound either way.
+    val leftDist = ClusteredDistribution(leftKeys, allowNullKeySpreading = 
true)
+    val rightDist = ClusteredDistribution(rightKeys, allowNullKeySpreading = 
true)
+    val coalesced = for {
+      l <- hashPartitionings(leftPartitioning)
+      r <- hashPartitionings(right.outputPartitioning)
+      p <- coalescedPartitionings(l, leftDist, r, rightDist)
+    } yield p

Review Comment:
   Thanks @dongjoon-hyun! Fixed in 7dd9f36cbdc with `lazy val rightHashes`, as 
suggested.
   



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