Skip to main content

arrow_data/equal/
list_view.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::ArrayData;
19use crate::data::count_nulls;
20use crate::equal::equal_range;
21use arrow_buffer::ArrowNativeType;
22use num_integer::Integer;
23
24pub(super) fn list_view_equal<T: ArrowNativeType + Integer>(
25    lhs: &ArrayData,
26    rhs: &ArrayData,
27    lhs_start: usize,
28    rhs_start: usize,
29    len: usize,
30) -> bool {
31    let lhs_offsets = lhs.buffer::<T>(0);
32    let lhs_sizes = lhs.buffer::<T>(1);
33
34    let rhs_offsets = rhs.buffer::<T>(0);
35    let rhs_sizes = rhs.buffer::<T>(1);
36
37    let lhs_data = &lhs.child_data()[0];
38    let rhs_data = &rhs.child_data()[0];
39
40    let lhs_null_count = count_nulls(lhs.nulls(), lhs_start, len);
41    let rhs_null_count = count_nulls(rhs.nulls(), rhs_start, len);
42
43    if lhs_null_count != rhs_null_count {
44        return false;
45    }
46
47    // All four slices are `len` long
48    let lhs_range_sizes = &lhs_sizes[lhs_start..lhs_start + len];
49    let rhs_range_sizes = &rhs_sizes[rhs_start..rhs_start + len];
50    let lhs_range_offsets = &lhs_offsets[lhs_start..lhs_start + len];
51    let rhs_range_offsets = &rhs_offsets[rhs_start..rhs_start + len];
52
53    if lhs_null_count == 0 {
54        // non-null pathway: all sizes must be equal, and all values must be equal
55        if lhs_range_sizes != rhs_range_sizes {
56            return false;
57        }
58
59        // Check values for equality
60        for ((&lhs_offset, &rhs_offset), &size) in lhs_range_offsets
61            .iter()
62            .zip(rhs_range_offsets)
63            .zip(lhs_range_sizes)
64        {
65            let lhs_offset = lhs_offset.to_usize().unwrap();
66            let rhs_offset = rhs_offset.to_usize().unwrap();
67            let size = size.to_usize().unwrap();
68
69            if !equal_range(lhs_data, rhs_data, lhs_offset, rhs_offset, size) {
70                return false;
71            }
72        }
73    } else {
74        // Need to integrate validity check in the inner loop.
75        let lhs_nulls = lhs.nulls().unwrap().slice(lhs_start, len);
76        let rhs_nulls = rhs.nulls().unwrap().slice(rhs_start, len);
77
78        // Check values for equality, with null checking
79        for (index, ((&lhs_offset, &rhs_offset), (&lhs_size, &rhs_size))) in lhs_range_offsets
80            .iter()
81            .zip(rhs_range_offsets)
82            .zip(lhs_range_sizes.iter().zip(rhs_range_sizes))
83            .enumerate()
84        {
85            let lhs_is_null = lhs_nulls.is_null(index);
86            let rhs_is_null = rhs_nulls.is_null(index);
87
88            if lhs_is_null != rhs_is_null {
89                return false;
90            }
91
92            // Null rows are equal whatever their sizes and values
93            if lhs_is_null {
94                continue;
95            }
96
97            if lhs_size != rhs_size {
98                return false;
99            }
100
101            let lhs_offset = lhs_offset.to_usize().unwrap();
102            let rhs_offset = rhs_offset.to_usize().unwrap();
103            let size = lhs_size.to_usize().unwrap();
104
105            if !equal_range(lhs_data, rhs_data, lhs_offset, rhs_offset, size) {
106                return false;
107            }
108        }
109    }
110
111    true
112}