Skip to content

Use fsspec.parquet module for better remote-storage read_parquet performance - #8339

Merged
rjzamora merged 37 commits into
dask:mainfrom
rjzamora:use-fsspec-parquet
Jan 22, 2022
Merged

rjzamora merged 37 commits into
dask:mainfrom
rjzamora:use-fsspec-parquet

Conversation

@rjzamora

@rjzamora rjzamora commented Nov 3, 2021 •

Copy link
Copy Markdown
Member

Depends on fsspec#813

The new fsspec.parquet.open_parquet_file function offers an optimized version of fs.open for parquet-formatted data files. This PR updates both the "pyarrow" and "fastparquet" read_parquet implementations to (optionally) leverage this new feature.

API:

The new API change proposed in this PR is to add an open_file_options kwarg to read_parquet. This kwarg is intended as a central dictionary location for the user to define all options related to data-file opening. Within this dictionary, the user can specify an optimized "parquet" precaching strategy within a nested "cache_options" dictionary:

ddf = dd.read_parquet(paths, ..., open_file_options={"cache_options": {"precache": "parquet"}})

We choose to define all cache-related options under "cache_options" to simplify current and future fsspec integration. Although fsspec does not yet support the "precache" sub-field directly, there seems to be agreement that such a key can be recognized in the future (within fs.open and related). Until then, Dask can simply perform a manual check for {"precache": "parquet"} and redirect the fs.open call to open_parquet_file.

Blockers:

  • Merge/release fsspec#813 (or check that CI passes with master fsspec branch)
  • Ensure that the ability to import fsspec.parquet is a sufficient check for compatibility (or improve this check if it is not)
  • Add test to confirm that passing an open_parquet_file={} kwarg to read_parquet can be used to pass kwargs to fsspec.parquet.open_parquet_file

Performance Motivation

import dask.dataframe as dd

%%time
df = dd.read_parquet(
    ["gs://my-bucket/criteo-parquet/day_0.parquet"],
    columns=["I10"],
    split_row_groups=10,
    engine=...,
    storage_options=...,
    open_file_options={"cache_options": {"precache": "parquet"}},
)
# Main Branch - "fastparquet" Engine
CPU times: user 6.65 s, sys: 5 s, total: 11.6 s
Wall time: 8.52 s

# Main Branch - "pyarrow" Engine
CPU times: user 6.83 s, sys: 4.75 s, total: 11.6 s
Wall time: 8.04 s

# This PR - "fastparquet" Engine
CPU times: user 5.51 s, sys: 3.7 s, total: 9.21 s
Wall time: 4.78 s

# This PR - "pyarrow" Engine
CPU times: user 5.37 s, sys: 3.7 s, total: 9.07 s
Wall time: 4.75 s

@jorisvandenbossche

Copy link
Copy Markdown
Member

I am not fully sure if the pyarrow-based engine should (always) make use of this. Similar optimization are already done by the parquet/dataset readers, and so if we don't get benefit of that when used through dask (which seems to be the case based on your timings above), we should maybe rather investigate why this is not the case.

@rjzamora

Copy link
Copy Markdown
Member Author

I am not fully sure if the pyarrow-based engine should (always) make use of this. Similar optimization are already done by the parquet/dataset readers, and so if we don't get benefit of that when used through dask (which seems to be the case based on your timings above), we should maybe rather investigate why this is not the case.

Yes - There is no reason that pyarrow cannot optimize the data transfer in a similar way, and so I do want the final iteration of this to make the fsspec.parquet.open_parquet_file approach to be optional (with the default perhaps depending on the pyarrow version).

Comment thread dask/dataframe/io/parquet/core.py Resolved
@rjzamora
rjzamora marked this pull request as ready for review December 9, 2021 19:36
@rjzamora

Copy link
Copy Markdown
Member Author

@martindurant - I'm sorry that this is yet another "largish" PR, but I think your eyes would be very valuable here. In addition to a bit of kwarg-handling cleanup, the most controversial change is the addition of dd.io.utils.open_input_files. My visions is for this function to serve as the central source of logic for opening a list of input files. For now, the only options for file_format are "parquet" and None. However, I can imagine adding an "orc" optional as well. Note that this change also highlights the need for a public fsspec.parquet API for opening a list of parquet files (rather than just a single file at a time).

@martindurant

Copy link
Copy Markdown
Member

To the specific API:

  • I think it's fine to add the extra kwarg. The term open_options is ambiguous, though, because the dataset is also "opened". Maybe file_open_options
  • adding open_input_files is fine, but in practice I don't expect any user to call it or know about it, and I don't expect the "open_file_cb" argument to be used either. The docs of what this all means probably go elsewhere
  • {"file_format": "parquet"} should still be considered experimental and not the default? It's also a bit confusing in the name, since indeed the format is parquet, here we are saying that we want to use parquet-specific logic for our file access pattern (i.e., precaching). It feels more like an fsspec storage_option. Maybe fsspec's open functions should allow for an open_options that gets passed to the fs.open calls? That latter idea is definitely a good idea, on reflection, but I'm not sure if it helps simplify the code here at all (maybe stuff the rg, cols into cache_options... etc).

@rjzamora

rjzamora commented Jan 10, 2022 •

Copy link
Copy Markdown
Member Author

I think it's fine to add the extra kwarg. The term open_options is ambiguous, though, because the dataset is also "opened". Maybe file_open_options

file_open_options works for me. Note that soon after (or possibly before) this PR, I’d also like to revise the way kwargs are handled in read_parquet. That is, I’d like the function signature (and docstring) to include dataset_options= and read_options= (rather than just looking for these options under the “dataset” and “read” keys of kwargs). The reason I chose open_options= was just that this particular trio of names seemed to make the most sense: ’dataset_options’, open_options, and read_options.

adding open_input_files is fine, but in practice I don't expect any user to call it or know about it, and I don't expect the "open_file_cb" argument to be used either. The docs of what this all means probably go elsewhere

I see - I also don’t expect any users to know or care about open_input_files, but I wanted to create one clear point of logic for file-opening optimizations. Perhaps it would be best to make this function more-explicitly private, and use the read_parquet docstring to explain behavior?

Regarding "open_file_cb”: The key use case here is the direct generation of native Arrow-file objects. For engine=“pyarrow” with S3, S3FileSystem.open_input_file is likely to have similar performance as fsspec.parquet.open_parquet_file. However, there may be cases where the native file is preferable, and we will certainly want to be able to compare performance if/when there are improvements in pyarrow. What I am really trying to say here is that I feel strongly that we need to allow the user to override the default file-open function, but I don’t care much about what that mechanism is exactly.

{“file_format": "parquet"} should still be considered experimental and not the default?

I am fine with leaving the vanilla fs.open as the default for now.

It's also a bit confusing in the name, since indeed the format is parquet, here we are saying that we want to use parquet-specific logic for our file access pattern (i.e., precaching). It feels more like an fsspec storage_option. Maybe fsspec's open functions should allow for an open_options that gets passed to the fs.open calls? That latter idea is definitely a good idea, on reflection, but I'm not sure if it helps simplify the code here at all (maybe stuff the rg, cols into cache_options... etc).

I agree that it would be nice for these new “options” to be included in storage_options under open_options, but it is also seems possible that a user may want to read both CSV and Parquet files using the same fs. I think you hit the nail on the head when you said that we mostly want to control the “precaching” mechanism for a specific fs.open(…, cache_type=“parts”) call.

Perhaps the simplest solution to all this confusion is to simply check for cache_type=“parquet” in fs.open, and redirect the call through fsspec.parquet.open_parquet_file when this criteria is met? This way, the user could include all format-specific options within cache_options, and the same private _open_input_files utility could be used before and after the fsspec change to handle format-specific file opening (note that open_parquet_file arguments would just be included in cache_options):

_open_input_files(
    paths,
    fs=None,
    open_file_func=None,  # Defaults to `fs.open`
    cache_type=None,  # Allow "parquet" (in addition to "parts", etc)
    cache_options=None, # Use as kwargs to `open_parquet_file` when cache_type="parquet"
    **kwargs,
):

@rjzamora

Copy link
Copy Markdown
Member Author

Update: After an offline discussion with @martindurant, I decided to go with the following design:

  • The new read_parquet kwarg is called open_file_options
  • The dictionary of key-value arguments defined within open_file_options is passed to fs.open when a parquet data file is open for reading. However, there are two keys that receive special treatment:
    • If "open_file_func" is defined, the (callable) value will be used in lieu of fs.open. The purpose of this option is mostly to enable the optional use of native Arrow file-systems.
    • If "cache_options" is defined, Dask will check if this (sub-)dictionary contains {"precache": "parquet"}. If so, the fsspec.parquet.open_parquet_file function will be used in lieu of fs.open (with the other elements of cache_options being used as kwargs for that function call).
      • Important Note: In the future, this "precache" option should be handled upstream in fs.open (and fsspec.open_files). In other words, we shouldn't need to manually call open_parquet_file in Dask. However, I don't feel this upstream change should be a blocker for this PR. The only "blocker" is that we establish that the "precache" option (and kwargs for the appropriate precaching function) should be stored under the existing cache_options argument to fs.open.

@martindurant

Copy link
Copy Markdown
Member

I agree with everything in the comment above.

@rjzamora

Copy link
Copy Markdown
Member Author

I agree with everything in the comment above.

Thanks @martindurant ! Before I settle on what we currently have in this PR, can I get your thoughts on the alternative proposal I recently described in fsspec#885? Note that I am currently favoring the "v2b" proposal (and the temporary logic required on the Dask side is only a minor change from what we have here).

@martindurant

Copy link
Copy Markdown
Member

I will comment there soon

@rjzamora

rjzamora commented Jan 12, 2022 •

Copy link
Copy Markdown
Member Author

Further Update to Previous Status:

Due to a slight change in preference after compiling fsspec#885, all precache-related options should now be passed to open_file_options under a new "precache_options" key (and not under "cache_options"). These options must include a "method" key to specify the precaching method/function to be used.

This change is minor in terms of modified lines in this PR, and so I would be happy to further modify or roll back the tweak if necessary. However, to summarize the basic motivation: I feel that the precache phase must be explicitly seperate from cache initialization, and so a distinct argument for this separate phase seems necessary to me.

@rjzamora

Copy link
Copy Markdown
Member Author

It seems like you are generally supportive of the rough proposal in fsspec#885 @martindurant - Does this mean the precache_options approach used in this PR is reasonable? I'm cautiously optimistic that I may not need to completely revise this PR again - Woohoo

@martindurant

Copy link
Copy Markdown
Member

yes, supportive!

@rjzamora

Copy link
Copy Markdown
Member Author

Is there any interest or time for a review this week @dask/io? Also, do we know of new people that we can invite to join the "dask/io" team? :)

If no one has time to look through this in the next few days, I will likely do a deep review myself and merge on Friday.

@rjzamora

Copy link
Copy Markdown
Member Author

@martindurant - I plan to give this a final review soon, and will probably merge by the end of the day if there are no objections :)

@martindurant

Copy link
Copy Markdown
Member

no objections here

@rjzamora
rjzamora merged commit 08eee6d into dask:main Jan 22, 2022
@rjzamora
rjzamora deleted the use-fsspec-parquet branch January 22, 2022 04:20
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.

3 participants