diff --git a/datafusion/physical-expr/src/array_expressions.rs b/datafusion/physical-expr/src/array_expressions.rs index 3cf539b41624..45169e2d0ae5 100644 --- a/datafusion/physical-expr/src/array_expressions.rs +++ b/datafusion/physical-expr/src/array_expressions.rs @@ -1846,8 +1846,6 @@ pub fn string_to_array(args: &[ArrayRef]) -> Result [4, 1, NULL, 4] NULL not matched - // ([3, 1, NULL, 3], NULL, 5, 2) => [3, 1, NULL, 3] NULL not replaced (not eq) - // ([NULL], 3, 2, 1) => NULL - // ([3, 1, 3], 3, NULL, 1) => [NULL, 1 3] - let list_array = ListArray::from_iter_primitive::([ - Some(vec![Some(3), Some(1), None, Some(3)]), - Some(vec![Some(3), Some(1), None, Some(3)]), - None, - Some(vec![Some(3), Some(1), Some(3)]), - ]); - - let from = Int32Array::from(vec![Some(3), None, Some(3), Some(3)]); - let to = Int32Array::from(vec![Some(4), Some(5), Some(2), None]); - let n = vec![2, 2, 1, 1]; - - let expected = ListArray::from_iter_primitive::([ - Some(vec![Some(4), Some(1), None, Some(4)]), - Some(vec![Some(3), Some(1), None, Some(3)]), - None, - Some(vec![None, Some(1), Some(3)]), - ]); - - let list_array = Arc::new(list_array) as ArrayRef; - let from = Arc::new(from) as ArrayRef; - let to = Arc::new(to) as ArrayRef; - let expected = Arc::new(expected) as ArrayRef; - - let replaced = general_replace( - as_list_array(&list_array).unwrap(), - &from as &ArrayRef, - &to, - n, - ) - .unwrap(); - assert_eq!( - &replaced, - &expected, - "\n\n{}\n\n{}\n\n{}", - pretty_format_columns("input", &[Arc::new(list_array) as _]).unwrap(), - pretty_format_columns("replaced", &[replaced.clone()]).unwrap(), - pretty_format_columns("expected", &[expected.clone()]).unwrap(), - ); - } - #[test] fn test_nested_array_replace() { // array_replace( diff --git a/datafusion/sqllogictest/test_files/array.slt b/datafusion/sqllogictest/test_files/array.slt index b5601a22226c..ec67e9cb5690 100644 --- a/datafusion/sqllogictest/test_files/array.slt +++ b/datafusion/sqllogictest/test_files/array.slt @@ -1732,6 +1732,37 @@ select array_replace_all(make_array([1, 2, 3], [4, 5, 6], [4, 5, 6], [10, 11, 12 [[1, 2, 3], [4, 5, 6], [4, 5, 6], [10, 11, 12], [13, 14, 15], [10, 11, 12], [10, 11, 12], [28, 29, 30], [28, 29, 30], [28, 29, 30], [28, 29, 30], [22, 23, 24]] [[19, 20, 21], [19, 20, 21], [19, 20, 21], [22, 23, 24], [19, 20, 21], [25, 26, 27], [19, 20, 21], [22, 23, 24], [19, 20, 21], [19, 20, 21]] [[11, 12, 13], [11, 12, 13], [11, 12, 13], [22, 23, 24], [11, 12, 13], [25, 26, 27], [11, 12, 13], [22, 23, 24], [11, 12, 13], [11, 12, 13]] [[1, 2, 3], [4, 5, 6], [4, 5, 6], [10, 11, 12], [13, 14, 15], [10, 11, 12], [10, 11, 12], [19, 20, 21], [19, 20, 21], [37, 38, 39], [19, 20, 21], [22, 23, 24]] [[28, 29, 30], [31, 32, 33], [34, 35, 36], [28, 29, 30], [31, 32, 33], [34, 35, 36], [28, 29, 30], [31, 32, 33], [34, 35, 36], [28, 29, 30]] [[11, 12, 13], [31, 32, 33], [34, 35, 36], [11, 12, 13], [31, 32, 33], [34, 35, 36], [11, 12, 13], [31, 32, 33], [34, 35, 36], [11, 12, 13]] +# array_replace with null handling + +statement ok +create table t as values + (make_array(3, 1, NULL, 3), 3, 4, 2), + (make_array(3, 1, NULL, 3), NULL, 5, 2), + (NULL, 3, 2, 1), + (make_array(3, 1, 3), 3, NULL, 1) +; + + +# ([3, 1, NULL, 3], 3, 4, 2) => [4, 1, NULL, 4] NULL not matched +# ([3, 1, NULL, 3], NULL, 5, 2) => [3, 1, NULL, 3] NULL not replaced (not eq) +# ([NULL], 3, 2, 1) => NULL +# ([3, 1, 3], 3, NULL, 1) => [NULL, 1 3] + +query ?III? +select column1, column2, column3, column4, array_replace_n(column1, column2, column3, column4) from t; +---- +[3, 1, , 3] 3 4 2 [4, 1, , 4] +[3, 1, , 3] NULL 5 2 [3, 1, , 3] +NULL 3 2 1 NULL +[3, 1, 3] 3 NULL 1 [, 1, 3] + + + +statement ok +drop table t; + + + ## array_to_string (aliases: `list_to_string`, `array_join`, `list_join`) # array_to_string scalar function #1