weiting-chen commented on code in PR #13171:
URL: https://github.com/apache/gluten/pull/13171#discussion_r4625415090
##########
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:
**Avoid task-lifetime C2R array retention in keyed shuffle (AI-generated
finding)**
**Problem:** Each nonempty batch on this new keyed-shuffle path calls
`convertColumnarToRow(cb)`. That method registers an `IteratorCompleter` with
`TaskResources`; exhaustion closes the native converter but does not unregister
the completer or clear its input. The registry therefore retains the completed
row iterator and its final `NativeColumnarToRowInfo.offsets`/`lengths` arrays
until the mapper task finishes. For batches fitting one conversion, those
arrays alone accumulate eight bytes per processed row, plus iterator/registry
overhead. A sufficiently large mapper can exhaust JVM heap even though every
native converter closes.
**Evidence:**
```scala
computeAndAddPartitionId(cbIter, partitioner, partitionKeyExtractor)
```
This newly added call reaches
`convertColumnarToRow(cb).zipWithIndex.foreach` for every batch.
`IteratorsV1.scala:57-82` retains the iterator through a task recycler, and
`JniWrapper.cc:635-644` allocates the two JVM arrays. The existing range path
shares the limitation, but this change newly exposes key-grouped shuffle; its
previous vanilla fallback used the single-converter
`VeloxColumnarToRowExec.toRowIterator` path.
**Suggested Fix:** Follow that existing task-scoped converter pattern in
`computeAndAddPartitionId`: pass a conversion metric into the helper, create
one converter outside the per-batch map, use it for the PID loop, and register
its close callback once on the outer returned iterator. Preserve the existing
payload recycler and task-abort cleanup.
```scala
val converter = new VeloxColumnarToRowExec.Converter(convertTime)
```
Replace the per-batch `convertColumnarToRow(cb)` loop with:
```scala
converter.toRowIterator(cb).zipWithIndex.foreach {
case (row, i) =>
val pid = partitioner.getPartition(partitionKeyExtractor(row))
pidVec.set(i, pid)
}
```
Add this once to the outer iterator builder, before `.create()`:
```scala
.recycleIterator {
converter.close()
}
```
A many-batch regression should check that completed conversion state does
not accumulate before task completion, including early-abort cleanup.
<!-- pr-review-fingerprint:
prf-v1:392319e6507cfbba2befe30b8617270bb49b5fe9c745bf7542f7e1fc0412f75b -->
--
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]