Skip to main content

arrow_data/equal/
structure.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::{ArrayData, contains_nulls};
19
20use super::equal_range;
21
22/// Compares the values of two [ArrayData] starting at `lhs_start` and `rhs_start` respectively
23/// for `len` slots.
24fn equal_child_values(
25    lhs: &ArrayData,
26    rhs: &ArrayData,
27    lhs_start: usize,
28    rhs_start: usize,
29    len: usize,
30) -> bool {
31    let lhs_start = lhs_start + lhs.offset();
32    let rhs_start = rhs_start + rhs.offset();
33
34    lhs.child_data()
35        .iter()
36        .zip(rhs.child_data())
37        .all(|(lhs_values, rhs_values)| {
38            equal_range(lhs_values, rhs_values, lhs_start, rhs_start, len)
39        })
40}
41
42pub(super) fn struct_equal(
43    lhs: &ArrayData,
44    rhs: &ArrayData,
45    lhs_start: usize,
46    rhs_start: usize,
47    len: usize,
48) -> bool {
49    // Only checking one null mask here because by the time the control flow reaches
50    // this point, the equality of the two masks would have already been verified.
51    if !contains_nulls(lhs.nulls(), lhs_start, len) {
52        equal_child_values(lhs, rhs, lhs_start, rhs_start, len)
53    } else {
54        // get a ref of the null buffer bytes, to use in testing for nullness
55        let lhs_nulls = lhs.nulls().unwrap();
56        let rhs_nulls = rhs.nulls().unwrap();
57        // with nulls, we need to compare item by item whenever it is not null
58        (0..len).all(|i| {
59            let lhs_pos = lhs_start + i;
60            let rhs_pos = rhs_start + i;
61            // if both struct and child had no null buffers,
62            let lhs_is_null = lhs_nulls.is_null(lhs_pos);
63            let rhs_is_null = rhs_nulls.is_null(rhs_pos);
64
65            if lhs_is_null != rhs_is_null {
66                return false;
67            }
68
69            lhs_is_null || equal_child_values(lhs, rhs, lhs_pos, rhs_pos, 1)
70        })
71    }
72}