@@ -359,6 +359,14 @@ def test_multiple_dim_groups(self):
359359 def test_empty_dataset (self ):
360360 assert _group_vars_by_dims (xr .Dataset ()) == {}
361361
362+ def test_includes_scalar_group (self ):
363+ """Scalar (0-dim) variables group under the empty dims tuple."""
364+ ds = xr .Dataset (
365+ {"band" : (["y" , "x" ], np .zeros ((2 , 3 ))), "projection" : ((), 0 )}
366+ )
367+ groups = _group_vars_by_dims (ds )
368+ assert groups == {("y" , "x" ): ["band" ], (): ["projection" ]}
369+
362370 def test_ignores_coords (self ):
363371 """Coordinate variables shouldn't be returned as groups."""
364372 ds = xr .Dataset (
@@ -404,6 +412,49 @@ def test_registers_multiple_tables(self, mixed_ds):
404412 assert len (surface ) == 2 * 3 * 4
405413 assert len (upper ) == 2 * 3 * 4 * 2
406414
415+ @pytest .fixture
416+ def scalar_and_array_ds (self ):
417+ """A gridded variable plus a scalar metadata variable (GOES-like)."""
418+ np .random .seed (0 )
419+ return xr .Dataset (
420+ {
421+ "temperature_2m" : (
422+ ["time" , "lat" , "lon" ],
423+ np .random .rand (2 , 3 , 4 ),
424+ ),
425+ "projection" : ((), 0 ),
426+ },
427+ coords = {
428+ "time" : pd .date_range ("2020-01-01" , periods = 2 ),
429+ "lat" : np .linspace (- 90 , 90 , 3 ),
430+ "lon" : np .linspace (- 180 , 180 , 4 ),
431+ },
432+ ).chunk ({"time" : 1 })
433+
434+ def test_registers_scalar_var_as_single_row_table (
435+ self , scalar_and_array_ds
436+ ):
437+ ctx = XarrayContext ()
438+ ctx .from_dataset ("goes" , scalar_and_array_ds )
439+ surface = ctx .sql ("SELECT * FROM goes.time_lat_lon" ).to_pandas ()
440+ scalar = ctx .sql ("SELECT * FROM goes.scalar" ).to_pandas ()
441+ assert "temperature_2m" in surface .columns
442+ assert "projection" in scalar .columns
443+ assert len (scalar ) == 1
444+
445+ def test_scalar_group_in_catalog (self , scalar_and_array_ds ):
446+ ctx = XarrayContext ()
447+ ctx .from_dataset ("goes" , scalar_and_array_ds )
448+ tables = set (ctx .catalog ().schema ("goes" ).table_names ())
449+ assert tables == {"time_lat_lon" , "scalar" }
450+
451+ def test_scalar_table_name_override (self , scalar_and_array_ds ):
452+ ctx = XarrayContext ()
453+ ctx .from_dataset ("goes" , scalar_and_array_ds , table_names = {(): "meta" })
454+ result = ctx .sql ("SELECT * FROM goes.meta" ).to_pandas ()
455+ assert "projection" in result .columns
456+ assert len (result ) == 1
457+
407458 def test_table_names_override (self , mixed_ds ):
408459 ctx = XarrayContext ()
409460 ctx .from_dataset (
0 commit comments