Course contents 07 / 35

07 / 35 · Files and Tables

File formats: Avro and Parquet

Compare decoding complete records with reading selected column chunks, and trace how readers handle schema changes.

A sales export has ten fields per order. The checkout importer reads every field. An analyst sums amounts for one shop and needs only the shop ID and the amount. Both read the same logical records. The byte layout determines how much they have to transfer and decode.

Encode one record at a time#

Serialization maps typed values to bytes. Avro uses a writer schema to define those values and their order. For a record with order_id, shop_id and amount_cents, binary encoding writes the encoded fields in schema order. It does not repeat each field name in the binary record. An Avro object container file stores the writer schema in its header, then stores records in blocks that may be compressed. Synchronization markers delimit blocks and help a reader split a file for parallel work. (Apache Avro 1.12.0, Binary Encoding and Object Container Files)

A newer reader may expect a channel field that old records lack. In Avro schema resolution, the reader uses the writer schema to interpret the old bytes and maps the fields to the requested reader schema. If the reader schema supplies a default such as "unknown", it can supply that value for an absent writer field. Without an applicable default, the missing required reader field is an error. A reader default does not change the old file or let a writer omit a field of its own schema. (Apache Avro 1.12.0, Schema Resolution)

With a record layout, field values for each order are adjacent. It is a useful fit when the consumer decodes the whole order. A reader can avoid returning unneeded fields to its application, while compressed Avro record blocks still do not provide a separate contiguous byte range per column.

Read columns in each row group#

Parquet divides a file into row groups. Within a row group, values of one column occupy a column chunk. A chunk contains pages, where values are encoded and may be compressed. A reader loads file metadata from the footer to locate chunks. For a sum filtered by shop, the reader needs both shop_id for the predicate and amount_cents for the aggregate. The other eight columns need not be read. (Apache Parquet, File Format)

Figure

The sum needs two chunks per row group

The sum needs two chunks per row groupIn each row group we read shop_id and amount_cents. The other columns are shown as one block.Row group 1shop_idamount_cents8 other fieldsRow group 2shop_idamount_cents8 other fields
In each row group we read shop_id and amount_cents. The other columns are shown as one block.

Suppose a toy file contains 300 MiB of column payload, equally divided among ten columns. Reading two columns across all groups needs 300⋅2/10=60300\cdot2/10=60 MiB of payload, excluding footer and request overhead. The savings come from column projection. The calculation does not assume that any row group can be skipped. Real chunks differ in size, and a filter on an extra column can increase the amount read.

Columns group values of the same field, so an encoding can exploit repetition or small differences. For example, dictionary encoding can store repeated shop identifiers as codes into a dictionary. Compression can then reduce the encoded bytes further. Column order alone does not give a fixed compression ratio. With high-cardinality data, an unsuitable encoding can give little benefit, and reading every field removes the projection advantage. (Apache Parquet, Encodings and Compression)

Preserve nested record boundaries#

An order can have a list of items. A column of item prices alone does not reveal where one order ends and the next begins. Parquet’s nested encoding carries repetition levels to describe where a repeated part of a field path starts again and definition levels to describe how far that path exists for a value. A reader can reconstruct the needed record structure from those signals. Dremel describes this representation and its use for reading selected fields of nested records. (Apache Parquet, Nested Encoding, Melnik et al., §§3–4)

The format gives a reader a layout for one file. It does not specify which of many files belong to the dataset or how they should be split by date.

Next#

A reader can skip columns inside a file. Dataset layout and compaction applies the same idea at larger scales by arranging whole files and their internal ranges around common filters.

References#

  1. Apache Avro. Specification, 1.12.0. Binary record encoding, container blocks, writer schemas and reader defaults.
  2. Apache Parquet. File Format, Encodings, Compression and Nested Encoding. File footer, row groups, chunks, pages and levels.
  3. Sergey Melnik et al. Dremel: Interactive Analysis of Web-Scale Datasets. PVLDB 3(1–2), 2010, §§3–4. Columnar representation and assembly of nested records.