Skip to content

Commit 90c3bba

Browse files
committed
refactor: share the variable-width growth step between the append paths
writeVariableWidth, writeDictionaryVariableWidth and the unsafe array path each filled the offsets of values skipped since the last one set and grew the validity and offset buffers. They now call reserveValues, next to reserveData. A new test appends a dictionary-encoded string field as a column after its struct took the row path, which the dictionary path's growth step had no test for. The array of each primitive type no longer repeats the two that nestedTypes lists.
1 parent 0623dba commit 90c3bba

2 files changed

Lines changed: 75 additions & 30 deletions

File tree

‎spark/src/main/scala/org/apache/spark/sql/comet/execution/arrow/ArrowWriters.scala‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,7 @@ private[arrow] object ArrowFieldWriter {
461461
if (numRows == 0) {
462462
return
463463
}
464-
if (vector.getLastSet < outStart - 1) {
465-
vector.fillEmpties(outStart)
466-
}
467-
while (vector.getValueCapacity < outStart + numRows) {
468-
vector.reallocValidityAndOffsetBuffers()
469-
}
464+
reserveValues(vector, outStart, numRows)
470465
val offsets = vector.getOffsetBuffer
471466
val dataStart = vector.getStartOffset(outStart)
472467
val hasNull = input.hasNull
@@ -539,12 +534,7 @@ private[arrow] object ArrowFieldWriter {
539534
if (numRows == 0) {
540535
return
541536
}
542-
if (vector.getLastSet < outStart - 1) {
543-
vector.fillEmpties(outStart)
544-
}
545-
while (vector.getValueCapacity < outStart + numRows) {
546-
vector.reallocValidityAndOffsetBuffers()
547-
}
537+
reserveValues(vector, outStart, numRows)
548538
val offsets = vector.getOffsetBuffer
549539
val ids = input.getDictionaryIds
550540
val hasNull = input.hasNull
@@ -603,6 +593,19 @@ private[arrow] object ArrowFieldWriter {
603593
}
604594
}
605595

596+
/**
597+
* Grows the validity and offset buffers of `vector` to hold values `[outStart, outStart +
598+
* numValues)`, after filling in the offsets of any values skipped since the last one set.
599+
*/
600+
def reserveValues(vector: BaseVariableWidthVector, outStart: Int, numValues: Int): Unit = {
601+
if (vector.getLastSet < outStart - 1) {
602+
vector.fillEmpties(outStart)
603+
}
604+
while (vector.getValueCapacity < outStart + numValues) {
605+
vector.reallocValidityAndOffsetBuffers()
606+
}
607+
}
608+
606609
/** The data buffer of `vector`, grown to hold at least `end` bytes. */
607610
def reserveData(vector: BaseVariableWidthVector, end: Long): ArrowBuf = {
608611
checkDataEnd(end)
@@ -1474,13 +1477,8 @@ private[arrow] abstract class VariableWidthArrowFieldWriter extends ArrowFieldWr
14741477
}
14751478
i += 1
14761479
}
1477-
while (valueVector.getValueCapacity < count + numElements) {
1478-
valueVector.reallocValidityAndOffsetBuffers()
1479-
}
1480+
reserveValues(valueVector, count, numElements)
14801481
reserveData(valueVector, end)
1481-
if (valueVector.getLastSet < count - 1) {
1482-
valueVector.fillEmpties(count)
1483-
}
14841482
val base = array.getBaseObject
14851483
val baseOffset = array.getBaseOffset
14861484
val data = valueVector.getDataBuffer.memoryAddress

‎spark/src/test/scala/org/apache/spark/sql/comet/execution/arrow/CometArrowWriterSuite.scala‎

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,52 @@ class CometArrowWriterSuite extends AnyFunSuite with Matchers {
638638
}
639639
}
640640

641+
test("a dictionary-encoded field appends as a column after its struct took the row path") {
642+
// A struct with a collection field takes the row path in a batch that holds null structs,
643+
// which leaves the offsets of its string field's trailing nulls for the next value to fill. A
644+
// dictionary-encoded field has to fill them, and grow its buffers, before it appends a column.
645+
val st = new StructType().add("a", ArrayType(IntegerType)).add("s", StringType)
646+
val schema = new StructType().add("st", st)
647+
val rnd = new Random(5)
648+
val batches = Seq((0.5, 100), (0.0, 5000), (0.5, 100), (0.0, 5000)).map {
649+
case (nullFraction, n) =>
650+
val v = newVector(n, st, offHeap = false)
651+
(0 until n).foreach { i =>
652+
if (rnd.nextDouble() < nullFraction || (nullFraction > 0 && i == n - 1)) {
653+
v.putNull(i)
654+
}
655+
}
656+
fill(v.getChild(0), ArrayType(IntegerType), n, rnd, nullFraction = 0.0, reversed = false)
657+
fillDictionary(v.getChild(1), StringType, n, rnd, nullFraction = 0.0)
658+
// Spark's Parquet reader nulls the fields of a null struct.
659+
(0 until n).foreach { i =>
660+
if (v.isNullAt(i)) {
661+
v.getChild(0).putNull(i)
662+
v.getChild(1).putNull(i)
663+
}
664+
}
665+
new ColumnarBatch(Array[ColumnVector](v), n)
666+
}
667+
val allocator = new RootAllocator(Long.MaxValue)
668+
val arrowSchema = Utils.toArrowSchema(schema, "UTC")
669+
val columnar = VectorSchemaRoot.create(arrowSchema, allocator)
670+
val rows = VectorSchemaRoot.create(arrowSchema, allocator)
671+
try {
672+
val columnarWriter = ArrowWriter.create(columnar, 1)
673+
batches.foreach(b => columnarWriter.writeColumns(b, 0, b.numRows()))
674+
columnarWriter.finish()
675+
val rowWriter = ArrowWriter.create(rows, 1)
676+
batches.foreach(b => (0 until b.numRows()).foreach(i => rowWriter.write(b.getRow(i))))
677+
rowWriter.finish()
678+
assertSameRoots(rows, columnar)
679+
} finally {
680+
columnar.close()
681+
rows.close()
682+
allocator.close()
683+
batches.foreach(_.close())
684+
}
685+
}
686+
641687
test("a struct that switches between the columnar and row paths across appended batches") {
642688
// A struct with an array or a map field takes the row path only in batches that hold null
643689
// structs, so its fields' writers keep appending where the other path left off.
@@ -688,18 +734,19 @@ class CometArrowWriterSuite extends AnyFunSuite with Matchers {
688734
}
689735
}
690736

691-
// Nested shapes whose unsafe forms take paths of their own: an array of each primitive type,
692-
// whose elements are either copied in one block or converted one at a time, collections inside
693-
// collections, and a struct wider than one word of null bits.
694-
private val moreNestedTypes: Seq[DataType] = primitiveTypes.map(ArrayType(_)) ++ Seq(
695-
ArrayType(MapType(StringType, IntegerType)),
696-
MapType(StringType, ArrayType(StringType)),
697-
MapType(LongType, DecimalType(9, 2)),
698-
new StructType()
699-
.add("m", MapType(IntegerType, StringType))
700-
.add("s", new StructType().add("x", BinaryType).add("y", DecimalType(18, 4))),
701-
StructType(
702-
(0 until 70).map(i => StructField(s"f$i", if (i % 7 == 3) StringType else LongType))))
737+
// Nested shapes whose unsafe forms take paths of their own: an array of each primitive type not
738+
// in `nestedTypes`, whose elements are either copied in one block or converted one at a time,
739+
// collections inside collections, and a struct wider than one word of null bits.
740+
private val moreNestedTypes: Seq[DataType] =
741+
primitiveTypes.map(ArrayType(_)).filterNot(nestedTypes.contains) ++ Seq(
742+
ArrayType(MapType(StringType, IntegerType)),
743+
MapType(StringType, ArrayType(StringType)),
744+
MapType(LongType, DecimalType(9, 2)),
745+
new StructType()
746+
.add("m", MapType(IntegerType, StringType))
747+
.add("s", new StructType().add("x", BinaryType).add("y", DecimalType(18, 4))),
748+
StructType(
749+
(0 until 70).map(i => StructField(s"f$i", if (i % 7 == 3) StringType else LongType))))
703750

704751
/**
705752
* Writes `numRows` rows, `row(i)` through the generic row path and its unsafe projection

0 commit comments

Comments
 (0)