ELEMENT_AT
Function
The ELEMENT_AT function is used to extract the element value from an array, map, struct, or variant based on the specified index or key.
- When applied to an ARRAY, it returns the element at the specified position.
- When applied to a MAP, it returns the value corresponding to the specified key.
- When applied to a STRUCT, it returns the subfield at the specified position (starting from 1) or with the specified field name, equivalent to
STRUCT_ELEMENT. - When applied to a VARIANT, it returns the value of the specified subfield.
Syntax
ELEMENT_AT(container, key_or_index)
Parameters
container: Can beARRAY,MAP,STRUCT, orVARIANT.key_or_index:- For
ARRAY: An integer, with indexing starting from 1. - For
MAP: The key type (K) of theMAP, which can be any supported primitive type. - For
STRUCT: A constant integer field position (starting from 1) or a constant string field name (matched case-insensitively). - For
VARIANT: A string type.
- For
Return Value
- For
ARRAY: Returns the element at the specified index (Ttype). - For
MAP: Returns the value corresponding to the specified key (Vtype). - For
STRUCT: Returns the specified subfield value. - For
VARIANT: Returns aVARIANTtype value. - If the index or key does not exist, returns
NULL(forSTRUCT, an out-of-bound position or a non-existent field name reports an error). - If the parameter is
NULL, returnsNULL.
Notes
- Array indexes start from 1, not 0.
- Negative indexes are supported:
-1represents the last element,-2the second-to-last, and so on. - The
ELEMENT_AT(container, key_or_index)function behaves the same ascontainer[key_or_index](see examples for details).
Examples
-
The
ELEMENT_ATfunction works the same as[].SELECT ELEMENT_AT([1, 2, 3], 2);+--------------------------+| ELEMENT_AT([1, 2, 3], 2) |+--------------------------+| 2 |+--------------------------+SELECT [1, 2, 3][2];+--------------+| [1, 2, 3][2] |+--------------+| 2 |+--------------+ -
Array indexing starts from 1; out-of-bounds access returns
NULL.SELECT ELEMENT_AT([1, 2, 3], 0);+--------------------------+| ELEMENT_AT([1, 2, 3], 0) |+--------------------------+| NULL |+--------------------------+SELECT ELEMENT_AT([1, 2, 3], 4);+--------------------------+| ELEMENT_AT([1, 2, 3], 4) |+--------------------------+| NULL |+--------------------------+ -
Accessing a non-existent KEY in a
MAPreturnsNULL.SELECT ELEMENT_AT({"a": 1, "b": 2}, "c");+-----------------------------------+| ELEMENT_AT({"a": 1, "b": 2}, "c") |+-----------------------------------+| NULL |+-----------------------------------+ -
Accessing a
STRUCTsubfield by position or by field name (equivalent toSTRUCT_ELEMENT).SELECT ELEMENT_AT(NAMED_STRUCT('name', 'Jack', 'id', 1728923), 1);+------------------------------------------------------------+| ELEMENT_AT(NAMED_STRUCT('name', 'Jack', 'id', 1728923), 1) |+------------------------------------------------------------+| Jack |+------------------------------------------------------------+SELECT NAMED_STRUCT('name', 'Jack', 'id', 1728923)['id'];+--------------------------------------------------+| NAMED_STRUCT('name', 'Jack', 'id', 1728923)['id'] |+--------------------------------------------------+| 1728923 |+--------------------------------------------------+ -
When accessing a subfield of a
VARIANT, if theVARIANTvalue is not an OBJECT, an empty value is returned.SELECT ELEMENT_AT(CAST('{"a": 1, "b": 2}' AS VARIANT), "a");+------------------------------------------------------+| ELEMENT_AT(CAST('{"a": 1, "b": 2}' AS VARIANT), "a") |+------------------------------------------------------+| 1 |+------------------------------------------------------+SELECT ELEMENT_AT(CAST('123' AS VARIANT), "");+----------------------------------------+| ELEMENT_AT(CAST('123' AS VARIANT), "") |+----------------------------------------+| |+----------------------------------------+