no-mistakes-postgres-sql 0.109.128

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::QueryFrame;
use crate::fx::{FxHashMap, FxHashSet};
use crate::idents::{ident_key, implicit_output_label, unwrap_expr};
use sqlparser::ast::{Expr, OrderByKind, Query, SelectItem, SetExpr};

impl QueryFrame {
    pub(super) fn new(query: &Query, scalar: bool, scope_depth: usize) -> Self {
        let mut cte_names = FxHashSet::default();
        let mut pending_ctes = FxHashMap::default();
        if let Some(with) = &query.with {
            for cte in &with.cte_tables {
                let name = ident_key(&cte.alias.name);
                if with.recursive {
                    cte_names.insert(name);
                } else {
                    pending_ctes.insert(std::ptr::from_ref(cte.query.as_ref()), name);
                }
            }
        }
        let sort_names: FxHashSet<_> = query
            .order_by
            .iter()
            .filter_map(|order| match &order.kind {
                OrderByKind::Expressions(expressions) => Some(expressions),
                _ => None,
            })
            .flatten()
            .map(|item| unwrap_expr(&item.expr))
            .filter(|expr| matches!(expr, Expr::Identifier(_)))
            .map(std::ptr::from_ref)
            .collect();
        let mut body = query.body.as_ref();
        if !sort_names.is_empty() {
            while let SetExpr::Query(inner) = body {
                body = inner.body.as_ref();
            }
        }
        let wrapped_outputs = if matches!(query.body.as_ref(), SetExpr::Query(_)) {
            body.as_select()
                .into_iter()
                .flat_map(|select| &select.projection)
                .filter_map(output_label)
                .collect()
        } else {
            FxHashSet::default()
        };
        Self {
            cte_names,
            pending_ctes,
            sort_names,
            set_output_only: matches!(body, SetExpr::SetOperation { .. } | SetExpr::Values(_)),
            wrapped_outputs,
            direct_select: query.body.as_select().map(std::ptr::from_ref),
            wrapped_body: match query.body.as_ref() {
                SetExpr::Query(child) => Some(std::ptr::from_ref(child.as_ref())),
                _ => None,
            },
            scalar,
            scope_depth,
            retained_scope: false,
        }
    }
}

pub(super) fn output_label(item: &SelectItem) -> Option<String> {
    match item {
        SelectItem::ExprWithAlias { alias, .. } => Some(ident_key(alias)),
        SelectItem::UnnamedExpr(expr) => implicit_output_label(expr),
        _ => None,
    }
}

pub(super) fn mark_scalar_body(body: &SetExpr, scalar_queries: &mut FxHashSet<*const Query>) {
    let mut bodies = vec![body];
    while let Some(body) = bodies.pop() {
        match body {
            SetExpr::Query(query) => {
                scalar_queries.insert(std::ptr::from_ref(query.as_ref()));
            }
            SetExpr::SetOperation { left, right, .. } => {
                bodies.push(left);
                bodies.push(right);
            }
            _ => {}
        }
    }
}