Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/config-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand Down
40 changes: 40 additions & 0 deletions tests/test_registry_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
7 changes: 7 additions & 0 deletions toolkit/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)),
)


Expand Down
10 changes: 9 additions & 1 deletion toolkit/registry/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions toolkit/registry/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down