Skip to content

Commit

Permalink
port test to sqllogictest
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Nov 6, 2023
1 parent 0e6c5c8 commit c6dabcf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 48 deletions.
48 changes: 0 additions & 48 deletions datafusion/physical-expr/src/array_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1846,8 +1846,6 @@ pub fn string_to_array<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef
mod tests {
use super::*;
use arrow::datatypes::Int64Type;
use arrow::util::pretty::pretty_format_columns;
use arrow_array::types::Int32Type;
use datafusion_common::cast::as_uint64_array;

#[test]
Expand Down Expand Up @@ -2789,52 +2787,6 @@ mod tests {
);
}

#[test]
fn test_array_replace_with_null() {
// ([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]
let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>([
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::<Int32Type, _, _>([
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(
Expand Down
31 changes: 31 additions & 0 deletions datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c6dabcf

Please sign in to comment.