Skip to content

pq_glue

For when to give write_parquet a column_encoding, and what it changes, see Column encodings.

Glue Parquet files together, and write Arrow data to Parquet and read it back.

concat copies encoded row groups without decoding them (concat_many for many concatenations at once); write_parquet writes Arrow data with a choice of engines; read_parquet reads a file's row groups in parallel.

__version__ module-attribute

__version__: str

concat

concat(sources: Sequence[str | PathLike[str]], dst: str | PathLike[str]) -> int

Concatenate Parquet files that share one schema into one file.

Each input's row groups are copied to dst as they are encoded (pages, dictionaries, statistics), in input order, without being decoded. The output has the first input's schema and key-value metadata, and the inputs' column orders, page indexes and bloom filters.

Parameters:

Name Type Description Default
sources Sequence[str | PathLike[str]]

The Parquet files to concatenate, in order. Each must have the first one's Parquet schema, key-value metadata and column orders.

required
dst str | PathLike[str]

The file to write, created or truncated. It is removed if the copy fails part way, and must not be one of sources.

required

Returns:

Type Description
int

The number of rows written.

Raises:

Type Description
ValueError

If there are no sources, dst is one of them, a source is not a Parquet file, or the sources differ in schema, key-value metadata or column orders.

OSError

If a source cannot be opened or dst cannot be written.

concat_many

concat_many(
    jobs: Sequence[tuple[Sequence[str | PathLike[str]], str | PathLike[str]]],
) -> list[int]

Concatenate many sets of Parquet files at once, each into its own file.

Each job, (sources, dst), is a concat(sources, dst), with the same output; the jobs run in parallel, on a thread per CPU, with the GIL released for the whole batch. For many small concatenations (each a few files), whose time goes on opening files and writing footers more than on bytes copied, one call is quicker than a concat per job.

Parameters:

Name Type Description Default
jobs Sequence[tuple[Sequence[str | PathLike[str]], str | PathLike[str]]]

(sources, dst) tuples, as concat takes them. No two jobs may have the same dst, nor one job's dst be another's source.

required

Returns:

Type Description
list[int]

The number of rows written for each job, in the jobs' order.

Raises:

Type Description
ValueError

For the first job (in the jobs' order) that fails as concat raises ValueError, naming its dst.

OSError

For the first job that fails as concat raises OSError, naming its dst.

Notes

Every job runs, whether or not another fails: a job that fails leaves no dst, as concat leaves none, and the others' are written.

read_parquet

read_parquet(path: str | PathLike[str], row_groups: Sequence[int] | None = None)

Read a Parquet file to a pyarrow.Table, each row group decoded in parallel.

The table has the file's Arrow schema (from its ARROW:schema metadata) and one chunk per row group read, in the order of row_groups (all of them, in order, if None). The values are those pyarrow.parquet.ParquetFile.read and read_row_groups give; the file is read into memory once, then decoded by arrow-rs on every core, without the GIL.

Raises:

Type Description
ValueError

If the file is not Parquet, or a row group asked for is not in it.

OSError

If the file cannot be read.

write_parquet

write_parquet(
    data: object,
    path: str | PathLike[str],
    *,
    engine: Engine = "parallel",
    max_workers: int | None = None,
    row_group_size: int | None = None,
    compression: str = "zstd",
    compression_level: int | None = None,
    sorting_columns: Sequence[SortingColumn] | None = None,
    content_defined_chunking: bool | Mapping[str, int] | None = None,
    spec_names: bool = True,
    adaptive_dictionary: bool | None = None,
    column_encoding: Mapping[str, str] | None = None,
) -> int

Write Arrow data to a Parquet file with the engine asked for; returns the rows written.

Every engine writes the same values, in the same order, with the same Arrow schema, row groups, compression, sorting columns and content-defined chunking. Their bytes differ (page boundaries, dictionaries, the footer's created_by and format version): see the documentation's engines page. An option an engine lacks raises ValueError.

Parameters:

Name Type Description Default
data object

Any object with an __arrow_c_stream__ method: a pyarrow table or record batch reader, a Polars data frame, ...

required
path str | PathLike[str]

The file to write, created or truncated.

required
engine Engine
  • "native": arrow-rs, polars-genson's writer, on one thread.
  • "parallel": the same writer, on max_workers threads (by default one per CPU): each row group's leaf columns in parallel, or, with content-defined chunking, whole row groups in parallel, glued in order. The same bytes as "native", sooner, holding a row group in flight per thread.
  • "pyarrow": pyarrow's writer (pyarrow needed: pq-glue[pyarrow]), on one thread, or with max_workers above 1 a row group per thread, glued in order.
'parallel'
max_workers int | None

Threads that encode at once; by default 1 for "native" and "pyarrow", one per CPU for "parallel".

None
row_group_size int | None

Rows per row group; 1,048,576 if None.

None
compression str

"zstd" (the default), "snappy" or "none".

'zstd'
compression_level int | None

The zstd level; 1 if None.

None
sorting_columns Sequence[SortingColumn] | None

The sorting columns each row group declares: each a leaf column's path ("id", "a.b") or index, or a tuple of that, descending and nulls_first. The rows must already be in that order.

None
content_defined_chunking bool | Mapping[str, int] | None

Content-defined chunking of data pages, for deduplicating storage: True for the defaults (256 KiB minimum, 1 MiB maximum chunk, normalisation level 0), or a dict of min_chunk_size, max_chunk_size, norm_level. Chunking starts afresh at each row group in every engine.

None
spec_names bool

List and map groups take the Parquet specification's names (list, element, key_value).

True
adaptive_dictionary bool | None

("native", "parallel") Leaf columns whose sampled values are more than three quarters distinct are not dictionary encoded; True if None. "pyarrow" encodes every leaf with a dictionary, and rejects True.

None
column_encoding Mapping[str, str] | None

Leaf columns written in an encoding of their own, with no dictionary: a dict of each leaf's path in the Parquet schema ("id", "a.b", "xs.list.element") to its encoding's name, e.g. {"statement_id": "DELTA_BYTE_ARRAY"} for strings that share a prefix with the value before. Only the file's bytes change: every engine reads back the same values, schema and statistics. The encodings: "PLAIN", "RLE" (booleans), "DELTA_BINARY_PACKED" (32 and 64-bit integers), "DELTA_LENGTH_BYTE_ARRAY" (strings and binary), "DELTA_BYTE_ARRAY" (strings and binary, fixed-length too), "BYTE_STREAM_SPLIT" (floats, integers, fixed-length binary); "native" and "parallel" also take "ALP" (floats). A dictionary is adaptive_dictionary's to decide. An unknown column, or an encoding its type does not take, raises ValueError.

None

Returns:

Type Description
int

The number of rows written.