feat(sql): standard DuckDB macros per il layer CLEAN#416
Merged
Conversation
Aggiunge 7 macro SQL automaticamente caricate in ogni esecuzione clean: - normalize_italian_number(val) — 1.234,56 → 1234.56 - normalize_italian_integer(val) — 1.234 → 1234 - decode_flag(val, yes_value) — 'X' → TRUE - normalize_string(val) — TRIM + '' → NULL - cast_int(val) — TRY_CAST(val AS INTEGER) - cast_double(val) — TRY_CAST(val AS DOUBLE) - remove_dot_thousands(val) — 1.234 → 1234.0 Meccanismo: - toolkit/sql/macros.sql: definizioni CREATE OR REPLACE MACRO - clean/sql_execute.py:_load_standard_macros() — caricate all'inizio di ogni connessione DuckDB nel layer CLEAN - scaffold/clean.py: commento nel generated SQL che elenca le macro Test: 35 test DuckDB per tutte le macro + test di integrazione
…housands - pyproject.toml: aggiunto [tool.setuptools.package-data] con toolkit = ['sql/*.sql'] — le macro ora sono incluse nel wheel - sql_execute.py: _load_standard_macros ora solleva FileNotFoundError se macros.sql non esiste (nessun skip silenzioso) - macros.sql: normalize_italian_integer documenta che DuckDB CAST(DOUBLE AS INTEGER) arrotonda (non tronca) - macros.sql: remove_dot_thousands documenta precondizione 'solo interi' con warning esplicito - Test: 3 test nuovi (package data source + importlib + normalize_italian_integer .90 rounding) - Test: remove_dot_thousands now documents the '1234.56 → 123456' mangling as expected behavior, not a bug - 460 pure_unit test pass
Convertiti 5 dataset canonici a usare le macro standard del toolkit: - smoke/bdap_ckan_csv: TRY_CAST+TRIM → cast_int/cast_double/normalize_string - smoke/bdap_http_csv: 21 righe di TRY_CAST(TRIM(CAST(... AS VARCHAR)) AS DOUBLE) → cast_double (da 33 a 25 righe) - smoke/finanze_http_zip_2023: TRY_CAST → cast_int/cast_bigint + normalize_string - smoke/local_file_csv: CAST → cast_int/cast_double - project-example: normalize_string + normalize_italian_number al posto di TRIM/REPLACE/REPLACE/NULLIF manuali Questi dataset sono eseguiti negli smoke test CI — provano che le macro funzionano end-to-end in pipeline reali.
- docs/standard-macros.md: documentazione completa di tutte le 8 macro con esempi prima/dopo, tabella comparativa con normalize.py, e comandi per test diretto via DuckDB CLI - README.md: nuova sezione 'Scrivere clean.sql con le macro standard' sotto la CLI, con esempio concreto prima/dopo - README.md: docs/standard-macros.md aggiunto alla tabella documenti
Il dry-run CLI validava il clean.sql senza caricare le macro DuckDB standard, causando 'Catalog Error: Scalar Function cast_int does not exist' sui dataset che usano le macro (project-example, smoke test). Aggiunta chiamata a _load_standard_macros() in validate_sql_dry_run().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sintesi
7 macro DuckDB caricate automaticamente in ogni esecuzione clean.sql, per eliminare il boilerplate SQL che oggi è riscritto da zero in ogni dataset di dataset-incubator (TRY_CAST, REPLACE numeri italiani, CASE decode flag, TRIM).
Contesto collegato
Segue la discussione architetturale: toolkit può snellire DI? I pattern comuni nei clean.sql sono stati analizzati e le macro coprono l'80% del boilerplate.
Cosa cambia
Impatto su contratti pubblici
Nessuno. Le macro sono additive — un clean.sql che non le usa funziona identico.
Cosa contiene
toolkit/sql/macros.sql— 7 macro DuckDBnormalize_italian_number(val)1.234,56→1234.56normalize_italian_integer(val)1.234→1234decode_flag(val, 'X')'X'→TRUEnormalize_string(val)TRIM+''→NULLcast_int(val)TRY_CAST(val AS INTEGER)cast_double(val)TRY_CAST(val AS DOUBLE)remove_dot_thousands(val)1.234→1234.0toolkit/clean/sql_execute.py— caricamento automatico_load_standard_macros(con, logger)chiamata all'inizio di ogni_run_sql(), prima della vistaraw_input. I file macro sono dentro il pacchetto toolkit (toolkit/sql/macros.sql), raggiungibili da qualsiasi dataset.toolkit/scaffold/clean.py— documentazione nello scaffoldIl generated SQL ora include un commento con l'elenco delle macro disponibili.
Test:
tests/test_macros_sql.py— 35 test pure_unitEsempio di impatto su un clean.sql reale
Prima (ade-cinque-per-mille):
Dopo:
Verifica
pytest tests/test_macros_sql.pypassa (35 test)pytest -m pure_unitpassa (457 test)ruff check .passapure_unitChecklist PR
Note per chi revisiona
_load_standard_macros()ignora silenziosamente semacros.sqlnon esiste (sicura in sviluppo)remove_dot_thousandsè intenzionalmente solo per interi — numeri con virgola decimale vanno connormalize_italian_number