@@ -21,12 +21,15 @@ package org.apache.spark.sql.comet.util
2121
2222import java .nio .charset .StandardCharsets
2323
24+ import scala .jdk .CollectionConverters ._
25+
2426import org .apache .arrow .c .CDataDictionaryProvider
25- import org .apache .arrow .vector .{BaseLargeVariableWidthVector , LargeVarBinaryVector , LargeVarCharVector , ValueVector , VarBinaryVector , VarCharVector , VariableWidthFieldVector }
27+ import org .apache .arrow .vector .{BaseLargeVariableWidthVector , BitVectorHelper , LargeVarBinaryVector , LargeVarCharVector , ValueVector , VarBinaryVector , VarCharVector , VariableWidthFieldVector }
28+ import org .apache .arrow .vector .ipc .message .ArrowFieldNode
2629import org .apache .spark .SparkException
2730import org .apache .spark .sql .CometTestBase
2831import org .apache .spark .sql .execution .vectorized .ConstantColumnVector
29- import org .apache .spark .sql .types .{IntegerType , StringType , StructField , StructType , TimestampType }
32+ import org .apache .spark .sql .types .{BinaryType , IntegerType , StringType , StructField , StructType , TimestampType }
3033import org .apache .spark .sql .vectorized .{ColumnarBatch , ColumnVector }
3134
3235import org .apache .comet .CometArrowAllocator
@@ -174,26 +177,12 @@ class UtilsSuite extends CometTestBase {
174177 // getBatchFieldVectors writes a 32-bit copy of them rather than the vector itself. A caller
175178 // taking the direct write path expects the batch's own vectors, so isArrowBacked must report
176179 // false and let the caller convert the batch instead.
177- val numRows = 2
178- Seq [org.apache.arrow.vector.FieldVector ](
179- {
180- val v = new org.apache.arrow.vector.LargeVarCharVector (" s" , CometArrowAllocator )
181- v.allocateNew(numRows)
182- v.setSafe(0 , " hello" .getBytes(" UTF-8" ))
183- v.setSafe(1 , " world" .getBytes(" UTF-8" ))
184- v.setValueCount(numRows)
185- v
186- }, {
187- val v = new org.apache.arrow.vector.LargeVarBinaryVector (" b" , CometArrowAllocator )
188- v.allocateNew(numRows)
189- v.setSafe(0 , " hello" .getBytes(" UTF-8" ))
190- v.setSafe(1 , " world" .getBytes(" UTF-8" ))
191- v.setValueCount(numRows)
192- v
193- }).foreach { vector =>
180+ val values = Seq (" hello" , " world" )
181+ Seq [ValueVector ](
182+ fill(new LargeVarCharVector (" s" , CometArrowAllocator ), values),
183+ fill(new LargeVarBinaryVector (" b" , CometArrowAllocator ), values)).foreach { vector =>
194184 try {
195- val col = CometVector .getVector(vector, new CDataDictionaryProvider )
196- val batch = new ColumnarBatch (Array [ColumnVector ](col), numRows)
185+ val batch = new ColumnarBatch (Array (cometVector(vector)), values.length)
197186 assert(
198187 ! Utils .isArrowBacked(batch),
199188 s " ${vector.getClass.getSimpleName} must not be reported as directly writable " )
@@ -208,30 +197,56 @@ class UtilsSuite extends CometTestBase {
208197 // LargeVarCharVector or LargeVarBinaryVector. The column is still StringType or BinaryType,
209198 // so it is written as the Utf8 or Binary vector every reader of the bytes expects for those.
210199 val values = Seq (" hello" , null , " " , " wörld" )
211- val numRows = values.length
212200 val strings = fill(new LargeVarCharVector (" s" , CometArrowAllocator ), values)
213201 val binaries = fill(new LargeVarBinaryVector (" b" , CometArrowAllocator ), values)
214202 try {
215203 val batch =
216- new ColumnarBatch (Array (cometVector(strings), cometVector(binaries)), numRows)
217-
218- val (rowCount, buf) = Utils .serializeBatches(Iterator (batch)).next()
219- assert(rowCount == numRows)
204+ new ColumnarBatch (Array (cometVector(strings), cometVector(binaries)), values.length)
205+ assert(
206+ roundTrip(batch) == Seq (
207+ classOf [VarCharVector ] -> values,
208+ classOf [VarBinaryVector ] -> values))
209+ } finally {
210+ strings.close()
211+ binaries.close()
212+ }
213+ }
220214
221- val it = Utils .decodeBatches(buf, " test" )
222- assert(it.hasNext)
223- val out = it.next()
224- val vectorClasses = (0 until out.numCols()).map(i =>
225- out.column(i).asInstanceOf [CometVector ].getValueVector.getClass)
226- val gotStrings =
227- (0 until numRows).map(i => Option (out.column(0 ).getUTF8String(i)).map(_.toString).orNull)
228- val gotBinaries = (0 until numRows).map(i =>
229- Option (out.column(1 ).getBinary(i)).map(new String (_, StandardCharsets .UTF_8 )).orNull)
230- assert(! it.hasNext)
215+ test(" serializeBatches rebases large-offset vectors whose offsets start past zero" ) {
216+ // Offsets need not start at zero. A slice imported over the C Data interface keeps its
217+ // parent's offsets and data buffer, so they start past bytes that no value covers. The copy
218+ // holds only the bytes the values cover, with offsets counted from the first of them.
219+ val values = Seq (" hello" , null , " wörld" )
220+ val strings = startingPast(" xx" , new LargeVarCharVector (" s" , CometArrowAllocator ), values)
221+ val binaries = startingPast(" xx" , new LargeVarBinaryVector (" b" , CometArrowAllocator ), values)
222+ try {
223+ assert(strings.getOffsetBuffer.getLong(0 ) == 2 )
224+ assert(binaries.getOffsetBuffer.getLong(0 ) == 2 )
225+ val batch =
226+ new ColumnarBatch (Array (cometVector(strings), cometVector(binaries)), values.length)
227+ assert(
228+ roundTrip(batch) == Seq (
229+ classOf [VarCharVector ] -> values,
230+ classOf [VarBinaryVector ] -> values))
231+ } finally {
232+ strings.close()
233+ binaries.close()
234+ }
235+ }
231236
232- assert(vectorClasses == Seq (classOf [VarCharVector ], classOf [VarBinaryVector ]))
233- assert(gotStrings == values)
234- assert(gotBinaries == values)
237+ test(" serializeBatches writes empty large-offset vectors with 32-bit offsets" ) {
238+ // A vector that holds no values need not have an offset buffer, so none is read.
239+ val strings = new LargeVarCharVector (" s" , CometArrowAllocator )
240+ val binaries = new LargeVarBinaryVector (" b" , CometArrowAllocator )
241+ try {
242+ strings.setValueCount(0 )
243+ binaries.setValueCount(0 )
244+ assert(strings.getOffsetBuffer.capacity == 0 )
245+ val batch = new ColumnarBatch (Array (cometVector(strings), cometVector(binaries)), 0 )
246+ assert(
247+ roundTrip(batch) == Seq (
248+ classOf [VarCharVector ] -> Seq .empty,
249+ classOf [VarBinaryVector ] -> Seq .empty))
235250 } finally {
236251 strings.close()
237252 binaries.close()
@@ -254,8 +269,7 @@ class UtilsSuite extends CometTestBase {
254269
255270 val it = coalesced.iterator.flatMap(Utils .decodeBatches(_, " test" ))
256271 val out = it.next()
257- val got = (0 until out.numRows()).map(i =>
258- Option (out.column(0 ).getUTF8String(i)).map(_.toString).orNull)
272+ val got = stringValues(out.column(0 ), out.numRows())
259273 assert(! it.hasNext)
260274 assert(got == Seq (" a" , null , " b" , " c" ))
261275 } finally {
@@ -267,11 +281,8 @@ class UtilsSuite extends CometTestBase {
267281 test(" serializeBatches refuses a large-offset column too big for 32-bit offsets" ) {
268282 // The offsets alone claim 2 GiB of data. The size is checked before anything is copied, so
269283 // the data itself never has to exist.
270- val strings = fill( new LargeVarCharVector ( " s" , CometArrowAllocator ), Seq ( " a " ) )
284+ val strings = tooBigForIntOffsets( " s" )
271285 try {
272- strings.getOffsetBuffer.setLong(
273- BaseLargeVariableWidthVector .OFFSET_WIDTH ,
274- Int .MaxValue + 1L )
275286 val batch = new ColumnarBatch (Array (cometVector(strings)), 1 )
276287 val e = intercept[SparkException ](Utils .serializeBatches(Iterator (batch)).next())
277288 assert(e.getMessage.contains(" more than 32-bit offsets can address" ), e.getMessage)
@@ -280,6 +291,28 @@ class UtilsSuite extends CometTestBase {
280291 }
281292 }
282293
294+ test(" serializeBatches releases the vectors it made when a later column fails" ) {
295+ // A materialized constant and a 32-bit copy of a large-offset column are both made before the
296+ // last column is refused. Nothing outside serializeBatches ever holds them, so it has to
297+ // close them itself, while the batch's own vectors stay with their owner.
298+ val before = CometArrowAllocator .getAllocatedMemory
299+ val constant = new ConstantColumnVector (1 , IntegerType )
300+ constant.setInt(42 )
301+ val small = fill(new LargeVarCharVector (" small" , CometArrowAllocator ), Seq (" a" ))
302+ val tooBig = tooBigForIntOffsets(" big" )
303+ try {
304+ val batch = new ColumnarBatch (Array (constant, cometVector(small), cometVector(tooBig)), 1 )
305+ val e = intercept[SparkException ](Utils .serializeBatches(Iterator (batch)).next())
306+ assert(e.getMessage.contains(" more than 32-bit offsets can address" ), e.getMessage)
307+ assert(small.getValueCount == 1 && small.getObject(0 ).toString == " a" )
308+ } finally {
309+ constant.close()
310+ small.close()
311+ tooBig.close()
312+ }
313+ assert(CometArrowAllocator .getAllocatedMemory == before)
314+ }
315+
283316 /** Writes `values` into `vector`, leaving a null entry unset so that it reads back as null. */
284317 private def fill [V <: VariableWidthFieldVector ](vector : V , values : Seq [String ]): V = {
285318 vector.allocateNew(values.length)
@@ -290,6 +323,81 @@ class UtilsSuite extends CometTestBase {
290323 vector
291324 }
292325
326+ /**
327+ * Loads `values` into `vector` the way an imported slice holds them: the data buffer starts
328+ * with the bytes of `skipped`, which no value covers, so the offsets start past zero.
329+ */
330+ private def startingPast [V <: BaseLargeVariableWidthVector ](
331+ skipped : String ,
332+ vector : V ,
333+ values : Seq [String ]): V = {
334+ val skippedBytes = skipped.getBytes(StandardCharsets .UTF_8 )
335+ val valueBytes =
336+ values.map(v => if (v == null ) Array .emptyByteArray else v.getBytes(StandardCharsets .UTF_8 ))
337+ val offsets = valueBytes.scanLeft(skippedBytes.length.toLong)(_ + _.length)
338+ val width = BaseLargeVariableWidthVector .OFFSET_WIDTH .toLong
339+
340+ val validity =
341+ CometArrowAllocator .buffer(BitVectorHelper .getValidityBufferSize(values.length).toLong)
342+ val offsetBuffer = CometArrowAllocator .buffer(offsets.length * width)
343+ val data = CometArrowAllocator .buffer(offsets.last)
344+ val buffers = Seq (validity, offsetBuffer, data)
345+ try {
346+ validity.setZero(0 , validity.capacity)
347+ values.indices.filter(values(_) != null ).foreach { i =>
348+ BitVectorHelper .setBit(validity, i.toLong)
349+ }
350+ offsets.zipWithIndex.foreach { case (offset, i) => offsetBuffer.setLong(i * width, offset) }
351+ data.setBytes(0 , skippedBytes ++ valueBytes.flatten)
352+ vector.loadFieldBuffers(
353+ new ArrowFieldNode (values.length.toLong, values.count(_ == null ).toLong),
354+ buffers.asJava)
355+ vector
356+ } finally {
357+ // The vector takes references of its own.
358+ buffers.foreach(_.close())
359+ }
360+ }
361+
362+ /** A one-value vector whose offsets claim more data than 32-bit offsets can address. */
363+ private def tooBigForIntOffsets (name : String ): LargeVarCharVector = {
364+ val vector = fill(new LargeVarCharVector (name, CometArrowAllocator ), Seq (" a" ))
365+ vector.getOffsetBuffer.setLong(BaseLargeVariableWidthVector .OFFSET_WIDTH , Int .MaxValue + 1L )
366+ vector
367+ }
368+
293369 private def cometVector (vector : ValueVector ): ColumnVector =
294370 CometVector .getVector(vector, new CDataDictionaryProvider )
371+
372+ /**
373+ * Serializes `batch` on its own and decodes it as the broadcast consumers do, returning each
374+ * column's vector class and values.
375+ */
376+ private def roundTrip (batch : ColumnarBatch ): Seq [(Class [_], Seq [String ])] = {
377+ val (rowCount, buf) = Utils .serializeBatches(Iterator (batch)).next()
378+ assert(rowCount == batch.numRows())
379+
380+ // Everything is read before the iterator moves on, which releases the batch it returned.
381+ val it = Utils .decodeBatches(buf, " test" )
382+ val out = it.next()
383+ val columns = (0 until out.numCols()).map { i =>
384+ val column = out.column(i)
385+ val vectorClass = column.asInstanceOf [CometVector ].getValueVector.getClass
386+ (vectorClass, stringValues(column, out.numRows()))
387+ }
388+ assert(! it.hasNext)
389+ columns
390+ }
391+
392+ /** The values of a string or binary column, with binary values read as UTF-8. */
393+ private def stringValues (column : ColumnVector , numRows : Int ): Seq [String ] =
394+ (0 until numRows).map { i =>
395+ if (column.isNullAt(i)) {
396+ null
397+ } else if (column.dataType == BinaryType ) {
398+ new String (column.getBinary(i), StandardCharsets .UTF_8 )
399+ } else {
400+ column.getUTF8String(i).toString
401+ }
402+ }
295403}
0 commit comments