feldera-sqllib 0.331.0

SQL runtime library for Feldera
//! Boolean operations

use crate::some_function1;

#[doc(hidden)]
#[inline(always)]
pub fn wrap_bool(b: Option<bool>) -> bool {
    b.unwrap_or_default()
}

#[doc(hidden)]
#[inline(always)]
pub fn or_b_b<F>(left: bool, right: F) -> bool
where
    F: Fn() -> bool,
{
    left || right()
}

#[doc(hidden)]
#[inline(always)]
pub fn or_bN_b<F>(left: Option<bool>, right: F) -> Option<bool>
where
    F: Fn() -> bool,
{
    match left {
        Some(l) => Some(l || right()),
        None => match right() {
            true => Some(true),
            _ => None,
        },
    }
}

#[doc(hidden)]
#[inline(always)]
pub fn or_b_bN<F>(left: bool, right: F) -> Option<bool>
where
    F: Fn() -> Option<bool>,
{
    match left {
        false => right(),
        true => Some(true),
    }
}

#[doc(hidden)]
#[inline(always)]
pub fn or_bN_bN<F>(left: Option<bool>, right: F) -> Option<bool>
where
    F: Fn() -> Option<bool>,
{
    match left {
        None => match right() {
            Some(true) => Some(true),
            _ => None,
        },
        Some(false) => right(),
        Some(true) => Some(true),
    }
}

// OR and AND are special, they can't be generated by rules

#[doc(hidden)]
#[inline(always)]
pub fn and_b_b<F>(left: bool, right: F) -> bool
where
    F: Fn() -> bool,
{
    left && right()
}

#[doc(hidden)]
#[inline(always)]
pub fn and_bN_b<F>(left: Option<bool>, right: F) -> Option<bool>
where
    F: Fn() -> bool,
{
    match left {
        Some(false) => Some(false),
        Some(true) => Some(right()),
        None => match right() {
            false => Some(false),
            _ => None,
        },
    }
}

#[doc(hidden)]
#[inline(always)]
pub fn and_b_bN<F>(left: bool, right: F) -> Option<bool>
where
    F: Fn() -> Option<bool>,
{
    match left {
        false => Some(false),
        true => right(),
    }
}

#[doc(hidden)]
#[inline(always)]
pub fn and_bN_bN<F>(left: Option<bool>, right: F) -> Option<bool>
where
    F: Fn() -> Option<bool>,
{
    match left {
        Some(false) => Some(false),
        Some(true) => right(),
        None => match right() {
            Some(false) => Some(false),
            _ => None,
        },
    }
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_true_b_(left: bool) -> bool {
    left
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_true_bN_(left: Option<bool>) -> bool {
    matches!(left, Some(true))
}

#[doc(hidden)]
#[inline(always)]
pub fn is_false_b_(left: bool) -> bool {
    !left
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_false_bN_(left: Option<bool>) -> bool {
    matches!(left, Some(false))
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_not_true_b_(left: bool) -> bool {
    !left
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_not_true_bN_(left: Option<bool>) -> bool {
    match left {
        Some(true) => false,
        Some(false) => true,
        _ => true,
    }
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_not_false_b_(left: bool) -> bool {
    left
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_not_false_bN_(left: Option<bool>) -> bool {
    match left {
        Some(true) => true,
        Some(false) => false,
        _ => true,
    }
}

#[doc(hidden)]
#[inline(always)]
pub const fn bool_to_i8_(value: bool) -> i8 {
    if value { 1 } else { 0 }
}

some_function1!(bool_to_i8, bool, i8);

#[doc(hidden)]
#[inline(always)]
pub const fn i8_to_bool_(value: i8) -> bool {
    value != 0
}

some_function1!(i8_to_bool, i8, bool);