diff --git a/docs/config-schema.md b/docs/config-schema.md index a7826fd..3ba804b 100644 --- a/docs/config-schema.md +++ b/docs/config-schema.md @@ -331,6 +331,17 @@ Note pratiche: | `source_layer` | `string` | `clean` | | `source_table` | `string \| null` | `null` | | `row_group_size` | `int \| null` | `null` | +| `internal` | `bool` | `false` | + +`internal: true` = tabella intermedia del compose. Viene materializzata in +`out/` (le tabelle successive la leggono nella stessa sessione), ma **non** +entra in `mart_refs`/`marts` del registry: nessun consumer deve leggerla su +GCS. Usala per lo staging condiviso (es. una lettura unica di un support che +altrimenti verrebbe riscaricato da nodi, archi e copertura). + +Il flag governa il **contratto**, non il trasporto: un workflow che sincronizza +tutta `out/data/mart/` (es. `gsutil rsync`) carica comunque il parquet. Per non +pubblicarlo serve un sync a lista esplicita, come fa `legal-graph`. `MartValidate`: diff --git a/tests/test_registry_builder.py b/tests/test_registry_builder.py index 8223bc5..0dc08bd 100644 --- a/tests/test_registry_builder.py +++ b/tests/test_registry_builder.py @@ -1017,3 +1017,43 @@ def test_per_year_mart_single_year_stays_exact(self, tmp_path: Path) -> None: assert mart["layout"] == "year" assert mart["year"] == 2024 assert "location" not in mart + + +@pytest.mark.contract +def test_internal_mart_fuori_dal_contratto_pubblico(tmp_path: Path) -> None: + """`internal: true` = staging: materializzata, ma fuori da mart_refs/marts. + + Il parquet non viene caricato su GCS: se finisse nel registry il catalogo + annuncerebbe un oggetto inesistente. Il caso reale è lo staging condiviso + di un compose (una lettura unica di un support usata da nodi, archi e + copertura). + """ + ds_dir = tmp_path / "compose" / "demo" + ds_dir.mkdir(parents=True) + (ds_dir / "dataset.yml").write_text( + "root: '../../out'\n" + "dataset:\n" + " name: demo\n" + " years: [2024]\n" + "mart:\n" + " tables:\n" + " - name: stg_base\n" + " sql: 'sql/stg_base.sql'\n" + " internal: true\n" + " - name: mart_pubblico\n" + " sql: 'sql/mart_pubblico.sql'\n", + encoding="utf-8", + ) + layout = RepoLayout( + repo_root=tmp_path, dataset_dirs=("compose",), source_repo="dataciviclab/test" + ) + + catalog, errors = build_clean_catalog(layout) + assert not errors["derive"], errors["derive"] + ds = catalog["datasets"][0] + assert ds["mart_refs"] == ["demo__mart_pubblico"] + + contract = PathContract(prefix="test", clean_layout="year", mart_layout="year") + marts_catalog, mart_errors = build_mart_catalog(layout, path_contract=contract) + assert not mart_errors["derive"], mart_errors["derive"] + assert [m["table"] for m in marts_catalog["marts"]] == ["mart_pubblico"] diff --git a/toolkit/core/config.py b/toolkit/core/config.py index b233f37..9343474 100644 --- a/toolkit/core/config.py +++ b/toolkit/core/config.py @@ -461,6 +461,12 @@ class MartTableConfig: # group è l'unità minima che un lettore deve scaricare, e il default # mette un'intera tabella in un blocco solo. row_group_size: int | None = None + # Tabella intermedia del compose: si materializza (le tabelle successive + # la leggono nella stessa sessione e il parquet resta in out/), ma NON + # entra nel contratto pubblico né nel registry. Serve a dichiarare lo + # staging senza gonfiare il catalogo con artefatti che nessun consumer + # deve leggere. + internal: bool = False @staticmethod def from_dict(d: dict) -> MartTableConfig: @@ -472,6 +478,7 @@ def from_dict(d: dict) -> MartTableConfig: source_layer=d.get("source_layer", "clean"), source_table=d.get("source_table"), row_group_size=_optional_row_group_size(d.get("row_group_size")), + internal=bool(d.get("internal", False)), ) diff --git a/toolkit/registry/builders.py b/toolkit/registry/builders.py index 558fa65..e861927 100644 --- a/toolkit/registry/builders.py +++ b/toolkit/registry/builders.py @@ -288,8 +288,13 @@ def build_clean_catalog( # Link ai mart (convention {dataset}__{mart}) if manifest.mart_tables: + # Le tabelle `internal: true` sono staging del compose: restano in + # out/ per le tabelle successive, ma non sono un contratto pubblico + # e non vanno linkate nel catalogo (nessun oggetto su GCS). entry["mart_refs"] = [ - f"{manifest.slug}__{t.get('name')}" for t in manifest.mart_tables if t.get("name") + f"{manifest.slug}__{t.get('name')}" + for t in manifest.mart_tables + if t.get("name") and not t.get("internal") ] entry["last_run"] = last_run(latest_run_record(str(manifest.yml_path))) or ( @@ -355,6 +360,9 @@ def build_mart_catalog( name = table.get("name") if not name: continue + if table.get("internal"): + # staging interno: niente voce `marts` (non esiste su GCS) + continue entry: dict[str, Any] = { "slug": f"{manifest.slug}__{name}", "dataset": manifest.slug, diff --git a/toolkit/registry/layout.py b/toolkit/registry/layout.py index c727cc4..10bdb2d 100644 --- a/toolkit/registry/layout.py +++ b/toolkit/registry/layout.py @@ -278,6 +278,7 @@ def load_manifest(yml_path: Path) -> DatasetManifest | None: "name": t.name, "sql": t.sql, **({"years": t.years} if t.years else {}), + **({"internal": True} if getattr(t, "internal", False) else {}), } for t in cfg.mart.tables if t.name