Spark SQL - element_at Function

Kontext Kontext 0 1729 1.54 index 6/4/2022

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
spark-sql-function

Join the Discussion

View or add your thoughts below

Comments