|
| 1 | +use arrow::array::Array; |
| 2 | +use arrow::array::*; |
| 3 | +use arrow::datatypes::DataType; |
| 4 | +use datafusion_common::{DataFusionError, Result}; |
| 5 | +use datafusion_expr::ColumnarValue; |
| 6 | +use std::sync::Arc; |
| 7 | + |
| 8 | +macro_rules! null_if_equal { |
| 9 | + ($LEFT:expr, $RIGHT:expr, $TYPE:ident) => {{ |
| 10 | + let left = $LEFT |
| 11 | + .as_any() |
| 12 | + .downcast_ref::<$TYPE>() |
| 13 | + .expect("failed to downcast array"); |
| 14 | + let right = $RIGHT |
| 15 | + .as_any() |
| 16 | + .downcast_ref::<$TYPE>() |
| 17 | + .expect("failed to downcast array"); |
| 18 | + |
| 19 | + let res = left |
| 20 | + .iter() |
| 21 | + .zip(right.iter()) |
| 22 | + .map(|(l, r)| if l == r { None } else { l }) |
| 23 | + .collect::<$TYPE>(); |
| 24 | + |
| 25 | + Arc::new(res) as ArrayRef |
| 26 | + }}; |
| 27 | +} |
| 28 | + |
| 29 | +pub fn nullif_func_str(args: &[ColumnarValue]) -> Result<ColumnarValue> { |
| 30 | + let (lhs, rhs) = (&args[0], &args[1]); |
| 31 | + let (left_arr, right_arr) = match (lhs, rhs) { |
| 32 | + (ColumnarValue::Array(lhs), _) => (lhs, rhs.clone().into_array(lhs.len())), |
| 33 | + _ => { |
| 34 | + return Err(DataFusionError::NotImplemented( |
| 35 | + "nullif_str does not support a literal as first argument".to_string(), |
| 36 | + )) |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + if left_arr.data_type() != right_arr.data_type() { |
| 41 | + return Err(DataFusionError::NotImplemented( |
| 42 | + "both arguments have to have the same type".to_string(), |
| 43 | + )); |
| 44 | + } |
| 45 | + |
| 46 | + let res = match left_arr.data_type() { |
| 47 | + DataType::Utf8 => null_if_equal!(left_arr, right_arr, StringArray), |
| 48 | + DataType::LargeUtf8 => null_if_equal!(left_arr, right_arr, LargeStringArray), |
| 49 | + _ => { |
| 50 | + return Err(DataFusionError::NotImplemented( |
| 51 | + "nullif_str supports Utf8 and LargeUtf8 only".to_string(), |
| 52 | + )) |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + Ok(ColumnarValue::Array(res)) |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + use datafusion_common::ScalarValue; |
| 62 | + |
| 63 | + use super::*; |
| 64 | + |
| 65 | + #[test] |
| 66 | + fn test_nullif_str_array() { |
| 67 | + let a = GenericStringArray::<i32>::from(vec![ |
| 68 | + Some("1"), |
| 69 | + None, |
| 70 | + Some("2"), |
| 71 | + Some("3"), |
| 72 | + Some("4"), |
| 73 | + ]); |
| 74 | + let b = GenericStringArray::<i32>::from(vec![ |
| 75 | + None, |
| 76 | + None, |
| 77 | + Some("2"), |
| 78 | + Some("a"), |
| 79 | + Some("b"), |
| 80 | + ]); |
| 81 | + |
| 82 | + let expected = GenericStringArray::<i32>::from(vec![ |
| 83 | + Some("1"), |
| 84 | + None, |
| 85 | + None, |
| 86 | + Some("3"), |
| 87 | + Some("4"), |
| 88 | + ]); |
| 89 | + |
| 90 | + match nullif_func_str(&[ |
| 91 | + ColumnarValue::Array(Arc::new(a) as ArrayRef), |
| 92 | + ColumnarValue::Array(Arc::new(b) as ArrayRef), |
| 93 | + ]) |
| 94 | + .unwrap() |
| 95 | + { |
| 96 | + ColumnarValue::Array(arr) => { |
| 97 | + let we = arr |
| 98 | + .as_any() |
| 99 | + .downcast_ref::<StringArray>() |
| 100 | + .expect("failed to downcast array"); |
| 101 | + assert_eq!(&expected, we); |
| 102 | + } |
| 103 | + _ => panic!("test_nullif_str_array failed"), |
| 104 | + } |
| 105 | + |
| 106 | + let a = |
| 107 | + GenericStringArray::<i64>::from(vec![Some("FOO"), Some("BAR"), Some("KEK")]); |
| 108 | + |
| 109 | + let expected = |
| 110 | + GenericStringArray::<i64>::from(vec![Some("FOO"), Some("BAR"), None]); |
| 111 | + |
| 112 | + match nullif_func_str(&[ |
| 113 | + ColumnarValue::Array(Arc::new(a) as ArrayRef), |
| 114 | + ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some("KEK".to_string()))), |
| 115 | + ]) |
| 116 | + .unwrap() |
| 117 | + { |
| 118 | + ColumnarValue::Array(arr) => { |
| 119 | + let we = arr |
| 120 | + .as_any() |
| 121 | + .downcast_ref::<LargeStringArray>() |
| 122 | + .expect("failed to downcast array"); |
| 123 | + assert_eq!(&expected, we); |
| 124 | + } |
| 125 | + _ => panic!("test_nullif_str_array failed"), |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments