Mapping operation functions perform basic operations on the input mapped data, such as extracting Key or Value elements, and return the mapped data after the operation. OceanBase AI Database currently supports the following mapping operation functions: map_keys() and map_values().
map_keys
The map_keys() function returns the list of keys in the target map. The syntax is as follows:
map_keys(map)
The input parameter must be mapped data.
The return value is of the array type.
An example is as follows:
SELECT map_keys(map(1,"apple",2,"banana"));
+-------------------------------------+
| map_keys(map(1,"apple",2,"banana")) |
+-------------------------------------+
| [1,2] |
+-------------------------------------+
1 row in set
CREATE TABLE t1 (
id INT NOT NULL AUTO_INCREMENT,
m MAP(INT, INT)
);
INSERT INTO t1 (m) VALUES ('{1:1,2:2,3:3}');
INSERT INTO t1 (m) VALUES ('{"1":1, 2 : 2 , "3 " : 3}');
INSERT INTO t1 (m) VALUES ('{1.5:2, 1.6:"3"}');
SELECT m, map_keys(m) FROM t1;
+---------------+-------------+
| m | map_keys(m) |
+---------------+-------------+
| {1:1,2:2,3:3} | [1,2,3] |
| {1:1,2:2,3:3} | [1,2,3] |
| {2:3} | [2] |
+---------------+-------------+
3 rows in set
map_values
The map_values() function returns a list of values from the target map. The syntax is as follows:
map_values(map)
The input parameter must be mapped data.
The return value is of the array type.
An example is as follows:
SELECT map_values(map(1,"apple",2,"banana"));
+-------------------------------------+
| map_values(map(1,"apple",2,"banana")) |
+-------------------------------------+
| ["apple","banana"] |
+-------------------------------------+
1 row in set
-- For the table creation and insertion statements of t1, see the map_keys example.
SELECT m, map_values(m) FROM t1;
+---------------+---------------+
| m | map_values(m) |
+---------------+---------------+
| {1:1,2:2,3:3} | [1,2,3] |
| {1:1,2:2,3:3} | [1,2,3] |
| {2:3} | [3] |
+---------------+---------------+
3 rows in set