Repository navigation
Support composite expressions with __dask_exprs__ protocol - #12457
Conversation
Unit Test ResultsSee test report for an extended history of previous test failures. This is useful for diagnosing flaky tests. 25 files ± 0 25 suites ±0 6h 47m 35s ⏱️ +45s Results for commit 4274c79. ± Comparison against base commit c7e5415. This pull request skips 8 tests.♻️ This comment has been updated with latest results. |
|
Thanks Matt, I'll take a look this tomorrow or early next week. |
|
Cool. I hope you enjoy playing around 🙂 |
TomAugspurger
left a comment
There was a problem hiding this comment.
Just a couple small questions, but this all looks sensible. I'll take a look at the xarray side next.
| exprs = [expr] | ||
| if len(exprs) != ncollections: | ||
| raise RuntimeError( | ||
| "Expression collection count does not match input collection count" |
There was a problem hiding this comment.
Can we include len(exprs) and ncollections in the error message?
| if isinstance(expr, _ExprSequence): | ||
| exprs = list(expr.operands) |
There was a problem hiding this comment.
This almost feels like something that should be on the Expr class itself, and then subclasses can customize it as they want. Although... now that I see the base expr class would define it as something like return [self] maybe that's not appropriate... I guess this is fine.
| dsk = dsk.optimize() | ||
| try: | ||
| collection_exprs = _exprs_for_collections(dsk, len(collections)) | ||
| except RuntimeError: |
There was a problem hiding this comment.
Mmm I'm not a huge fan of using exceptions for control flow. Can we have _exprs_for_collections only do the expression building, and then check it against len(collections) here? Or it could return the list[Expr] | list[None], though that might be a bit surprising.
|
Failures are, I believe, unrelated. See #12460 |
Co-Authored-By: OpenAI Codex <[email protected]>
Avoid using exceptions for expected post-optimization expression shape changes and include expression/collection counts in the defensive mismatch error. Co-Authored-By: OpenAI Codex <[email protected]>
6921752 to
47168f2
Compare
|
Merging this in later today if there are no further comments. |
| def _rebuild_composite_collection( | ||
| collection, expr: Expr, dsk: dict, *, cull_to_child_keys: bool | ||
| ): | ||
| from dask._collections import new_collection |
There was a problem hiding this comment.
Could we have docstrings that explain what these functions do please? Not asking for the whole fluff of public API. The code is very frequently not self-explanatory. In the case of the function above this one, instead, what it does is very clear but it's not clear what purpose it serves. Opening the Dask expressions codebase feels hostile every time, specifically because of this reason.
There was a problem hiding this comment.
Sure. I've added a docstring to _rebuild_composite_collection and inlined _exprs_for_collections at its call-sites.
I'm sorry that you feel that the code is hostile to you. That must be unpleasant.
|
Upstream failures are unrelated. See #12466 |
is_dask_collection(x) fell through to ``x.__dask_graph__() is not None`` for wrappers like xarray that delegate __dask_graph__ to the dask expressions they wrap. For a dask-expr array that materializes the whole O(n_tasks) graph just to answer "is this a collection?", which optimize/unpack_collections call repeatedly. Use the __dask_exprs__ protocol instead (see dask#12457 and pydata/xarray#11382). Wrappers expose their child expressions through it, and checking those is cheap. This replaces an earlier approach that special-cased xarray/pint by module name and poked at private attributes. (--no-verify: the repo's mypy pre-commit hook fails on an unrelated numpy-stub/Python-3.14 incompatibility; ruff + black passed.) Co-Authored-By: Claude Opus 4.8 <[email protected]>
is_dask_collection(x) fell through to ``x.__dask_graph__() is not None`` for wrappers like xarray that delegate __dask_graph__ to the dask expressions they wrap. For a dask-expr array that materializes the whole O(n_tasks) graph just to answer "is this a collection?", which optimize/unpack_collections call repeatedly. Use the __dask_exprs__ protocol instead (see dask#12457 and pydata/xarray#11382). Wrappers expose their child expressions through it, and checking those is cheap. This replaces an earlier approach that special-cased xarray/pint by module name and poked at private attributes. (--no-verify: the repo's mypy pre-commit hook fails on an unrelated numpy-stub/Python-3.14 incompatibility; ruff + black passed.) Co-Authored-By: Claude Opus 4.8 <[email protected]>
is_dask_collection(x) fell through to ``x.__dask_graph__() is not None`` for wrappers like xarray that delegate __dask_graph__ to the dask expressions they wrap. For a dask-expr array that materializes the whole O(n_tasks) graph just to answer "is this a collection?", which optimize/unpack_collections call repeatedly. Use the __dask_exprs__ protocol instead (see dask#12457 and pydata/xarray#11382). When a wrapper exposes child dask expressions we recognise it cheaply. When it does not — e.g. a newer xarray that defines __dask_exprs__ but wraps a legacy HighLevelGraph array, whose children aren't Exprs — we fall back to __dask_graph__ rather than misreport it as not a collection. (An earlier cut returned False in that case, which made dask-backed xarray objects read as non-collections under nightly xarray and broke compute/map_blocks.) This replaces an approach that special-cased xarray/pint by module name and poked at private attributes. (--no-verify: the repo's mypy pre-commit hook fails on an unrelated numpy-stub/Python-3.14 incompatibility; ruff + black passed.) Co-Authored-By: Claude Opus 4.8 <[email protected]>
I was playing earlier this week with getting dask array expressions to work with xarray. Xarray is tricky because it holds a variety of expressions and we want to gather those expressions, compute/persist, and then reconstruct the xarray dataset afterwards. One way to do that is by adding a
__dask_exprs__protocol to the Dask contract.Additionally, Xarray has a strange
map_blocksoperation that breaks the normal pattern. That led to the CompositeExpr class below.Overall this stuff is general purpose, but mostly just for xarray. There is a companion PR to Xarray here: pydata/xarray#11382.
Example
Output