Skip to main content

Module normalizer

Module normalizer 

Source
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:

Output is one String per parsed statement, formatted by sqlparser’s Display after the rewrite.

Structs§

Normalizer
VisitorMut impl that performs the normalization rewrite. Most callers go through normalize / normalize_with_options or Normalizer::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.
NormalizerOptions
Toggles for normalize_with_options. Defaults to all false (placeholder substitution only).

Functions§

normalize
Parse sql under dialect and normalize each statement with default options (literal-to-? placeholder substitution only).
normalize_with_options
Parse sql under dialect and normalize each statement, applying any extra collapses enabled in options.