Returns the value that meets a specific condition in a map.
Usage notes
- If var1 is of the
map<K, V>
type, this function obtains the element that is at position var2 in var1. The position numbers of elements are counted from left to right and start from 0. - If var1 is of the
map<K, V>
type, this function obtains the value whose key is var2 in var1.
Note When you use this function, you must remove
index
and directly execute <var1>[<var2>]
. Otherwise, an error is returned.
Syntax
index(<var1>[<var2>])
Parameters
- var1: required. The value must be of the
array<T>
ormap<K, V>
type.T
inarray<T>
specifies the data type of the elements in an array. The elements can be of any data type.K
andV
inmap<K, V>
specify the keys and values of a map. - var2: required.
- If var1 is of the
array<T>
type, var2 must be of the BIGINT type and greater than or equal to 0. - If var1 is of the
map<K, V>
type, var2 must be of the same data type as K.
- If var1 is of the
Return value
- If var1 is of the
array<T>
type, a value of the data type that is specified by T is returned. The return value varies based on the following rules:- If the number of elements in var1 is less than var2, null is returned.
- If var1 is null, null is returned.
- If var1 is of the
map<K, V>
type, a value that is of the same data type as V is returned. The return value varies based on the following rules:- If
map<K, V>
does not contain a key whose value is var2, null is returned. - If var1 is null, null is returned.
- If
Examples
- Example 1: var1 is of the
array<T>
type. Sample statement:-- c is returned. select array('a','b','c')[2];
- Example 2: var1 is of the
map<K, V>
type. Sample statement:-- The value 1 is returned. select str_to_map("test1=1,test2=2")["test1"];