arrow_data/equal/fixed_binary.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::BitSliceIterator;
19use crate::contains_nulls;
20use crate::data::{ArrayData, get_fixed_size_binary_width};
21use crate::equal::primitive::NULL_SLICES_SELECTIVITY_THRESHOLD;
22
23use super::utils::equal_len;
24
25pub(super) fn fixed_binary_equal(
26 lhs: &ArrayData,
27 rhs: &ArrayData,
28 lhs_start: usize,
29 rhs_start: usize,
30 len: usize,
31) -> bool {
32 let size = get_fixed_size_binary_width(lhs.data_type());
33
34 let lhs_values = &lhs.buffers()[0].as_slice()[lhs.offset() * size..];
35 let rhs_values = &rhs.buffers()[0].as_slice()[rhs.offset() * size..];
36
37 // Only checking one null mask here because by the time the control flow reaches
38 // this point, the equality of the two masks would have already been verified.
39 if !contains_nulls(lhs.nulls(), lhs_start, len) {
40 equal_len(
41 lhs_values,
42 rhs_values,
43 size * lhs_start,
44 size * rhs_start,
45 size * len,
46 )
47 } else {
48 let selectivity_frac = lhs.null_count() as f64 / lhs.len() as f64;
49
50 if selectivity_frac >= NULL_SLICES_SELECTIVITY_THRESHOLD {
51 // get a ref of the null buffer bytes, to use in testing for nullness
52 let lhs_nulls = lhs.nulls().unwrap();
53 let rhs_nulls = rhs.nulls().unwrap();
54 // with nulls, we need to compare item by item whenever it is not null
55 (0..len).all(|i| {
56 let lhs_pos = lhs_start + i;
57 let rhs_pos = rhs_start + i;
58
59 let lhs_is_null = lhs_nulls.is_null(lhs_pos);
60 let rhs_is_null = rhs_nulls.is_null(rhs_pos);
61
62 lhs_is_null
63 || (lhs_is_null == rhs_is_null)
64 && equal_len(
65 lhs_values,
66 rhs_values,
67 lhs_pos * size,
68 rhs_pos * size,
69 size, // 1 * size since we are comparing a single entry
70 )
71 })
72 } else {
73 let lhs_nulls = lhs.nulls().unwrap();
74 let lhs_slices_iter =
75 BitSliceIterator::new(lhs_nulls.validity(), lhs_start + lhs_nulls.offset(), len);
76 let rhs_nulls = rhs.nulls().unwrap();
77 let rhs_slices_iter =
78 BitSliceIterator::new(rhs_nulls.validity(), rhs_start + rhs_nulls.offset(), len);
79
80 lhs_slices_iter
81 .zip(rhs_slices_iter)
82 .all(|((l_start, l_end), (r_start, r_end))| {
83 l_start == r_start
84 && l_end == r_end
85 && equal_len(
86 lhs_values,
87 rhs_values,
88 (lhs_start + l_start) * size,
89 (rhs_start + r_start) * size,
90 (l_end - l_start) * size,
91 )
92 })
93 }
94 }
95}