21.2. MAP Data #

This section describes how to query and transform MAP data.

MAP data is an ordered list of key-value pairs with a single key data type and a single value data type. The key data type does not need to match the value data type. Unlike STRUCT data, MAP does not require each row to contain the same keys. However, MAP data cannot contain duplicate keys and returns NULL for missing keys instead of an error.

21.2.1. Retrieving the Size of MAP Data (cardinality) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT cardinality(MAP)');

Where MAP is the MAP data whose size must be retrieved.

You can specify a column with the MAP data type or a MAP literal.

Postgres Pro AXE will return the number of key-value pairs in the MAP data.

Example 21.13. Retrieving the Size of MAP Data

Retrieving the size of MAP data with key-value pairs:

  SELECT * FROM axe.query('SELECT cardinality(MAP {''a'': 1, ''b'': 2, ''c'': 3})');
  3

Retrieving the size of empty MAP data:

  SELECT * FROM axe.query('SELECT cardinality(MAP {})');
  0

MAP data in the data column of the map_table table:

 id |          data
----+-------------------------
  1 | {'a': 1, 'b': 2, 'c': 3}

Retrieving the size of MAP data from a column:

  SELECT * FROM axe.query('SELECT cardinality(data) FROM map_table');
  3

21.2.2. Retrieving a Value from MAP Data (map_extract_value) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map_extract_value(MAP, key)');

Where:

  • MAP: The MAP data from which a value must be retrieved.

    You can specify a column with the MAP data type or a MAP literal.

  • key: The key for which a value must be retrieved.

Postgres Pro AXE will return the value for the specified key. If the data type of the key does not match the data type of the MAP key, an error is returned. If the MAP data does not contain the key, NULL is returned.

Example 21.14. Retrieving a Value from MAP Data

MAP data in the data column of the map_table table:

 id |          data
----+-------------------------
  1 | {'a': 42, 'b': 43}

Retrieving a value for an existing key:

  SELECT * FROM axe.query('SELECT map_extract_value(data, ''a'') FROM map_table');
  42

Retrieving a value for a missing key:

  SELECT * FROM axe.query('SELECT map_extract_value(data, ''c'') FROM map_table');
  NULL

21.2.3. Retrieving a Value from MAP Data as a List (map_extract) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map_extract(MAP, key)');

Where:

  • MAP: The MAP data from which a value must be retrieved.

    You can specify a column with the MAP data type or a MAP literal.

  • key: The key for which a value must be retrieved.

Postgres Pro AXE will return a list containing the value for the specified key. If the data type of the specified key does not match the data type of the MAP key, an error is returned. If the MAP data does not contain the key, an empty list is returned.

Example 21.15. Retrieving a Value from MAP Data as a List

MAP data in the data column of the map_table table:

 id |          data
----+-------------------------
  1 | {'a': 42, 'b': 43}

Retrieving a value for an existing key:

  SELECT * FROM axe.query('SELECT map_extract(data, ''a'') FROM map_table');
  [42]

Retrieving a value for a missing key:

  SELECT * FROM axe.query('SELECT map_extract(data, ''c'') FROM map_table');
  []

21.2.4. Concatenating MAP Values (map_concat) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map_concat(MAP[, MAP ...])');

Where MAP are comma-separated MAP values that must be concatenated.

You can specify columns with the MAP data type or MAP literals.

Postgres Pro AXE will concatenate the specified MAP values into a single MAP value. If multiple values contain the same key, the last value takes precedence.

Example 21.16. Concatenating MAP Values

MAP data in the data1 and data2 columns of the map_table table:

 id |       data1        |       data2
----+--------------------+--------------------
  1 | {'a': 1}           | {'b': 2}
  2 | {'a': 1, 'b': 2}   | {'b': 3, 'c': 4}

Concatenating two MAP values with distinct keys:

  SELECT * FROM axe.query('SELECT map_concat(data1, data2) FROM map_table WHERE id = 1');
  {a=1, b=2}

Concatenating two MAP values that contain the same key:

  SELECT * FROM axe.query('SELECT map_concat(data1, data2) FROM map_table WHERE id = 2');
  {a=1, b=3, c=4}

21.2.5. Checking a MAP Key (map_contains) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map_contains(MAP, key)');

Where:

  • MAP: The MAP data where the search must be performed.

    You can specify a column with the MAP data type or a MAP literal.

  • key: The key that must be searched.

Postgres Pro AXE will return true if the MAP data contains the specified key and false otherwise.

Example 21.17. Checking a MAP Key

MAP data in the data column of the map_table table:

 id |          data
----+-------------------------
  1 | {'a': 1, 'b': 2}

Checking MAP data for an existing key:

  SELECT * FROM axe.query('SELECT map_contains(data, ''a'') FROM map_table');
  true

Checking MAP data for a missing key:

  SELECT * FROM axe.query('SELECT map_contains(data, ''c'') FROM map_table');
  false

21.2.6. Checking a MAP Key-Value Pair (map_contains_entry) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map_contains_entry(MAP, key, value)');

Where:

  • MAP: The MAP data where the search must be performed.

    You can specify a column with the MAP data type or a MAP literal.

  • key: The key that must be searched.

  • value: The value that must be searched.

Postgres Pro AXE will return true if the MAP data contains the key-value pair and false otherwise.

Example 21.18. Checking a MAP Key-Value Pair

MAP data in the data column of the map_table table:

 id |          data
----+-------------------------
  1 | {'a': 1, 'b': 2}

Checking MAP data for an existing key-value pair:

  SELECT * FROM axe.query('SELECT map_contains_entry(data, ''a'', 1) FROM map_table');
  true

Checking MAP data for a missing key-value pair:

  SELECT * FROM axe.query('SELECT map_contains_entry(data, ''a'', 5) FROM map_table');
  false

21.2.7. Checking a MAP Value (map_contains_value) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map_contains_value(MAP, value)');

Where:

  • MAP: The MAP data where the search must be performed.

    You can specify a column with the MAP data type or a MAP literal.

  • value: The value that must be searched.

Postgres Pro AXE will return true if the MAP data contains the value and false otherwise.

Example 21.19. Checking a MAP Value

MAP data in the data column of the map_table table:

 id |          data
----+-------------------------
  1 | {'a': 1, 'b': 2}

Checking MAP data for an existing value:

  SELECT * FROM axe.query('SELECT map_contains_value(data, 2) FROM map_table');
  true

Checking MAP data for a missing value:

  SELECT * FROM axe.query('SELECT map_contains_value(data, 5) FROM map_table');
  false

21.2.8. Retrieving Key-Value Pairs from MAP Data (map_entries) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map_entries(MAP)');

Where MAP is the MAP data from which key-value pairs must be retrieved.

You can specify a column with the MAP data type or a MAP literal.

Postgres Pro AXE will return a list of STRUCT values containing the key-value pairs from the MAP data.

Example 21.20. Retrieving Key-Value Pairs from MAP Data

MAP data in the data column of the map_table table:

 id |          data
----+-------------------------
  1 | {'a': 1, 'b': 2, 'c': 3}
  SELECT * FROM axe.query('SELECT map_entries(data) FROM map_table');
  [{'key': 'a', 'value': 1}, {'key': 'b', 'value': 2}, {'key': 'c', 'value': 3}]

21.2.9. Retrieving Keys from MAP Data (map_keys) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map_keys(MAP)');

Where MAP is the MAP data from which keys must be retrieved.

You can specify a column with the MAP data type or a MAP literal.

Postgres Pro AXE will return a list of keys from the MAP data.

Example 21.21. Retrieving Keys from MAP Data

MAP data in the data column of the map_table table:

 id |          data
----+-------------------------
  1 | {'a': 1, 'b': 2, 'c': 3}
  SELECT * FROM axe.query('SELECT map_keys(data) FROM map_table');
  [a, b, c]

21.2.10. Retrieving Values from MAP Data (map_values) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map_values(MAP)');

Where MAP is the MAP data from which values must be retrieved.

You can specify a column with the MAP data type or a MAP literal.

Postgres Pro AXE will return a list of values from the MAP data.

Example 21.22. Retrieving Values from MAP Data

MAP data in the data column of the map_table table:

 id |          data
----+-------------------------
  1 | {'a': 1, 'b': 2, 'c': 3}
  SELECT * FROM axe.query('SELECT map_values(data) FROM map_table');
  [1, 2, 3]

21.2.11. Creating MAP Data from Key-Value Pairs (map_from_entries) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map_from_entries(STRUCT[, STRUCT ...])');

Where STRUCT are the values with the k and v fields that represent key-value pairs.

Postgres Pro AXE will return the MAP data created from the specified key-value pairs.

Example 21.23. Creating MAP Data from Key-Value Pairs

  SELECT * FROM axe.query('SELECT map_from_entries([{k: 5, v: ''val1''}, {k: 3, v: ''val2''}])');
  {5=val1, 3=val2}

21.2.12. Creating Empty MAP Data (map) #

This stored procedure must be executed inside axe.query() as Postgres Pro AXE does not support the MAP data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM axe.query('SELECT map()');

Postgres Pro AXE will return empty MAP data.

Example 21.24. Creating Empty MAP Data

  SELECT * FROM axe.query('SELECT map()');
  {}

FAQ