Repository navigation
Description
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
jsignell commentedon Jan 21, 2021
I suspect that what you are asking for is already supported at least for the
pyarrow-datasetengine. I recommend taking a close read of the documentation on theread_parquetfunction there are several places where row-groups are explicitly discussed.jmsmdy commentedon Jan 21, 2021
On a test dataset, I have compared the runtime of:
versus
versus
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:
run many times faster than:
which runs at almost exactly same speed as:
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).
jsignell commentedon Jan 21, 2021
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.
rjzamora commentedon Jan 21, 2021
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
getitemoptimization - 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.martindurant commentedon Jan 21, 2021
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.gforsyth commentedon Jan 22, 2021
The column selection should be working. This shouldn't make a difference, but what does timing look like if you do
jmsmdy commentedon Jan 22, 2021
@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.
I ran each of these five times (interlacing them to eliminate any potential effects of caching, just in case), and these were the results:
martindurant commentedon Jan 22, 2021
^ please also try with the two other engines, if you wouldn't mind
jmsmdy commentedon Jan 22, 2021
Adding other engines:
EDIT: here are the versions I am currently using, in case it is relevant:
I can't use newer versions as I am in an environment with a security policy that prevents it.
TomAugspurger commentedon Jan 22, 2021
#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.
gforsyth commentedon Jan 22, 2021
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
daskanddistributedare you using?rjzamora commentedon Jan 22, 2021
I am looking into this now - It seems that the column selection isn't pushed down to the
read_parquetcall if the thegetitemoperation was performed last. The problem is in this block of optimize_read_parquet_getitem. I'll see what can be done heremartindurant commentedon Jan 22, 2021
^ so
df.employee.sum().compute()(or some other operation that makes sense for that column) would have triggered the optimisation?rjzamora commentedon Jan 22, 2021
Yes - If you add any other layer(s) to the high-level graph, the optimization should be triggered.
rjzamora commentedon Jan 24, 2021
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).jmsmdy commentedon May 2, 2021
Look like #7415 (moving read_parquet to Blockwise, replacing #7042) was just merged.
secrettoad commentedon Feb 8, 2023
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.
secrettoad commentedon Feb 8, 2023
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.
rjzamora commentedon Feb 9, 2023
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 :)