no-mistakes-postgres-sql 0.109.131

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
//! Adapt PL/pgSQL variable targets only in a parser clone of an identified body.
use sqlparser::tokenizer::{Token, TokenWithSpan};

pub(super) fn normalize(tokens: &mut Vec<TokenWithSpan>) -> bool {
    let code: Vec<_> = tokens
        .iter()
        .enumerate()
        .filter(|(_, token)| !matches!(token.token, Token::Whitespace(_)))
        .collect();
    if !code
        .first()
        .is_some_and(|(_, token)| word(&token.token, "SELECT") || word(&token.token, "WITH"))
    {
        return true;
    }
    let mut depth = 0usize;
    let mut dml = false;
    let Some(at) = code.iter().position(|(_, token)| {
        match token.token {
            Token::LParen | Token::LBracket => depth += 1,
            Token::RParen | Token::RBracket => depth = depth.saturating_sub(1),
            _ => {}
        }
        dml |= depth == 0
            && ["INSERT", "UPDATE", "DELETE", "MERGE"]
                .iter()
                .any(|name| word(&token.token, name));
        depth == 0 && word(&token.token, "INTO")
    }) else {
        return true;
    };
    // WITH may carry DML (including INSERT ... INTO), not a SELECT variable target.
    if dml {
        return true;
    }
    let mut start = at + 1;
    if code
        .get(start)
        .is_some_and(|(_, token)| word(&token.token, "STRICT"))
    {
        start += 1;
    }
    let mut depth = 0usize;
    let end = code
        .iter()
        .enumerate()
        .skip(start)
        .find_map(|(index, (_, token))| {
            match token.token {
                Token::LParen | Token::LBracket => depth += 1,
                Token::RParen | Token::RBracket => depth = depth.saturating_sub(1),
                _ => {}
            }
            (depth == 0
                && [
                    "FROM",
                    "WHERE",
                    "GROUP",
                    "HAVING",
                    "WINDOW",
                    "ORDER",
                    "LIMIT",
                    "OFFSET",
                    "FETCH",
                    "FOR",
                    "UNION",
                    "INTERSECT",
                    "EXCEPT",
                ]
                .iter()
                .any(|name| word(&token.token, name)))
            .then_some(index)
        })
        .unwrap_or(code.len());
    let targets: Vec<_> = code[start..end]
        .iter()
        .map(|(_, token)| &token.token)
        .collect();
    if !super::procedural::assignment_parts(&targets)
        .is_some_and(|parts| parts.into_iter().all(super::procedural::assignment_target))
    {
        return false;
    }
    // Native SelectInto retains every target but has no STRICT field. Only
    // remove STRICT from this clone; source tokens and target spans stay intact.
    if start > at + 1 {
        tokens.remove(code[at + 1].0);
    }
    true
}

fn word(token: &Token, name: &str) -> bool {
    matches!(token, Token::Word(word) if word.quote_style.is_none() && word.value.eq_ignore_ascii_case(name))
}

#[cfg(all(test, any(nm_shard_all, nm_shard_0)))]
mod tests;