Coverage Report

Created: 2025-08-26 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/Users/andrewlamb/Software/arrow-rs/arrow-data/src/equal/union.rs
Line
Count
Source
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
18
use crate::data::ArrayData;
19
use arrow_schema::{DataType, UnionFields, UnionMode};
20
21
use super::equal_range;
22
23
#[allow(clippy::too_many_arguments)]
24
0
fn equal_dense(
25
0
    lhs: &ArrayData,
26
0
    rhs: &ArrayData,
27
0
    lhs_type_ids: &[i8],
28
0
    rhs_type_ids: &[i8],
29
0
    lhs_offsets: &[i32],
30
0
    rhs_offsets: &[i32],
31
0
    lhs_fields: &UnionFields,
32
0
    rhs_fields: &UnionFields,
33
0
) -> bool {
34
0
    let offsets = lhs_offsets.iter().zip(rhs_offsets.iter());
35
36
0
    lhs_type_ids
37
0
        .iter()
38
0
        .zip(rhs_type_ids.iter())
39
0
        .zip(offsets)
40
0
        .all(|((l_type_id, r_type_id), (l_offset, r_offset))| {
41
0
            let lhs_child_index = lhs_fields
42
0
                .iter()
43
0
                .position(|(r, _)| r == *l_type_id)
44
0
                .unwrap();
45
0
            let rhs_child_index = rhs_fields
46
0
                .iter()
47
0
                .position(|(r, _)| r == *r_type_id)
48
0
                .unwrap();
49
0
            let lhs_values = &lhs.child_data()[lhs_child_index];
50
0
            let rhs_values = &rhs.child_data()[rhs_child_index];
51
52
0
            equal_range(
53
0
                lhs_values,
54
0
                rhs_values,
55
0
                *l_offset as usize,
56
0
                *r_offset as usize,
57
                1,
58
            )
59
0
        })
60
0
}
61
62
0
fn equal_sparse(
63
0
    lhs: &ArrayData,
64
0
    rhs: &ArrayData,
65
0
    lhs_start: usize,
66
0
    rhs_start: usize,
67
0
    len: usize,
68
0
) -> bool {
69
0
    lhs.child_data()
70
0
        .iter()
71
0
        .zip(rhs.child_data())
72
0
        .all(|(lhs_values, rhs_values)| {
73
0
            equal_range(
74
0
                lhs_values,
75
0
                rhs_values,
76
0
                lhs_start + lhs.offset(),
77
0
                rhs_start + rhs.offset(),
78
0
                len,
79
            )
80
0
        })
81
0
}
82
83
0
pub(super) fn union_equal(
84
0
    lhs: &ArrayData,
85
0
    rhs: &ArrayData,
86
0
    lhs_start: usize,
87
0
    rhs_start: usize,
88
0
    len: usize,
89
0
) -> bool {
90
0
    let lhs_type_ids = lhs.buffer::<i8>(0);
91
0
    let rhs_type_ids = rhs.buffer::<i8>(0);
92
93
0
    let lhs_type_id_range = &lhs_type_ids[lhs_start..lhs_start + len];
94
0
    let rhs_type_id_range = &rhs_type_ids[rhs_start..rhs_start + len];
95
96
0
    match (lhs.data_type(), rhs.data_type()) {
97
        (
98
0
            DataType::Union(lhs_fields, UnionMode::Dense),
99
0
            DataType::Union(rhs_fields, UnionMode::Dense),
100
        ) => {
101
0
            let lhs_offsets = lhs.buffer::<i32>(1);
102
0
            let rhs_offsets = rhs.buffer::<i32>(1);
103
104
0
            let lhs_offsets_range = &lhs_offsets[lhs_start..lhs_start + len];
105
0
            let rhs_offsets_range = &rhs_offsets[rhs_start..rhs_start + len];
106
107
0
            lhs_type_id_range == rhs_type_id_range
108
0
                && equal_dense(
109
0
                    lhs,
110
0
                    rhs,
111
0
                    lhs_type_id_range,
112
0
                    rhs_type_id_range,
113
0
                    lhs_offsets_range,
114
0
                    rhs_offsets_range,
115
0
                    lhs_fields,
116
0
                    rhs_fields,
117
                )
118
        }
119
        (DataType::Union(_, UnionMode::Sparse), DataType::Union(_, UnionMode::Sparse)) => {
120
0
            lhs_type_id_range == rhs_type_id_range
121
0
                && equal_sparse(lhs, rhs, lhs_start, rhs_start, len)
122
        }
123
0
        _ => unimplemented!(
124
            "Logical equality not yet implemented between dense and sparse union arrays"
125
        ),
126
    }
127
0
}