arrow_data/equal/boolean.rs
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
18use crate::bit_iterator::BitIndexIterator;
19use crate::data::{contains_nulls, ArrayData};
20use arrow_buffer::bit_util::get_bit;
21
22use super::utils::{equal_bits, equal_len};
23
24/// Returns true if the value data for the arrays is equal, assuming the null masks have
25/// already been checked for equality
26pub(super) fn boolean_equal(
27 lhs: &ArrayData,
28 rhs: &ArrayData,
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 contains_nulls = contains_nulls(lhs.nulls(), lhs_start, len);
37
38 if !contains_nulls {
39 // Optimize performance for starting offset at u8 boundary.
40 if lhs_start % 8 == 0
41 && rhs_start % 8 == 0
42 && lhs.offset() % 8 == 0
43 && rhs.offset() % 8 == 0
44 {
45 let quot = len / 8;
46 if quot > 0
47 && !equal_len(
48 lhs_values,
49 rhs_values,
50 lhs_start / 8 + lhs.offset() / 8,
51 rhs_start / 8 + rhs.offset() / 8,
52 quot,
53 )
54 {
55 return false;
56 }
57
58 // Calculate for suffix bits.
59 let rem = len % 8;
60 if rem == 0 {
61 return true;
62 } else {
63 let aligned_bits = len - rem;
64 lhs_start += aligned_bits;
65 rhs_start += aligned_bits;
66 len = rem
67 }
68 }
69
70 equal_bits(
71 lhs_values,
72 rhs_values,
73 lhs_start + lhs.offset(),
74 rhs_start + rhs.offset(),
75 len,
76 )
77 } else {
78 // get a ref of the null buffer bytes, to use in testing for nullness
79 let lhs_nulls = lhs.nulls().unwrap();
80
81 BitIndexIterator::new(lhs_nulls.validity(), lhs_start + lhs_nulls.offset(), len).all(|i| {
82 let lhs_pos = lhs_start + lhs.offset() + i;
83 let rhs_pos = rhs_start + rhs.offset() + i;
84 get_bit(lhs_values, lhs_pos) == get_bit(rhs_values, rhs_pos)
85 })
86 }
87}