spark-test commented on code in PR #58685:
URL: https://github.com/apache/spark/pull/58685#discussion_r4508051394


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/BatchScanExec.scala:
##########
@@ -153,6 +153,17 @@ case class BatchScanExec(
     }
   }
 
+  override def outputOrdering: Seq[SortOrder] = {
+    // The base class checks grouping by the original partition keys. A later 
projection or
+    // reduction can concatenate separately ordered splits, so that ordering 
no longer applies.
+    // Check only planning parameters here: inspecting filteredPartitions 
executes runtime filters.
+    val projectsSubset = spjParams.joinKeyPositions.exists { positions =>

Review Comment:
   **Non-blocking (P2):** With partially clustered distribution, `inputRDD` 
gives each split on the non-replicated side its own partition 
(`splits.map(Seq(_)).padTo(...)`), so that side concatenates nothing and the 
inherited check's ordering is still true there. `projectsSubset` drops it 
anyway, so a partially clustered subset-key join now sorts that side, which 
partial clustering picks as the larger input, wherever the base branch streamed 
it in connector order. Results are unchanged; the cost is extra CPU, sorter 
memory and possible spill. For example, with partial clustering and 
`allowJoinKeysSubsetOfPartitionKeys` enabled, two tables partitioned by `(id, 
data)` that report `(id, ts)` ordering per split and are joined on `id` and 
`ts` now get a sort on both sides: the replicated side's sort is the correction 
this change needs, but the non-replicated side's sort is not. The layout is 
known from planning parameters (`applyPartialClustering` without 
`replicatePartitions`), so checkin
 g only planning parameters, as the code comment requires, does not force 
dropping the ordering here.
   
   See **Shared repair plan 1** in the review body.



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/KeyGroupedPartitioningSuite.scala:
##########
@@ -3109,6 +3110,83 @@ class KeyGroupedPartitioningSuite extends 
DistributionAndOrderingSuiteBase {
     }
   }
 
+  for (fullKey <- Seq(false, true)) {
+    test(s"SPARK-55411: scan ordering after SPJ key projection, 
fullKey=$fullKey") {
+      val orderedColumns = Array("discard", "k", "id")
+        .map(name => Column.create(name, IntegerType))
+      val partitionKeys = if (fullKey) Array("k", "id") else Array("discard", 
"k")
+      val ordering = Array("k", "id").map(name => Expressions.sort(
+        Expressions.column(name), SortDirection.ASCENDING, 
NullOrdering.NULLS_FIRST))
+      createTable("ordered", orderedColumns, partitionKeys.map(identity), 
ordering = ordering)
+      // Each split has one row and truthfully reports (k, id) ordering. 
Grouping on k concatenates

Review Comment:
   **Nit (P3):** This comment is inside the `fullKey` loop but describes only 
the `fullKey=false` iteration. With `fullKey=true` the table is partitioned by 
`(k, id)`, so there are no `discard=0` and `discard=1` partitions: the join 
covers the whole partition key, nothing is concatenated, and the test asserts 
that the ordering is kept with no `SortExec`. As written, a reader is told that 
both iterations need a sort, which hides that the second one is the no-sort 
control.
   
   **Verification:**
   - **Inspection:** The comment in the parameterized key-projection test 
matches what each iteration builds and asserts: concatenation and a required 
sort when a partition key is dropped, and a kept ordering with no sort when the 
full key is joined.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/BatchScanExec.scala:
##########
@@ -153,6 +153,17 @@ case class BatchScanExec(
     }
   }
 
+  override def outputOrdering: Seq[SortOrder] = {
+    // The base class checks grouping by the original partition keys. A later 
projection or
+    // reduction can concatenate separately ordered splits, so that ordering 
no longer applies.
+    // Check only planning parameters here: inspecting filteredPartitions 
executes runtime filters.
+    val projectsSubset = spjParams.joinKeyPositions.exists { positions =>
+      spjParams.keyGroupedPartitioning.exists(_.length > positions.length)
+    }
+    val reducesKeys = spjParams.reducers.exists(_.exists(_.isDefined))
+    if (projectsSubset || reducesKeys) Seq.empty else super.outputOrdering

Review Comment:
   **Non-blocking (P2):** This drops the whole reported ordering for every 
key-subset projection, including joins whose keys are all kept identity 
partition columns. `inputRDD` groups splits by the projected key, so each such 
column is constant within every output partition, and the join's required 
ascending order on it still holds after concatenation. For example, with 
`allowJoinKeysSubsetOfPartitionKeys` enabled, two tables partitioned by `(id, 
data)` that report an ordering beginning with `id`, with one split per `(id, 
data)`, are joined on `id`: the base branch plans no sort and returns correct 
rows, while this change adds a `SortExec` over every merged partition on both 
sides. Results are unchanged; the cost is row copies into the sorter, sorter 
memory and possible spill, concentrated on the ids with the most splits.
   
   See **Shared repair plan 1** in the review body.



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