Spark SQL - element_at Function

visibility 400 event 2022-06-04 access_time 7 months ago language English
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
copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts