fsspec-data 0.7.0__tar.gz → 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/Cargo.lock +2 -2
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/Cargo.toml +1 -1
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/PKG-INFO +3 -3
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/README.md +2 -2
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/__init__.py +1 -3
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/tests/test_all.py +17 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/tests/test_describe.py +23 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/tests/test_schema.py +10 -14
- fsspec_data-0.8.0/fsspec_data/tests/test_view_conversion.py +164 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/view.py +80 -13
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/pyproject.toml +2 -3
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/Cargo.lock +1 -1
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/Cargo.toml +1 -1
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/coverage +922 -922
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/python/lib.rs +32 -14
- fsspec_data-0.7.0/fsspec_data/filesystem.py +0 -151
- fsspec_data-0.7.0/fsspec_data/tests/test_filesystem.py +0 -223
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/.gitignore +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/LICENSE +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/_version.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/backends/__init__.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/backends/base.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/backends/http.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/backends/sqlalchemy.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/confluent.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/converters.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/describe.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/interchange.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/json_schema.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/schema.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/tests/__init__.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/tests/conftest.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/tests/test_converters.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/tests/test_view.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/fsspec_data/xarray_zarr.py +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/.config/nextest.toml +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/avro.rs +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/codec.rs +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/confluent.rs +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/converter.rs +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/describe.rs +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/error.rs +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/json_schema.rs +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/lib.rs +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/plan.rs +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/schema.rs +0 -0
- {fsspec_data-0.7.0 → fsspec_data-0.8.0}/rust/src/stream.rs +0 -0
|
@@ -721,7 +721,7 @@ checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
|
|
|
721
721
|
|
|
722
722
|
[[package]]
|
|
723
723
|
name = "fsspec_data"
|
|
724
|
-
version = "0.
|
|
724
|
+
version = "0.8.0"
|
|
725
725
|
dependencies = [
|
|
726
726
|
"arrow",
|
|
727
727
|
"arrow-avro",
|
|
@@ -735,7 +735,7 @@ dependencies = [
|
|
|
735
735
|
|
|
736
736
|
[[package]]
|
|
737
737
|
name = "fsspec_data_py"
|
|
738
|
-
version = "0.
|
|
738
|
+
version = "0.8.0"
|
|
739
739
|
dependencies = [
|
|
740
740
|
"arrow",
|
|
741
741
|
"fsspec_data",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: fsspec-data
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.0
|
|
4
4
|
Summary: Arrow interchange planning for the fsspec data ecosystem
|
|
5
5
|
Project-URL: Repository, https://github.com/1kbgz/fsspec-data
|
|
6
6
|
Project-URL: Homepage, https://github.com/1kbgz/fsspec-data
|
|
@@ -54,8 +54,8 @@ Convert tabular formats and reconcile Arrow schemas over fsspec.
|
|
|
54
54
|
[](https://pypi.python.org/pypi/fsspec-data)
|
|
55
55
|
|
|
56
56
|
`fsspec-data` plans schema mappings and executes them over Arrow record batches.
|
|
57
|
-
Codecs handle Arrow IPC, Parquet, CSV, and JSONL.
|
|
58
|
-
exposes converted files; a separate converter registry handles resources such as
|
|
57
|
+
Codecs handle Arrow IPC, Parquet, CSV, and JSONL. The read-only `dataview` filesystem
|
|
58
|
+
exposes converted files, schemas, and metadata of any source; a separate converter registry handles resources such as
|
|
59
59
|
Xarray datasets and Zarr stores.
|
|
60
60
|
|
|
61
61
|
## Start here
|
|
@@ -8,8 +8,8 @@ Convert tabular formats and reconcile Arrow schemas over fsspec.
|
|
|
8
8
|
[](https://pypi.python.org/pypi/fsspec-data)
|
|
9
9
|
|
|
10
10
|
`fsspec-data` plans schema mappings and executes them over Arrow record batches.
|
|
11
|
-
Codecs handle Arrow IPC, Parquet, CSV, and JSONL.
|
|
12
|
-
exposes converted files; a separate converter registry handles resources such as
|
|
11
|
+
Codecs handle Arrow IPC, Parquet, CSV, and JSONL. The read-only `dataview` filesystem
|
|
12
|
+
exposes converted files, schemas, and metadata of any source; a separate converter registry handles resources such as
|
|
13
13
|
Xarray datasets and Zarr stores.
|
|
14
14
|
|
|
15
15
|
## Start here
|
|
@@ -5,7 +5,6 @@ from .converters import (
|
|
|
5
5
|
ConverterRegistry,
|
|
6
6
|
)
|
|
7
7
|
from .describe import DataSource, Facet, FacetNotSupportedError, FileDataSource, Node
|
|
8
|
-
from .filesystem import DataFileSystem
|
|
9
8
|
from .interchange import (
|
|
10
9
|
DEFAULT_REGISTRY,
|
|
11
10
|
Codec,
|
|
@@ -35,7 +34,7 @@ from .schema import (
|
|
|
35
34
|
)
|
|
36
35
|
from .view import DEFAULT_MAPPING, DataViewFileSystem, ViewEntry
|
|
37
36
|
|
|
38
|
-
__version__ = "0.
|
|
37
|
+
__version__ = "0.8.0"
|
|
39
38
|
|
|
40
39
|
__all__ = [
|
|
41
40
|
"CONVERTER_ENTRY_POINT_GROUP",
|
|
@@ -48,7 +47,6 @@ __all__ = [
|
|
|
48
47
|
"CodecRegistry",
|
|
49
48
|
"Converter",
|
|
50
49
|
"ConverterRegistry",
|
|
51
|
-
"DataFileSystem",
|
|
52
50
|
"DataFormat",
|
|
53
51
|
"DataSource",
|
|
54
52
|
"DataViewFileSystem",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import io
|
|
2
|
+
import threading
|
|
2
3
|
from typing import Any
|
|
3
4
|
|
|
4
5
|
import pyarrow as pa
|
|
@@ -401,6 +402,22 @@ def test_lazy_decode_observes_cancellation():
|
|
|
401
402
|
next(stream)
|
|
402
403
|
|
|
403
404
|
|
|
405
|
+
def test_streams_can_be_read_from_any_thread():
|
|
406
|
+
codec = DEFAULT_REGISTRY.get(DataFormat.CSV)
|
|
407
|
+
source = io.BytesIO(b"id\n1\n2\n3\n")
|
|
408
|
+
stream = codec.iter_batches(source, schema=pa.schema([("id", pa.int64())]), batch_size=1)
|
|
409
|
+
batches = [next(stream)]
|
|
410
|
+
|
|
411
|
+
def read_rest():
|
|
412
|
+
batches.extend(stream)
|
|
413
|
+
stream.cancel()
|
|
414
|
+
|
|
415
|
+
worker = threading.Thread(target=read_rest)
|
|
416
|
+
worker.start()
|
|
417
|
+
worker.join()
|
|
418
|
+
assert [batch.column(0).to_pylist() for batch in batches] == [[1], [2], [3]]
|
|
419
|
+
|
|
420
|
+
|
|
404
421
|
def test_lazy_decode_enforces_byte_limit():
|
|
405
422
|
schema = pa.schema([("id", pa.int64())])
|
|
406
423
|
codec = DEFAULT_REGISTRY.get(DataFormat.ARROW)
|
|
@@ -109,3 +109,26 @@ def test_a_catalog_backend_describes_metadata_at_every_level():
|
|
|
109
109
|
assert rows(catalog.scan(curves.path)) == 3
|
|
110
110
|
with pytest.raises(FacetNotSupportedError):
|
|
111
111
|
catalog.open_raw(curves.path)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_facets_a_source_does_not_implement_raise_facet_not_supported():
|
|
115
|
+
class Bare(DataSource):
|
|
116
|
+
def describe(self, path):
|
|
117
|
+
return Node(path, [])
|
|
118
|
+
|
|
119
|
+
source = Bare()
|
|
120
|
+
assert source.describe("x").facets == frozenset()
|
|
121
|
+
calls = [
|
|
122
|
+
(Facet.CHILDREN, source.children),
|
|
123
|
+
(Facet.TABLE, source.schema),
|
|
124
|
+
(Facet.TABLE, source.scan),
|
|
125
|
+
(Facet.RAW, source.open_raw),
|
|
126
|
+
(Facet.METADATA, source.metadata),
|
|
127
|
+
]
|
|
128
|
+
for facet, call in calls:
|
|
129
|
+
with pytest.raises(FacetNotSupportedError) as error:
|
|
130
|
+
call("x")
|
|
131
|
+
assert error.value.facet is facet
|
|
132
|
+
assert str(error.value) == f"x has no {facet.value} facet"
|
|
133
|
+
with pytest.raises(NotImplementedError):
|
|
134
|
+
DataSource().describe("x")
|
|
@@ -9,8 +9,8 @@ import pytest
|
|
|
9
9
|
import fsspec_data.confluent as confluent_module
|
|
10
10
|
import fsspec_data.schema as schema_module
|
|
11
11
|
from fsspec_data import (
|
|
12
|
-
DataFileSystem,
|
|
13
12
|
DataFormat,
|
|
13
|
+
DataViewFileSystem,
|
|
14
14
|
InterchangeRequest,
|
|
15
15
|
SchemaPolicy,
|
|
16
16
|
SchemaRef,
|
|
@@ -140,8 +140,8 @@ def test_data_filesystem_resolves_independent_schema_sources(monkeypatch):
|
|
|
140
140
|
source_fs = fsspec.filesystem("memory")
|
|
141
141
|
source_fs.store.clear()
|
|
142
142
|
source_fs.pipe("orders.csv", b"id,name\n1,ada\n2,grace\n")
|
|
143
|
-
fs =
|
|
144
|
-
|
|
143
|
+
fs = DataViewFileSystem(
|
|
144
|
+
"orders.csv",
|
|
145
145
|
fs=source_fs,
|
|
146
146
|
provided_schema={
|
|
147
147
|
"url": "memory://schemas/source.json",
|
|
@@ -176,10 +176,9 @@ def test_fsspec_open_resolves_referenced_schemas():
|
|
|
176
176
|
memory_fs.pipe("orders.csv", b"id,name\n1,ada\n2,grace\n")
|
|
177
177
|
memory_fs.pipe("schemas/source.json", json.dumps(SOURCE_SCHEMA_JSON).encode())
|
|
178
178
|
memory_fs.pipe("schemas/target.arrow", TARGET_SCHEMA.serialize().to_pybytes())
|
|
179
|
-
fsspec.register_implementation("fsspec-data", DataFileSystem, clobber=True)
|
|
180
179
|
|
|
181
180
|
with fsspec.open(
|
|
182
|
-
"
|
|
181
|
+
"dataview://orders.parquet::memory://orders.csv",
|
|
183
182
|
provided_schema={
|
|
184
183
|
"url": "memory://schemas/source.json",
|
|
185
184
|
"format": "arrow-json",
|
|
@@ -253,10 +252,9 @@ def test_fsspec_open_resolves_json_schemas():
|
|
|
253
252
|
memory_fs.pipe("orders.csv", b"id,name\n1,ada\n2,grace\n")
|
|
254
253
|
memory_fs.pipe("schemas/source.schema.json", json.dumps(SOURCE_JSON_SCHEMA).encode())
|
|
255
254
|
memory_fs.pipe("schemas/target.schema.json", json.dumps(TARGET_JSON_SCHEMA).encode())
|
|
256
|
-
fsspec.register_implementation("fsspec-data", DataFileSystem, clobber=True)
|
|
257
255
|
|
|
258
256
|
with fsspec.open(
|
|
259
|
-
"
|
|
257
|
+
"dataview://orders.parquet::memory://orders.csv",
|
|
260
258
|
provided_schema={
|
|
261
259
|
"url": "memory://schemas/source.schema.json",
|
|
262
260
|
"format": "json-schema",
|
|
@@ -397,10 +395,9 @@ def test_fsspec_open_composes_confluent_schema_with_file_schema(monkeypatch):
|
|
|
397
395
|
memory_fs.store.clear()
|
|
398
396
|
memory_fs.pipe("orders.csv", b"id,name\n1,ada\n2,grace\n")
|
|
399
397
|
memory_fs.pipe("schemas/target.schema.json", json.dumps(TARGET_JSON_SCHEMA).encode())
|
|
400
|
-
fsspec.register_implementation("fsspec-data", DataFileSystem, clobber=True)
|
|
401
398
|
|
|
402
399
|
with fsspec.open(
|
|
403
|
-
"
|
|
400
|
+
"dataview://orders.parquet::memory://orders.csv",
|
|
404
401
|
provided_schema={
|
|
405
402
|
"url": "confluent://orders-value/versions/latest",
|
|
406
403
|
"format": "json-schema",
|
|
@@ -505,8 +502,8 @@ def test_schema_resolution_precedes_source_read(monkeypatch):
|
|
|
505
502
|
)
|
|
506
503
|
source_fs = fsspec.filesystem("memory")
|
|
507
504
|
monkeypatch.setattr(source_fs, "open", lambda *args, **kwargs: pytest.fail("source data must not be opened"))
|
|
508
|
-
fs =
|
|
509
|
-
|
|
505
|
+
fs = DataViewFileSystem(
|
|
506
|
+
"orders.csv",
|
|
510
507
|
fs=source_fs,
|
|
511
508
|
provided_schema={"url": "memory://schemas/source.json", "format": "arrow-json"},
|
|
512
509
|
)
|
|
@@ -517,7 +514,7 @@ def test_schema_resolution_precedes_source_read(monkeypatch):
|
|
|
517
514
|
|
|
518
515
|
def test_schema_reference_rejects_unknown_options():
|
|
519
516
|
with pytest.raises(ValueError, match="unknown schema reference options: credentials"):
|
|
520
|
-
|
|
517
|
+
DataViewFileSystem(
|
|
521
518
|
fo="orders.csv",
|
|
522
519
|
fs=fsspec.filesystem("memory"),
|
|
523
520
|
provided_schema={"url": "memory://schemas/source.arrow", "credentials": {}},
|
|
@@ -543,10 +540,9 @@ def test_fsspec_open_maps_referenced_csv_schema_onto_renamed_and_added_fields():
|
|
|
543
540
|
)
|
|
544
541
|
memory_fs.pipe("schemas/source.json", json.dumps(source_schema).encode())
|
|
545
542
|
memory_fs.pipe("schemas/target.arrow", target_schema.serialize().to_pybytes())
|
|
546
|
-
fsspec.register_implementation("fsspec-data", DataFileSystem, clobber=True)
|
|
547
543
|
|
|
548
544
|
with fsspec.open(
|
|
549
|
-
"
|
|
545
|
+
"dataview://orders.parquet::memory://orders.csv",
|
|
550
546
|
provided_schema={"url": "memory://schemas/source.json", "format": "arrow-json"},
|
|
551
547
|
requested_schema="memory://schemas/target.arrow",
|
|
552
548
|
schema_policy="compatible",
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
import fsspec
|
|
5
|
+
import pyarrow as pa
|
|
6
|
+
import pyarrow.parquet as pq
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from fsspec_data import DEFAULT_REGISTRY, Codec, DataViewFileSystem, DecodedBatchStream
|
|
10
|
+
|
|
11
|
+
SCHEMA = pa.schema([pa.field("id", pa.int64(), nullable=False), pa.field("name", pa.string())])
|
|
12
|
+
SCHEMA_OPTIONS = {
|
|
13
|
+
"fields": [
|
|
14
|
+
{"name": "id", "type": "int64", "nullable": False},
|
|
15
|
+
{"name": "name", "type": "string", "nullable": True},
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
ROWS = [{"id": 1, "name": "ada"}, {"id": 2, "name": "grace"}]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@pytest.fixture
|
|
22
|
+
def memory_fs():
|
|
23
|
+
fs = fsspec.filesystem("memory")
|
|
24
|
+
fs.store.clear()
|
|
25
|
+
fs.pipe("orders.csv", b"id,name\n1,ada\n2,grace\n")
|
|
26
|
+
return fs
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_a_single_file_converts_with_a_provided_schema(memory_fs):
|
|
30
|
+
fs = DataViewFileSystem("orders.csv", fs=memory_fs, provided_schema=SCHEMA_OPTIONS)
|
|
31
|
+
|
|
32
|
+
assert fs.ls("", detail=False) == ["orders.csv", "orders.metadata.json", "orders.parquet", "orders.schema.json"]
|
|
33
|
+
with fs.open("orders.parquet", "rb") as file:
|
|
34
|
+
assert file.read(4) == b"PAR1"
|
|
35
|
+
file.seek(0)
|
|
36
|
+
table = pq.read_table(file)
|
|
37
|
+
assert table.schema.equals(SCHEMA, check_metadata=False)
|
|
38
|
+
assert table.to_pylist() == ROWS
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_a_single_file_converts_to_avro(memory_fs):
|
|
42
|
+
fs = DataViewFileSystem("orders.csv", fs=memory_fs, mapping={"table": ["avro"]}, provided_schema=SCHEMA_OPTIONS)
|
|
43
|
+
|
|
44
|
+
data = fs.cat_file("orders.avro")
|
|
45
|
+
assert data.startswith(b"Obj\x01")
|
|
46
|
+
assert pa.Table.from_batches(DEFAULT_REGISTRY.get("avro").decode_batches(data).batches).to_pylist() == ROWS
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_conversion_streams_from_the_source_and_honors_limits(memory_fs, monkeypatch):
|
|
50
|
+
encoded = b"id,name\n" + b"".join(f"{index},name-{index}\n".encode() for index in range(50_000))
|
|
51
|
+
bytes_read = 0
|
|
52
|
+
|
|
53
|
+
class CountingReader(io.BytesIO):
|
|
54
|
+
def read(self, size=-1):
|
|
55
|
+
nonlocal bytes_read
|
|
56
|
+
data = super().read(size)
|
|
57
|
+
bytes_read += len(data)
|
|
58
|
+
return data
|
|
59
|
+
|
|
60
|
+
monkeypatch.setattr(memory_fs, "open", lambda *args, **kwargs: CountingReader(encoded))
|
|
61
|
+
monkeypatch.setattr(memory_fs, "cat_file", lambda *args, **kwargs: pytest.fail("cat_file should not be called"))
|
|
62
|
+
fs = DataViewFileSystem("orders.csv", fs=memory_fs, provided_schema=SCHEMA_OPTIONS, batch_size=1, row_limit=1)
|
|
63
|
+
|
|
64
|
+
assert pq.read_table(fs.open("orders.parquet")).to_pylist() == [{"id": 0, "name": "name-0"}]
|
|
65
|
+
assert bytes_read < len(encoded)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_conversion_streams_batches_into_spooled_output(memory_fs, monkeypatch):
|
|
69
|
+
monkeypatch.setattr(DecodedBatchStream, "collect", lambda self: pytest.fail("decoded batches should not be collected"))
|
|
70
|
+
monkeypatch.setattr(Codec, "encode_batches", lambda self, *args, **kwargs: pytest.fail("encoded bytes should not be collected"))
|
|
71
|
+
fs = DataViewFileSystem("orders.csv", fs=memory_fs, provided_schema=SCHEMA_OPTIONS)
|
|
72
|
+
|
|
73
|
+
assert pq.read_table(fs.open("orders.parquet")).to_pylist() == ROWS
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def test_fsspec_urls_name_the_rendered_file_and_the_source(memory_fs):
|
|
77
|
+
fs, path = fsspec.core.url_to_fs("dataview://orders.parquet::memory://orders.csv", provided_schema=SCHEMA_OPTIONS)
|
|
78
|
+
|
|
79
|
+
assert isinstance(fs, DataViewFileSystem)
|
|
80
|
+
assert path == "orders.parquet"
|
|
81
|
+
assert pq.read_table(fs.open(path)).to_pylist() == ROWS
|
|
82
|
+
assert pq.read_table(DataViewFileSystem("memory://orders.csv").open("orders.parquet")).num_rows == 2
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_requested_schemas_project_embedded_schemas(memory_fs):
|
|
86
|
+
parquet = pa.BufferOutputStream()
|
|
87
|
+
pq.write_table(pa.table({"id": [1, 2], "name": ["ada", "grace"]}), parquet)
|
|
88
|
+
memory_fs.pipe("orders.parquet", parquet.getvalue().to_pybytes())
|
|
89
|
+
fs = DataViewFileSystem(
|
|
90
|
+
"orders.parquet",
|
|
91
|
+
fs=memory_fs,
|
|
92
|
+
mapping={"table": ["arrow"], "schema": ["json"]},
|
|
93
|
+
requested_schema={"fields": [{"name": "name", "type": "string"}]},
|
|
94
|
+
schema_policy="projection",
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
with fs.open("orders.arrow") as file:
|
|
98
|
+
assert pa.ipc.open_stream(file).read_all().to_pylist() == [{"name": "ada"}, {"name": "grace"}]
|
|
99
|
+
assert [field["name"] for field in json.loads(fs.cat_file("orders.schema.json"))["fields"]] == ["name"]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def test_conversions_replace_the_native_file_of_the_same_format(memory_fs):
|
|
103
|
+
fs = DataViewFileSystem(
|
|
104
|
+
"orders.csv",
|
|
105
|
+
fs=memory_fs,
|
|
106
|
+
mapping={"table": ["raw", "csv"]},
|
|
107
|
+
requested_schema={"fields": [{"name": "name", "type": "string"}]},
|
|
108
|
+
schema_policy="projection",
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
assert fs.ls("", detail=False) == ["orders.csv"]
|
|
112
|
+
assert fs.cat_file("orders.csv") == b"name\nada\ngrace\n"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def test_provided_schemas_must_match_embedded_schemas(memory_fs):
|
|
116
|
+
parquet = pa.BufferOutputStream()
|
|
117
|
+
pq.write_table(pa.table({"id": [1]}), parquet)
|
|
118
|
+
memory_fs.pipe("orders.parquet", parquet.getvalue().to_pybytes())
|
|
119
|
+
fs = DataViewFileSystem("orders.parquet", fs=memory_fs, mapping={"table": ["arrow"]}, provided_schema=pa.schema([("id", pa.int32())]))
|
|
120
|
+
|
|
121
|
+
with pytest.raises(ValueError, match="provided schema does not match"):
|
|
122
|
+
fs.open("orders.arrow")
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def test_converted_output_rolls_over_at_spool_limit(memory_fs):
|
|
126
|
+
fs = DataViewFileSystem("orders.csv", fs=memory_fs, provided_schema=SCHEMA, spool_max_size=1)
|
|
127
|
+
|
|
128
|
+
with fs.open("orders.parquet") as file:
|
|
129
|
+
assert file._rolled
|
|
130
|
+
assert file.read(4) == b"PAR1"
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def test_unmapped_names_are_not_found(memory_fs):
|
|
134
|
+
fs = DataViewFileSystem("orders.csv", fs=memory_fs, provided_schema=SCHEMA)
|
|
135
|
+
|
|
136
|
+
with pytest.raises(FileNotFoundError):
|
|
137
|
+
fs.open("orders.bin")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_schema_options_accept_a_field_sequence(memory_fs):
|
|
141
|
+
fs = DataViewFileSystem(
|
|
142
|
+
"orders.csv",
|
|
143
|
+
fs=memory_fs,
|
|
144
|
+
provided_schema=[{"name": "id", "type": pa.int64(), "nullable": False}, {"name": "name", "type": "string"}],
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
assert pq.read_table(fs.open("orders.parquet")).to_pylist() == ROWS
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
@pytest.mark.parametrize(
|
|
151
|
+
("schema", "error", "message"),
|
|
152
|
+
[
|
|
153
|
+
({"fields": "invalid"}, TypeError, "sequence of fields"),
|
|
154
|
+
({"fields": ["invalid"]}, TypeError, "fields must be mappings"),
|
|
155
|
+
({"fields": [{}]}, ValueError, "missing 'name'"),
|
|
156
|
+
({"fields": [{"name": "id"}]}, ValueError, "missing 'type'"),
|
|
157
|
+
({"fields": [{"name": 1, "type": "int64"}]}, TypeError, "name and type"),
|
|
158
|
+
({"fields": [{"name": "id", "type": object()}]}, TypeError, "name and type"),
|
|
159
|
+
({"fields": [{"name": "id", "type": "int64", "nullable": "yes"}]}, TypeError, "nullable must be a boolean"),
|
|
160
|
+
],
|
|
161
|
+
)
|
|
162
|
+
def test_invalid_schema_options_are_rejected(memory_fs, schema, error, message):
|
|
163
|
+
with pytest.raises(error, match=message):
|
|
164
|
+
DataViewFileSystem("orders.csv", fs=memory_fs, provided_schema=schema)
|
|
@@ -12,7 +12,8 @@ from fsspec.core import url_to_fs
|
|
|
12
12
|
from fsspec.implementations.chained import ChainedFileSystem
|
|
13
13
|
|
|
14
14
|
from .describe import DataSource, Facet, FileDataSource, Node
|
|
15
|
-
from .interchange import DEFAULT_REGISTRY, DataFormat
|
|
15
|
+
from .interchange import DEFAULT_REGISTRY, DataFormat, SchemaPolicy, plan_schema
|
|
16
|
+
from .schema import SchemaInput, SchemaProvenance, normalize_schema, resolve_schema
|
|
16
17
|
|
|
17
18
|
DEFAULT_MAPPING: dict[str, list[str]] = {
|
|
18
19
|
"table": ["raw", "parquet"],
|
|
@@ -66,6 +67,16 @@ class DataViewFileSystem(ChainedFileSystem):
|
|
|
66
67
|
fs: AbstractFileSystem | None = None,
|
|
67
68
|
source: DataSource | None = None,
|
|
68
69
|
mapping: Mapping[str, Sequence[str]] | None = None,
|
|
70
|
+
provided_schema: SchemaInput = None,
|
|
71
|
+
requested_schema: SchemaInput = None,
|
|
72
|
+
schema_policy: SchemaPolicy | str = SchemaPolicy.EXACT,
|
|
73
|
+
renames: Mapping[str, str] | None = None,
|
|
74
|
+
defaults: Mapping[str, Any] | None = None,
|
|
75
|
+
drop: Sequence[str] | None = None,
|
|
76
|
+
casts: Mapping[str, str] | None = None,
|
|
77
|
+
batch_size: int = 1024,
|
|
78
|
+
row_limit: int | None = None,
|
|
79
|
+
byte_limit: int | None = None,
|
|
69
80
|
spool_max_size: int = 8 * 1024 * 1024,
|
|
70
81
|
**kwargs: Any,
|
|
71
82
|
) -> None:
|
|
@@ -84,6 +95,18 @@ class DataViewFileSystem(ChainedFileSystem):
|
|
|
84
95
|
self.source = source
|
|
85
96
|
self.root = fo
|
|
86
97
|
self.mapping = _validate_mapping(DEFAULT_MAPPING if mapping is None else mapping)
|
|
98
|
+
self.provided_schema = normalize_schema(provided_schema)
|
|
99
|
+
self.requested_schema = normalize_schema(requested_schema)
|
|
100
|
+
self.schema_policy = SchemaPolicy(schema_policy)
|
|
101
|
+
self.renames = renames
|
|
102
|
+
self.defaults = defaults
|
|
103
|
+
self.drop = drop
|
|
104
|
+
self.casts = casts
|
|
105
|
+
self.batch_size = batch_size
|
|
106
|
+
self.row_limit = row_limit
|
|
107
|
+
self.byte_limit = byte_limit
|
|
108
|
+
self.provided_schema_provenance: SchemaProvenance | None = None
|
|
109
|
+
self.requested_schema_provenance: SchemaProvenance | None = None
|
|
87
110
|
self.spool_max_size = spool_max_size
|
|
88
111
|
self._listings: dict[str, dict[str, ViewEntry]] = {}
|
|
89
112
|
self._sizes: dict[str, int] = {}
|
|
@@ -154,11 +177,11 @@ class DataViewFileSystem(ChainedFileSystem):
|
|
|
154
177
|
"""``path`` relative to the view. Paths under the root are accepted too, since fsspec
|
|
155
178
|
hands a chained URL without an outer path (``dataview::file:///data``) the wrapped
|
|
156
179
|
filesystem's path."""
|
|
157
|
-
path = self._strip_protocol(path)
|
|
158
|
-
root = self.root.
|
|
159
|
-
if root and (path == root or path.startswith(f"{root}/")):
|
|
160
|
-
path = path[len(root) :]
|
|
161
|
-
return path
|
|
180
|
+
path = self._strip_protocol(path)
|
|
181
|
+
root = self.root.rstrip("/")
|
|
182
|
+
if root and (path.rstrip("/") == root or path.startswith(f"{root}/")):
|
|
183
|
+
path = path[len(root) :]
|
|
184
|
+
return path.strip("/")
|
|
162
185
|
|
|
163
186
|
def _info(self, path: str, entry: ViewEntry) -> dict[str, Any]:
|
|
164
187
|
if entry.facet == "children":
|
|
@@ -179,6 +202,11 @@ class DataViewFileSystem(ChainedFileSystem):
|
|
|
179
202
|
entries: dict[str, ViewEntry] = {}
|
|
180
203
|
if source_dir == self.root:
|
|
181
204
|
root = self.source.describe(self.root)
|
|
205
|
+
if not root.has(Facet.CHILDREN):
|
|
206
|
+
# A single object as the root: the view holds that object's renderings.
|
|
207
|
+
entries = {entry.name: entry for entry in self._render_node(root, _basename(root.path))}
|
|
208
|
+
self._listings[source_dir] = entries
|
|
209
|
+
return entries
|
|
182
210
|
for entry in self._render_node(root, ""):
|
|
183
211
|
if entry.facet != "children":
|
|
184
212
|
entries[entry.name] = entry
|
|
@@ -221,11 +249,15 @@ class DataViewFileSystem(ChainedFileSystem):
|
|
|
221
249
|
entries += [self._entry_for(node, f"{stem}.{form}", "index", form) for form in self.mapping.get("index", ())]
|
|
222
250
|
if node.has(Facet.TABLE):
|
|
223
251
|
forms = self.mapping.get("table", ())
|
|
252
|
+
native = node.native_format.value if node.native_format is not None else None
|
|
253
|
+
# The native file and a conversion to its own format share a name: with schema
|
|
254
|
+
# options the conversion wins, otherwise the native file does.
|
|
255
|
+
keep_raw = "raw" in forms and node.has(Facet.RAW) and not (self.converts and native in forms)
|
|
224
256
|
for form in forms:
|
|
225
257
|
if form == "raw":
|
|
226
|
-
if
|
|
258
|
+
if keep_raw:
|
|
227
259
|
entries.append(self._entry_for(node, base, "raw", "raw"))
|
|
228
|
-
elif not (
|
|
260
|
+
elif not (form == native and keep_raw):
|
|
229
261
|
entries.append(self._entry_for(node, f"{stem}.{form}", "table", form))
|
|
230
262
|
entries += [self._entry_for(node, f"{stem}.schema.{form}", "schema", form) for form in self.mapping.get("schema", ())]
|
|
231
263
|
elif node.has(Facet.RAW) and "raw" in self.mapping.get("raw", ()):
|
|
@@ -238,22 +270,57 @@ class DataViewFileSystem(ChainedFileSystem):
|
|
|
238
270
|
url = self.fs.unstrip_protocol(node.path) if self.fs is not None else None
|
|
239
271
|
return ViewEntry(name, node, facet, form, url)
|
|
240
272
|
|
|
273
|
+
@property
|
|
274
|
+
def converts(self) -> bool:
|
|
275
|
+
"""Whether the view applies schema options to the tables it renders, so their rows differ
|
|
276
|
+
from the source's."""
|
|
277
|
+
return any(
|
|
278
|
+
value is not None for value in (self.provided_schema, self.requested_schema, self.renames, self.defaults, self.drop, self.casts)
|
|
279
|
+
) or (self.schema_policy is not SchemaPolicy.EXACT)
|
|
280
|
+
|
|
281
|
+
def _rows(self, node: Node) -> tuple[pa.Schema, Any]:
|
|
282
|
+
"""The schema and batches a table renders, after the view's schema options."""
|
|
283
|
+
limits = {"batch_size": self.batch_size, "row_limit": self.row_limit, "byte_limit": self.byte_limit}
|
|
284
|
+
provided = resolve_schema(self.provided_schema)
|
|
285
|
+
requested = resolve_schema(self.requested_schema)
|
|
286
|
+
self.provided_schema_provenance = provided.provenance if provided is not None else None
|
|
287
|
+
self.requested_schema_provenance = requested.provenance if requested is not None else None
|
|
288
|
+
if provided is not None and node.native_format is not None and node.has(Facet.RAW):
|
|
289
|
+
decoded = DEFAULT_REGISTRY.get(node.native_format).iter_batches(self.source.open_raw(node.path), schema=provided.schema, **limits)
|
|
290
|
+
else:
|
|
291
|
+
decoded = self.source.scan(node.path, **limits)
|
|
292
|
+
if provided is not None and not decoded.schema.equals(provided.schema, check_metadata=False):
|
|
293
|
+
raise ValueError("provided schema does not match the source schema")
|
|
294
|
+
if not self.converts:
|
|
295
|
+
return decoded.schema, decoded
|
|
296
|
+
plan = plan_schema(
|
|
297
|
+
decoded.schema,
|
|
298
|
+
requested.schema if requested is not None else decoded.schema,
|
|
299
|
+
self.schema_policy,
|
|
300
|
+
renames=self.renames,
|
|
301
|
+
defaults=self.defaults,
|
|
302
|
+
drop=self.drop,
|
|
303
|
+
casts=self.casts,
|
|
304
|
+
)
|
|
305
|
+
return plan.requested_schema, (plan.apply_batch(batch) for batch in decoded)
|
|
306
|
+
|
|
241
307
|
def _render(self, entry: ViewEntry, output) -> None:
|
|
242
308
|
path = entry.node.path
|
|
243
|
-
|
|
309
|
+
native = entry.node.native_format
|
|
310
|
+
if entry.facet == "raw" or (entry.facet == "table" and native is not None and entry.form == native.value and not self.converts):
|
|
244
311
|
with self.source.open_raw(path) as raw:
|
|
245
312
|
while chunk := raw.read(1024 * 1024):
|
|
246
313
|
output.write(chunk)
|
|
247
314
|
elif entry.facet == "table":
|
|
248
|
-
|
|
315
|
+
schema, batches = self._rows(entry.node)
|
|
249
316
|
if entry.form == "json":
|
|
250
|
-
_write_json(pa.Table.from_batches(list(
|
|
317
|
+
_write_json(pa.Table.from_batches(list(batches), schema=schema).to_pylist(), output)
|
|
251
318
|
else:
|
|
252
|
-
DEFAULT_REGISTRY.get(entry.form).encode_batches_to(
|
|
319
|
+
DEFAULT_REGISTRY.get(entry.form).encode_batches_to(batches, output, schema=schema)
|
|
253
320
|
elif entry.facet == "metadata":
|
|
254
321
|
_write_json(self.source.metadata(path), output)
|
|
255
322
|
elif entry.facet == "schema":
|
|
256
|
-
schema = self.source.schema(path)
|
|
323
|
+
schema = self._rows(entry.node)[0] if self.converts else self.source.schema(path)
|
|
257
324
|
_write_json({"fields": [{"name": field.name, "type": str(field.type), "nullable": field.nullable} for field in schema]}, output)
|
|
258
325
|
elif entry.facet == "index":
|
|
259
326
|
rows = [self._index_row(child) for child in self.source.children(path)]
|
|
@@ -13,7 +13,7 @@ authors = [
|
|
|
13
13
|
description = "Arrow interchange planning for the fsspec data ecosystem"
|
|
14
14
|
readme = "README.md"
|
|
15
15
|
license = { text = "Apache-2.0" }
|
|
16
|
-
version = "0.
|
|
16
|
+
version = "0.8.0"
|
|
17
17
|
requires-python = ">=3.11"
|
|
18
18
|
keywords = ["arrow", "data", "fsspec", "interchange"]
|
|
19
19
|
|
|
@@ -63,7 +63,6 @@ develop = [
|
|
|
63
63
|
[project.scripts]
|
|
64
64
|
|
|
65
65
|
[project.entry-points."fsspec.specs"]
|
|
66
|
-
"fsspec-data" = "fsspec_data.filesystem:DataFileSystem"
|
|
67
66
|
"dataview" = "fsspec_data.view:DataViewFileSystem"
|
|
68
67
|
|
|
69
68
|
[project.urls]
|
|
@@ -71,7 +70,7 @@ Repository = "https://github.com/1kbgz/fsspec-data"
|
|
|
71
70
|
Homepage = "https://github.com/1kbgz/fsspec-data"
|
|
72
71
|
|
|
73
72
|
[tool.bumpversion]
|
|
74
|
-
current_version = "0.
|
|
73
|
+
current_version = "0.8.0"
|
|
75
74
|
commit = true
|
|
76
75
|
tag = false
|
|
77
76
|
commit_args = "-s"
|