Skip to content

Commit 3d49c4a

Browse files
committed
unifiying "template_table" with just the "template" argument.
1 parent 221a28d commit 3d49c4a

2 files changed

Lines changed: 35 additions & 45 deletions

File tree

tests/test_ds.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Round-trip identity across varied source Datasets (one parametrized
88
``assert_identical`` test, not eight per-aspect checks).
99
* Aggregation, ``dimension_columns`` inference, and the template /
10-
``template_table`` resolution rules with their error paths.
10+
``template`` resolution rules (name or Dataset) with their error paths.
1111
* Sparsity handling and ``fill_value`` dtype behavior.
1212
* The vectorized-indexer fallback through xarray's adapter.
1313
@@ -267,12 +267,12 @@ def test_chunks_auto_snaps_to_source_partitions():
267267
def test_to_dataset_multi_registered_requires_explicit_template(
268268
air_dataset_small,
269269
):
270-
"""With more than one registered Dataset, the caller must
271-
disambiguate via ``template_table=``."""
270+
"""With more than one registered Dataset, the caller disambiguates by
271+
passing a registered table name as ``template=``."""
272272
ctx = XarrayContext()
273273
ctx.from_dataset("air1", air_dataset_small)
274274
ctx.from_dataset("air2", air_dataset_small)
275-
out = ctx.sql("SELECT * FROM air1").to_dataset(template_table="air1")
275+
out = ctx.sql("SELECT * FROM air1").to_dataset(template="air1")
276276
assert set(out.dims) == {"time", "lat", "lon"}
277277

278278

@@ -288,36 +288,31 @@ def test_to_dataset_infer_fails_when_no_template_fits(air_dataset_small):
288288
).to_dataset()
289289

290290

291-
def test_template_table_explicit_override(air_dataset_small):
292-
"""``template_table=`` picks a registered Dataset deterministically."""
291+
def test_template_accepts_name_or_dataset(air_dataset_small):
292+
"""``template=`` accepts either a registered table name or a Dataset
293+
object, with equivalent metadata recovery."""
293294
other = air_dataset_small.copy()
294295
other.attrs = {"flag": "other"}
295296
ctx = XarrayContext()
296297
ctx.from_dataset("air", air_dataset_small)
297298
ctx.from_dataset("other", other)
298-
out = ctx.sql("SELECT * FROM air").to_dataset(
299-
dims=["time", "lat", "lon"], template_table="other"
299+
300+
by_name = ctx.sql("SELECT * FROM air").to_dataset(
301+
dims=["time", "lat", "lon"], template="other"
302+
)
303+
by_object = ctx.sql("SELECT * FROM air").to_dataset(
304+
dims=["time", "lat", "lon"], template=other
300305
)
301-
assert out.attrs == {"flag": "other"}
306+
assert by_name.attrs == {"flag": "other"}
307+
assert by_object.attrs == {"flag": "other"}
302308

303309

304-
def test_template_table_unknown_raises(air_dataset_small):
310+
def test_template_unknown_name_raises(air_dataset_small):
305311
ctx = XarrayContext()
306312
ctx.from_dataset("air", air_dataset_small)
307313
with pytest.raises(ValueError, match="not a registered table"):
308314
ctx.sql("SELECT * FROM air").to_dataset(
309-
dims=["time", "lat", "lon"], template_table="missing"
310-
)
311-
312-
313-
def test_template_and_template_table_mutually_exclusive(air_dataset_small):
314-
ctx = XarrayContext()
315-
ctx.from_dataset("air", air_dataset_small)
316-
with pytest.raises(ValueError, match="Pass at most one"):
317-
ctx.sql("SELECT * FROM air").to_dataset(
318-
dims=["time", "lat", "lon"],
319-
template=air_dataset_small,
320-
template_table="air",
315+
dims=["time", "lat", "lon"], template="missing"
321316
)
322317

323318

xarray_sql/ds.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,7 @@ def to_pandas(self) -> pd.DataFrame:
663663
def to_dataset(
664664
self,
665665
dims: list[str] | None = None,
666-
template: xr.Dataset | None = None,
667-
template_table: str | None = None,
666+
template: xr.Dataset | str | None = None,
668667
sparsity: Sparsity = "result",
669668
fill_value: Any = np.nan,
670669
chunks: Mapping[str, int] | str | None = "inherit",
@@ -677,10 +676,11 @@ def to_dataset(
677676
referenced by the SQL ``FROM`` clause (if exactly one
678677
matches), or any single registered Dataset whose dims are
679678
all present in the result columns.
680-
template: Source ``xr.Dataset`` to recover metadata from.
681-
Overrides any auto-resolved template.
682-
template_table: Name of a registered table to use as the
683-
template. Mutually exclusive with ``template``.
679+
template: Source to recover metadata (attrs, encoding, non-dim
680+
coordinates, dim-coord dtype) from. Either an ``xr.Dataset``
681+
used directly, or the name of a registered table (e.g.
682+
``"era5.surface"``) whose Dataset is looked up. When ``None``
683+
and exactly one Dataset is registered, that one is used.
684684
sparsity: ``"result"`` (default) keeps only dim values
685685
present in the result. ``"template"`` reindexes to the
686686
template's full coord ranges, filling absent cells with
@@ -715,15 +715,13 @@ def to_dataset(
715715
Raises:
716716
ValueError: ``dims`` cannot be inferred, names a missing
717717
column, or the result has duplicate dim tuples;
718-
``template_table`` is unknown; both ``template`` and
719-
``template_table`` are passed; or
718+
``template`` names an unknown registered table; or
720719
``sparsity="template"`` is requested without a
721720
resolvable template.
722721
"""
723-
if template is not None and template_table is not None:
724-
raise ValueError("Pass at most one of template= or template_table=")
725-
if template is None:
726-
template = self._resolve_template(template_table)
722+
if not isinstance(template, xr.Dataset):
723+
# ``template`` is a registered-table name or None; look it up.
724+
template = self._resolve_template(template)
727725
if dims is None:
728726
dims = self._infer_dimension_columns(preferred_template=template)
729727
resolved_chunks = self._resolve_chunks(chunks, template, dims)
@@ -770,25 +768,22 @@ def _resolve_chunks(
770768
return inherited or None
771769
return chunks
772770

773-
def _resolve_template(
774-
self, template_table: str | None
775-
) -> xr.Dataset | None:
776-
"""Pick a template Dataset for metadata recovery.
771+
def _resolve_template(self, name: str | None) -> xr.Dataset | None:
772+
"""Pick a template Dataset for metadata recovery by registered name.
777773
778774
Priority:
779-
1. Explicit ``template_table`` argument.
775+
1. The named registered table (``name``).
780776
2. If exactly one Dataset is registered on the context, use it.
781777
3. None.
782778
"""
783779
templates = self._templates
784-
if template_table is not None:
785-
if template_table not in templates:
780+
if name is not None:
781+
if name not in templates:
786782
raise ValueError(
787-
f"template_table={template_table!r} is not a "
788-
"registered table on this context. Registered: "
789-
f"{list(templates)}"
783+
f"template={name!r} is not a registered table on this "
784+
f"context. Registered: {list(templates)}"
790785
)
791-
return templates[template_table]
786+
return templates[name]
792787
if len(templates) == 1:
793788
return next(iter(templates.values()))
794789
return None

0 commit comments

Comments
 (0)