diff --git a/datafusion/src/physical_plan/expressions/in_list.rs b/datafusion/src/physical_plan/expressions/in_list.rs index 41f111006ea2a..cf8ef2d26af6a 100644 --- a/datafusion/src/physical_plan/expressions/in_list.rs +++ b/datafusion/src/physical_plan/expressions/in_list.rs @@ -17,14 +17,13 @@ //! InList expression -use std::any::Any; -use std::sync::Arc; - -use arrow::array::GenericStringArray; -use arrow::array::{ - ArrayRef, BooleanArray, Float32Array, Float64Array, Int16Array, Int32Array, - Int64Array, Int8Array, StringOffsetSizeTrait, UInt16Array, UInt32Array, UInt64Array, - UInt8Array, +use std::{any::Any, collections::HashSet}; +use std::{hash::Hash, sync::Arc}; + +use arrow::array::{ArrayRef, BooleanArray, StringOffsetSizeTrait}; +use arrow::{ + array::{Array, GenericStringArray, PrimitiveArray}, + datatypes::ArrowPrimitiveType, }; use arrow::{ datatypes::{DataType, Schema}, @@ -43,59 +42,205 @@ pub struct InListExpr { negated: bool, } -macro_rules! make_contains { - ($ARRAY:expr, $LIST_VALUES:expr, $NEGATED:expr, $SCALAR_VALUE:ident, $ARRAY_TYPE:ident) => {{ - let array = $ARRAY.as_any().downcast_ref::<$ARRAY_TYPE>().unwrap(); - - let mut contains_null = false; - let values = $LIST_VALUES - .iter() - .flat_map(|expr| match expr { - ColumnarValue::Scalar(s) => match s { - ScalarValue::$SCALAR_VALUE(Some(v)) => Some(*v), - ScalarValue::$SCALAR_VALUE(None) => { - contains_null = true; - None - } - ScalarValue::Utf8(None) => { - contains_null = true; - None - } - datatype => unimplemented!("Unexpected type {} for InList", datatype), - }, - ColumnarValue::Array(_) => { - unimplemented!("InList does not yet support nested columns.") - } +use std::iter::Iterator; +//This function computes the in list boolean array over an iterator +//the checks for the behavior for the negated and the contains_null are lifted outside the loops to try to avoid branches within the loop +fn compute_contains_array< + V, + I: Iterator>, + F: FnMut(Option) -> Option, +>( + it: I, + negated: bool, + contains_null: bool, + contains: F, +) -> BooleanArray { + let it_contains = it.map(contains); + match (negated, contains_null) { + (true, true) => it_contains + .map(|x| match x { + Some(true) => Some(false), + Some(false) | None => None, }) - .collect::>(); + .collect::(), + (true, false) => it_contains.map(|x| x.map(|x| !x)).collect::(), + (false, true) => it_contains + .map(|x| match x { + Some(true) => Some(true), + Some(false) | None => None, + }) + .collect::(), + (false, false) => it_contains.collect::(), + } +} +//Collects the scalar values from a specified discriminant into a vector, marking if there are any null values. +fn collect_scalar_value Option>( + vals: Vec, + contains_null: &mut bool, + disc: std::mem::Discriminant, + scalar_handler: F, +) -> Vec { + vals.iter() + .flat_map(|expr| match expr { + ColumnarValue::Scalar(s) => match s { + sc if std::mem::discriminant(s) == disc => { + scalar_handler(sc, contains_null) + } + ScalarValue::Utf8(None) => { + *contains_null = true; + None + } + datatype => unimplemented!("Unexpected type {} for InList", datatype), + }, + ColumnarValue::Array(_) => { + unimplemented!("InList does not yet support nested columns.") + } + }) + .collect::>() +} - Ok(ColumnarValue::Array(Arc::new( - array - .iter() - .map(|x| { - let contains = x.map(|x| values.contains(&x)); - match contains { - Some(true) => { - if $NEGATED { - Some(false) - } else { - Some(true) - } - } - Some(false) => { - if contains_null { - None - } else if $NEGATED { - Some(true) - } else { - Some(false) - } - } - None => None, - } - }) - .collect::(), - ))) +#[allow(clippy::comparison_chain)] +//Allow comparision chain because sometimes the match solution can be slower due to the +//cmp call not getting inlined. +fn contains_sorted(data: &[E], item: &E) -> bool { + if data.is_empty() { + return false; + } + //At this point slice is not empty so it will never panic + let lowest = data.first().unwrap(); + let highest = data.last().unwrap(); + + if item < lowest || highest < item { + return false; + } + + for elem in data.iter() { + if *elem == *item { + return true; + } else if *elem > *item { + return false; + } + } + false +} + +//Allow unnecessary_wraps to avoid wrapping with result every time this is invoked since this is a fairly specialized function +#[allow(clippy::unnecessary_wraps)] +fn contains_ord_types_it( + it: I, + mut list_values: Vec, + negated: bool, + contains_null: bool, + linear_search_bound: usize, +) -> Result +where + I: Iterator>, + O: Ord + Hash + Eq, +{ + let num_values = list_values.len(); + let contains_arr = if num_values == 0 { + compute_contains_array(it, negated, contains_null, |val| val.map(|_x| false)) + } else if num_values <= linear_search_bound { + list_values.sort_unstable(); + compute_contains_array(it, negated, contains_null, |val| { + val.map(|x| contains_sorted(list_values.as_slice(), &x)) + }) + } else { + let set = list_values.into_iter().collect::>(); // HashSet::with_capacity(values.len()); + compute_contains_array(it, negated, contains_null, |val| { + val.map(|x| set.contains(&x)) + }) + }; + let col_val = ColumnarValue::Array(Arc::new(contains_arr)); + Ok(col_val) +} + +fn contains_ord_types( + array: Arc, + list_values: Vec, + negated: bool, + disc: std::mem::Discriminant, + linear_search_bound: usize, + scalar_handler: F, +) -> Result +where + A: ArrowPrimitiveType, + A::Native: Ord + Hash + Eq, + F: Fn(&ScalarValue, &mut bool) -> Option, +{ + let arr = array + .as_ref() + .as_any() + .downcast_ref::>() + .unwrap(); + let mut contains_null = false; + let values = + collect_scalar_value(list_values, &mut contains_null, disc, scalar_handler); + contains_ord_types_it( + arr.iter(), + values, + negated, + contains_null, + linear_search_bound, + ) +} + +//Checks for values in the array of the specified ScalarValue discriminant for partially ordered that can be totally ordered types like f32 and f64. +fn contains_partial_ord_type( + array: Arc, + list_values: Vec, + negated: bool, + disc: std::mem::Discriminant, + linear_search_bound: usize, + make_ord_val: V, + scalar_handler: F, +) -> Result +where + A: ArrowPrimitiveType, + V: Copy + Fn(A::Native) -> O, + O: Ord + Hash + Eq, + F: Fn(&ScalarValue, &mut bool) -> Option, +{ + let arr = array + .as_ref() + .as_any() + .downcast_ref::>() + .unwrap(); + let mut contains_null = false; + let handler_wrapper = |s: &ScalarValue, cont_null: &mut bool| { + scalar_handler(s, cont_null).map(make_ord_val) + }; + let values = + collect_scalar_value(list_values, &mut contains_null, disc, handler_wrapper); + contains_ord_types_it( + arr.iter().map(|v| v.map(make_ord_val)), + values, + negated, + contains_null, + linear_search_bound, + ) +} + +//The last two literals are the linear search upper bound and the binary search upper bound before the strategy switches to a HashSet. +//If a single literal is given then it is used for both search bounds and the binary search will never be performed. + +macro_rules! make_ord_contains { + ($ARRAY_TYPE:ty, $ARRAY:expr, $LIST_VALUES:expr, $NEGATED:expr, $SCALAR_VALUE:ident, $LINEAR_SEARCH_BOUND:literal) => {{ + contains_ord_types::<$ARRAY_TYPE, _>( + $ARRAY, + $LIST_VALUES, + $NEGATED, + std::mem::discriminant(&ScalarValue::$SCALAR_VALUE(None)), + $LINEAR_SEARCH_BOUND, + |s, contains_null| match s { + ScalarValue::$SCALAR_VALUE(Some(v)) => Some(*v), + ScalarValue::$SCALAR_VALUE(None) => { + *contains_null = true; + None + } + _ => unreachable!(), + }, + ) }}; } @@ -134,7 +279,6 @@ impl InListExpr { &self, array: ArrayRef, list_values: Vec, - negated: bool, ) -> Result { let array = array .as_any() @@ -142,7 +286,7 @@ impl InListExpr { .unwrap(); let mut contains_null = false; - let values = list_values + let mut values = list_values .iter() .flat_map(|expr| match expr { ColumnarValue::Scalar(s) => match s { @@ -162,35 +306,28 @@ impl InListExpr { unimplemented!("InList does not yet support nested columns.") } }) - .collect::>(); - - Ok(ColumnarValue::Array(Arc::new( - array - .iter() - .map(|x| { - let contains = x.map(|x| values.contains(&x)); - match contains { - Some(true) => { - if negated { - Some(false) - } else { - Some(true) - } - } - Some(false) => { - if contains_null { - None - } else if negated { - Some(true) - } else { - Some(false) - } - } - None => None, - } + .collect::>(); + //TODO: determine better bound for when to switch to hashset + // Possibly based off of the number of characters the values in the list have combined with the total length of the list? + const LINEAR_SEARCH_UPPER_BOUND: usize = 8; + let contains_arr = match values.len() { + 0 => compute_contains_array(array.iter(), self.negated, contains_null, |x| { + x.map(|_x| false) + }), + 1..=LINEAR_SEARCH_UPPER_BOUND => { + values.sort_unstable(); + compute_contains_array(array.iter(), self.negated, contains_null, |x| { + x.map(|x| contains_sorted(values.as_slice(), &x)) + }) + } + _ => { + let set = values.into_iter().collect::>(); + compute_contains_array(array.iter(), self.negated, contains_null, |x| { + x.map(|s| set.contains(s)) }) - .collect::(), - ))) + } + }; + Ok(ColumnarValue::Array(Arc::new(contains_arr))) } } @@ -231,45 +368,165 @@ impl PhysicalExpr for InListExpr { ColumnarValue::Array(array) => array, ColumnarValue::Scalar(scalar) => scalar.to_array(), }; - + use arrow::datatypes::{ + Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, + UInt16Type, UInt32Type, UInt64Type, UInt8Type, + }; match value_data_type { DataType::Float32 => { - make_contains!(array, list_values, self.negated, Float32, Float32Array) + let disc = std::mem::discriminant(&ScalarValue::Float32(None)); + contains_partial_ord_type::( + array, + list_values, + self.negated, + disc, + 256, + ordered_float::OrderedFloat::from, + |s, contains_null| match s { + ScalarValue::Float32(Some(v)) => Some(*v), + ScalarValue::Float32(None) => { + *contains_null = true; + None + } + _ => unreachable!(), + }, + ) } DataType::Float64 => { - make_contains!(array, list_values, self.negated, Float64, Float64Array) + let disc = std::mem::discriminant(&ScalarValue::Float64(None)); + contains_partial_ord_type::( + array, + list_values, + self.negated, + disc, + 128, + ordered_float::OrderedFloat::from, + |s, contains_null| match s { + ScalarValue::Float64(Some(v)) => Some(*v), + ScalarValue::Float64(None) => { + *contains_null = true; + None + } + _ => unreachable!(), + }, + ) + } + DataType::Int8 => { + make_ord_contains!(Int8Type, array, list_values, self.negated, Int8, 256) } DataType::Int16 => { - make_contains!(array, list_values, self.negated, Int16, Int16Array) + make_ord_contains!( + Int16Type, + array, + list_values, + self.negated, + Int16, + 256 + ) } DataType::Int32 => { - make_contains!(array, list_values, self.negated, Int32, Int32Array) + make_ord_contains!( + Int32Type, + array, + list_values, + self.negated, + Int32, + 256 + ) } DataType::Int64 => { - make_contains!(array, list_values, self.negated, Int64, Int64Array) + make_ord_contains!( + Int64Type, + array, + list_values, + self.negated, + Int64, + 128 + ) } - DataType::Int8 => { - make_contains!(array, list_values, self.negated, Int8, Int8Array) + DataType::UInt8 => { + make_ord_contains!( + UInt8Type, + array, + list_values, + self.negated, + UInt8, + 256 + ) } DataType::UInt16 => { - make_contains!(array, list_values, self.negated, UInt16, UInt16Array) + make_ord_contains!( + UInt16Type, + array, + list_values, + self.negated, + UInt16, + 256 + ) } DataType::UInt32 => { - make_contains!(array, list_values, self.negated, UInt32, UInt32Array) + make_ord_contains!( + UInt32Type, + array, + list_values, + self.negated, + UInt32, + 256 + ) } DataType::UInt64 => { - make_contains!(array, list_values, self.negated, UInt64, UInt64Array) - } - DataType::UInt8 => { - make_contains!(array, list_values, self.negated, UInt8, UInt8Array) + make_ord_contains!( + UInt64Type, + array, + list_values, + self.negated, + UInt64, + 128 + ) } + DataType::Boolean => { - make_contains!(array, list_values, self.negated, Boolean, BooleanArray) - } - DataType::Utf8 => self.compare_utf8::(array, list_values, self.negated), - DataType::LargeUtf8 => { - self.compare_utf8::(array, list_values, self.negated) + let mut contains_null = false; + let bool_disc = std::mem::discriminant(&ScalarValue::Boolean(None)); + let values = collect_scalar_value( + list_values, + &mut contains_null, + bool_disc, + |s, cont_null| match s { + ScalarValue::Boolean(Some(v)) => Some(*v), + ScalarValue::Boolean(None) => { + *cont_null = true; + None + } + _ => unreachable!(), + }, + ); + let bool_arr = array.as_any().downcast_ref::().unwrap(); + let mut found_true = false; + let mut found_false = false; + //Check if the list contains both true and false values + values.iter().any(|b| { + match b { + true => { + found_true = true; + } + false => { + found_false = true; + } + } + found_true && found_false + }); + + let contains_arr = compute_contains_array( + bool_arr.iter(), + self.negated, + contains_null, + |o| o.map(|b| (found_true && b) || (found_false && !b)), + ); + Ok(ColumnarValue::Array(Arc::new(contains_arr))) } + DataType::Utf8 => self.compare_utf8::(array, list_values), + DataType::LargeUtf8 => self.compare_utf8::(array, list_values), datatype => { unimplemented!("InList does not support datatype {:?}.", datatype) } @@ -288,7 +545,11 @@ pub fn in_list( #[cfg(test)] mod tests { - use arrow::{array::StringArray, datatypes::Field}; + use arrow::{ + array::StringArray, + array::{Float64Array, Int64Array}, + datatypes::Field, + }; use super::*; use crate::error::Result; @@ -428,31 +689,60 @@ mod tests { #[test] fn in_list_bool() -> Result<()> { let schema = Schema::new(vec![Field::new("a", DataType::Boolean, true)]); - let a = BooleanArray::from(vec![Some(true), None]); + let a = BooleanArray::from(vec![Some(true), None, Some(false)]); let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)])?; // expression: "a in (true)" let list = vec![lit(ScalarValue::Boolean(Some(true)))]; - in_list!(batch, list, &false, vec![Some(true), None]); + in_list!(batch, list, &false, vec![Some(true), None, Some(false)]); // expression: "a not in (true)" let list = vec![lit(ScalarValue::Boolean(Some(true)))]; - in_list!(batch, list, &true, vec![Some(false), None]); + in_list!(batch, list, &true, vec![Some(false), None, Some(true)]); // expression: "a in (true, NULL)" let list = vec![ lit(ScalarValue::Boolean(Some(true))), lit(ScalarValue::Utf8(None)), ]; - in_list!(batch, list, &false, vec![Some(true), None]); + in_list!(batch, list, &false, vec![Some(true), None, None]); // expression: "a not in (true, NULL)" let list = vec![ lit(ScalarValue::Boolean(Some(true))), lit(ScalarValue::Utf8(None)), ]; - in_list!(batch, list, &true, vec![Some(false), None]); + in_list!(batch, list, &true, vec![Some(false), None, None]); + + // expression: "a in (true, false)" + let list = vec![ + lit(ScalarValue::Boolean(Some(true))), + lit(ScalarValue::Boolean(Some(false))), + ]; + in_list!(batch, list, &false, vec![Some(true), None, Some(true)]); + //expression: "a not in (true, false)" + let list = vec![ + lit(ScalarValue::Boolean(Some(true))), + lit(ScalarValue::Boolean(Some(false))), + ]; + in_list!(batch, list, &true, vec![Some(false), None, Some(false)]); + + // expression: "a in (true, false, NULL)" + let list = vec![ + lit(ScalarValue::Boolean(Some(true))), + lit(ScalarValue::Boolean(Some(false))), + lit(ScalarValue::Utf8(None)), + ]; + in_list!(batch, list, &false, vec![Some(true), None, Some(true)]); + + //expression: "a not in (true, false, NULL)" + let list = vec![ + lit(ScalarValue::Boolean(Some(true))), + lit(ScalarValue::Boolean(Some(false))), + lit(ScalarValue::Utf8(None)), + ]; + in_list!(batch, list, &true, vec![Some(false), None, Some(false)]); Ok(()) } }