Skip to content

Fixing schema handling in arrow-parquet - #5307

Merged
TomAugspurger merged 6 commits into
dask:masterfrom
rjzamora:schema-fix
Aug 28, 2019
Merged

TomAugspurger merged 6 commits into
dask:masterfrom
rjzamora:schema-fix

Conversation

@rjzamora

Copy link
Copy Markdown
Member

Addresses the arrow + parquet "schema" bug discussed in issue#5252.

When a dask-dataframe partition includes a column with all values missing, the corresponding parquet file for that partition (when written) will not include schema information for the correct column type (because there is no data). This leads pyarrow to throw a schema-validation error in ParquetDataset. This can be avoided by setting (by default) "validate_schema"=False in the special case that a shared "_metadata" file is available.

If the user actually want the schema to be vaidated for each file/partition, they can still pass dataset={"validate_schema": True} as a kwarg to read_parquet.

Note that this PR also fixes a small (but important) bug in _determine_dataset_parts (paths vs allpaths mistake)

cc @bluecoconut

  • Tests added / passed
  • Passes black dask / flake8 dask

@jrbourbeau

Copy link
Copy Markdown
Member

@TomAugspurger might you have a chance to look at this?

Comment thread dask/dataframe/io/parquet/arrow.py Resolved
@TomAugspurger

Copy link
Copy Markdown
Member

@rjzamora does this close #5252? I haven't followed it closely, but I would hope that empty partitions can be handled the same w.r.t. schema validation as non-empty partitions (but this is fine as a workaround).

@rjzamora

Copy link
Copy Markdown
Member Author

@TomAugspurger - Thanks for reviewing this. This is meant to address #5252, but I am actually still unsure if we should really be changing the default setting for validate_schema. Previously, the "correct" way to deal with the schema validation error was to explicitely specify a schema in the call to to_parquet (this was essentially the point/goal of #5034 )

I would hope that empty partitions can be handled the same w.r.t. schema validation as non-empty partitions

In pyarrow, the local file metadata does not seem to include a schema with the "correct" type when there is no data in the corresponding column (unless you explicitely specify the schema during the write). Note that @bluecoconut suggested that we try to detect/specify the correct arrow schema in a more automated way (during the write). This may be a better approach than the "validate_schema" change used here.

@rjzamora

rjzamora commented Aug 28, 2019 •

Copy link
Copy Markdown
Member Author

I spent a bit of time implementing a fix for the null-schema issue on the to_parquet end. However, the current approach actually seems a bit "cleaner" to me (avoiding schema validation for "safe" cases).

The central issue here is that the dataframe for each partition is used to define the pyarrow schema for each partition. This is fine for most datatypes, but causes an issue when there is an empty column with dtype="object". In this case, pyarrow will define the type as null, rather than "string", because there is no data available to infer what "object" means. Using df._metato define the schema leads to the same problem, because it is empty. The only way to automatically avoid this problem before writing is to (1) generate a schema from df, and then (2) change the type to pa.string() in any location where the type is pa.null().

Overall, I think the current "validate_schema" solution is a better workaround than changing null to string on write (although I am still open). Perhaps the best solution is to raise an issue in pyarrow to suggest that the schema-validation step should ignore empty columns.

@rjzamora

Copy link
Copy Markdown
Member Author

@TomAugspurger The current state of the PR does not close all of #5252, but it should close the "schema" component (there is also a repartition-failure discussed there).

Overall, I think the current implementation is a reasonable workaround for the schema-validation failure.

@TomAugspurger

Copy link
Copy Markdown
Member

This is fine for most datatypes, but causes an issue when there is an empty column with dtype="object". In this case, pyarrow will define the type as null, rather than "string", because there is no data available to infer what "object" means.

pandas-dev/pandas#27949 may interest you :) Since that's likely to land in pandas 1.0 (and hopefully pyarrow can use it shortly after), putting in workarounds here doesn't seem worth it.

The changes here look nice. Thanks @rjzamora.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants