tinyetl 0.12.0

Fast, zero-config ETL in a single binary for transforming data between formats and databases
"""Compare an exported CSV with the original, value by value.

Usage: compare.py [--ignore-header-case] original.csv exported.csv [key_column]
Numbers are compared numerically, booleans by truth value, ISO dates and timestamps as instants
(a date is midnight UTC), and empty means NULL.
Column order must match exactly; with --ignore-header-case, column names may differ in case
(Snowflake upper-cases unquoted names). Prints the differences and exits 1 if there are any.
"""
import csv
import sys
from datetime import datetime, timezone
from decimal import Decimal, InvalidOperation

TRUE = {"1", "true", "t", "yes"}
FALSE = {"0", "false", "f", "no"}


def norm(value):
    v = value.strip()
    if v == "":
        return None
    low = v.lower()
    if low in TRUE:
        return True
    if low in FALSE:
        return False
    try:
        return Decimal(v).normalize()
    except InvalidOperation:
        pass
    try:
        dt = datetime.fromisoformat(v)
        return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)
    except ValueError:
        return value


def load(path):
    with open(path, newline="", encoding="utf-8") as f:
        reader = csv.reader(f)
        header = next(reader)
        return header, [row for row in reader]


args = [a for a in sys.argv[1:] if not a.startswith("--")]
ignore_case = "--ignore-header-case" in sys.argv[1:]
orig_path, out_path = args[0], args[1]
key = args[2] if len(args) > 2 else None
oh, orows = load(orig_path)
eh, erows = load(out_path)
if ignore_case:
    oh = [h.lower() for h in oh]
    eh = [h.lower() for h in eh]
    key = key.lower() if key else None
problems = []
if oh != eh:
    problems.append(f"header differs:\n  expected {oh}\n  got      {eh}")
if len(orows) != len(erows):
    problems.append(f"row count differs: expected {len(orows)}, got {len(erows)}")
if key:
    k = oh.index(key)
    orows.sort(key=lambda r: norm(r[k]))
    erows.sort(key=lambda r: norm(r[eh.index(key)] if key in eh else r[k]))
for i, (a, b) in enumerate(zip(orows, erows)):
    for col, name in enumerate(oh):
        if name not in eh:
            continue
        va, vb = norm(a[col]), norm(b[eh.index(name)])
        if va != vb:
            sa, sb = repr(a[col])[:60], repr(b[eh.index(name)])[:60]
            problems.append(f"row {i + 1} column {name}: expected {sa}, got {sb}")
if problems:
    print("\n".join(problems))
    sys.exit(1)
print(f"OK: {len(orows)} rows, {len(oh)} columns match")