Skip to content

Empty partitions failure #8832

Closed
Closed

Description

@rhjmoore

The to_parquet function fails using the pyarrow engine if you have a dask dataframe with an empty partition. It may require a certain mix of data types to trigger the defect.

Support you have a csv file:
field1, field2
a,5

you then read it in, repartition to 2 partitions and then try to write as parquet.

    import dask.dataframe as dd
    ddf = dd.read_parquet(<path to csv>)
    ddf2 = ddf.repartition(npartitions=ddf.napartitions*2)
    ddf2.to_parquet(<path>, engine='pyarrow')

This will fail with a "inconsistent schema" error. I can resolve this error by using the following function to first remove the empty dataframes; the function comes from MRocklin in 2017...

      def cull_empty_partitions(ddf):
          ll = list(ddf.map_partitions(len).compute())
          df_delayed = ddf.to_delayed()
          df_delayed_new = list()
          pempty = None
          for ix, n in enumerate(ll):
              if 0 == n:
                  pempty = ddf.get_partition(ix)
              else:
                  df_delayed_new.append(df_delayed[ix])
          if pempty is not None:
              ddf = dd.from_delayed(df_delayed_new, meta=pempty)
          return ddf

dask version: 2021.12.0
python version: 3.9
OS version: MacOS and Windows tested
install method: conda

Activity

  1. added
    needs triage
    on Mar 23, 2022
  2. rjzamora commented on Mar 23, 2022

    @rjzamora
    Member

    When I run the reproducer (with typos fixed the first read_parquet changed to read_csv), I get:

    `RuntimeError: Schemas are inconsistent, try using `to_parquet(..., schema="infer")`, or pass an explicit pyarrow schema. Such as `to_parquet(..., schema={"column1": pa.string()})`
    

    If I follow the advice in the warning, and use ddf2.to_parquet(<path>, engine='pyarrow') ddf2.to_parquet(<path>, engine='pyarrow', schema='infer'), everything runs fine for me. Are you not getting this warning, or did it not resolve your issue?

  3. added
    needs info
    and removed
    needs triage
    on Mar 23, 2022
  4. rhjmoore commented on Mar 23, 2022

    @rhjmoore
    ContributorAuthor

    Apologies for whatever the typos were!
    Yes, you can follow the suggestion; I assume you used ddf2.to_parquet(, engine='pyarrow', schema='infer')?
    This might work, but should not be necessary; it is not necessary if you do not have an empty partition. I believe it is only occurring because of the empty partition, and this would therefore be unintended behaviour.

    MRocklin asked for reports of functions that do not work on data dataframes that contain empty partitions.

  5. rjzamora commented on Mar 23, 2022

    @rjzamora
    Member

    I assume you used ddf2.to_parquet(, engine='pyarrow', schema='infer')?

    Yes. My mistake!

    This might work, but should not be necessary; it is not necessary if you do not have an empty partition. I believe it is only occurring because of the empty partition, and this would therefore be unintended behaviour.

    I agree that it would be ideal for this to work with an empty partition, but the problem has come up before and the only way to "fix" it is to change the default behavior to schema="infer". Since this blind schema inference can be expensive, the compromise was a clear error message (see 4194, #6490, and #6657).

    Note that there is also another open issue asking to further improve this error message (#8619), but I don't think there is any way to avoid making the user pass a schema argument in Dask. (but we are certainly open to suggestions).

  6. removed
    needs info
    on Mar 23, 2022
  7. jcrist commented on Apr 7, 2022

    @jcrist
    Member

    @rjzamora, perhaps a naive question, but could we just not write any file for an empty partition? This would mean that a part.#.parquet file wouldn't exist for that partition, but would avoid doing any schema merging. I suppose this would look confusing to downstream consumers (like, perhaps a file is missing), but it would avoid the issue I think.

  8. rjzamora commented on Apr 7, 2022

    @rjzamora
    Member

    @rjzamora, perhaps a naive question, but could we just not write any file for an empty partition? This would mean that a part.#.parquet file wouldn't exist for that partition, but would avoid doing any schema merging. I suppose this would look confusing to downstream consumers (like, perhaps a file is missing), but it would avoid the issue I think.

    This is probably fine. I was actually overlooking this possibility, because I was thinking about the case of "null" partitions in addition to "empty" partitions. However, if the partition is completely empty, then I don't see any obvious problem with your suggestion (although, we may want it to be optional?).

  9. jcrist commented on Apr 7, 2022

    @jcrist
    Member

    Alternatively, is this mainly/only an issue when writing the _metadata file? If we switched to defaulting to write_metadata_file=False would that resolve this issue for most users? I know that _meta doesn't map 1:1 to a pyarrow schema, but if empty files were close-enough in schema (e.g. all columns present, proper int/float/str/... dtypes) would that be enough that most downstream readers (dask & others) be fine?

  10. rjzamora commented on Apr 8, 2022

    @rjzamora
    Member

    Alternatively, is this mainly/only an issue when writing the _metadata file? If we switched to defaulting to write_metadata_file=False would that resolve this issue for most users? I know that _meta doesn't map 1:1 to a pyarrow schema, but if empty files were close-enough in schema (e.g. all columns present, proper int/float/str/... dtypes) would that be enough that most downstream readers (dask & others) be fine?

    I've been thinking about this a bit, and will probably need to summarize my thoughts in #8901 - However, the short version is that I would love to rip out _metadata usage from both a maintenance and stability perspective, but the clear performance benefit of using _metadata still makes me a bit hesitant to suggest this (though I am leaning toward it).

  11. self-assigned this
    on May 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions