Tengfei Huang created SPARK-62341:
-------------------------------------
Summary: Deserialize approx_top_k aggregation buffers without
copying the sketch
Key: SPARK-62341
URL: https://issues.apache.org/jira/browse/SPARK-62341
Project: Spark
Issue Type: Task
Components: SQL
Affects Versions: 4.2.1
Reporter: Tengfei Huang
Deserializing an approx_top_k aggregation buffer copies the entire serialized
sketch into a new byte array before DataSketches reads it, and
{{approx_top_k_combine}} copies it once more before that. Both copies are
unnecessary and add heap pressure proportional to the sketch size.
h3. Problem
{{ApproxTopKAggregateBuffer.deserialize}} copies the sketch bytes out of the
serialized buffer before handing them to DataSketches:
{code:scala}
val byteBuffer = java.nio.ByteBuffer.wrap(bytes)
val sketchBytesLength = bytes.length - 8
val sketchBytes = new Array[Byte](sketchBytesLength)
byteBuffer.get(sketchBytes, 0, sketchBytesLength)
val nullCount = byteBuffer.getLong(sketchBytesLength)
val deserializedSketch = ItemsSketch.getInstance(Memory.wrap(sketchBytes),
serDe)
{code}
{{CombineInternal.deserialize}} (the {{approx_top_k_combine}} aggregation
buffer) first copies the sketch section out of its input array and then calls
the method above, which copies it a second time.
These copies are not needed: {{ItemsSketch.getInstance}} copies the counts and
items out of the {{Memory}} it is given and keeps no reference to it, so it can
read the sketch directly from a region of the input array.
Each copy is a short-lived allocation as large as the whole sketch. With a
large {{maxItemsTracked}} (up to 1,000,000) over wide string columns, a
serialized sketch can reach hundreds of MB. On the final aggregation side of
{{ObjectHashAggregateExec}}, a buffer is deserialized for every partial
aggregate that is merged, so these copies add significant heap pressure and can
contribute to executor OOMs.
Affected paths:
* {{approx_top_k}} / {{approx_top_k_accumulate}}: deserializing partial
aggregation buffers (one redundant copy per buffer)
* {{approx_top_k_combine}}: reading each input sketch state in {{update}} (one
redundant copy), and deserializing partial aggregation buffers (two redundant
copies)
* {{approx_top_k_estimate}}: reading the sketch state (one redundant copy)
h3. Proposed fix
* Add an overload {{ApproxTopKAggregateBuffer.deserialize(bytes, offset,
length, serDe)}} that reads the null count and passes the sketch region to
DataSketches in place via {{Memory.wrap(bytes).region(offset, sketchLength)}}.
The existing signature delegates to it.
* {{CombineInternal.deserialize}} passes the offset and length of the sketch
section instead of copying it out.
The serialized format and the decoded sketch are unchanged, so no config is
needed and there is no compatibility impact. The format must stay the same
because {{approx_top_k_accumulate}} returns it as the sketch state, which users
can persist.
Out of scope: the copies on the serialize side ({{ItemsSketch.toByteArray}}
plus appending the null count).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]