Cloud

map

The map data type stores a collection of key-value pairs with a uniform key type and a uniform value type. Because PostgreSQL has no map type, Redpanda SQL represents a map on the wire as an array of (key, value) pairs, in both the text and binary formats.

Keys and values

A map’s key type and value type are fixed for the whole map. Keys are never nullable: the key type is always a required type. Values can be nullable or required, depending on the source schema or how you build the map.

Syntax

Build a map value with the map function, passing alternating keys and values:

map(key1, value1 [, key2, value2 ... ])

Values can be literals, composite ROW(…​) values, or NULL.

Cast to a map type

To change a map’s key type or value type, cast it to a MAP(key_type, value_type) type with CAST or the :: operator:

SELECT CAST(map('a', 1) AS MAP(text, bigint));
SELECT (map('a', 1)::MAP(text, text))['a'];

The key type and the value type can be any type, including an array or another map. A map-to-map cast casts the keys and the values independently. Redpanda SQL casts each key to the target key type and each value to the target value type. The cast succeeds only when both the key cast and the value cast are possible. If either one is impossible, the whole cast fails. For example, map('a', 1)::MAP(text, geography) fails because an integer cannot be cast to geography.

An array of maps is not supported. A type such as MAP(text, int)[] returns the error Arrays of maps are not supported.

Cast an array of pairs to a map

You can cast an array of key-value pairs to a map. Then you can look up a value by its key with the subscript operator. Use this cast for an array that comes from a topic, such as the record headers or an Avro array of two-field records. See Look up a header by name.

The array must meet these requirements:

  • Each element of the array has a composite type with exactly two fields, such as an Avro record or a Protobuf message with two fields. The first field is the key. The second field is the value. The field names don’t matter.

  • The first field has a supported map key type: an integer, floating-point, decimal, boolean, text, byte-string, timestamp, date, time, UUID, or oid type. The key type of the target map is also one of these types.

  • Each field can be cast to its target type.

An array of anonymous ROW(…​) values, such as ARRAY[ROW('a', 1)], can’t be cast to a map.

The cast is explicit only. Use CAST or the :: operator. Redpanda SQL never casts an array to a map implicitly.

In this example, tags is an array of records with two text fields:

SELECT (tags::MAP(text, text))['color']
FROM default_redpanda_catalog=>products;

If the array is NULL, the cast returns NULL.

Redpanda SQL checks each array for NULL entries when the query runs:

  • If an element of the array is NULL, the query fails with the error map entries cannot be NULL.

  • If the key of an element is NULL, the query fails with the error map keys cannot be NULL.

  • The value of an element can be NULL.

If a key appears more than once in the array, the last matching pair wins. See Duplicate keys.

Create a map

SELECT map('a', 1, 'b', 2);

Redpanda SQL displays a map as an array of (key, value) pairs, with each pair shown as a quoted composite value:

       map
-------------------
 {"(a,1)","(b,2)"}
(1 row)

A value can be NULL. For a pair with a null value, Redpanda SQL shows nothing after the comma:

SELECT map('a', 1, 'b', NULL);
       map
------------------
 {"(a,1)","(b,)"}
(1 row)

A value can be a composite ROW(…​), which Redpanda SQL displays as a quoted, nested composite:

SELECT map('a', ROW(1, 2));
        map
-------------------
 {"(a,\"(1,2)\")"}
(1 row)

Because a map is displayed like an array of pairs, use pg_typeof to confirm that a value is a map:

SELECT pg_typeof(map('a', 1));
     pg_typeof
-------------------
 map(text,integer)
(1 row)

Access map values

Retrieve a value by its key with the subscript operator. When you subscript an inline map() constructor, wrap the constructor in parentheses so the subscript applies to the map it returns:

SELECT (map('a', 1, 'b', 2))['a'];

This returns the value 1.

Subscript a map column directly, without parentheses. See Query map columns from a topic.

If the key is not present in the map, the subscript returns NULL:

SELECT (map('a', 1))['z'];

This returns NULL.

A NULL lookup key is also treated as a key miss and returns NULL, even though a map’s keys are never null:

SELECT (map('a', 1))[NULL];

This returns NULL.

Looking up a key always returns a nullable result, because a lookup that doesn’t match a key returns NULL. If the value type is nullable, the result has the same type. If the value type is required, the result is the nullable form of that type.

Duplicate keys

A map can contain duplicate keys. Redpanda SQL preserves every pair you provide, in order, and does not deduplicate or compact them. This keeps a map loaded from Iceberg identical to the one stored.

When you look up a key that appears more than once, the last matching pair wins:

SELECT (map('a', 1, 'a', 2))['a'];

This returns 2.

Because the last pair wins, a later pair that sets the key to NULL shadows any earlier value for that key. A key whose winning value is NULL is indistinguishable from a missing key on lookup, because both return NULL. The two differ only when you display the whole map, which still shows every stored pair, including the ones with null values.

Duplicate keys can also come from a source schema. Redpanda SQL applies the same last-pair-wins rule to those keys.

Query map columns from a topic

When a topic’s registered schema includes a map field (Avro or Protobuf), Redpanda SQL maps it to a map column when you create the table. Because a map is a composite type, the table’s struct_mapping_policy controls this mapping: the default COMPOUND policy produces a map column, while the JSON policy stores the field as JSON, as it does for any composite type. The map’s keys and values take the SQL types that correspond to the source key and value types. For an Iceberg-enabled topic, Redpanda SQL reads map columns from both the topic’s live records and its Iceberg-committed history.

Access values in a map column with the subscript operator:

SELECT attributes['color'] FROM products;

Limitations

Maps have the following restrictions, which apply independently.

  • Building a map with map()

    • A map key must be a supported scalar type, such as a text, integer, floating-point, decimal, boolean, timestamp, date, UUID, or byte-string type. You can’t use composite types (row), arrays, or maps as keys.

    • A decimal key or value requires an explicit cast, such as 1.5::NUMERIC(5, 2). A bare number literal with a decimal point, such as 1.5, is a floating-point value.

    • map() accepts only literal keys and values. Values can be ROW(…​) literals. Column references and other non-literal expressions aren’t supported.

    • map() requires an even number of arguments, alternating keys and values.

  • Subscripting with []

    • The key you subscript a map with must be a supported scalar type. A map whose key type is a composite (row) can be loaded from a source schema but can’t be subscripted.

    • To look up a decimal key, cast the lookup key to the map’s key type, such as m['2.50'::NUMERIC(10, 2)]. A lookup key such as 1.5 is a floating-point value, so the lookup fails with the error cannot implicitly cast from real to numeric(10,2).

    • Slicing a map, such as ['a':'b'], is not supported.

  • Using a map in a query

    • A map can’t be used in GROUP BY, ORDER BY, or DISTINCT, because the type has no equality or ordering operator.

    • A map can’t be cast to text.

  • Loading a map from Iceberg

    • Map columns loaded from an Iceberg-enabled topic have no map-specific restrictions. You can load struct, array, and map keys and values, including maps nested inside structs.

    • The only unsupported shapes are those that the array rules forbid: a list whose element is another list fails with Multi-dimensional arrays are not supported, and a list whose element is a map fails with Arrays of maps are not supported. Both errors occur at CREATE TABLE or REFRESH.

Suggested reading

  • CREATE TABLE: Maps a Redpanda topic to a SQL table. Map fields in the topic’s registered schema become map columns.

  • array: The single-type collection that a map is presented as on the wire.

  • row: The composite type used for each key-value pair.

  • Query Iceberg-enabled topics: Query a topic together with its Iceberg-committed history.