Map constructor functions and operators are used to create new map data types. The map constructors currently supported by OceanBase Database include the map() function, and the operator {}.
map
The map() function is used to construct a map. The syntax is as follows:
map(key1, value1, key2, value2, ..., keyN, valueN)
Description of the input parameters:
- The number of input parameters must be even. Parameters at odd positions form the key array, while parameters at even positions form the value array.
- The data types of
key1tokeyNandvalue1tovalueNshould conform to the Overview of the map data type. - Note that keys in the input key-value pairs must be unique. If duplicate keys are provided, the value of the most recent key-value pair will take precedence, and earlier values will be overwritten.
The return value of the function is a map data type.
Here are some examples:
-- The constructed map contains only one key-value pair, where the key is 1 and the value is 2.
SELECT map(1,2);
+----------+
| map(1,2) |
+----------+
| {1:2} |
+----------+
1 row in set
-- The constructed map contains 3 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-positioned parameters, while the data type of the value array is determined by the even-positioned 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"} |
+-------------------------------+