"Zero-copy" sounds like a slogan until you see what it relies on: an Arrow array is nothing but a few byte buffers whose layout is fixed in a specification. Anyone who knows the rules can find value number 5000 by arithmetic, in any language, with no library at all. This topic reads those buffers by hand, which is also the best way to learn why Arrow is fast.
The idea in plain words
Where we are. Topic 3 used the buffers without looking at them. This topic opens them: sizes, the validity bitmap for nulls, the offsets that locate strings. It explains the arithmetic of topic 1 and makes the next topic, sharing memory between libraries, concrete: two libraries can share an array because they agree on exactly these buffers.
A buffer with one bit per element: 1 means the value is present, 0 means null. It lets a column of numbers have holes without reserving a special number for "missing", and it is omitted entirely when nothing is null.
Sizes and bitmaps
An int64 array with nulls has two buffers: the validity bitmap and the 8-byte values. Ten values need a two-byte bitmap, because eight bits fit in a byte and the last two spill into a second. Null positions still occupy their 8 bytes in the values buffer, holding unspecified bytes that readers must ignore. To know whether element i is null, read bit i % 8 of byte i // 8.
# Ten prices with three nulls: two buffers, and the bitmap read back bit by bitprint("sizes:", buffer_sizes(prices), "(bitmap bytes, value bytes)")print("bits :", validity_bits(prices))print("slice of 4 starting at 3:", validity_bits(prices.slice(3, 4)))
sizes: [2, 80] (bitmap bytes, value bytes) bits : [1, 0, 1, 1, 0, 1, 1, 1, 1, 0] slice of 4 starting at 3: [1, 0, 1, 1]
Strings: offsets and one data buffer
A string column cannot be fixed-width, so Arrow stores all the characters in one data buffer, back to back, and adds an offsets buffer with n + 1 entries: string i is the bytes from offsets[i] to offsets[i + 1]. A null or an empty string makes two equal offsets. Offsets count bytes, which is why 日本 takes six. To find a string you do two reads and a slice, no search, and string_at does exactly that.
# Five strings, one of them null, one empty, one non-ASCII: the offsets, then each string read from the raw buffersprint(string_offsets(names))for i in range(len(names)): print(i, repr(string_at(names, i)))
[0, 4, 4, 6, 6, 12] 0 'AAPL' 1 None 2 'KO' 3 '' 4 '日本'
# The real symbol column: 6,699 AAPL rows need almost no text, but still a 4-byte offset eachprint(len(symbols), "elements; buffers (bitmap, offsets, data):", buffer_sizes(symbols))print("offsets:", 4 * (len(symbols) + 1), "bytes = 4 x (n + 1) | data:", 4 * len(symbols), "bytes = 4 characters x n")
6699 elements; buffers (bitmap, offsets, data): [0, 26800, 26796] offsets: 26800 bytes = 4 x (n + 1) | data: 26796 bytes = 4 characters x n
A symbol column pays offsets and characters for every row even though it holds five distinct values. Topic 7 shows the Arrow answer, the same idea as in Parquet: store each distinct string once and keep a small integer per row.
What the tasks ask
- 1`buffer_sizes`
array.buffers()returns the buffers orNone; usebuffer.sizeand count a missing one as 0. - 2`validity_bits`
memoryview(bitmap); elementiis(raw[(offset + i) >> 3] >> ((offset + i) & 7)) & 1; no bitmap means all present. - 3`string_offsets` and `string_at`
memoryview(offsets_buffer).cast("i")views the offsets as 32-bit integers; the string isbytes(memoryview(data)[a:b]).decode(); check the bitmap first for nulls.
Lab · what's graded
4 tasks · 100 pts- Weigh the buffers:
buffer_sizes25 pts - Read the validity bitmap:
validity_bits25 pts - Read the offsets:
string_offsets25 pts - Pull one string out:
string_at25 pts