Skip to content

Apache DataFusion SQL Query Engine

Fix IN expr for NaN (#7378)

Kousuke Sarutab0d538eec582
diff --git a/datafusion/physical-expr/src/expressions/in_list.rs b/datafusion/physical-expr/src/expressions/in_list.rs
index 612060aeb6..8f7515f29a 100644
--- a/datafusion/physical-expr/src/expressions/in_list.rs
+++ b/datafusion/physical-expr/src/expressions/in_list.rs
@@ -95,7 +95,7 @@ impl<T> Set for ArraySet<T>
where
T: Array + 'static,
for<'a> &'a T: ArrayAccessor,
- for<'a> <&'a T as ArrayAccessor>::Item: PartialEq + HashValue,
+ for<'a> <&'a T as ArrayAccessor>::Item: IsEqual,
{
fn contains(&self, v: &dyn Array, negated: bool) -> Result<BooleanArray> {
downcast_dictionary_array! {
@@ -119,7 +119,7 @@ where
.hash_set
.map
.raw_entry()
- .from_hash(hash, |idx| in_array.value(*idx) == v)
+ .from_hash(hash, |idx| in_array.value(*idx).is_equal(&v))
.is_some();
match contains {
@@ -142,7 +142,7 @@ where
fn make_hash_set<T>(array: T) -> ArrayHashSet
where
T: ArrayAccessor,
- T::Item: PartialEq + HashValue,
+ T::Item: IsEqual,
{
let state = RandomState::new();
let mut map: HashMap<usize, (), ()> =
@@ -153,7 +153,7 @@ where
let hash = value.hash_one(&state);
if let RawEntryMut::Vacant(v) = map
.raw_entry_mut()
- .from_hash(hash, |x| array.value(*x) == value)
+ .from_hash(hash, |x| array.value(*x).is_equal(&value))
{
v.insert_with_hasher(hash, idx, (), |x| array.value(*x).hash_one(&state));
}
@@ -229,6 +229,40 @@ fn try_cast_static_filter_to_set(
make_set(evaluate_list(list, &batch)?.as_ref())
}
+/// Custom equality check function which is used with [`ArrayHashSet`] for existence check.
+trait IsEqual: HashValue {
+ fn is_equal(&self, other: &Self) -> bool;
+}
+
+impl<'a, T: IsEqual + ?Sized> IsEqual for &'a T {
+ fn is_equal(&self, other: &Self) -> bool {
+ T::is_equal(self, other)
+ }
+}
+
+macro_rules! is_equal {
+ ($($t:ty),+) => {
+ $(impl IsEqual for $t {
+ fn is_equal(&self, other: &Self) -> bool {
+ self == other
+ }
+ })*
+ };
+}
+is_equal!(i8, i16, i32, i64, i128, i256, u8, u16, u32, u64);
+is_equal!(bool, str, [u8]);
+
+macro_rules! is_equal_float {
+ ($($t:ty),+) => {
+ $(impl IsEqual for $t {
+ fn is_equal(&self, other: &Self) -> bool {
+ self.to_bits() == other.to_bits()
+ }
+ })*
+ };
+}
+is_equal_float!(half::f16, f32, f64);
+
impl InListExpr {
/// Create a new InList expression
pub fn new(
@@ -610,50 +644,100 @@ mod tests {
#[test]
fn in_list_float64() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", DataType::Float64, true)]);
- let a = Float64Array::from(vec![Some(0.0), Some(0.2), None]);
+ let a = Float64Array::from(vec![
+ Some(0.0),
+ Some(0.2),
+ None,
+ Some(f64::NAN),
+ Some(-f64::NAN),
+ ]);
let col_a = col("a", &schema)?;
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
- // expression: "a in (0.0, 0.2)"
+ // expression: "a in (0.0, 0.1)"
let list = vec![lit(0.0f64), lit(0.1f64)];
in_list!(
batch,
list,
&false,
- vec![Some(true), Some(false), None],
+ vec![Some(true), Some(false), None, Some(false), Some(false)],
col_a.clone(),
&schema
);
- // expression: "a not in (0.0, 0.2)"
+ // expression: "a not in (0.0, 0.1)"
let list = vec![lit(0.0f64), lit(0.1f64)];
in_list!(
batch,
list,
&true,
- vec![Some(false), Some(true), None],
+ vec![Some(false), Some(true), None, Some(true), Some(true)],
col_a.clone(),
&schema
);
- // expression: "a in (0.0, 0.2, NULL)"
+ // expression: "a in (0.0, 0.1, NULL)"
let list = vec![lit(0.0f64), lit(0.1f64), lit(ScalarValue::Null)];
in_list!(
batch,
list,
&false,
- vec![Some(true), None, None],
+ vec![Some(true), None, None, None, None],
col_a.clone(),
&schema
);
- // expression: "a not in (0.0, 0.2, NULL)"
+ // expression: "a not in (0.0, 0.1, NULL)"
let list = vec![lit(0.0f64), lit(0.1f64), lit(ScalarValue::Null)];
in_list!(
batch,
list,
&true,
- vec![Some(false), None, None],
+ vec![Some(false), None, None, None, None],
+ col_a.clone(),
+ &schema
+ );
+
+ // expression: "a in (0.0, 0.1, NaN)"
+ let list = vec![lit(0.0f64), lit(0.1f64), lit(f64::NAN)];
+ in_list!(
+ batch,
+ list,
+ &false,
+ vec![Some(true), Some(false), None, Some(true), Some(false)],
+ col_a.clone(),
+ &schema
+ );
+
+ // expression: "a not in (0.0, 0.1, NaN)"
+ let list = vec![lit(0.0f64), lit(0.1f64), lit(f64::NAN)];
+ in_list!(
+ batch,
+ list,
+ &true,
+ vec![Some(false), Some(true), None, Some(false), Some(true)],
+ col_a.clone(),
+ &schema
+ );
+
+ // expression: "a in (0.0, 0.1, -NaN)"
+ let list = vec![lit(0.0f64), lit(0.1f64), lit(-f64::NAN)];
+ in_list!(
+ batch,
+ list,
+ &false,
+ vec![Some(true), Some(false), None, Some(false), Some(true)],
+ col_a.clone(),
+ &schema
+ );
+
+ // expression: "a not in (0.0, 0.1, -NaN)"
+ let list = vec![lit(0.0f64), lit(0.1f64), lit(-f64::NAN)];
+ in_list!(
+ batch,
+ list,
+ &true,
+ vec![Some(false), Some(true), None, Some(true), Some(false)],
col_a.clone(),
&schema
);
diff --git a/datafusion/sqllogictest/test_files/predicates.slt b/datafusion/sqllogictest/test_files/predicates.slt
index aa233115b5..a168dfe361 100644
--- a/datafusion/sqllogictest/test_files/predicates.slt
+++ b/datafusion/sqllogictest/test_files/predicates.slt
@@ -253,6 +253,40 @@ SELECT * FROM test WHERE column1 IN ('foo', 'Bar', 'fazzz')
foo
fazzz
+statement ok
+CREATE TABLE IF NOT EXISTS test_float AS VALUES(1.2),(2.1),(NULL),('NaN'::double),('-NaN'::double);
+
+# IN expr for float
+query R
+SELECT * FROM test_float WHERE column1 IN (0.0, -1.2)
+----
+
+query R
+SELECT * FROM test_float WHERE column1 IN (0.0, 1.2)
+----
+1.2
+
+query R
+SELECT * FROM test_float WHERE column1 IN (2.1, 1.2)
+----
+1.2
+2.1
+
+query R
+SELECT * FROM test_float WHERE column1 IN (0.0, 1.2, NULL)
+----
+1.2
+
+query R
+SELECT * FROM test_float WHERE column1 IN (0.0, -1.2, NULL)
+----
+
+query R
+SELECT * FROM test_float WHERE column1 IN (0.0, 1.2, 'NaN'::double, '-NaN'::double)
+----
+1.2
+NaN
+NaN
###
# Test logical plan simplifies large OR chains
@@ -352,6 +386,9 @@ drop table alltypes_plain;
statement ok
DROP TABLE test;
+statement ok
+DROP TABLE test_float;
+
#########
# Predicates on memory tables / statistics generation
# Reproducer for https://github.com/apache/arrow-datafusion/issues/7125