vortex_array/compute/
boolean.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use vortex_error::{vortex_bail, VortexResult};

use crate::array::BoolArray;
use crate::{ArrayDType, ArrayData, IntoArrayVariant};
pub trait AndFn {
    /// Point-wise logical _and_ between two Boolean arrays.
    ///
    /// This method uses Arrow-style null propagation rather than the Kleene logic semantics.
    ///
    /// # Examples
    ///
    /// ```
    /// use vortex_array::ArrayData;
    /// use vortex_array::compute::and;
    /// use vortex_array::IntoCanonical;
    /// use vortex_array::accessor::ArrayAccessor;
    /// let a = ArrayData::from(vec![Some(true), Some(true), Some(true), None, None, None, Some(false), Some(false), Some(false)]);
    /// let b = ArrayData::from(vec![Some(true), None, Some(false), Some(true), None, Some(false), Some(true), None, Some(false)]);
    /// let result = and(a, b)?.into_canonical()?.into_bool()?;
    /// let result_vec = result.with_iterator(|it| it.map(|x| x.cloned()).collect::<Vec<_>>())?;
    /// assert_eq!(result_vec, vec![Some(true), None, Some(false), None, None, None, Some(false), None, Some(false)]);
    /// # use vortex_error::VortexError;
    /// # Ok::<(), VortexError>(())
    /// ```
    fn and(&self, array: &ArrayData) -> VortexResult<ArrayData>;

    /// Point-wise Kleene logical _and_ between two Boolean arrays.
    ///
    /// # Examples
    ///
    /// ```
    /// use vortex_array::ArrayData;
    /// use vortex_array::compute::and_kleene;
    /// use vortex_array::IntoCanonical;
    /// use vortex_array::accessor::ArrayAccessor;
    /// let a = ArrayData::from(vec![Some(true), Some(true), Some(true), None, None, None, Some(false), Some(false), Some(false)]);
    /// let b = ArrayData::from(vec![Some(true), None, Some(false), Some(true), None, Some(false), Some(true), None, Some(false)]);
    /// let result = and_kleene(a, b)?.into_canonical()?.into_bool()?;
    /// let result_vec = result.with_iterator(|it| it.map(|x| x.cloned()).collect::<Vec<_>>())?;
    /// assert_eq!(result_vec, vec![Some(true), None, Some(false), None, None, Some(false), Some(false), Some(false), Some(false)]);
    /// # use vortex_error::VortexError;
    /// # Ok::<(), VortexError>(())
    /// ```
    fn and_kleene(&self, array: &ArrayData) -> VortexResult<ArrayData>;
}

pub trait OrFn {
    /// Point-wise logical _or_ between two Boolean arrays.
    ///
    /// This method uses Arrow-style null propagation rather than the Kleene logic semantics.
    ///
    /// # Examples
    ///
    /// ```
    /// use vortex_array::ArrayData;
    /// use vortex_array::compute::or;
    /// use vortex_array::IntoCanonical;
    /// use vortex_array::accessor::ArrayAccessor;
    /// let a = ArrayData::from(vec![Some(true), Some(true), Some(true), None, None, None, Some(false), Some(false), Some(false)]);
    /// let b = ArrayData::from(vec![Some(true), None, Some(false), Some(true), None, Some(false), Some(true), None, Some(false)]);
    /// let result = or(a, b)?.into_canonical()?.into_bool()?;
    /// let result_vec = result.with_iterator(|it| it.map(|x| x.cloned()).collect::<Vec<_>>())?;
    /// assert_eq!(result_vec, vec![Some(true), None, Some(true), None, None, None, Some(true), None, Some(false)]);
    /// # use vortex_error::VortexError;
    /// # Ok::<(), VortexError>(())
    /// ```
    fn or(&self, array: &ArrayData) -> VortexResult<ArrayData>;

    /// Point-wise Kleene logical _or_ between two Boolean arrays.
    ///
    /// # Examples
    ///
    /// ```
    /// use vortex_array::ArrayData;
    /// use vortex_array::compute::or_kleene;
    /// use vortex_array::IntoCanonical;
    /// use vortex_array::accessor::ArrayAccessor;
    /// let a = ArrayData::from(vec![Some(true), Some(true), Some(true), None, None, None, Some(false), Some(false), Some(false)]);
    /// let b = ArrayData::from(vec![Some(true), None, Some(false), Some(true), None, Some(false), Some(true), None, Some(false)]);
    /// let result = or_kleene(a, b)?.into_canonical()?.into_bool()?;
    /// let result_vec = result.with_iterator(|it| it.map(|x| x.cloned()).collect::<Vec<_>>())?;
    /// assert_eq!(result_vec, vec![Some(true), Some(true), Some(true), Some(true), None, None, Some(true), None, Some(false)]);
    /// # use vortex_error::VortexError;
    /// # Ok::<(), VortexError>(())
    /// ```
    fn or_kleene(&self, array: &ArrayData) -> VortexResult<ArrayData>;
}

fn lift_boolean_operator<F, G>(
    lhs: impl AsRef<ArrayData>,
    rhs: impl AsRef<ArrayData>,
    trait_fun: F,
    bool_array_fun: G,
) -> VortexResult<ArrayData>
where
    F: Fn(&ArrayData, &ArrayData) -> Option<VortexResult<ArrayData>>,
    G: FnOnce(BoolArray, &ArrayData) -> VortexResult<ArrayData>,
{
    let lhs = lhs.as_ref();
    let rhs = rhs.as_ref();

    if lhs.len() != rhs.len() {
        vortex_bail!("Boolean operations aren't supported on arrays of different lengths")
    }

    if !lhs.dtype().is_boolean() || !rhs.dtype().is_boolean() {
        vortex_bail!("Boolean operations are only supported on boolean arrays")
    }

    if let Some(selection) = trait_fun(lhs, rhs) {
        return selection;
    }

    if let Some(selection) = trait_fun(rhs, lhs) {
        return selection;
    }

    // If neither side implements the trait, we try to expand the left-hand side into a `BoolArray`,
    // which we know does implement it, and call into that implementation.
    let lhs = lhs.clone().into_bool()?;

    bool_array_fun(lhs, rhs)
}

pub fn and(lhs: impl AsRef<ArrayData>, rhs: impl AsRef<ArrayData>) -> VortexResult<ArrayData> {
    lift_boolean_operator(
        lhs,
        rhs,
        |lhs, rhs| lhs.with_dyn(|lhs| lhs.and().map(|lhs| lhs.and(rhs))),
        |lhs, rhs| lhs.and(rhs),
    )
}

pub fn and_kleene(
    lhs: impl AsRef<ArrayData>,
    rhs: impl AsRef<ArrayData>,
) -> VortexResult<ArrayData> {
    lift_boolean_operator(
        lhs,
        rhs,
        |lhs, rhs| lhs.with_dyn(|lhs| lhs.and_kleene().map(|lhs| lhs.and_kleene(rhs))),
        |lhs, rhs| lhs.and_kleene(rhs),
    )
}

pub fn or(lhs: impl AsRef<ArrayData>, rhs: impl AsRef<ArrayData>) -> VortexResult<ArrayData> {
    lift_boolean_operator(
        lhs,
        rhs,
        |lhs, rhs| lhs.with_dyn(|lhs| lhs.or().map(|lhs| lhs.or(rhs))),
        |lhs, rhs| lhs.or(rhs),
    )
}

pub fn or_kleene(
    lhs: impl AsRef<ArrayData>,
    rhs: impl AsRef<ArrayData>,
) -> VortexResult<ArrayData> {
    lift_boolean_operator(
        lhs,
        rhs,
        |lhs, rhs| lhs.with_dyn(|lhs| lhs.or_kleene().map(|lhs| lhs.or_kleene(rhs))),
        |lhs, rhs| lhs.or_kleene(rhs),
    )
}

#[cfg(test)]
mod tests {
    use rstest::rstest;

    use super::*;
    use crate::array::BoolArray;
    use crate::compute::unary::scalar_at;
    use crate::IntoArrayData;

    #[rstest]
    #[case(BoolArray::from_iter([Some(true), Some(true), Some(false), Some(false)].into_iter())
    .into_array(), BoolArray::from_iter([Some(true), Some(false), Some(true), Some(false)].into_iter())
    .into_array())]
    #[case(BoolArray::from_iter([Some(true), Some(false), Some(true), Some(false)].into_iter()).into_array(),
        BoolArray::from_iter([Some(true), Some(true), Some(false), Some(false)].into_iter()).into_array())]
    fn test_or(#[case] lhs: ArrayData, #[case] rhs: ArrayData) {
        let r = or(&lhs, &rhs).unwrap();

        let r = r.into_bool().unwrap().into_array();

        let v0 = scalar_at(&r, 0).unwrap().value().as_bool().unwrap();
        let v1 = scalar_at(&r, 1).unwrap().value().as_bool().unwrap();
        let v2 = scalar_at(&r, 2).unwrap().value().as_bool().unwrap();
        let v3 = scalar_at(&r, 3).unwrap().value().as_bool().unwrap();

        assert!(v0.unwrap());
        assert!(v1.unwrap());
        assert!(v2.unwrap());
        assert!(!v3.unwrap());
    }

    #[rstest]
    #[case(BoolArray::from_iter([Some(true), Some(true), Some(false), Some(false)].into_iter())
    .into_array(), BoolArray::from_iter([Some(true), Some(false), Some(true), Some(false)].into_iter())
    .into_array())]
    #[case(BoolArray::from_iter([Some(true), Some(false), Some(true), Some(false)].into_iter()).into_array(),
        BoolArray::from_iter([Some(true), Some(true), Some(false), Some(false)].into_iter()).into_array())]
    fn test_and(#[case] lhs: ArrayData, #[case] rhs: ArrayData) {
        let r = and(&lhs, &rhs).unwrap().into_bool().unwrap().into_array();

        let v0 = scalar_at(&r, 0).unwrap().value().as_bool().unwrap();
        let v1 = scalar_at(&r, 1).unwrap().value().as_bool().unwrap();
        let v2 = scalar_at(&r, 2).unwrap().value().as_bool().unwrap();
        let v3 = scalar_at(&r, 3).unwrap().value().as_bool().unwrap();

        assert!(v0.unwrap());
        assert!(!v1.unwrap());
        assert!(!v2.unwrap());
        assert!(!v3.unwrap());
    }
}