Sub topic 4
What partitioning means: values in directory names
This chapter assumes you can read basic Python and know what a table with columns is. It also assumes you know a Parquet file is a columnar file on disk. Earlier chapters covered Arrow tables, Parquet row groups, and the dataset and fragment abstraction. Here the focus is one idea: a column's value can be stored in a directory name instead of inside the files.
The core idea
A partitioned dataset splits one logical table into many files. It chooses one or more columns, the partition columns, and places each row in a directory determined by that row's values in those columns.
Suppose a sales table has year, country, and amount. If you partition by year and country, every row with year = 2023 and country = "DE" goes into the same directory. That directory's name records both values.
The partition values live in the path, and the files hold only the remaining columns. This has two consequences:
- A reader can learn a file's
yearandcountryfrom its path alone, without opening the file. - A reader that wants only
year = 2024can skip every other directory without touching it.
The second consequence is why partitioning exists. Later chapters cover the filtering mechanics. Here you need to see how the layout works.
See it on disk
This script writes the small table from the diagram and lists every file it created. Hive-style naming (name=value) is one of two directory styles. The next chapter compares them, so treat the flavor argument as given for now.
from pathlib import Path
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",
)
for path in sorted(Path("sales").rglob("*.parquet")):
print(path.as_posix())The output is:
sales/year=2023/country=DE/part-0.parquet
sales/year=2023/country=FR/part-0.parquet
sales/year=2024/country=DE/part-0.parquet
sales/year=2024/country=FR/part-0.parquetEach of the four rows landed in its own directory because each has a different (year, country) pair.
What is inside the files
The partition columns are removed from the file contents when you write. You can check this by opening one leaf file directly and looking at its schema.
import pyarrow.parquet as pq
pf = pq.ParquetFile("sales/year=2023/country=DE/part-0.parquet")
print(pf.schema_arrow)
print(pf.read().to_pydict())The schema contains only amount. The file does not know it belongs to 2023 or to Germany. That information exists only in the directories above it.
This also saves space. A column that is constant within a directory would otherwise be repeated in every row of every file. Storing it once in the path removes that repetition.
How a reader gets the columns back
When you open the dataset with partition discovery turned on, PyArrow parses each file's path and adds the partition columns to the dataset schema. Each file is a fragment, and each fragment carries the values parsed from its path.
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
print(dataset.schema)
for fragment in dataset.get_fragments():
print(fragment.path, fragment.partition_expression)The schema now lists amount, year, and country, even though only amount is stored in the files. The partition_expression of each fragment is a filter expression such as (year == 2023) and (country == "DE"). It records what the path says about every row in that fragment. When you read the data, PyArrow fills in those values as columns, so the table looks like the original.
The partition columns come back with types that PyArrow infers from the path text. For year=2023, it infers an integer. The inferred type can differ from the type you wrote with. For example, you wrote int64, but inference gives int32. Declaring the partition schema explicitly fixes this, and a later chapter covers it.
Why this layout helps queries
Each fragment has a known partition expression before any file is opened. A query for year == 2024 can compare that condition with every fragment's expression and discard fragments that cannot match. That check reads no file data, only paths. This is partition pruning, and the chapter after next builds on it.
The table below shows how a few paths map to values and which queries need them.
| Path | `year` | `country` | Needed by `year == 2024`? |
|---|---|---|---|
| `sales/year=2023/country=DE/part-0.parquet` | 2023 | DE | No |
| `sales/year=2024/country=DE/part-0.parquet` | 2024 | DE | Yes |
| `sales/year=2024/country=FR/part-0.parquet` | 2024 | FR | Yes |
Pruning only helps when queries filter on the partition columns. A query filtering on amount > 10 cannot skip any directory, because amount is not in any path. It must open every file.
Order and depth
Each partition column adds one directory level, and the order of the columns in the partitioning schema sets the nesting. With ("year", "country"), years are the top level. With ("country", "year"), countries are the top level. The files hold the same rows either way, and the queries return the same results. The order affects how the tree looks and how easy it is to browse or delete a whole group. For example, with year first, removing all of 2023 means deleting one directory.
Directory depth also affects the number of files. Every distinct combination of partition values that appears in the data produces at least one file. Two years and two countries give four files here. A thousand dates and fifty countries could give up to 50,000 directories, each holding a tiny file. This is the small-files problem, and the chapter on choosing partition keys covers it. For now, keep in mind that the number of directories is multiplicative across partition columns.
Nulls and awkward values
A partition value must be written into a directory name, which creates some edge cases:
- Null values need a placeholder. The Hive flavor uses the directory name
__HIVE_DEFAULT_PARTITION__for them by default. - Values are text in the path, so special characters such as
/or=need escaping. PyArrow URL-encodes values when it writes Hive-style paths. - Only columns with simple types make good partition columns. Strings and integers work well. Floating-point values and long free-text strings make poor directory names.
Summary of the model
- A partitioned dataset is a directory tree where path segments encode column values.
- Partition columns are stored in the paths, not in the files.
- Opening the dataset with partition discovery rebuilds those columns from the paths.
- Each fragment's path gives it a partition expression, which lets a reader skip fragments without opening them.
- The number of directories grows with the number of distinct value combinations.
Exercise
You write a table partitioned by ("region", "month") and then open one leaf file directly with pq.ParquetFile. A teammate expects to see region and month in its schema. What do you actually see, and where do those values live? Also, what happens to the dataset's contents if someone moves a file from month=01/ to month=02/?
Show answer
The file's schema contains only the non-partition columns. The region and month values live in the directory names, for example region=EU/month=01/. When the dataset is opened with partition discovery, those values are parsed from the path and added as columns. If a file is moved from month=01/ to month=02/, every row in it is read as having month = 02, because the path is the only record of the value. The file contents do not change, but the data a reader sees does.
Exercise
A dataset is partitioned by ("year", "country"). Which of these queries can skip directories using the layout alone: country == "FR", amount > 100, or year == 2023?
Show answer
country == "FR" and year == 2023 can skip directories, because both columns are encoded in the paths. amount > 100 cannot, because amount is stored inside the files and not in any path, so every file must be opened.