Skip to content

Refactor read_metadata in pyarrow parquet engines - #8072

Merged
rjzamora merged 34 commits into
dask:mainfrom
rjzamora:read-metadata-refactor
Oct 1, 2021
Merged

rjzamora merged 34 commits into
dask:mainfrom
rjzamora:read-metadata-refactor

Conversation

@rjzamora

@rjzamora rjzamora commented Aug 20, 2021 •

Copy link
Copy Markdown
Member

This is a follow-up to #8034, and corresponds to the "short-term" ArrowDatasetEngine component of the plan discussed in #8058 .

Currently, when the user specifies ignore_metadata_file=True (or if the _metadata file is missing) the logic in ArrowDatasetEngine.read_metadata will construct a read-parquet "plan" by iterating through every file in the dataset (in serial). With the changes in this PR, the output-partitioning plan will be executed in parallel. In order to expose control over this algorithm to the user, a files_per_metadata_task argument was added. This argument allows the user to specify how many distinct paths should ne processed within the same parquet-metadata-processing task. If files_per_metadata_task=0, parallel metadata-processing is dissabled.

Note that the PyarrowLegacyEngine code was also refactored here, but the changes in that engine fall short of supporting parallel parquet-metadata processing.

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

Copy link
Copy Markdown
Member Author

rerun tests

Comment thread dask/dataframe/io/parquet/arrow.py Resolved
rapids-bot Bot pushed a commit to NVIDIA/cudf that referenced this pull request Sep 9, 2021
This PR fixes some un-tested hive-partitioning edge cases in read-parquet.  The "full" fix requires [dask#8072](dask/dask#8072).  However, this PR should be merged before the upstream change goes in (otherwise dask_cudf.read_parquet CI will temporarily break).  Note that this change is non-breaking, but the corresponding dask-dataframe change **is**.

Authors:
  - Richard (Rick) Zamora (https://github.com/rjzamora)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)
  - Benjamin Zaitlen (https://github.com/quasiben)

URL: #9122
@rjzamora

Copy link
Copy Markdown
Member Author

@martindurant - This PR doesn't include any fastparquet-engine changes, but the follow-up PR (#8092) does (and it follows pretty much the same pattern). At this point, the PRs are getting a bit old (and are making it hard for me to focus on other Dask-parquet bug-fixes/improvements). It would be great to get your eyes on both PRs if you find the time. I also welcome other reviewers who might be interested :)

Note that I do feel relatively confident that this PR is "low-risk". There is not a lot of new logic, and that logic is only used in the case that a global _metadata file is missing. With that said, it is a complete arrow-engine implementation of the plan in #8058, and so the diff is still quite large.

@martindurant martindurant 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.

Here are comments from the diff alone - mostly questions.
I will go through the code on the branch in detail too, but I don't think that should be a blocker.

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

# Main parts/stats-construction
if has_metadata_file or metadata_task_size == 0:
# We have a global _metadata file to work with.

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.

if metadata_task_size is 0, we are not bothering with stats at all, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No. metadata_task_size=0 just means that we will not use parallel metadata collection. If the user still needs statistics (e.g. for filtering), then we will still collect/process them in serial.

all_files = [
filef
for filef in all_files
if filef.split(fs.sep)[-1] in valid_paths

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.

Why only the last path element?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is not new code, but I'm sure it could be simplified in a simple stand-alone PR. We are essentially comparing the file names to a list of "valid" filenames that have not been filtered out during dataset discovery. We only take the last element, because that is the file name.

stats += stat
return parts, stats

gather_parts_dsk["final-" + name] = (_combine_parts, finalize_list)

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.

It's actually quite HLG-ish. Shouldn't matter here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Are you suggesting that this could be an HLG layer?

Comment thread dask/dataframe/io/parquet/arrow.py Resolved
cmin = statistics[name]["min"]
cmax = statistics[name]["max"]
last = cmax_last.get(name, None)
if not (filters or chunksize or aggregation_depth):

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.

It feels like there are cases in which we can skip this whole set of loops altogether

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Can you elaborate? This nested loop is indeed a bit much, but I've found it surprisingly challenging to improve.

@rjzamora rjzamora left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for taking the time to slog through this @martindurant !

I left some temporary responses to your comments, and I'll try to address whatever I can tonight.

Comment thread dask/dataframe/io/parquet/arrow.py Resolved
Comment thread dask/dataframe/io/parquet/arrow.py Resolved
Comment thread dask/dataframe/io/parquet/arrow.py Resolved
if file_frag is not None:
# Check/correct order of `categories` using last file_frag
#
# Note that `_get_partition_keys` does NOT preserve the

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I would be pretty far from surprised to learn that the curent workaround is not the most efficient solution. In fact, knowing myself, am a bit confused why I would have used re before trying a simple split. In either case, I will try simplifying this a bit. Note that fastparquet does not require this workaround at all.

# - "partitioning_method" : (dict) The `partitioning` options
# used for file discovory by pyarrow.
#
return {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ar you referring to to the returned datset_info dict, or the "partitioning information" discuessed just above?


# Main parts/stats-construction
if has_metadata_file or metadata_task_size == 0:
# We have a global _metadata file to work with.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No. metadata_task_size=0 just means that we will not use parallel metadata collection. If the user still needs statistics (e.g. for filtering), then we will still collect/process them in serial.

all_files = [
filef
for filef in all_files
if filef.split(fs.sep)[-1] in valid_paths

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is not new code, but I'm sure it could be simplified in a simple stand-alone PR. We are essentially comparing the file names to a list of "valid" filenames that have not been filtered out during dataset discovery. We only take the last element, because that is the file name.

stats += stat
return parts, stats

gather_parts_dsk["final-" + name] = (_combine_parts, finalize_list)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Are you suggesting that this could be an HLG layer?

cmin = statistics[name]["min"]
cmax = statistics[name]["max"]
last = cmax_last.get(name, None)
if not (filters or chunksize or aggregation_depth):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Can you elaborate? This nested loop is indeed a bit much, but I've found it surprisingly challenging to improve.

Comment thread dask/dask.yaml
Comment on lines +5 to +7
parquet:
metadata-task-size-local: 512 # Number of files per local metadata-processing task
metadata-task-size-remote: 16 # Number of files per remote metadata-processing task

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay - Did a fair bit of local testing to confirm that tese are reasonable defaults. Note that I also tweaked the logic so that the parallel metadata processing will not kick in unless the number of files exceeds this number.

rapids-bot Bot pushed a commit to NVIDIA/cudf that referenced this pull request Sep 30, 2021
A similar fix for this problem was recently submitted in #9159 and closed in favor of #9203. It seems that the test added in the latter PR was not actually capturing the original problem. However, after [dask#8072](dask/dask#8072) is merged, the new test will certainly start failing.

Authors:
  - Richard (Rick) Zamora (https://github.com/rjzamora)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)
  - Benjamin Zaitlen (https://github.com/quasiben)

URL: #9314
@rjzamora

rjzamora commented Oct 1, 2021

Copy link
Copy Markdown
Member Author

@dask/io - Please let me know if anyone has plans to review this PR in the near future. If not, I think it may be best to merge at the end of the day so I can shift my focus to #8092 (and other fixes/improvements).

@martindurant

Copy link
Copy Markdown
Member

I am +1 for merge and iterate

@rjzamora
rjzamora merged commit 0b0b2f7 into dask:main Oct 1, 2021
@MrPowers

MrPowers commented Oct 1, 2021 •

Copy link
Copy Markdown
Contributor

I created a notebook with some benchmarks (using a version of Dask before this was merged).

This takes 1 second:

ddf = dd.read_parquet(
    "s3://coiled-datasets/timeseries/20-years/parquet",
    storage_options={"anon": True, "use_ssl": True},
    ignore_metadata_file=False,
    split_row_groups=True,
    engine="pyarrow",
)

This takes 210 seconds:

ddf2 = dd.read_parquet(
    "s3://coiled-datasets/timeseries/20-years/parquet",
    storage_options={"anon": True, "use_ssl": True},
    ignore_metadata_file=True,
    split_row_groups=True,
    engine="pyarrow",
)

Once a new version of Dask is released, I'll quantify gains and see how much faster ddf2 runs.

Great work @rjzamora!

@rjzamora

rjzamora commented Oct 1, 2021

Copy link
Copy Markdown
Member Author

Sounds good @MrPowers - I'll do my best to get the fastparquet follow-up (#8092) merged before the next release as well.

@rjzamora
rjzamora deleted the read-metadata-refactor branch October 4, 2021 13:47
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.

4 participants