Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/scalar/equal.rs
6939 views
1
use std::sync::Arc;
2
3
use super::*;
4
use crate::{match_integer_type, with_match_primitive_type_full};
5
6
impl PartialEq for dyn Scalar + '_ {
7
fn eq(&self, that: &dyn Scalar) -> bool {
8
equal(self, that)
9
}
10
}
11
12
impl PartialEq<dyn Scalar> for Arc<dyn Scalar + '_> {
13
fn eq(&self, that: &dyn Scalar) -> bool {
14
equal(&**self, that)
15
}
16
}
17
18
impl PartialEq<dyn Scalar> for Box<dyn Scalar + '_> {
19
fn eq(&self, that: &dyn Scalar) -> bool {
20
equal(&**self, that)
21
}
22
}
23
24
macro_rules! dyn_eq {
25
($ty:ty, $lhs:expr, $rhs:expr) => {{
26
let lhs = $lhs.as_any().downcast_ref::<$ty>().unwrap();
27
let rhs = $rhs.as_any().downcast_ref::<$ty>().unwrap();
28
lhs == rhs
29
}};
30
}
31
32
fn equal(lhs: &dyn Scalar, rhs: &dyn Scalar) -> bool {
33
if lhs.dtype() != rhs.dtype() {
34
return false;
35
}
36
37
use PhysicalType::*;
38
match lhs.dtype().to_physical_type() {
39
Null => dyn_eq!(NullScalar, lhs, rhs),
40
Boolean => dyn_eq!(BooleanScalar, lhs, rhs),
41
Primitive(primitive) => with_match_primitive_type_full!(primitive, |$T| {
42
dyn_eq!(PrimitiveScalar<$T>, lhs, rhs)
43
}),
44
LargeUtf8 => dyn_eq!(Utf8Scalar<i64>, lhs, rhs),
45
LargeBinary => dyn_eq!(BinaryScalar<i64>, lhs, rhs),
46
LargeList => dyn_eq!(ListScalar<i64>, lhs, rhs),
47
Dictionary(key_type) => match_integer_type!(key_type, |$T| {
48
dyn_eq!(DictionaryScalar<$T>, lhs, rhs)
49
}),
50
Struct => dyn_eq!(StructScalar, lhs, rhs),
51
FixedSizeBinary => dyn_eq!(FixedSizeBinaryScalar, lhs, rhs),
52
FixedSizeList => dyn_eq!(FixedSizeListScalar, lhs, rhs),
53
Union => dyn_eq!(UnionScalar, lhs, rhs),
54
Map => dyn_eq!(MapScalar, lhs, rhs),
55
Utf8View => dyn_eq!(BinaryViewScalar<str>, lhs, rhs),
56
_ => unimplemented!(),
57
}
58
}
59
60