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.
concat
¶
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 |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of rows written. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If there are no sources, |
OSError
|
If a source cannot be opened or |
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]]]
|
|
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 |
OSError
|
For the first job that fails as |
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 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 |
required |
path
|
str | PathLike[str]
|
The file to write, created or truncated. |
required |
engine
|
Engine
|
|
'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 ( |
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 |
None
|
spec_names
|
bool
|
List and map groups take the Parquet specification's names ( |
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 ( |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The number of rows written. |