apache / apache/datafusion

`array_position` doesn't check nulls in array index & fails to handle nulls properly

Open
#21,792 1 comment 0 reactions 1 assignee Claimed by @Jefffrey View on GitHub
bug
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Describe the bug

Test cases:

```sql
select array_position(haystack, needle, index_from)
from values
([1, 2], 1, 2),
([1, 2], 1, null)
t(haystack, needle, index_from)
```

- Currently this fails because `0` is out of bounds, but this is only because by default the value underneath a null is usually 0; we can construct an array where this isn't the case and pass in a valid in-bounds value even if it should be masked by a null
- This should instead fail by checking nulls, as scalar path also raises exec error if it detects a null for the index argument

```sql
> select array_position(haystack, needle), array_positions(haystack, needle)
from values
([[1], null], null),
([[]], null)
t(haystack, needle);
+-------------------------------------+--------------------------------------+
| array_position(t.haystack,t.needle) | array_positions(t.haystack,t.needle) |
+-------------------------------------+--------------------------------------+
| NULL | [] |
| 1 | [1] |
+-------------------------------------+--------------------------------------+
```

- This should return `2 | [2]` for the first row and `NULL | []` for the second row
- This is because in the first row, we can find a null element so we return that position
- And for the second row we can't find any null, so we shouldn't return a position

### To Reproduce

_No response_

### Expected behavior

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.