Excessibility.SQLFingerprint (Excessibility v0.19.0)

Copy Markdown View Source

Normalizes SQL into a stable, value-free shape and derives a fingerprint.

Postgres-oriented (the Ecto reference adapter). Ecto emits parameterized SQL ($1, $2 …) with bind values already separated, so normalization only has to canonicalize whitespace/case, fold IN (...) arity, and scrub the few inline literals that appear in fragments. The fingerprint is derived from the normalized string, so grouping (N+1, compare) depends on this being correct — see the leak-guard tests.

Summary

Functions

Fingerprint SQL as "sha256:<16 hex>".

Normalize SQL to a stable, value-free string. The generic folds here are dialect-agnostic; any dialect-specific normalization is applied last via Excessibility.Dialect.normalize_extras/1 (no-op for Postgres today).

Functions

fingerprint(sql)

Fingerprint SQL as "sha256:<16 hex>".

normalize(sql)

Normalize SQL to a stable, value-free string. The generic folds here are dialect-agnostic; any dialect-specific normalization is applied last via Excessibility.Dialect.normalize_extras/1 (no-op for Postgres today).

The literal-aware scan/3 runs first, on the original-case SQL: Postgres dollar-quote tags ($TAG$) and E-string escapes are case-sensitive, so lowercasing before scanning could turn look-alike text inside a literal into a false closing delimiter and leak literal contents (see #166). The scan folds every string / dollar-quoted / E-string literal to ? and strips comments, so only value-free tokens remain; downcasing the scanned result is then safe.