no-mistakes-postgres-sql 0.109.129

PostgreSQL SQL parsing, statement facts, and identifier extraction for no-mistakes
Documentation
use crate::idents::{ident_key, unwrap_expr};
use sqlparser::ast::{
    BinaryOperator, DataType, Expr, FunctionArg, FunctionArgExpr, FunctionArguments,
    ObjectNamePart::Identifier, UnaryOperator, Value,
};

pub(super) fn count_boolean(function: &sqlparser::ast::Function) -> bool {
    let FunctionArguments::List(arguments) = &function.args else {
        return false;
    };
    if arguments.duplicate_treatment == Some(sqlparser::ast::DuplicateTreatment::Distinct) {
        return false;
    }
    let builtin = match function.name.0.as_slice() {
        [Identifier(name)] => ident_key(name) == "count",
        [Identifier(schema), Identifier(name)] => {
            ident_key(schema) == "pg_catalog" && ident_key(name) == "count"
        }
        _ => false,
    };
    builtin
        && matches!(&arguments.args[..], [FunctionArg::Unnamed(FunctionArgExpr::Expr(expr))]
        if is_boolean(expr))
}

fn is_boolean(expr: &Expr) -> bool {
    match unwrap_expr(expr) {
        Expr::Value(value) => matches!(value.value, Value::Boolean(_)),
        Expr::TypedString(literal) => {
            matches!(literal.data_type, DataType::Boolean | DataType::Bool)
        }
        Expr::Cast {
            data_type: DataType::Boolean | DataType::Bool,
            ..
        }
        | Expr::UnaryOp {
            op: UnaryOperator::Not,
            ..
        }
        | Expr::IsNull(_)
        | Expr::IsNotNull(_)
        | Expr::IsTrue(_)
        | Expr::IsFalse(_)
        | Expr::IsNotTrue(_)
        | Expr::IsNotFalse(_)
        | Expr::IsJson { .. }
        | Expr::IsNormalized { .. }
        | Expr::IsUnknown(_)
        | Expr::IsNotUnknown(_)
        | Expr::AnyOp { .. }
        | Expr::AllOp { .. }
        | Expr::IsDistinctFrom(_, _)
        | Expr::IsNotDistinctFrom(_, _)
        | Expr::Between { .. }
        | Expr::In { .. }
        | Expr::Exists { .. }
        | Expr::Like { .. }
        | Expr::ILike { .. }
        | Expr::SimilarTo { .. } => true,
        Expr::BinaryOp { op, .. } => matches!(
            op,
            BinaryOperator::Eq
                | BinaryOperator::NotEq
                | BinaryOperator::Lt
                | BinaryOperator::LtEq
                | BinaryOperator::Gt
                | BinaryOperator::GtEq
                | BinaryOperator::AtArrow
                | BinaryOperator::PGOverlap
                | BinaryOperator::ArrowAt
                | BinaryOperator::Question
                | BinaryOperator::QuestionAnd
                | BinaryOperator::QuestionPipe
                | BinaryOperator::And
                | BinaryOperator::Or
                | BinaryOperator::PGRegexMatch
                | BinaryOperator::PGRegexIMatch
                | BinaryOperator::PGRegexNotMatch
                | BinaryOperator::PGRegexNotIMatch
                | BinaryOperator::PGLikeMatch
                | BinaryOperator::PGILikeMatch
                | BinaryOperator::PGNotLikeMatch
                | BinaryOperator::PGNotILikeMatch
        ),
        _ => false,
    }
}