MAP
MAP is a key-value pair data type used to store and retrieve data associated with specific keys. Each key in a MAP uniquely corresponds to one value.
Syntax
Type Declaration
Constructor
Parameter Description
keyType: The data type of the key, supporting basic data types (such asSTRING,INT,BIGINT, etc.).valueType: The data type of the value, supporting basic data types and complex types.key1, value1, key2, value2, ...: One or more key-value pairs, with keys and values alternating; the number of arguments must be even.
Accessing MAP Elements
Use bracket syntax to access the value corresponding to a key:
If the key does not exist, NULL is returned.
Usage Examples
1. Constructing a MAP
Map employee names to departments:
2. Accessing MAP Elements
3. Using MAP Columns in a Table
Create a table and insert data:
Access values in the MAP column by key:
4. MAP-Related Functions
MAP_KEYS — Returns an array of all keys in the MAP:
MAP_VALUES — Returns an array of all values in the MAP:
CARDINALITY / SIZE — Returns the number of key-value pairs in the MAP:
Notes
- Key types must be comparable basic data types (such as
STRING,INT,BIGINT,DOUBLE, etc.); complex types (such asARRAY,MAP) are not supported as keys. - All keys within the same MAP must be of the same type, and all values must also be of the same type.
- The number of arguments to the constructor
MAP(key1, value1, ...)must be even; otherwise, an error is raised. - If the same key appears multiple times, the later value overwrites the earlier one.
- Accessing a non-existent key returns
NULLwithout raising an error. - The order of arrays returned by
MAP_KEYSandMAP_VALUESmatches the insertion order, but cross-version stability is not guaranteed.
