Sub topic 14
Nested data: definition and repetition levels
This chapter assumes you know Parquet's row groups, column chunks, and pages, and that you have read file metadata with PyArrow. It explains how Parquet stores lists and structs in flat columns.
Nested data becomes leaf columns
Parquet stores each leaf field of a schema as its own column. A list<string> column named tags becomes one leaf column with the path tags.list.element. Each leaf has its own column chunks, pages, encodings, and statistics.
A flat list of values cannot show where one record ends and the next begins, or which values are null. Parquet adds two small integers to every entry in a leaf column to carry that structure:
- Repetition level: which repeated field in the path is repeating. A value of 0 means a new record starts.
- Definition level: how many optional or repeated fields along the path are actually present. The maximum level means the value exists. Anything lower means a null or empty ancestor.
Both are stored as small integers that are run-length and bit-packed, so they cost very little when most values are present.
A worked example
Take a tags column with four records: ["a", "b"], [], null, and ["c", null]. PyArrow writes the nullable list as three levels: an optional tags group, a repeated list group, and an optional element. That gives a maximum definition level of 3 and a maximum repetition level of 1.
| Record | Entry | Repetition | Definition | Stored value |
|---|---|---|---|---|
| `["a", "b"]` | first | 0 | 3 | `a` |
| `["a", "b"]` | second | 1 | 3 | `b` |
| `[]` | only | 0 | 1 | none |
| `null` | only | 0 | 0 | none |
| `["c", null]` | first | 0 | 3 | `c` |
| `["c", null]` | second | 1 | 2 | none |
Read the definition levels from the top:
- Level 0: the whole list is null.
- Level 1: the list exists but has no entries.
- Level 2: a list entry exists but its element is null.
- Level 3: the element has a value.
The file stores only three values (a, b, c) plus six pairs of levels.
Checking the levels with PyArrow
import pyarrow as pa
import pyarrow.parquet as pq
table = pa.table({
"tags": pa.array(
[["a", "b"], [], None, ["c", None]],
type=pa.list_(pa.string()),
)
})
pq.write_table(table, "tags.parquet")
leaf = pq.ParquetFile("tags.parquet").schema.column(0)
print(leaf.path, leaf.max_definition_level, leaf.max_repetition_level)This prints tags.list.element 3 1. The leaf name can differ in older PyArrow versions or other writers, but the levels follow from the schema structure.
A flat nullable column such as id has a maximum definition level of 1 and a maximum repetition level of 0. A required flat column has both at 0, so Parquet stores no levels for it.
What this means for statistics and filtering
Statistics are kept per leaf column chunk. Two details matter for nested data:
- Min and max describe only the stored leaf values, such as the strings inside
tags. They say nothing about list lengths. - Per the format specification, the null count counts every entry whose definition level is below the maximum. In the example, the empty list, the null list, and the null element each count as one null.
Because of this, a filter on a top-level flat column skips row groups as usual. A filter on a nested leaf is less reliable: readers differ in how well they push such predicates down, so test the behavior with your engine.
Practical guidance
- Nesting has a cost. Reading
tagsmeans decoding levels as well as values, and rebuilding lists takes extra work. - Read only the leaf columns you need. Selecting
tagsreads one leaf, while a struct with ten fields has ten leaves, and you can request just one. - Frequently filtered fields are better as top-level flat columns, because their statistics are simpler and better supported.
- Deep nesting with many optional levels raises the maximum definition level and so the width of the stored level data.
Exercise
A leaf column has maximum definition level 3 and maximum repetition level 1. An entry has repetition level 1 and definition level 2. What does it mean?
Show answer
Repetition level 1 means the entry continues the current list rather than starting a new record. Definition level 2 means the list entry exists but its element is null, because the value is below the maximum of 3. No value is stored for it, and it counts toward the null count.