Skip to content

Change to_parquet default to write_metadata_file=False #8901

Closed

Description

@jcrist

Some parquet tools and libraries make use of a _metadata file. This file aggregates the parquet metadata from all files in the dataset, making it more efficient to access this metadata.

Doing some software history spelunking:

  • This convention appears (without comment) in the apache/parquet-mr repo ~9 years ago.
  • It's then deprecated (equally without comment) ~4 years ago as "not recommended"
  • Spark (the main historical producer of such files) stopped writing these files ~6 years ago due to scalability issues

The issue is that creation of a _metadata file requires collecting the metadata from all parquet files in a written dataset, merging this metadata, and then writing it out. The size of this metadata scales with total_n_row_groups * n_columns in the dataset - for large datasets (for varying definitions of "large") the size of this metadata can swamp a worker leading to execution failure. A rudimentary benchmark shows creating this file using pyarrow takes roughly 1 KiB per row group per column (this seems very high, but even if it was lower we'd still eventually hit a limit). There are several issues in the dask issue tracker documenting this:

Note that this can also cause issues in the read_parquet side if the _metadata file is large, though the scaling is a bit different here.

Currently creation of the _metadata file can be disabled by passing write_metadata_file=False to to_parquet, which is the recommendation we give in #8899. I'd prefer not to have to mention this caveat though, and have our defaults match our recommendations. Given that other tools have stopped creating or using this file I'd argue that we should stop as well.

I'm not sure if there's a good way to smoothly deprecate this behavior. The best option I've come up with is warning if write_metadata_file isn't set explicitly, but that may be overly noisy for users who won't care about this change. Not sure.

Activity

  1. jcrist commented on Apr 7, 2022

    @jcrist
    MemberAuthor
  2. martindurant commented on Apr 7, 2022

    @martindurant
    Member

    So two issues, one at a time:

    • do we in fact want to change the default?
    • if yes, how to implement this in a way that is obvious but not too annoying to users.

    Note that we do allow an option to ignore the file on read, even if it is present, for better or worse. Also, we are able to create such a file after a successful write of all of the data files, but I doubt this happens often. Finally, we allow to aggregate an effective _metadata file from data fles when it is not present, for the purposes of allowing row-group filtering, and this can be done either by the client or dask workers.

  3. jcrist commented on Apr 7, 2022

    @jcrist
    MemberAuthor

    Yes, that's an accurate summary. Do you have thoughts on these two questions?

  4. martindurant commented on Apr 8, 2022

    @martindurant
    Member

    It is not straight forward. A _metadata file can be a significant read-time optimisation:

    • for opening a dataset (single read can be faster than even just a file listing)
    • for avoiding unnecessary reads via filter pushdown
      and is not a significant cost for 1-100 files.

    Aside: it is unfortunate that people store ML models in parquet, using a tabular structure for what is an array; they should use something like zarr instead.

    At read, if there is such a file, we can ignore it, and if not, we can scan all the datafiles anyway; so the default we choose needs to be considered in conjunction with these other defaults.

    Since it is the number of row-groups * number of columns which matters, we can make a sensible default of writing only when we expect the metadata to be small enough. Unfortunately, there's no way to know at write time whether the stats information might become useful for filtering purposes. Also, if there is directory partitioning, is becomes much less likely that the global statistics are useful. Note that, as least with fastparquet, the user can opt not to create statistics on write, which saves a little time and not insignificant space in the metadata - and this can be done per-column, so you might only do it for columns you intend to filter on. More options, more complications.

    So I think I advocate write_metedata_file="auto" (or suitable) name, where we do write if nrowgroup*ncols < 100,000, or configurable value.

    On read, we can similarly prefer to ignore the metadata file if it exists and it's size is above some threshold, for the case that the user doesn't explicitly want to get statistics.

  5. martindurant commented on Apr 8, 2022

    @martindurant
    Member

    I forgot to mention another side-issue: you would not recommend a _metadata file for datasets intended for multiple append; but at append time, you need to respect one if it's already there (removing it or leaving it inconsistent seem bad, and adding row groups is relatively cheap).

  6. jcrist commented on Apr 20, 2022

    @jcrist
    MemberAuthor

    We've talked in person since the above comments, but rehashing for posterity:

    for opening a dataset (single read can be faster than even just a file listing)

    Agreed, but in my experience the cost of a file listing is usually not that expensive. It might be slower, but not by much.

    for avoiding unnecessary reads via filter pushdown and is not a significant cost for 1-100 files.

    Since most dask-dataframe operations scale linearly with the number of partitions (as opposed to dask-array, where quadratic+ isn't uncommon), I think I'm fine with creating partitions that will ultimately be empty at runtime. We can still filter at runtime to load less data from disk using the underlying parquet engine, the same as we do for columns. If users really need to reduce the number loaded files, they might make use of directory partitioning (which can be done efficiently) instead of relying on a _metadata file to allow row-group filtering.

    So I think I advocate write_metedata_file="auto" (or suitable) name, where we do write if nrowgroup*ncols < 100,000, or configurable value.

    I'm not enthused about systems where the generated artifact changes form based on some characteristic of the data (in this case its size). Since the behavior of reading with a _metadata file is different than that without, having a magic switch would likely confuse users (they'd see one behavior on small data and a different behavior on large data).

    I'm advocating that we should drop writing the _metadata file by default. Users can still ask to explicitly write it if they want, but it won't be the default.

    Aside: since other systems have moved to more complicated (but also more scalable) metadata files, we may also want to make it simpler for users to bring their own metadata (Rick's mentioned some related ideas in #8937 (comment) and #8944). Users can already specify a list of file paths, making it easier to also include divisions/metadata/... might be an interesting problem.

    Since this change will be tricky to deprecate without annoying most users, I'll make the following proposal:

    Change the default to write_metadata_file=None. If None, a metadata file is only written when appending to an existing dataset that has a metadata file. Otherwise no metadata file is written. No deprecation period or warnings added, we'll just be sure to highlight this change in the release notes.

    I think this is unlikely to cause downstream issues, but it might. The other option is to warn unless write_metadata_file is explicitly set, which would definitely alert users but would also mean all usage would have to set this parameter for a while which feels unfriendly.

    One issue with this proposal is that afaict the pyarrow engine only supports appending to datasets that currently have a _metadata file. The fastparquet engine doesn't appear to have this restriction (based on

    try:
    # to append to a dataset without _metadata, need to load
    # _common_metadata or any data file here
    pf = fastparquet.api.ParquetFile(path, open_with=fs.open)
    except (OSError, ValueError):
    # append for create
    append = False
    , cc @martindurant for clarification on the expected behavior here). This means that writing and appending to a dataset with the pyarrow engine would then break without explicitly passing in write_metadata_file=True, which we don't want. So this change would also require modifying the existing engines so append would still work if no metadata file is present.

  7. martindurant commented on Apr 20, 2022

    @martindurant
    Member

    the cost of a file listing is usually not that expensive.

    On a remote datastore with >=1000 files, it is indeed slow, because the listing must be paged. If we know something about the distribution of paths beforehand, we can run listing concurrently, in theory, but paging will be serial as things stand.

    for clarification on the expected behavior here

    This simply ensures that append works even if the original location doesn't yet contain any data.

    ould also require modifying the existing engines so append would still work if no metadata file is present

    That should be the easier case - you just need to know the file numbering to go with to avoid any overwrites. However, we would need to decide what to do if a _metadata file does exist: do we still use it, remove it, error, or ...?

  8. rjzamora commented on Apr 21, 2022

    @rjzamora
    Member

    I'm not enthused about systems where the generated artifact changes form based on some characteristic of the data (in this case its size). Since the behavior of reading with a _metadata file is different than that without, having a magic switch would likely confuse users (they'd see one behavior on small data and a different behavior on large data).

    This is a conclusion that I have also recently reached. I think it makes a lot of sense to use "magic" switches to select internal algorithm defaults, but these switches should never affect the structure or content of the result. In other words, I think its fine to use file count and/or _metadata presence to decide if we will do things like process metadata in parallel. However, these characteristics should not control partition sizes and/or metadata output. (This is obviously just my personal opinion, but I'm starting to feel more strongly about it by the day)

  9. rjzamora commented on Apr 21, 2022

    @rjzamora
    Member

    However, we would need to decide what to do if a _metadata file does exist: do we still use it, remove it, error, or ...?

    This is a good question. It certainly feels like we would definetly want to update the _metadata file on append if one is present. So, this would probably be the exception to the write_metadata_file=False-default rule. It may make sense to add an ignore_metadata_file=False to to_parquet in the off-chance that the user really does want to append to a dataset without updating the global metadata, but I have a hard time imagining a case where this isn't a terrible idea. So, throwing a clear error is probably the "correct" thing to do.

  10. jcrist commented on Apr 21, 2022

    @jcrist
    MemberAuthor

    It certainly feels like we would definetly want to update the _metadata file on append if one is present.

    Agreed, this is what I suggested above:

    Change the default to write_metadata_file=None. If None, a metadata file is only written when appending to an existing dataset that has a metadata file. Otherwise no metadata file is written. No deprecation period or warnings added, we'll just be sure to highlight this change in the release notes.

    The issue is that appending behavior doesn't currently work without a _metadata file, and there are some inconsistencies between backends with regards to what happens. Assorted issues:

    • pyarrow doesn't work with appending without a _metadata file. An error is raised suggesting setting ignore_divisions=True should make things work, but it doesn't
    • fastparquet only works without a _metadata file if ignore_divisions=True, even if df.known_divisions is False.

    I can fix these, but I'm curious about what the expected behavior is here.

    • If df has known_divisions and no _metadata file, fastparquet will load the last file and attempt to see if the divisions are still sorted (and if not will error). Is this a restriction we want to keep?
    • If known_divisions is False for df, should we skip this check (basically treat this as ignore_divisions=True). Is there a good way to tell from the last file if previous writes had known divisions even if the new data doesn't?
  11. jcrist commented on Apr 21, 2022

    @jcrist
    MemberAuthor

    More broadly, what kind of changes to the backing engine classes are acceptable, given implementations live outside of dask. Can I change the signature of a backend method? Only add new methods (and don't call them if they're missing)? What's the best way to not break cudf here?

  12. rjzamora commented on Apr 21, 2022

    @rjzamora
    Member

    Change the default to write_metadata_file=None. If None, a metadata file is only written when appending to an existing dataset that has a metadata file. Otherwise no metadata file is written. No deprecation period or warnings added, we'll just be sure to highlight this change in the release notes.

    I completely agree with your proposal @jcrist

    Also, it is good to know that there are existing bugs related to appending. Thanks for summarizing!

    I don’t have any strong feelings on exactly how to resolve existing bugs, but my intuition is that we should probably assume the user doesn’t want to calculate/preserve divisions unless they explicitly ask for this extra work (note that one of my proposals below is to do this in read_parquet anyway, and I would assume reading and appending should have consistent defaults).

    More broadly, what kind of changes to the backing engine classes are acceptable, given implementations live outside of dask. Can I change the signature of a backend method? Only add new methods (and don't call them if they're missing)? What's the best way to not break cudf here?

    

I wouldn’t be too nervous about “breaking” dask_cudf on the write side. If a test breaks as a result of different default behavior, I can certainly find and “fix” it before the related Dask PR is even merged. Regarding Engine-API changes: dask_cudf is just doing to_parquet = partial(dd.to_parquet, engine=CudfEngine), and CudfEngine is just inheriting from ArrowDatasetEngine (and overriding the write_partition and write_metadata class methods). CudfEngine does not override initialize_write (where most appending logic currently lives), so fixing append bugs is probably pretty "safe."

    Just to clarify some of the Parquet-related changes I am personally supportive of:

    1. [Write] Change write_metadata_file default to None (becomes False, unless appending)

    2. [Read] Change split_row_groups default to False (starts as None in the top-level signature so we can temporarily “warn” users when we hit the “old” code path where split_row_groups was originally switching to True). We would then submite a follow-up PR in ~2+ months to remove the warning (and set the False default in top-level function signature)

    3. [Read, but may relate to Write-with-append] Add calculate_divisions argument, and deprecate gather_statistics. The default for calculate_divisions will be False, but we will start with None in the top-level function signature (for now) so we can warn the user if they do not set anythin explicitly and there is a _metadata file present (because they will no longer get known divisions). We could then submit a follow-up PR in ~2+ months to remove gather_statistics altogether and remove the warning (and set the False default in top-level function signature)

    4. [Read][Explicit Plan TBD] Modify (and maybe deprecate?) chunksize and/or aggregate_files. I would like to use from_dataset to explore “good” defaults/options for this.

    5. [Read][Explicit Plan TBD] Modify categories to include directory-partitioned columns. Converting a partitioned column to a pandas-category column has overhead, and not all users want/need a categorical dtype (e.g. Dask-sql). My opinion is that the user should need to “opt-in” to this extra work by including directory-partitioned columns in categories (right now this is actually not allowed).

  13. martindurant commented on Apr 21, 2022

    @martindurant
    Member

    ^ would you mind annotating those 5 points for whether they affect reading or writing? We have so many options, I am not certain any more!

    fastparquet only works without a _metadata file if ignore_divisions=True, even if df.known_divisions is False.

    I suppose an attempt to ensure that if there was a reliable (ordered) index, we don't break it. Not sure that's documented or tested, and I don't remember writing it. fastparquet itself doesn't care about this, it must be in dask's fp engine.

  14. self-assigned this
    on Apr 27, 2022
  15. added a commit that references this issue on Feb 24, 2024
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