Mapping constructors/operators are used to create new mapping data types. OceanBase AI Database currently supports the map() constructor and the {} operator.
map
The map() function is used to create a mapping of data. The syntax is as follows:
map(key1, value1, key2, value2, ..., keyN, valueN)
The input parameters are described as follows:
- The number of input parameters must be even. Parameters with odd indices form the Key array, and those with even indices form the Value array.
- For the data element types of key1 to keyN and value1 to valueN, see Overview of mapped data types.
- Note that the keys in the input key-value pairs must be unique. If duplicate keys exist, the later input value will overwrite the earlier one.
The return value is of the MAP data type.
An example is as follows:
-- The constructed mapping data contains only one key-value pair: key = 1, value = 2.
SELECT map(1,2);
+----------+
| map(1,2) |
+----------+
| {1:2} |
+----------+
1 row in set
-- The generated mapping data contains three key-value pairs:
SELECT map(1,"a",2,"b",3,"c");
+------------------------+
| map(1,"a",2,"b",3,"c") |
+------------------------+
| {1:"a",2:"b",3:"c"} |
+------------------------+
1 row in set
-- The data type of the Key array is determined by the odd-numbered parameters, and the data type of the Value array is determined by the even-numbered parameters.
SELECT map(1,"a","b",1.8,"ccc",3);
+-------------------------------+
| map(1,"a","b",1.8,"ccc",3) |
+-------------------------------+
| {"1":"a","b":"1.8","ccc":"3"} |
+-------------------------------+