MAP
MAP is a key-value pair data type used to store dynamic attributes and name-to-value mappings. Unlike STRUCT (which has fixed field names), MAP keys are determined at runtime, making it suitable for scenarios where the number of fields is not fixed.
Type selection reference for complex types:
| Type | Structure | Suitable Scenarios |
|---|---|---|
MAP<K,V> | Key-value pairs, uniform key type | Dynamic attributes, configuration items, name-to-value mappings |
STRUCT<f:T,...> | Named fields, varying types | Fixed-structure objects where field names are determined at table creation |
ARRAY<T> | Ordered list, uniform element type | Tags, scores, ID lists |
Syntax and Definition
Type Declaration
keyType: Key type, supports basic types (STRING,INT,BIGINT,DOUBLE, etc.), does not supportARRAY,MAP, orSTRUCTvalueType: Value type, supports both basic types and complex types
Construction Methods
Element Access: Returns NULL when the key does not exist, no error is thrown:
Creating Tables and Writing Data
Query basic information:
| id | age | score | attr_count |
|---|---|---|---|
| 1 | 25 | 90 | 2 |
| 2 | 30 | 85 | 2 |
| 3 | 28 | NULL | 1 |
Common Functions
| Function | Description |
|---|---|
size(m) / cardinality(m) | Number of key-value pairs |
map_keys(m) | Returns all keys as an ARRAY |
map_values(m) | Returns all values as an ARRAY |
element_at(m, key) | Get value by key, equivalent to m[key] |
map_from_arrays(keys, values) | Construct a MAP from two arrays |
map_concat(m1, m2, ...) | Merge multiple MAPs; for duplicate keys, the latter value wins |
map_filter(m, (k,v) -> cond) | Retain key-value pairs that satisfy the condition |
transform_values(m, (k,v) -> expr) | Apply a function to each value, keys unchanged |
transform_keys(m, (k,v) -> expr) | Apply a function to each key, values unchanged |
map_from_arrays Example
map_concat Example
For duplicate keys, the latter value wins:
Higher-Order Function Examples
Aggregation and Expansion
MAP_AGG — Rows to MAP
Aggregate key-value pairs from multiple rows into a single MAP:
Use with GROUP BY to aggregate by group:
| dept | scores |
|---|---|
| Eng | {"Alice":95,"Bob":87} |
| HR | {"Carol":92} |
EXPLODE — Expand MAP to Rows
EXPLODE expands a MAP into multiple rows, one row per key-value pair:
| id | k | v |
|---|---|---|
| 1 | age | 25 |
| 1 | score | 90 |
| 2 | age | 30 |
| 2 | score | 85 |
| 3 | age | 28 |
Type Conversion
Format difference between CAST to STRING and TO_JSON:
NULL Handling
Notes
- MAP does not support the
=comparison operator and cannot be used asORDER BY,GROUP BY, or JOIN keys. - All keys within the same MAP must be of the same type, and all values must also be of the same type.
- Neither
MAP()normap_from_arrays()automatically deduplicates duplicate keys. Behavior with duplicate keys:map_keysretains all duplicate keys,m[key]returns the first matching value,TO_JSONretains all duplicate keys (e.g.,{"k":1,"k":2}). Ensure keys are unique before constructing a MAP. - The order of arrays returned by
map_keysandmap_valuesis consistent with the insertion order, but is not guaranteed to be stable across versions.
Related Documentation
- ARRAY: Ordered list of elements of the same type
- STRUCT: Named structure with multiple fields and types
- Data Type Conversion: Complex type conversion rules
