fix(core): restore adapter command behavior

This commit is contained in:
2026-07-11 21:04:53 +02:00
parent a35efa16de
commit 45d57756aa
7 changed files with 141 additions and 29 deletions
+27 -24
View File
@@ -12,6 +12,30 @@ def _lsh_dir(cfg) -> Path:
return cfg.paths.indexes / "lsh"
def _extract_lsh_values(dwh, physical, annotations, limit):
from tht.db.sampling import SkippedColumn, TruncatedColumn, is_text_type
from tht.mschema.eligibility import effective_eligibility
values, skipped, truncated = {}, [], []
for table_name, table in physical.tables.items():
table_ann = annotations.tables.get(table_name)
for column_name, column in table.columns.items():
ann_col = table_ann.columns.get(column_name) if table_ann else None
if not is_text_type(column.type) or not effective_eligibility(column, ann_col)[0]:
continue
try:
distinct = dwh.distinct_values(table_name, column_name, limit=limit)
except Exception as exc:
skipped.append(SkippedColumn(table_name, column_name, f"errore: {exc}"))
continue
vals = [str(value) for value in distinct.values if value not in (None, "")]
if vals:
values.setdefault(table_name, {})[column_name] = vals
if distinct.truncated:
truncated.append(TruncatedColumn(table_name, column_name, len(vals)))
return values, skipped, truncated
@lsh_app.command("build")
def build_cmd(config: Path = CONFIG_OPT) -> None:
"""Costruisce l'indice LSH dai valori del database e lo salva su pickle."""
@@ -33,31 +57,10 @@ def build_cmd(config: Path = CONFIG_OPT) -> None:
typer.echo("Estrazione valori (i più frequenti) dalle colonne testuali eligible...")
from tht.adapters.factory import build_dwh
from tht.db.sampling import SkippedColumn, TruncatedColumn, is_text_type
from tht.mschema.eligibility import effective_eligibility
dwh = build_dwh(cfg)
values: dict[str, dict[str, list[str]]] = {}
skipped: list[SkippedColumn] = []
truncated: list[TruncatedColumn] = []
for table_name, table in physical.tables.items():
table_ann = annotations.tables.get(table_name)
for column_name, column in table.columns.items():
ann_col = table_ann.columns.get(column_name) if table_ann else None
if not is_text_type(column.type) or not effective_eligibility(column, ann_col)[0]:
continue
try:
distinct = dwh.distinct_values(
table_name, column_name, limit=cfg.lsh.max_values_per_column
)
except Exception as e:
skipped.append(SkippedColumn(table_name, column_name, f"errore: {e}"))
continue
vals = [str(value) for value in distinct.values if value not in (None, "")]
if vals:
values.setdefault(table_name, {})[column_name] = vals
if distinct.truncated or len(vals) >= cfg.lsh.max_values_per_column:
truncated.append(TruncatedColumn(table_name, column_name, len(vals)))
values, skipped, truncated = _extract_lsh_values(
dwh, physical, annotations, cfg.lsh.max_values_per_column
)
n_values = sum(len(v) for t in values.values() for v in t.values())
typer.echo(f" {n_values} valori da {sum(len(t) for t in values.values())} colonne")
for s in skipped: