Apache DataFusion SQL Query Engine
Fix `array_position` on empty list (#16292)
Arttu39c100a0151e
diff --git a/datafusion/functions-nested/src/position.rs b/datafusion/functions-nested/src/position.rsindex b186b65407..76ee7133d3 100644--- a/datafusion/functions-nested/src/position.rs+++ b/datafusion/functions-nested/src/position.rs@@ -52,7 +52,7 @@ make_udf_expr_and_func!(#[user_doc(doc_section(label = "Array Functions"),- description = "Returns the position of the first occurrence of the specified element in the array.",+ description = "Returns the position of the first occurrence of the specified element in the array, or NULL if not found.",syntax_example = "array_position(array, element)\narray_position(array, element, index)",sql_example = r#"```sql> select array_position([1, 2, 2, 3, 1, 4], 2);@@ -76,7 +76,10 @@ make_udf_expr_and_func!(name = "element",description = "Element to search for position in the array."),- argument(name = "index", description = "Index at which to start searching.")+ argument(+ name = "index",+ description = "Index at which to start searching (1-indexed)."+ ))]#[derive(Debug)]pub struct ArrayPosition {@@ -170,7 +173,7 @@ fn general_position_dispatch<O: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<Ar// if `start_from` index is out of bounds, return errorfor (arr, &from) in list_array.iter().zip(arr_from.iter()) {if let Some(arr) = arr {- if from < 0 || from as usize >= arr.len() {+ if from < 0 || from as usize > arr.len() {return internal_err!("start_from index out of bounds");}} else {diff --git a/datafusion/sqllogictest/test_files/array.slt b/datafusion/sqllogictest/test_files/array.sltindex ac96daed0d..4b07b286b7 100644--- a/datafusion/sqllogictest/test_files/array.slt+++ b/datafusion/sqllogictest/test_files/array.slt@@ -3367,10 +3367,16 @@ select array_concat(make_array(column3), column1, column2) from arrays_values_v2## array_position (aliases: `list_position`, `array_indexof`, `list_indexof`)## array_position with NULL (follow PostgreSQL)-#query I-#select array_position([1, 2, 3, 4, 5], null), array_position(NULL, 1);-#-----#NULL NULL+query II+select array_position([1, 2, 3, 4, 5], arrow_cast(NULL, 'Int64')), array_position(arrow_cast(NULL, 'List(Int64)'), 1);+----+NULL NULL++# array_position with no match (incl. empty array) returns NULL+query II+select array_position([], 1), array_position([2], 1);+----+NULL NULL# array_position scalar function #1query IIIdiff --git a/docs/source/user-guide/sql/scalar_functions.md b/docs/source/user-guide/sql/scalar_functions.mdindex cbcec710e2..e1a741e71a 100644--- a/docs/source/user-guide/sql/scalar_functions.md+++ b/docs/source/user-guide/sql/scalar_functions.md@@ -3142,7 +3142,7 @@ array_pop_front(array)### `array_position`-Returns the position of the first occurrence of the specified element in the array.+Returns the position of the first occurrence of the specified element in the array, or NULL if not found.```sqlarray_position(array, element)@@ -3153,7 +3153,7 @@ array_position(array, element, index)- **array**: Array expression. Can be a constant, column, or function, and any combination of array operators.- **element**: Element to search for position in the array.-- **index**: Index at which to start searching.+- **index**: Index at which to start searching (1-indexed).#### Example