1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 use crate::array::{data::count_nulls, ArrayData};
19 use crate::buffer::Buffer;
20 use crate::util::bit_util::get_bit;
21 
22 use super::utils::{equal_bits, equal_len};
23 
boolean_equal( lhs: &ArrayData, rhs: &ArrayData, lhs_nulls: Option<&Buffer>, rhs_nulls: Option<&Buffer>, mut lhs_start: usize, mut rhs_start: usize, mut len: usize, ) -> bool24 pub(super) fn boolean_equal(
25     lhs: &ArrayData,
26     rhs: &ArrayData,
27     lhs_nulls: Option<&Buffer>,
28     rhs_nulls: Option<&Buffer>,
29     mut lhs_start: usize,
30     mut rhs_start: usize,
31     mut len: usize,
32 ) -> bool {
33     let lhs_values = lhs.buffers()[0].as_slice();
34     let rhs_values = rhs.buffers()[0].as_slice();
35 
36     let lhs_null_count = count_nulls(lhs_nulls, lhs_start, len);
37     let rhs_null_count = count_nulls(rhs_nulls, rhs_start, len);
38 
39     if lhs_null_count == 0 && rhs_null_count == 0 {
40         // Optimize performance for starting offset at u8 boundary.
41         if lhs_start % 8 == 0 && rhs_start % 8 == 0 {
42             let quot = len / 8;
43             if quot > 0
44                 && !equal_len(
45                     lhs_values,
46                     rhs_values,
47                     lhs_start / 8 + lhs.offset(),
48                     rhs_start / 8 + rhs.offset(),
49                     quot,
50                 )
51             {
52                 return false;
53             }
54 
55             // Calculate for suffix bits.
56             let rem = len % 8;
57             if rem == 0 {
58                 return true;
59             } else {
60                 let aligned_bits = len - rem;
61                 lhs_start += aligned_bits;
62                 rhs_start += aligned_bits;
63                 len = rem
64             }
65         }
66 
67         equal_bits(
68             lhs_values,
69             rhs_values,
70             lhs_start + lhs.offset(),
71             rhs_start + rhs.offset(),
72             len,
73         )
74     } else {
75         // get a ref of the null buffer bytes, to use in testing for nullness
76         let lhs_null_bytes = lhs_nulls.as_ref().unwrap().as_slice();
77         let rhs_null_bytes = rhs_nulls.as_ref().unwrap().as_slice();
78 
79         let lhs_start = lhs.offset() + lhs_start;
80         let rhs_start = rhs.offset() + rhs_start;
81 
82         (0..len).all(|i| {
83             let lhs_pos = lhs_start + i;
84             let rhs_pos = rhs_start + i;
85             let lhs_is_null = !get_bit(lhs_null_bytes, lhs_pos);
86             let rhs_is_null = !get_bit(rhs_null_bytes, rhs_pos);
87 
88             lhs_is_null
89                 || (lhs_is_null == rhs_is_null)
90                     && equal_bits(lhs_values, rhs_values, lhs_pos, rhs_pos, 1)
91         })
92     }
93 }
94