Skip to content

Commit 4fb4147

Browse files
Fix ScalarValue partial ordering behavior with NULLs
1 parent b818f93 commit 4fb4147

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

datafusion/common/src/scalar/mod.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,15 @@ impl PartialEq for ScalarValue {
568568
impl PartialOrd for ScalarValue {
569569
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
570570
use ScalarValue::*;
571+
572+
if self.is_null() || other.is_null() {
573+
return None;
574+
}
571575
// This purposely doesn't have a catch-all "(_, _)" so that
572576
// any newly added enum variant will require editing this list
573577
// or else face a compile error
574578
match (self, other) {
579+
(Null, _) | (_, Null) => None,
575580
(Decimal32(v1, p1, s1), Decimal32(v2, p2, s2)) => {
576581
if p1.eq(p2) && s1.eq(s2) {
577582
v1.partial_cmp(v2)
@@ -723,8 +728,6 @@ impl PartialOrd for ScalarValue {
723728
if k1 == k2 { v1.partial_cmp(v2) } else { None }
724729
}
725730
(Dictionary(_, _), _) => None,
726-
(Null, Null) => Some(Ordering::Equal),
727-
(Null, _) => None,
728731
}
729732
}
730733
}
@@ -5760,10 +5763,9 @@ mod tests {
57605763
.unwrap(),
57615764
Ordering::Less
57625765
);
5763-
assert_eq!(
5766+
assert!(
57645767
ScalarValue::try_cmp(&ScalarValue::Int32(None), &ScalarValue::Int32(Some(2)))
5765-
.unwrap(),
5766-
Ordering::Less
5768+
.is_err()
57675769
);
57685770
assert_starts_with(
57695771
ScalarValue::try_cmp(
@@ -9348,4 +9350,23 @@ mod tests {
93489350
]
93499351
);
93509352
}
9353+
#[test]
9354+
fn scalar_partial_ordering_nulls() {
9355+
use ScalarValue::*;
9356+
9357+
assert_eq!(
9358+
Int32(Some(3)).partial_cmp(&Int32(None)),
9359+
None
9360+
);
9361+
9362+
assert_eq!(
9363+
Int32(None).partial_cmp(&Int32(Some(3))),
9364+
None
9365+
);
9366+
9367+
assert_eq!(
9368+
Int32(None).partial_cmp(&Int32(None)),
9369+
None
9370+
);
9371+
}
93519372
}

datafusion/common/src/utils/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,22 +1026,26 @@ mod tests {
10261026
ScalarValue::Int32(Some(2)),
10271027
Null,
10281028
ScalarValue::Int32(Some(0)),
1029-
] < vec![
1029+
]
1030+
.partial_cmp(&vec![
10301031
ScalarValue::Int32(Some(2)),
10311032
Null,
10321033
ScalarValue::Int32(Some(1)),
1033-
]
1034+
])
1035+
.is_none()
10341036
);
10351037
assert!(
10361038
vec![
10371039
ScalarValue::Int32(Some(2)),
10381040
ScalarValue::Int32(None),
10391041
ScalarValue::Int32(Some(0)),
1040-
] < vec![
1042+
]
1043+
.partial_cmp(&vec![
10411044
ScalarValue::Int32(Some(2)),
10421045
ScalarValue::Int32(None),
10431046
ScalarValue::Int32(Some(1)),
1044-
]
1047+
])
1048+
.is_none()
10451049
);
10461050
}
10471051

0 commit comments

Comments
 (0)