sqlparser-canonicalize
sqlparser-canonicalize produces canonical predicate text and stable hashes from sqlparser syntax trees. Equivalent predicate spellings produce identical bytes for durable deduplication.
use PostgreSqlDialect;
use ;
let canonicalizer = new;
let normalized = canonicalizer.normalize_sql?;
assert_eq!;
// The same predicate written the other way round produces the same bytes, and so the same key.
let commuted = canonicalizer.normalize_sql?;
assert_eq!;
# Ok::
Canonical text, and so every hash, is stable within a minor version. A new minor version may spell some predicates differently, so keys stored under one minor version are rebuilt after upgrading to the next. A predicate the crate cannot spell in one way that reads back as the same predicate is refused, never keyed.
The dialect belongs to the canonicalizer because it decides the answer: whether "Status" and Status are one column or two is a PostgreSQL question, not a SQL one.
Normalization is O(n log n) for AND and OR chains because operands are sorted, and O(n) for other syntax tree shapes.
The crate works without the standard library if you turn off default features, at the cost of the parser's stack protection, so that configuration can be crashed by deeply nested input and should only take SQL you trust.