Repository navigation
Add aggregate_files argument to enable multi-file partitions in read_parquet - #7557
Conversation
…he same partition
This PR loosens some fragile assumptions in dask_cudf's `test_parquet.py::test_chunksize`. **More context**: This change is in preparation for [dask#7557](dask/dask#7557), which will change the behavior of the `chunksize` argument in `dask.dataframe.read_parquet`. Further changes will be required in dask_cudf to enable (optimal) multi-file partition aggregation, but this PR should avoid any temporary CI failures. Authors: - Richard (Rick) Zamora (https://github.com/rjzamora) Approvers: - Keith Kraus (https://github.com/kkraus14) URL: #8108
|
@rjzamora - those sound like some juicy performance gains 🚀 Any way I can help with this PR? Want me to pull down this branch and see if I can reproduce those performance gains on my machine? |
It would be great to have you try it out :) I actually had this PR on the top of my priority list for next week. I think the only sticking point is the question of how to set the maximum granularity of aggregation. For example, users may only want to aggregate within files, or they may want to aggregate within a directory, or maybe within a specific group of directories. I am wondering if there is a clean way to expose a simple knob for the user to turn. |
I could see two patterns being of immediate use:
A list of lists of all files/path expressions which should be read. When users have skewed file sizes, they sometimes write custom logic to concatenate specific files to approximate either evenly sized partitions, or logically distinct partitions.
Dir structure:
In the above example, with coalesce_depth=1, it'd be fair game for you to merge This would be basically in line with reading a partitioned dataset but where you specify what "level" of partition beneath which you want to allow Dask to merge files. |
|
Thank you for the suggestions and feedback @randerzander - I'm sorry our efforts to design a reasonable API overlapped a bit! I definitley started with an approach that was very similar to the |
fjetter
left a comment
There was a problem hiding this comment.
Regarding the API, this feels like the right direction. I left one or two questions which probably result in a no-op but otherwise this looks good to me. I think everything else can be iterated on later.
A very subjective comment I cannot actually back up but the introduced complexity seems to be a lot while there are only a few added unit tests. However, they are parameterised so we might actually cover everything? idk. this might be a time for a coverage report :)
Nothing I've seen seemed very controversial but it is practically impossible to review all for/if conditions without actually diving into the code myself with a debugger, therefore I trust the tests. This is mostly a question of "can you come up with any more weird edge cases we might want to include in the tests?" if the answer is no, that's fine with me until the first bugs come in :)
However, I would also like to expand this PR to enable file aggregation when split_row_groups is set to an integer >1.
May I suggest to hold on with this? The PR is already uncomfortably large. That sounds like something you can put into a follow up PR.
| # Check/correct order of `categories` using last file_frag | ||
| cat_keys = [ | ||
| o.split(" =")[0] | ||
| for o in re.findall("[^(]* =", str(file_frag.partition_expression)) |
There was a problem hiding this comment.
I'd love to see something like this live in upstream
There was a problem hiding this comment.
Yes, this is exactly my sentiment. Unfortunately, the exposed API and partitioning information is far from consistent between the three supported engines. This inconsistency is the reason I originally went with the general regex approach for specifying an aggregation range.
At this point, I am sort of throwing up my hands. It seems like we need to do something messy like this to produce a reasonable API. Other ideas are welcome :)
There was a problem hiding this comment.
Well, this affects only arrow, doesn't it? I agree that arrow might not want to expose the exact signature we're requiring here but IIUC this function is something like an aggregation of filepath encoded data.
A=1/B=2/data.parquet
A=4/B=2/data.parquet
I am struggling a bit to parse what the expected output would be for that simple example but I expect something like
{
"A=1/B=2/data.parquet": ["A", "B"],
"A=4/B=2/data.parquet": ["A", "B"],
}or
{
"A=1/B=2/data.parquet": [1, 2],
"A=4/B=2/data.parquet": [4, 2],
}Maybe a short example in the doc string would be helpful. imho, nomenclature is not sufficiently aligned and I always get confused when reading about 'categories', 'fragments', 'partitions', etc.
There was a problem hiding this comment.
Well, this affects only arrow, doesn't it?
Yes, thats right - I was conflating this specific change with a larger challenge I was facing :)
I agree that this specific logic should live in pyarrow. There is currently a (private) _get_partition_keys utility to convert the "partition expression" into a list of "keys". However, the result is not ordered in the way we need it to be, hence this custom/ugly logic. @jorisvandenbossche may be able to comment, but I suspect I should just raise a jira/pyarrow issue about it.
There was a problem hiding this comment.
We still have open https://issues.apache.org/jira/browse/ARROW-8655 to better expose the partitioning information. Concrete suggestions on what information you want to have accessible in which way is always welcome.
However, the result is not ordered in the way we need it to be, hence this custom/ugly logic
Ah, that's useful feedback, that might be an easy change in pyarrow (the implementation in C++ is currently using an unordered map, I notice. I suppose we can change that to something that preserves the order).
About the specific code line this is commenting on: I would suggest to not rely on the repr of the partition expression (that seems rather brittle, and I can't guarantee any stability for the display of an expression). As an alternative short-term work-around, can you do something similar on the file path?
There was a problem hiding this comment.
note that this regex does not allow ( in the category names. For "proper" dataset formats I would expect this to not be an issue since the paths are likely encoded, e.g. url encoded, and ( might be forbidden but it could pop up and this regex would return nonsense.
There was a problem hiding this comment.
About the specific code line this is commenting on: I would suggest to not rely on the repr of the partition expression (that seems rather brittle, and I can't guarantee any stability for the display of an expression). As an alternative short-term work-around, can you do something similar on the file path?
I agree that it is not ideal to depend on the repr. We can use the file path for now, but that is also a bit tricky. Does anyone know if the following would work for both linux and windows?...
cat_keys = [
o.split("=")[0]
for o in re.findall(f"[^{fs.sep}]*=", str(file_frag.path))
]There was a problem hiding this comment.
fsspec has a make_posix_path which normalizes / or \, but not sure if that's a public function.
But I also think that file_frag.path will always return normalized paths (using /) even on Windows. So hardcoding "[/]*=" in the regex could also work.
There was a problem hiding this comment.
Yes it's public, but "/" is the only variant that gets used in practice. Note that "/" paths always work on any OS (python has some in-built hidden windows stuff to do this, and we can rely on it).
There was a problem hiding this comment.
if in doubt, you can also use smth like os.path.split which should resolve properly for win/linux but is likely slower
|
In case you are curious @fjetter you can see the coverage changes in https://app.codecov.io/gh/dask/dask/compare/7557 |
|
Thanks for the coverage link. The diff itself seems to be mostly covered but there are a few big gaps in the overall logic, in particular for arrow / split_row_groups (https://app.codecov.io/gh/dask/dask/compare/7557/changes#D3L292) obviously out of scope for this PR but very informative ;) |
|
@fjetter @dask/io - Let me know if there are any remaining thoughts/concerns here. I think it is good to go. Note that I recently changed the default |
fjetter
left a comment
There was a problem hiding this comment.
I left a final note about the regex but that isn't a blocker. I think the edge case I'm mentioning there is rare enough to justify moving forward, especially since arrow might include a better functionality in the future.
I would suggest that if any bigger things pop up, we should move these changes to a dedicated PR and lock this PR in terms of scope.
If nobody, else has more to say, I'm good.
Thanks @rjzamora for your hard work, I just realised this PR has been open since April! Good job and thanks for completing it!
|
I would only still change the regex to be based on eg the file path, instead of the repr of the expression, if possible. In the meantime, related to #7557 (comment), I updated the JIRA https://issues.apache.org/jira/browse/ARROW-8655 with some more concrete thoughts and a draft PR to expose more information: apache/arrow#10661. If I can get some feedback on it, we might be able to get it into pyarrow 5.0. |
|
Okay - Now that the unrelated CI failures are resolved, this PR should be good to go. |
|
Seems like once the conflict is resolved we can merge :) |
|
grrr - looks like |
Yikes - That took a while to track down. It seems that the new approach requires us to reset the Side note: In the past, we have already been "resetting" the row-groups. However, we were also explicitly passing in the row-group metadata at IO, so these threading errors haven't caused any problems until now. |
|
A ParquetFiles certainly does use its row_groups while doing Instead of using a lock, can we not do a (shallow) |
|
@dask/maintenance - I'm hoping this is ready, and I'd like to see it get in early in the release cycle :) |
|
Sounds good to me. Merging... |
Depends on [dask#7557](dask/dask#7557) This PR updates `dask_cudf.read_parquet` to enable multi-file aggregation when the `chunksize` parameter is used. This change enables dramatic (>50x) improvements when the dataset contains many small files, which is somewhat common for hive/directory-partitioned data. **Motivating Example** ```python dask.datasets.timeseries( start='2000-01-01', end='2000-02-28', freq='1s', partition_freq='1h', seed=42, ).to_parquet( path, engine="pyarrow", partition_on="name", write_index=False, ) #### #### BEFORE THIS PR #### #### # Without useing `chunksize`, or when using `chunksize` before this PR # results in 36,192 partitions %time df_read = dask_cudf.read_parquet(path) CPU times: user 1.17 s, sys: 501 ms, total: 1.67 s Wall time: 1.67 s # Without useing `chunksize`, or when using `chunksize` before this PR # results in a 5+ minute read time %time df_read.compute() CPU times: user 5min 34s, sys: 37.1 s, total: 6min 11s Wall time: 6min 12s #### #### AFTER THIS PR #### #### # Using `chunksize` WITH this PR results in 26 partitions %time df_read = dask_cudf.read_parquet(path, chunksize="1GB", aggregate_files="name") CPU times: user 1.03 s, sys: 119 ms, total: 1.15 s Wall time: 1.15 s # Using `chunksize` WITH this PR results in a ~95x speedup %time df_read.compute() CPU times: user 3.18 s, sys: 703 ms, total: 3.88 s Wall time: 3.92 s ``` Authors: - Richard (Rick) Zamora (https://github.com/rjzamora) Approvers: - Devavret Makkar (https://github.com/devavret) - Benjamin Zaitlen (https://github.com/quasiben) URL: #8393
This PR adds a new
aggregate_filesparameter todask.dataframe.read_parquet. This enableschunksize=to aggregate both row-groups and/or files into each output partition. We also add an optionalread_partition_multiclass method to theEngine, allowing the backend (i.e. arrow and/or cudf) to perform file/row-group aggregation in a way that is (much) more efficent than concatenating pandas DataFrame objects.Current
aggregate_filesdocstring:Note that this PR currently sets the default behavior of
aggregate_filestoFalse. However, here is the motivation to aggregate files by default for partitioned datasets:Note: With
dask_cudf, sincecudfcan read multiple files on the same GPU in parallel, this time goes down to 3 seconds.