When a query projects a dimension away, the rows that dimension separated collapse onto the same dim tuple. to_dataset builds a grid from them anyway, keeping whichever row comes last, and returns it without a warning. The docstring says it raises instead:
Raises:
ValueError: ``dims`` cannot be inferred, names a missing
column, or the result has duplicate dim tuples;
Repro on 0.3.3 and 0.4.0rc1:
import numpy as np
import pandas as pd
import xarray as xr
from xarray_sql import XarrayContext
temp = np.arange(12.0).reshape(3, 2, 2)
ds = xr.Dataset(
{"temperature": (["time", "lat", "lon"], temp)},
coords={
"time": pd.date_range("2020-01-01", periods=3),
"lat": [10.0, 20.0],
"lon": [100.0, 110.0],
},
)
ctx = XarrayContext()
ctx.from_dataset("temperature", ds, chunks={"time": 3, "lat": 2, "lon": 2})
result = ctx.sql("SELECT lat, lon, temperature FROM temperature")
print(len(result.to_pandas())) # 12 rows, 4 unique (lat, lon)
print(result.to_dataset(dims=["lat", "lon"])["temperature"].values)
That is temp[-1], the last time step. Two thirds of the input is gone and nothing says so. Inferring the dims (dims=None) gives the same result.
The value is also arbitrary rather than merely lossy: it depends on the order rows happen to arrive in, so the same query can return different numbers across partitionings.
Raising the documented ValueError seems right to me, since the alternative is picking an aggregation the caller did not ask for. Detecting it costs a scan, so making it conditional (or checking only when dims was inferred) may be worth it.
I hit this from Lumen, where a projecting SQL transform drops a dim before the source grids the result. Happy to send a PR if you want it fixed this way.
When a query projects a dimension away, the rows that dimension separated collapse onto the same dim tuple.
to_datasetbuilds a grid from them anyway, keeping whichever row comes last, and returns it without a warning. The docstring says it raises instead:Repro on 0.3.3 and 0.4.0rc1:
That is
temp[-1], the last time step. Two thirds of the input is gone and nothing says so. Inferring the dims (dims=None) gives the same result.The value is also arbitrary rather than merely lossy: it depends on the order rows happen to arrive in, so the same query can return different numbers across partitionings.
Raising the documented
ValueErrorseems right to me, since the alternative is picking an aggregation the caller did not ask for. Detecting it costs a scan, so making it conditional (or checking only whendimswas inferred) may be worth it.I hit this from Lumen, where a projecting SQL transform drops a dim before the source grids the result. Happy to send a PR if you want it fixed this way.