minni31 commented on code in PR #13171:
URL: https://github.com/apache/gluten/pull/13171#discussion_r4640789318


##########
backends-velox/src/main/scala/org/apache/spark/sql/execution/utils/ExecUtil.scala:
##########
@@ -200,11 +242,52 @@ object ExecUtil {
                 UnsafeProjection.create(sortingExpressions.map(_.child), 
outputAttributes)
               row => projection(row)
             }
-            val newIter = computeAndAddPartitionId(cbIter, 
partitionKeyExtractor)
+            val newIter =
+              computeAndAddPartitionId(cbIter, rangePartitioner.get, 
partitionKeyExtractor)
             newIter
           },
           isOrderSensitive = isOrderSensitive
         )
+      case _ if keyGroupedShuffleInfo.nonEmpty =>
+        val expressions = keyGroupedShuffleInfo.get.expressions
+        val partitionValueBytes = keyGroupedPartitionValueBytes.get
+        rdd.mapPartitionsWithIndexInternal(
+          (_, cbIter) => {
+            val dataTypes = expressions.map(_.dataType)
+            val ordering = 
RowOrdering.createNaturalAscendingOrdering(dataTypes)
+            val partitionIds = partitionValueBytes.foldLeft(
+              TreeMap.empty[InternalRow, Int](ordering)) {
+              case (ids, (bytes, index)) =>
+                val partitionValue = new UnsafeRow(expressions.size)
+                partitionValue.pointTo(bytes, bytes.length)
+                ids.updated(partitionValue, index)
+            }
+            val boundExpressions =
+              BindReferences.bindReferences(expressions, 
outputAttributes).toArray
+            val partitionKey = new GenericInternalRow(boundExpressions.length)
+            val comparableUnknownKey = if 
(keyGroupedUnknownKeyUsesComparableHash) {
+              Some(InternalRowComparableWrapper(partitionKey, expressions))
+            } else {
+              None
+            }
+            val partitioner =
+              new KeyGroupedPartitionIdPartitioner(
+                partitionIds,
+                dataTypes,
+                comparableUnknownKey)
+            val partitionKeyExtractor: InternalRow => Any = {
+              row =>
+                var index = 0
+                while (index < boundExpressions.length) {
+                  partitionKey.update(index, boundExpressions(index).eval(row))
+                  index += 1
+                }
+                partitionKey
+            }
+            computeAndAddPartitionId(cbIter, partitioner, 
partitionKeyExtractor)

Review Comment:
   Addressed in 3ac1a65aa2420c001176809d729e55fd364a6251.
   
   Both keyed and range partition-ID paths now create one 
`VeloxColumnarToRowExec.Converter` per partition iterator and pass it to 
`computeAndAddPartitionId`. The PID loop uses `converter.toRowIterator(cb)`, 
which does not register a task recycler for each batch. A single outer 
`.recycleIterator { converter.close() }` handles exhaustion and task 
termination; the existing composed-batch payload recycler is preserved.
   
   Added `GlutenPartitionIdSuite` for Spark 4.1/Velox, covering 128 batches 
with a stable iterator-recycler count, PID/payload correctness, exactly-once 
converter closure on exhaustion, and cleanup on early completion or task 
failure.
   
   Scala formatting, license-header and whitespace checks passed. Compilation 
and native/Spark runtime tests were not run locally. The PR currently reports 
no fork runs for the Velox x86, ARM and Kafka checks, so native CI validation 
remains pending; these regressions are added coverage, not claimed passing 
runtime results.
   



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