Skip to content

Enfore consistent schema in to_parquet pyarrow - #9131

Merged
jcrist merged 2 commits into
dask:mainfrom
jcrist:enforce-schema-to-parquet-pyarrow
May 27, 2022
Merged

jcrist merged 2 commits into
dask:mainfrom
jcrist:enforce-schema-to-parquet-pyarrow

Conversation

@jcrist

@jcrist jcrist commented May 26, 2022

Copy link
Copy Markdown
Member

Previously each partition written in to_parquet with
engine="pyarrow" had their schema inferred based solely on data within
that partition alone. This leads to a few common problems:

  • Mismatched inferred schemas between partitions aren't caught, leading
    to issues in downstream consumers. Most readers assume a uniform
    parquet schema across files, which wasn't enforced by default by the
    pyarrow engine.
  • Inference was supported with schema="infer", but:
    • It was expensive, requiring computing at least one partition
      (sometimes more) to get a sample of any object dtypes.
    • Inference here for non-string object dtypes is silently incorrect,
      users should really be explicit here when trying to write more
      complicated schemas containing structured fields.
    • Inference was off by default
  • When writing a _metadata file, differences in per-file schemas would
    result in opaque errors when merging the metadata before writing. This
    error is deep in the arrow codebase, and doesn't provide enough
    information for the user to debug where the schema mismatch occured.
    Turning on schema enforcement by default lets us generate a better
    error message,

This PR changes the schema parameter in the following way:

  • schema="infer" is the default
  • schema inference assumes object fields are strings (common), and makes
    no attempt at more complicated inference. Users with more complicated
    schemas should specify those explicitly. This means that schema
    inference is now cheap.
  • A better error message is generated for mismatched partition dtypes in
    to_parquet for the pyarrow engine. The fastparquet engine already
    does this.

Fixes #8619, Fixes #8832

Previously each partition written in `to_parquet` with
`engine="pyarrow"` had their schema inferred based solely on data within
that partition alone. This leads to a few common problems:

- Mismatched inferred schemas between partitions aren't caught, leading
  to issues in downstream consumers. Most readers assume a uniform
  parquet schema across files, which wasn't enforced by default by the
  pyarrow engine.
- Inference was supported with `schema="infer"`, but:
    - It was expensive, requiring computing at least one partition
      (sometimes more) to get a sample of any `object` dtypes.
    - Inference here for non-string object dtypes is silently incorrect,
      users should really be explicit here when trying to write more
      complicated schemas containing structured fields.
    - Inference was off by default
- When writing a `_metadata` file, differences in per-file schemas would
  result in opaque errors when merging the metadata before writing. This
  error is deep in the `arrow` codebase, and doesn't provide enough
  information for the user to debug where the schema mismatch occured.
  Turning on schema enforcement by default lets us generate a better
  error message,

This PR changes the `schema` parameter in the following way:
- `schema="infer"` is the default
- schema inference assumes object fields are strings (common), and makes
  no attempt at more complicated inference. Users with more complicated
  schemas should specify those explicitly. This means that schema
  inference is now cheap.
- A better error message is generated for mismatched partition dtypes in
  `to_parquet` for the `pyarrow` engine. The fastparquet engine already
  does this.
@jcrist

jcrist commented May 26, 2022

Copy link
Copy Markdown
Member Author

cc @rjzamora, @ian-r-rose

@ian-r-rose
ian-r-rose self-requested a review May 26, 2022 16:53
@jcrist

jcrist commented May 26, 2022

Copy link
Copy Markdown
Member Author

Test failures are unrelated.

@rjzamora
rjzamora self-requested a review May 26, 2022 19:18

@rjzamora rjzamora left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working through this @jcrist !

I definitely +1 on this change. The previous schema=“infer” option was a bit of a misnomer (schema=“sample” would have been more accurate), and it didn’t do much (anything?) to prevent schema-mismatch errors. I think it makes sense to use the non-empty metadata by default, and simply do a better job of pointing the user towards a “proper” schema= argument when this goes wrong.

@ian-r-rose ian-r-rose left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jcrist, this looks good to me. Any time we can remove eager computation, I'm in favor.

Comment thread dask/dataframe/io/parquet/arrow.py Resolved
Comment thread dask/dataframe/io/parquet/arrow.py Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty partitions failure Improve to_parquet "Schemas are inconsistent" error messages

3 participants