Skip to main content

polars_core/chunked_array/comparison/
mod.rs

1mod scalar;
2
3#[cfg(feature = "dtype-categorical")]
4mod categorical;
5
6use std::ops::{BitAnd, BitOr, Not};
7
8use arrow::array::BooleanArray;
9use arrow::bitmap::{Bitmap, BitmapBuilder};
10use num_traits::{NumCast, ToPrimitive};
11use polars_compute::comparisons::{TotalEqKernel, TotalOrdKernel};
12
13use crate::prelude::*;
14use crate::series::IsSorted;
15use crate::series::implementations::null::NullChunked;
16
17impl<T> ChunkCompareEq<&ChunkedArray<T>> for ChunkedArray<T>
18where
19    T: PolarsNumericType,
20    T::Array: TotalOrdKernel<Scalar = T::Native> + TotalEqKernel<Scalar = T::Native>,
21{
22    type Item = BooleanChunked;
23
24    fn equal(&self, rhs: &ChunkedArray<T>) -> BooleanChunked {
25        // Broadcast.
26        match (self.len(), rhs.len()) {
27            (_, 1) => {
28                if let Some(value) = rhs.get(0) {
29                    self.equal(value)
30                } else {
31                    BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
32                }
33            },
34            (1, _) => {
35                if let Some(value) = self.get(0) {
36                    rhs.equal(value)
37                } else {
38                    BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
39                }
40            },
41            _ => arity::binary_mut_values(
42                self,
43                rhs,
44                |a, b| a.tot_eq_kernel(b).into(),
45                PlSmallStr::EMPTY,
46            ),
47        }
48    }
49
50    fn equal_missing(&self, rhs: &ChunkedArray<T>) -> BooleanChunked {
51        // Broadcast.
52        match (self.len(), rhs.len()) {
53            (_, 1) => {
54                if let Some(value) = rhs.get(0) {
55                    self.equal_missing(value)
56                } else {
57                    self.is_null()
58                }
59            },
60            (1, _) => {
61                if let Some(value) = self.get(0) {
62                    rhs.equal_missing(value)
63                } else {
64                    rhs.is_null()
65                }
66            },
67            _ => arity::binary_mut_with_options(
68                self,
69                rhs,
70                |a, b| a.tot_eq_missing_kernel(b).into(),
71                PlSmallStr::EMPTY,
72            ),
73        }
74    }
75
76    fn not_equal(&self, rhs: &ChunkedArray<T>) -> BooleanChunked {
77        // Broadcast.
78        match (self.len(), rhs.len()) {
79            (_, 1) => {
80                if let Some(value) = rhs.get(0) {
81                    self.not_equal(value)
82                } else {
83                    BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
84                }
85            },
86            (1, _) => {
87                if let Some(value) = self.get(0) {
88                    rhs.not_equal(value)
89                } else {
90                    BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
91                }
92            },
93            _ => arity::binary_mut_values(
94                self,
95                rhs,
96                |a, b| a.tot_ne_kernel(b).into(),
97                PlSmallStr::EMPTY,
98            ),
99        }
100    }
101
102    fn not_equal_missing(&self, rhs: &ChunkedArray<T>) -> BooleanChunked {
103        // Broadcast.
104        match (self.len(), rhs.len()) {
105            (_, 1) => {
106                if let Some(value) = rhs.get(0) {
107                    self.not_equal_missing(value)
108                } else {
109                    self.is_not_null()
110                }
111            },
112            (1, _) => {
113                if let Some(value) = self.get(0) {
114                    rhs.not_equal_missing(value)
115                } else {
116                    rhs.is_not_null()
117                }
118            },
119            _ => arity::binary_mut_with_options(
120                self,
121                rhs,
122                |a, b| a.tot_ne_missing_kernel(b).into(),
123                PlSmallStr::EMPTY,
124            ),
125        }
126    }
127}
128
129impl<T> ChunkCompareIneq<&ChunkedArray<T>> for ChunkedArray<T>
130where
131    T: PolarsNumericType,
132    T::Array: TotalOrdKernel<Scalar = T::Native> + TotalEqKernel<Scalar = T::Native>,
133{
134    type Item = BooleanChunked;
135
136    fn lt(&self, rhs: &ChunkedArray<T>) -> BooleanChunked {
137        // Broadcast.
138        match (self.len(), rhs.len()) {
139            (_, 1) => {
140                if let Some(value) = rhs.get(0) {
141                    self.lt(value)
142                } else {
143                    BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
144                }
145            },
146            (1, _) => {
147                if let Some(value) = self.get(0) {
148                    rhs.gt(value)
149                } else {
150                    BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
151                }
152            },
153            _ => arity::binary_mut_values(
154                self,
155                rhs,
156                |a, b| a.tot_lt_kernel(b).into(),
157                PlSmallStr::EMPTY,
158            ),
159        }
160    }
161
162    fn lt_eq(&self, rhs: &ChunkedArray<T>) -> BooleanChunked {
163        // Broadcast.
164        match (self.len(), rhs.len()) {
165            (_, 1) => {
166                if let Some(value) = rhs.get(0) {
167                    self.lt_eq(value)
168                } else {
169                    BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
170                }
171            },
172            (1, _) => {
173                if let Some(value) = self.get(0) {
174                    rhs.gt_eq(value)
175                } else {
176                    BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
177                }
178            },
179            _ => arity::binary_mut_values(
180                self,
181                rhs,
182                |a, b| a.tot_le_kernel(b).into(),
183                PlSmallStr::EMPTY,
184            ),
185        }
186    }
187
188    fn gt(&self, rhs: &Self) -> BooleanChunked {
189        rhs.lt(self)
190    }
191
192    fn gt_eq(&self, rhs: &Self) -> BooleanChunked {
193        rhs.lt_eq(self)
194    }
195}
196
197impl ChunkCompareEq<&NullChunked> for NullChunked {
198    type Item = BooleanChunked;
199
200    fn equal(&self, rhs: &NullChunked) -> Self::Item {
201        BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))
202    }
203
204    fn equal_missing(&self, rhs: &NullChunked) -> Self::Item {
205        BooleanChunked::full(self.name().clone(), true, get_broadcast_length(self, rhs))
206    }
207
208    fn not_equal(&self, rhs: &NullChunked) -> Self::Item {
209        BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))
210    }
211
212    fn not_equal_missing(&self, rhs: &NullChunked) -> Self::Item {
213        BooleanChunked::full(self.name().clone(), false, get_broadcast_length(self, rhs))
214    }
215}
216
217impl ChunkCompareIneq<&NullChunked> for NullChunked {
218    type Item = BooleanChunked;
219
220    fn gt(&self, rhs: &NullChunked) -> Self::Item {
221        BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))
222    }
223
224    fn gt_eq(&self, rhs: &NullChunked) -> Self::Item {
225        BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))
226    }
227
228    fn lt(&self, rhs: &NullChunked) -> Self::Item {
229        BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))
230    }
231
232    fn lt_eq(&self, rhs: &NullChunked) -> Self::Item {
233        BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))
234    }
235}
236
237#[inline]
238fn get_broadcast_length(lhs: &NullChunked, rhs: &NullChunked) -> usize {
239    match (lhs.len(), rhs.len()) {
240        (1, len_r) => len_r,
241        (len_l, 1) => len_l,
242        (len_l, len_r) if len_l == len_r => len_l,
243        _ => panic!("Cannot compare two series of different lengths."),
244    }
245}
246
247impl ChunkCompareEq<&BooleanChunked> for BooleanChunked {
248    type Item = BooleanChunked;
249
250    fn equal(&self, rhs: &BooleanChunked) -> BooleanChunked {
251        // Broadcast.
252        match (self.len(), rhs.len()) {
253            (_, 1) => {
254                if let Some(value) = rhs.get(0) {
255                    arity::unary_mut_values(self, |arr| arr.tot_eq_kernel_broadcast(&value).into())
256                } else {
257                    BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
258                }
259            },
260            (1, _) => {
261                if let Some(value) = self.get(0) {
262                    arity::unary_mut_values(rhs, |arr| arr.tot_eq_kernel_broadcast(&value).into())
263                } else {
264                    BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
265                }
266            },
267            _ => arity::binary_mut_values(
268                self,
269                rhs,
270                |a, b| a.tot_eq_kernel(b).into(),
271                PlSmallStr::EMPTY,
272            ),
273        }
274    }
275
276    fn equal_missing(&self, rhs: &BooleanChunked) -> BooleanChunked {
277        // Broadcast.
278        match (self.len(), rhs.len()) {
279            (_, 1) => {
280                if let Some(value) = rhs.get(0) {
281                    arity::unary_mut_with_options(self, |arr| {
282                        arr.tot_eq_missing_kernel_broadcast(&value).into()
283                    })
284                } else {
285                    self.is_null()
286                }
287            },
288            (1, _) => {
289                if let Some(value) = self.get(0) {
290                    arity::unary_mut_with_options(rhs, |arr| {
291                        arr.tot_eq_missing_kernel_broadcast(&value).into()
292                    })
293                } else {
294                    rhs.is_null()
295                }
296            },
297            _ => arity::binary_mut_with_options(
298                self,
299                rhs,
300                |a, b| a.tot_eq_missing_kernel(b).into(),
301                PlSmallStr::EMPTY,
302            ),
303        }
304    }
305
306    fn not_equal(&self, rhs: &BooleanChunked) -> BooleanChunked {
307        // Broadcast.
308        match (self.len(), rhs.len()) {
309            (_, 1) => {
310                if let Some(value) = rhs.get(0) {
311                    arity::unary_mut_values(self, |arr| arr.tot_ne_kernel_broadcast(&value).into())
312                } else {
313                    BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
314                }
315            },
316            (1, _) => {
317                if let Some(value) = self.get(0) {
318                    arity::unary_mut_values(rhs, |arr| arr.tot_ne_kernel_broadcast(&value).into())
319                } else {
320                    BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
321                }
322            },
323            _ => arity::binary_mut_values(
324                self,
325                rhs,
326                |a, b| a.tot_ne_kernel(b).into(),
327                PlSmallStr::EMPTY,
328            ),
329        }
330    }
331
332    fn not_equal_missing(&self, rhs: &BooleanChunked) -> BooleanChunked {
333        // Broadcast.
334        match (self.len(), rhs.len()) {
335            (_, 1) => {
336                if let Some(value) = rhs.get(0) {
337                    arity::unary_mut_with_options(self, |arr| {
338                        arr.tot_ne_missing_kernel_broadcast(&value).into()
339                    })
340                } else {
341                    self.is_not_null()
342                }
343            },
344            (1, _) => {
345                if let Some(value) = self.get(0) {
346                    arity::unary_mut_with_options(rhs, |arr| {
347                        arr.tot_ne_missing_kernel_broadcast(&value).into()
348                    })
349                } else {
350                    rhs.is_not_null()
351                }
352            },
353            _ => arity::binary_mut_with_options(
354                self,
355                rhs,
356                |a, b| a.tot_ne_missing_kernel(b).into(),
357                PlSmallStr::EMPTY,
358            ),
359        }
360    }
361}
362
363impl ChunkCompareIneq<&BooleanChunked> for BooleanChunked {
364    type Item = BooleanChunked;
365
366    fn lt(&self, rhs: &BooleanChunked) -> BooleanChunked {
367        // Broadcast.
368        match (self.len(), rhs.len()) {
369            (_, 1) => {
370                if let Some(value) = rhs.get(0) {
371                    arity::unary_mut_values(self, |arr| arr.tot_lt_kernel_broadcast(&value).into())
372                } else {
373                    BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
374                }
375            },
376            (1, _) => {
377                if let Some(value) = self.get(0) {
378                    arity::unary_mut_values(rhs, |arr| arr.tot_gt_kernel_broadcast(&value).into())
379                } else {
380                    BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
381                }
382            },
383            _ => arity::binary_mut_values(
384                self,
385                rhs,
386                |a, b| a.tot_lt_kernel(b).into(),
387                PlSmallStr::EMPTY,
388            ),
389        }
390    }
391
392    fn lt_eq(&self, rhs: &BooleanChunked) -> BooleanChunked {
393        // Broadcast.
394        match (self.len(), rhs.len()) {
395            (_, 1) => {
396                if let Some(value) = rhs.get(0) {
397                    arity::unary_mut_values(self, |arr| arr.tot_le_kernel_broadcast(&value).into())
398                } else {
399                    BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
400                }
401            },
402            (1, _) => {
403                if let Some(value) = self.get(0) {
404                    arity::unary_mut_values(rhs, |arr| arr.tot_ge_kernel_broadcast(&value).into())
405                } else {
406                    BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
407                }
408            },
409            _ => arity::binary_mut_values(
410                self,
411                rhs,
412                |a, b| a.tot_le_kernel(b).into(),
413                PlSmallStr::EMPTY,
414            ),
415        }
416    }
417
418    fn gt(&self, rhs: &Self) -> BooleanChunked {
419        rhs.lt(self)
420    }
421
422    fn gt_eq(&self, rhs: &Self) -> BooleanChunked {
423        rhs.lt_eq(self)
424    }
425}
426
427impl ChunkCompareEq<&StringChunked> for StringChunked {
428    type Item = BooleanChunked;
429
430    fn equal(&self, rhs: &StringChunked) -> BooleanChunked {
431        self.as_binary().equal(&rhs.as_binary())
432    }
433
434    fn equal_missing(&self, rhs: &StringChunked) -> BooleanChunked {
435        self.as_binary().equal_missing(&rhs.as_binary())
436    }
437
438    fn not_equal(&self, rhs: &StringChunked) -> BooleanChunked {
439        self.as_binary().not_equal(&rhs.as_binary())
440    }
441
442    fn not_equal_missing(&self, rhs: &StringChunked) -> BooleanChunked {
443        self.as_binary().not_equal_missing(&rhs.as_binary())
444    }
445}
446
447impl ChunkCompareIneq<&StringChunked> for StringChunked {
448    type Item = BooleanChunked;
449
450    fn gt(&self, rhs: &StringChunked) -> BooleanChunked {
451        self.as_binary().gt(&rhs.as_binary())
452    }
453
454    fn gt_eq(&self, rhs: &StringChunked) -> BooleanChunked {
455        self.as_binary().gt_eq(&rhs.as_binary())
456    }
457
458    fn lt(&self, rhs: &StringChunked) -> BooleanChunked {
459        self.as_binary().lt(&rhs.as_binary())
460    }
461
462    fn lt_eq(&self, rhs: &StringChunked) -> BooleanChunked {
463        self.as_binary().lt_eq(&rhs.as_binary())
464    }
465}
466
467macro_rules! binary_eq_ineq_impl {
468    ($($ca:ident),+) => {
469        $(
470        impl ChunkCompareEq<&$ca> for $ca {
471            type Item = BooleanChunked;
472
473            fn equal(&self, rhs: &$ca) -> BooleanChunked {
474                // Broadcast.
475                match (self.len(), rhs.len()) {
476                    (_, 1) => {
477                        if let Some(value) = rhs.get(0) {
478                            self.equal(value)
479                        } else {
480                            BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
481                        }
482                    },
483                    (1, _) => {
484                        if let Some(value) = self.get(0) {
485                            rhs.equal(value)
486                        } else {
487                            BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
488                        }
489                    },
490                    _ => arity::binary_mut_values(
491                        self,
492                        rhs,
493                        |a, b| a.tot_eq_kernel(b).into(),
494                        PlSmallStr::EMPTY,
495                    ),
496                }
497            }
498
499            fn equal_missing(&self, rhs: &$ca) -> BooleanChunked {
500                // Broadcast.
501                match (self.len(), rhs.len()) {
502                    (_, 1) => {
503                        if let Some(value) = rhs.get(0) {
504                            self.equal_missing(value)
505                        } else {
506                            self.is_null()
507                        }
508                    },
509                    (1, _) => {
510                        if let Some(value) = self.get(0) {
511                            rhs.equal_missing(value)
512                        } else {
513                            rhs.is_null()
514                        }
515                    },
516                    _ => arity::binary_mut_with_options(
517                        self,
518                        rhs,
519                        |a, b| a.tot_eq_missing_kernel(b).into(),
520                        PlSmallStr::EMPTY,
521                    ),
522                }
523            }
524
525            fn not_equal(&self, rhs: &$ca) -> BooleanChunked {
526                // Broadcast.
527                match (self.len(), rhs.len()) {
528                    (_, 1) => {
529                        if let Some(value) = rhs.get(0) {
530                            self.not_equal(value)
531                        } else {
532                            BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
533                        }
534                    },
535                    (1, _) => {
536                        if let Some(value) = self.get(0) {
537                            rhs.not_equal(value)
538                        } else {
539                            BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
540                        }
541                    },
542                    _ => arity::binary_mut_values(
543                        self,
544                        rhs,
545                        |a, b| a.tot_ne_kernel(b).into(),
546                        PlSmallStr::EMPTY,
547                    ),
548                }
549            }
550
551            fn not_equal_missing(&self, rhs: &$ca) -> BooleanChunked {
552                // Broadcast.
553                match (self.len(), rhs.len()) {
554                    (_, 1) => {
555                        if let Some(value) = rhs.get(0) {
556                            self.not_equal_missing(value)
557                        } else {
558                            self.is_not_null()
559                        }
560                    },
561                    (1, _) => {
562                        if let Some(value) = self.get(0) {
563                            rhs.not_equal_missing(value)
564                        } else {
565                            rhs.is_not_null()
566                        }
567                    },
568                    _ => arity::binary_mut_with_options(
569                        self,
570                        rhs,
571                        |a, b| a.tot_ne_missing_kernel(b).into(),
572                        PlSmallStr::EMPTY,
573                    ),
574                }
575            }
576        }
577
578        impl ChunkCompareIneq<&$ca> for $ca {
579            type Item = BooleanChunked;
580
581            fn lt(&self, rhs: &$ca) -> BooleanChunked {
582                // Broadcast.
583                match (self.len(), rhs.len()) {
584                    (_, 1) => {
585                        if let Some(value) = rhs.get(0) {
586                            self.lt(value)
587                        } else {
588                            BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
589                        }
590                    },
591                    (1, _) => {
592                        if let Some(value) = self.get(0) {
593                            rhs.gt(value)
594                        } else {
595                            BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
596                        }
597                    },
598                    _ => arity::binary_mut_values(
599                        self,
600                        rhs,
601                        |a, b| a.tot_lt_kernel(b).into(),
602                        PlSmallStr::EMPTY,
603                    ),
604                }
605            }
606
607            fn lt_eq(&self, rhs: &$ca) -> BooleanChunked {
608                // Broadcast.
609                match (self.len(), rhs.len()) {
610                    (_, 1) => {
611                        if let Some(value) = rhs.get(0) {
612                            self.lt_eq(value)
613                        } else {
614                            BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
615                        }
616                    },
617                    (1, _) => {
618                        if let Some(value) = self.get(0) {
619                            rhs.gt_eq(value)
620                        } else {
621                            BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len())
622                        }
623                    },
624                    _ => arity::binary_mut_values(
625                        self,
626                        rhs,
627                        |a, b| a.tot_le_kernel(b).into(),
628                        PlSmallStr::EMPTY,
629                    ),
630                }
631            }
632
633            fn gt(&self, rhs: &Self) -> BooleanChunked {
634                rhs.lt(self)
635            }
636
637            fn gt_eq(&self, rhs: &Self) -> BooleanChunked {
638                rhs.lt_eq(self)
639            }
640        }
641        )+
642    };
643}
644
645binary_eq_ineq_impl!(BinaryChunked, BinaryOffsetChunked);
646
647fn _list_comparison_helper<F, B>(
648    lhs: &ListChunked,
649    rhs: &ListChunked,
650    op: F,
651    broadcast_op: B,
652    missing: bool,
653    is_ne: bool,
654) -> BooleanChunked
655where
656    F: Fn(&ListArray<i64>, &ListArray<i64>) -> Bitmap,
657    B: Fn(&ListArray<i64>, &Box<dyn Array>) -> Bitmap,
658{
659    match (lhs.len(), rhs.len()) {
660        (_, 1) => {
661            let right = rhs
662                .downcast_iter()
663                .find(|x| !x.is_empty())
664                .unwrap()
665                .as_any()
666                .downcast_ref::<ListArray<i64>>()
667                .unwrap();
668
669            if !right.validity().is_none_or(|v| v.get(0).unwrap()) {
670                if missing {
671                    if is_ne {
672                        return lhs.is_not_null();
673                    } else {
674                        return lhs.is_null();
675                    }
676                } else {
677                    return BooleanChunked::full_null(PlSmallStr::EMPTY, lhs.len());
678                }
679            }
680
681            let values = right.values().sliced(
682                (*right.offsets().first()).try_into().unwrap(),
683                right.offsets().range().try_into().unwrap(),
684            );
685
686            if missing {
687                arity::unary_mut_with_options(lhs, |a| broadcast_op(a, &values).into())
688            } else {
689                arity::unary_mut_values(lhs, |a| broadcast_op(a, &values).into())
690            }
691        },
692        (1, _) => {
693            let left = lhs
694                .downcast_iter()
695                .find(|x| !x.is_empty())
696                .unwrap()
697                .as_any()
698                .downcast_ref::<ListArray<i64>>()
699                .unwrap();
700
701            if !left.validity().is_none_or(|v| v.get(0).unwrap()) {
702                if missing {
703                    if is_ne {
704                        return rhs.is_not_null();
705                    } else {
706                        return rhs.is_null();
707                    }
708                } else {
709                    return BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len());
710                }
711            }
712
713            let values = left.values().sliced(
714                (*left.offsets().first()).try_into().unwrap(),
715                left.offsets().range().try_into().unwrap(),
716            );
717
718            if missing {
719                arity::unary_mut_with_options(rhs, |a| broadcast_op(a, &values).into())
720            } else {
721                arity::unary_mut_values(rhs, |a| broadcast_op(a, &values).into())
722            }
723        },
724        _ => {
725            if missing {
726                arity::binary_mut_with_options(lhs, rhs, |a, b| op(a, b).into(), PlSmallStr::EMPTY)
727            } else {
728                arity::binary_mut_values(lhs, rhs, |a, b| op(a, b).into(), PlSmallStr::EMPTY)
729            }
730        },
731    }
732}
733
734impl ChunkCompareEq<&ListChunked> for ListChunked {
735    type Item = BooleanChunked;
736    fn equal(&self, rhs: &ListChunked) -> BooleanChunked {
737        _list_comparison_helper(
738            self,
739            rhs,
740            TotalEqKernel::tot_eq_kernel,
741            TotalEqKernel::tot_eq_kernel_broadcast,
742            false,
743            false,
744        )
745    }
746
747    fn equal_missing(&self, rhs: &ListChunked) -> BooleanChunked {
748        _list_comparison_helper(
749            self,
750            rhs,
751            TotalEqKernel::tot_eq_missing_kernel,
752            TotalEqKernel::tot_eq_missing_kernel_broadcast,
753            true,
754            false,
755        )
756    }
757
758    fn not_equal(&self, rhs: &ListChunked) -> BooleanChunked {
759        _list_comparison_helper(
760            self,
761            rhs,
762            TotalEqKernel::tot_ne_kernel,
763            TotalEqKernel::tot_ne_kernel_broadcast,
764            false,
765            true,
766        )
767    }
768
769    fn not_equal_missing(&self, rhs: &ListChunked) -> BooleanChunked {
770        _list_comparison_helper(
771            self,
772            rhs,
773            TotalEqKernel::tot_ne_missing_kernel,
774            TotalEqKernel::tot_ne_missing_kernel_broadcast,
775            true,
776            true,
777        )
778    }
779}
780
781#[cfg(feature = "dtype-struct")]
782fn struct_helper<F, R>(
783    a: &StructChunked,
784    b: &StructChunked,
785    op: F,
786    reduce: R,
787    op_is_ne: bool,
788    is_missing: bool,
789) -> BooleanChunked
790where
791    F: Fn(&Series, &Series) -> BooleanChunked,
792    R: Fn(BooleanChunked, BooleanChunked) -> BooleanChunked,
793{
794    let len_a = a.len();
795    let len_b = b.len();
796    let broadcasts = len_a == 1 || len_b == 1;
797    assert!(a.struct_fields().len() == b.struct_fields().len());
798    assert!(a.len() == b.len() || broadcasts);
799
800    let mut out = a
801        .fields_as_series()
802        .iter()
803        .zip(b.fields_as_series().iter())
804        .map(|(l, r)| op(l, r))
805        .reduce(&reduce)
806        .unwrap_or_else(|| BooleanChunked::full(PlSmallStr::EMPTY, !op_is_ne, a.len()));
807
808    if is_missing && (a.has_nulls() || b.has_nulls()) {
809        // Do some allocations so that we can use the Series dispatch, it otherwise
810        // gets complicated dealing with combinations of ==, != and broadcasting.
811        let default =
812            || BooleanChunked::with_chunk(PlSmallStr::EMPTY, BooleanArray::from_slice([true]));
813        let validity_to_ca = |x| unsafe {
814            BooleanChunked::with_chunk(
815                PlSmallStr::EMPTY,
816                BooleanArray::from_inner_unchecked(ArrowDataType::Boolean, x, None),
817            )
818        };
819
820        let a_s = a.rechunk_validity().map_or_else(default, validity_to_ca);
821        let b_s = b.rechunk_validity().map_or_else(default, validity_to_ca);
822
823        let shared_validity = (&a_s).bitand(&b_s);
824        let valid_nested = if op_is_ne {
825            (shared_validity).bitand(out)
826        } else {
827            (!shared_validity).bitor(out)
828        };
829        out = reduce(op(&a_s.into_series(), &b_s.into_series()), valid_nested);
830    }
831
832    if !is_missing && (a.has_nulls() || b.has_nulls()) {
833        use arrow::compute::utils::combine_validities_and;
834        let av = a.rechunk_validity();
835        let bv = b.rechunk_validity();
836        out.set_validity(combine_validities_and(av.as_ref(), bv.as_ref()));
837    }
838
839    out
840}
841
842#[cfg(feature = "dtype-struct")]
843impl ChunkCompareEq<&StructChunked> for StructChunked {
844    type Item = BooleanChunked;
845    fn equal(&self, rhs: &StructChunked) -> BooleanChunked {
846        struct_helper(
847            self,
848            rhs,
849            |l, r| l.equal_missing(r).unwrap(),
850            |a, b| a.bitand(b),
851            false,
852            false,
853        )
854    }
855
856    fn equal_missing(&self, rhs: &StructChunked) -> BooleanChunked {
857        struct_helper(
858            self,
859            rhs,
860            |l, r| l.equal_missing(r).unwrap(),
861            |a, b| a.bitand(b),
862            false,
863            true,
864        )
865    }
866
867    fn not_equal(&self, rhs: &StructChunked) -> BooleanChunked {
868        struct_helper(
869            self,
870            rhs,
871            |l, r| l.not_equal_missing(r).unwrap(),
872            |a, b| a.bitor(b),
873            true,
874            false,
875        )
876    }
877
878    fn not_equal_missing(&self, rhs: &StructChunked) -> BooleanChunked {
879        struct_helper(
880            self,
881            rhs,
882            |l, r| l.not_equal_missing(r).unwrap(),
883            |a, b| a.bitor(b),
884            true,
885            true,
886        )
887    }
888}
889
890#[cfg(feature = "dtype-array")]
891fn _array_comparison_helper<F, B>(
892    lhs: &ArrayChunked,
893    rhs: &ArrayChunked,
894    op: F,
895    broadcast_op: B,
896    missing: bool,
897    is_ne: bool,
898) -> BooleanChunked
899where
900    F: Fn(&FixedSizeListArray, &FixedSizeListArray) -> Bitmap,
901    B: Fn(&FixedSizeListArray, &Box<dyn Array>) -> Bitmap,
902{
903    match (lhs.len(), rhs.len()) {
904        (_, 1) => {
905            let right = rhs
906                .downcast_iter()
907                .find(|x| !x.is_empty())
908                .unwrap()
909                .as_any()
910                .downcast_ref::<FixedSizeListArray>()
911                .unwrap();
912
913            if !right.validity().is_none_or(|v| v.get(0).unwrap()) {
914                if missing {
915                    if is_ne {
916                        return lhs.is_not_null();
917                    } else {
918                        return lhs.is_null();
919                    }
920                } else {
921                    return BooleanChunked::full_null(PlSmallStr::EMPTY, lhs.len());
922                }
923            }
924
925            if missing {
926                arity::unary_mut_with_options(lhs, |a| broadcast_op(a, right.values()).into())
927            } else {
928                arity::unary_mut_values(lhs, |a| broadcast_op(a, right.values()).into())
929            }
930        },
931        (1, _) => {
932            let left = lhs
933                .downcast_iter()
934                .find(|x| !x.is_empty())
935                .unwrap()
936                .as_any()
937                .downcast_ref::<FixedSizeListArray>()
938                .unwrap();
939
940            if !left.validity().is_none_or(|v| v.get(0).unwrap()) {
941                if missing {
942                    if is_ne {
943                        return rhs.is_not_null();
944                    } else {
945                        return rhs.is_null();
946                    }
947                } else {
948                    return BooleanChunked::full_null(PlSmallStr::EMPTY, rhs.len());
949                }
950            }
951
952            if missing {
953                arity::unary_mut_with_options(rhs, |a| broadcast_op(a, left.values()).into())
954            } else {
955                arity::unary_mut_values(rhs, |a| broadcast_op(a, left.values()).into())
956            }
957        },
958        _ => {
959            if missing {
960                arity::binary_mut_with_options(lhs, rhs, |a, b| op(a, b).into(), PlSmallStr::EMPTY)
961            } else {
962                arity::binary_mut_values(lhs, rhs, |a, b| op(a, b).into(), PlSmallStr::EMPTY)
963            }
964        },
965    }
966}
967
968#[cfg(feature = "dtype-array")]
969impl ChunkCompareEq<&ArrayChunked> for ArrayChunked {
970    type Item = BooleanChunked;
971    fn equal(&self, rhs: &ArrayChunked) -> BooleanChunked {
972        _array_comparison_helper(
973            self,
974            rhs,
975            TotalEqKernel::tot_eq_kernel,
976            TotalEqKernel::tot_eq_kernel_broadcast,
977            false,
978            false,
979        )
980    }
981
982    fn equal_missing(&self, rhs: &ArrayChunked) -> BooleanChunked {
983        _array_comparison_helper(
984            self,
985            rhs,
986            TotalEqKernel::tot_eq_missing_kernel,
987            TotalEqKernel::tot_eq_missing_kernel_broadcast,
988            true,
989            false,
990        )
991    }
992
993    fn not_equal(&self, rhs: &ArrayChunked) -> BooleanChunked {
994        _array_comparison_helper(
995            self,
996            rhs,
997            TotalEqKernel::tot_ne_kernel,
998            TotalEqKernel::tot_ne_kernel_broadcast,
999            false,
1000            true,
1001        )
1002    }
1003
1004    fn not_equal_missing(&self, rhs: &ArrayChunked) -> Self::Item {
1005        _array_comparison_helper(
1006            self,
1007            rhs,
1008            TotalEqKernel::tot_ne_missing_kernel,
1009            TotalEqKernel::tot_ne_missing_kernel_broadcast,
1010            true,
1011            true,
1012        )
1013    }
1014}
1015
1016impl Not for &BooleanChunked {
1017    type Output = BooleanChunked;
1018
1019    fn not(self) -> Self::Output {
1020        let chunks = self.downcast_iter().map(polars_compute::boolean::not);
1021        ChunkedArray::from_chunk_iter(self.name().clone(), chunks)
1022    }
1023}
1024
1025impl Not for BooleanChunked {
1026    type Output = BooleanChunked;
1027
1028    fn not(self) -> Self::Output {
1029        (&self).not()
1030    }
1031}
1032
1033impl BooleanChunked {
1034    /// Returns whether any of the values in the column are `true`.
1035    ///
1036    /// Null values are ignored.
1037    pub fn any(&self) -> bool {
1038        self.downcast_iter()
1039            .any(|a| polars_compute::boolean::any(a).unwrap_or(false))
1040    }
1041
1042    /// Returns whether all values in the array are `true`.
1043    ///
1044    /// Null values are ignored.
1045    pub fn all(&self) -> bool {
1046        self.downcast_iter()
1047            .all(|a| polars_compute::boolean::all(a).unwrap_or(true))
1048    }
1049
1050    /// Returns whether any of the values in the column are `true`.
1051    ///
1052    /// The output is unknown (`None`) if the array contains any null values and
1053    /// no `true` values.
1054    pub fn any_kleene(&self) -> Option<bool> {
1055        for arr in self.downcast_iter() {
1056            if let Some(true) = polars_compute::boolean::any(arr) {
1057                return Some(true);
1058            }
1059        }
1060        if self.has_nulls() { None } else { Some(false) }
1061    }
1062
1063    /// Returns whether all values in the column are `true`.
1064    ///
1065    /// The output is unknown (`None`) if the array contains any null values and
1066    /// no `false` values.
1067    pub fn all_kleene(&self) -> Option<bool> {
1068        for arr in self.downcast_iter() {
1069            if let Some(false) = polars_compute::boolean::all(arr) {
1070                return Some(false);
1071            }
1072        }
1073        if self.has_nulls() { None } else { Some(true) }
1074    }
1075}
1076
1077#[cfg(test)]
1078#[cfg_attr(feature = "nightly", allow(clippy::manual_repeat_n))] // remove once stable
1079mod test {
1080    use std::iter::repeat_n;
1081
1082    use super::super::test::get_chunked_array;
1083    use crate::prelude::*;
1084
1085    pub(crate) fn create_two_chunked() -> (Int32Chunked, Int32Chunked) {
1086        let mut a1 = Int32Chunked::new(PlSmallStr::from_static("a"), &[1, 2, 3]);
1087        let a2 = Int32Chunked::new(PlSmallStr::from_static("a"), &[4, 5, 6]);
1088        let a3 = Int32Chunked::new(PlSmallStr::from_static("a"), &[1, 2, 3, 4, 5, 6]);
1089        a1.append(&a2).unwrap();
1090        (a1, a3)
1091    }
1092
1093    #[test]
1094    fn test_bitwise_ops() {
1095        let a = BooleanChunked::new(PlSmallStr::from_static("a"), &[true, false, false]);
1096        let b = BooleanChunked::new(
1097            PlSmallStr::from_static("b"),
1098            &[Some(true), Some(true), None],
1099        );
1100        assert_eq!(Vec::from(&a | &b), &[Some(true), Some(true), None]);
1101        assert_eq!(Vec::from(&a & &b), &[Some(true), Some(false), Some(false)]);
1102        assert_eq!(Vec::from(!b), &[Some(false), Some(false), None]);
1103    }
1104
1105    #[test]
1106    fn test_compare_chunk_diff() {
1107        let (a1, a2) = create_two_chunked();
1108
1109        assert_eq!(
1110            a1.equal(&a2).iter().collect::<Vec<_>>(),
1111            repeat_n(Some(true), 6).collect::<Vec<_>>()
1112        );
1113        assert_eq!(
1114            a2.equal(&a1).iter().collect::<Vec<_>>(),
1115            repeat_n(Some(true), 6).collect::<Vec<_>>()
1116        );
1117        assert_eq!(
1118            a1.not_equal(&a2).iter().collect::<Vec<_>>(),
1119            repeat_n(Some(false), 6).collect::<Vec<_>>()
1120        );
1121        assert_eq!(
1122            a2.not_equal(&a1).iter().collect::<Vec<_>>(),
1123            repeat_n(Some(false), 6).collect::<Vec<_>>()
1124        );
1125        assert_eq!(
1126            a1.gt(&a2).iter().collect::<Vec<_>>(),
1127            repeat_n(Some(false), 6).collect::<Vec<_>>()
1128        );
1129        assert_eq!(
1130            a2.gt(&a1).iter().collect::<Vec<_>>(),
1131            repeat_n(Some(false), 6).collect::<Vec<_>>()
1132        );
1133        assert_eq!(
1134            a1.gt_eq(&a2).iter().collect::<Vec<_>>(),
1135            repeat_n(Some(true), 6).collect::<Vec<_>>()
1136        );
1137        assert_eq!(
1138            a2.gt_eq(&a1).iter().collect::<Vec<_>>(),
1139            repeat_n(Some(true), 6).collect::<Vec<_>>()
1140        );
1141        assert_eq!(
1142            a1.lt_eq(&a2).iter().collect::<Vec<_>>(),
1143            repeat_n(Some(true), 6).collect::<Vec<_>>()
1144        );
1145        assert_eq!(
1146            a2.lt_eq(&a1).iter().collect::<Vec<_>>(),
1147            repeat_n(Some(true), 6).collect::<Vec<_>>()
1148        );
1149        assert_eq!(
1150            a1.lt(&a2).iter().collect::<Vec<_>>(),
1151            repeat_n(Some(false), 6).collect::<Vec<_>>()
1152        );
1153        assert_eq!(
1154            a2.lt(&a1).iter().collect::<Vec<_>>(),
1155            repeat_n(Some(false), 6).collect::<Vec<_>>()
1156        );
1157    }
1158
1159    #[test]
1160    fn test_equal_chunks() {
1161        let a1 = get_chunked_array();
1162        let a2 = get_chunked_array();
1163
1164        assert_eq!(
1165            a1.equal(&a2).iter().collect::<Vec<_>>(),
1166            repeat_n(Some(true), 3).collect::<Vec<_>>()
1167        );
1168        assert_eq!(
1169            a2.equal(&a1).iter().collect::<Vec<_>>(),
1170            repeat_n(Some(true), 3).collect::<Vec<_>>()
1171        );
1172        assert_eq!(
1173            a1.not_equal(&a2).iter().collect::<Vec<_>>(),
1174            repeat_n(Some(false), 3).collect::<Vec<_>>()
1175        );
1176        assert_eq!(
1177            a2.not_equal(&a1).iter().collect::<Vec<_>>(),
1178            repeat_n(Some(false), 3).collect::<Vec<_>>()
1179        );
1180        assert_eq!(
1181            a1.gt(&a2).iter().collect::<Vec<_>>(),
1182            repeat_n(Some(false), 3).collect::<Vec<_>>()
1183        );
1184        assert_eq!(
1185            a2.gt(&a1).iter().collect::<Vec<_>>(),
1186            repeat_n(Some(false), 3).collect::<Vec<_>>()
1187        );
1188        assert_eq!(
1189            a1.gt_eq(&a2).iter().collect::<Vec<_>>(),
1190            repeat_n(Some(true), 3).collect::<Vec<_>>()
1191        );
1192        assert_eq!(
1193            a2.gt_eq(&a1).iter().collect::<Vec<_>>(),
1194            repeat_n(Some(true), 3).collect::<Vec<_>>()
1195        );
1196        assert_eq!(
1197            a1.lt_eq(&a2).iter().collect::<Vec<_>>(),
1198            repeat_n(Some(true), 3).collect::<Vec<_>>()
1199        );
1200        assert_eq!(
1201            a2.lt_eq(&a1).iter().collect::<Vec<_>>(),
1202            repeat_n(Some(true), 3).collect::<Vec<_>>()
1203        );
1204        assert_eq!(
1205            a1.lt(&a2).iter().collect::<Vec<_>>(),
1206            repeat_n(Some(false), 3).collect::<Vec<_>>()
1207        );
1208        assert_eq!(
1209            a2.lt(&a1).iter().collect::<Vec<_>>(),
1210            repeat_n(Some(false), 3).collect::<Vec<_>>()
1211        );
1212    }
1213
1214    #[test]
1215    fn test_null_handling() {
1216        // assert we comply with arrows way of handling null data
1217        // we check comparison on two arrays with one chunk and verify it is equal to a differently
1218        // chunked array comparison.
1219
1220        // two same chunked arrays
1221        let a1: Int32Chunked = [Some(1), None, Some(3)].iter().copied().collect();
1222        let a2: Int32Chunked = [Some(1), Some(2), Some(3)].iter().copied().collect();
1223
1224        let mut a2_2chunks: Int32Chunked = [Some(1), Some(2)].iter().copied().collect();
1225        a2_2chunks
1226            .append(&[Some(3)].iter().copied().collect())
1227            .unwrap();
1228
1229        assert_eq!(
1230            a1.equal(&a2).iter().collect::<Vec<_>>(),
1231            a1.equal(&a2_2chunks).iter().collect::<Vec<_>>()
1232        );
1233
1234        assert_eq!(
1235            a1.not_equal(&a2).iter().collect::<Vec<_>>(),
1236            a1.not_equal(&a2_2chunks).iter().collect::<Vec<_>>()
1237        );
1238        assert_eq!(
1239            a1.not_equal(&a2).iter().collect::<Vec<_>>(),
1240            a2_2chunks.not_equal(&a1).iter().collect::<Vec<_>>()
1241        );
1242
1243        assert_eq!(
1244            a1.gt(&a2).iter().collect::<Vec<_>>(),
1245            a1.gt(&a2_2chunks).iter().collect::<Vec<_>>()
1246        );
1247        assert_eq!(
1248            a1.gt(&a2).iter().collect::<Vec<_>>(),
1249            a2_2chunks.gt(&a1).iter().collect::<Vec<_>>()
1250        );
1251
1252        assert_eq!(
1253            a1.gt_eq(&a2).iter().collect::<Vec<_>>(),
1254            a1.gt_eq(&a2_2chunks).iter().collect::<Vec<_>>()
1255        );
1256        assert_eq!(
1257            a1.gt_eq(&a2).iter().collect::<Vec<_>>(),
1258            a2_2chunks.gt_eq(&a1).iter().collect::<Vec<_>>()
1259        );
1260
1261        assert_eq!(
1262            a1.lt_eq(&a2).iter().collect::<Vec<_>>(),
1263            a1.lt_eq(&a2_2chunks).iter().collect::<Vec<_>>()
1264        );
1265        assert_eq!(
1266            a1.lt_eq(&a2).iter().collect::<Vec<_>>(),
1267            a2_2chunks.lt_eq(&a1).iter().collect::<Vec<_>>()
1268        );
1269
1270        assert_eq!(
1271            a1.lt(&a2).iter().collect::<Vec<_>>(),
1272            a1.lt(&a2_2chunks).iter().collect::<Vec<_>>()
1273        );
1274        assert_eq!(
1275            a1.lt(&a2).iter().collect::<Vec<_>>(),
1276            a2_2chunks.lt(&a1).iter().collect::<Vec<_>>()
1277        );
1278    }
1279
1280    #[test]
1281    fn test_left_right() {
1282        // This failed with arrow comparisons.
1283        // sliced
1284        let a1: Int32Chunked = [Some(1), Some(2)].iter().copied().collect();
1285        let a1 = a1.slice(1, 1);
1286        let a2: Int32Chunked = [Some(2)].iter().copied().collect();
1287        assert_eq!(a1.equal(&a2).sum(), a2.equal(&a1).sum());
1288        assert_eq!(a1.not_equal(&a2).sum(), a2.not_equal(&a1).sum());
1289        assert_eq!(a1.gt(&a2).sum(), a2.gt(&a1).sum());
1290        assert_eq!(a1.lt(&a2).sum(), a2.lt(&a1).sum());
1291        assert_eq!(a1.lt_eq(&a2).sum(), a2.lt_eq(&a1).sum());
1292        assert_eq!(a1.gt_eq(&a2).sum(), a2.gt_eq(&a1).sum());
1293
1294        let a1: StringChunked = ["a", "b"].iter().copied().collect();
1295        let a1 = a1.slice(1, 1);
1296        let a2: StringChunked = ["b"].iter().copied().collect();
1297        assert_eq!(a1.equal(&a2).sum(), a2.equal(&a1).sum());
1298        assert_eq!(a1.not_equal(&a2).sum(), a2.not_equal(&a1).sum());
1299        assert_eq!(a1.gt(&a2).sum(), a2.gt(&a1).sum());
1300        assert_eq!(a1.lt(&a2).sum(), a2.lt(&a1).sum());
1301        assert_eq!(a1.lt_eq(&a2).sum(), a2.lt_eq(&a1).sum());
1302        assert_eq!(a1.gt_eq(&a2).sum(), a2.gt_eq(&a1).sum());
1303    }
1304
1305    #[test]
1306    fn test_kleene() {
1307        let a = BooleanChunked::new(PlSmallStr::EMPTY, &[Some(true), Some(false), None]);
1308        let trues = BooleanChunked::from_slice(PlSmallStr::EMPTY, &[true, true, true]);
1309        let falses = BooleanChunked::from_slice(PlSmallStr::EMPTY, &[false, false, false]);
1310
1311        let c = &a | &trues;
1312        assert_eq!(Vec::from(&c), &[Some(true), Some(true), Some(true)]);
1313
1314        let c = &a | &falses;
1315        assert_eq!(Vec::from(&c), &[Some(true), Some(false), None])
1316    }
1317
1318    #[test]
1319    fn list_broadcasting_lists() {
1320        let s_el = Series::new(PlSmallStr::EMPTY, &[1, 2, 3]);
1321        let s_lhs = Series::new(PlSmallStr::EMPTY, &[s_el.clone(), s_el.clone()]);
1322        let s_rhs = Series::new(PlSmallStr::EMPTY, std::slice::from_ref(&s_el));
1323
1324        let result = s_lhs.list().unwrap().equal(s_rhs.list().unwrap());
1325        assert_eq!(result.len(), 2);
1326        assert!(result.all());
1327    }
1328
1329    #[test]
1330    fn test_broadcasting_bools() {
1331        let a = BooleanChunked::from_slice(PlSmallStr::EMPTY, &[true, false, true]);
1332        let true_ = BooleanChunked::from_slice(PlSmallStr::EMPTY, &[true]);
1333        let false_ = BooleanChunked::from_slice(PlSmallStr::EMPTY, &[false]);
1334
1335        let out = a.equal(&true_);
1336        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(true)]);
1337        let out = true_.equal(&a);
1338        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(true)]);
1339        let out = a.equal(&false_);
1340        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(false)]);
1341        let out = false_.equal(&a);
1342        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(false)]);
1343
1344        let out = a.not_equal(&true_);
1345        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(false)]);
1346        let out = true_.not_equal(&a);
1347        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(false)]);
1348        let out = a.not_equal(&false_);
1349        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(true)]);
1350        let out = false_.not_equal(&a);
1351        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(true)]);
1352
1353        let out = a.gt(&true_);
1354        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(false)]);
1355        let out = true_.gt(&a);
1356        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(false)]);
1357        let out = a.gt(&false_);
1358        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(true)]);
1359        let out = false_.gt(&a);
1360        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(false)]);
1361
1362        let out = a.gt_eq(&true_);
1363        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(true)]);
1364        let out = true_.gt_eq(&a);
1365        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(true)]);
1366        let out = a.gt_eq(&false_);
1367        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(true)]);
1368        let out = false_.gt_eq(&a);
1369        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(false)]);
1370
1371        let out = a.lt(&true_);
1372        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(false)]);
1373        let out = true_.lt(&a);
1374        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(false)]);
1375        let out = a.lt(&false_);
1376        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(false)]);
1377        let out = false_.lt(&a);
1378        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(true)]);
1379
1380        let out = a.lt_eq(&true_);
1381        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(true)]);
1382        let out = true_.lt_eq(&a);
1383        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(true)]);
1384        let out = a.lt_eq(&false_);
1385        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(false)]);
1386        let out = false_.lt_eq(&a);
1387        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(true)]);
1388
1389        let a =
1390            BooleanChunked::from_slice_options(PlSmallStr::EMPTY, &[Some(true), Some(false), None]);
1391        let all_true = BooleanChunked::from_slice(PlSmallStr::EMPTY, &[true, true, true]);
1392        let all_false = BooleanChunked::from_slice(PlSmallStr::EMPTY, &[false, false, false]);
1393        let out = a.equal(&true_);
1394        assert_eq!(Vec::from(&out), &[Some(true), Some(false), None]);
1395        let out = a.not_equal(&true_);
1396        assert_eq!(Vec::from(&out), &[Some(false), Some(true), None]);
1397
1398        let out = a.equal(&all_true);
1399        assert_eq!(Vec::from(&out), &[Some(true), Some(false), None]);
1400        let out = a.not_equal(&all_true);
1401        assert_eq!(Vec::from(&out), &[Some(false), Some(true), None]);
1402        let out = a.equal(&false_);
1403        assert_eq!(Vec::from(&out), &[Some(false), Some(true), None]);
1404        let out = a.not_equal(&false_);
1405        assert_eq!(Vec::from(&out), &[Some(true), Some(false), None]);
1406        let out = a.equal(&all_false);
1407        assert_eq!(Vec::from(&out), &[Some(false), Some(true), None]);
1408        let out = a.not_equal(&all_false);
1409        assert_eq!(Vec::from(&out), &[Some(true), Some(false), None]);
1410    }
1411
1412    #[test]
1413    fn test_broadcasting_numeric() {
1414        let a = Int32Chunked::from_slice(PlSmallStr::EMPTY, &[1, 2, 3]);
1415        let one = Int32Chunked::from_slice(PlSmallStr::EMPTY, &[1]);
1416        let three = Int32Chunked::from_slice(PlSmallStr::EMPTY, &[3]);
1417
1418        let out = a.equal(&one);
1419        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(false)]);
1420        let out = one.equal(&a);
1421        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(false)]);
1422        let out = a.equal(&three);
1423        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(true)]);
1424        let out = three.equal(&a);
1425        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(true)]);
1426
1427        let out = a.not_equal(&one);
1428        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(true)]);
1429        let out = one.not_equal(&a);
1430        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(true)]);
1431        let out = a.not_equal(&three);
1432        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(false)]);
1433        let out = three.not_equal(&a);
1434        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(false)]);
1435
1436        let out = a.gt(&one);
1437        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(true)]);
1438        let out = one.gt(&a);
1439        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(false)]);
1440        let out = a.gt(&three);
1441        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(false)]);
1442        let out = three.gt(&a);
1443        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(false)]);
1444
1445        let out = a.lt(&one);
1446        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(false)]);
1447        let out = one.lt(&a);
1448        assert_eq!(Vec::from(&out), &[Some(false), Some(true), Some(true)]);
1449        let out = a.lt(&three);
1450        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(false)]);
1451        let out = three.lt(&a);
1452        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(false)]);
1453
1454        let out = a.gt_eq(&one);
1455        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(true)]);
1456        let out = one.gt_eq(&a);
1457        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(false)]);
1458        let out = a.gt_eq(&three);
1459        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(true)]);
1460        let out = three.gt_eq(&a);
1461        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(true)]);
1462
1463        let out = a.lt_eq(&one);
1464        assert_eq!(Vec::from(&out), &[Some(true), Some(false), Some(false)]);
1465        let out = one.lt_eq(&a);
1466        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(true)]);
1467        let out = a.lt_eq(&three);
1468        assert_eq!(Vec::from(&out), &[Some(true), Some(true), Some(true)]);
1469        let out = three.lt_eq(&a);
1470        assert_eq!(Vec::from(&out), &[Some(false), Some(false), Some(true)]);
1471    }
1472}