to_dataset: without ORDER BY, coordinates come back in the engine's nondeterministic row order

未关闭
#276 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
database

调研方向

Start in xarray_sql/ds.py at the pd.unique call that builds coord_arrays, then trace how those coordinates are used by the scatter path. Reproduce with the provided DuckDB GROUP BY query, comparing repeated runs and a result with ORDER BY ... DESC. Done means unordered results have deterministic coordinates while explicitly ordered results retain their order.

由索引模型根据 Issue 内容生成。

描述

to_dataset builds each dimension's coordinate in the order its values first appear in the result. That keeps an ORDER BY level DESC in the Dataset, which is the intent (see the comment in xarray_sql/ds.py, where pd.unique builds coord_arrays). But without an ORDER BY, SQL leaves the row order unspecified. DuckDB's parallel scans return rows in a different order on each run, so the same query gives Datasets whose coordinates are in different orders, and rarely in the template's order.

Repro

import numpy as np, pandas as pd, xarray as xr, duckdb
import xarray_sql as xql

ds = xr.Dataset(
    {"temp": (("time", "lat", "lon"), np.arange(12.0).reshape(3, 2, 2) + 10)},
    coords={"time": pd.date_range("2024-01-01", periods=3),
            "lat": [10.0, 20.0], "lon": [100.0, 110.0]},
)
con = duckdb.connect()
xql.register(con, "weather", ds)
for _ in range(3):
    rel = con.sql("SELECT lat, lon, MAX(temp) AS temp FROM weather GROUP BY lat, lon")
    out = xql.to_dataset(rel, template=ds)
    print(out.lat.values, out.lon.values)

Output (xarray-sql main, DuckDB 1.5.5):

[20. 10.] [100. 110.]
[10. 20.] [110. 100.]
[20. 10.] [110. 100.]

Labels are right (out.temp.sel(lat=20.0, lon=110.0) is 21.0 on every run), so nothing is wrong cell by cell. But anything positional is affected:

  • .values;
  • .isel;
  • plots, which draw the axis in that order;
  • xr.testing.assert_equal against the source;
  • concatenating results from two runs.

That's surprising for an Xarray user, since Xarray coordinates from a store are almost always monotonic.

Expected

When the result's order says nothing about a dimension, its coordinate should come back in a deterministic order. Two options:

  • Follow the template's order for that dimension. The template is usually ascending, and this makes assert_equal against the source work.
  • Sort ascending when there's no template.

A result whose rows are monotonic along a dimension (an ORDER BY, including DESC) would keep that order, as now.

One way to decide: if each dimension's first-appearance coordinate is monotonic, keep it; otherwise sort it, or reindex it to the template. The scatter path already argsorts, so the cost is in choosing coord_arrays, not in placing values.

Workaround

Add ORDER BY on the dimensions. The Performance guide already recommends it for speed ("Round-trip faster with ORDER BY"), but not for determinism.

主要语言
Python
星标
142
派生
19
平均合并
3 天 47 分钟
30 天内合并 PR
8

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

xqlsystems/xarray-sql 的其他 Issue

查看 xqlsystems/xarray-sql 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。