Skip to content

Commit d5024c8

Browse files
committed
Revert to dad6e02: Refactor dictionary handling and simplify batch logic
1 parent 377fb5d commit d5024c8

1 file changed

Lines changed: 10 additions & 31 deletions

File tree

  • datafusion/functions-aggregate-common/src

datafusion/functions-aggregate-common/src/min_max.rs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,7 @@ macro_rules! min_max_generic {
141141
}};
142142
}
143143

144-
// min/max of two logically compatible scalar values.
145-
// Dictionary scalars are unwrapped to their inner values for comparison,
146-
// then rewrapped with the dictionary key type when both inputs are dictionaries
147-
// after validating that their key types match.
144+
// min/max of two scalar values of the same type
148145
macro_rules! min_max {
149146
($VALUE:expr, $DELTA:expr, $OP:ident) => {{
150147
Ok(match ($VALUE, $DELTA) {
@@ -425,40 +422,24 @@ macro_rules! min_max {
425422
let result = min_max_generic!(lhs, rhs, $OP);
426423

427424
match lhs_key_type.zip(rhs_key_type) {
428-
Some((lhs_key_type, rhs_key_type)) => {
429-
if lhs_key_type != rhs_key_type {
430-
return internal_err!(
431-
"MIN/MAX is not expected to receive dictionary scalars with different key types ({:?} vs {:?})",
432-
lhs_key_type,
433-
rhs_key_type
434-
);
435-
}
436-
437-
ScalarValue::Dictionary(
438-
Box::new(lhs_key_type.clone()),
439-
Box::new(result),
440-
)
425+
Some((key_type, _)) => {
426+
ScalarValue::Dictionary(Box::new(key_type.clone()), Box::new(result))
441427
}
442428
None => result,
443429
}
444430
}
445431

446432
e => {
447433
return internal_err!(
448-
"MIN/MAX is not expected to receive logically incompatible scalar values {:?}",
434+
"MIN/MAX is not expected to receive scalars of incompatible types {:?}",
449435
e
450436
)
451437
}
452438
})
453439
}};
454440
}
455441

456-
/// Finds the min/max by scanning logical rows via `ScalarValue::try_from_array`.
457-
///
458-
/// This path is required for dictionary arrays because comparing
459-
/// `dictionary.values()` is not semantically correct: it can include
460-
/// unreferenced values and ignore null key positions.
461-
fn scalar_row_extreme(values: &ArrayRef, ordering: Ordering) -> Result<ScalarValue> {
442+
fn scalar_batch_extreme(values: &ArrayRef, ordering: Ordering) -> Result<ScalarValue> {
462443
let mut index = 0;
463444
let mut extreme = loop {
464445
if index == values.len() {
@@ -494,9 +475,7 @@ fn dictionary_scalar_parts(value: &ScalarValue) -> (&ScalarValue, Option<&DataTy
494475
}
495476
}
496477

497-
// Primitive, string, and binary types use specialized Arrow min/max kernels.
498-
// These remaining types fall back to scalar row-by-row logical comparison.
499-
fn requires_logical_row_scan(data_type: &DataType) -> bool {
478+
fn is_row_wise_batch_type(data_type: &DataType) -> bool {
500479
matches!(
501480
data_type,
502481
DataType::Struct(_)
@@ -844,8 +823,8 @@ pub fn min_batch(values: &ArrayRef) -> Result<ScalarValue> {
844823
min_binary_view
845824
)
846825
}
847-
data_type if requires_logical_row_scan(data_type) => {
848-
scalar_row_extreme(values, Ordering::Greater)?
826+
data_type if is_row_wise_batch_type(data_type) => {
827+
scalar_batch_extreme(values, Ordering::Greater)?
849828
}
850829
_ => min_max_batch!(values, min),
851830
})
@@ -896,8 +875,8 @@ pub fn max_batch(values: &ArrayRef) -> Result<ScalarValue> {
896875
let value = value.map(|e| e.to_vec());
897876
ScalarValue::FixedSizeBinary(*size, value)
898877
}
899-
data_type if requires_logical_row_scan(data_type) => {
900-
scalar_row_extreme(values, Ordering::Less)?
878+
data_type if is_row_wise_batch_type(data_type) => {
879+
scalar_batch_extreme(values, Ordering::Less)?
901880
}
902881
_ => min_max_batch!(values, max),
903882
})

0 commit comments

Comments
 (0)