Skip to content

Commit f07de1b

Browse files
committed
extract _ignore_nullability methods out and fix ordering
Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
1 parent 76745ac commit f07de1b

3 files changed

Lines changed: 54 additions & 31 deletions

File tree

vortex-array/src/dtype/dtype_impl.rs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,9 @@ impl DType {
123123
lhs_size == rhs_size && lhs_dtype.eq_ignore_nullability(rhs_dtype)
124124
}
125125
(Struct(lhs_dtype, _), Struct(rhs_dtype, _)) => {
126-
(lhs_dtype.names() == rhs_dtype.names())
127-
&& (lhs_dtype
128-
.fields()
129-
.zip_eq(rhs_dtype.fields())
130-
.all(|(l, r)| l.eq_ignore_nullability(&r)))
131-
}
132-
(Union(lhs), Union(rhs)) => {
133-
// Equal `names` implies equal length by FieldNames equality.
134-
lhs.names() == rhs.names()
135-
&& lhs.type_ids() == rhs.type_ids()
136-
&& lhs
137-
.variants()
138-
.zip_eq(rhs.variants())
139-
.all(|(l, r)| l.eq_ignore_nullability(&r))
126+
lhs_dtype.eq_ignore_nullability(rhs_dtype)
140127
}
128+
(Union(lhs), Union(rhs)) => lhs.eq_ignore_nullability(rhs),
141129
(Variant(_), Variant(_)) => true,
142130
(Extension(lhs_extdtype), Extension(rhs_extdtype)) => {
143131
lhs_extdtype.eq_ignore_nullability(rhs_extdtype)
@@ -156,22 +144,11 @@ impl DType {
156144
Decimal(decimal, _) => decimal.hash(state),
157145
List(element, _) => element.hash_ignore_nullability(state),
158146
FixedSizeList(element, size, _) => {
159-
size.hash(state);
160147
element.hash_ignore_nullability(state);
148+
size.hash(state);
161149
}
162-
Struct(fields, _) => {
163-
fields.names().hash(state);
164-
for field in fields.fields() {
165-
field.hash_ignore_nullability(state);
166-
}
167-
}
168-
Union(variants) => {
169-
variants.names().hash(state);
170-
variants.type_ids().hash(state);
171-
for variant in variants.variants() {
172-
variant.hash_ignore_nullability(state);
173-
}
174-
}
150+
Struct(fields, _) => fields.hash_ignore_nullability(state),
151+
Union(variants) => variants.hash_ignore_nullability(state),
175152
Extension(ext) => ext.hash_ignore_nullability(state),
176153
}
177154
}

vortex-array/src/dtype/struct_.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::fmt::Display;
55
use std::fmt::Formatter;
66
use std::hash::Hash;
7+
use std::hash::Hasher;
78
use std::sync::Arc;
89
use std::sync::OnceLock;
910

@@ -83,7 +84,7 @@ impl PartialEq for FieldDTypeInner {
8384
impl Eq for FieldDTypeInner {}
8485

8586
impl Hash for FieldDTypeInner {
86-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
87+
fn hash<H: Hasher>(&self, state: &mut H) {
8788
match self {
8889
FieldDTypeInner::Owned(owned) => {
8990
owned.hash(state);
@@ -255,12 +256,33 @@ impl PartialEq for StructFieldsInner {
255256
impl Eq for StructFieldsInner {}
256257

257258
impl Hash for StructFieldsInner {
258-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
259+
fn hash<H: Hasher>(&self, state: &mut H) {
259260
self.names.hash(state);
260261
self.dtypes.hash(state);
261262
}
262263
}
263264

265+
impl StructFields {
266+
/// Check if these struct fields are equal, ignoring field dtype nullability recursively.
267+
pub fn eq_ignore_nullability(&self, other: &Self) -> bool {
268+
Arc::ptr_eq(&self.0, &other.0)
269+
|| (self.0.names == other.0.names
270+
&& self
271+
.fields()
272+
.zip_eq(other.fields())
273+
.all(|(lhs, rhs)| lhs.eq_ignore_nullability(&rhs)))
274+
}
275+
276+
/// Hash these struct fields using the same equivalence relation as
277+
/// [`Self::eq_ignore_nullability`].
278+
pub(crate) fn hash_ignore_nullability<H: Hasher>(&self, state: &mut H) {
279+
self.0.names.hash(state);
280+
for field in self.fields() {
281+
field.hash_ignore_nullability(state);
282+
}
283+
}
284+
}
285+
264286
impl Default for StructFields {
265287
fn default() -> Self {
266288
Self::empty()

vortex-array/src/dtype/union.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use std::fmt;
55
use std::hash::Hash;
6+
use std::hash::Hasher;
67
use std::sync::Arc;
78

89
use itertools::Itertools;
@@ -134,13 +135,36 @@ impl PartialEq for UnionVariantsInner {
134135
impl Eq for UnionVariantsInner {}
135136

136137
impl Hash for UnionVariantsInner {
137-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
138+
fn hash<H: Hasher>(&self, state: &mut H) {
138139
self.names.hash(state);
139140
self.dtypes.hash(state);
140141
self.type_ids.hash(state);
141142
}
142143
}
143144

145+
impl UnionVariants {
146+
/// Check if these union variants are equal, ignoring variant dtype nullability recursively.
147+
pub fn eq_ignore_nullability(&self, other: &Self) -> bool {
148+
Arc::ptr_eq(&self.0, &other.0)
149+
|| (self.0.names == other.0.names
150+
&& self
151+
.variants()
152+
.zip_eq(other.variants())
153+
.all(|(lhs, rhs)| lhs.eq_ignore_nullability(&rhs))
154+
&& self.0.type_ids == other.0.type_ids)
155+
}
156+
157+
/// Hash these union variants using the same equivalence relation as
158+
/// [`Self::eq_ignore_nullability`].
159+
pub(crate) fn hash_ignore_nullability<H: Hasher>(&self, state: &mut H) {
160+
self.0.names.hash(state);
161+
for variant in self.variants() {
162+
variant.hash_ignore_nullability(state);
163+
}
164+
self.0.type_ids.hash(state);
165+
}
166+
}
167+
144168
impl Default for UnionVariants {
145169
fn default() -> Self {
146170
Self::empty()

0 commit comments

Comments
 (0)