Construct a map from alternating key-value arguments.
MAP_KV(key1, value1, key2, value2, ...)
## Overview Constructs a map from alternating key and value arguments. MAP_KV is the explicit variadic constructor that pairs adjacent positional arguments as (key, value) entries and is an alias for the MAP constructor in this dialect. Typical uses include building small attribute maps inline in a projection, assembling maps from column values, and composing maps for downstream MAP_CONCAT or MAP_ENTRIES-based pipelines. ## Behavior - Returns a MAP<K, V> whose entries are built from each (key_i, value_i) pair in argument order. - The number of arguments must be even; an odd count raises a compile-time or runtime error. - All keys must be of the same type; all values must be of a compatible type. Result types are the common super-types. - Keys must be non-NULL. - Duplicate keys raise a runtime error rather than silently resolving. - Iteration order of the result is not guaranteed. ## Null and empty handling - NULL key argument raises an error; keys cannot be NULL in maps. - NULL value argument is stored as NULL under its paired key. - Zero-argument form requires an explicit cast to return an empty map of a known type: CAST(MAP_KV() AS MAP<STRING, INT>). - A mismatched argument count (odd total) raises an error. ## Compatibility - MAP_KV is an alias for MAP in this dialect. Other array/map SQL dialects may use the MAP constructor directly. - Equivalent to MAP_FROM_ARRAYS(ARRAY(key_args), ARRAY(value_args)) provided the keys are unique and non-NULL.
| Name | Type | Description |
|---|---|---|
key | Specifies one or more keys, each followed by its value. All keys must be of the same type and must not be NULL. | |
value | Specifies the value paired with the preceding key. All values must be of a compatible type; NULL is permitted. |
-- Build a simple attribute map
SELECT MAP_KV('name', 'Alice', 'age', '30'); -- {name: 'Alice', age: '30'}
-- Integer keys and values
SELECT MAP_KV(1, 100, 2, 200, 3, 300); -- {1: 100, 2: 200, 3: 300}
-- NULL value allowed
SELECT MAP_KV('a', 1, 'b', CAST(NULL AS INT)); -- {a: 1, b: NULL}
-- Single pair
SELECT MAP_KV('key', 'value'); -- {key: 'value'}
-- Build a per-row tag map inline from columns
SELECT session_id, MAP_KV('country', country, 'device', device) AS dimensions
FROM analytics.events.user_sessions;
-- Compose with MAP_CONCAT for overrides
SELECT MAP_CONCAT(MAP_KV('env', 'prod'), MAP_KV('env', 'staging')); -- {env: 'staging'}