Skip to content

Commit 1c8f68c

Browse files
committed
fix: release the vectors a failed batch assembly made, and test offsets past zero
getBatchFieldVectorsWithProviders keeps the vectors it makes, the 32-bit copies of large-offset columns and the materialized ConstantColumnVectors, and closes them if a later column fails. Only the root serializeBatches builds afterwards would have released them, so a failure left them allocated from CometArrowAllocator. The batch's own vectors stay with their owner. narrowOffsets passes the validity length as a Long, which -Pstrict-warnings requires, explains why the validity bitmap is copied from bit 0, and points at #6933 for large-offset children of nested columns. UtilsSuite covers offsets that start past zero, empty large-offset vectors with no offset buffer, and the cleanup after a later column fails, and builds its vectors with fill throughout.
1 parent 7f6774b commit 1c8f68c

2 files changed

Lines changed: 215 additions & 84 deletions

File tree

‎spark/src/main/scala/org/apache/spark/sql/comet/util/Utils.scala‎

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -533,45 +533,62 @@ object Utils extends CometTypeShim with Logging {
533533
def getBatchFieldVectorsWithProviders(
534534
batch: ColumnarBatch): Seq[(FieldVector, Option[DictionaryProvider])] = {
535535
val rows = batch.numRows()
536-
(0 until batch.numCols()).map { index =>
537-
batch.column(index) match {
538-
case a: CometVector =>
539-
val valueVector = a.getValueVector
540-
val provider =
541-
if (valueVector.getField.getDictionary != null) Some(a.getDictionaryProvider)
542-
else None
543-
544-
val fieldVector = valueVector match {
545-
case v: LargeVarCharVector => narrowOffsets(v, ArrowType.Utf8.INSTANCE)
546-
case v: LargeVarBinaryVector => narrowOffsets(v, ArrowType.Binary.INSTANCE)
547-
case v => getFieldVector(v, "serialize")
548-
}
549-
(fieldVector, provider)
550-
551-
case cv: ConstantColumnVector =>
552-
// Spark wraps file-source partition columns and other per-batch constants in
553-
// `ConstantColumnVector`. Materialise to an Arrow vector so the serialisation path
554-
// doesn't reject the batch. "UTC" is intentional -- see `ConstantColumnVectors`.
555-
val materialized = ConstantColumnVectors.materialize(
556-
cv,
557-
cv.dataType(),
558-
rows,
559-
s"_const_$index",
560-
org.apache.comet.CometArrowAllocator,
561-
"UTC")
562-
(materialized, None)
563-
564-
case c =>
565-
throw new SparkException(
566-
s"Comet execution only takes Arrow Arrays, but got ${c.getClass}. " +
567-
"This typically happens when a Comet scan falls back to Spark due to unsupported " +
568-
"data types (e.g., complex types like structs, arrays, or maps). " +
569-
"To resolve this, you can: " +
570-
"(1) enable spark.comet.scan.allowIncompatible=true to use a compatible native " +
571-
"scan variant, or " +
572-
"(2) enable spark.comet.convert.parquet.enabled=true to convert Spark Parquet " +
573-
"data to Arrow format automatically.")
536+
// Vectors made here rather than taken from `batch`. The caller releases them with the rest of
537+
// what it writes, but if a later column fails it never gets them, so they are closed here.
538+
val made = scala.collection.mutable.ArrayBuffer.empty[FieldVector]
539+
def keep(vector: FieldVector): FieldVector = {
540+
made += vector
541+
vector
542+
}
543+
544+
try {
545+
(0 until batch.numCols()).map { index =>
546+
batch.column(index) match {
547+
case a: CometVector =>
548+
val valueVector = a.getValueVector
549+
val provider =
550+
if (valueVector.getField.getDictionary != null) Some(a.getDictionaryProvider)
551+
else None
552+
553+
val fieldVector = valueVector match {
554+
case v: LargeVarCharVector => keep(narrowOffsets(v, ArrowType.Utf8.INSTANCE))
555+
case v: LargeVarBinaryVector => keep(narrowOffsets(v, ArrowType.Binary.INSTANCE))
556+
case v => getFieldVector(v, "serialize")
557+
}
558+
(fieldVector, provider)
559+
560+
case cv: ConstantColumnVector =>
561+
// Spark wraps file-source partition columns and other per-batch constants in
562+
// `ConstantColumnVector`. Materialise to an Arrow vector so the serialisation path
563+
// doesn't reject the batch. "UTC" is intentional -- see `ConstantColumnVectors`.
564+
val materialized = keep(
565+
ConstantColumnVectors.materialize(
566+
cv,
567+
cv.dataType(),
568+
rows,
569+
s"_const_$index",
570+
org.apache.comet.CometArrowAllocator,
571+
"UTC"))
572+
(materialized, None)
573+
574+
case c =>
575+
throw new SparkException(
576+
s"Comet execution only takes Arrow Arrays, but got ${c.getClass}. " +
577+
"This typically happens when a Comet scan falls back to Spark due to " +
578+
"unsupported data types (e.g., complex types like structs, arrays, or maps). " +
579+
"To resolve this, you can: " +
580+
"(1) enable spark.comet.scan.allowIncompatible=true to use a compatible native " +
581+
"scan variant, or " +
582+
"(2) enable spark.comet.convert.parquet.enabled=true to convert Spark Parquet " +
583+
"data to Arrow format automatically.")
584+
}
574585
}
586+
} catch {
587+
case NonFatal(e) =>
588+
made.foreach(v =>
589+
try v.close()
590+
catch { case NonFatal(closeError) => e.addSuppressed(closeError) })
591+
throw e
575592
}
576593
}
577594

@@ -587,6 +604,10 @@ object Utils extends CometTypeShim with Logging {
587604
* stream's schema from the first one too. Only a column holding more data than 32-bit offsets
588605
* can address is refused.
589606
*
607+
* Only top-level columns are narrowed. A `large_string` or `large_binary` child of a struct,
608+
* list or map column passes [[getFieldVector]] with its parent and keeps its 64-bit offsets
609+
* (#6933).
610+
*
590611
* Like a materialized `ConstantColumnVector`, the copy is released when the caller clears the
591612
* vectors it wrote. `large` itself stays with its owner.
592613
*/
@@ -616,7 +637,9 @@ object Utils extends CometTypeShim with Logging {
616637
try {
617638
narrow.allocateNew(dataLength, numValues)
618639
if (numValues > 0) {
619-
val validityBytes = BitVectorHelper.getValidityBufferSize(numValues)
640+
// An Arrow Java vector has no array offset, so validity bit i belongs to value i even when
641+
// the offsets start past zero, and the bitmap is copied from bit 0.
642+
val validityBytes = BitVectorHelper.getValidityBufferSize(numValues).toLong
620643
narrow.getValidityBuffer.setBytes(0, large.getValidityBuffer, 0, validityBytes)
621644
val offsets = narrow.getOffsetBuffer
622645
(0 to numValues).foreach { i =>

‎spark/src/test/scala/org/apache/spark/sql/comet/util/UtilsSuite.scala‎

Lines changed: 153 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ package org.apache.spark.sql.comet.util
2121

2222
import java.nio.charset.StandardCharsets
2323

24+
import scala.jdk.CollectionConverters._
25+
2426
import 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
2629
import org.apache.spark.SparkException
2730
import org.apache.spark.sql.CometTestBase
2831
import 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}
3033
import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector}
3134

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

Comments
 (0)