Spark SQL - element_at Function

Kontext Kontext event 2022-06-04 visibility 1,631
more_vert

Spark SQL function element_at(array, index) returns element of array at given index. The index is 1-based (not 0-based).The function returns NULL if the index exceeds the length of the array and spark.sql.ansi.enabled is set to false; otherwise it throws ArrayIndexOutOfBoundsException for invalid indices.

element_at(map, key) returns value for given key. The function returns NULL if the key is not contained in the map and spark.sql.ansi.enabled is set to false; otherwise it throws NoSuchElementException instead.

Code snippets

The following are some examples using this function in Spark SQL:

spark-sql> select element_at(array(1,2,3,4,5),1);
element_at(array(1, 2, 3, 4, 5), 1)
1

For map objects:

spark-sql> select element_at(map(1,'A',2,'B',3,'C',4,'D'),1);
element_at(map(1, A, 2, B, 3, C, 4, D), 1)
A
More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts