Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
parkma99 committed Jun 11, 2023
1 parent ae649be commit 98ee834
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
54 changes: 48 additions & 6 deletions datafusion/core/tests/sqllogictests/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ select make_array()
[]

# array scalar function #7
query ? rowsort
select make_array(make_array())
query ?? rowsort
select make_array(make_array()), make_array(make_array(make_array()))
----
[[]]
[[]] [[[]]]

# array_append scalar function #1
query ? rowsort
Expand Down Expand Up @@ -109,6 +109,12 @@ select array_fill(1, make_array(1, 1, 1)), array_fill(2, make_array(2, 2, 2, 2,
----
[[[1]]] [[[[[2, 2], [2, 2]], [[2, 2], [2, 2]]], [[[2, 2], [2, 2]], [[2, 2], [2, 2]]]], [[[[2, 2], [2, 2]], [[2, 2], [2, 2]]], [[[2, 2], [2, 2]], [[2, 2], [2, 2]]]]]

# array_fill scalar function #3
query ?
select array_fill(1, make_array())
----
[]

# array_concat scalar function #1
query ?? rowsort
select array_concat(make_array(1, 2, 3), make_array(4, 5, 6), make_array(7, 8, 9)), array_concat(make_array([1], [2]), make_array([3], [4]));
Expand All @@ -133,13 +139,13 @@ select array_concat(make_array([[1]]), make_array([[2]]));
----
[[[1]], [[2]]]

# array_concat scalar function #6
# array_concat scalar function #5
query ? rowsort
select array_concat(make_array(2, 3), make_array());
----
[2, 3]

# array_concat scalar function #5
# array_concat scalar function #6
query ? rowsort
select array_concat(make_array(), make_array(2, 3));
----
Expand Down Expand Up @@ -181,6 +187,12 @@ select array_to_string([1, 1, 1], '1'), array_to_string([[1, 2], [3, 4], [5, 6]]
----
11111 1+2+3+4+5+6 3/\3/\3/\3/\3/\3/\3/\3/\3/\3/\3/\3

# array_to_string scalar function #3
query ?
select array_to_string(make_array(), ',')
----
(empty)

# cardinality scalar function
query III
select cardinality(make_array(1, 2, 3, 4, 5)), cardinality([1, 3, 5]), cardinality(make_array('h', 'e', 'l', 'l', 'o'));
Expand All @@ -193,7 +205,13 @@ select cardinality(make_array([1, 2], [3, 4], [5, 6])), cardinality(array_fill(3
----
6 18

# trim_array scalar function
# cardinality scalar function #3
query II
select cardinality(make_array()), cardinality(make_array(make_array()))
----
0 0

# trim_array scalar function #1
query ???
select trim_array(make_array(1, 2, 3, 4, 5), 2), trim_array(['h', 'e', 'l', 'l', 'o'], 3), trim_array([1.0, 2.0, 3.0], 2);
----
Expand All @@ -211,6 +229,12 @@ select array_concat(trim_array(make_array(1, 2, 3), 3), make_array(4, 5), make_a
----
[4, 5]

# trim_array scalar function #3
query ??
select trim_array(make_array(), 0), trim_array(make_array(), 1)
----
[] []

# array_length scalar function
query III rowsort
select array_length(make_array(1, 2, 3, 4, 5)), array_length(make_array(1, 2, 3)), array_length(make_array([1, 2], [3, 4], [5, 6]));
Expand All @@ -235,6 +259,12 @@ select array_length(array_fill(3, [3, 2, 5]), 1), array_length(array_fill(3, [3,
----
3 2 5 NULL

# array_length scalar function #5
query III rowsort
select array_length(make_array()), array_length(make_array(), 1), array_length(make_array(), 2)
----
0 0 NULL

# array_dims scalar function
query III rowsort
select array_dims(make_array(1, 2, 3)), array_dims(make_array([1, 2], [3, 4])), array_dims(make_array([[[[1], [2]]]]));
Expand All @@ -247,6 +277,12 @@ select array_dims(array_fill(2, [1, 2, 3])), array_dims(array_fill(3, [2, 5, 4])
----
[1, 2, 3] [2, 5, 4]

# array_dims scalar function #3
query II rowsort
select array_dims(make_array()), array_dims(make_array(make_array()))
----
[0] [1, 0]

# array_ndims scalar function
query III rowsort
select array_ndims(make_array(1, 2, 3)), array_ndims(make_array([1, 2], [3, 4])), array_ndims(make_array([[[[1], [2]]]]));
Expand All @@ -258,3 +294,9 @@ query II rowsort
select array_ndims(array_fill(1, [1, 2, 3])), array_ndims([[[[[[[[[[[[[[[[[[[[[1]]]]]]]]]]]]]]]]]]]]]);
----
3 21

# array_ndims scalar function #3
query II rowsort
select array_ndims(make_array()), array_ndims(make_array(make_array()))
----
1 2
15 changes: 13 additions & 2 deletions datafusion/physical-expr/src/array_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ pub fn array_fill(args: &[ColumnarValue]) -> Result<ColumnarValue> {
DataType::UInt16 => fill!(array_values, element, UInt16Array),
DataType::UInt32 => fill!(array_values, element, UInt32Array),
DataType::UInt64 => fill!(array_values, element, UInt64Array),
DataType::Null => {
return Ok(datafusion_expr::ColumnarValue::Scalar(
ScalarValue::new_list(Some(vec![]), DataType::Null),
))
}
data_type => {
return Err(DataFusionError::Internal(format!(
"Array_fill is not implemented for type '{data_type:?}'."
Expand Down Expand Up @@ -838,6 +843,7 @@ pub fn array_to_string(args: &[ColumnarValue]) -> Result<ColumnarValue> {
DataType::UInt16 => to_string!(arg, arr, &delimeter, UInt16Array),
DataType::UInt32 => to_string!(arg, arr, &delimeter, UInt32Array),
DataType::UInt64 => to_string!(arg, arr, &delimeter, UInt64Array),
DataType::Null => Ok(arg),
data_type => Err(DataFusionError::NotImplemented(format!(
"Array is not implemented for type '{data_type:?}'."
))),
Expand All @@ -846,8 +852,13 @@ pub fn array_to_string(args: &[ColumnarValue]) -> Result<ColumnarValue> {

let mut arg = String::from("");
let mut res = compute_array_to_string(&mut arg, arr, delimeter.clone())?.clone();
res.truncate(res.len() - delimeter.len());
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(res))))
match res.as_str() {
"" => Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(res)))),
_ => {
res.truncate(res.len() - delimeter.len());
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(res))))
}
}
}

/// Trim_array SQL function
Expand Down

0 comments on commit 98ee834

Please sign in to comment.