Skip to content

Add filter optimization for remote parquet #7090

Closed

Description

@jmsmdy

This feature request is long, sorry in advance.

Suppose we are reading a parquet file on s3, partitioned into multiple parts according to a "date" column. Correct me if I am wrong, but it appears that:

df = dd.read_parquet('s3://...')
df[df['date'] == '2021-01-20'].compute()

does not run any faster than

df = dd.read_parquet('s3://...')
pdf = df.compute()
pdf[pdf['date'] == '2021-01-20']

In principle optimizations should allow the former to run approximately n times faster where n is the number of partitions, assuming the partitions are roughly equal in size (the main limiting factor being network transfer speeds).

How about filtering by a column which is not used to partition this parquet file? The code

df = dd.read_parquet('s3://...')
df[df['employee'] == 'Jane'].compute()

does not run any faster than

df = dd.read_parquet('s3://...')
pdf = df.compute()
pdf[pdf['employee'] == 'Jane']

In principle, the former could take advantage of row group filtering by loading only certain row groups even within a single parquet file (the row groups to load being determined from looking only at the employee column). This may or may not give a performance benefit depending on how the rows are arranged.

Note that pandas already allows filtering based on row groups when loading a parquet file to avoid loading the entire file into memory via the "filter" argument of pd.read_parquet: pandas-dev/pandas#26551

Dask supports a "filter" argument in dd.read_parquet, but it has different behavior than pandas. If the column was not used for partitioning when writing the parquet file in the first place, the result of setting the "filter" argument in dd.read_parquet will return an empty dataframe, regardless of whether there are rows in the entire dataset meeting the filter condition.

Fixing the behavior of the "filter" argument of dd.read_parquet to match that of pandas would be nice, but this fix would not fully satisfy this feature request, because having to specify the filters when creating the original dask dataframe goes against the principle of how dask is supposed to work: it doesn't provide an abstraction over partitioning, lazy-loading, etc. Ideally lazy loading of partitions (or better yet, row groups) would happen automatically when computing heavily-filtered dask dataframes.

In the example above filtering by "employee", the optimal thing for dask to do would be to first load the "employees" column entirely, identify the row groups containing at least one matching row, then load the other columns only for those row groups. If the filter involves the values of more than one column, this kind of optimization could still work: load only the relevant columns for the filter, identify relevant row groups, then load the rest of the data only from those row groups.

Activity

  1. jsignell commented on Jan 21, 2021

    @jsignell
    Member

    I suspect that what you are asking for is already supported at least for the pyarrow-dataset engine. I recommend taking a close read of the documentation on the read_parquet function there are several places where row-groups are explicitly discussed.

  2. jmsmdy commented on Jan 21, 2021

    @jmsmdy
    Author

    On a test dataset, I have compared the runtime of:

    df = dd.read_parquet('s3://...')
    df[df['employee'] == 'Jane'].compute()

    versus

    df = dd.read_parquet('s3://...', engine='pyarrow-dataset')
    df[df['employee'] == 'Jane'].compute()

    versus

    df = dd.read_parquet('s3://...', engine='pyarrow-dataset', filters=[('employee', '=', 'Jane')])
    df.compute()

    Examples 1 & 2 have the same runtime (up to error). Example 3 runs multiple times faster than example 2. This indicates that even when using the 'pyarrow-dataset' engine (the only one supporting row group optimizations according to the docs), the row group optimization is only performed when using the "filter" argument when initially creating the dask dataframe object, not when performing filtering using pandas-style filtering after the initial creation of the dask dataframe object.

    This means, effectively, that to limit network transfers via row group optimization requires knowing your filter conditions before creating the dask dataframe. So the requested feature does not appear to be implemented.


    I also did some more experiments and found that it appears even column fetching optimization only applies when specified using the "columns" argument when initially creating the dask dataframe using dd.read_parquet. That is:

    df = dd.read_parquet('s3://...', engine='pyarrow-dataset', columns=['employee'])
    df.compute()

    run many times faster than:

    df = dd.read_parquet('s3://...', engine='pyarrow-dataset')
    df['employee'].compute()

    which runs at almost exactly same speed as:

    df = dd.read_parquet('s3://...', engine='pyarrow-dataset')
    df.compute()

    It appears that example 2 here is pulling all the columns from s3, even though it only needs to read the 'employee' column. This would also need to be fixed for post-dataframe-creation row group filtering to work.


    The use case for post-dataframe-creation row and column filter optimization is to allow dynamic exploration of large remote datasets while limiting bandwidth. As it is, even if you heavily filter both rows and columns, the entire dataframe appears to be transferred over the network when you call compute(), unless you specify the filters at the moment you create the dataframe (which is not useful for dynamically exploring a dataset).

  3. jsignell commented on Jan 21, 2021

    @jsignell
    Member

    Oh! I misunderstood the original request - sorry I closed so quickly.

    I believe there was an intention to pass down getitem requests so that only the required partition would be read. I am not sure whether or not that is expected to work in current versions or whether it's still in the works. But @rjzamora or @martindurant should know.

  4. rjzamora commented on Jan 21, 2021

    @rjzamora
    Member

    Thanks for raising @jmsmdy - Predicate pushdown is not yet implemented, so it makes sense that you need to pass an explicit filter to avoid IO. Note that I certainly agree this feature is doable.

    I am not sure what is going on with the getitem optimization - Do you only see this problem with a remote fs? Either way, I will be happy to look into this as soon as I can.

  5. martindurant commented on Jan 21, 2021

    @martindurant
    Member

    Note that pyarrow-dataset will be the only backend supporting row-level filtering, all three support row-group filtering.

    However, only column selection was passed down to the loader (this should still work), and filtering out row-groups required the filters= syntax.

  6. gforsyth commented on Jan 22, 2021

    @gforsyth
    Contributor

    The column selection should be working. This shouldn't make a difference, but what does timing look like if you do

    df = dd.read_parquet('s3://...', engine='pyarrow-dataset')
    df = df["employee"]
    df.compute()
    
  7. jmsmdy commented on Jan 22, 2021

    @jmsmdy
    Author

    @gforsyth

    I'm using a dataset with ~1 million rows, ~25 columns for testing. Of course this dataset can easily fit into memory, but this is just a toy example (in my application it is going to be scaled up drastically). I could manufacture a larger dataset if it would help diagnose what is going on.

    # using read_parquet filter
    df = dd.read_parquet('s3://...', engine='pyarrow-dataset', columns=['employee'])
    df.compute()
    # pandas filter one line
    df = dd.read_parquet('s3://...', engine='pyarrow-dataset')
    df['employee'].compute()
    # pandas filter two lines
    df = dd.read_parquet('s3://...', engine='pyarrow-dataset')
    df = df["employee"]
    df.compute()

    I ran each of these five times (interlacing them to eliminate any potential effects of caching, just in case), and these were the results:

    #     read_parquet filter: 0.211 0.267 0.700 0.245 0.283
    #  pandas filter one line: 3.037 3.072 3.095 3.482 2.978
    # pandas filter two lines: 3.015 3.026 3.009 3.184 3.169
  8. martindurant commented on Jan 22, 2021

    @martindurant
    Member

    ^ please also try with the two other engines, if you wouldn't mind

  9. jmsmdy commented on Jan 22, 2021

    @jmsmdy
    Author

    Adding other engines:

    ##### pyarrow legacy
    #     read_parquet filter: 0.333 0.232 0.263 0.243 0.256
    #  pandas filter one-line: 3.091 3.085 3.107 3.662 3.202
    # pandas filter two-lines: 3.113 3.098 3.025 3.082 3.076
    
    ##### pyarrow
    #     read_parquet filter: 0.250 0.264 0.313 0.252 0.259
    #  pandas filter one-line: 3.128 3.046 3.086 3.055 3.086
    # pandas filter two-lines: 3.070 3.137 3.105 3.037 3.065
    
    
    ##### fastparquet
    #     read_parquet filter: 0.484 0.437 0.338 0.357 0.337
    #  pandas filter one-line: 1.652 1.845 1.715 1.674 1.743
    # pandas filter two-lines: 1.797 1.696 1.669 1.718 1.701
    
    ##### pyarrow-dataset
    #     read_parquet filter: 0.211 0.267 0.700 0.245 0.283
    #  pandas filter one-line: 3.037 3.072 3.095 3.482 2.978
    # pandas filter two-lines: 3.015 3.026 3.009 3.184 3.169

    EDIT: here are the versions I am currently using, in case it is relevant:

    # dask          == 2020.12.0
    # pyarrow       == 2.0.0
    # fastparquet   == 0.5.0

    I can't use newer versions as I am in an environment with a security policy that prevents it.

  10. TomAugspurger commented on Jan 22, 2021

    @TomAugspurger
    Member

    #6261 was a prototype of pushing boolean filters down to parquet filters. I suspect that anything written today would be built off layers and look pretty different.

  11. gforsyth commented on Jan 22, 2021

    @gforsyth
    Contributor

    I could manufacture a larger dataset if it would help diagnose what is going on.

    For now, I think the numbers you have here demonstrate that the column selection isn't getting pushed down (at least in this config).

    What versions of dask and distributed are you using?

  12. rjzamora commented on Jan 22, 2021

    @rjzamora
    Member

    I am looking into this now - It seems that the column selection isn't pushed down to the read_parquet call if the the getitem operation was performed last. The problem is in this block of optimize_read_parquet_getitem. I'll see what can be done here

  13. martindurant commented on Jan 22, 2021

    @martindurant
    Member

    ^ so df.employee.sum().compute() (or some other operation that makes sense for that column) would have triggered the optimisation?

  14. rjzamora commented on Jan 22, 2021

    @rjzamora
    Member

    ^ so df.employee.sum().compute() (or some other operation that makes sense for that column) would have triggered the optimisation?

    Yes - If you add any other layer(s) to the high-level graph, the optimization should be triggered.

  15. rjzamora commented on Jan 24, 2021

    @rjzamora
    Member

    Thanks again for exposing the column-selection problem @jmsmdy - #7106 should re-activate the optimization for your example. We can attack predicate pushdown soon, but it probably makes sense to wait until the read_parquet code is officially moved to Blockwise (#7042).

  16. jmsmdy commented on May 2, 2021

    @jmsmdy
    Author

    Look like #7415 (moving read_parquet to Blockwise, replacing #7042) was just merged.

  17. secrettoad commented on Feb 8, 2023

    @secrettoad

    Is there any established work-around or best practice for filtering on the directory-based partitioning structure created via to_parquet with partition_by? I have 27,000 partitions in s3 written this way, one of the partition_by columns being a date that I am attempting to filter aggressively at read-time, but it is taking an abnormal amount of time to create the dataframe (not compute).

    Using dask==2022.11.1 and distributed==2022.11.1 along with Python 3.10 and the Pyarrow backend for both read and write.

    I apologize if this isn't the right forum for this, but my first instinct was the same as @jmsmdy 's in that I assume filtering the dataframe post-read would trim the task graph on compute, but it did not do so, and so I figure this is probably tangential enough to this conversation.

    dd.read_parquet('s3://bucket/path', columns=['columns'], filters=[("day", "<=", "2018-08-01")])

    I aborted the read at 10 minutes and have run it several times to confirm it is reproducible at least in this scope. When I look to the scheduler for more info, I see that ~1600 instances of gather_pq_parts are being distributed and run as part of the filter.

    Thank you in advance for any help.

  18. secrettoad commented on Feb 8, 2023

    @secrettoad

    For anyone else finding this or in case it is helpful to anyone, creating a list of glob strings that filter the files and passing that as the path argument to read_parquet brought performance back into the expected realm. Perhaps that is the established work around. It was pretty simple to implement.

  19. rjzamora commented on Feb 9, 2023

    @rjzamora
    Member

    Thanks for sharing this experience @secrettoad - It will always be a bit expensive to read thousands of footer-metadata chunks from s3. However, I'm pretty sure you are just running into an unfortunate edge case (bug?) where we are doing the path-based filtering after spawning the metadata tasks when we could do that kind of filtering up-front.

    I would expect performance to improve for your case after #9637 is merged. If you get a chance to test that PR out before then, I'd be interested to get your feedback :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions