arrow_data/equal/
run.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;
19
20use super::equal_range;
21
22/// The current implementation of comparison of run array support physical comparison.
23/// Comparing run encoded array based on logical indices (`lhs_start`, `rhs_start`) will
24/// be time consuming as converting from logical index to physical index cannot be done
25/// in constant time. The current comparison compares the underlying physical arrays.
26pub(super) fn run_equal(
27    lhs: &ArrayData,
28    rhs: &ArrayData,
29    lhs_start: usize,
30    rhs_start: usize,
31    len: usize,
32) -> bool {
33    if lhs_start != 0
34        || rhs_start != 0
35        || (lhs.len() != len && rhs.len() != len)
36        || lhs.offset() > 0
37        || rhs.offset() > 0
38    {
39        unimplemented!("Logical comparison for run array not supported.")
40    }
41
42    if lhs.len() != rhs.len() {
43        return false;
44    }
45
46    let lhs_child_data = lhs.child_data();
47    let lhs_run_ends_array = &lhs_child_data[0];
48    let lhs_values_array = &lhs_child_data[1];
49
50    let rhs_child_data = rhs.child_data();
51    let rhs_run_ends_array = &rhs_child_data[0];
52    let rhs_values_array = &rhs_child_data[1];
53
54    if lhs_run_ends_array.len() != rhs_run_ends_array.len() {
55        return false;
56    }
57
58    if lhs_values_array.len() != rhs_values_array.len() {
59        return false;
60    }
61
62    // check run ends array are equal. The length of the physical array
63    // is used to validate the child arrays.
64    let run_ends_equal = equal_range(
65        lhs_run_ends_array,
66        rhs_run_ends_array,
67        lhs_start,
68        rhs_start,
69        lhs_run_ends_array.len(),
70    );
71
72    // if run ends array are not the same return early without validating
73    // values array.
74    if !run_ends_equal {
75        return false;
76    }
77
78    // check values array are equal
79    equal_range(
80        lhs_values_array,
81        rhs_values_array,
82        lhs_start,
83        rhs_start,
84        rhs_values_array.len(),
85    )
86}