|
9 | 9 |
|
10 | 10 | from xarray_sql import Context |
11 | 11 |
|
| 12 | +try: |
| 13 | + from einfold.datafusion import EinFold |
| 14 | +except ImportError: |
| 15 | + EinFold = None |
| 16 | + |
12 | 17 | # The build side of the join, `a`, is what a reused hash join would keep. |
13 | 18 | JOIN = """ |
14 | 19 | SELECT b.i, SUM(t.v2 * b.w) AS s |
|
17 | 22 | """ |
18 | 23 |
|
19 | 24 |
|
| 25 | +@pytest.mark.parametrize("extension", [False, True]) |
| 26 | +def test_join_key_data_columns_can_change_their_range(extension): |
| 27 | + ctx = Context() |
| 28 | + if extension: |
| 29 | + if EinFold is None: |
| 30 | + pytest.skip("requires the optional EinFold planner") |
| 31 | + ctx = ctx.with_extensions(EinFold()) |
| 32 | + a = xr.Dataset({"key": ("i", np.arange(128)), "v": ("i", np.ones(128))}) |
| 33 | + b = xr.Dataset({"key": ("j", np.arange(256)), "w": ("j", np.arange(256.0))}) |
| 34 | + ctx.from_dataset("a", a) |
| 35 | + ctx.from_dataset("b", b) |
| 36 | + sql = "SELECT a.i,b.j,a.v+b.w AS s FROM a JOIN b ON a.key=b.key ORDER BY a.i,b.j" |
| 37 | + query = ctx.prepare(sql) |
| 38 | + for start in (0, 128, 16): |
| 39 | + updated = a.copy(deep=True) |
| 40 | + updated["key"][:] = np.arange(start, start + 128) |
| 41 | + rows = query(a=updated) |
| 42 | + assert rows.equals(ctx.sql(sql).to_arrow_table()) |
| 43 | + |
| 44 | + |
20 | 45 | def _a(v): |
21 | 46 | return xr.Dataset( |
22 | 47 | {"v": (("i", "j"), v)}, |
@@ -262,3 +287,61 @@ def test_a_quoted_table_replaced_in_sql_counts_too(): |
262 | 287 | ctx.sql('CREATE OR REPLACE TABLE "Mixed" AS VALUES (1), (2)').collect() |
263 | 288 | with pytest.raises(ValueError, match="registered again"): |
264 | 289 | query() |
| 290 | + |
| 291 | + |
| 292 | +def test_extension_handle_keeps_dataset_metadata_and_invalidation(ctx): |
| 293 | + want = ctx.sql("SELECT * FROM a").to_dataset() |
| 294 | + derived = ctx.with_extensions() |
| 295 | + query = derived.prepare("SELECT i, j, v FROM a ORDER BY i, j") |
| 296 | + xr.testing.assert_identical( |
| 297 | + derived.sql("SELECT * FROM a").to_dataset(), want |
| 298 | + ) |
| 299 | + ctx.deregister_table("a") |
| 300 | + with pytest.raises(ValueError, match="registered again"): |
| 301 | + query() |
| 302 | + |
| 303 | + |
| 304 | +@pytest.mark.parametrize( |
| 305 | + "chunks", [{"i": 1, "j": 1}, {"i": 1}, {"i": 4, "j": 5}] |
| 306 | +) |
| 307 | +def test_fine_chunk_registration_keeps_rows_metadata_and_writes(chunks): |
| 308 | + values = np.arange(129 * 33, dtype=np.float64).reshape(129, 33) |
| 309 | + ds = _a(values) |
| 310 | + ds.attrs["experiment"] = "fine chunks" |
| 311 | + ctx = Context() |
| 312 | + ctx.from_dataset("a", ds, chunks=chunks) |
| 313 | + sql = "SELECT i,j,v FROM a ORDER BY i,j" |
| 314 | + xr.testing.assert_identical(ctx.sql(sql).to_dataset(), ds) |
| 315 | + run = ctx.prepare(sql) |
| 316 | + replacement = ds * 2 |
| 317 | + rows = run(a=replacement) |
| 318 | + np.testing.assert_array_equal(rows["v"], replacement.v.values.reshape(-1)) |
| 319 | + |
| 320 | + |
| 321 | +@pytest.mark.parametrize("threads", [1, 60]) |
| 322 | +def test_thread_target_is_explicit_and_keeps_xarray_support(threads): |
| 323 | + ctx = Context(threads=threads) |
| 324 | + assert ctx.sql( |
| 325 | + "SELECT value FROM information_schema.df_settings " |
| 326 | + "WHERE name='datafusion.execution.target_partitions'" |
| 327 | + ).to_pylist() == [{"value": str(threads)}] |
| 328 | + ctx.from_dataset("a", _a(np.ones((4, 3)))) |
| 329 | + assert ctx.prepare("SELECT SUM(v) AS s FROM a")().to_pylist() == [ |
| 330 | + {"s": 12.0} |
| 331 | + ] |
| 332 | + |
| 333 | + |
| 334 | +def test_thread_target_leaves_the_given_config_unchanged(): |
| 335 | + config = ( |
| 336 | + SessionConfig().with_information_schema(True).with_target_partitions(7) |
| 337 | + ) |
| 338 | + Context(config, threads=2) |
| 339 | + assert Context(config).sql( |
| 340 | + "SELECT value FROM information_schema.df_settings " |
| 341 | + "WHERE name='datafusion.execution.target_partitions'" |
| 342 | + ).to_pylist() == [{"value": "7"}] |
| 343 | + |
| 344 | + |
| 345 | +def test_thread_target_must_be_positive(): |
| 346 | + with pytest.raises(ValueError, match="positive"): |
| 347 | + Context(threads=0) |
0 commit comments