Categories:

Semi-structured and Structured Data Functions (Map)

MAP_CONTAINS_KEY

Determines whether the specified MAP contains the specified key.

Syntax

MAP_CONTAINS_KEY( <key> , <map> )
Copy

Arguments

key

The key to find.

map

The map to be searched.

Returns

  • Returns TRUE if the specified map contains the specified key.

  • Returns FALSE if the specified map does not contain the specified key.

Usage Notes

  • The type of the key expression must match the type of the map’s key. If the type is VARCHAR, the types can be different lengths.

  • For NULL input, the output is NULL.

Examples

The function searches for the k1 key and finds it in the map:

SELECT MAP_CONTAINS_KEY(
  'k1',{'k1':'v1','k2':'v2','k3':'v3'}::MAP(VARCHAR,VARCHAR))
  AS contains_key;
Copy
+--------------+
| CONTAINS_KEY |
|--------------|
| True         |
+--------------+

The function searches for the k1 key and doesn’t find it in the map:

SELECT MAP_CONTAINS_KEY(
  'k1',{'ka':'va','kb':'vb','kc':'vc'}::MAP(VARCHAR,VARCHAR))
  AS contains_key;
Copy
+--------------+
| CONTAINS_KEY |
|--------------|
| False        |
+--------------+

A SELECT statement passes in a key that uses a different type than the key in the map:

SELECT MAP_CONTAINS_KEY(
  'k1',{'1':'va','2':'vb','3':'vc'}::MAP(NUMBER,VARCHAR))
  AS contains_key;
Copy
001065 (22023): SQL compilation error:
Function MAP_CONTAINS_KEY cannot be used with arguments of types VARCHAR(2) and map(NUMBER
(38,0), VARCHAR(16777216))