-
Notifications
You must be signed in to change notification settings - Fork 14
fix: infer surviving dims in to_dataset for aggregations (closes #189) #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ghostiee-11
wants to merge
2
commits into
xqlsystems:main
Choose a base branch
from
ghostiee-11:fix/189-infer-dims-groupby
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+90
−30
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # AGENTS.md | ||
|
|
||
| Guidance for contributors (including AI assistants) working on `xarray-sql`. It | ||
| summarizes recurring maintainer review feedback so changes land clean. | ||
|
|
||
| ## Documentation and comments | ||
|
|
||
| - Keep docstrings and comments self-contained. Do **not** put GitHub issue or PR | ||
| numbers in docstrings or code comments; a reader should not need the issue | ||
| tracker to understand the code. Issue references belong in the commit message | ||
| and PR description (e.g. `Closes #189`), not in the source. | ||
| - Do not reference the review conversation, chat, or "the reporter" in comments. | ||
| Describe the behavior, not how it came up. | ||
|
|
||
| ## API surface | ||
|
|
||
| - Mark internal helpers private with a leading underscore when they are not part | ||
| of the public API. | ||
| - Prefer doing setup work in the functional entry point (e.g. `read_xarray`) | ||
| rather than in a class constructor; keep constructors minimal. | ||
|
|
||
| ## Tests | ||
|
|
||
| - Test the public contract (values, dims, coords, attrs), not internal call | ||
| counts or private classes, so the suite survives refactors. | ||
| - Avoid redundant tests: if a public-path test already covers a behavior, do not | ||
| add a second lower-level test for the same thing. | ||
| - Make query results deterministic with `ORDER BY` so assertions do not have to | ||
| re-sort the output. | ||
| - Do not pass `dims=` to `to_dataset()` when inference already resolves them. | ||
| Reserve explicit `dims=` / `template=` for genuinely ambiguous cases (multiple | ||
| registered Datasets, or a test that is specifically exercising those | ||
| arguments). | ||
|
|
||
| ## Commits | ||
|
|
||
| - Use conventional commit prefixes: `fix:`, `feat:`, `refactor:`, `chore:`, | ||
| `docs:`, `test:`. | ||
| - Keep imports at the top of the file. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,7 +125,7 @@ def test_aggregation_drops_dim(air_dataset_small): | |
| ctx.from_dataset("air", air_dataset_small) | ||
| out = ctx.sql( | ||
| "SELECT lat, lon, AVG(air) AS air_avg FROM air GROUP BY lat, lon" | ||
| ).to_dataset(dims=["lat", "lon"]) | ||
| ).to_dataset() | ||
| assert set(out.dims) == {"lat", "lon"} | ||
| assert "air_avg" in out.data_vars | ||
| assert "air" not in out.data_vars | ||
|
|
@@ -139,6 +139,23 @@ def test_aggregation_drops_dim(air_dataset_small): | |
| np.testing.assert_allclose(actual, expected) | ||
|
|
||
|
|
||
| def test_aggregation_infers_dims(air_dataset_small): | ||
| """to_dataset() infers the surviving GROUP BY dim when dims is omitted.""" | ||
| ctx = XarrayContext() | ||
| ctx.from_dataset("air", air_dataset_small) | ||
|
|
||
| # Grouping by the time coordinate keeps time as the sole dimension; the | ||
| # ORDER BY makes the result order deterministic so no sort is needed below. | ||
| out = ctx.sql( | ||
| 'SELECT "time", AVG("air") AS air FROM "air" ' | ||
| 'GROUP BY "time" ORDER BY "time"' | ||
| ).to_dataset() | ||
| assert set(out.dims) == {"time"} | ||
| assert "air" in out.data_vars | ||
| expected = air_dataset_small.compute().mean(dim=["lat", "lon"])["air"] | ||
| np.testing.assert_allclose(out["air"].values, expected.values) | ||
|
|
||
|
|
||
| def test_barrier_query_scans_source_once(air_dataset_small): | ||
| """A barrier plan (aggregation) executes the source exactly once. | ||
|
|
||
|
|
@@ -166,7 +183,7 @@ def test_barrier_query_scans_source_once(air_dataset_small): | |
|
|
||
| out = ctx.sql( | ||
| "SELECT lat, lon, AVG(air) AS air_avg FROM air GROUP BY lat, lon" | ||
| ).to_dataset(dims=["lat", "lon"]) | ||
| ).to_dataset() | ||
| reads_after_construct = len(reads) | ||
| out.compute() | ||
| reads_after_compute = len(reads) | ||
|
|
@@ -188,7 +205,7 @@ def test_order_by_direction_sets_dim_order(air_dataset_small): | |
| ctx.from_dataset("air", air_dataset_small) | ||
| out = ctx.sql( | ||
| "SELECT lat, AVG(air) AS air_avg FROM air GROUP BY lat ORDER BY lat DESC" | ||
| ).to_dataset(dims=["lat"]) | ||
| ).to_dataset() | ||
|
|
||
| lat = out["lat"].values | ||
| assert (np.diff(lat) < 0).all(), f"expected descending lat, got {lat}" | ||
|
|
@@ -289,7 +306,7 @@ def test_fast_path_uses_scanned_tables_coords_not_user_template( | |
|
|
||
|
|
||
| def test_round_trip_preserves_descending_lat_on_lazy_path(air_dataset_small): | ||
| """Lazy round-trip preserves source dim order (xarray-sql#171). | ||
| """Lazy round-trip preserves source dim order. | ||
|
|
||
| NCEP ``air_temperature`` ships descending lat (75.0 -> 15.0). The | ||
| discovery path's ``.distinct().sort()`` previously flipped lat to | ||
|
|
@@ -383,14 +400,12 @@ def test_to_dataset_multi_registered_requires_explicit_template( | |
| assert set(out.dims) == {"time", "lat", "lon"} | ||
|
|
||
|
|
||
| def test_to_dataset_infer_fails_when_no_template_fits(air_dataset_small): | ||
| """If no registered Dataset's dims fit the result -> clear error.""" | ||
| def test_to_dataset_infer_fails_when_no_dim_survives(air_dataset_small): | ||
| """A global aggregation leaves no registered dim in the result -> clear error.""" | ||
| ctx = XarrayContext() | ||
| ctx.from_dataset("air", air_dataset_small) | ||
| with pytest.raises(ValueError, match="dims cannot be inferred"): | ||
| ctx.sql( | ||
| "SELECT lat, lon, AVG(air) AS air_avg FROM air GROUP BY lat, lon" | ||
| ).to_dataset() | ||
| ctx.sql("SELECT AVG(air) AS air_avg FROM air").to_dataset() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will you also fix all the other tests that have specified dims when they now no longer need them? |
||
|
|
||
|
|
||
| def test_template_accepts_name_or_dataset(air_dataset_small): | ||
|
|
@@ -447,7 +462,7 @@ def test_template_aggregation_alias_no_attrs(air_dataset_small): | |
| ctx.from_dataset("air", ds) | ||
| out = ctx.sql( | ||
| "SELECT lat, lon, AVG(air) AS air_avg FROM air GROUP BY lat, lon" | ||
| ).to_dataset(dims=["lat", "lon"]) | ||
| ).to_dataset() | ||
| assert "air_avg" in out.data_vars | ||
| assert out["air_avg"].attrs == {} | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice