Kontext Kontext | Code Snippets & Tips

Spark SQL - element_at Function

event 2022-06-04 visibility 1,487 comment 0 insights toc
more_vert
insights Stats
toc Table of contents

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