Skip to main content

no_mistakes_postgres_sql/
predicate_normalization.rs

1//! Stable predicate comparison preserves literal and quoted identifier contents.
2use sqlparser::dialect::PostgreSqlDialect;
3use sqlparser::tokenizer::Token;
4
5#[doc(hidden)]
6pub fn normalize(text: &str) -> String {
7    let Ok(tokens) =
8        crate::parse::operator_boundary::tokenize_with_unescape(&PostgreSqlDialect {}, text, false)
9    else {
10        return text.to_string();
11    };
12    tokens
13        .into_iter()
14        .map(|token| match token {
15            Token::Whitespace(_) => " ".to_string(),
16            Token::Word(mut word) if word.quote_style.is_none() => {
17                word.value = word.value.to_ascii_lowercase();
18                Token::Word(word).to_string()
19            }
20            // Escaped literals are decoded even when tokenizer unescaping is disabled.
21            // AST value rendering restores their delimiters and backslashes safely.
22            Token::EscapedStringLiteral(value) => {
23                sqlparser::ast::Value::EscapedStringLiteral(value).to_string()
24            }
25            // Unicode escapes are decoded by the tokenizer too. Re-render the AST value so a
26            // decoded quote cannot erase the original literal boundary.
27            Token::UnicodeStringLiteral(value) => {
28                sqlparser::ast::Value::UnicodeStringLiteral(value).to_string()
29            }
30            // Dollar-quote tags delimit a value but are not part of that value. Fold only the
31            // tag spelling; the body remains case-sensitive.
32            Token::DollarQuotedString(mut value) => {
33                if let Some(tag) = &mut value.tag {
34                    tag.make_ascii_lowercase();
35                }
36                Token::DollarQuotedString(value).to_string()
37            }
38            // PostgreSQL hexadecimal digits are case-insensitive; ordinary string bodies are
39            // intentionally left untouched.
40            Token::HexStringLiteral(value) => {
41                Token::HexStringLiteral(value.to_ascii_lowercase()).to_string()
42            }
43            // Numeric exponent markers are case-insensitive, but other token contents should
44            // retain their original spelling.
45            Token::Number(mut value, long) => {
46                if let Some(index) = value.find('E') {
47                    value.replace_range(index..index + 1, "e");
48                }
49                Token::Number(value, long).to_string()
50            }
51            other => other.to_string(),
52        })
53        .fold(String::new(), |mut normalized, token| {
54            // Collapse only whitespace tokens: literal/identifier contents retain their spacing.
55            if token != " " || !normalized.ends_with(' ') {
56                normalized.push_str(&token);
57            }
58            normalized
59        })
60        .trim()
61        .to_string()
62}
63
64#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
65mod tests;