≈ The Lake

Part 3 · Parquet and columnar files · Chapter 5

Parquet file layout

A Parquet file byte by byte: magic, row groups, column chunks, pages, the Thrift-encoded footer, and how a reader finds what it needs with a tail read and one range per column chunk.

complete ~35 min

After this chapter you should be able to:
  • Draw the physical layout of a Parquet file and name what each byte range holds.
  • Read FileMetaData, RowGroup, ColumnChunk and PageHeader fields from parquet.thrift, and decode a page header by hand.
  • Predict which byte ranges a reader fetches for a given projection and filter.
  • Explain how row group size and page size trade scan efficiency against memory and overhead.

Every Delta and Iceberg table in this book stores its rows in Parquet files. The table formats decide which files make up a version; Parquet decides how a reader gets from a file to the values of one column without reading the others. This chapter takes a Parquet file apart byte by byte, using a small writer and reader built for the book that produce real files: Arrow’s reader reads its files, and it reads Arrow’s.

Spec links point to apache/parquet-format at 6f87925. Implementation links point to Arrow C++ 26.0.0 and parquet-java 1.17.1.

From Dremel to Parquet

Google’s Dremel paper (Melnik et al., PVLDB 3(1), 2010) described a columnar storage format for nested records: shred each record into one column per leaf field, with two small integers per value (repetition and definition levels) that let a reader put the record back together.[Dremel] Parquet was created in 2013 by engineers at Twitter and Cloudera to bring that representation to the Hadoop ecosystem. The spec’s motivation section says it plainly: “We created Parquet to make the advantages of compressed, efficient columnar data representation available to any project in the Hadoop ecosystem,” and it is “built from the ground up with complex nested data structures in mind”.[README]

This chapter covers flat columns. Chapter 6 covers encodings and compression, chapter 7 the levels for nested data, and chapter 8 statistics, page indexes and bloom filters.

  1. 2010 Dremel: Interactive Analysis of Web-Scale Datasets · Sergey Melnik et al. (Google), PVLDB 3(1)
  2. 2013 Parquet 1.0 announced · Twitter (Julien Le Dem) and Cloudera (Nong Li)
  3. 2015-04 Parquet becomes an Apache top-level project · ASF

The bytes

The spec’s glossary defines three levels. A row group is “a logical horizontal partitioning of the data into rows”. A column chunk holds one column’s data for one row group, and column chunks “are guaranteed to be contiguous in the file”. A page is “conceptually an indivisible unit (in terms of compression and encoding)”. So: “a file consists of one or more row groups. A row group contains exactly one column chunk per column. Column chunks contain one or more pages.”[README]

Here is the book’s sample file: 24 rows of (trip_id INT64, city STRING, fare DOUBLE nullable), written with 8 rows per row group and 4 rows per page, no dictionary, no compression. It is 2,084 bytes: 4 bytes of magic, three row groups of three column chunks each, a 728-byte footer, the 4-byte little-endian footer length and the closing magic.

Inside the sample Parquet file
The 2,084-byte file begins with PAR1, then stores row groups 0, 1 and 2, followed by FileMetaData (728 bytes), its four-byte length and closing PAR1. Row group 0 covers table rows 1 to 8 and has contiguous trip_id, city and fare chunks, one per column. Its fare chunk has two data pages, for rows 1 to 4 and 5 to 8. File bytes PAR1 Group 0 Group 1 Group 2 FileMetaData Tail Byte 0 1,348 2,084 (end) Row group 0 Table rows 1–8 Column chunks trip_id city fare Data pages in fare Page 1 Rows 1–4 Page 2 Rows 5–8 Tail Final 8 bytes Footer length 4 bytes · little-endian 728 Closing magic 4 bytes PAR1
Each row group stores one chunk per column. The reader finds FileMetaData by stepping back 728 bytes from the start of the tail. Byte offsets are from the sample file, and box widths are schematic.[Parquet format]

The metadata comes last “to allow for single pass writing”: a writer streams column chunks out as their buffers fill, keeps each chunk’s offsets and statistics in memory, and writes them all at the end. The price is in the spec’s error-recovery section: “If the file metadata is corrupt, the file is lost.”[README] A writer that crashes before the footer leaves a file with no way in, which is one more reason table formats only publish a file after it is complete.

Quiz The bytesAnswers are scheduled for spaced review on the Review page.

Q1. Which statement matches the spec's hierarchy?

How sure are you?

Q2. What does the spec guarantee about a column chunk's bytes?

How sure are you?

Q3. What are the last 8 bytes of every (unencrypted) Parquet file?

How sure are you?

Q4. Why is the file metadata at the end of the file rather than the start?

How sure are you?

The footer is one Thrift struct, FileMetaData, and “all thrift structures are serialized using the TCompactProtocol”.[README] Its fields, trimmed:[thrift]

struct FileMetaData {
  1: required i32 version
  2: required list<SchemaElement> schema;      // the schema tree, flattened depth-first
  3: required i64 num_rows
  4: required list<RowGroup> row_groups
  5: optional list<KeyValue> key_value_metadata
  6: optional string created_by
  7: optional list<ColumnOrder> column_orders;
  ...
}
  • version. The spec notes that there is “no agreed upon consensus of what constitutes version 2”, so writers should write 1, and readers should accept 1 and 2 interchangeably.[thrift]
  • schema. The tree “flattened to a list by doing a depth-first traversal”, root first. A group element has num_children; a leaf has a physical type, a repetition_type (REQUIRED, OPTIONAL or REPEATED) and optionally a logical type such as STRING.[thrift] The physical types are deliberately few: BOOLEAN, INT32, INT64, INT96 (deprecated), FLOAT, DOUBLE, BYTE_ARRAY and FIXED_LEN_BYTE_ARRAY. A string is a BYTE_ARRAY annotated STRING.[thrift]
  • row_groups. Each RowGroup has its num_rows, byte sizes and one ColumnChunk per leaf column, in schema order.[thrift]
  • key_value_metadata. Free-form. Arrow stores its own schema here as ARROW:schema, which is why Arrow’s version of the sample file has a 1,263-byte footer against this writer’s 728.

Each ColumnChunk wraps a ColumnMetaData, which is what a reader plans with:[thrift]

struct ColumnMetaData {
  1: required Type type
  2: required list<Encoding> encodings
  3: required list<string> path_in_schema
  4: required CompressionCodec codec
  5: required i64 num_values                 // includes nulls
  6: required i64 total_uncompressed_size    // all pages, headers included
  7: required i64 total_compressed_size
  9: required i64 data_page_offset           // first data page
  11: optional i64 dictionary_page_offset    // the dictionary page, if any
  12: optional Statistics statistics;        // min_value, max_value, null_count, ...
  13: optional list<PageEncodingStats> encoding_stats  // pages per (page type, encoding)
  ...
}

The chunk’s bytes run from its first page (the dictionary page if there is one, else data_page_offset) for total_compressed_size bytes. Statistics holds null_count and min_value/max_value under the column’s sort order, encoded as PLAIN “except that variable-length byte arrays do not include a length prefix”; a writer may store shorter bounds that are not actual values.[thrift] Those statistics are how a reader skips whole row groups without reading them (chapter 8 goes further).

Quiz The footerAnswers are scheduled for spaced review on the Review page.

Q1. How are FileMetaData and page headers serialized?

How sure are you?

Q2. How does FileMetaData store the schema tree?

How sure are you?

Q3. A nullable column chunk holds 8 rows, 2 of them null. What is ColumnMetaData.num_values, and how many values are in the data pages' value sections?

How sure are you?

Q4. How is the min_value of a STRING column stored in Statistics?

How sure are you?

Q5. What should a writer put in FileMetaData.version at the pin?

How sure are you?

Pages

Column chunks are “composed of pages written back to back”. Each page starts with a PageHeader that says its type, its sizes and how it is encoded, so a reader “can skip over pages they are not interested in”.[README][thrift]

The first page of the sample file is a data page for trip_id. Its 41-byte header decodes field by field:

15 00                 1: type = DATA_PAGE (0)            delta 1, i32, zigzag(0)
15 40                 2: uncompressed_page_size = 32     zigzag(32) = 64 = 0x40
15 40                 3: compressed_page_size = 32
2c                    5: data_page_header (struct)       delta 2, type 12
   15 08                 1: num_values = 4
   15 00                 2: encoding = PLAIN
   15 06                 3: definition_level_encoding = RLE (3)
   15 06                 4: repetition_level_encoding = RLE
   1c                    5: statistics (struct)
      36 00                 3: null_count = 0            delta 3, i64
      28 08 ec 03 00 …      5: max_value = 1004          binary, 8 bytes little endian
      18 08 e9 03 00 …      6: min_value = 1001
      00                 stop
   00                 stop
00                    stop

After the header come, back to back with no padding, “repetition levels data, definition levels data, encoded values”; uncompressed_page_size covers all three.[README] For a non-nested column the repetition levels are omitted (they would always be 0), and for a required column the definition levels are omitted (they would always be the maximum). So trip_id’s page body is just four PLAIN 8-byte integers: 32 bytes.

The fare column is optional, so its pages carry definition levels: 1 for a value, 0 for a null. In a v1 data page they are RLE/bit-packing hybrid encoded with a 4-byte length prefix.[Encodings] “NULL values are not encoded in the data”: a page of four fares with one null has four definition levels and three values.[README]

Four rows, three stored fares
The first fare page covers 7, 21.25, 12.5, a null. The page stores a header, definition levels 1, 1, 1, 0, and only the 3 non-null values. This flat column has no repetition levels. Logical fare column 7 21.25 12.5 NULL V1 data page Header Definition levels 1 1 1 0 Encoded values 7 · 21.25 · 12.5
The reader advances to the next encoded value for each level 1. At level 0 it emits a null. The diagram shows the decoded contents of each page section.[Parquet nulls]

With dictionary encoding on, each chunk starts with a dictionary page that holds the distinct values in PLAIN encoding, and the data pages hold indices into it (RLE_DICTIONARY: one byte of bit width, then the indices in the hybrid encoding). “The dictionary page must be placed at the first position of the column chunk. At most one dictionary page can be placed in a column chunk.”[README][Encodings] Chapter 6 covers how writers choose and when they fall back.

Quiz PagesAnswers are scheduled for spaced review on the Review page.

Q1. In a data page (v1) of an optional, non-nested column, what follows the page header?

How sure are you?

Q2. Where can a dictionary page appear?

How sure are you?

Reading a file

“Readers are expected to first read the file metadata to find all the column chunks they are interested in. The columns chunks should then be read sequentially.”[README] On an object store that becomes a small number of range GETs (chapter 3):

  1. The tail. parquet-java seeks to size - 8, reads the footer length and checks the magic (PAR1, or PARE for an encrypted footer), then seeks back and reads exactly the footer.[parquet-java] Arrow C++ saves a round trip by reading a speculative tail of footer_read_size bytes, 64 KiB by default, and only issues a second read if the footer is bigger than that.[Arrow][Arrow]
  2. Row group selection. With the footer decoded, the reader drops row groups whose statistics cannot satisfy the filter. This needs no data at all.
  3. Column chunk reads. For each surviving row group, one range per projected column, [start, start + total_compressed_size). Readers often merge ranges that are close together into one request.
  4. Pages. The chunk is decoded page by page from its headers.
Reading one Parquet file from S3
Engineone scan taskParquet readerArrow C++S3events/part-0001.parquet1. open(file, size from LIST or the table log)2. GET Range: last 64 KiB3. GET Range: the rest of the footer (only if it was bigger)4. FileMetaData: schema, row groups, chunk offsets, statistics5. project [fare], filter trip_id >= 10176. GET Range: fare chunk of row group 2 (in parallel with others)7. pages → decode levels and values → batches
  1. open(file, size from LIST or the table log). A table format already knows each file's size (Delta add.size, Iceberg file_size_in_bytes), so an engine can hand it to the reader instead of asking S3 with a HEAD request.
  2. GET Range: last 64 KiB. Arrow reads a speculative tail of footer_read_size bytes (64 KiB by default). The last 8 bytes are the footer length and "PAR1". If the footer fits in the tail, this one request is enough. ParseMetaData ↗
  3. GET Range: the rest of the footer (only if it was bigger). A footer larger than the tail costs a second request. parquet-java always does it in two steps: seek to the length, then read the whole footer. second read ↗
  4. FileMetaData: schema, row groups, chunk offsets, statistics. Decoded from Thrift compact. Everything the reader needs to plan is here: which row groups exist, where each column chunk starts and ends, and min/max/null counts. FileMetaData ↗
  5. project [fare], filter trip_id >= 1017. Row groups whose trip_id statistics cannot match are dropped before any data is read. Only the fare column chunks of the remaining row groups are needed. Statistics ↗
  6. GET Range: fare chunk of row group 2 (in parallel with others). A column chunk is contiguous, so one range per chunk. Readers may merge nearby ranges to save requests. column chunk ↗
  7. pages → decode levels and values → batches. Each page header says its type, size and encoding. A dictionary page, if any, comes first in the chunk. column chunks ↗
Follows the parquet-format README and Arrow C++ 26.0.0's footer read.

For SELECT fare WHERE trip_id >= 1017 on the sample file, row groups 0 and 1 have trip_id in [1001, 1008] and [1009, 1016], so only row group 2 is read, and only its trip_id chunk (to apply the filter to each row) and its fare chunk: four requests (tail, footer, two chunks) instead of the whole file. The explorer below computes this for any choice of columns and filter.

Quiz ReadingAnswers are scheduled for spaced review on the Review page.

Q1. How does Arrow C++ read the footer of a file on S3 by default?

How sure are you?

Q2. SELECT fare WHERE trip_id >= 1017. Row group 0 has trip_id min 1001, max 1008. What does the reader fetch from row group 0?

How sure are you?

Sizing

Two sizes matter, and the spec’s advice is old:[README]

  • Row group size. “Larger row groups allow for larger column chunks which makes it possible to do larger sequential IO. Larger groups also require more buffering in the write path.” The README recommends 512 MB to 1 GB row groups, sized to an HDFS block. A writer buffers a whole row group before it can write it, for every column at once.
  • Page size. “Data pages should be considered indivisible so smaller data pages allow for more fine-grained reading (e.g. single row lookup). Larger page sizes incur less space overhead (less page headers) and potentially less parsing overhead.” The README recommends 8 KB.

What implementations actually default to:

WriterRow groupPage
parquet-java (parquet.block.size, parquet.page.size)128 MiB1 MiB, at most 20,000 rows
Arrow C++ / pyarrow1,048,576 rows1 MiB, at most 20,000 rows
[ParquetWriter] [ParquetProperties] [Arrow properties.h]

The sample file shows why tiny pages are bad: 718 of its 2,084 bytes are page headers, because each page holds only four values. At 1 MiB per page the headers are noise. Row groups trade the other way: more row groups give the reader more chances to skip with statistics and more units of parallelism, but each one adds a set of column chunks and footer entries. Table formats add a third size on top, the target file size, which the compaction chapter covers.

Quiz SizingAnswers are scheduled for spaced review on the Review page.

Q1. What is parquet-java's default row group size (parquet.block.size)?

How sure are you?

Q2. What trade-off does page size control?

How sure are you?

Try it

The explorer writes the 24-row file in your browser with the settings you pick, and reads it back.

  1. Click the footer segment (the long one near the end) and expand row_groups → [0] → columns → [2] → meta_data: that is everything a reader knows about the first fare chunk before reading it.
  2. Click a page header and compare it with the hand-decoded bytes above.
  3. Set rows per page to 2, then 8, and watch the page-header share of the file change.
  4. Turn on dictionary encoding: each chunk now starts with a dictionary page, and city’s values shrink to a bit width and a few bytes of bit-packed indices (chapter 6).
  5. Change the query. With city = 'Denver' every row group is skipped from statistics alone; with no filter every projected chunk is read.
Rows per row group
Rows per page

24 rows of (trip_id INT64, city STRING, fare DOUBLE nullable). 2084 bytes: 3 row groups, 18 pages, footer 728 bytes. Page headers 718 B, levels 36 B, values 590 B.

magicpage headerdictionarydef levelsvaluesfooterfooter lengthfetched by the query below

Byte ranges · click one

  1. 0–3 magic "PAR1"
  2. 4–44 rg 0 · trip_id: data page header
  3. 45–76 rg 0 · trip_id: values (PLAIN)
  4. 77–117 rg 0 · trip_id: data page header
  5. 118–149 rg 0 · trip_id: values (PLAIN)
  6. 150–186 rg 0 · city: data page header
  7. 187–226 rg 0 · city: values (PLAIN)
  8. 227–264 rg 0 · city: data page header
  9. 265–306 rg 0 · city: values (PLAIN)
  10. 307–347 rg 0 · fare: data page header
  11. 348–353 rg 0 · fare: definition levels (hybrid, 4-byte length)
  12. 354–377 rg 0 · fare: values (PLAIN)
  13. 378–418 rg 0 · fare: data page header
  14. 419–424 rg 0 · fare: definition levels (hybrid, 4-byte length)
  15. 425–456 rg 0 · fare: values (PLAIN)
  16. 457–497 rg 1 · trip_id: data page header
  17. 498–529 rg 1 · trip_id: values (PLAIN)
  18. 530–570 rg 1 · trip_id: data page header
  19. 571–602 rg 1 · trip_id: values (PLAIN)
  20. 603–640 rg 1 · city: data page header
  21. 641–681 rg 1 · city: values (PLAIN)
  22. 682–719 rg 1 · city: data page header
  23. 720–760 rg 1 · city: values (PLAIN)
  24. 761–801 rg 1 · fare: data page header
  25. 802–807 rg 1 · fare: definition levels (hybrid, 4-byte length)
  26. 808–831 rg 1 · fare: values (PLAIN)
  27. 832–872 rg 1 · fare: data page header
  28. 873–878 rg 1 · fare: definition levels (hybrid, 4-byte length)
  29. 879–902 rg 1 · fare: values (PLAIN)
  30. 903–943 rg 2 · trip_id: data page header
  31. 944–975 rg 2 · trip_id: values (PLAIN)
  32. 976–1016 rg 2 · trip_id: data page header
  33. 1017–1048 rg 2 · trip_id: values (PLAIN)
  34. 1049–1086 rg 2 · city: data page header
  35. 1087–1128 rg 2 · city: values (PLAIN)
  36. 1129–1165 rg 2 · city: data page header
  37. 1166–1205 rg 2 · city: values (PLAIN)
  38. 1206–1246 rg 2 · fare: data page header
  39. 1247–1252 rg 2 · fare: definition levels (hybrid, 4-byte length)
  40. 1253–1276 rg 2 · fare: values (PLAIN)
  41. 1277–1317 rg 2 · fare: data page header
  42. 1318–1323 rg 2 · fare: definition levels (hybrid, 4-byte length)
  43. 1324–1347 rg 2 · fare: values (PLAIN)
  44. 1348–2075 FileMetaData (Thrift compact)
  45. 2076–2079 footer length = 728 (little endian)
  46. 2080–2083 magic "PAR1"

Select a byte range: the footer and page headers decode to their Thrift fields.

A query against this file

SELECT
WHERE
GET Rangebyteswhy
2076-20838footer length + "PAR1"
1348-2075728FileMetaData (728 bytes)
903-1048146row group 2, trip_id (to evaluate the filter)
1206-1347142row group 2, fare
skipped0row group 0: trip_id in [1001, 1008] cannot match >= 1017
skipped0row group 1: trip_id in [1009, 1016] cannot match >= 1017

1024 of 2084 bytes in 4 requests. Row-group statistics used for skipping: rg 0 [1001, 1008], rg 1 [1009, 1016], rg 2 [1017, 1024]

Summary

  • A Parquet file is PAR1, column chunks grouped by row group, a Thrift-compact FileMetaData, its 4-byte length and PAR1. Column chunks are contiguous; row groups are only a logical grouping.
  • The footer holds the flattened schema, and per column chunk its offsets, sizes, encodings and statistics. It is written last, so writing is single-pass and a file without a footer is unreadable.
  • Pages are the unit of encoding and compression. A v1 data page is header, repetition levels, definition levels, values; nulls exist only as definition levels. A dictionary page, if any, comes first in the chunk.
  • A reader reads the tail, then the footer (Arrow in one 64 KiB read when it fits), drops row groups by statistics, and reads one range per projected column chunk.
  • Writers default to 128 MiB (parquet-java) or 1 Mi rows (Arrow) per row group and 1 MiB pages; the spec’s README recommends larger row groups and much smaller pages.

Concepts in this chapter

Explained here

See how the book's concepts connect →

Primary sources

Review this chapter

This chapter has 15 quiz questions.

Review this chapter's cards → Review all due cards