no-mistakes-postgres-sql 0.109.129

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use super::super::{pins::Resolver, Scope};
use crate::idents::unwrap_expr;
use crate::statements::value::{is_placeholder_ident_at, PlaceholderPositions};
use crate::statements::{SqlBoundItem, SqlBoundItemKind};
use sqlparser::ast::{BinaryOperator, Distinct, Expr, GroupByExpr, Select};

pub(super) fn fixed(
    select: &Select,
    items: &[SqlBoundItem],
    scope: &Scope,
    positions: PlaceholderPositions<'_>,
) -> bool {
    let Some(Distinct::On(expressions)) = &select.distinct else {
        return false;
    };
    let Some(selection) = select.selection.as_ref() else {
        return false;
    };
    if !matches!(&select.group_by, GroupByExpr::Expressions(expressions, modifiers)
        if expressions.is_empty() && modifiers.is_empty())
        || super::super::aggregate::projection_may_erase_input(select, scope)
        || has_opaque_row_inputs(items)
    {
        return false;
    }
    let resolver = Resolver::new(items, scope.names(), positions);
    !expressions.is_empty()
        && expressions.iter().all(|expr| {
            matches!(unwrap_expr(expr), Expr::CompoundIdentifier(_))
                && resolver
                    .column(expr)
                    .is_some_and(|column| fixed_by_where(selection, &column, &resolver, positions))
        })
}

fn has_opaque_row_inputs(items: &[SqlBoundItem]) -> bool {
    items.iter().any(|item| match &item.kind {
        SqlBoundItemKind::Opaque => true,
        SqlBoundItemKind::Query(query) => query.has_opaque_row_inputs(),
        _ => false,
    })
}

fn fixed_by_where(
    expr: &Expr,
    column: &(usize, String),
    resolver: &Resolver,
    positions: PlaceholderPositions<'_>,
) -> bool {
    match unwrap_expr(expr) {
        Expr::BinaryOp {
            left,
            op: BinaryOperator::And,
            right,
        } => {
            fixed_by_where(left, column, resolver, positions)
                || fixed_by_where(right, column, resolver, positions)
        }
        Expr::BinaryOp {
            left,
            op: BinaryOperator::Eq,
            right,
        } => {
            (resolver.column(left).as_ref() == Some(column) && scalar_constant(right, positions))
                || (resolver.column(right).as_ref() == Some(column)
                    && scalar_constant(left, positions))
        }
        _ => false,
    }
}

fn scalar_constant(expr: &Expr, positions: PlaceholderPositions<'_>) -> bool {
    match unwrap_expr(expr) {
        Expr::Value(_) => true,
        Expr::Identifier(ident) => is_placeholder_ident_at(ident, positions),
        _ => false,
    }
}