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 // get a ref of the null buffer bytes, to use in testing for nullness
51 let lhs_nulls = lhs.nulls().unwrap();
52 let rhs_nulls = rhs.nulls().unwrap();
53
54 if selectivity_frac >= NULL_SLICES_SELECTIVITY_THRESHOLD {
55 // with nulls, we need to compare item by item whenever it is not null
56 (0..len).all(|i| {
57 let lhs_pos = lhs_start + i;
58 let rhs_pos = rhs_start + i;
59
60 let lhs_is_null = lhs_nulls.is_null(lhs_pos);
61 let rhs_is_null = rhs_nulls.is_null(rhs_pos);
62
63 lhs_is_null
64 || (lhs_is_null == rhs_is_null)
65 && equal_len(
66 lhs_values,
67 rhs_values,
68 lhs_pos * size,
69 rhs_pos * size,
70 size, // 1 * size since we are comparing a single entry
71 )
72 })
73 } else {
74 let lhs_slices_iter =
75 BitSliceIterator::new(lhs_nulls.validity(), lhs_start + lhs_nulls.offset(), len);
76 let rhs_slices_iter =
77 BitSliceIterator::new(rhs_nulls.validity(), rhs_start + rhs_nulls.offset(), len);
78
79 lhs_slices_iter
80 .zip(rhs_slices_iter)
81 .all(|((l_start, l_end), (r_start, r_end))| {
82 l_start == r_start
83 && l_end == r_end
84 && equal_len(
85 lhs_values,
86 rhs_values,
87 (lhs_start + l_start) * size,
88 (rhs_start + r_start) * size,
89 (l_end - l_start) * size,
90 )
91 })
92 }
93 }
94}