Skip to content

Commit b918672

Browse files
committed
changed behavior again since the partial_cmp approach doesn't handle nulls correctly
1 parent c3108bb commit b918672

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

datafusion/physical-expr/src/aggregate/min_max.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,20 @@ macro_rules! typed_min_max {
488488
}};
489489
}
490490

491+
macro_rules! typed_min_max_float {
492+
($VALUE:expr, $DELTA:expr, $SCALAR:ident, $OP:ident) => {{
493+
ScalarValue::$SCALAR(match ($VALUE, $DELTA) {
494+
(None, None) => None,
495+
(Some(a), None) => Some(*a),
496+
(None, Some(b)) => Some(*b),
497+
(Some(a), Some(b)) => match a.total_cmp(b) {
498+
choose_min_max!($OP) => Some(*b),
499+
_ => Some(*a),
500+
},
501+
})
502+
}};
503+
}
504+
491505
// min/max of two scalar string values.
492506
macro_rules! typed_min_max_string {
493507
($VALUE:expr, $DELTA:expr, $SCALAR:ident, $OP:ident) => {{
@@ -500,7 +514,7 @@ macro_rules! typed_min_max_string {
500514
}};
501515
}
502516

503-
macro_rules! cmp_choose_min_max {
517+
macro_rules! choose_min_max {
504518
(min) => {
505519
std::cmp::Ordering::Greater
506520
};
@@ -509,11 +523,10 @@ macro_rules! cmp_choose_min_max {
509523
};
510524
}
511525

512-
// Use ScalarValue::partial_cmp to compare the values
513-
macro_rules! cmp_min_max {
526+
macro_rules! interval_min_max {
514527
($OP:tt, $LHS:expr, $RHS:expr) => {{
515528
match $LHS.partial_cmp(&$RHS) {
516-
Some(cmp_choose_min_max!($OP)) => $RHS.clone(),
529+
Some(choose_min_max!($OP)) => $RHS.clone(),
517530
Some(_) => $LHS.clone(),
518531
None => {
519532
return internal_err!("Comparison error while computing interval min/max")
@@ -555,11 +568,11 @@ macro_rules! min_max {
555568
(ScalarValue::Boolean(lhs), ScalarValue::Boolean(rhs)) => {
556569
typed_min_max!(lhs, rhs, Boolean, $OP)
557570
}
558-
(ScalarValue::Float64(_), ScalarValue::Float64(_)) => {
559-
cmp_min_max!($OP, $VALUE, $DELTA)
571+
(ScalarValue::Float64(lhs), ScalarValue::Float64(rhs)) => {
572+
typed_min_max_float!(lhs, rhs, Float64, $OP)
560573
}
561-
(ScalarValue::Float32(_), ScalarValue::Float32(_)) => {
562-
cmp_min_max!($OP, $VALUE, $DELTA)
574+
(ScalarValue::Float32(lhs), ScalarValue::Float32(rhs)) => {
575+
typed_min_max_float!(lhs, rhs, Float32, $OP)
563576
}
564577
(ScalarValue::UInt64(lhs), ScalarValue::UInt64(rhs)) => {
565578
typed_min_max!(lhs, rhs, UInt64, $OP)
@@ -691,7 +704,7 @@ macro_rules! min_max {
691704
ScalarValue::IntervalDayTime(_),
692705
ScalarValue::IntervalMonthDayNano(_),
693706
) => {
694-
cmp_min_max!($OP, $VALUE, $DELTA)
707+
interval_min_max!($OP, $VALUE, $DELTA)
695708
}
696709
(
697710
ScalarValue::DurationSecond(lhs),

0 commit comments

Comments
 (0)