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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -9099,6 +9098,31 @@ object SQLConf {
       .checkValue(_ >= 0, "The maximum number of union children must be 
non-negative.")
       .createWithDefault(10)
 
+  val PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_V2_BUCKETING_ENABLED =
+    
buildConf("spark.sql.adaptive.pushDownShuffleThroughUnion.allowV2Bucketing.enabled")
+      .internal()
+      .doc("When true, union shuffle push-down may use a V2 keyed layout as 
the union's " +
+        "output, whether the shuffle it replaces was keyed or hash. Children 
already reporting " +
+        "the picked domain keep their layouts, and an unmarked child reporting 
a subset of its " +
+        "keys keeps it too; a marked child has to declare the domain's list in 
the same order. " +
+        "The others are shuffled onto it, which creates keyed shuffles, and 
rows of a key the " +
+        "domain does not declare are routed by hash, which the layout records. 
The grouping " +
+        "above the rebuilt union then holds the full-key claim only. Set to 
false to restrict " +
+        s"the rule to hash layouts. Requires 
${PUSH_DOWN_SHUFFLE_THROUGH_UNION_ENABLED.key} and " +
+        s"${V2_BUCKETING_ENABLED.key}, ${V2_BUCKETING_SHUFFLE_ENABLED.key} 
since a child may be " +
+        s"shuffled onto the domain, and " +
+        s"${V2_BUCKETING_PARTIALLY_CLUSTERED_DISTRIBUTION_ENABLED.key} to be 
disabled. A target " +
+        s"naming only part of the consumer's cluster keys further needs " +

Review Comment:
   The `allowV2Bucketing` doc now states that a target partitioned by a 
transform over the cluster keys, such as `bucket(n, id)`, needs 
`allowPartitionKeysSubsetOfClusterKeys` whatever 
`requireAllClusterKeysForCoPartition` is set to, while a target naming only 
part of the cluster keys needs both that permission and the co-partition 
requirement off. A transform domain meets the co-partition requirement because 
it references every cluster key. Fixed in 3b7f95c6c1f.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -9099,6 +9098,31 @@ object SQLConf {
       .checkValue(_ >= 0, "The maximum number of union children must be 
non-negative.")
       .createWithDefault(10)
 
+  val PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_V2_BUCKETING_ENABLED =
+    
buildConf("spark.sql.adaptive.pushDownShuffleThroughUnion.allowV2Bucketing.enabled")
+      .internal()
+      .doc("When true, union shuffle push-down may use a V2 keyed layout as 
the union's " +
+        "output, whether the shuffle it replaces was keyed or hash. Children 
already reporting " +
+        "the picked domain keep their layouts, and an unmarked child reporting 
a subset of its " +
+        "keys keeps it too; a marked child has to declare the domain's list in 
the same order. " +
+        "The others are shuffled onto it, which creates keyed shuffles, and 
rows of a key the " +
+        "domain does not declare are routed by hash, which the layout records. 
The grouping " +
+        "above the rebuilt union then holds the full-key claim only. Set to 
false to restrict " +
+        s"the rule to hash layouts. Requires 
${PUSH_DOWN_SHUFFLE_THROUGH_UNION_ENABLED.key} and " +
+        s"${V2_BUCKETING_ENABLED.key}, ${V2_BUCKETING_SHUFFLE_ENABLED.key} 
since a child may be " +
+        s"shuffled onto the domain, and " +
+        s"${V2_BUCKETING_PARTIALLY_CLUSTERED_DISTRIBUTION_ENABLED.key} to be 
disabled. A target " +
+        s"naming only part of the consumer's cluster keys further needs " +
+        s"${V2_BUCKETING_REQUIRE_ALL_CLUSTER_KEYS_FOR_CO_PARTITION.key} off 
and " +
+        
"spark.sql.adaptive.pushDownShuffleThroughUnion.allowPartitionKeysSubsetOfClusterKeys."
 +
+        "enabled on. A source-reported keyed layout is kept only when the 
consumer's shuffle " +
+        s"group cannot coalesce, so ${COALESCE_PARTITIONS_ENABLED.key} must be 
off or " +

Review Comment:
   The prerequisite sentence now says a source-reported keyed layout is kept 
only when the consumer's shuffle group cannot coalesce: coalescing off, 
`keepNonCoalescibleBranches` on, or a consumer whose inputs do not all reach an 
exchange, which the rule admits without either setting. Fixed in 3b7f95c6c1f.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/physical/partitioning.scala:
##########
@@ -579,6 +579,41 @@ case class KeyLayout(
    */
   def describesSameKeys(other: KeyLayout): Boolean =
     dataTypes == other.dataTypes && partitionKeys == other.partitionKeys
+
+  /**
+   * Whether every key of this layout is one of `other`'s. The types are 
asked; the order and how
+   * often a key repeats are not. Callers ask this beside 
`describesSameLayoutState`, and only
+   * where a later node merges the key lists: the rows are already in the 
partitions `other` names
+   * for their keys, and the layouts declaring the remaining keys supply the 
rest.
+   */
+  def describesSubsetOfKeys(other: KeyLayout): Boolean =
+    dataTypes == other.dataTypes && {
+      val declared = other.partitionKeys.toSet
+      partitionKeys.forall(declared.contains)
+    }
+
+  /**
+   * Whether `other` describes the same keys as this layout as a set: the same 
key values at the
+   * same types, whatever their order and how often each repeats. The types 
are asked for the reason
+   * given on `describesSameKeys`.
+   *
+   * A call site may read a domain this way only when a merge and a sort have 
already settled the
+   * key order: the caller compares such a result against the layout it was 
built for. A pairing
+   * and a concatenation keep the order they are given, so they ask 
`describesSameKeys`. Callers ask
+   * this beside `describesSameLayoutState`, which makes the repeats harmless: 
equal grouping state
+   * means both lists hold unique keys, so the set says the same as the 
multiset.

Review Comment:
   The doc now says a caller asks it beside `describesSameLayoutState`, which 
makes the repeats harmless while one side is grouped, and that two ungrouped 
layouts can agree on both and still repeat their keys differently. Fixed in 
3b7f95c6c1f.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/PushDownShuffleExchangeThroughUnion.scala:
##########
@@ -240,56 +267,76 @@ case class PushDownShuffleExchangeThroughUnion(
       maxUnionChildren: Int,
       maxTargetPartitionNumFactor: Double,
       keepJoinBranches: Boolean,
+      allowV2Bucketing: Boolean,
       allowSubsetTargets: Boolean,
       preferRemovedLayout: Boolean = false)
 
-  /** Lazily discover and rank layouts once per site for both joint and 
fallback attempts. */
-  private class PreparedChild(val pushable: PushableChild, policy: 
LayoutPolicy) {
+  /** Lazily discover the reported layouts and rank the targets once per site 
for both attempts. */
+  private class PreparedChild(
+      val pushable: PushableChild,
+      required: ClusteredDistribution,
+      policy: LayoutPolicy) {
     /**
-     * Eligible child layouts in child order, expressed in the union's output 
attributes.
-     * Collections offer their hash members; a child offering nothing takes a 
shuffle.
-     * Equality follows UnionExec's comparison after prepareOutputPartitioning 
rebinding.
+     * The hash or keyed layouts each child reports, in child order, expressed 
in the union's output
+     * attributes. A collection offers its hash members; a keyed layout offers 
itself when it is
+     * grouped and its grouped form when it is not, the form 
`EnsureRequirements` plans. A child
+     * offering nothing takes a shuffle.
+     *
+     * A kept marked leg is still subject to the union's concatenation: beside 
another marked leg,
+     * it has to declare the same key list in the same order and still route 
by hash. One that
+     * fails those makes the candidate fail its final check, and the plan is 
left as it was.

Review Comment:
   The `layouts` doc now says a marked leg that declares the target's key list 
in a different order, or no longer routes by hash, is laid out like any other 
child, so the site is rewritten instead of left as it was. Fixed in 3b7f95c6c1f.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/PushDownShuffleExchangeThroughUnion.scala:
##########
@@ -639,8 +793,22 @@ case class PushDownShuffleExchangeThroughUnion(
     // Origin inheritance can tag a zero-move candidate, so it must not reuse 
the input union.
     val rebuilt = union.copy(children = newChildren)
     rebuilt.copyTagsFrom(union)
-    Option.when(
-      
PartitioningCollection.flatten(rebuilt.outputPartitioning).contains(target))(rebuilt)
+    val grouped = rebuilt.outputPartitioning match {
+      case keyed: KeyedPartitioning if !keyed.isGrouped =>
+        // Grouping merges the partitions that share a key. That loses 
granularity only for a
+        // collapsed layout, which the planner gates behind the V2 subset 
permission; ask the same
+        // question here. Every kept child shares the target's collapse state, 
so the concatenation
+        // gives the same answer as the target.
+        if (!keyed.mayGroupToSatisfy(required)) {
+          return None
+        }
+        GroupPartitionsExec(rebuilt)
+      case _ => rebuilt
+    }
+    // The last word on a candidate: the union has to report the target's key 
domain. The grouping
+    // above sorts the keys, so the order of the target is not part of that 
check.
+    Option.when(PartitioningCollection.flatten(grouped.outputPartitioning)

Review Comment:
   `rebuildUnion` now derives the union's partitioning once and hands the same 
value to the final key-set check, so a hash candidate pays one derivation. 
Fixed in 3b7f95c6c1f.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/adaptive/PushDownShuffleExchangeThroughUnionSuite.scala:
##########
@@ -201,6 +213,36 @@ class PushDownShuffleExchangeThroughUnionSuite extends 
QueryTest with SharedSpar
       spark.sql(sql).collect().toSeq
     }
 
+  test("SPARK-61785: a query on the defaults keeps its shuffle") {

Review Comment:
   The test now resets each config to its declared default through 
`defaultValueString`, including `pushDownShuffleThroughUnion.enabled` and 
`maxTargetPartitionNumFactor`, instead of pinning explicit `false` values, so 
the asserted plan is the one the declared defaults produce. Fixed in 
3b7f95c6c1f.
   



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