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