Sub topic 17
Common pitfalls and how to debug them
This chapter assumes you can write and read a Hive-partitioned Parquet dataset with ds.write_dataset and ds.dataset, and that you know what filters and scanners do. It covers the errors you are most likely to hit and how to find their cause.
The working model for debugging
A dataset is a list of fragments (files). Each fragment carries a partition expression built from its path. A filter is checked against that expression first, and then against Parquet statistics. Most problems come from one of three places: the path, the partition schema, or the filter.
Common pitfalls
- Forgetting
partitioning="hive"when reading. Without it,yearandcountryare not in the schema, and filtering on them raises an error. The data is still read, but the partition columns are missing. - Reading directory partitioning without a schema. Paths like
sales_dir/2023/hold only values. PyArrow cannot guess the column name, so you must pass a partitioning schema, as in the directory example earlier. - Type surprises. Hive discovery infers types from the path text.
year=2023becomesint32, and a value such ascountry=007may be read as an integer and lose its zeros. Declare the schema explicitly for such keys. - Too many small files. A high-cardinality key such as
user_idcreates one directory and one tiny file per value. Listing and opening these files costs more than reading them. Pick a key with moderate cardinality, or lower the number of partitions. - Stale data after a rewrite. The default
existing_data_behavioris"error"when the target directory has data."overwrite_or_ignore"leaves old files in place, so a rerun with different row counts can leave duplicates or old rows. Use"delete_matching"to replace only the partitions you write. - Filter type mismatch. Comparing a string field to an integer, such as
ds.field("year") == "2024", fails or matches nothing. Match the Python value to the field type.
Inspecting a dataset
Print what PyArrow discovered before you blame the filter.
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)
flt = ds.field("year") == 2024
kept = [f.path for f in dataset.get_fragments(filter=flt)]
print(kept)get_fragments(filter=...) shows which files survive pruning. If kept is empty but you expect results, compare the printed partition expressions with your filter. If it holds too many files, the filter probably uses a column that is not a partition column, so only Parquet statistics can prune it.
Exercise
You read sales with ds.dataset("sales", format="parquet") and ds.field("year") == 2024 fails because the field is unknown. What is the cause and the fix?
Show answer
The partitioning argument is missing, so year is not part of the schema. Pass partitioning="hive" to ds.dataset, then check dataset.schema and the fragments' partition_expression values.