Skip to main content

arrow_data/equal/
union.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use crate::data::ArrayData;
6use arrow_schema::{DataType, UnionFields, UnionMode};
7
8use super::equal_range;
9
10#[expect(clippy::too_many_arguments)]
11fn equal_dense(
12    lhs: &ArrayData,
13    rhs: &ArrayData,
14    lhs_type_ids: &[i8],
15    rhs_type_ids: &[i8],
16    lhs_offsets: &[i32],
17    rhs_offsets: &[i32],
18    lhs_fields: &UnionFields,
19    rhs_fields: &UnionFields,
20) -> bool {
21    let offsets = lhs_offsets.iter().zip(rhs_offsets.iter());
22
23    lhs_type_ids
24        .iter()
25        .zip(rhs_type_ids.iter())
26        .zip(offsets)
27        .all(|((l_type_id, r_type_id), (l_offset, r_offset))| {
28            let lhs_child_index = lhs_fields
29                .iter()
30                .position(|(r, _)| r == *l_type_id)
31                .unwrap();
32            let rhs_child_index = rhs_fields
33                .iter()
34                .position(|(r, _)| r == *r_type_id)
35                .unwrap();
36            let lhs_values = &lhs.child_data()[lhs_child_index];
37            let rhs_values = &rhs.child_data()[rhs_child_index];
38
39            equal_range(
40                lhs_values,
41                rhs_values,
42                *l_offset as usize,
43                *r_offset as usize,
44                1,
45            )
46        })
47}
48
49/// Compares `type_ids.len()` slots of two sparse unions whose type ids over that
50/// range are already known to be equal.
51///
52/// Only the child selected by each slot's type id contributes to the logical
53/// value of a sparse union; the values held by the other children at that slot
54/// are arbitrary and must not affect equality. Consecutive slots that select the
55/// same child are compared as a single range of that child.
56fn equal_sparse(
57    lhs: &ArrayData,
58    rhs: &ArrayData,
59    type_ids: &[i8],
60    fields: &UnionFields,
61    lhs_start: usize,
62    rhs_start: usize,
63) -> bool {
64    let mut run_start = 0;
65    type_ids.chunk_by(|a, b| a == b).all(|run| {
66        let start = run_start;
67        run_start += run.len();
68
69        // Both sides share the same `UnionFields` (checked when comparing the
70        // data types), so the type id maps to the same child index on each side.
71        let child_index = fields.iter().position(|(id, _)| id == run[0]).unwrap();
72        equal_range(
73            &lhs.child_data()[child_index],
74            &rhs.child_data()[child_index],
75            lhs_start + lhs.offset() + start,
76            rhs_start + rhs.offset() + start,
77            run.len(),
78        )
79    })
80}
81
82pub(super) fn union_equal(
83    lhs: &ArrayData,
84    rhs: &ArrayData,
85    lhs_start: usize,
86    rhs_start: usize,
87    len: usize,
88) -> bool {
89    let lhs_type_ids = lhs.buffer::<i8>(0);
90    let rhs_type_ids = rhs.buffer::<i8>(0);
91
92    let lhs_type_id_range = &lhs_type_ids[lhs_start..lhs_start + len];
93    let rhs_type_id_range = &rhs_type_ids[rhs_start..rhs_start + len];
94
95    match (lhs.data_type(), rhs.data_type()) {
96        (
97            DataType::Union(lhs_fields, UnionMode::Dense),
98            DataType::Union(rhs_fields, UnionMode::Dense),
99        ) => {
100            let lhs_offsets = lhs.buffer::<i32>(1);
101            let rhs_offsets = rhs.buffer::<i32>(1);
102
103            let lhs_offsets_range = &lhs_offsets[lhs_start..lhs_start + len];
104            let rhs_offsets_range = &rhs_offsets[rhs_start..rhs_start + len];
105
106            lhs_type_id_range == rhs_type_id_range
107                && equal_dense(
108                    lhs,
109                    rhs,
110                    lhs_type_id_range,
111                    rhs_type_id_range,
112                    lhs_offsets_range,
113                    rhs_offsets_range,
114                    lhs_fields,
115                    rhs_fields,
116                )
117        }
118        (DataType::Union(fields, UnionMode::Sparse), DataType::Union(_, UnionMode::Sparse)) => {
119            lhs_type_id_range == rhs_type_id_range
120                && equal_sparse(lhs, rhs, lhs_type_id_range, fields, lhs_start, rhs_start)
121        }
122        _ => unimplemented!(
123            "Logical equality not yet implemented between dense and sparse union arrays"
124        ),
125    }
126}