spark-test commented on code in PR #60234:
URL: https://github.com/apache/spark/pull/60234#discussion_r4513205205


##########
sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java:
##########
@@ -1105,6 +1130,12 @@ protected WritableColumnVector(int capacity, DataType 
dataType) {
       }
       this.childColumns = new WritableColumnVector[1];
       this.childColumns[0] = reserveNewInternalColumn(childCapacity, 
childType);
+      if (!(type instanceof ArrayType)) {
+        // The child holds the bytes of this vector's own values, which are 
never null by
+        // themselves: a null marks a whole value, on this vector. The 
per-byte null bits the
+        // child allocated would double the memory a long value column takes 
for no use.
+        this.childColumns[0].stopTrackingNulls();

Review Comment:
   **Nit (P3):** This call removes the byte-data child's null bytes, which 
makes the late-materialization Parquet reader's charge documentation false. 
`LateMaterializationParquetRecordReader` is not touched by this PR, and it 
still derives its variable-length charge from one null byte per data byte:
   
   - The `VARIABLE_LENGTH_BYTES_FACTOR` Javadoc (lines 110-117) says "The byte 
child holds a null byte per byte". It also says a `hugeVectorReserveRatio` 
above 2 under-counts; with a data-only child that happens only above 4.
   - The variable-length fixed part `1 + 4 + 4 + 2 * 
DEFAULT_CHILD_BYTES_PER_ROW` (lines 487-493) is commented "with a null byte 
each", which gives 17 bytes per row where the child now takes 13.
   
   The constants are unchanged, so charges and splice decisions are the same as 
on the base branch and nothing regresses. But a maintainer reading that reader 
is told the wrong layout, and the charge is now about twice the footprint its 
comments say it derives, so the memory this change saves never reaches that 
reader's budget.
   
   **Verification:**
   - **Inspection:** The late-materialization charge comments describe a 
byte-data child that holds only data bytes, at most twice a value's bytes after 
growth. They state the reserve ratio above which the factor would under-count 
under that layout, and they explain why the factor of 4 and the per-row child 
term are kept as a margin.
   - **Regression:** The existing exact-charge late-materialization 
storage-filter tests pass unchanged, showing that charges and splice decisions 
are the same as on the base branch.



##########
sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/OffHeapColumnVector.java:
##########
@@ -674,7 +677,10 @@ public int putByteArray(int rowId, byte[] value, int 
offset, int length) {
   protected void reserveInternal(int newCapacity) {
     if (isAllNull()) return; // Skip allocation for all-null vector.
 
-    int oldCapacity = (nulls == 0L) ? 0 : capacity;
+    // A vector that holds no null bits frees them, so they can not mark the 
memory it did
+    // allocate; the data itself has to. Only a leaf byte-data child ever 
stops tracking nulls
+    // (see stopTrackingNulls's call site), so it always holds its own data 
and never children.
+    int oldCapacity = (tracksNulls ? nulls != 0L : data != 0L) ? capacity : 0;

Review Comment:
   **Non-blocking (P2):** The `data == 0` arm here is reached only after 
`reset()` releases the byte-data child. That happens when 
`spark.sql.inMemoryColumnarStorage.hugeVectorThreshold` is set and the child 
grew past it: `reset()` calls `releaseMemory()`, which zeroes `data`, and then 
re-reserves the default capacity. Construction runs while `tracksNulls` is 
still true, and every other growth starts with `data` allocated.
   
   No test reaches this arm. `SPARK-44239: Test column vector reserve policy` 
resets only a top-level `ByteType` vector, which still tracks nulls, and the 
new growth test sets no threshold. So an edit that treats a released child as 
still allocated would pass every test. With the threshold set, 
`Platform.reallocateMemory` would then copy `capacity` bytes from native 
address 0 and crash the JVM during an off-heap vectorized read. The arm is 
correct today, but it is the one branch of this subtle check that nothing tests.
   
   **Verification:**
   - **Regression:** Under the huge-vector reserve policy, grow a string 
vector's byte data past the threshold on both on-heap and off-heap vectors and 
reset it. Confirm the byte-data child returns to its default capacity, then 
write and grow it again and check that every value reads back correctly. The 
case must fail if the off-heap old-capacity computation treats a released 
non-tracking child as allocated.



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/vectorized/ColumnVectorSuite.scala:
##########
@@ -1061,6 +1061,64 @@ class ColumnVectorSuite extends SparkFunSuite with 
SQLHelper {
     }
   }
 
+  test("SPARK-61210: The byte data of a string column vector holds no null 
bits") {
+    Array(new OnHeapColumnVector(32, StringType),
+      new OffHeapColumnVector(32, StringType)).foreach { vector =>
+      try {
+        vector.putByteArray(0, "hello".getBytes)
+        assert(!vector.arrayData().isNullAt(0))
+        // A null marks a whole value on the string vector, not a byte of its 
data.
+        vector.putNull(0)
+        assert(vector.isNullAt(0))
+        assert(!vector.arrayData().isNullAt(0))
+        // Writing nulls to the byte data itself changes nothing there.
+        vector.arrayData().putNull(1)
+        assert(!vector.arrayData().isNullAt(1))
+      } finally {
+        vector.close()
+      }
+    }
+
+    // An array's elements can be null each on their own, so an element vector 
keeps its null
+    // bits, unlike the byte data of a string.
+    val arrayVector = new OnHeapColumnVector(32, ArrayType(ByteType, 
containsNull = true))
+    try {
+      arrayVector.arrayData().putNull(0)
+      assert(arrayVector.arrayData().isNullAt(0))
+    } finally {
+      arrayVector.close()
+    }
+  }
+
+  test("SPARK-61210: Growing the byte data of a string column vector keeps 
earlier values") {
+    Array(new OnHeapColumnVector(4, StringType),
+      new OffHeapColumnVector(4, StringType)).foreach { vector =>
+      try {
+        val byteData = vector.arrayData()
+        val initialCapacity = byteData.capacity
+
+        // Fits within the byte-data child's initial capacity, so this does 
not grow it; these
+        // bytes sit in the region a later growth must carry over.
+        val first = (0 until 10).map(_.toByte).toArray
+        vector.putByteArray(0, first)
+        assert(byteData.capacity == initialCapacity)
+
+        // Exceeds the remaining capacity, forcing the byte-data child - which 
holds no null
+        // bits of its own - to grow. The no-null-tracking oldCapacity 
computation in
+        // OffHeapColumnVector#reserveInternal must still treat `first`, 
already written below,

Review Comment:
   **Nit (P3):** `first` is written by `vector.putByteArray(0, first)` above 
this comment (line 1103), and nothing after the comment writes it, so "already 
written below" sends a reader looking for a later write that isn't there. If 
"below" meant below the byte-data child's old capacity, the comment doesn't say 
so. Yet that reading is the one that explains why the oldCapacity fix has to 
carry these bytes over.
   
   **Verification:**
   - **Inspection:** The comment explaining why the growth test protects the 
off-heap carry-over refers unambiguously to the earlier write of the first 
value and to the region below the child's old capacity.



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/vectorized/ColumnVectorSuite.scala:
##########
@@ -1061,6 +1061,64 @@ class ColumnVectorSuite extends SparkFunSuite with 
SQLHelper {
     }
   }
 
+  test("SPARK-61210: The byte data of a string column vector holds no null 
bits") {

Review Comment:
   **Nit (P3):** This test's name says the byte-data child holds no null bits, 
but its assertions can't tell. It checks only the child's `isNullAt` answers, 
which the new `tracksNulls && ...` short-circuit returns whether or not a null 
buffer exists. The growth test checks only the values read back. Two 
regressions would each keep every assertion green: dropping `releaseNullBits()` 
from `stopTrackingNulls()`, or allocating `new byte[newCapacity]` for a 
non-tracking vector in `OnHeapColumnVector.reserveInternal`. Off heap, a 
never-released initial buffer also passes. `nulls` is private in both classes, 
and `isFree()` and `valuesNativeAddress()` can't isolate it, because the 
child's data is always allocated. So the memory saving, which is this change's 
only user-visible effect, could regress unnoticed.
   
   **Verification:**
   - **Regression:** On both on-heap and off-heap vectors, check right after 
construction, and again after forcing the byte-data child to grow, that the 
child holds no null storage while the parent and an array element child do. The 
check must fail if the child's initial null buffer is not released or if growth 
allocates one again.



-- 
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]

Reply via email to