Sub topic 3
Inside an array: buffers, validity bitmaps and offsets
This chapter assumes you can run Python with pyarrow installed, that you know what a column of values is, and that you have read the earlier chapter on columnar layout. It also assumes you know that a byte is eight bits. Here you will look inside a single Arrow array and see which pieces of memory it is made of.
An array is a length plus a few buffers
A buffer is a contiguous region of memory with a known size. An Arrow array is a sequence of values of one type, and it is stored as a length plus a small, fixed set of buffers. The type decides how many buffers exist and what each one means. Reading the value at index i means doing a bit of arithmetic on those buffers. No per-value objects are involved, unlike a Python list.
Three kinds of buffer appear most often:
- A validity bitmap records which slots hold a value and which are null.
- A data buffer holds the values themselves, packed back to back.
- An offsets buffer marks where each variable-length value starts and ends inside a data buffer.
The sections below take them in turn.
Validity bitmaps: one bit per slot
Arrow does not reserve a special "missing" value such as NaN or -1. It keeps one bit per slot in a separate bitmap. A 1 means the slot is valid, and a 0 means it is null. Bits are packed least-significant-bit first. Slot 0 is bit 0 of byte 0, slot 7 is bit 7 of byte 0, and slot 8 is bit 0 of byte 1.
This design works for every type. An integer column can hold any 32-bit value and still have nulls. Because null tracking is separate from the values, a null check on a million slots reads about 125 KB of bitmap, and the data buffer is not touched.
The first example shows the two buffers of a nullable int32 array:
import pyarrow as pa
arr = pa.array([1, None, 3], type=pa.int32())
print(arr.null_count)
print(len(arr.buffers()))This prints 1 and then 2. The two buffers are the validity bitmap and the data buffer. You can read the bitmap directly:
import struct
import pyarrow as pa
arr = pa.array([1, None, 3], type=pa.int32())
validity, data = arr.buffers()
first_byte = validity.to_pybytes()[0]
print([(first_byte >> i) & 1 for i in range(len(arr))])
values = struct.unpack("<3i", data.to_pybytes()[:12])
print(values[0], values[2])The output is [1, 0, 1], then 1 3. The bitmap byte is 0b00000101. The data buffer holds three 4-byte integers, 12 bytes in all. The middle four bytes belong to the null slot.
The bytes under a null slot are unspecified. They are often zero, but a reader must not rely on that, and it must check the bitmap before it trusts a value. This is the most common mistake when you read raw buffers by hand.
Arrays with no nulls
If an array has no nulls, the validity bitmap may be absent. In pyarrow its entry in buffers() is then None:
import pyarrow as pa
arr = pa.array([1, 2, 3], type=pa.int32())
print(arr.null_count)
print(arr.buffers()[0] is None)This prints 0 and then True. The array still has two buffer slots, but the first one is empty, so the code that reads the buffers must handle None. Because no bitmap is allocated, a dense column saves that memory and the work of filling it in.
Fixed-width types: the data buffer is a flat array
For types whose values all have the same size, the data buffer is a flat C-style array. The value at index i of an int32 array sits at byte 4 * i. No lookup is needed, and the CPU can scan the memory in sequence.
| Type | Value storage | Buffers |
|---|---|---|
| `int8` to `int64`, `float32`, `float64` | N bytes per slot | validity, data |
| `bool` | 1 bit per slot, packed like the bitmap | validity, data |
| `date32`, `timestamp` | integers with a unit | validity, data |
| `string`, `binary` | variable bytes | validity, offsets, data |
| `list<T>` | variable-length runs of a child array | validity, offsets, plus the child's buffers |
Booleans use a bit-packed data buffer, so a boolean array has two bitmaps: one for validity and one for the values.
Variable-length types: offsets point into a data buffer
A string value has no fixed size, so the position 4 * i trick does not work. Arrow adds an offsets buffer. Offset k is the byte position in the data buffer where value k starts. The offsets buffer holds one more entry than the array has slots. Value i occupies the bytes from offsets[i] up to, but not including, offsets[i + 1].
import numpy as np
import pyarrow as pa
arr = pa.array(["ab", None, "cde"])
validity, offsets, data = arr.buffers()
off = np.frombuffer(offsets, dtype=np.int32)[: len(arr) + 1]
print(off)
print(data.to_pybytes()[: off[-1]])The output is [0 2 2 5] and then b'abcde'. Slot 0 spans bytes 0 to 2, which is ab. Slot 1 spans bytes 2 to 2, so it is empty, and the bitmap marks it null. Slot 2 spans bytes 2 to 5, which is cde. A null string normally takes zero bytes, so two equal offsets are consistent with either an empty string or a null. Only the bitmap tells them apart.
Offset width and size limits
The default string and binary types use 32-bit signed offsets. One array can hold at most about 2 GiB of string bytes. The large_string and large_binary types use 64-bit offsets and remove that limit, at the cost of a bigger offsets buffer. Arrays that are too big for one string array are usually split into chunks. A table column that is a chunked array can have many chunks, and each chunk has its own buffers.
Lists work the same way. A list<int32> array has a validity bitmap and an offsets buffer. The offsets index into a child array that holds all the inner values end to end. In pyarrow, buffers() returns the parent's buffers followed by the child's.
Slicing shares buffers
An array also stores an offset, the index of its first element within the buffers. Slicing uses it. slice returns a new array object that points at the same buffers with a different offset and length, so no values are copied.
import pyarrow as pa
arr = pa.array([10, 20, 30, 40, 50], type=pa.int32())
part = arr.slice(1, 3)
print(part.to_pylist())
print(part.offset)
print(part.buffers()[1].address == arr.buffers()[1].address)This prints [20, 30, 40], 1 and True. The sliced array reads the same data buffer starting one element in. Slicing therefore costs about the same for 3 values or 3 billion.
The consequence is memory retention. If you slice 10 values out of a 1 GB array and keep only the slice, the whole 1 GB stays alive, because the slice still references the original buffers. To release the large buffers, copy the slice into new memory, for example by building a new array from its values or by calling pa.concat_arrays([part]).
Alignment and padding
Arrow recommends that each buffer start at an address that is a multiple of 64 bytes, and that its allocated size be padded to a multiple of 64 bytes. The specification requires only 8-byte padding. The 64-byte recommendation matches CPU cache lines and lets vectorized code load whole registers without special cases at the end. This is why a buffer's allocated capacity can be larger than the bytes you need. Use buf.size for the logical size, not the allocation.
Common errors
- Reading the data buffer without checking validity. The bytes under a null are unspecified.
- Assuming
buffers()[0]is always a bitmap. It can beNone. - Forgetting that
offsetshaslen + 1entries. Reading onlylenentries drops the end of the last value. - Ignoring
arr.offsetwhen you decode raw buffers by hand. After a slice, slot 0 of the array is not slot 0 of the buffers. - Treating
null_countas free. Arrow may compute it lazily from the bitmap on first use.
Why this layout helps
Every Arrow library in every language agrees on these layouts. A C++ engine can build an array, and a Python process can read it by interpreting the same bytes. Nothing needs to be parsed first. The next chapters build on this. Schemas name the types, tables group arrays, and zero-copy sharing hands the same buffers to another tool.
Exercise
What are the three buffers of pa.array(["x", None, "yz"]), and what offsets do you expect? Which buffer tells you whether slot 1 is null?
Show answer
The buffers are the validity bitmap, the offsets buffer and the data buffer. The offsets are [0, 1, 1, 3] (int32, four entries for three slots), and the data buffer holds b"xyz". The validity bitmap (bits 1, 0, 1) says slot 1 is null. The offsets alone cannot tell, because a null and an empty string both span zero bytes.
Exercise
You build a 50-million-row int64 array and keep only arr.slice(0, 10). Why might memory stay high, and how would you fix it?
Show answer
The slice shares the original buffers, so the full data buffer (about 400 MB) stays alive while the slice exists. Copy the slice into new buffers, for example with pa.concat_arrays([arr.slice(0, 10)]), and then drop the reference to the original array so its memory can be freed.