Skip to content

Commit

Permalink
fix: some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
izveigor committed Jul 30, 2023
1 parent 7ad25bf commit 8a83f33
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
2 changes: 0 additions & 2 deletions datafusion/core/tests/sql/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ async fn query_nested_get_indexed_field_on_struct() -> Result<()> {
"| i0 |",
"+----+",
"| 0 |",
"| 4 |",
"| 8 |",
"+----+",
];
assert_batches_eq!(expected, &actual);
Expand Down
52 changes: 29 additions & 23 deletions datafusion/core/tests/sqllogictests/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -591,103 +591,109 @@ select array_slice(make_array(1, 2, 3, 4, 5), 2, 6), array_slice(make_array('h',
----
[2, 3, 4, 5] [l, l, o]

# array_slice scalar function #6 (with zero and positive number)
# array_slice scalar function #6 (with negative indexes; nested array)
query ?
select array_slice(make_array(make_array(1, 2, 3, 4, 5), make_array(6, 7, 8, 9, 10)), 1, 1);
----
[[1, 2, 3, 4, 5]]

# array_slice scalar function #7 (with zero and positive number)
query ??
select array_slice(make_array(1, 2, 3, 4, 5), 0, 4), array_slice(make_array('h', 'e', 'l', 'l', 'o'), 0, 3);
----
[1, 2, 3, 4] [h, e, l]

# array_slice scalar function #7 (with NULL and positive number)
# array_slice scalar function #8 (with NULL and positive number)
query error
select array_slice(make_array(1, 2, 3, 4, 5), NULL, 4), array_slice(make_array('h', 'e', 'l', 'l', 'o'), NULL, 3);
----
[1, 2, 3, 4] [h, e, l]

# array_slice scalar function #8 (with positive number and NULL)
# array_slice scalar function #9 (with positive number and NULL)
query error
select array_slice(make_array(1, 2, 3, 4, 5), 2, NULL), array_slice(make_array('h', 'e', 'l', 'l', 'o'), 3, NULL);
----
[2, 3, 4, 5] [l, l, o]

# array_slice scalar function #9 (with zero-zero)
# array_slice scalar function #10 (with zero-zero)
query ??
select array_slice(make_array(1, 2, 3, 4, 5), 0, 0), array_slice(make_array('h', 'e', 'l', 'l', 'o'), 0, 0);
----
[] []

# array_slice scalar function #10 (with NULL-NULL)
# array_slice scalar function #11 (with NULL-NULL)
query error
select array_slice(make_array(1, 2, 3, 4, 5), NULL), array_slice(make_array('h', 'e', 'l', 'l', 'o'), NULL);
----
[] []

# array_slice scalar function #11 (with zero and negative number)
# array_slice scalar function #12 (with zero and negative number)
query ??
select array_slice(make_array(1, 2, 3, 4, 5), 0, -4), array_slice(make_array('h', 'e', 'l', 'l', 'o'), 0, -3);
----
[1] [h, e]

# array_slice scalar function #12 (with negative number and NULL)
# array_slice scalar function #13 (with negative number and NULL)
query error
select array_slice(make_array(1, 2, 3, 4, 5), 2, NULL), array_slice(make_array('h', 'e', 'l', 'l', 'o'), 3, NULL);
----
[4, 5] [l, l, o]
[2, 3, 4, 5] [l, l, o]

# array_slice scalar function #13 (with NULL and negative number)
# array_slice scalar function #14 (with NULL and negative number)
query error
select array_slice(make_array(1, 2, 3, 4, 5), NULL, -4), array_slice(make_array('h', 'e', 'l', 'l', 'o'), NULL, -3);
----
[1] [h, e]

# array_slice scalar function #14 (with negative indexes)
# array_slice scalar function #15 (with negative indexes)
query ??
select array_slice(make_array(1, 2, 3, 4, 5), -4, -1), array_slice(make_array('h', 'e', 'l', 'l', 'o'), -3, -1);
----
[2, 3, 4] [l, l]

# array_slice scalar function #15 (with negative indexes; almost full array (only with negative indices cannot return full array))
# array_slice scalar function #16 (with negative indexes; almost full array (only with negative indices cannot return full array))
query ??
select array_slice(make_array(1, 2, 3, 4, 5), -5, -1), array_slice(make_array('h', 'e', 'l', 'l', 'o'), -5, -1);
----
[1, 2, 3, 4] [h, e, l, l]

# array_slice scalar function #16 (with negative indexes; first index = second index)
# array_slice scalar function #17 (with negative indexes; first index = second index)
query ??
select array_slice(make_array(1, 2, 3, 4, 5), -4, -4), array_slice(make_array('h', 'e', 'l', 'l', 'o'), -3, -3);
----
[] []

# array_slice scalar function #17 (with negative indexes; first index > second_index)
# array_slice scalar function #18 (with negative indexes; first index > second_index)
query ??
select array_slice(make_array(1, 2, 3, 4, 5), -4, -6), array_slice(make_array('h', 'e', 'l', 'l', 'o'), -3, -6);
----
[] []

# array_slice scalar function #18 (with negative indexes; out of bounds)
# array_slice scalar function #19 (with negative indexes; out of bounds)
query ??
select array_slice(make_array(1, 2, 3, 4, 5), -7, -2), array_slice(make_array('h', 'e', 'l', 'l', 'o'), -7, -3);
----
[] []

# array_slice scalar function #19 (with negative indexes; nested array)
# array_slice scalar function #20 (with negative indexes; nested array)
query ?
select array_slice(make_array(make_array(1, 2, 3, 4, 5), make_array(6, 7, 8, 9, 10)), 1, 1);
select array_slice(make_array(make_array(1, 2, 3, 4, 5), make_array(6, 7, 8, 9, 10)), -2, -1);
----
[[1, 2, 3, 4, 5]]

# array_slice scalar function #20 (with first positive index and last negative index)
# array_slice scalar function #21 (with first positive index and last negative index)
query ??
select array_slice(make_array(1, 2, 3, 4, 5), -2, 5), array_slice(make_array('h', 'e', 'l', 'l', 'o'), -3, 4);
select array_slice(make_array(1, 2, 3, 4, 5), 2, -3), array_slice(make_array('h', 'e', 'l', 'l', 'o'), 2, -2);
----
[4, 5] [l, l]
[2] [e, l]

# array_slice scalar function #21 (with first negative index and last positive index)
# array_slice scalar function #22 (with first negative index and last positive index)
query ??
select array_slice(make_array(1, 2, 3, 4, 5), 2, -3), array_slice(make_array('h', 'e', 'l', 'l', 'o'), 2, -2);
select array_slice(make_array(1, 2, 3, 4, 5), -2, 5), array_slice(make_array('h', 'e', 'l', 'l', 'o'), -3, 4);
----
[2] [e, l]
[4, 5] [l, l]

# list_slice scalar function #22 (function alias `array_slice`)
# list_slice scalar function #23 (function alias `array_slice`)
query ??
select list_slice(make_array(1, 2, 3, 4, 5), 2, 4), list_slice(make_array('h', 'e', 'l', 'l', 'o'), 1, 2);
----
Expand Down
4 changes: 3 additions & 1 deletion datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub enum Expr {
/// arithmetic negation of an expression, the operand must be of a signed numeric data type
Negative(Box<Expr>),
/// Returns the field of a [`arrow::array::ListArray`] or [`arrow::array::StructArray`] by key
///
GetIndexedField(GetIndexedField),
/// Whether an expression is between a given range.
Between(Between),
Expand Down Expand Up @@ -358,7 +359,8 @@ impl ScalarUDF {
}
}

/// Returns the field of a [`arrow::array::ListArray`] or [`arrow::array::StructArray`] by key
/// Returns the field of a [`arrow::array::ListArray`] or [`arrow::array::StructArray`] by `key`.
/// If `extra_key` is not `None`, returns the slice of a [`arrow::array::ListArray`] in the range from `key` to `extra_key`.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct GetIndexedField {
/// the expression to take the field from
Expand Down
2 changes: 1 addition & 1 deletion datafusion/expr/src/field_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use arrow::datatypes::{DataType, Field};
use datafusion_common::{DataFusionError, Result};

/// Returns the field access indexed by `key` from a [`DataType::List`] or [`DataType::Struct`]
/// Returns the field access indexed by `key` and/or `extra_key` from a [`DataType::List`] or [`DataType::Struct`]
/// # Error
/// Errors if
/// * the `data_type` is not a Struct or a List,
Expand Down

0 comments on commit 8a83f33

Please sign in to comment.