"""Compara impl1 (DuckDB, unified) con impl2 (ATOM) por entidad: k, I, m.""" import json, pyarrow.parquet as pq a = {r['organo_nif']: r for r in pq.read_table('/dc/interno/enc_entidad.parquet').to_pylist()} b = json.load(open('/dc/interno/enc_impl2_entidad.json')) dif = [] for o in sorted(set(a) | set(b)): x, y = a.get(o), b.get(o) if not x or not y or x['k'] != y['k'] or abs(x['i'] - y['i']) > 1 or x['m'] != y['m']: dif.append((o, x and (x['k'], round(x['i'], 2), x['m']), y and (y['k'], y['i'], y['m']))) R = {'entidades_impl1': len(a), 'entidades_impl2': len(b), 'diferencias': len(dif), 'ejemplos': dif[:20], 'coinciden_pct': round(100 * (1 - len(dif) / max(len(set(a) | set(b)), 1)), 3)} json.dump(R, open('/dc/salidas/enc_cmp_impl.json', 'w'), indent=1, default=str) print(json.dumps(R, indent=1, default=str))