This is an automated email from the ASF dual-hosted git repository.

dongjoon-hyun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new dc1e8a7ac606 [SPARK-60325][SQL][FOLLOWUP] Do not grow reused NullType 
column vectors
dc1e8a7ac606 is described below

commit dc1e8a7ac606c8f2985477320c6505ce5db20f34
Author: Liang-Chi Hsieh <[email protected]>
AuthorDate: Wed Nov 11 08:23:10 2026 -0800

    [SPARK-60325][SQL][FOLLOWUP] Do not grow reused NullType column vectors
    
    ### What changes were proposed in this pull request?
    
    This is a follow-up of #59582, addressing the two non-blocking items in 
https://github.com/apache/spark/pull/59582#pullrequestreview-5853478685.
    
    1. Reused `NullType` column vectors no longer grow across batches.
       - `RowToColumnConverter` uses the no-op `NullConverter` directly for a 
nullable `NullType` field, instead of wrapping it in 
`BasicNullableTypeConverter`, which called `appendNull()` for every row.
       - `WritableColumnVector.reset()` now clears `elementsAppended` of an 
all-null vector (`NullType` or missing column) before returning early. A null 
struct still appends a null to each of its children (`appendStruct(true)`), so 
a `NullType` field of a nullable struct grew the same way, which the converter 
change alone does not cover.
    2. With `emptyBatches`, `ArrowBackedPartitionReader` in the test data 
source always returns `ArrowBackedDataSourceV2.NUM_EMPTY_BATCHES` (3) batches 
without rows. The positions are now kept in a queue instead of a set, so they 
are not deduplicated when they coincide (fewer than two batches). 
`ArrowColumnarPythonUDFSuite` uses the constant instead of a literal `3`.
    
    ### Why are the changes needed?
    
    `WritableColumnVector.reset()` returns early for an all-null vector (since 
SPARK-54220), so `elementsAppended` is never cleared for a `NullType` vector. 
When the vectors are reused across batches, by `rowsToColumnarBatches` in 
`ColumnarArrowEvalPythonEvaluatorFactory` and by `RowToColumnarExec`, every row 
with a null `NullType` value, or a null struct with a `NullType` field, grows 
it. Nothing is allocated for the vector, but after about 2^31 rows in one task 
`reserve` fails with "Can [...]
    
    The test data source fix keeps the number of empty batches independent of 
the reader's parameters, so the test does not silently break if its parameters 
change.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No. `NullType` support in `RowToColumnConverter` was added by #59582, which 
is not released yet.
    
    ### How was this patch tested?
    
    New test in `RowToColumnConverterSuite`: converts rows with a nullable 
`NullType` column and a nullable struct with a `NullType` field into reused 
vectors over several batches with `reset()` in between. It fails without either 
change (`10 did not equal 0` for the top-level column without the converter 
change, `15 was not less than or equal to 10` for the struct child without the 
`reset()` change).
    
    Ran `RowToColumnConverterSuite`, `ArrowColumnarPythonUDFSuite`, 
`ArrowCachedPythonUDFSuite`, `ColumnarBatchSuite`, and, for the `reset()` 
change, `ParquetIOSuite`, `ParquetSchemaSuite` and `FileBasedDataSourceSuite`.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Claude Opus 5.5)
    
    Closes #60324 from viirya/SPARK-60325-followup.
    
    Authored-by: Liang-Chi Hsieh <[email protected]>
    Signed-off-by: Dongjoon Hyun <[email protected]>
    
    
    skip-checks: true
---
 .../execution/vectorized/WritableColumnVector.java |  8 ++++++-
 .../org/apache/spark/sql/execution/Columnar.scala  |  2 ++
 .../sql/execution/RowToColumnConverterSuite.scala  | 25 ++++++++++++++++++++++
 .../execution/python/ArrowBackedDataSourceV2.scala | 23 +++++++++++++-------
 .../python/ArrowColumnarPythonUDFSuite.scala       |  4 ++--
 5 files changed, 51 insertions(+), 11 deletions(-)

diff --git 
a/sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java
 
b/sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java
index dd1d67d3348e..49442b26b8ed 100644
--- 
a/sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java
+++ 
b/sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java
@@ -80,7 +80,13 @@ public abstract class WritableColumnVector extends 
ColumnVector {
    * Resets this column for writing. The currently stored values are no longer 
accessible.
    */
   public void reset() {
-    if (isConstant || isAllNull()) return;
+    if (isConstant) return;
+    if (isAllNull()) {
+      // An all-null vector stores nothing, but appending nulls to it (e.g. 
through a null
+      // parent struct) still advances elementsAppended, so it must be cleared 
here.
+      elementsAppended = 0;
+      return;
+    }
 
     if (childColumns != null) {
       for (WritableColumnVector c: childColumns) {
diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/Columnar.scala 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/Columnar.scala
index 346c8e24076f..ab14e156bdfc 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/execution/Columnar.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/Columnar.scala
@@ -321,6 +321,8 @@ private object RowToColumnConverter {
       dataType match {
         // The converter of its SQL type handles the nulls.
         case _: UserDefinedType[_] => core
+        // A NullType vector is all null by its type, so appending nulls to it 
is not needed.
+        case NullType => core
         case CalendarIntervalType | VariantType => new 
StructNullableTypeConverter(core)
         case _: AnyTimestampNanoType =>
           new StructNullableTypeConverter(core)
diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/RowToColumnConverterSuite.scala
 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/RowToColumnConverterSuite.scala
index 711aa4663e6d..e34023fa7418 100644
--- 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/RowToColumnConverterSuite.scala
+++ 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/RowToColumnConverterSuite.scala
@@ -245,6 +245,31 @@ class RowToColumnConverterSuite extends SparkFunSuite {
     }
   }
 
+  test("SPARK-60325: reused null type vectors do not grow across batches") {
+    val batchSize = 10
+    val schema = StructType(Seq(
+      StructField("n", NullType),
+      StructField("s", new StructType().add("n", NullType))))
+    val converter = new RowToColumnConverter(schema)
+    val vectors =
+      schema.map(f => new OnHeapColumnVector(5, 
f.dataType)).toArray[WritableColumnVector]
+    (0 until 5).foreach { _ =>
+      vectors.foreach(_.reset())
+      (0 until batchSize).foreach { i =>
+        converter.convert(InternalRow(null, if (i % 2 == 0) null else 
InternalRow(null)), vectors)
+      }
+      // Nulls of a top-level NullType column are not appended at all.
+      assert(vectors(0).getElementsAppended === 0)
+      // A null struct appends a null to its NullType child, which reset() 
must clear.
+      assert(vectors(1).getChild(0).getElementsAppended <= batchSize)
+      (0 until batchSize).foreach { i =>
+        assert(vectors(0).isNullAt(i))
+        assert(vectors(1).isNullAt(i) === (i % 2 == 0))
+        if (i % 2 == 1) assert(vectors(1).getStruct(i).isNullAt(0))
+      }
+    }
+  }
+
   test("SPARK-60325: user-defined type columns hold their SQL type") {
     // A null of a struct-backed type must be appended as a struct.
     val arrayBacked = new ExamplePointUDT
diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/python/ArrowBackedDataSourceV2.scala
 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/python/ArrowBackedDataSourceV2.scala
index 6afb78eac88f..e21038d695d3 100644
--- 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/python/ArrowBackedDataSourceV2.scala
+++ 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/python/ArrowBackedDataSourceV2.scala
@@ -81,6 +81,8 @@ object ArrowBackedDataSourceV2 {
   val DEFAULT_NUM_ROWS: Int = 10000
   val DEFAULT_NUM_PARTITIONS: Int = 2
   val BATCH_SIZE: Int = 1024
+  // The number of batches without rows that a partition reader returns with 
`emptyBatches`.
+  val NUM_EMPTY_BATCHES: Int = 3
 
   // The batches that ArrowBackedPartitionReaders returned, and the ones among 
them that their
   // consumer released with closeIfFreeable.
@@ -167,8 +169,9 @@ private case class ArrowPartition(start: Int, end: Int)
  *   the columnar optimization.
  * @param arrowIdColumn if false, the `id` column of the batches is not an
  *   [[ArrowColumnVector]], so that a batch is not entirely Arrow-backed.
- * @param emptyBatches if true, the reader with fresh vectors also returns 
batches without rows,
- *   first, in the middle and last.
+ * @param emptyBatches if true, the reader with fresh vectors also returns
+ *   [[ArrowBackedDataSourceV2.NUM_EMPTY_BATCHES]] batches without rows, 
first, in the middle and
+ *   last.
  */
 private class ArrowBackedReaderFactory(
     columnar: Boolean,
@@ -272,8 +275,10 @@ private class ReusingArrowPartitionReader(start: Int, end: 
Int, batchSize: Int)
  * [[ArrowBackedDataSourceV2]].
  *
  * If `arrowIdColumn` is false, the `id` column is a [[ColumnVector]] over its 
Arrow vector that is
- * not an [[ArrowColumnVector]]. If `emptyBatches` is true, the reader also 
returns batches without
- * rows, before the first batch, after half of the batches and after the last 
one.
+ * not an [[ArrowColumnVector]]. If `emptyBatches` is true, the reader also 
returns
+ * [[ArrowBackedDataSourceV2.NUM_EMPTY_BATCHES]] batches without rows: before 
the first batch,
+ * after half of the batches and after the last one. Some of them are adjacent 
if there are fewer
+ * than two batches.
  */
 private class ArrowBackedPartitionReader(
     start: Int,
@@ -284,11 +289,12 @@ private class ArrowBackedPartitionReader(
   extends PartitionReader[ColumnarBatch] {
 
   private var current = start
-  // The positions before which the reader returns a batch without rows.
+  // The positions before which the reader returns a batch without rows, in 
order. A position can
+  // occur more than once, so that the number of the batches does not depend 
on the parameters.
   private val emptyBatchPositions = if (emptyBatches) {
-    scala.collection.mutable.Set(start, start + (end - start) / batchSize / 2 
* batchSize, end)
+    scala.collection.mutable.Queue(start, start + (end - start) / batchSize / 
2 * batchSize, end)
   } else {
-    scala.collection.mutable.Set.empty[Int]
+    scala.collection.mutable.Queue.empty[Int]
   }
   private var currentBatch: ColumnarBatch = _
 
@@ -357,7 +363,8 @@ private class ArrowBackedPartitionReader(
   }
 
   override def next(): Boolean = {
-    val count = if (emptyBatchPositions.remove(current)) {
+    val count = if (emptyBatchPositions.headOption.contains(current)) {
+      emptyBatchPositions.dequeue()
       0
     } else if (current >= end) {
       return false
diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/python/ArrowColumnarPythonUDFSuite.scala
 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/python/ArrowColumnarPythonUDFSuite.scala
index 36a36e0c3a5d..997cfb45a156 100644
--- 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/python/ArrowColumnarPythonUDFSuite.scala
+++ 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/python/ArrowColumnarPythonUDFSuite.scala
@@ -259,14 +259,14 @@ class ArrowColumnarPythonUDFSuite extends 
SharedSparkSession {
 
       val numRows = 2000
       val numBatches = 20
-      // Returns the data sent to Python for the batches of the reader, with 
and without the three
+      // Returns the data sent to Python for the batches of the reader, with 
and without the
       // batches without rows that it can also return, first, in the middle 
and last.
       def dataSent(input: String, emptyBatches: Boolean): Long = {
         val result = readArrowSource(numRows = numRows, batchSize = numRows / 
numBatches,
             emptyBatches = emptyBatches)
           .selectExpr("id", "name", "value", "data", s"arrow_empty_udf($input) 
as udf_name")
         val arrowExec = 
singleArrowEvalPythonExec(result.queryExecution.executedPlan)
-        val numEmpty = if (emptyBatches) 3 else 0
+        val numEmpty = if (emptyBatches) 
ArrowBackedDataSourceV2.NUM_EMPTY_BATCHES else 0
 
         ArrowBackedDataSourceV2.resetBatchCounters()
         val rows = result.collect()


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to