diff --git a/CHANGELOG.md b/CHANGELOG.md index b094638..fc4b58e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ - **Registry: dataset mart-only**: i compose senza `clean:` non restano più fuori dal catalogo con i mart orfani. `build_clean_catalog` emette una entry dataset con metadati e `mart_refs`, senza `columns`/`location`; `registry.schema.json` rende i due campi opzionali. Così ACB/MCP vedono il dataset (`find`/`explore`) e risolvono i suoi mart (`gcs_query`). - **`toolkit run preflight --sql-check`**: preflight con validazione SQL in due stadi. `sql_syntax` è un parse-only (`json_serialize_sql`) di clean e mart SQL, bloccante e deterministico: non richiede RAW, schema o support materializzato (i placeholder `{...}` sono neutralizzati e i riferimenti `{support.NOME}` confrontati con i support dichiarati; i commenti fuori dai letterali sono esclusi). `sql_validation` è il binding reale contro lo schema del preview, advisory e disponibile solo per le fonti CSV che espongono colonne. Il report espone anche `col_types` per fonte. Misurato sul workspace: 634/636 config passano il syntax check con 0 falsi positivi (i 2 fail sono config davvero invalidi), 1 file `PIVOT` saltato. - **`toolkit run --dry-run` documentato** in `docs/advanced-workflows.md`: validazione del binding senza eseguire i layer, limiti dell'euristica senza schema, nota sul run record `DRY_RUN` scritto in `data/_runs`. +- **`mart_name`** (#425): pipeline mode, CLI (`toolkit inspect config --mart-name`) e MCP (`toolkit_query`) selezionano la tabella mart per nome invece che per indice. `mart_index` resta per backward compat ma e' deprecato; un nome non dichiarato fallisce con l'elenco delle tabelle disponibili. +- **Registry: codelists dai support id** (#471): `build_codelists` deriva i nomi dalle entry `support:` con `type: codelist` e `id` (fonte canonica SDMX), con fallback ai `codelists/*.csv` per i repo legacy. ### Changed @@ -28,6 +30,7 @@ - **Warning su `all_varchar` + `columns` insieme**: DuckDB usa solo `columns` (i tipi dichiarati) e ignora `all_varchar`. Il toolkit ora avvisa invece di lasciare l'opzione senza effetto. - **Layering ripristinato** (PR #504): rimosse 6 inversioni di dipendenza `core → clean/domain/scout/scaffold`. `sql_validation` spostato in `domain/`, orchestration support in `domain/support.py`, `ProbePool` in `scout/pool.py`, scaffold clean.sql spostato da `raw/run.py` a `cmd_run` (comportamento invariato). Contratto condiviso `dataset_yml_slug`/`iter_workspace_dataset_ymls` in `registry/layout.py`. - **Catalogo unico tipi fonte** (PR #505): nuovo `toolkit/core/source_types.py` come single source of truth per registry plugin, dispatch fetch e contratto AI (`contracts._SOURCE_TYPES` generato). Eliminato `_fetch_fallback` (tipo sconosciuto → `ValueError` esplicito). Tipo `script` documentato nel contratto con nota di sicurezza. +- **Run MART** (#426): i parquet non piu' dichiarati in `dataset.yml` vengono rimossi dal run (per-anno e multi-year), cosi' una tabella rinominata non lascia il vecchio file a inquinare il layer tool. ### Fixed diff --git a/tests/test_mart_multi_year.py b/tests/test_mart_multi_year.py index 4b76a19..e89ff55 100644 --- a/tests/test_mart_multi_year.py +++ b/tests/test_mart_multi_year.py @@ -67,6 +67,74 @@ def test_mart_multi_year_on_project_example(project_example: Path) -> None: assert any(t.get("years") == [2022, 2023] for t in tables), "years missing from metadata" +def _add_multi_year_table(project_example: Path) -> None: + """Aggiunge a project_example la tabella multi-year ``clean_union``.""" + sql_dir = project_example / "sql" / "multi_year" + sql_dir.mkdir(parents=True, exist_ok=True) + (sql_dir / "clean_union.sql").write_text( + "\n".join( + [ + "select", + " count(*) as rows_total,", + " count(distinct anno) as anni_distinti", + "from clean_input", + ] + ), + encoding="utf-8", + ) + config_path = project_example / "dataset.yml" + config_data = yaml.safe_load(config_path.read_text(encoding="utf-8")) + config_data["dataset"]["years"] = [2022, 2023] + config_data.setdefault("mart", {}).setdefault("tables", []).append( + { + "name": "clean_union", + "sql": "sql/multi_year/clean_union.sql", + "years": [2022, 2023], + } + ) + config_path.write_text( + yaml.dump(config_data, default_flow_style=False, allow_unicode=True, sort_keys=False), + encoding="utf-8", + ) + + +def test_mart_multi_year_cleanup_rimuove_stale(project_example: Path) -> None: + """#426: la pulizia copre anche la dir multi-year (dataset-level).""" + _add_multi_year_table(project_example) + config_path = project_example / "dataset.yml" + run_cmd(step="all", config=str(config_path)) + + mart_dir = project_example / "_smoke_out" / "data" / "mart" / "project_example" + stale = mart_dir / "mart_vecchia.parquet" + stale.write_bytes(b"stale") + + run_cmd(step="mart", config=str(config_path)) + + assert not stale.exists(), "il parquet non dichiarato va rimosso anche a livello dataset" + assert (mart_dir / "clean_union.parquet").exists() + assert (mart_dir / "2022" / "rd_by_regione.parquet").exists() + + +def test_clean_preview_mart_name_multi_year(project_example: Path) -> None: + """#425: ``mart_name`` seleziona anche una tabella multi-year.""" + from toolkit.domain.layer import clean_preview + + _add_multi_year_table(project_example) + config_path = project_example / "dataset.yml" + run_cmd(step="all", config=str(config_path)) + + result = clean_preview( + str(config_path), layer="mart", mart_name="clean_union", year=2022, limit=5 + ) + assert result["mart_name"] == "clean_union" + assert result["columns"] + + with pytest.raises(ValueError, match="non dichiarata"): + clean_preview( + str(config_path), layer="mart", mart_name="mart_inesistente", year=2022, limit=5 + ) + + def test_mart_only_multi_year(project_example: Path) -> None: """Solo tabelle multi-year: il run per-anno non deve fallire (issue #445). diff --git a/tests/test_mcp_ops.py b/tests/test_mcp_ops.py index 0199fb9..ab9c5e1 100644 --- a/tests/test_mcp_ops.py +++ b/tests/test_mcp_ops.py @@ -752,3 +752,28 @@ def test_dataset_status_shape(tmp_path, monkeypatch): for section in ("paths_info", "summary", "readiness", "run_stats", "info"): assert section in result assert result["info"]["dataset"] == dataset + + +def test_clean_preview_mart_name( + mcp_project_example: tuple[Path, str, int], +) -> None: + """Issue #425: selezione della tabella mart per nome, non per indice.""" + from toolkit.mcp.errors import ToolkitClientError + from toolkit.mcp.schema_ops import clean_preview + + config_path, dataset, year = mcp_project_example + root = Path(config_path).parent / "_smoke_out" + mart_dir = root / "data" / "mart" / dataset / str(year) + mart_dir.mkdir(parents=True, exist_ok=True) + _make_parquet(mart_dir / "rd_by_regione.parquet") + _make_parquet(mart_dir / "rd_by_provincia.parquet") + + result = clean_preview( + str(config_path), layer="mart", mart_name="rd_by_provincia", year=year, limit=5 + ) + assert result["mart_name"] == "rd_by_provincia" + + with pytest.raises(ToolkitClientError, match="non dichiarata"): + clean_preview( + str(config_path), layer="mart", mart_name="mart_inesistente", year=year, limit=5 + ) diff --git a/tests/test_pipeline_integration.py b/tests/test_pipeline_integration.py index 097efa1..3fbb523 100644 --- a/tests/test_pipeline_integration.py +++ b/tests/test_pipeline_integration.py @@ -342,3 +342,48 @@ def test_run_mart_supports_support_placeholder(tmp_path: Path) -> None: f"SELECT marker FROM read_parquet('{mart_output.as_posix()}')" ).fetchone() == ("ok",) assert result["output_rows"] == 1 + + +def test_run_mart_rimuove_output_non_dichiarati(project_example: Path, monkeypatch) -> None: + """Issue #426: il run mart elimina i parquet non piu' dichiarati.""" + _simplify_sql_project(project_example) + config_path = project_example / "dataset.yml" + + monkeypatch.chdir(project_example) + cfg = load_config(config_path) + year = cfg.years[0] + logger = NoopLogger() + + run_raw( + cfg.dataset, + year, + cfg.root, + cfg.raw, + logger, + base_dir=cfg.base_dir, + output_cfg=cfg.output, + clean_cfg=cfg.clean, + ) + run_profile_fn(cfg, [year], logger) + run_clean( + cfg.dataset, year, cfg.root, cfg.clean, logger, base_dir=cfg.base_dir, output_cfg=cfg.output + ) + + mart_dir = Path(cfg.root) / "data" / "mart" / cfg.dataset / str(year) + mart_dir.mkdir(parents=True, exist_ok=True) + stale = mart_dir / "mart_vecchia.parquet" + stale.write_bytes(b"stale") + + run_mart( + cfg.dataset, + year, + cfg.root, + cfg.mart, + logger, + base_dir=cfg.base_dir, + clean_cfg=cfg.clean, + output_cfg=cfg.output, + ) + + assert not stale.exists(), "il parquet non dichiarato deve essere rimosso" + assert (mart_dir / "rd_by_regione.parquet").exists() diff --git a/tests/test_registry_builder.py b/tests/test_registry_builder.py index 3305036..2f74200 100644 --- a/tests/test_registry_builder.py +++ b/tests/test_registry_builder.py @@ -590,6 +590,90 @@ def test_registry_builder_imports() -> None: # --------------------------------------------------------------------------- +@pytest.mark.contract +def test_codelists_from_support_ids(tmp_path: Path) -> None: + """codelists derivate dai support id (SDMX), con fallback ai CSV repo.""" + from toolkit.registry.builders import build_codelists + + ds_dir = tmp_path / "datasets" / "demo" + ds_dir.mkdir(parents=True) + (ds_dir / "dataset.yml").write_text( + """ +root: "../../out" +schema_version: 1 +dataset: + name: demo_sdmx + years: [2024] +support: + - name: freq + type: codelist + provider: sdmx + agency: ESTAT + id: FREQ + - name: unit + type: codelist + provider: sdmx + agency: ESTAT + id: unit + - name: comuni + type: dataset + config: "../support_datasets/comuni/dataset.yml" + years: [2024] +""", + encoding="utf-8", + ) + cl_dir = tmp_path / "codelists" + cl_dir.mkdir() + (cl_dir / "geo.csv").write_text("code,label_en\nIT,Lombardia\n", encoding="utf-8") + + layout = RepoLayout( + repo_root=tmp_path, + dataset_dirs=("datasets",), + source_repo="dataciviclab/demo", + ) + payload, errors = build_codelists(layout) + assert errors == {"derive": [], "validation": []}, f"errori inattesi: {errors}" + assert payload["codelists"] == ["freq", "geo", "unit"] + + +@pytest.mark.contract +def test_support_without_type_is_not_a_codelist(tmp_path: Path) -> None: + """Un support senza `type` vale "dataset": il suo `id` non è una codelist. + + In ``core/config.py`` il tipo mancante diventa ``"dataset"``. Se il + support porta anche un ``id`` (campo che il modello non vieta), non deve + finire nella sezione codelists. + """ + from toolkit.registry.builders import build_codelists + + ds_dir = tmp_path / "datasets" / "demo" + ds_dir.mkdir(parents=True) + (ds_dir / "dataset.yml").write_text( + """ +root: "../../out" +schema_version: 1 +dataset: + name: demo_senza_type + years: [2024] +support: + - name: comuni + config: "../support_datasets/comuni/dataset.yml" + years: [2024] + id: NON_E_UNA_CODELIST +""", + encoding="utf-8", + ) + + layout = RepoLayout( + repo_root=tmp_path, + dataset_dirs=("datasets",), + source_repo="dataciviclab/demo", + ) + payload, errors = build_codelists(layout) + assert errors == {"derive": [], "validation": []}, f"errori inattesi: {errors}" + assert payload["codelists"] == [] + + @pytest.mark.contract def test_codelists_contract(tmp_path: Path) -> None: """codelists espone i NOMI dei codelist del repo (contract).""" diff --git a/toolkit/cli/inspect/__init__.py b/toolkit/cli/inspect/__init__.py index 49d026f..d79ccf4 100644 --- a/toolkit/cli/inspect/__init__.py +++ b/toolkit/cli/inspect/__init__.py @@ -54,7 +54,10 @@ def inspect_config( year: int = typer.Option(0, "--year", "-y", help="Anno"), sql: str | None = typer.Option(None, "--sql", help="SQL query (mode=sql)"), limit: int = typer.Option(20, "--limit", help="Max righe (mode=preview/sql)"), - mart_index: int = typer.Option(0, "--mart-index", help="Indice tabella mart"), + mart_index: int = typer.Option(0, "--mart-index", help="Indice tabella mart (deprecato)"), + mart_name: str | None = typer.Option( + None, "--mart-name", help="Nome tabella mart (es. mart_comuni)" + ), diff: bool = typer.Option(False, "--diff", help="Schema-diff RAW tra anni"), json_output: bool = typer.Option(False, "--json", help="Output JSON"), ): @@ -67,6 +70,7 @@ def inspect_config( sql=sql, limit=limit, mart_index=mart_index, + mart_name=mart_name, diff=diff, json_output=json_output, ) diff --git a/toolkit/cli/inspect/config_ops.py b/toolkit/cli/inspect/config_ops.py index e65384d..40088d0 100644 --- a/toolkit/cli/inspect/config_ops.py +++ b/toolkit/cli/inspect/config_ops.py @@ -37,7 +37,10 @@ def config( year: int = typer.Option(0, "--year", "-y", help="Anno (default: ultimo configurato)"), sql: str | None = typer.Option(None, "--sql", help="SQL query (solo mode=sql)"), limit: int = typer.Option(20, "--limit", help="Max righe (solo mode=preview/sql)"), - mart_index: int = typer.Option(0, "--mart-index", help="Indice tabella mart (default 0)"), + mart_index: int = typer.Option(0, "--mart-index", help="Indice tabella mart (deprecato)"), + mart_name: str | None = typer.Option( + None, "--mart-name", help="Nome tabella mart (es. mart_comuni)" + ), diff: bool = typer.Option(False, "--diff", help="Schema-diff RAW tra anni"), json_output: bool = typer.Option(False, "--json", help="Output JSON"), ) -> None: @@ -122,6 +125,7 @@ def config( limit=limit, sql=sql, mart_index=mart_index, + mart_name=mart_name, ) except (ValueError, FileNotFoundError) as exc: if json_output: diff --git a/toolkit/domain/layer.py b/toolkit/domain/layer.py index 4d8fae5..d762599 100644 --- a/toolkit/domain/layer.py +++ b/toolkit/domain/layer.py @@ -68,11 +68,22 @@ def _resolve_clean_path(cfg: Any, year: int) -> Path: return Path(parquet_str) -def _resolve_mart_path(cfg: Any, year: int, mart_index: int = 0) -> Path: +def _resolve_mart_path( + cfg: Any, year: int, mart_index: int = 0, mart_name: str | None = None +) -> Path: paths = _payload_for_year(cfg, year) outputs = paths["paths"]["mart"].get("outputs") or [] if not outputs: raise FileNotFoundError("Nessun output mart configurato") + if mart_name: + name = str(mart_name).strip() + for output in outputs: + if Path(output).stem == name: + return Path(output) + available = ", ".join(sorted(Path(p).stem for p in outputs)) + raise ValueError( + f"Tabella mart '{name}' non dichiarata in dataset.yml: disponibili {available}" + ) if mart_index < 0 or mart_index >= len(outputs): raise ValueError(f"Indice mart {mart_index} non valido: {len(outputs)} output disponibili") return Path(outputs[mart_index]) @@ -260,6 +271,7 @@ def clean_preview( mart_index: int = 0, year: int | None = None, limit: int = 10, + mart_name: str | None = None, ) -> dict[str, Any]: """Preview dati da un parquet clean o mart.""" from toolkit.domain.path_resolver import payload_for_year as _payload_for_year @@ -272,7 +284,7 @@ def clean_preview( if layer == "clean": parquet_path = _resolve_clean_path(cfg, year) elif layer == "mart": - parquet_path = _resolve_mart_path(cfg, year, mart_index) + parquet_path = _resolve_mart_path(cfg, year, mart_index, mart_name) else: raise ValueError(f"layer deve essere 'clean' o 'mart', non '{layer}'") @@ -394,6 +406,7 @@ def layer_sql( limit: int = 20, sql: str | None = None, mart_index: int = 0, + mart_name: str | None = None, table: str | None = None, dry_run: bool = False, ) -> dict[str, Any]: @@ -411,7 +424,9 @@ def layer_sql( limit: Max righe. sql: Query SQL. I dati sono disponibili come tabella ``data`` (singolo dataset) o CTE col nome slug (multi dataset). - mart_index: Indice tabella mart (solo pipeline mode). + mart_index: Indice tabella mart (solo pipeline mode, deprecato: + usare ``mart_name``). + mart_name: Nome tabella mart (es ``"mart_comuni"``). dry_run: Se True, valida lo scope SQL e fa EXPLAIN senza eseguire (stesso contratto del dry_run di clean-query). @@ -492,7 +507,7 @@ def layer_sql( if layer == "clean": parquet_path = _resolve_clean_path(cfg, year) elif layer == "mart": - parquet_path = _resolve_mart_path(cfg, year, mart_index) + parquet_path = _resolve_mart_path(cfg, year, mart_index, mart_name) else: raise ValueError(f"layer deve essere 'raw', 'clean' o 'mart', non '{layer}'") @@ -571,6 +586,7 @@ def layer_query( limit: int = 20, sql: str | None = None, mart_index: int = 0, + mart_name: str | None = None, table: str | None = None, dry_run: bool = False, ) -> dict[str, Any]: @@ -584,7 +600,9 @@ def layer_query( year: Anno filtro. limit: Max righe in preview/sql. sql: Query SQL per mode=sql. - mart_index: Indice tabella mart (solo pipeline mode). + mart_index: Indice tabella mart (solo pipeline mode, deprecato: + usare ``mart_name``). + mart_name: Nome tabella mart (es ``"mart_comuni"``). dry_run: Se True e mode=sql, valida lo scope + EXPLAIN senza eseguire. Raises: @@ -644,7 +662,12 @@ def layer_query( if safe_layer == "raw": return raw_preview(config_path, year=year, limit=limit) return clean_preview( - config_path, layer=safe_layer, mart_index=mart_index, year=year, limit=limit + config_path, + layer=safe_layer, + mart_index=mart_index, + mart_name=mart_name, + year=year, + limit=limit, ) if safe_mode == "sql": return layer_sql( @@ -655,6 +678,7 @@ def layer_query( limit=limit, sql=sql, mart_index=mart_index, + mart_name=mart_name, ) raise RuntimeError(f"mode non gestito: {safe_mode}") diff --git a/toolkit/mart/run.py b/toolkit/mart/run.py index d4201a2..5cfee5b 100644 --- a/toolkit/mart/run.py +++ b/toolkit/mart/run.py @@ -53,6 +53,31 @@ def parquet_copy_options(table_cfg: Any) -> str: return opts +def _cleanup_stale_mart_outputs(mart_dir: Path, declared: set[str], logger) -> list[Path]: + """Rimuove i parquet mart non piu' dichiarati in dataset.yml (issue #426). + + Le tabelle rinominate lasciavano il vecchio parquet su disco: il layer + tool poteva leggere quello sbagliato (ordine alfabetico). La pulizia gira + dopo la scrittura dei nuovi output, cosi' un run fallito non cancella + nulla. + + La dir e' considerata output-only: ogni ``*.parquet`` non dichiarato viene + rimosso, compresi eventuali file manuali (il warning li nomina). Nel + percorso multi-year gira prima di ``_validate_multi_year_tables``: se la + validazione fallisce, i file rimossi sono comunque non dichiarati. + """ + removed: list[Path] = [] + if not declared or not mart_dir.is_dir(): + return removed + for path in sorted(mart_dir.glob("*.parquet")): + if path.stem in declared: + continue + path.unlink() + removed.append(path) + logger.warning("MART: rimosso output non dichiarato %s", short_path(path)) + return removed + + def _validate_multi_year_tables( mart_dir: Path, mart_cfg: dict[str, Any], @@ -212,6 +237,11 @@ def run_mart_multi_year( # le table_rules/required_tables delle tabelle con years vengono # applicate qui, dopo la produzione. Prima questa validazione non # esisteva: le regole multi-year non venivano mai applicate. + _cleanup_stale_mart_outputs( + multi_year_dir, + {str(t.get("name")) for t in multi_year_tables if t.get("name")}, + logger, + ) validation_result = _validate_multi_year_tables( multi_year_dir, mart_cfg, @@ -556,6 +586,10 @@ def run_mart( executed.extend(hierarchy_executed) total_rows += hierarchy_rows + declared_names = {str(t.get("name")) for t in single_year_tables if t.get("name")} + declared_names.update(str(e["name"]) for e in executed if e.get("name")) + _cleanup_stale_mart_outputs(mart_dir, declared_names, logger) + outputs = [file_record(p) for p in written] metadata_payload: dict[str, Any] = { "layer": "mart", diff --git a/toolkit/mcp/aggregate_ops.py b/toolkit/mcp/aggregate_ops.py index a860470..ea7caea 100644 --- a/toolkit/mcp/aggregate_ops.py +++ b/toolkit/mcp/aggregate_ops.py @@ -51,6 +51,7 @@ def layer_query( limit: int = 20, sql: str | None = None, mart_index: int = 0, + mart_name: str | None = None, table: str | None = None, dry_run: bool = False, ) -> dict[str, Any]: @@ -70,8 +71,10 @@ def layer_query( year: Anno filtro. limit: Max righe. sql: Query SQL per mode=sql. - mart_index: Indice tabella mart (solo pipeline mode). - table: Nome tabella mart (es ``"mart_top_sa"``). + mart_index: Indice tabella mart (solo pipeline mode, deprecato: + usare ``mart_name``). + mart_name: Nome tabella mart, pipeline mode (es ``"mart_top_sa"``). + table: Nome tabella mart, catalog mode (es ``"mart_top_sa"``). dry_run: Se True (mode=sql), valida lo scope SQL e fa EXPLAIN senza eseguire. Raises: @@ -98,6 +101,7 @@ def layer_query( limit=limit, sql=sql, mart_index=mart_index, + mart_name=mart_name, table=table, dry_run=dry_run, ) diff --git a/toolkit/mcp/schema_ops.py b/toolkit/mcp/schema_ops.py index 988dee3..2933403 100644 --- a/toolkit/mcp/schema_ops.py +++ b/toolkit/mcp/schema_ops.py @@ -296,6 +296,7 @@ def clean_preview( mart_index: int = 0, year: int | None = None, limit: int = 10, + mart_name: str | None = None, ) -> dict[str, Any]: """Preview dati da un parquet clean o mart. @@ -308,6 +309,7 @@ def clean_preview( str(_safe_path(config_path)), layer=layer, mart_index=mart_index, + mart_name=mart_name, year=year, limit=limit, ) diff --git a/toolkit/mcp/server.py b/toolkit/mcp/server.py index c5d3af2..849ea83 100644 --- a/toolkit/mcp/server.py +++ b/toolkit/mcp/server.py @@ -160,7 +160,7 @@ def toolkit_dataset( "Query dati: SQL su dataset (raw/clean/mart) o preview URL CSV/TSV.\n\n" "Actions:\n" "- run: esegui SQL su uno o piu' dataset\n" - " Params: datasets (list[str]), sql, layer, mode, year, limit, dry_run, config_path, mart_index, table\n" + " Params: datasets (list[str]), sql, layer, mode, year, limit, dry_run, config_path, mart_index, mart_name, table\n" " Catalog mode: datasets=[slug1, slug2], sql usa gli slug come tabelle\n" " Pipeline mode: config_path, sql usa 'data' come tabella\n" "- preview: preview remoto CSV/TSV (params: url, known_encoding, known_delim, known_decimal, known_skip)" @@ -179,6 +179,7 @@ def toolkit_query( dry_run: bool = False, config_path: str | None = None, mart_index: int = 0, + mart_name: str | None = None, table: str | None = None, # preview params url: str | None = None, @@ -206,6 +207,7 @@ def toolkit_query( limit=limit, sql=sql, mart_index=mart_index, + mart_name=mart_name, table=table, dry_run=dry_run, ) diff --git a/toolkit/registry/builders.py b/toolkit/registry/builders.py index 7e432c0..23d53e6 100644 --- a/toolkit/registry/builders.py +++ b/toolkit/registry/builders.py @@ -12,7 +12,9 @@ - ``marts``: tabelle analitiche con il *come* sono partizionate (``layout``); - ``checks``: solo anomalie riproducibili dal config (clean.sql mancante, nessun mart) — vuoto quando è tutto a posto; -- ``codelists``: nomi dei codelist (contenuto letto dai CSV del repo). +- ``codelists``: nomi dei codelist, dagli ``id`` dei support ``type: + codelist`` e dagli stem dei CSV legacy — contenuto non embeddato, la + sorgente si risolve dal ``dataset.yml`` del repo. Il contratto v1 (``location`` per voce, ``signals``, ``entities``, ``stage``) non è più prodotto. ``schema_version: 2``; il parsing di v1 e v2 è @@ -433,22 +435,55 @@ def build_checks(layout: RepoLayout) -> tuple[dict[str, Any], dict[str, list[str # --------------------------------------------------------------------------- +def _support_codelist_ids(layout: RepoLayout) -> list[str]: + """Id delle codelist dichiarate nei ``support:`` dei dataset.yml (issue #471). + + Fonte canonica: ``support:`` con ``type: codelist`` e ``id`` (id SDMX, + es. ``FREQ``, ``UNIT``). Normalizzati a lowercase e deduplicati. + """ + ids: set[str] = set() + for manifest in iter_manifests(layout): + for entry in getattr(manifest.cfg, "support", None) or []: + if not isinstance(entry, dict): + continue + # Un support senza `type` vale "dataset" (core/config.py), quindi + # non va trattato come codelist anche se ha un `id`. + if entry.get("type") != "codelist": + continue + cid = entry.get("id") + if isinstance(cid, str) and cid.strip(): + ids.add(cid.strip().lower()) + return sorted(ids) + + def build_codelists( layout: RepoLayout, codelists_dir: Path | None = None, ) -> tuple[dict[str, Any], dict[str, list[str]]]: """Costruisce la sezione codelists del registry: i NOMI dei codelist. - Legge ``{repo_root}/codelists/*.csv`` (convenzione eurostat: geo.csv, - units.csv, freq.csv, ...) e pubblica solo i nomi come lista. Il contenuto - (le righe) NON è embeddato: è letto dal CSV del repo dal consumer on-demand - (pattern eurostat-mcp). Se la dir non esiste, lista vuota senza errori. + I nomi derivano dai ``support:`` con ``type: codelist`` e ``id`` dei + dataset.yml (fonte canonica, es. id SDMX) e, per compatibilita' con i + repo legacy, anche dai CSV in ``{repo_root}/codelists/*.csv`` (convenzione + eurostat: geo.csv, units.csv, freq.csv, ...). Il contenuto (le righe) NON + e' embeddato: e' letto dal CSV del repo dal consumer on-demand (pattern + eurostat-mcp). Embeddare le righe rendeva il registry enorme + (es. eurostat: codelists = 60% del file) senza che alcun consumer leggesse + i valori dal registry. + + Returns: + Tuple (payload, errors) con errors = {"derive": [], "validation": [...]}. + + Se non ci sono ne' support id ne' dir codelists, ritorna una lista + vuota senza errori (codelists e' opzionale). """ codelists_dir = codelists_dir or (layout.repo_root / "codelists") - if not codelists_dir.is_dir(): - return {"codelists": []}, {"derive": [], "validation": []} - names = sorted(p.stem for p in codelists_dir.glob("*.csv")) - return {"codelists": names}, {"derive": [], "validation": []} + + names = set(_support_codelist_ids(layout)) + if codelists_dir.is_dir(): + names.update(p.stem for p in codelists_dir.glob("*.csv")) + + return {"codelists": sorted(names)}, {"derive": [], "validation": []} # --------------------------------------------------------------------------- diff --git a/toolkit/registry/schemas/README.md b/toolkit/registry/schemas/README.md index e03e140..4632915 100644 --- a/toolkit/registry/schemas/README.md +++ b/toolkit/registry/schemas/README.md @@ -18,7 +18,21 @@ run records + parquet locali): | `datasets` | "cosa esiste e com'è fatto" | slug, columns (role/semantic_type), period, **partitions** (partizione → righe clean), **last_run**, mart_refs | ACB → dcl-mcp, data-explorer, resolver, dashboards | | `marts` | "cosa esiste a livello analitico" | tabelle, **layout** (flat/year/glob), year, **primary_key** | dcl-mcp, data-explorer, `support external` | | `checks` | "cosa non va" | anomalie riproducibili dal config (`warn`/`error`), vuoto = ok | ACB (triage/bootstrap) | -| `codelists` | quali dimensioni hanno un codelist | nomi dei CSV in `codelists/` (contenuto letto on-demand) | agenti MCP | +| `codelists` | quali dimensioni hanno un codelist | nomi: `id` dei `support: type: codelist` + stem dei CSV legacy (contenuto non embeddato) | agenti MCP | + +### Semantica di `codelists` + +La sezione è una lista di **nomi**, non di riferimenti: non dice dove sta il +contenuto. Un nome può arrivare da due posti: + +- `support:` con `type: codelist` e `id` (es. SDMX: `FREQ` → `freq`), che il + toolkit materializza in `out/data/support//.parquet` al run; +- gli stem dei CSV in `codelists/` (repo legacy), che restano nel repo finché + non migrano a un support. + +I due insiemi possono sovrapporsi con nomi diversi per lo stesso codelist +(es. eurostat: `unit`/`units`, `obs_flag`/`flags`). Per risolvere il contenuto +la fonte è il `dataset.yml` del repo, non questo campo. Il contratto di pubblicazione vive nel blocco `gcs`: diff --git a/toolkit/registry/schemas/registry.schema.json b/toolkit/registry/schemas/registry.schema.json index 49d90d8..630da5a 100644 --- a/toolkit/registry/schemas/registry.schema.json +++ b/toolkit/registry/schemas/registry.schema.json @@ -66,7 +66,7 @@ "items": { "type": "string" }, - "description": "Nomi dei codelist del repo (contenuto letto dal CSV in repo, non embeddato)." + "description": "Nomi dei codelist del repo: gli `id` dei support `type: codelist` (es. SDMX) e gli stem dei CSV in `codelists/`. Contenuto non embeddato; la sorgente va risolta dal dataset.yml del repo, non da questo campo." } }, "additionalProperties": true,