to_dataset: without ORDER BY, coordinates come back in the engine's nondeterministic row order
Mantenedores costumam responder em até 3 dias
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 48/100
Direção de pesquisa
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.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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_equalagainst 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_equalagainst 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.
- Linguagem predominante
- Python
- Estrelas
- 142
- Forks
- 19
- Merge médio
- 3d 47min
- PRs com merge (30d)
- 8
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de xqlsystems/xarray-sql
-
dependencies github_actions
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
xqlsystems/xarray-sql#295 ·
Mantenedores costumam responder em até 3 dias
-
A big-endian data variable registers, then every query fails: Byte-swapped arrays not supportedAbertabug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
xqlsystems/xarray-sql#288 ·
Mantenedores costumam responder em até 3 dias
-
documentation ml
Dificuldade 1/5 1-3 horas Facilidade para iniciantes 85/100
xqlsystems/xarray-sql#268 ·
Mantenedores costumam responder em até 3 dias
-
enhancement ml
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 64/100
xqlsystems/xarray-sql#267 · 4 comentários ·
Mantenedores costumam responder em até 3 dias
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 74/100
xqlsystems/xarray-sql#243 ·
Mantenedores costumam responder em até 3 dias
Todas as issues de xqlsystems/xarray-sql
Issues semelhantes
-
documentation good first issue
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 88/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 84/100
MarketSquare/robotframework-browser#5479 ·
Mantenedores costumam responder em até 1 dia
-
Visual COBOL: missing 11.0 and 12.0, and 9.0 date differs from Rocket Software's lifecycle pageAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
endoflife-date/endoflife.date#11561 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
OCA/l10n-brazil#5602 ·
Mantenedores costumam responder em até 1 dia
-
documentation review-2.0.0
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
dkuenzel/meshcore-home-gateway#106 ·
Mantenedores costumam responder em até 1 dia