arrow_data/equal/
mod.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
18//! Module containing functionality to compute array equality.
19//! This module uses [ArrayData] and does not
20//! depend on dynamic casting of `Array`.
21
22use crate::data::ArrayData;
23use arrow_buffer::i256;
24use arrow_schema::{DataType, IntervalUnit};
25use half::f16;
26
27mod boolean;
28mod byte_view;
29mod dictionary;
30mod fixed_binary;
31mod fixed_list;
32mod list;
33mod null;
34mod primitive;
35mod run;
36mod structure;
37mod union;
38mod utils;
39mod variable_size;
40
41// these methods assume the same type, len and null count.
42// For this reason, they are not exposed and are instead used
43// to build the generic functions below (`equal_range` and `equal`).
44use boolean::boolean_equal;
45use byte_view::byte_view_equal;
46use dictionary::dictionary_equal;
47use fixed_binary::fixed_binary_equal;
48use fixed_list::fixed_list_equal;
49use list::list_equal;
50use null::null_equal;
51use primitive::primitive_equal;
52use structure::struct_equal;
53use union::union_equal;
54use variable_size::variable_sized_equal;
55
56use self::run::run_equal;
57
58/// Compares the values of two [ArrayData] starting at `lhs_start` and `rhs_start` respectively
59/// for `len` slots.
60#[inline]
61fn equal_values(
62    lhs: &ArrayData,
63    rhs: &ArrayData,
64    lhs_start: usize,
65    rhs_start: usize,
66    len: usize,
67) -> bool {
68    match lhs.data_type() {
69        DataType::Null => null_equal(lhs, rhs, lhs_start, rhs_start, len),
70        DataType::Boolean => boolean_equal(lhs, rhs, lhs_start, rhs_start, len),
71        DataType::UInt8 => primitive_equal::<u8>(lhs, rhs, lhs_start, rhs_start, len),
72        DataType::UInt16 => primitive_equal::<u16>(lhs, rhs, lhs_start, rhs_start, len),
73        DataType::UInt32 => primitive_equal::<u32>(lhs, rhs, lhs_start, rhs_start, len),
74        DataType::UInt64 => primitive_equal::<u64>(lhs, rhs, lhs_start, rhs_start, len),
75        DataType::Int8 => primitive_equal::<i8>(lhs, rhs, lhs_start, rhs_start, len),
76        DataType::Int16 => primitive_equal::<i16>(lhs, rhs, lhs_start, rhs_start, len),
77        DataType::Int32 => primitive_equal::<i32>(lhs, rhs, lhs_start, rhs_start, len),
78        DataType::Int64 => primitive_equal::<i64>(lhs, rhs, lhs_start, rhs_start, len),
79        DataType::Float32 => primitive_equal::<f32>(lhs, rhs, lhs_start, rhs_start, len),
80        DataType::Float64 => primitive_equal::<f64>(lhs, rhs, lhs_start, rhs_start, len),
81        DataType::Decimal128(_, _) => primitive_equal::<i128>(lhs, rhs, lhs_start, rhs_start, len),
82        DataType::Decimal256(_, _) => primitive_equal::<i256>(lhs, rhs, lhs_start, rhs_start, len),
83        DataType::Date32 | DataType::Time32(_) | DataType::Interval(IntervalUnit::YearMonth) => {
84            primitive_equal::<i32>(lhs, rhs, lhs_start, rhs_start, len)
85        }
86        DataType::Date64
87        | DataType::Interval(IntervalUnit::DayTime)
88        | DataType::Time64(_)
89        | DataType::Timestamp(_, _)
90        | DataType::Duration(_) => primitive_equal::<i64>(lhs, rhs, lhs_start, rhs_start, len),
91        DataType::Interval(IntervalUnit::MonthDayNano) => {
92            primitive_equal::<i128>(lhs, rhs, lhs_start, rhs_start, len)
93        }
94        DataType::Utf8 | DataType::Binary => {
95            variable_sized_equal::<i32>(lhs, rhs, lhs_start, rhs_start, len)
96        }
97        DataType::LargeUtf8 | DataType::LargeBinary => {
98            variable_sized_equal::<i64>(lhs, rhs, lhs_start, rhs_start, len)
99        }
100        DataType::FixedSizeBinary(_) => fixed_binary_equal(lhs, rhs, lhs_start, rhs_start, len),
101        DataType::BinaryView | DataType::Utf8View => {
102            byte_view_equal(lhs, rhs, lhs_start, rhs_start, len)
103        }
104        DataType::List(_) => list_equal::<i32>(lhs, rhs, lhs_start, rhs_start, len),
105        DataType::ListView(_) | DataType::LargeListView(_) => {
106            unimplemented!("ListView/LargeListView not yet implemented")
107        }
108        DataType::LargeList(_) => list_equal::<i64>(lhs, rhs, lhs_start, rhs_start, len),
109        DataType::FixedSizeList(_, _) => fixed_list_equal(lhs, rhs, lhs_start, rhs_start, len),
110        DataType::Struct(_) => struct_equal(lhs, rhs, lhs_start, rhs_start, len),
111        DataType::Union(_, _) => union_equal(lhs, rhs, lhs_start, rhs_start, len),
112        DataType::Dictionary(data_type, _) => match data_type.as_ref() {
113            DataType::Int8 => dictionary_equal::<i8>(lhs, rhs, lhs_start, rhs_start, len),
114            DataType::Int16 => dictionary_equal::<i16>(lhs, rhs, lhs_start, rhs_start, len),
115            DataType::Int32 => dictionary_equal::<i32>(lhs, rhs, lhs_start, rhs_start, len),
116            DataType::Int64 => dictionary_equal::<i64>(lhs, rhs, lhs_start, rhs_start, len),
117            DataType::UInt8 => dictionary_equal::<u8>(lhs, rhs, lhs_start, rhs_start, len),
118            DataType::UInt16 => dictionary_equal::<u16>(lhs, rhs, lhs_start, rhs_start, len),
119            DataType::UInt32 => dictionary_equal::<u32>(lhs, rhs, lhs_start, rhs_start, len),
120            DataType::UInt64 => dictionary_equal::<u64>(lhs, rhs, lhs_start, rhs_start, len),
121            _ => unreachable!(),
122        },
123        DataType::Float16 => primitive_equal::<f16>(lhs, rhs, lhs_start, rhs_start, len),
124        DataType::Map(_, _) => list_equal::<i32>(lhs, rhs, lhs_start, rhs_start, len),
125        DataType::RunEndEncoded(_, _) => run_equal(lhs, rhs, lhs_start, rhs_start, len),
126    }
127}
128
129fn equal_range(
130    lhs: &ArrayData,
131    rhs: &ArrayData,
132    lhs_start: usize,
133    rhs_start: usize,
134    len: usize,
135) -> bool {
136    utils::equal_nulls(lhs, rhs, lhs_start, rhs_start, len)
137        && equal_values(lhs, rhs, lhs_start, rhs_start, len)
138}
139
140/// Logically compares two [ArrayData].
141///
142/// Two arrays are logically equal if and only if:
143/// * their data types are equal
144/// * their lengths are equal
145/// * their null counts are equal
146/// * their null bitmaps are equal
147/// * each of their items are equal
148///
149/// Two items are equal when their in-memory representation is physically equal
150/// (i.e. has the same bit content).
151///
152/// The physical comparison depend on the data type.
153///
154/// # Panics
155///
156/// This function may panic whenever any of the [ArrayData] does not follow the
157/// Arrow specification. (e.g. wrong number of buffers, buffer `len` does not
158/// correspond to the declared `len`)
159pub fn equal(lhs: &ArrayData, rhs: &ArrayData) -> bool {
160    utils::base_equal(lhs, rhs)
161        && lhs.null_count() == rhs.null_count()
162        && utils::equal_nulls(lhs, rhs, 0, 0, lhs.len())
163        && equal_values(lhs, rhs, 0, 0, lhs.len())
164}
165
166// See arrow/tests/array_equal.rs for tests