Sub topic 4
What partitioning means: values in directory names
This chapter assumes you know basic Python, can read a table with rows and columns (as in pandas or SQL), and know that Parquet is a columnar file format. You do not need to have used pyarrow.dataset yet.
The idea
Partitioning splits one logical table into many files. It uses the values of one or more columns to decide which file a row goes into. Each distinct value, or combination of values, gets its own directory. The directory name records the value.
Suppose a sales table has year, country and amount columns. With Hive-style partitioning on year and country, the files look like this:
sales/
year=2023/
country=DE/part-0.parquet
country=FR/part-0.parquet
year=2024/
country=DE/part-0.parquet
country=FR/part-0.parquetEach directory level matches one partition column, in the order you declare them. The path sales/year=2024/country=DE/part-0.parquet tells you that every row in that file has year 2024 and country "DE".
Why put values in paths
A reader can learn a lot from a path without opening the file. If a query asks for year == 2024, the reader can skip every year=2023 directory. It never lists or opens those files. This is called partition pruning, and a later chapter covers it in detail.
The path also stores the value only once. The partition columns are normally not written inside the Parquet files, so files get a little smaller. When you read the dataset, PyArrow rebuilds those columns from the paths.
A working example
This code writes the four rows as a partitioned dataset. It then prints the paths that were created.
import pyarrow as pa
import pyarrow.dataset as ds
table = pa.table({
"year": [2023, 2023, 2024, 2024],
"country": ["DE", "FR", "DE", "FR"],
"amount": [10.5, 20.0, 7.25, 3.0],
})
part = ds.partitioning(
pa.schema([("year", pa.int64()), ("country", pa.string())]),
flavor="hive",
)
ds.write_dataset(
table,
"sales",
format="parquet",
partitioning=part,
existing_data_behavior="overwrite_or_ignore",
)
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
print(sorted(dataset.files))The printed list has four paths, such as sales/year=2023/country=DE/part-0.parquet. The file name may differ between PyArrow versions, but the directory structure is the same.
What partitioning does not do
Partitioning only helps when queries filter on the partition columns. A query that filters on amount still has to open every file.
Each distinct value creates a directory, and often at least one file. A column with millions of distinct values, such as a user ID, produces millions of tiny files. That is slow to list and slow to read. Chapter 15 covers how to choose partition keys.
Exercise
A dataset is partitioned by year and then month. Write the path of a file that holds rows for March 2024 under a root directory called events, and say what a query for month == 3 across all years has to do.
Show answer
One valid path is events/year=2024/month=3/part-0.parquet. The query can skip nothing at the year level, because month is nested inside year. It must look inside every year=... directory, but within each one it only needs the month=3 subdirectory, so it still skips the other months.