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.
1718use crate::data::{contains_nulls, ArrayData};
19use arrow_buffer::ArrowNativeType;
20use num::Integer;
2122use super::utils::equal_len;
2324fn offset_value_equal<T: ArrowNativeType + Integer>(
25 lhs_values: &[u8],
26 rhs_values: &[u8],
27 lhs_offsets: &[T],
28 rhs_offsets: &[T],
29 lhs_pos: usize,
30 rhs_pos: usize,
31 len: usize,
32) -> bool {
33let lhs_start = lhs_offsets[lhs_pos].as_usize();
34let rhs_start = rhs_offsets[rhs_pos].as_usize();
35let lhs_len = (lhs_offsets[lhs_pos + len] - lhs_offsets[lhs_pos])
36 .to_usize()
37 .unwrap();
38let rhs_len = (rhs_offsets[rhs_pos + len] - rhs_offsets[rhs_pos])
39 .to_usize()
40 .unwrap();
4142if lhs_len == 0 && rhs_len == 0 {
43return true;
44 }
4546 lhs_len == rhs_len && equal_len(lhs_values, rhs_values, lhs_start, rhs_start, lhs_len)
47}
4849pub(super) fn variable_sized_equal<T: ArrowNativeType + Integer>(
50 lhs: &ArrayData,
51 rhs: &ArrayData,
52 lhs_start: usize,
53 rhs_start: usize,
54 len: usize,
55) -> bool {
56let lhs_offsets = lhs.buffer::<T>(0);
57let rhs_offsets = rhs.buffer::<T>(0);
5859// the offsets of the `ArrayData` are ignored as they are only applied to the offset buffer.
60let lhs_values = lhs.buffers()[1].as_slice();
61let rhs_values = rhs.buffers()[1].as_slice();
6263// Only checking one null mask here because by the time the control flow reaches
64 // this point, the equality of the two masks would have already been verified.
65if !contains_nulls(lhs.nulls(), lhs_start, len) {
66 offset_value_equal(
67 lhs_values,
68 rhs_values,
69 lhs_offsets,
70 rhs_offsets,
71 lhs_start,
72 rhs_start,
73 len,
74 )
75 } else {
76 (0..len).all(|i| {
77let lhs_pos = lhs_start + i;
78let rhs_pos = rhs_start + i;
7980// the null bits can still be `None`, indicating that the value is valid.
81let lhs_is_null = lhs.nulls().map(|v| v.is_null(lhs_pos)).unwrap_or_default();
82let rhs_is_null = rhs.nulls().map(|v| v.is_null(rhs_pos)).unwrap_or_default();
8384 lhs_is_null
85 || (lhs_is_null == rhs_is_null)
86 && offset_value_equal(
87 lhs_values,
88 rhs_values,
89 lhs_offsets,
90 rhs_offsets,
91 lhs_pos,
92 rhs_pos,
931,
94 )
95 })
96 }
97}