saths008 commented on code in PR #59566:
URL: https://github.com/apache/spark/pull/59566#discussion_r4627821410


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetWriteSupport.scala:
##########
@@ -243,8 +244,20 @@ class ParquetWriteSupport extends 
WriteSupport[InternalRow] with Logging {
 
       case _: StringType =>
         (row: SpecializedGetters, ordinal: Int) =>
-          recordConsumer.addBinary(
-            Binary.fromReusedByteArray(row.getUTF8String(ordinal).getBytes))
+          val value = row.getUTF8String(ordinal)
+          // Mark the bytes as reused so Parquet copies values that it retains.
+          recordConsumer.addBinary(value.getBaseObject match {
+            case bytes: Array[Byte] =>
+              if (value.getBaseOffset == Platform.BYTE_ARRAY_OFFSET &&
+                  value.numBytes() == bytes.length) {
+                // Use the smaller wrapper for whole-array strings to avoid 
extra allocation.
+                Binary.fromReusedByteArray(bytes)
+              } else {
+                Binary.fromReusedByteArray(bytes,
+                  (value.getBaseOffset - Platform.BYTE_ARRAY_OFFSET).toInt, 
value.numBytes())
+              }

Review Comment:
   It looks like `getByteBuffer` creates a temp buffer array, with some of the 
benchmarks we were seeing higher allocs so keeping as it currently is for now. 
let me know what you think



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