Expand description
SQL normalization — rewrite the AST so structurally identical
queries hash to the same string. See normalize as the entry
point.
The base pass replaces every literal Value with a ?
placeholder, so queries that differ only in their parameter
values collapse to the same string.
“Every literal” is meant literally: it includes Values in
structurally significant positions, not just bound-parameter slots.
A JSON path (JSON_TABLE(data, '$.a'), JSON_EXTRACT(data, '$.a')),
a CAST(x AS DATE FORMAT 'YYYY-MM-DD') format string, the
TABLESAMPLE (BUCKET 3 OUT OF 10) / (10 PERCENT) counts, and
LIMIT / OFFSET are all rewritten to ?. So two queries differing
only in such a literal — e.g. selecting a different JSON field or
sampling a different bucket — collapse to the same normalized string.
Three opt-in toggles (NormalizerOptions) further collapse
repetitive shapes:
unify_in_list:IN (1, 2, 3)→IN (...).unify_values:VALUES (1, 2, 3), (4, 5, 6)→VALUES (...).alphabetize_insert_columns:INSERT INTO t (c, b, a) VALUES (...)→INSERT INTO t (a, b, c) VALUES (...), only when VALUES is unified.
Output is one String per parsed statement, formatted by
sqlparser’s Display after the rewrite.
Structs§
- Normalizer
VisitorMutimpl that performs the normalization rewrite. Most callers go throughnormalize/normalize_with_optionsorNormalizer::normalize(which constructs and drives this visitor internally). Use the struct directly only when you want to integrate the rewrite into a larger AST traversal.- Normalizer
Options - Toggles for
normalize_with_options. Defaults to allfalse(placeholder substitution only).
Functions§
- normalize
- Parse
sqlunderdialectand normalize each statement with default options (literal-to-?placeholder substitution only). - normalize_
with_ options - Parse
sqlunderdialectand normalize each statement, applying any extra collapses enabled inoptions.