Sub topic 2
Columnar layout: one contiguous run of values per column
This chapter assumes you can read basic Python and that you know what rows and columns are from pandas or SQL. You should also know that data in memory is stored at numbered addresses. Arrow's buffers, schemas and tables come in later chapters. Here the focus is the layout idea they all rest on.
Row layout versus column layout
A table can be laid out in memory in two ways.
- Row-oriented: the values of one row sit next to each other. Row 1's
id,nameandscorecome first, then row 2's, and so on. - Column-oriented (columnar): the values of one column sit next to each other. All
idvalues come first, then allnamevalues, then allscorevalues.
Arrow uses the columnar layout. Each column is stored as one contiguous run of memory, with no values from other columns mixed in.
Why contiguous columns help
Analytical work usually touches a few columns across many rows. An example is summing score over a million rows. This layout suits that work for three reasons.
- Less data read. The program reads only the
scorerun. It never loadsnameorid. - Cache friendliness. A CPU loads memory in small blocks called cache lines. In a contiguous run of numbers, every value in a loaded block is useful.
- Vectorization. Modern CPUs can apply one instruction to several adjacent values at once (SIMD). Contiguous values of one type make that possible.
Row layout wins in the opposite case. If you often fetch or insert one whole row, such as a single customer record, the row's values are best kept together. That is why transactional databases are usually row-oriented.
What this means for sharing
Because a column is a plain run of fixed-width values, another tool can read it in place if both tools agree on the layout. Arrow is that agreement. A buffer written by one library has a known shape: a start address, a length, and a value width. A second library can use it without parsing or converting each value.
You can see the effect in pyarrow. Converting a NumPy array with no nulls reuses the existing memory.
import numpy as np
import pyarrow as pa
values = np.array([1, 2, 3], dtype=np.int64)
arr = pa.array(values)
back = arr.to_numpy(zero_copy_only=True)
print(back)Here zero_copy_only=True makes pyarrow raise an error if it would have to copy. That error tells you the layouts did not match. Later chapters cover when each conversion copies.
A common mistake
Columnar does not mean every column is a simple run of numbers. Variable-width data such as strings needs an extra buffer that records where each value starts and ends. Missing values need a record of which positions are null. Both are still stored per column, and the next chapter covers them.
Also, a columnar layout does not make every operation faster. Appending one row means touching every column's run. Reading one whole row means gathering a value from each column. Arrow tables are built for scanning and sharing, and they are a poor fit for frequent single-row updates.
Exercise
A table has 50 columns and 10 million rows. A job computes the average of one numeric column. Why does a columnar layout reduce the work compared with a row layout, and what kind of job would favor the row layout instead?
Show answer
In a columnar layout the job reads only the one contiguous run for that column, about 1/50 of the data, and the CPU can process the adjacent values efficiently. In a row layout every row would be loaded, including the 49 unused values in each row. A job that reads or writes whole individual rows, such as looking up one customer record or inserting single records, favors the row layout.