Skip to content

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

@Jefffrey

Description

@Jefffrey

Describe the bug

Test cases:

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
> 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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions