wangyum opened a new pull request, #60234:
URL: https://github.com/apache/spark/pull/60234
### What changes were proposed in this pull request?
The byte-data child vector that `WritableColumnVector` allocates for a
`String`,
`Binary`, byte-array-backed `DECIMAL`, `GEOMETRY`, or `GEOGRAPHY` column's
raw
bytes no longer tracks null bits of its own.
Class hierarchy touched (all in `sql/core/.../execution/vectorized`):
WritableColumnVector (abstract)
+ tracksNulls: boolean = true <- new field
+ releaseNullBits(): void <- new, no-op base impl
+ stopTrackingNulls(): void <- new, private
|
+-- OnHeapColumnVector
| putNull/putNotNull/putNulls/putNotNulls/isNullAt: now check
tracksNulls first
| reserveInternal: skips allocating `nulls` when !tracksNulls
| releaseNullBits(): nulls = null
|
+-- OffHeapColumnVector
putNull/putNotNull/putNulls/putNotNulls/isNullAt: now check
tracksNulls first
reserveInternal: oldCapacity now falls back to `data != 0L` when
!tracksNulls
(see "Why are the changes needed?" - this is
the subtle part)
releaseNullBits(): frees the native `nulls` pointer
The constructor's array-type branch calls
`childColumns[0].stopTrackingNulls()`
only when the child is the byte-data array of one of the types above (i.e.
not
for a genuine `ArrayType`'s element child, which still needs per-element
nulls).
### Why are the changes needed?
A `String`/`Binary`/`DECIMAL`/`GEOMETRY`/`GEOGRAPHY` column's vector is
really
two vectors: the parent (offsets + lengths + the parent's own null bit) and a
`ByteType` "byte-data child" that holds the concatenated raw bytes of every
value in the batch. Before this change, that byte-data child was built
exactly
like any other `WritableColumnVector`, which means it *also* allocated a
`nulls` byte array exactly as large as its data array - one null byte per
byte
of string/binary content:
BEFORE this fix - a batch of 3 strings ("ab", "", "xyz")
====================================================================
Parent vector (StringType) Byte-data child (ByteType)
-------------------------- --------------------------
nulls: [0, 0, 0] (3 bytes) data: [a, b, x, y, z] (5
bytes)
offsets: [0, 2, 2] (12 bytes) nulls: [0, 0, 0, 0, 0] (5
bytes) <- dead weight
lengths: [2, 0, 3] (12 bytes)
-------------------------- --------------------------
parent total: 27 bytes child total: 10 bytes
(5 of which are never read
by anyone)
AFTER this fix
====================================================================
Parent vector (StringType) Byte-data child (ByteType)
-------------------------- --------------------------
nulls: [0, 0, 0] (3 bytes) data: [a, b, x, y, z] (5
bytes)
offsets: [0, 2, 2] (12 bytes) (no nulls array at all)
lengths: [2, 0, 3] (12 bytes)
-------------------------- --------------------------
parent total: 27 bytes child total: 5 bytes
The reason the child's `nulls` array was always dead weight: whether a value
is null is a property of the *whole value*, recorded once on the **parent**
vector (`isNullAt(rowId)`). A byte of that value's raw content can never be
"null" on its own - `getUTF8String`/`getBinary`/`getBinaryView` all check the
parent's null bit and then read bytes straight from the child via
`getBytes(offset, length)`; nothing in Spark ever calls
`arrayData().isNullAt(...)` on the child. So that array was pure overhead
with
no reader behind it, for every vectorized Parquet/ORC read and the in-memory
columnar cache, on every batch of every `String`/`Binary`/large-`DECIMAL`/
`GEOMETRY`/`GEOGRAPHY` column.
Because the child's `nulls` array was always allocated 1:1 with its `data`
array, removing it always **cuts the byte-data child's memory exactly in
half**, no matter how large it grows. For short values (IDs, short names) the
child is a small fraction of the column's total footprint, so the saving is
modest. For workloads with large values - long text fields, embedded JSON,
geometry WKB blobs - the child dominates the column's memory, and the saving
approaches 50% of that column's footprint. The original bug report's example
makes this concrete: a batch of 4096 45KB strings needs the byte-data child
to
hold 45KB * 4096 ~ 183MB of real content; Spark's default growth policy
rounds that up to 366MB for `data`. Before this fix, `nulls` mirrored that
exactly, for another 366MB - 732MB total for one column's byte-data child
in one batch. This fix removes that second 366MB outright.
One pitfall this fix had to get right, and the reason `OffHeapColumnVector`
needed a second change beyond the simple "skip the nulls array" part:
`reserveInternal`'s `oldCapacity` used to be computed as `(nulls == 0L) ? 0 :
capacity`, i.e. it used "do I have a nulls pointer yet" as a proxy for "have
I
been initialized before". Once a child stops allocating `nulls` at all, that
proxy is permanently false, so every growth of the child's `data` buffer
would
believe there was nothing to carry over - and `Platform.reallocateMemory`
copies zero bytes for an `oldCapacity` of zero, silently dropping every byte
already written. This PR fixes the proxy to check `data != 0L` instead when
the vector doesn't track nulls, and adds a regression test
(`ColumnVectorSuite`, "SPARK-61210: Growing the byte data of a string column
vector keeps earlier values") that specifically forces a byte-data child to
grow mid-write and asserts the earlier bytes survive, on both
`OnHeapColumnVector` and `OffHeapColumnVector`.
### Does this PR introduce _any_ user-facing change?
Yes, a memory-footprint change (not an API or SQL-semantics change): a
vectorized Parquet/ORC read or in-memory columnar cache of a `String`,
`Binary`, byte-array-backed `DECIMAL`, `GEOMETRY`, or `GEOGRAPHY` column now
uses less heap (on-heap) or native (off-heap) memory per batch.
### How was this patch tested?
- Added `ColumnVectorSuite`:
- `"SPARK-61210: The byte data of a string column vector holds no null
bits"`
- asserts a string column's byte-data child reports every row as non-null
regardless of what the parent's null bit says, and that writing a null
directly to the child is a no-op; also asserts a genuine `ArrayType`'s
element child is unaffected and still tracks nulls normally.
- `"SPARK-61210: Growing the byte data of a string column vector keeps
earlier values"`
- forces a string column's byte-data child to grow past its initial
capacity mid-write, on both `OnHeapColumnVector` and
`OffHeapColumnVector`, and asserts the bytes written before the growth
are still readable afterward (regression test for the `oldCapacity` fix
described above).
- Ran the full existing `ColumnVectorSuite` (all pre-existing tests, e.g. the
`string`/`binary`/`SPARK-22092` reallocation tests) and
`ParquetStorageFilterSuite` (which exercises
`LateMaterializationParquetRecordReader`'s byte-charge accounting) to
confirm nothing else regressed; both fully green.
```
build/sbt 'sql/testOnly
org.apache.spark.sql.execution.vectorized.ColumnVectorSuite
org.apache.spark.sql.execution.datasources.parquet.ParquetStorageFilterSuite'
```
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Cursor (model: GLM 5.3).
--
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]