Map functions - Amazon OpenSearch Service

Map functions

Note

To see which AWS data source integrations support this SQL command, see Supported OpenSearch SQL commands and functions.

Function Description
element_at(array, index) Returns element of array at given (1-based) index.
element_at(map, key) Returns value for given key. The function returns NULL if the key is not contained in the map.
map(key0, value0, key1, value1, ...) Creates a map with the given key/value pairs.
map_concat(map, ...) Returns the union of all the given maps
map_contains_key(map, key) Returns true if the map contains the key.
map_entries(map) Returns an unordered array of all entries in the given map.
map_from_arrays(keys, values) Creates a map with a pair of the given key/value arrays. All elements in keys should not be null
map_from_entries(arrayOfEntries) Returns a map created from the given array of entries.
map_keys(map) Returns an unordered array containing the keys of the map.
map_values(map) Returns an unordered array containing the values of the map.
str_to_map(text[, pairDelim[, keyValueDelim]]) Creates a map after splitting the text into key/value pairs using delimiters. Default delimiters are ',' for `pairDelim` and ':' for `keyValueDelim`. Both `pairDelim` and `keyValueDelim` are treated as regular expressions.
try_element_at(array, index) Returns element of array at given (1-based) index. If Index is 0, the system will throw an error. If index < 0, accesses elements from the last to the first. The function always returns NULL if the index exceeds the length of the array.
try_element_at(map, key) Returns value for given key. The function always returns NULL if the key is not contained in the map.

Examples

-- element_at SELECT element_at(array(1, 2, 3), 2); +-----------------------------+ |element_at(array(1, 2, 3), 2)| +-----------------------------+ | 2| +-----------------------------+ SELECT element_at(map(1, 'a', 2, 'b'), 2); +------------------------------+ |element_at(map(1, a, 2, b), 2)| +------------------------------+ | b| +------------------------------+ -- map SELECT map(1.0, '2', 3.0, '4'); +--------------------+ | map(1.0, 2, 3.0, 4)| +--------------------+ |{1.0 -> 2, 3.0 -> 4}| +--------------------+ -- map_concat SELECT map_concat(map(1, 'a', 2, 'b'), map(3, 'c')); +--------------------------------------+ |map_concat(map(1, a, 2, b), map(3, c))| +--------------------------------------+ | {1 -> a, 2 -> b, ...| +--------------------------------------+ -- map_contains_key SELECT map_contains_key(map(1, 'a', 2, 'b'), 1); +------------------------------------+ |map_contains_key(map(1, a, 2, b), 1)| +------------------------------------+ | true| +------------------------------------+ SELECT map_contains_key(map(1, 'a', 2, 'b'), 3); +------------------------------------+ |map_contains_key(map(1, a, 2, b), 3)| +------------------------------------+ | false| +------------------------------------+ -- map_entries SELECT map_entries(map(1, 'a', 2, 'b')); +----------------------------+ |map_entries(map(1, a, 2, b))| +----------------------------+ | [{1, a}, {2, b}]| +----------------------------+ -- map_from_arrays SELECT map_from_arrays(array(1.0, 3.0), array('2', '4')); +---------------------------------------------+ |map_from_arrays(array(1.0, 3.0), array(2, 4))| +---------------------------------------------+ | {1.0 -> 2, 3.0 -> 4}| +---------------------------------------------+ -- map_from_entries SELECT map_from_entries(array(struct(1, 'a'), struct(2, 'b'))); +---------------------------------------------------+ |map_from_entries(array(struct(1, a), struct(2, b)))| +---------------------------------------------------+ | {1 -> a, 2 -> b}| +---------------------------------------------------+ -- map_keys SELECT map_keys(map(1, 'a', 2, 'b')); +-------------------------+ |map_keys(map(1, a, 2, b))| +-------------------------+ | [1, 2]| +-------------------------+ -- map_values SELECT map_values(map(1, 'a', 2, 'b')); +---------------------------+ |map_values(map(1, a, 2, b))| +---------------------------+ | [a, b]| +---------------------------+ -- str_to_map SELECT str_to_map('a:1,b:2,c:3', ',', ':'); +-----------------------------+ |str_to_map(a:1,b:2,c:3, ,, :)| +-----------------------------+ | {a -> 1, b -> 2, ...| +-----------------------------+ SELECT str_to_map('a'); +-------------------+ |str_to_map(a, ,, :)| +-------------------+ | {a -> NULL}| +-------------------+ -- try_element_at SELECT try_element_at(array(1, 2, 3), 2); +---------------------------------+ |try_element_at(array(1, 2, 3), 2)| +---------------------------------+ | 2| +---------------------------------+ SELECT try_element_at(map(1, 'a', 2, 'b'), 2); +----------------------------------+ |try_element_at(map(1, a, 2, b), 2)| +----------------------------------+ | b| +----------------------------------+