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