no-mistakes-postgres-sql 0.109.124

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
//! Syntactic calls shared by SQL policies; names and locations retain SQL identity.
use super::idents::ident_key;
use sqlparser::ast::{
    Expr, ObjectName, OrderBy, Query, Select, Spanned, Statement, TableFactor, Visit, Visitor,
};
use std::ops::ControlFlow;

mod clause;
mod clauses;
pub use clause::SqlFunctionClause;

/// One function expression, excluding column and alias identifiers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlFunctionCallFact {
    pub name_parts: Vec<String>,
    pub line: usize,
    pub column: usize,
    /// None for calls outside the supported clauses, including FROM and GROUP BY.
    pub clause: Option<SqlFunctionClause>,
}

pub(super) fn collect(statement: &Statement, out: &mut Vec<SqlFunctionCallFact>) {
    collect_projected(statement, out, false);
}

/// Project calls from statements that actually execute, while retaining the
/// syntactic collector for callers that inspect the original SQL expression.
pub(super) fn collect_executed(statement: &Statement, out: &mut Vec<SqlFunctionCallFact>) {
    let mut executed = Vec::new();
    super::statements::walk_executed(statement, &mut executed);
    for statement in executed {
        collect(statement, out);
    }
}

pub(super) fn collect_projected(
    statement: &Statement,
    out: &mut Vec<SqlFunctionCallFact>,
    synthetic_select: bool,
) {
    let _ = statement.visit(&mut Calls {
        out,
        roots: clauses::Roots::default(),
        current: None,
        contexts: Vec::new(),
        synthetic_select,
    });
}

/// Visit an expression owned by parser recovery using its actual clause.
pub(super) fn collect_expression(
    expression: &Expr,
    out: &mut Vec<SqlFunctionCallFact>,
    clause: Option<SqlFunctionClause>,
) {
    let _ = expression.visit(&mut Calls {
        out,
        roots: clauses::Roots::default(),
        current: clause,
        contexts: Vec::new(),
        synthetic_select: false,
    });
}

struct Calls<'a> {
    out: &'a mut Vec<SqlFunctionCallFact>,
    roots: clauses::Roots,
    current: Option<SqlFunctionClause>,
    contexts: Vec<Option<SqlFunctionClause>>,
    synthetic_select: bool,
}

impl Calls<'_> {
    fn enter(&mut self, clause: Option<SqlFunctionClause>) {
        self.contexts.push(self.current);
        self.current = clause;
    }

    fn leave(&mut self) {
        self.current = self.contexts.pop().expect("balanced SQL visitor context");
    }
}

impl Visitor for Calls<'_> {
    type Break = ();

    fn pre_visit_statement(&mut self, statement: &Statement) -> ControlFlow<()> {
        self.enter(None);
        self.roots.statement(statement);
        ControlFlow::Continue(())
    }

    fn post_visit_statement(&mut self, _statement: &Statement) -> ControlFlow<()> {
        self.leave();
        ControlFlow::Continue(())
    }

    fn pre_visit_query(&mut self, query: &Query) -> ControlFlow<()> {
        // A subquery owns its clauses even when it occurs inside an outer expression.
        self.enter(None);
        self.roots.query(query);
        ControlFlow::Continue(())
    }

    fn post_visit_query(&mut self, _query: &Query) -> ControlFlow<()> {
        self.leave();
        ControlFlow::Continue(())
    }

    fn pre_visit_select(&mut self, select: &Select) -> ControlFlow<()> {
        self.roots.select(select, !self.synthetic_select);
        self.synthetic_select = false;
        ControlFlow::Continue(())
    }

    fn pre_visit_order_by(&mut self, ordering: &OrderBy) -> ControlFlow<()> {
        self.roots.order_by(ordering);
        ControlFlow::Continue(())
    }

    fn pre_visit_expr(&mut self, expression: &Expr) -> ControlFlow<()> {
        self.enter(self.roots.clause(expression).unwrap_or(self.current));
        if let Expr::Function(function) = expression {
            self.roots.function(function);
            record(&function.name, self.current, self.out);
        }
        ControlFlow::Continue(())
    }

    fn post_visit_expr(&mut self, _expression: &Expr) -> ControlFlow<()> {
        self.leave();
        ControlFlow::Continue(())
    }

    fn pre_visit_table_factor(&mut self, table: &TableFactor) -> ControlFlow<()> {
        match table {
            TableFactor::Table {
                name,
                args: Some(_),
                ..
            }
            | TableFactor::Function { name, .. } => record(name, self.current, self.out),
            _ => {}
        }
        ControlFlow::Continue(())
    }
}

fn record(
    name: &ObjectName,
    clause: Option<SqlFunctionClause>,
    out: &mut Vec<SqlFunctionCallFact>,
) {
    let parts: Option<Vec<_>> = name
        .0
        .iter()
        .map(|part| part.as_ident().map(ident_key))
        .collect();
    if let Some(name_parts) = parts {
        let start = name.span().start;
        out.push(SqlFunctionCallFact {
            name_parts,
            line: start.line.max(1) as usize,
            column: start.column.max(1) as usize,
            clause,
        });
    }
}

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

#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod clause_tests;

#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod join_tests;

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

#[cfg(all(test, any(nm_shard_all, nm_shard_1)))]
mod mutation_tests;

#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod execution_tests;