/Users/andrewlamb/Software/arrow-rs/arrow-data/src/equal/utils.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::{contains_nulls, ArrayData}; |
19 | | use arrow_buffer::bit_chunk_iterator::BitChunks; |
20 | | use arrow_schema::DataType; |
21 | | |
22 | | // whether bits along the positions are equal |
23 | | // `lhs_start`, `rhs_start` and `len` are _measured in bits_. |
24 | | #[inline] |
25 | 154 | pub(super) fn equal_bits( |
26 | 154 | lhs_values: &[u8], |
27 | 154 | rhs_values: &[u8], |
28 | 154 | lhs_start: usize, |
29 | 154 | rhs_start: usize, |
30 | 154 | len: usize, |
31 | 154 | ) -> bool { |
32 | 154 | let lhs = BitChunks::new(lhs_values, lhs_start, len).iter_padded(); |
33 | 154 | let rhs = BitChunks::new(rhs_values, rhs_start, len).iter_padded(); |
34 | 154 | lhs.zip(rhs).all(|(a, b)| a == b) |
35 | 154 | } |
36 | | |
37 | | #[inline] |
38 | 697 | pub(super) fn equal_nulls( |
39 | 697 | lhs: &ArrayData, |
40 | 697 | rhs: &ArrayData, |
41 | 697 | lhs_start: usize, |
42 | 697 | rhs_start: usize, |
43 | 697 | len: usize, |
44 | 697 | ) -> bool { |
45 | 697 | match (lhs.nulls(), rhs.nulls()) { |
46 | 149 | (Some(lhs), Some(rhs)) => equal_bits( |
47 | 149 | lhs.validity(), |
48 | 149 | rhs.validity(), |
49 | 149 | lhs.offset() + lhs_start, |
50 | 149 | rhs.offset() + rhs_start, |
51 | 149 | len, |
52 | | ), |
53 | 0 | (Some(lhs), None) => !contains_nulls(Some(lhs), lhs_start, len), |
54 | 6 | (None, Some(rhs)) => !contains_nulls(Some(rhs), rhs_start, len), |
55 | 542 | (None, None) => true, |
56 | | } |
57 | 697 | } |
58 | | |
59 | | #[inline] |
60 | 439 | pub(super) fn base_equal(lhs: &ArrayData, rhs: &ArrayData) -> bool { |
61 | 439 | let equal_type = match (lhs.data_type(), rhs.data_type()) { |
62 | 0 | (DataType::Union(l_fields, l_mode), DataType::Union(r_fields, r_mode)) => { |
63 | 0 | l_fields == r_fields && l_mode == r_mode |
64 | | } |
65 | 3 | (DataType::Map(l_field, l_sorted), DataType::Map(r_field, r_sorted)) => { |
66 | 3 | let field_equal = match (l_field.data_type(), r_field.data_type()) { |
67 | 3 | (DataType::Struct(l_fields), DataType::Struct(r_fields)) |
68 | 3 | if l_fields.len() == 2 && r_fields.len() == 2 => |
69 | | { |
70 | 3 | let l_key_field = &l_fields[0]; |
71 | 3 | let r_key_field = &r_fields[0]; |
72 | 3 | let l_value_field = &l_fields[1]; |
73 | 3 | let r_value_field = &r_fields[1]; |
74 | | |
75 | | // We don't enforce the equality of field names |
76 | 3 | let data_type_equal = l_key_field.data_type() == r_key_field.data_type() |
77 | 3 | && l_value_field.data_type() == r_value_field.data_type(); |
78 | 3 | let nullability_equal = l_key_field.is_nullable() == r_key_field.is_nullable() |
79 | 3 | && l_value_field.is_nullable() == r_value_field.is_nullable(); |
80 | 3 | let metadata_equal = l_key_field.metadata() == r_key_field.metadata() |
81 | 3 | && l_value_field.metadata() == r_value_field.metadata(); |
82 | 3 | data_type_equal && nullability_equal && metadata_equal |
83 | | } |
84 | 0 | _ => panic!("Map type should have 2 fields Struct in its field"), |
85 | | }; |
86 | 3 | field_equal && l_sorted == r_sorted |
87 | | } |
88 | 436 | (l_data_type, r_data_type) => l_data_type == r_data_type, |
89 | | }; |
90 | 439 | equal_type && lhs.len() == rhs.len() |
91 | 439 | } |
92 | | |
93 | | // whether the two memory regions are equal |
94 | | #[inline] |
95 | 530 | pub(super) fn equal_len( |
96 | 530 | lhs_values: &[u8], |
97 | 530 | rhs_values: &[u8], |
98 | 530 | lhs_start: usize, |
99 | 530 | rhs_start: usize, |
100 | 530 | len: usize, |
101 | 530 | ) -> bool { |
102 | 530 | lhs_values[lhs_start..(lhs_start + len)] == rhs_values[rhs_start..(rhs_start + len)] |
103 | 530 | } |