All Products
Search
Document Center

MaxCompute:INDEX

Last Updated:Jul 21, 2023

Returns the value that meets a specific condition in a map.

Usage notes

  • If var1 is of the array<T> 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> or map<K, V> type. T in array<T> specifies the data type of the elements in an array. The elements can be of any data type. K and V in map<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.

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 of the data type that is specified by 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.

Examples

  • Example 1: var1 is of the array<T> type. Sample statement:

    -- The return value is c. 
    select array('a','b','c')[2];
  • Example 2: var1 is of the map<K, V> type. Sample statement:

    -- The return value is 1. 
    select str_to_map("test1=1,test2=2")["test1"];

Related functions

INDEX is a complex type function. For more information about the functions that are used to process data of complex data types, such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.