Support a mutable/appendable table for in-SQL iterative state (e.g. optimizer trajectories)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
- #206 của @JHON12091986 — đã đóng, không merge
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
Hướng nghiên cứu
The issue points to the _table_schemas() path and the autograd Substrait rewrite, but does not name their files; start by locating where schemas come from _registered_datasets and how rewrite tables are registered. The proposed direction is a separate mutable scratch table exposed from Python, with schema support in the rewrite. Done means SQL can append optimizer state without rebuilding xarray history; the issue recommends deferring this work.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Motivation
The autograd work (#192) makes it possible to run gradient descent in SQL — the gradient of a loss is AVG(grad(loss, θ)), and an update is θ_new = θ - lr * AVG(grad(loss, θ)). A natural, SQL-native way to express the optimizer is a parameter table that grows one row per step, so the whole training trajectory becomes a relation you can query (e.g. the loss curve in a single GROUP BY). See the discussion in https://github.com/alxmrs/xarray-sql/pull/192#discussion_r3488049015.
Today that "append a new generation" step can't happen in SQL. The tables registered via XarrayContext.from_dataset are backed by a read-only, lazy Arrow stream provider (LazyArrowStreamTable), so there is no INSERT INTO. The demo works around it by rebuilding the whole history as an xarray Dataset and re-registering it each step:
def register_params():
ctx.deregister_table("params")
ds = xr.Dataset({"a": (("step",), a_hist), "b": (("step",), b_hist)},
coords={"step": steps})
ctx.from_dataset("params", ds, chunks={"step": len(steps)})
It's correct and fine for a demo, but it's the one un-SQL-native part of an otherwise fully-relational training loop, and it re-materializes O(history) each step.
What we'd want
Some way to do, in SQL:
INSERT INTO params
SELECT :next_step,
cur.a - lr * AVG(grad(loss, a)),
cur.b - lr * AVG(grad(loss, b))
FROM d CROSS JOIN (SELECT a, b FROM params WHERE step = :k) cur
GROUP BY cur.a, cur.b;
so the optimizer's state lives in the engine and the Python side only drives iteration (or, eventually, doesn't — see "recursive CTE" below).
Design tension (why "xarray-backed append" specifically is awkward)
The obvious framing — "make xarray tables appendable" — fights the data model:
- xarray/Zarr tables model immutable, dense, gridded arrays. A SQL row is one cell
(coords…, values…). "Appending a row" to a gridded array isn't anINSERT; it's growing/concatenating along a coordinate dimension (a rechunk/xr.concat), which only makes sense for some tables (a 1-Dparams(step)) and not others (a 3-D(time, lat, lon)field). - The real need that surfaced is a small mutable scratch table for iteration state, which is conceptually separate from the large immutable data tables we read from Zarr.
So I'd frame this as "support mutable state tables," not "make xarray reads mutable."
Options
- Mutable in-memory scratch table (recommended if we do anything). Register a DataFusion-native mutable table (e.g. a
MemTable, which already supportsINSERT INTOin DataFusion) for iteration state, kept separate from xarray-backed tables. Work needed: expose a way to create/register such a table from Python, and make the autograd Substrait rewrite aware of it (it currently resolves table schemas from_registered_datasets, so a non-xarray table would need to be included in_table_schemas()/ registered as an empty table on the rewrite side). - Appendable xarray/Zarr-backed provider. A
TableProviderthat supportsinsert_intoby appending along a dimension (or writing to a Zarr store). More work, and only natural for 1-D / append-along-one-dim tables; risks implying mutability for gridded data where it doesn't belong. - Recursive CTE for the whole loop. The most ambitious "fully in SQL" answer: express N steps as
WITH RECURSIVE. Blocked today — DataFusion's recursive-CTE support is limited (aggregates in the recursive term), and recursive CTEs + scalar subqueries don't round-trip through Substrait yet (the demo already hit "Cannot convert <subquery> to Substrait"). Tracking-only for now.
Do we think we should implement this?
My recommendation: not a priority, and not as "appendable xarray tables." Reasons:
- The current re-registration workaround is correct and adequate for the demo; the per-step re-materialization cost is small for the state tables this is about.
- "Append to xarray-backed tables" cuts against the immutable-gridded-array model that is the library's core value; baking mutability into that provider adds surface area and conceptual confusion for little gain.
- If/when we want in-engine iteration, the clean path is a separate mutable scratch table (option 1), not mutating the data tables — and even that is arguably premature until there's a use case beyond the demo.
Filing this mainly to capture the idea and the design reasoning so the trade-off is on record. Happy to be argued out of it — option 1 is genuinely appealing for the "differential database" direction (a training loop whose state is a table), and a recursive-CTE loop (option 3) would be the dream once the engine/Substrait support lands.
Context: came out of building the autograd feature (#192) and its gradient-descent / MNIST-MLP-in-SQL demos.
- Ngôn ngữ chính
- Python
- Star
- 142
- Fork
- 19
- Merge trung bình
- 1 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 9
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của xqlsystems/xarray-sql
-
documentation
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 85/100
xqlsystems/xarray-sql#268 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
xqlsystems/xarray-sql#267 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 74/100
xqlsystems/xarray-sql#243 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
publish.yml: the TestPyPI verification step is unreachable (and installs the wrong package name)Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
xqlsystems/xarray-sql#242 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
xqlsystems/xarray-sql#237 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của xqlsystems/xarray-sql
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
SwiftKickMobile/skai#84 ·
-
Lychee link check failedĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
jxxghp/MoviePilot#7070 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
flop-labs/technocore-chat#997 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
tunneld on Windows: Wi-Fi devices are not discovered because uvicorn runs on a ProactorEventLoopĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
doronz88/pymobiledevice3#2051 ·
Maintainer thường phản hồi trong vòng 1 ngày