Skip to content

Commit e1c8435

Browse files
authored
chore: suggest using a cast for float-float and int-int comparisons (#5393)
Resolves: #5364 As we gather less expert users, I think it's worthwhile to suggest to those users how to resolve a floating point mismatch error. Signed-off-by: Daniel King <[email protected]>
1 parent e269163 commit e1c8435

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

vortex-array/src/compute/compare.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,20 @@ impl ComputeFnVTable for Compare {
206206
let CompareArgs { lhs, rhs, .. } = CompareArgs::try_from(args)?;
207207

208208
if !lhs.dtype().eq_ignore_nullability(rhs.dtype()) {
209+
if lhs.dtype().is_float() && rhs.dtype().is_float() {
210+
vortex_bail!(
211+
"Cannot compare different floating-point types ({}, {}). Consider using cast.",
212+
lhs.dtype(),
213+
rhs.dtype(),
214+
);
215+
}
216+
if lhs.dtype().is_int() && rhs.dtype().is_int() {
217+
vortex_bail!(
218+
"Cannot compare different fixed-width types ({}, {}). Consider using cast.",
219+
lhs.dtype(),
220+
rhs.dtype()
221+
);
222+
}
209223
vortex_bail!(
210224
"Cannot compare different DTypes {} and {}",
211225
lhs.dtype(),
@@ -371,6 +385,7 @@ mod tests {
371385
BoolArray, ConstantArray, ListArray, ListViewArray, PrimitiveArray, StructArray,
372386
VarBinArray, VarBinViewArray,
373387
};
388+
use crate::expr::{get_item, lt, root};
374389
use crate::test_harness::to_int_indices;
375390
use crate::validity::Validity;
376391

@@ -613,4 +628,58 @@ mod tests {
613628
assert!(result.scalar_at(1).is_valid());
614629
assert!(result.scalar_at(2).is_valid());
615630
}
631+
632+
#[test]
633+
fn test_different_floats_error_messages() {
634+
let result = compare(
635+
&buffer![0.0f32].into_array(),
636+
&buffer![0.0f64].into_array(),
637+
Operator::Lt,
638+
);
639+
assert!(result.as_ref().is_err_and(|err| {
640+
err.to_string()
641+
.contains("Cannot compare different floating-point types")
642+
}));
643+
644+
let expr = lt(get_item("l", root()), get_item("r", root()));
645+
let result = expr.evaluate(
646+
&StructArray::from_fields(&[
647+
("l", buffer![0.0f32].into_array()),
648+
("r", buffer![0.0f64].into_array()),
649+
])
650+
.unwrap()
651+
.into_array(),
652+
);
653+
assert!(result.as_ref().is_err_and(|err| {
654+
err.to_string()
655+
.contains("Cannot compare different floating-point types")
656+
}));
657+
}
658+
659+
#[test]
660+
fn test_different_ints_error_messages() {
661+
let result = compare(
662+
&buffer![0u8].into_array(),
663+
&buffer![0u16].into_array(),
664+
Operator::Lt,
665+
);
666+
assert!(result.as_ref().is_err_and(|err| {
667+
err.to_string()
668+
.contains("Cannot compare different fixed-width types")
669+
}));
670+
671+
let expr = lt(get_item("l", root()), get_item("r", root()));
672+
let result = expr.evaluate(
673+
&StructArray::from_fields(&[
674+
("l", buffer![0u8].into_array()),
675+
("r", buffer![0u16].into_array()),
676+
])
677+
.unwrap()
678+
.into_array(),
679+
);
680+
assert!(result.as_ref().is_err_and(|err| {
681+
err.to_string()
682+
.contains("Cannot compare different fixed-width types")
683+
}));
684+
}
616685
}

0 commit comments

Comments
 (0)