Add lazy SQL → xarray round-trip via XarrayDataFrame.to_dataset (closes #58) - #167
Merged
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #58.
ctx.sql(...)now returns anXarrayDataFramethat adds.to_dataset()on top of the existing.to_pandas(). The Dataset is lazy by default slicing pushes down into SQLWHEREclauses so only the requested slab gets materialized.Why
Going
Dataset → SQLworked great. Going the other way meant hand-rollingset_index().to_xarray()and waving goodbye to attrs, encoding, and non-dim coords. This closes the loop. API shape follows the wrapper proposal in the issue thread; lazy-default viaBackendArray+LazilyIndexedArrayis the direction that came out of the design discussion.Before / After
Before
After
RSS on
xr.tutorial.open_dataset('air_temperature')(~31 MB):ctx.sql(...).to_pandas()WHEREqueryto_dataset()+isel(time=0).valuesAbout 11× lighter than the eager path, basically tied with hand-written filtered SQL.
Scope
xarray_sql/ds.py:XarrayDataFrame+SQLBackendArray(pushes indexers down to SQLWHERE) + template-based metadata recoveryXarrayContext.sql()returns the wrapper;.to_pandas()stays bit-for-bit identical (there's a regression test for this)BackendArray+LazilyIndexedArraywithIndexingSupport.OUTER; dask stays orthogonalsparse_extent="result"|"template"for filtered queriesFollow-ups per the issue thread: chunking / rechunker primitive, hypothesis property tests, simulation testing, promoting the standalone helper to public.
Test plan
uv run pytest tests/- 158/158uv run ruff check xarray_sql/ tests/cleanuv run ruff format -check xarray_sql/ tests/cleanuv run python -m mypy xarray_sql/- 4 errors, all pre-existing (net −1 vs main)