Sub topic 14
Nested data: definition and repetition levels
Parquet stores each leaf field of a nested schema as its own column chunk. This chapter shows how it rebuilds lists, structs, and nulls from flat columns. It assumes you know the layout from earlier chapters (row groups, column chunks, pages, footer statistics) and can read basic PyArrow code.
The problem: flat columns, nested rows
Parquet is columnar, so it cannot store a list as one blob per row. Each leaf field is stored as a flat sequence of values. A leaf is a field with no children, such as tags.element or user.country.
A flat sequence loses two facts that a nested record carries:
- Where one row's list ends and the next row's begins.
- Which level of nesting was null or empty when a value is missing.
Parquet keeps both facts in two small integer streams stored next to the values in every page. These are the repetition levels and the definition levels. This design comes from the Dremel paper, and Parquet adopted it.
Definition levels
A definition level says how many optional or repeated ancestors on the path to a leaf are actually present. A leaf value is stored only when the definition level equals the column's maximum.
Take a nullable list of nullable strings, tags: list<string>. The path to the leaf has three levels that can be absent:
tagsitself can be null.- The repeated
listnode can have zero entries (an empty list). - The
elementcan be null.
So the maximum definition level is 3. The values mean:
| Definition level | Meaning |
|---|---|
| 0 | The list is null |
| 1 | The list exists but is empty |
| 2 | The list has an entry, but the element is null |
| 3 | The element has a value |
A required (non-nullable) field adds no level, because it can never be absent. A schema with fewer nullable fields therefore has smaller maximum levels and cheaper level streams.
Repetition levels
A repetition level says at which repeated ancestor a new entry starts. For a single list the rule is short:
- Repetition level 0 starts a new row.
- Repetition level 1 continues the list of the current row.
The maximum repetition level equals the number of repeated nodes on the path. For list<string> it is 1. A list<list<string>> has maximum repetition level 2, where 1 means "new inner list" and 2 means "next element in the current inner list". Structs add definition levels if nullable, but never repetition levels.
A worked example
Consider four rows of tags:
| Row | Value |
|---|---|
| 0 | `["a", "b"]` |
| 1 | `[]` |
| 2 | `null` |
| 3 | `[null, "c"]` |
Parquet stores six entries for this column, one per list element plus one placeholder for each empty or null list:
| Entry | Repetition | Definition | Stored value |
|---|---|---|---|
| 1 | 0 | 3 | `a` |
| 2 | 1 | 3 | `b` |
| 3 | 0 | 1 | none |
| 4 | 0 | 0 | none |
| 5 | 0 | 2 | none |
| 6 | 1 | 3 | `c` |
Only three entries (a, b, c) store a real value. The other three are recorded in the level streams alone. Rows 1 and 2 stay distinguishable (empty list versus null list) even though neither has any values.
The next diagram shows how a reader decodes one entry.
Seeing levels in PyArrow
PyArrow does not print the raw level streams, but the schema reports the maximum for each leaf. The column chunk metadata reports the number of entries.
import pyarrow as pa
import pyarrow.parquet as pq
tags = pa.array(
[["a", "b"], [], None, [None, "c"]],
type=pa.list_(pa.string()),
)
table = pa.table({"id": [1, 2, 3, 4], "tags": tags})
pq.write_table(table, "nested.parquet")
pf = pq.ParquetFile("nested.parquet")
for i in range(pf.metadata.num_columns):
col = pf.schema.column(i)
print(col.path, col.max_definition_level, col.max_repetition_level)
rg = pf.metadata.row_group(0)
print(rg.num_rows, rg.column(1).num_values)The loop prints a flat id column with maximum levels 0 and 0. It prints the list leaf with maximum definition level 3 and maximum repetition level 1. The leaf path is tags.list.element in current PyArrow, and older versions may show tags.list.item.
The last line prints 4 6. The row group has 4 rows, but the tags chunk has 6 entries. Remember this when you read num_values for a repeated column. It counts level entries, not rows.
Structs and leaf-only access
A struct adds no repetition level. It adds a definition level only if the struct or its fields are nullable. Each child field is still its own column chunk, so you can read one child without touching its siblings.
import pyarrow as pa
import pyarrow.parquet as pq
users = pa.array(
[{"country": "DE", "age": 31}, None, {"country": None, "age": 25}],
type=pa.struct([("country", pa.string()), ("age", pa.int64())]),
)
pq.write_table(pa.table({"user": users}), "users.parquet")
pf = pq.ParquetFile("users.parquet")
for i in range(pf.metadata.num_columns):
col = pf.schema.column(i)
print(col.path, col.max_definition_level, col.max_repetition_level)
only_country = pq.read_table("users.parquet", columns=["user.country"])
print(only_country.schema)Both leaves print maximum repetition level 0. user.country has maximum definition level 2, because both user and country can be null. The final read decodes only the user.country column chunk. The age pages are never read, which is the same column-pruning benefit as for flat tables.
What levels cost
Levels are not free, but they are cheap in typical data:
- Levels are encoded with the RLE/bit-packing hybrid from the encodings chapter. A column with long runs of the same level (no nulls, or one value per row) compresses to almost nothing.
- A column with maximum level 0 stores no level stream at all. A flat required column pays nothing.
- Each level uses just enough bits to hold the maximum, so level 3 takes 2 bits per entry before RLE.
- Deep nesting multiplies work. A reader must walk the levels to rebuild offsets and validity, so decoding a deeply nested column is slower than decoding a flat one of the same size.
Interaction with statistics and skipping
Statistics and indexes are kept per leaf column chunk, so they work on nested data with some caveats:
- Min and max describe the leaf values, not the lists. For
tags.element, the min and max cover every element in the chunk. A filter on one element value can skip a row group if that value is outside the range, but the statistics cannot tell you anything about list lengths or which row holds the value. - Null counts on repeated columns are easy to misread. Nulls can come from a null element, an empty list, or a null list, and writers and readers differ in how they count these. Do not use
null_counton a repeated column to decide whether any row is null. Check how your engine defines it first. - Filters on struct children (for example
user.age >= 30) behave like filters on flat columns, because a struct child with no repeated ancestor has one entry per row. - Some engines do not push predicates into repeated columns at all, and they read and filter after decoding. Test with your own reader.
Design guidance
- Prefer flat columns for fields you filter on often. A top-level
countrycolumn is simpler and skips better than atagslist that you search. - Use nested types when the data is naturally hierarchical and you usually read the parent as a whole, such as a list of line items per order.
- Mark fields required when they are never null. This lowers maximum levels and shrinks the level streams.
- Keep nesting shallow. Every extra repeated level adds a repetition level and extra offset reconstruction.
- Avoid very large lists inside one row. A single row's list cannot be split across row groups, so extreme lists create uneven row groups and memory spikes.
Common errors
- Confusing an empty list with a null list. They have different definition levels (1 versus 0), and many tools show both as empty. Check your reader if the difference matters.
- Forgetting that a null element costs a level entry but no stored value.
- Expecting
columns=["tags"]to read only part of a list. The whole list leaf is read, because the levels are needed to rebuild it. - Expecting list-aware pruning. Row group skipping uses leaf min and max, not list shape.
Exercise
A column scores has type list<int64> where both the list and its elements are nullable. For the rows [5], null, [], [7, null], give the (repetition, definition) pair for each entry in order, and say how many values are actually stored.
Show answer
The maximum definition level is 3 (list present, entry present, element present). The entries are: 5 is (0, 3); the null list is (0, 0); the empty list is (0, 1); 7 is (0, 3); the null element is (1, 2). That makes 5 entries, so num_values is 5 for 4 rows. Only 2 values are stored, 5 and 7.
Exercise
You need to filter on the field user.age and also search tag strings in tags. Which one is more likely to benefit from row group skipping on min/max statistics, and why?
Show answer
user.age. A struct child with no repeated ancestor has one entry per row, so its min and max describe the rows directly and the range comparison is meaningful. The min and max of tags.element only cover the elements in the chunk, so they exclude row groups only when the searched string is outside the whole range, which rarely happens for strings. Engine support for pushdown into repeated columns also varies.