|
| 1 | +"""Dask dataframe fsspec entry points against OpenDAL services. |
| 2 | +
|
| 3 | +Adapted from Dask ``test_read_csv_files`` and |
| 4 | +``test_multiple_read_csv_has_deterministic_name`` in |
| 5 | +``dask/dataframe/io/tests/test_csv.py``, plus |
| 6 | +``test_fsspec_to_parquet_filesystem_option`` in |
| 7 | +``dask/dataframe/io/tests/test_parquet.py``, from the Dask 2026.8.0 release. |
| 8 | +""" |
| 9 | + |
| 10 | +import dask.dataframe as dd |
| 11 | +import pandas as pd |
| 12 | +import pandas.testing as tm |
| 13 | +import pytest |
| 14 | + |
| 15 | +from opendalfs import register_opendal_service |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def opendal_memory_url(): |
| 20 | + import fsspec |
| 21 | + |
| 22 | + register_opendal_service("memory") |
| 23 | + url = "opendal+memory://test/dask" |
| 24 | + fs, path = fsspec.core.url_to_fs(url) |
| 25 | + if fs.exists(path): |
| 26 | + fs.rm(path, recursive=True) |
| 27 | + yield fs, url, path |
| 28 | + if fs.exists(path): |
| 29 | + fs.rm(path, recursive=True) |
| 30 | + |
| 31 | + |
| 32 | +def test_read_csv_url_glob_and_tokenization(opendal_memory_url): |
| 33 | + fs, base_url, base_path = opendal_memory_url |
| 34 | + files = { |
| 35 | + "2014-01-01.csv": b"name,amount,id\nAlice,100,1\nBob,200,2\n", |
| 36 | + "2014-01-02.csv": b"name,amount,id\nCharlie,300,3\n", |
| 37 | + } |
| 38 | + for name, content in files.items(): |
| 39 | + fs.pipe_file(f"{base_path}/{name}", content) |
| 40 | + |
| 41 | + first = dd.read_csv(f"{base_url}/2014-01-*.csv") |
| 42 | + second = dd.read_csv(f"{base_url}/2014-01-*.csv") |
| 43 | + expected = pd.DataFrame({ |
| 44 | + "name": ["Alice", "Bob", "Charlie"], |
| 45 | + "amount": [100, 200, 300], |
| 46 | + "id": [1, 2, 3], |
| 47 | + }) |
| 48 | + |
| 49 | + assert first._name == second._name |
| 50 | + assert sorted(first.dask.keys(), key=str) == sorted(second.dask.keys(), key=str) |
| 51 | + tm.assert_frame_equal( |
| 52 | + first.compute().reset_index(drop=True), |
| 53 | + expected.reset_index(drop=True), |
| 54 | + check_dtype=False, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def test_read_parquet_url(s3_fs, s3_config): |
| 59 | + expected = pd.DataFrame({"a": range(10)}) |
| 60 | + path = "dask/url.parquet" |
| 61 | + expected.to_parquet(path, filesystem=s3_fs) |
| 62 | + url = f"opendal+s3://{s3_config.bucket}/{path}" |
| 63 | + storage_options = { |
| 64 | + "endpoint": s3_config.endpoint, |
| 65 | + "region": s3_config.region, |
| 66 | + "access_key_id": s3_config.access_key_id, |
| 67 | + "secret_access_key": s3_config.secret_access_key, |
| 68 | + } |
| 69 | + |
| 70 | + result = dd.read_parquet(url, storage_options=storage_options).compute() |
| 71 | + |
| 72 | + tm.assert_frame_equal(result, expected) |
| 73 | + |
| 74 | + |
| 75 | +def test_read_parquet_filesystem(s3_fs): |
| 76 | + expected = pd.DataFrame({"a": range(10)}) |
| 77 | + path = "dask/filesystem.parquet" |
| 78 | + expected.to_parquet(path, filesystem=s3_fs) |
| 79 | + |
| 80 | + result = dd.read_parquet(path, filesystem=s3_fs).compute() |
| 81 | + |
| 82 | + tm.assert_frame_equal(result, expected) |
0 commit comments