polars_arrow/array/equal/
mod.rs

1use super::*;
2use crate::offset::Offset;
3use crate::types::NativeType;
4
5mod binary;
6mod binary_view;
7mod boolean;
8mod dictionary;
9mod fixed_size_binary;
10mod fixed_size_list;
11mod list;
12mod map;
13mod null;
14mod primitive;
15mod struct_;
16mod union;
17mod utf8;
18
19impl PartialEq for dyn Array + '_ {
20    fn eq(&self, that: &dyn Array) -> bool {
21        equal(self, that)
22    }
23}
24
25impl PartialEq<dyn Array> for std::sync::Arc<dyn Array + '_> {
26    fn eq(&self, that: &dyn Array) -> bool {
27        equal(&**self, that)
28    }
29}
30
31impl PartialEq<dyn Array> for Box<dyn Array + '_> {
32    fn eq(&self, that: &dyn Array) -> bool {
33        equal(&**self, that)
34    }
35}
36
37impl PartialEq<NullArray> for NullArray {
38    fn eq(&self, other: &Self) -> bool {
39        null::equal(self, other)
40    }
41}
42
43impl PartialEq<&dyn Array> for NullArray {
44    fn eq(&self, other: &&dyn Array) -> bool {
45        equal(self, *other)
46    }
47}
48
49impl<T: NativeType> PartialEq<&dyn Array> for PrimitiveArray<T> {
50    fn eq(&self, other: &&dyn Array) -> bool {
51        equal(self, *other)
52    }
53}
54
55impl<T: NativeType> PartialEq<PrimitiveArray<T>> for &dyn Array {
56    fn eq(&self, other: &PrimitiveArray<T>) -> bool {
57        equal(*self, other)
58    }
59}
60
61impl<T: NativeType> PartialEq<PrimitiveArray<T>> for PrimitiveArray<T> {
62    fn eq(&self, other: &Self) -> bool {
63        primitive::equal::<T>(self, other)
64    }
65}
66
67impl PartialEq<BooleanArray> for BooleanArray {
68    fn eq(&self, other: &Self) -> bool {
69        equal(self, other)
70    }
71}
72
73impl PartialEq<&dyn Array> for BooleanArray {
74    fn eq(&self, other: &&dyn Array) -> bool {
75        equal(self, *other)
76    }
77}
78
79impl<O: Offset> PartialEq<Utf8Array<O>> for Utf8Array<O> {
80    fn eq(&self, other: &Self) -> bool {
81        utf8::equal(self, other)
82    }
83}
84
85impl<O: Offset> PartialEq<&dyn Array> for Utf8Array<O> {
86    fn eq(&self, other: &&dyn Array) -> bool {
87        equal(self, *other)
88    }
89}
90
91impl<O: Offset> PartialEq<Utf8Array<O>> for &dyn Array {
92    fn eq(&self, other: &Utf8Array<O>) -> bool {
93        equal(*self, other)
94    }
95}
96
97impl<O: Offset> PartialEq<BinaryArray<O>> for BinaryArray<O> {
98    fn eq(&self, other: &Self) -> bool {
99        binary::equal(self, other)
100    }
101}
102
103impl<O: Offset> PartialEq<&dyn Array> for BinaryArray<O> {
104    fn eq(&self, other: &&dyn Array) -> bool {
105        equal(self, *other)
106    }
107}
108
109impl<O: Offset> PartialEq<BinaryArray<O>> for &dyn Array {
110    fn eq(&self, other: &BinaryArray<O>) -> bool {
111        equal(*self, other)
112    }
113}
114
115impl PartialEq<FixedSizeBinaryArray> for FixedSizeBinaryArray {
116    fn eq(&self, other: &Self) -> bool {
117        fixed_size_binary::equal(self, other)
118    }
119}
120
121impl PartialEq<&dyn Array> for FixedSizeBinaryArray {
122    fn eq(&self, other: &&dyn Array) -> bool {
123        equal(self, *other)
124    }
125}
126
127impl<O: Offset> PartialEq<ListArray<O>> for ListArray<O> {
128    fn eq(&self, other: &Self) -> bool {
129        list::equal(self, other)
130    }
131}
132
133impl<O: Offset> PartialEq<&dyn Array> for ListArray<O> {
134    fn eq(&self, other: &&dyn Array) -> bool {
135        equal(self, *other)
136    }
137}
138
139impl PartialEq<FixedSizeListArray> for FixedSizeListArray {
140    fn eq(&self, other: &Self) -> bool {
141        fixed_size_list::equal(self, other)
142    }
143}
144
145impl PartialEq<&dyn Array> for FixedSizeListArray {
146    fn eq(&self, other: &&dyn Array) -> bool {
147        equal(self, *other)
148    }
149}
150
151impl PartialEq<StructArray> for StructArray {
152    fn eq(&self, other: &Self) -> bool {
153        struct_::equal(self, other)
154    }
155}
156
157impl PartialEq<&dyn Array> for StructArray {
158    fn eq(&self, other: &&dyn Array) -> bool {
159        equal(self, *other)
160    }
161}
162
163impl<K: DictionaryKey> PartialEq<DictionaryArray<K>> for DictionaryArray<K> {
164    fn eq(&self, other: &Self) -> bool {
165        dictionary::equal(self, other)
166    }
167}
168
169impl<K: DictionaryKey> PartialEq<&dyn Array> for DictionaryArray<K> {
170    fn eq(&self, other: &&dyn Array) -> bool {
171        equal(self, *other)
172    }
173}
174
175impl PartialEq<UnionArray> for UnionArray {
176    fn eq(&self, other: &Self) -> bool {
177        union::equal(self, other)
178    }
179}
180
181impl PartialEq<&dyn Array> for UnionArray {
182    fn eq(&self, other: &&dyn Array) -> bool {
183        equal(self, *other)
184    }
185}
186
187impl PartialEq<MapArray> for MapArray {
188    fn eq(&self, other: &Self) -> bool {
189        map::equal(self, other)
190    }
191}
192
193impl PartialEq<&dyn Array> for MapArray {
194    fn eq(&self, other: &&dyn Array) -> bool {
195        equal(self, *other)
196    }
197}
198
199/// Logically compares two [`Array`]s.
200/// Two arrays are logically equal if and only if:
201/// * their data types are equal
202/// * each of their items are equal
203pub fn equal(lhs: &dyn Array, rhs: &dyn Array) -> bool {
204    if lhs.dtype() != rhs.dtype() {
205        return false;
206    }
207
208    use crate::datatypes::PhysicalType::*;
209    match lhs.dtype().to_physical_type() {
210        Null => {
211            let lhs = lhs.as_any().downcast_ref().unwrap();
212            let rhs = rhs.as_any().downcast_ref().unwrap();
213            null::equal(lhs, rhs)
214        },
215        Boolean => {
216            let lhs = lhs.as_any().downcast_ref().unwrap();
217            let rhs = rhs.as_any().downcast_ref().unwrap();
218            boolean::equal(lhs, rhs)
219        },
220        Primitive(primitive) => with_match_primitive_type_full!(primitive, |$T| {
221            let lhs = lhs.as_any().downcast_ref().unwrap();
222            let rhs = rhs.as_any().downcast_ref().unwrap();
223            primitive::equal::<$T>(lhs, rhs)
224        }),
225        Utf8 => {
226            let lhs = lhs.as_any().downcast_ref().unwrap();
227            let rhs = rhs.as_any().downcast_ref().unwrap();
228            utf8::equal::<i32>(lhs, rhs)
229        },
230        LargeUtf8 => {
231            let lhs = lhs.as_any().downcast_ref().unwrap();
232            let rhs = rhs.as_any().downcast_ref().unwrap();
233            utf8::equal::<i64>(lhs, rhs)
234        },
235        Binary => {
236            let lhs = lhs.as_any().downcast_ref().unwrap();
237            let rhs = rhs.as_any().downcast_ref().unwrap();
238            binary::equal::<i32>(lhs, rhs)
239        },
240        LargeBinary => {
241            let lhs = lhs.as_any().downcast_ref().unwrap();
242            let rhs = rhs.as_any().downcast_ref().unwrap();
243            binary::equal::<i64>(lhs, rhs)
244        },
245        List => {
246            let lhs = lhs.as_any().downcast_ref().unwrap();
247            let rhs = rhs.as_any().downcast_ref().unwrap();
248            list::equal::<i32>(lhs, rhs)
249        },
250        LargeList => {
251            let lhs = lhs.as_any().downcast_ref().unwrap();
252            let rhs = rhs.as_any().downcast_ref().unwrap();
253            list::equal::<i64>(lhs, rhs)
254        },
255        Struct => {
256            let lhs = lhs.as_any().downcast_ref::<StructArray>().unwrap();
257            let rhs = rhs.as_any().downcast_ref::<StructArray>().unwrap();
258            struct_::equal(lhs, rhs)
259        },
260        Dictionary(key_type) => {
261            match_integer_type!(key_type, |$T| {
262                let lhs = lhs.as_any().downcast_ref().unwrap();
263                let rhs = rhs.as_any().downcast_ref().unwrap();
264                dictionary::equal::<$T>(lhs, rhs)
265            })
266        },
267        FixedSizeBinary => {
268            let lhs = lhs.as_any().downcast_ref().unwrap();
269            let rhs = rhs.as_any().downcast_ref().unwrap();
270            fixed_size_binary::equal(lhs, rhs)
271        },
272        FixedSizeList => {
273            let lhs = lhs.as_any().downcast_ref().unwrap();
274            let rhs = rhs.as_any().downcast_ref().unwrap();
275            fixed_size_list::equal(lhs, rhs)
276        },
277        Union => {
278            let lhs = lhs.as_any().downcast_ref().unwrap();
279            let rhs = rhs.as_any().downcast_ref().unwrap();
280            union::equal(lhs, rhs)
281        },
282        Map => {
283            let lhs = lhs.as_any().downcast_ref().unwrap();
284            let rhs = rhs.as_any().downcast_ref().unwrap();
285            map::equal(lhs, rhs)
286        },
287        BinaryView => {
288            let lhs = lhs.as_any().downcast_ref().unwrap();
289            let rhs = rhs.as_any().downcast_ref().unwrap();
290            binary_view::equal::<[u8]>(lhs, rhs)
291        },
292        Utf8View => {
293            let lhs = lhs.as_any().downcast_ref().unwrap();
294            let rhs = rhs.as_any().downcast_ref().unwrap();
295            binary_view::equal::<str>(lhs, rhs)
296        },
297    }
298}