[WIP] Proposal: Dynaword bundle layout (data.parquet + metadata.parquet)
#129
by Argonus92 - opened
- AGENTS.md +26 -0
- README.md +5 -1
- src/migrate_to_bundle.py +109 -0
- src/normalize_schema.py +19 -0
- src/test_added_dates.py +3 -2
- src/test_migrate_to_bundle.py +66 -0
- src/test_uniform_schema.py +36 -3
AGENTS.md
CHANGED
|
@@ -62,6 +62,32 @@ in five more shipped sources. Rules:
|
|
| 62 |
8. Never write to rights holders, accept terms, create accounts or get past a
|
| 63 |
CAPTCHA — hand those to the maintainer.
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
## Add a source
|
| 66 |
|
| 67 |
1. Produce the artifact under `data/<key>/`:
|
|
|
|
| 62 |
8. Never write to rights holders, accept terms, create accounts or get past a
|
| 63 |
CAPTCHA — hand those to the maintainer.
|
| 64 |
|
| 65 |
+
## Bundle layout (proposed for the next version)
|
| 66 |
+
|
| 67 |
+
The next version moves every source to the Danish Dynaword bundle layout. A source
|
| 68 |
+
directory then holds:
|
| 69 |
+
|
| 70 |
+
- `data.parquet` — `id, text, source, added, created, token_count` (`DATA_SCHEMA`
|
| 71 |
+
in `src/normalize_schema.py`); `created` is a date pair `YYYY-MM-DD, YYYY-MM-DD`,
|
| 72 |
+
with `0001-01-01` as an unknown lower bound.
|
| 73 |
+
- `metadata.parquet` — `id, license, author, source_ref, attribution,
|
| 74 |
+
quality_warnings` (`METADATA_SCHEMA`), one row per row of `data.parquet`, same
|
| 75 |
+
order, joined by `id`. Per-record provenance lives here instead of
|
| 76 |
+
`<key>.attribution.jsonl`: `source_ref` is the record's URI, `attribution` the
|
| 77 |
+
remaining evidence as a JSON string.
|
| 78 |
+
- `<key>.md` — the datasheet, with the Danish YAML header (`pretty_name`,
|
| 79 |
+
`language`, `license`, `domains`).
|
| 80 |
+
- `<key>.stats.json`, `<key>.license-evidence.json` — unchanged.
|
| 81 |
+
|
| 82 |
+
`src/migrate_to_bundle.py` converts a legacy `<key>.parquet` (+ attribution
|
| 83 |
+
sidecar) without touching it; it refuses a `created` value it cannot read as a
|
| 84 |
+
date, year or year range rather than guess. The Hub default config is one split, so
|
| 85 |
+
legacy shards and bundles never ship side by side
|
| 86 |
+
(`test_a_source_ships_one_layout_and_the_corpus_does_not_mix_them`): all sources
|
| 87 |
+
move in the release that switches `configs` in the README to `data/*/data.parquet`
|
| 88 |
+
(plus a `metadata` config over `data/*/metadata.parquet`). Until then, add sources
|
| 89 |
+
in the current layout below.
|
| 90 |
+
|
| 91 |
## Add a source
|
| 92 |
|
| 93 |
1. Produce the artifact under `data/<key>/`:
|
README.md
CHANGED
|
@@ -15,7 +15,11 @@ configs:
|
|
| 15 |
- config_name: default
|
| 16 |
data_files:
|
| 17 |
- split: train
|
| 18 |
-
path: data/*/
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
---
|
| 20 |
|
| 21 |
# Polish DynaWord
|
|
|
|
| 15 |
- config_name: default
|
| 16 |
data_files:
|
| 17 |
- split: train
|
| 18 |
+
path: data/*/data.parquet
|
| 19 |
+
- config_name: metadata
|
| 20 |
+
data_files:
|
| 21 |
+
- split: train
|
| 22 |
+
path: data/*/metadata.parquet
|
| 23 |
---
|
| 24 |
|
| 25 |
# Polish DynaWord
|
src/migrate_to_bundle.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Convert one legacy source (`<key>.parquet` + optional `<key>.attribution.jsonl`) into the bundle layout.
|
| 3 |
+
|
| 4 |
+
Writes `data.parquet` and `metadata.parquet` into `--out` and never touches the legacy files.
|
| 5 |
+
Usage: python3 src/migrate_to_bundle.py data/<key> --out <dir>
|
| 6 |
+
"""
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import argparse
|
| 10 |
+
import json
|
| 11 |
+
import re
|
| 12 |
+
import sys
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
import pyarrow as pa
|
| 16 |
+
import pyarrow.parquet as pq
|
| 17 |
+
|
| 18 |
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
| 19 |
+
from normalize_schema import DATA_NAME, DATA_SCHEMA, METADATA_NAME, METADATA_SCHEMA, SCHEMA
|
| 20 |
+
|
| 21 |
+
BATCH_ROWS = 50_000
|
| 22 |
+
# the recipe's convention for an unknown lower bound; the upper bound falls back to the row's `added`
|
| 23 |
+
UNKNOWN_START = "0001-01-01"
|
| 24 |
+
_DAY = r"\d{4}-\d{2}-\d{2}"
|
| 25 |
+
_PAIR = re.compile(rf"({_DAY}), ({_DAY})")
|
| 26 |
+
_YEARS = re.compile(r"(\d{4})(?:\s*[-–]\s*(\d{4}))?")
|
| 27 |
+
_URL_KEYS = ("url", "source_ref", "canonical_url", "source_url")
|
| 28 |
+
_MOVED_KEYS = ("id", "license", "author", *_URL_KEYS)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def created_range(value: str | None, added: str) -> str:
|
| 32 |
+
text = (value or "").strip()
|
| 33 |
+
if _PAIR.fullmatch(text):
|
| 34 |
+
return text
|
| 35 |
+
if re.fullmatch(_DAY, text):
|
| 36 |
+
return f"{text}, {text}"
|
| 37 |
+
years = _YEARS.fullmatch(text)
|
| 38 |
+
if years:
|
| 39 |
+
first, last = years.group(1), years.group(2) or years.group(1)
|
| 40 |
+
return f"{first}-01-01, {last}-12-31"
|
| 41 |
+
if text in ("", "unknown"):
|
| 42 |
+
return f"{UNKNOWN_START}, {added}"
|
| 43 |
+
raise ValueError(f"created value is not a date, year or range: {text!r}")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def read_attribution(path: Path) -> dict[str, dict]:
|
| 47 |
+
if not path.is_file():
|
| 48 |
+
return {}
|
| 49 |
+
records = {}
|
| 50 |
+
with path.open(encoding="utf-8") as lines:
|
| 51 |
+
for line in lines:
|
| 52 |
+
if line.strip():
|
| 53 |
+
record = json.loads(line)
|
| 54 |
+
records[str(record["id"])] = record
|
| 55 |
+
return records
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def metadata_fields(record: dict | None) -> tuple[str, str]:
|
| 59 |
+
if not record:
|
| 60 |
+
return "", ""
|
| 61 |
+
source_ref = next((str(record[key]) for key in _URL_KEYS if record.get(key)), "")
|
| 62 |
+
rest = {key: value for key, value in record.items() if key not in _MOVED_KEYS}
|
| 63 |
+
return source_ref, json.dumps(rest, ensure_ascii=False, sort_keys=True) if rest else ""
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def migrate(source_dir: Path, out_dir: Path) -> int:
|
| 67 |
+
key = source_dir.name
|
| 68 |
+
shard = pq.ParquetFile(source_dir / f"{key}.parquet")
|
| 69 |
+
if shard.schema_arrow.names != SCHEMA.names:
|
| 70 |
+
raise ValueError(f"{key}: legacy shard does not have the canonical schema")
|
| 71 |
+
attribution = read_attribution(source_dir / f"{key}.attribution.jsonl")
|
| 72 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 73 |
+
rows = 0
|
| 74 |
+
with pq.ParquetWriter(out_dir / DATA_NAME, DATA_SCHEMA) as data, pq.ParquetWriter(out_dir / METADATA_NAME, METADATA_SCHEMA) as meta:
|
| 75 |
+
for batch in shard.iter_batches(batch_size=BATCH_ROWS):
|
| 76 |
+
table = pa.Table.from_batches([batch])
|
| 77 |
+
ids = table["id"].to_pylist()
|
| 78 |
+
added = table["added"].to_pylist()
|
| 79 |
+
created = [created_range(value, day) for value, day in zip(table["created"].to_pylist(), added)]
|
| 80 |
+
data.write_table(table.select(DATA_SCHEMA.names).set_column(4, "created", pa.array(created, pa.string())).cast(DATA_SCHEMA))
|
| 81 |
+
refs, notes = zip(*(metadata_fields(attribution.get(doc_id)) for doc_id in ids)) if ids else ((), ())
|
| 82 |
+
meta.write_table(
|
| 83 |
+
pa.table(
|
| 84 |
+
{
|
| 85 |
+
"id": ids,
|
| 86 |
+
"license": table["license"].to_pylist(),
|
| 87 |
+
"author": table["author"].to_pylist(),
|
| 88 |
+
"source_ref": list(refs),
|
| 89 |
+
"attribution": list(notes),
|
| 90 |
+
"quality_warnings": [""] * len(ids),
|
| 91 |
+
},
|
| 92 |
+
schema=METADATA_SCHEMA,
|
| 93 |
+
)
|
| 94 |
+
)
|
| 95 |
+
rows += len(ids)
|
| 96 |
+
return rows
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def main(argv: list[str] | None = None) -> int:
|
| 100 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 101 |
+
parser.add_argument("source_dir", type=Path)
|
| 102 |
+
parser.add_argument("--out", type=Path, required=True)
|
| 103 |
+
args = parser.parse_args(argv)
|
| 104 |
+
print(f"{args.source_dir.name}: {migrate(args.source_dir, args.out)} rows -> {args.out}")
|
| 105 |
+
return 0
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
raise SystemExit(main())
|
src/normalize_schema.py
CHANGED
|
@@ -32,6 +32,25 @@ SCHEMA = pa.schema(
|
|
| 32 |
("author", pa.string()),
|
| 33 |
]
|
| 34 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
def add_missing_columns(tbl: pa.Table, source: str, default_license: str) -> pa.Table:
|
|
|
|
| 32 |
("author", pa.string()),
|
| 33 |
]
|
| 34 |
)
|
| 35 |
+
# The bundle layout (data/<key>/data.parquet + metadata.parquet), row-aligned and joined by `id`.
|
| 36 |
+
DATA_SCHEMA = pa.schema([field for field in SCHEMA if field.name not in ("license", "author")])
|
| 37 |
+
METADATA_SCHEMA = pa.schema(
|
| 38 |
+
[
|
| 39 |
+
("id", pa.string()),
|
| 40 |
+
("license", pa.string()),
|
| 41 |
+
("author", pa.string()),
|
| 42 |
+
("source_ref", pa.string()),
|
| 43 |
+
("attribution", pa.string()),
|
| 44 |
+
("quality_warnings", pa.string()),
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
DATA_NAME = "data.parquet"
|
| 48 |
+
METADATA_NAME = "metadata.parquet"
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def shard_path(source_dir: Path) -> Path:
|
| 52 |
+
bundle = source_dir / DATA_NAME
|
| 53 |
+
return bundle if bundle.is_file() else source_dir / f"{source_dir.name}.parquet"
|
| 54 |
|
| 55 |
|
| 56 |
def add_missing_columns(tbl: pa.Table, source: str, default_license: str) -> pa.Table:
|
src/test_added_dates.py
CHANGED
|
@@ -12,6 +12,7 @@ import pytest
|
|
| 12 |
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
| 13 |
import build_dynaword # noqa: E402
|
| 14 |
import make_docs # noqa: E402
|
|
|
|
| 15 |
from sources import ADDED, SOURCES # noqa: E402
|
| 16 |
|
| 17 |
DATA = Path(__file__).resolve().parents[1] / "data"
|
|
@@ -22,7 +23,7 @@ STALE_SHARDS = {
|
|
| 22 |
"wikipedia": "2026-07-19", "wikinews": "2026-07-19", "wikivoyage": "2026-07-19", "wikibooks": "2026-07-19",
|
| 23 |
"sejm_interpellations": "2026-07-19",
|
| 24 |
}
|
| 25 |
-
SHARDS = sorted(key for key in SOURCES if (DATA / key
|
| 26 |
CARDS = sorted(key for key in SOURCES if (DATA / key / f"{key}.md").is_file())
|
| 27 |
STATS = sorted(key for key in SOURCES if (DATA / key / f"{key}.stats.json").is_file())
|
| 28 |
|
|
@@ -34,7 +35,7 @@ def test_v020_sources_carry_the_v020_date():
|
|
| 34 |
|
| 35 |
@pytest.mark.parametrize("key", SHARDS)
|
| 36 |
def test_shard_carries_the_registry_date(key):
|
| 37 |
-
dates = pc.unique(pq.read_table(DATA / key
|
| 38 |
assert dates == [STALE_SHARDS.get(key, SOURCES[key]["added"])]
|
| 39 |
|
| 40 |
|
|
|
|
| 12 |
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
| 13 |
import build_dynaword # noqa: E402
|
| 14 |
import make_docs # noqa: E402
|
| 15 |
+
from normalize_schema import shard_path # noqa: E402
|
| 16 |
from sources import ADDED, SOURCES # noqa: E402
|
| 17 |
|
| 18 |
DATA = Path(__file__).resolve().parents[1] / "data"
|
|
|
|
| 23 |
"wikipedia": "2026-07-19", "wikinews": "2026-07-19", "wikivoyage": "2026-07-19", "wikibooks": "2026-07-19",
|
| 24 |
"sejm_interpellations": "2026-07-19",
|
| 25 |
}
|
| 26 |
+
SHARDS = sorted(key for key in SOURCES if shard_path(DATA / key).is_file())
|
| 27 |
CARDS = sorted(key for key in SOURCES if (DATA / key / f"{key}.md").is_file())
|
| 28 |
STATS = sorted(key for key in SOURCES if (DATA / key / f"{key}.stats.json").is_file())
|
| 29 |
|
|
|
|
| 35 |
|
| 36 |
@pytest.mark.parametrize("key", SHARDS)
|
| 37 |
def test_shard_carries_the_registry_date(key):
|
| 38 |
+
dates = pc.unique(pq.read_table(shard_path(DATA / key), columns=["added"])["added"]).to_pylist()
|
| 39 |
assert dates == [STALE_SHARDS.get(key, SOURCES[key]["added"])]
|
| 40 |
|
| 41 |
|
src/test_migrate_to_bundle.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
import pyarrow as pa
|
| 4 |
+
import pyarrow.parquet as pq
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
from migrate_to_bundle import created_range, migrate
|
| 8 |
+
from normalize_schema import DATA_NAME, DATA_SCHEMA, METADATA_NAME, METADATA_SCHEMA, SCHEMA
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def _legacy(tmp_path, rows, attribution=None):
|
| 12 |
+
source = tmp_path / "data" / "probe"
|
| 13 |
+
source.mkdir(parents=True)
|
| 14 |
+
pq.write_table(pa.Table.from_pylist(rows, schema=SCHEMA), source / "probe.parquet")
|
| 15 |
+
if attribution is not None:
|
| 16 |
+
(source / "probe.attribution.jsonl").write_text("".join(json.dumps(r) + "\n" for r in attribution), encoding="utf-8")
|
| 17 |
+
return source
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def _row(doc_id, created="2002-2026", license_="CC-BY-4.0", author="A"):
|
| 21 |
+
return {"id": doc_id, "text": "t " + doc_id, "source": "probe", "added": "2026-06-15", "created": created,
|
| 22 |
+
"token_count": 3, "license": license_, "author": author}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@pytest.mark.parametrize(
|
| 26 |
+
"value, expected",
|
| 27 |
+
[
|
| 28 |
+
("2011-12-24, 2026-05-25", "2011-12-24, 2026-05-25"),
|
| 29 |
+
("2002-2026", "2002-01-01, 2026-12-31"),
|
| 30 |
+
("2019", "2019-01-01, 2019-12-31"),
|
| 31 |
+
("2020-03-04", "2020-03-04, 2020-03-04"),
|
| 32 |
+
("unknown", "0001-01-01, 2026-06-15"),
|
| 33 |
+
("", "0001-01-01, 2026-06-15"),
|
| 34 |
+
],
|
| 35 |
+
)
|
| 36 |
+
def test_created_becomes_a_date_pair(value, expected):
|
| 37 |
+
assert created_range(value, "2026-06-15") == expected
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def test_free_text_created_is_refused_not_guessed():
|
| 41 |
+
with pytest.raises(ValueError):
|
| 42 |
+
created_range("2018-2024 (book publication metadata proxy)", "2026-06-15")
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_a_legacy_source_becomes_a_row_aligned_bundle(tmp_path):
|
| 46 |
+
attribution = [{"id": "b", "url": "https://example.org/b", "license": "CC-BY-4.0", "text_sha256": "x"}]
|
| 47 |
+
source = _legacy(tmp_path, [_row("a"), _row("b", license_="CC0-1.0", author="")], attribution)
|
| 48 |
+
out = tmp_path / "bundle"
|
| 49 |
+
|
| 50 |
+
assert migrate(source, out) == 2
|
| 51 |
+
|
| 52 |
+
data, meta = pq.read_table(out / DATA_NAME), pq.read_table(out / METADATA_NAME)
|
| 53 |
+
assert data.schema == DATA_SCHEMA and meta.schema == METADATA_SCHEMA
|
| 54 |
+
assert data["id"].to_pylist() == meta["id"].to_pylist() == ["a", "b"]
|
| 55 |
+
assert data["created"].to_pylist() == ["2002-01-01, 2026-12-31"] * 2
|
| 56 |
+
assert meta.to_pylist()[1] == {"id": "b", "license": "CC0-1.0", "author": "", "source_ref": "https://example.org/b",
|
| 57 |
+
"attribution": '{"text_sha256": "x"}', "quality_warnings": ""}
|
| 58 |
+
assert meta.to_pylist()[0]["source_ref"] == "" and (source / "probe.parquet").is_file()
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def test_a_shard_without_the_canonical_schema_is_refused(tmp_path):
|
| 62 |
+
source = tmp_path / "data" / "probe"
|
| 63 |
+
source.mkdir(parents=True)
|
| 64 |
+
pq.write_table(pa.table({"id": ["a"], "text": ["t"]}), source / "probe.parquet")
|
| 65 |
+
with pytest.raises(ValueError):
|
| 66 |
+
migrate(source, tmp_path / "bundle")
|
src/test_uniform_schema.py
CHANGED
|
@@ -3,13 +3,46 @@ from pathlib import Path
|
|
| 3 |
|
| 4 |
import pyarrow.parquet as pq
|
| 5 |
|
| 6 |
-
from normalize_schema import SCHEMA
|
| 7 |
|
| 8 |
DATA = Path(__file__).resolve().parents[1] / "data"
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def test_every_source_parquet_has_the_canonical_schema():
|
| 12 |
-
shards =
|
| 13 |
-
assert shards, "no source shards found under data/*/*.parquet"
|
| 14 |
offenders = {str(p.relative_to(DATA.parent)): pq.read_schema(p).names for p in shards if pq.read_schema(p).names != SCHEMA.names}
|
| 15 |
assert offenders == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import pyarrow.parquet as pq
|
| 5 |
|
| 6 |
+
from normalize_schema import DATA_NAME, DATA_SCHEMA, METADATA_NAME, METADATA_SCHEMA, SCHEMA
|
| 7 |
|
| 8 |
DATA = Path(__file__).resolve().parents[1] / "data"
|
| 9 |
|
| 10 |
|
| 11 |
+
def _bundles():
|
| 12 |
+
return sorted(p.parent for p in DATA.glob(f"*/{DATA_NAME}"))
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _legacy_shards():
|
| 16 |
+
return sorted(p for p in DATA.glob("*/*.parquet") if p.name not in (DATA_NAME, METADATA_NAME))
|
| 17 |
+
|
| 18 |
+
|
| 19 |
def test_every_source_parquet_has_the_canonical_schema():
|
| 20 |
+
shards = _legacy_shards()
|
| 21 |
+
assert shards or _bundles(), "no source shards found under data/*/*.parquet"
|
| 22 |
offenders = {str(p.relative_to(DATA.parent)): pq.read_schema(p).names for p in shards if pq.read_schema(p).names != SCHEMA.names}
|
| 23 |
assert offenders == {}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_every_bundle_has_the_canonical_data_and_metadata_schemas():
|
| 27 |
+
offenders = {}
|
| 28 |
+
for bundle in _bundles():
|
| 29 |
+
for name, schema in ((DATA_NAME, DATA_SCHEMA), (METADATA_NAME, METADATA_SCHEMA)):
|
| 30 |
+
path = bundle / name
|
| 31 |
+
names = pq.read_schema(path).names if path.is_file() else None
|
| 32 |
+
if names != schema.names:
|
| 33 |
+
offenders[str(path.relative_to(DATA.parent))] = names
|
| 34 |
+
assert offenders == {}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_every_bundle_metadata_row_matches_its_data_row():
|
| 38 |
+
for bundle in _bundles():
|
| 39 |
+
data_ids = pq.read_table(bundle / DATA_NAME, columns=["id"])["id"].to_pylist()
|
| 40 |
+
metadata_ids = pq.read_table(bundle / METADATA_NAME, columns=["id"])["id"].to_pylist()
|
| 41 |
+
assert data_ids == metadata_ids, bundle.name
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_a_source_ships_one_layout_and_the_corpus_does_not_mix_them():
|
| 45 |
+
# the Hub default config is one split: legacy 8-column shards and 6-column data.parquet cannot share it
|
| 46 |
+
bundles = {p.name for p in _bundles()}
|
| 47 |
+
legacy = {p.parent.name for p in _legacy_shards()}
|
| 48 |
+
assert not (bundles and legacy), {"bundle": sorted(bundles), "legacy": sorted(legacy)}
|