Sub topic 12
Page index for finer-grained skipping
This chapter assumes you know how a Parquet file is organized into row groups, column chunks, and pages. It also assumes you understand min/max column statistics and predicate pushdown at row group level. You need basic Python and PyArrow to run the examples.
The problem with row group statistics
Row group statistics skip data in large units. A row group often holds hundreds of thousands or millions of rows. If a filter such as id >= 950_000 matches even one row in a row group, the reader must fetch and decode that whole column chunk, even when only a handful of rows qualify.
Row group statistics are also coarse in a second way. They hold one min and one max for the whole column chunk. A column chunk with values spread across a wide range yields a wide min/max interval, and most filters overlap it.
The page index fixes this by recording statistics for every data page in a column chunk. A reader can then skip individual pages inside a row group that it could not skip as a whole.
What the page index contains
The page index is two structures per column chunk. Both are stored outside the data pages.
| Structure | Contents per page | Purpose |
|---|---|---|
| Column index | min, max, whether the page is all nulls, null count | Decide which pages might match a predicate |
| Offset index | file offset, compressed page size, first row index | Locate a page and map it to row numbers |
The column index also stores one flag for the whole column chunk called the boundary order. It says whether the page min/max values are ascending, descending, or unordered. If they are ordered, a reader can binary-search the pages instead of scanning them all.
The first_row_index in the offset index is the key to combining columns. It gives the row number at which each page starts, relative to the row group.
Where the index lives in the file
The page index is not inside the footer. It sits in its own region after the last row group and before the footer. The column chunk metadata in the footer holds the offset and length of that column's column index and offset index.
This placement matters for two reasons:
- Readers that ignore the page index never load it, so the footer stays small.
- Readers that use it fetch only the index bytes for the columns they filter on, using the offsets from the footer.
How a filtered read uses the page index
Take a query that selects value where id is between 60,000 and 110,000. A reader that supports the page index proceeds like this:
- Read the footer. Use row group statistics to drop row groups whose
idrange cannot match. - For each surviving row group, read the column index of
id. Keep the pages whose min/max interval overlaps the predicate. - Read the offset index of
id. Convert each kept page into a row range usingfirst_row_indexand the next page's first row. - Merge those ranges. This is the set of rows that might match.
- Read the offset index of
value. Find the pages ofvaluethat cover the row ranges. Read only those pages. - Decode the pages and apply the exact predicate to the remaining rows.
Step 5 is where the saving compounds. Pages in different columns are not aligned. A page of id may cover 25,000 rows while a page of value covers 40,000. The offset index lets the reader translate row ranges between columns without decoding anything.
Writing a file with a page index
PyArrow can write the page index. The write_page_index option is available in recent releases (PyArrow 14 or later is a safe choice). Page size is controlled by data_page_size, a target in bytes for the uncompressed size of each data page.
import pyarrow as pa
import pyarrow.parquet as pq
n = 1_000_000
table = pa.table({
"id": pa.array(range(n), type=pa.int64()),
"value": pa.array([i % 1000 for i in range(n)], type=pa.int32()),
})
pq.write_table(
table,
"events_indexed.parquet",
row_group_size=500_000,
data_page_size=32 * 1024,
compression="zstd",
write_page_index=True,
)
md = pq.ParquetFile("events_indexed.parquet").metadata
for rg in range(md.num_row_groups):
for c in range(md.num_columns):
col = md.row_group(rg).column(c)
print(rg, col.path_in_schema, col.has_column_index, col.has_offset_index)Each line should print True True. The metadata reports whether the index exists for each column chunk. It does not expose the per-page values through this API, so PyArrow shows you that the index is present rather than its contents.
The small data_page_size of 32 KiB forces many pages per column chunk, so the index has something to work with. The default is about 1 MiB, which gives few pages for a column of small integers.
Simulating the page selection
The logic of steps 2 to 4 is short enough to write by hand. This sketch uses made-up page statistics that mirror a sorted id column with 25,000 rows per page.
# Each page: (first_row, min_id, max_id)
pages = [(start, start, start + 24_999) for start in range(0, 200_000, 25_000)]
total_rows = 200_000
lo, hi = 60_000, 110_000 # predicate: lo <= id <= hi
ranges = []
for i, (first_row, pmin, pmax) in enumerate(pages):
if pmax >= lo and pmin <= hi:
next_first = pages[i + 1][0] if i + 1 < len(pages) else total_rows
ranges.append((first_row, next_first - 1))
print("pages kept:", len(ranges), "of", len(pages))
print("row ranges:", ranges)The output keeps three of eight pages, covering rows 50,000 to 124,999. Other columns would then read only the pages that overlap those rows. The exact predicate still runs afterwards, because a kept page can hold rows outside the interval.
When the page index helps
The page index pays off when values are clustered. Sorted or nearly sorted columns, such as timestamps or auto-incrementing ids, give narrow, non-overlapping page intervals. A selective filter then keeps very few pages.
It helps little in these cases:
- The column is randomly distributed. Every page's min/max spans nearly the whole range, so no page is skipped.
- The filter matches most rows. There is nothing to skip.
- The reader does not implement page-level filtering. The index is then written but unused.
Sorting data by the column you filter on most often is the cheapest way to make both row group statistics and the page index effective.
Trade-offs and gotchas
Smaller pages mean finer skipping but more overhead. Each page adds a header, an entry in both indexes, and a decode step. Very small pages also compress and encode worse, because each page is compressed on its own and dictionary or run-length runs are cut at page boundaries. Pages in the tens of kilobytes to about a megabyte are typical.
Index size grows with page count. The index is read for each filtered column. Many tiny pages can make the index itself costly, especially on object storage where each extra request has latency.
Truncated bounds. Writers may truncate long string or binary min/max values to save space. A truncated min is rounded down and a truncated max is rounded up, so the bounds stay valid but looser. Pages may be kept that a full-precision bound would have skipped. This is safe but less selective.
All-null pages. A page whose values are all null is flagged as a null page in the column index and has no meaningful min/max. Readers must handle the flag, and predicates like IS NOT NULL can skip these pages using the null counts.
Floating-point NaN. Min/max for float columns is ambiguous when NaN values are present. Older writers could record unreliable bounds, so engines treat such statistics cautiously.
Dictionary pages are separate. A dictionary-encoded column chunk starts with a dictionary page that all data pages refer to. A reader that skips data pages must still read and decode that dictionary for any page it keeps. The offset index describes data pages, so the reader gets the dictionary page from the column chunk's own metadata.
Rewrites invalidate nothing, but they change layout. The index is written with the file. If you compact files or change the sort order, the page boundaries and their selectivity change too. Check the effect after each such change.
Page index versus other skipping tools
| Tool | Granularity | Best for | Limit |
|---|---|---|---|
| Row group statistics | Row group | Range filters on sorted or clustered data | Coarse |
| Page index | Page | Range filters on sorted data, selective queries | Needs engine support, needs clustering |
| Bloom filter | Column chunk | Equality lookups on high-cardinality values | No range queries |
The page index and row group statistics answer the same question, which is whether a range overlaps. Bloom filters answer a different question: whether a specific value is present. They are covered in the next chapter.
Exercise
A column chunk holds 40 pages. The column index shows the page min/max intervals of an event_time column in ascending order, with no overlap. A query asks for one hour of data that falls entirely inside the interval of page 17. Which structures does a page-index-aware reader consult to read the user_id column for those rows, and why can't it simply read page 17 of user_id?
Show answer
It reads the column index of event_time to find that only page 17 overlaps the hour, then the offset index of event_time to turn that page into a row range using first_row_index. It then reads the offset index of user_id and finds the user_id pages covering that row range. It cannot assume page 17 of user_id, because pages in different columns are not aligned. Each column splits its rows into pages by its own size, so the same rows may sit in different page numbers, and the first_row_index values are what map between them.
Exercise
You write a file with write_page_index=True but query it through an engine that only applies row group statistics. Your filter is selective and the data is sorted by the filter column. What changes in the query's performance, and what does the page index cost you?
Show answer
Nothing improves, because the engine ignores the page index and still reads whole column chunks for any row group that overlaps the filter. The cost is a small amount of extra file size for the index region, plus a little extra write time. The footer is not enlarged, so readers that ignore the index are not slowed down.