Accept `optimizations` in the streaming engines' `execute` #25662
<!-- coderabbit-ai-generated:v1 -->
<!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- review_stack_entry_start -->
<a href="https://app.coderabbit.ai/change-stack/NVIDIA/cudf/pull/25662?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg" alt="Explain Change →" height="32"></picture></a>
<!-- review_stack_entry_end -->
<!-- recent_review_status_start -->
No actionable comments were generated in the recent review.
<!-- recent_review_status_end -->
<!-- walkthrough_start -->
<details>
<summary><strong>Summary</strong></summary>
<dl>
<dd>
<br>
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
Summary by CodeRabbit
New Features
Distributed query engines now accept optional optimization settings, allowing you to control which query optimizations are applied. If omitted, default optimizations are used.
Persisted query results now support Polars’ query-result interface. The head and total-row-count metadata are reported as unavailable.
Documentation
Updated the API reference to describe the persisted query result metadata.
Tests
Added coverage for optimization settings, result metadata, and collecting persisted query results.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
DaskEngine, RayEngine, and SPMDEngine accept optional optimization flags and pass them to persisted-query execution. PersistedQueryResult implements Polars’ QueryResult protocol with head and n_rows_total properties that return None. Tests cover result compatibility, collection, and optimization effects on translated plans.
Changes: Polars engine execution
|Layer / File(s)|Summary|
|:---|:---|
|Persisted result and optimization handling <br> python/cudf_polars/cudf_polars/engine/persisted_result.py, docs/cudf/source/cudf_polars/api.md, docs/cudf/source/conf.py|PersistedQueryResult adds head and n_rows_total properties that return None. execute_persisted_query accepts optional optimization flags and applies them before translation when provided. The API reference lists the new properties, and Sphinx ignores the pl.QueryOptFlags class target during nitpicky checks.|
|Engine forwarding and execution tests <br> python/cudf_polars/cudf_polars/engine/dask.py, python/cudf_polars/cudf_polars/engine/ray.py, python/cudf_polars/cudf_polars/engine/spmd.py, python/cudf_polars/tests/test_engine_execute.py|The three engines accept and forward optional optimization flags. Tests check result compatibility, collection, and translated plan roots for selected optimization settings.|
<!-- change_assessment_start -->
Estimated code review effort: 3 (Moderate) | ~20 minutes
<!-- change_assessment_commit:"8bd98869ef78199f5031387cdc0095b22312dbc1" -->
<!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} -->
Change: Bug fix · Severity of issue fixed: Medium
<!-- change_assessment_end -->
Possibly related issues
PersistedQueryResult that implements Polars’ QueryResult protocol. Other gaps described in that issue are not addressed.Suggested reviewers: tomaugspurger
</dd>
</dl>
</details>
<!-- walkthrough_end -->
<details>
<summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-unassessed-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minimal</strong> · up to <code>8bd98</code></summary>
<dl>
<dd>
<br>
<!-- final_review_risk_start -->
<!-- final_review_risk_label
Merge Risk: ⚪ Minimal · up to 8bd98
-->
<!-- final_review_risk_coverage:{"sourceCommitId":"8bd98869ef78199f5031387cdc0095b22312dbc1","coveredCommitId":"8bd98869ef78199f5031387cdc0095b22312dbc1","kind":"reviewed"} -->
This change lets lf.execute(engine=...) work with the SPMD, Dask, and Ray engines and accept optimization flags. Existing calls behave as before, and the new tests cover the main paths. No merge-blocking risk is evident.
<!-- final_review_risk_end -->
</dd>
</dl>
</details>
<details>
<summary><strong>Security architecture risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-unassessed-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minimal</strong> · up to <code>8bd98</code></summary>
<dl>
<dd>
<br>
<!-- architecture_review_start -->
<!-- architecture_review_label
Security Architecture Review
Security architecture risk: ⚪ Minimal · up to 8bd98
-->
Polars callers can now pass optimizer flags to the streaming engines and receive a GPU-resident result. The change uses the same engine, validation, and cleanup path as direct engine execution. The result's preview and total row count remain unsupported.
<details>
<summary><strong>Security review details</strong></summary>
<dl>
<dd>
<br>
Security Blast Radius
Trust Boundaries and Controls
Data and control flow
Resilience and Maintainability Implications
</dd>
</dl>
</details>
<!-- architecture_review_end -->
</dd>
</dl>
</details>
<!-- pre_merge_checks_walkthrough_start -->
<details>
<summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4</strong></summary>
<dl>
<dd>
<br>
| Check name | Status | Explanation |
| :------------------------ | :--------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Title check | !Passed | The title names the main change: the streaming engines' execute methods now accept the optimizations argument. The raw summary confirms SPMDEngine, DaskEngine, and RayEngine each gained this… |
| Description check | !Passed | The description matches the changeset. It explains that lf.execute(engine=...) now works with SPMDEngine, DaskEngine, and RayEngine. It describes the new keyword-only optimizations argument … |
| Linked Issues check | !Passed | [#25651] The issue requires three coding behaviors. (1) lf.execute(engine=SPMDEngine()) must accept the optimizations keyword instead of raising TypeError. (2) Non-None flags must apply to the… |
| Out of Scope Changes check | !Passed | Every changed area maps to #25651 or its stated scope. spmd.py, dask.py, and ray.py add and forward the optimizations argument, and the TODOs in these files name the same mismatch. persisted_… |
<details>
<summary><strong>Full details: Title check</strong></summary>
<dl>
<dd>
<br>
Explanation
The title names the main change: the streaming engines' execute methods now accept the optimizations argument. The raw summary confirms SPMDEngine, DaskEngine, and RayEngine each gained this keyword-only argument, forwarded to execute_persisted_query. The title is specific and matches the changeset. It omits the PersistedQueryResult protocol change, which is acceptable because the title need not cover every detail.
</dd>
</dl>
</details>
<details>
<summary><strong>Full details: Description check</strong></summary>
<dl>
<dd>
<br>
Explanation
The description matches the changeset. It explains that lf.execute(engine=...) now works with SPMDEngine, DaskEngine, and RayEngine. It describes the new keyword-only optimizations argument and the PersistedQueryResult protocol changes, where head and n_rows_total return None. It also links issue #25651 and lists the checklist items.
</dd>
</dl>
</details>
<details>
<summary><strong>Full details: Linked Issues check</strong></summary>
<dl>
<dd>
<br>
Explanation
[#25651] The issue requires three coding behaviors. (1) lf.execute(engine=SPMDEngine()) must accept the optimizations keyword instead of raising TypeError. (2) Non-None flags must apply to the lazy frame before translation. (3) The call must return a PersistedQueryResult, and direct engine.execute(lf) must keep working. At the current head, SPMDEngine.execute, DaskEngine.execute, and RayEngine.execute each take a keyword-only optimizations: pl.QueryOptFlags | None = None and forward it to execute_persisted_query. This review read the dask.py and persisted_result.py diffs against the merge base directly. execute_persisted_query calls ldf.with_optimizations(optimizations._pyoptflags) before Translator(ldf.visit(), engine). The return type is unchanged, and the default None path keeps direct calls working. PersistedQueryResult provides head and n_rows_total returning None, which the PR states but the issue does not require. test_engine_execute.py covers the lf.execute(engine=...) round trip and the HStack versus Filter root difference for predicate_pushdown=False, which addresses the issue's plan-change point. The issue's older Polars version compatibility point is not covered by any test in the PR. The previous PASS remains supported.
</dd>
</dl>
</details>
<details>
<summary><strong>Full details: Out of Scope Changes check</strong></summary>
<dl>
<dd>
<br>
Explanation
Every changed area maps to #25651 or its stated scope. spmd.py, dask.py, and ray.py add and forward the optimizations argument, and the TODOs in these files name the same mismatch. persisted_result.py adds the optimization hook in execute_persisted_query and the QueryResult protocol properties head and n_rows_total, which support the issue's compatible-result requirement. The docs changes in conf.py (ignoring pl.QueryOptFlags as a nitpick target) and api.md (documenting head and n_rows_total) support the new public API surface. The tests in test_engine_execute.py cover the same behavior. No unrelated module or behavior change was found in the inspected diffs.
</dd>
</dl>
</details>
</dd>
</dl>
</details>
<!-- pre_merge_checks_walkthrough_end -->
<!-- finishing_touch_checkbox_start -->
<details>
<summary><strong>Finishing Touches</strong></summary>
<dl>
<dd>
<br>
<details open>
<summary><strong>Generate unit tests (beta)</strong></summary>
<dl>
<dd>
<br>
</dd>
</dl>
</details>
</dd>
</dl>
</details>
<!-- finishing_touch_checkbox_end -->
<!-- autopilot:start -->
<!-- autopilot:end -->
<!-- recent_review_start -->
<h6>
<details>
<summary><strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Recent review info</strong></summary>
<dl>
<dd>
<br>
<!-- review_info_content -->
<details>
<summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</strong></summary>
<dl>
<dd>
<br>
</dd>
</dl>
</details>
<details>
<summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</strong></summary>
<dl>
<dd>
<br>
Reviewing files that changed from the base of the PR and between 08d8ab7a1e3621c3c726436b790a14690182fb5e and 8bd98869ef78199f5031387cdc0095b22312dbc1.
</dd>
</dl>
</details>
<details>
<summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 3</strong></summary>
<dl>
<dd>
<br>
docs/cudf/source/conf.pydocs/cudf/source/cudf_polars/api.md
python/cudf_polars/cudf_polars/engine/persisted_result.py`
</dd>
</dl>
</details>
<details>
<summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-similar-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-similar.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files skipped from review as they are similar to previous changes: 1</strong></summary>
<dl>
<dd>
<br>
</dd>
</dl>
</details>
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.
</dd>
</dl>
</details>
</h6>
<!-- recent_review_end -->
Do we not have an existing test for this? If so, the new result.head and result.n_rows_total assertions could be added to that.
Approved these changes.
Not for lf.execute(engine=...), which was the bug, but test_execute_lazy_roundtrip covered the same round trip via engine.execute(lf). I've merged them: that test now goes through lf.execute(engine=...) and has the new assertions.
/merge
Thanks @madsbk!
TomAugspurger
madsbk
Description
This PR makes
lf.execute(engine=...)work withSPMDEngine,DaskEngineandRayEngine. It returns the engine's GPU-residentPersistedQueryResult, asengine.execute(lf)already does. Previously it raisedTypeError: ... got an unexpected keyword argument 'optimizations'.executenow takes an optional keyword-onlyoptimizationsargument, which is applied to the query before translation, so flags such aspl.QueryOptFlags(predicate_pushdown=False)take effect.PersistedQueryResultnow implements the PolarsQueryResultprotocol. Itsheadandn_rows_totalare not supported yet and areNonefor now.Closes #25651
Checklist