MAP

Construct a MAP value from alternating key-value arguments.

Category: collectionReturns: MAP<K, V>Dialect: Standard

Syntax

MAP(key1, value1, key2, value2, ...)

Description

## Overview Constructs a MAP value from alternating key and value arguments. MAP is the canonical literal constructor for map-typed values and is used wherever a map literal is needed: in projections, in DDL defaults, and as arguments to other map functions. Typical uses include building small attribute maps inline, assembling per-row dimension maps from column values, and composing maps that feed into MAP_CONCAT, MAP_KEYS, or MAP_VALUES. ## Behavior - Returns a MAP<K, V> whose entries are the (key_i, value_i) pairs given in argument order. - The total number of arguments must be even; odd counts raise an error. - All keys must share a compatible type; all values must share a compatible type. - Keys must be non-NULL; a NULL key raises a runtime error. - Duplicate keys: this engine resolves with last-write-wins, retaining the value from the rightmost occurrence. - Iteration order of the result is not guaranteed. - The empty form MAP() requires a type cast to establish the key and value types. ## Null and empty handling - NULL key raises an error. Keys cannot be NULL. - NULL value is stored under its paired key. - Empty MAP() requires a type cast because the key and value types are otherwise unknown. - Zero entries is a valid result when the cast is provided. ## Compatibility - Matches the SQL standard array/map convention for MAP literal construction. MAP_KV is an alias. - The bracket form {k1 -> v1, k2 -> v2} is not supported; always use function-call syntax.

Parameters

NameTypeDescription
keySpecifies one or more keys for the map. All keys must share a compatible type and must not be NULL.
valueSpecifies the value paired with the preceding key. All values must share a compatible type; NULL values are permitted.

Examples

-- String-keyed map
SELECT MAP('a', 1, 'b', 2, 'c', 3);  -- {a: 1, b: 2, c: 3}
-- Single entry
SELECT MAP('key', 'value');  -- {key: 'value'}
-- Integer keys
SELECT MAP(1, 'one', 2, 'two');  -- {1: 'one', 2: 'two'}
-- NULL value allowed
SELECT MAP('a', 1, 'b', CAST(NULL AS INT));  -- {a: 1, b: NULL}
-- Empty map (with cast to pin the type)
SELECT CAST(MAP() AS MAP<STRING, INT>);  -- {}
-- Build a per-session dimension map from columns
SELECT session_id, MAP('country', country, 'device', device) AS dims
FROM analytics.events.user_sessions;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →