Returns an element from an array by position, or a value from a map by key.
Syntax
Array access
<array_expr>[<index>]Map access
<map_expr>[<key>]Important
Use bracket notation directly. Do not wrap it in INDEX(...) — calling INDEX(<var1>[<var2>]) returns an error.
Usage notes
Array indexing is 0-based: the first element is at index
0, the second at1, and so on.For map access, the key must match the declared key type
Kof the map exactly.
Parameters
Array access
| Parameter | Type | Description |
|---|---|---|
array_expr | array<T> | The array to access. Returns null if the array is null. |
index | BIGINT, >= 0 | The 0-based position of the element to retrieve. Returns null if the index is out of bounds. |
Return type: T — the element type declared in array<T>.
Map access
| Parameter | Type | Description |
|---|---|---|
map_expr | map<K, V> | The map to access. Returns null if the map is null. |
key | Same type as K | The key to look up. Returns null if the key does not exist in the map. |
Return type: V — the value type declared in map<K, V>.
Examples
Access an array element by position
-- Returns 'c' (index 2 is the third element)
SELECT ARRAY('a', 'b', 'c')[2];Access a map value by key
-- Returns 1
SELECT STR_TO_MAP("test1=1,test2=2")["test1"];Related functions
INDEX is a complex type function. For more information about functions that process ARRAY, MAP, STRUCT, and JSON data, see Complex type functions.