saths008 commented on code in PR #59566:
URL: https://github.com/apache/spark/pull/59566#discussion_r4627798495
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetIOSuite.scala:
##########
@@ -164,6 +173,124 @@ class ParquetIOSuite extends ParquetTest with
SharedSparkSession {
withSQLConf(SQLConf.PARQUET_BINARY_AS_STRING.key ->
"true")(checkParquetFile(data))
}
+ test("SPARK-60321: string writer reuses the backing array of a UTF8String
slice") {
+ val consumer = mock(classOf[RecordConsumer])
+ val support = new ParquetWriteSupport
+ support.init(parquetStringWriterConf)
+ support.prepareForWrite(consumer)
+ val bytes = "prefix-value-suffix".getBytes(UTF_8)
+ support.write(InternalRow(UTF8String.fromBytes(bytes, 7, 5)))
+
+ val argument = ArgumentCaptor.forClass(classOf[Binary])
+ verify(consumer).addBinary(argument.capture())
+ val binary = argument.getValue
+ assert(binary.isBackingBytesReused)
+ assert(binary.toByteBuffer.array() eq bytes)
+ assert(binary.toStringUsingUTF8 == "value")
+ }
+
+ for {
+ storage <- Seq("array", "slice", "offheap")
+ dictionary <- Seq("off", "on", "fallback")
+ version <- ParquetProperties.WriterVersion.values()
+ } {
+ test(s"SPARK-60321: strings with reused $storage memory, " +
+ s"dictionary $dictionary, writer $version") {
+ val expected = reusedStringTestValues
+ val offHeap = storage == "offheap"
+ withTempPath { file =>
+ val conf = parquetStringWriterConf
+ val path = new Path(file.toURI)
+ val padding = 13
+ val shared = new Array[Byte](64 * 1024)
+ val address = if (offHeap) Platform.allocateMemory(shared.length) else
0L
+ try {
+ // Surround each payload with sentinel bytes, so that an off-by-one
offset or length
+ // corrupts the output instead of silently passing.
+ Arrays.fill(shared, '!'.toByte)
+ if (offHeap) {
+ Platform.setMemory(address, '!'.toByte, shared.length)
+ }
+ val writer = new InternalRowParquetWriterBuilder(path)
+ .withConf(conf)
+ .withWriterVersion(version)
+ .withCompressionCodec(SNAPPY)
+ .withDictionaryEncoding(dictionary != "off")
+ .withDictionaryPageSize(if (dictionary == "fallback") 4096 else
1024 * 1024)
+ .withPageRowCountLimit(64)
+ .withMinRowCountForPageSizeCheck(1)
+ .withMaxRowCountForPageSizeCheck(64)
+ .withStatisticsTruncateLength(stringStatisticsTruncateLength)
+ .build()
+ try {
+ expected.foreach { text =>
+ if (text == null) {
+ writer.write(InternalRow(null))
+ } else {
+ // Each case writes into the storage it exercises and then
overwrites just the
+ // range it wrote: retained values must survive the producer
reusing its storage.
+ val bytes = text.getBytes(UTF_8)
+ storage match {
+ case "array" =>
+ writer.write(InternalRow(UTF8String.fromBytes(bytes)))
+ Arrays.fill(bytes, '?'.toByte)
+ case "slice" =>
+ System.arraycopy(bytes, 0, shared, padding, bytes.length)
+ writer.write(InternalRow(UTF8String.fromBytes(shared,
padding, bytes.length)))
+ Arrays.fill(shared, padding, padding + bytes.length,
'?'.toByte)
+ case "offheap" =>
+ Platform.copyMemory(
+ bytes, Platform.BYTE_ARRAY_OFFSET, null, address +
padding, bytes.length)
+ writer.write(
+ InternalRow(UTF8String.fromAddress(null, address +
padding, bytes.length)))
+ Platform.setMemory(address + padding, '?'.toByte,
bytes.length)
+ }
+ }
+ }
+ } finally {
+ writer.close()
+ }
+ } finally {
+ if (offHeap) {
+ Platform.freeMemory(address)
+ }
+ }
+
+ withAllParquetReaders {
+ checkAnswer(spark.read.parquet(file.toString), expected.map(Row(_)))
+ }
+ val reader = ParquetFileReader.open(HadoopInputFile.fromPath(path,
conf))
+ try {
+ val blocks = reader.getFooter.getBlocks
+ assert(blocks.size() == 1)
+ val column = blocks.get(0).getColumns.get(0)
+ val encodings = column.getEncodingStats
+ assert(encodings.hasDictionaryEncodedPages == (dictionary != "off"))
+ assert(encodings.hasNonDictionaryEncodedPages == (dictionary !=
"on"))
+ val ordering: Ordering[Binary] =
+
Ordering.comparatorToOrdering(UNSIGNED_LEXICOGRAPHICAL_BINARY_COMPARATOR)
+ val values = expected.filter(_ !=
null).distinct.map(Binary.fromString)
+ assert(column.getStatistics.genericGetMin == values.min(ordering))
+ assert(column.getStatistics.genericGetMax == values.max(ordering))
+ assert(column.getStatistics.getNumNulls == expected.count(_ == null))
+ } finally {
+ reader.close()
+ }
+ }
+ }
+ }
+
+ test("string slices in nested Parquet values") {
Review Comment:
Removed 👍
##########
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())
+ }
+ case _ => Binary.fromReusedByteArray(value.getBytes)
Review Comment:
Changed 👍
--
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]