Make grad() an in-planner rewrite (via FFI) so it works inside any query — recursive CTEs, DML
维护者通常 1 天内回复
还没有人认领这个 Issue。
- #205 来自 @JHON12091986 —— 已关闭,未合并
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 25/100
调研方向
Start by reading the autograd implementation and the DataFusion FFI UDF code in datafusion-ffi 52, especially datafusion/ffi/src/udf/mod.rs. The preferred direction is to forward ScalarUDFImpl::simplify across the FFI boundary, including a way to pass argument Expr values. Done means grad() can be rewritten inside recursive CTEs, DML, and scalar subqueries without the plan-level Substrait round trip.
由索引模型根据 Issue 内容生成。
描述
Context
The autograd grad() surface (#192) is applied by round-tripping the whole logical plan through Substrait: XarrayContext.sql() plans the query, produces a Substrait plan, the native extension (grad_rewrite) consumes it, rewrites every grad(...) into the differentiated expression, and re-produces Substrait for Python to execute.
This works for ordinary SELECT / aggregate / join plans, but Substrait can't represent every plan shape, so grad() can't appear inside:
- Recursive CTEs —
to_substrait_planrejectsRecursiveQuery(NotImplemented("Unsupported plan type: RecursiveQuery ...")). This is what blocks a fully-declarative training loop withgradinside the recursion. - DML —
INSERT ... SELECT grad(...)doesn't convert (DML plans aren't produced to Substrait). - Scalar subqueries also don't convert (
Cannot convert <subquery> to Substrait).
In the gradient-descent demo (#195) we worked around this with differentiate_sql (differentiate once to SQL text, then iterate it in a recursive CTE that contains no grad marker). That's a good pattern, but the original goal — grad() directly inside the query, including recursive/DML — needs an upstream change.
Root cause
The native engine is a separate cdylib that statically links its own copy of DataFusion, so it can't share Expr/LogicalPlan objects with datafusion-python — hence the Substrait serialization bridge. Substrait is then the limiting factor: any plan shape Substrait can't carry, grad() can't be rewritten in.
The fix is to stop round-tripping plans and instead run the rewrite in-planner, as a native DataFusion rewrite that fires for any query shape. That requires one of the following upstream capabilities.
Upstream changes that would enable it
Option 1 (preferred): datafusion-ffi — forward ScalarUDFImpl::simplify for foreign UDFs
If a foreign (FFI-registered) scalar UDF could implement simplify, grad/jvp/vjp would be self-rewriting UDFs: their simplify() differentiates the argument Expr during the SimplifyExpressions pass, which runs for every query shape (recursive CTEs, DML, subqueries) with no Substrait involved.
- Today
FFI_ScalarUDF/ForeignScalarUDFonly forwardname,signature,return_type/return_field_from_args,coerce_types,invoke_with_args(seedatafusion/ffi/src/udf/mod.rsin datafusion-ffi 52) — notsimplify, so the default (identity) is used. - Fix: add
simplifyto theFFI_ScalarUDFvtable (anextern "C"entry) and call it fromForeignScalarUDF::simplify. The hard part is passing the argumentExprs across the C ABI — needs an FFI representation ofExpr(e.g. reuse datafusion-proto'sExpr(de)serialization, or SubstraitExtendedExpression, for just the UDF's args). This is the smallest, most surgical change and unblocks all query shapes at once. It would also let us delete the whole plan-level Substrait bridge.
Option 2: datafusion-python + datafusion-ffi — register a foreign AnalyzerRule / FunctionRewrite over FFI
Expose registering a foreign analyzer/optimizer rule on the SessionContext (e.g. SessionContext.register_analyzer_rule(<capsule>)), backed by a new FFI_AnalyzerRule in datafusion-ffi. Then grad is an analyzer rule that rewrites grad(...) during analysis for any plan. Broader than Option 1 (general extension point) but more API surface.
Option 3 (narrower / long path): Substrait support for recursion + DML
Keep the current bridge but teach it the missing plan shapes:
- DML: Substrait has a
WriteRel;datafusion-substraitwould need producer/consumer support for it soINSERT ... SELECT grad(...)round-trips. - Recursion: Substrait the spec has no recursive-relation construct, so this is an upstream substrait-io spec gap, not just a
datafusion-substraitchange — the longest path.
Recommendation
Pursue Option 1 (FFI simplify forwarding). It's the smallest upstream change, removes the Substrait round-trip and its codec complexity entirely, and makes grad() work inside recursive CTEs, DML, and subqueries — i.e. the fully-declarative training loop with grad in the query. Until then, differentiate_sql + a recursive CTE (see #195) is the supported way to express an in-SQL training loop.
Filed as a follow-up to the autograd feature (#192) and its demos (#195); related to the mutable-state discussion in #194.
- 主要语言
- Python
- 星标
- 142
- 派生
- 19
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 9
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
xqlsystems/xarray-sql 的其他 Issue
-
documentation
难度 1/5 1-3 小时 新手友好度 85/100
xqlsystems/xarray-sql#268 ·
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 64/100
xqlsystems/xarray-sql#267 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 74/100
xqlsystems/xarray-sql#243 ·
维护者通常 1 天内回复
-
publish.yml: the TestPyPI verification step is unreachable (and installs the wrong package name)未关闭
难度 2/5 1-3 小时 新手友好度 62/100
xqlsystems/xarray-sql#242 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
xqlsystems/xarray-sql#237 ·
维护者通常 1 天内回复
查看 xqlsystems/xarray-sql 的全部 Issue
相似的 Issue
-
good first issue tools
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
bug deferred domain:ci P1
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
docling-project/docling-mcp#147 ·
-
issue: A pipe model's Valves button ignores the Allow Chat Valves permission可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭bug confirmed issue
难度 2/5 1-3 小时 新手友好度 72/100
open-webui/open-webui#32771 · 1 条评论 ·
维护者通常 1 天内回复
-
Scheduled Security Scan is failing on main可能已有人在做 @pkupt 今天认领。 未关闭security-scan-failure
难度 1/5 1-3 小时 新手友好度 74/100
semantica-agi/semantica#2045 ·
维护者通常 2 天内回复