Sub topic 5
Hive versus directory partitioning
This chapter assumes you can write a partitioned dataset with ds.write_dataset and that you know partition values are stored in directory names. Here you will compare the two layouts PyArrow supports for those names, and see how each affects reading, writing, and interoperability.
Two ways to put values in a path
A partitioned dataset stores some column values in the path instead of inside the files. PyArrow offers two main flavors.
- Hive partitioning writes each directory level as
key=value, for exampleyear=2023/country=DE/part-0.parquet. - Directory partitioning writes only the value, for example
2023/DE/part-0.parquet. The column names are not in the path.
Both flavors produce the same logical table. The difference is where the column names come from. In Hive layout, the path supplies them. In directory layout, you supply them when you open the dataset.
Writing both layouts from one table
The only change is the flavor argument of ds.partitioning. Passing flavor="hive" selects Hive. Passing flavor=None, the default, selects directory partitioning.
import tempfile
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],
})
schema = pa.schema([("year", pa.int64()), ("country", pa.string())])
root = Path(tempfile.mkdtemp())
for flavor in ("hive", None):
target = root / (flavor or "directory")
ds.write_dataset(
table,
str(target),
format="parquet",
partitioning=ds.partitioning(schema, flavor=flavor),
)
files = sorted(p.relative_to(target).as_posix() for p in target.rglob("*.parquet"))
print(flavor, files)The Hive run lists year=2023/country=DE/part-0.parquet and three similar paths. The directory run lists 2023/DE/part-0.parquet and three similar paths. In both cases, the order of fields in the schema sets the nesting order. Putting year first makes it the top-level directory.
The partition columns are removed from the file contents on write. The Parquet files hold only amount. The path is the only place year and country are stored.
Reading: key-based versus position-based
The two flavors differ most when you open the dataset.
hive = ds.dataset(root / "hive", format="parquet", partitioning="hive")
print(hive.schema)
directory = ds.dataset(
root / "directory",
format="parquet",
partitioning=["year", "country"],
)
print(directory.schema)For Hive, partitioning="hive" is enough. PyArrow scans paths for key=value segments and infers a type for each key. Integer values are inferred as int32, not int64, so the inferred schema can differ from the one you wrote with.
For directory partitioning, PyArrow cannot guess the names. Passing a list of names gives it positions: the first segment is year, the second is country. Types are still inferred. To control types, pass a full schema instead of a list of names:
typed = ds.partitioning(
pa.schema([("year", pa.int16()), ("country", pa.string())])
)
dataset = ds.dataset(root / "directory", format="parquet", partitioning=typed)
print(dataset.schema)Both datasets answer filters the same way:
flt = (ds.field("year") == 2024) & (ds.field("country") == "DE")
print(hive.to_table(filter=flt).num_rows, directory.to_table(filter=flt).num_rows)Each prints one row. Filters on partition columns select directories in both layouts, so the choice does not change pruning behavior. Chapters later in the tutorial cover pruning in detail.
Differences that matter in practice
| Aspect | Hive (`key=value`) | Directory (`value`) |
|---|---|---|
| Column names in path | Yes | No |
| Parsing | By key, so level order does not matter when reading | By position, so schema order must match the paths |
| Information needed to read | Nothing, since names and types can be inferred | Field names, or a schema |
| Null values | Written as `__HIVE_DEFAULT_PARTITION__` by default | No equivalent marker, so clean nulls before writing |
| Path length | Longer | Shorter |
| Tool support | Spark, Hive, DuckDB, and most query engines recognize it | Little outside PyArrow-style readers |
Self-describing paths
A Hive path can be understood without any other information. You can list a bucket and see what each directory means. A directory-partitioned dataset needs documentation or code to say that the first level is a year. If the person who knew the layout leaves, the data becomes harder to use.
Order dependence
In directory layout, reordering the schema silently changes the meaning. Opening 2023/DE with fields ordered country, year does not fail for strings. It produces a country of "2023" and a year of "DE" (or a parse error if year is typed as an integer). The result may look plausible until a filter returns nothing.
Special characters
By default, PyArrow URI-encodes segment values. A country value of "a/b" is written as a%2Fb, so it does not create an extra directory level. The reader decodes it again. Other tools may not decode it the same way, which is another reason to avoid free-text partition columns.
Choosing a flavor
Use Hive by default. It is the layout most engines understand, it survives being copied or inspected by hand, and it needs no extra reader configuration.
Directory partitioning is reasonable when all of these hold:
- You control every reader and writer of the data.
- The layout is fixed and documented, for example
YYYY/MM/DDfor logs. - Path length matters, or an existing system already produces the paths.
If you inherit directory-style data, open it with an explicit schema and consider rewriting it as Hive once with ds.write_dataset, reading from the old dataset and writing to a new location.
Exercise
A colleague opens a dataset whose files are at 2023/DE/part-0.parquet using ds.dataset(path, format="parquet", partitioning="hive"). No error appears, but filtering on year fails. Why, and how do you fix it?
Show answer
The paths contain no key=value segments, so Hive discovery finds no partition fields and the dataset has no year column. The fix is to give the positional names or a schema, for example partitioning=["year", "country"], or ds.partitioning(pa.schema([("year", pa.int16()), ("country", pa.string())])) to also set the types.
Exercise
You write a table with ds.partitioning(schema, flavor=None) where the schema lists country before year. What does the first-level directory contain, and which column is the top-level directory when the schema is reversed?
Show answer
With country first, the first-level directories are country values such as DE and FR, and year values are nested below. With the schema reversed, the first-level directories are years such as 2023. The schema order sets the nesting order.