arrow_data/equal/
variable_size.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::data::{contains_nulls, ArrayData};
19use arrow_buffer::ArrowNativeType;
20use num::Integer;
21
22use super::utils::equal_len;
23
24fn 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 {
33    let lhs_start = lhs_offsets[lhs_pos].as_usize();
34    let rhs_start = rhs_offsets[rhs_pos].as_usize();
35    let lhs_len = (lhs_offsets[lhs_pos + len] - lhs_offsets[lhs_pos])
36        .to_usize()
37        .unwrap();
38    let rhs_len = (rhs_offsets[rhs_pos + len] - rhs_offsets[rhs_pos])
39        .to_usize()
40        .unwrap();
41
42    if lhs_len == 0 && rhs_len == 0 {
43        return true;
44    }
45
46    lhs_len == rhs_len && equal_len(lhs_values, rhs_values, lhs_start, rhs_start, lhs_len)
47}
48
49pub(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 {
56    let lhs_offsets = lhs.buffer::<T>(0);
57    let rhs_offsets = rhs.buffer::<T>(0);
58
59    // the offsets of the `ArrayData` are ignored as they are only applied to the offset buffer.
60    let lhs_values = lhs.buffers()[1].as_slice();
61    let rhs_values = rhs.buffers()[1].as_slice();
62
63    // 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.
65    if !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| {
77            let lhs_pos = lhs_start + i;
78            let rhs_pos = rhs_start + i;
79
80            // the null bits can still be `None`, indicating that the value is valid.
81            let lhs_is_null = lhs.nulls().map(|v| v.is_null(lhs_pos)).unwrap_or_default();
82            let rhs_is_null = rhs.nulls().map(|v| v.is_null(rhs_pos)).unwrap_or_default();
83
84            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,
93                        1,
94                    )
95        })
96    }
97}