diff --git a/docs/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md b/docs/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md new file mode 100644 index 0000000000000..6a627d3a5b9ae --- /dev/null +++ b/docs/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md @@ -0,0 +1,164 @@ +--- +{ + "title": "MAP_CONCAT", + "language": "en" +} +--- + +## Description + +Concatenates multiple maps into a single map. When concatenating maps with different key or value types, the function finds common types for keys and values. + +## Syntax + +```sql +MAP_CONCAT( [, [, ... ]]) +``` + +## Parameters +- ``, ``, ``, ...: [`MAP`](../../../basic-element/sql-data-types/semi-structured/MAP.md) type, the input maps to concatenate. + +**Supported key and value types:** +- **Key types**: All primitive types that support comparison (numeric, string, date/time, boolean, IP) +- **Value types**: All primitive types plus complex types (ARRAY, MAP, STRUCT) + +**Type compatibility notes:** +- When concatenating maps with different key types, the function finds a common key type +- When concatenating maps with different value types, the function finds a common value type + +## Return Value +Returns a concatenated `MAP` that merges all key-value pairs from the input maps from left to right. For duplicate keys, the value from the last map wins (and the key's position in the result is determined by its last occurrence). + +**Behavior:** +- The resulting map's key type is the common type of all input map key types +- The resulting map's value type is the common type of all input map value types +- If types cannot be converted (e.g., incompatible key types), an error is thrown + +## Usage Notes +1. The function accepts zero or more map arguments. +2. If any argument is NULL, the result is NULL. +3. Duplicate keys: If multiple maps contain the same key, the value from the last map wins + +## Examples +1. Basic usage + ```sql + select map_concat(); + ``` + ```text + +--------------+ + | map_concat() | + +--------------+ + | {} | + +--------------+ + ``` + + ```sql + select map_concat(map('single', 'argument')); + ``` + ```text + +---------------------------------------+ + | map_concat(map('single', 'argument')) | + +---------------------------------------+ + | {"single":"argument"} | + +---------------------------------------+ + ``` + + ```sql + select map_concat({'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}); + ``` + ```text + +-----------------------------------------------------------+ + | map_concat({'a':'apple'},{'b':'banana'},{'c':'cherry'}) | + +-----------------------------------------------------------+ + | {"a":"apple", "b":"banana", "c":"cherry"} | + +-----------------------------------------------------------+ + ``` + +2. NULL parameters + ```sql + select map_concat({'a': 'apple'}, NULL); + ``` + ```text + +---------------------------------+ + | map_concat({'a':'apple'}, NULL) | + +---------------------------------+ + | NULL | + +---------------------------------+ + ``` + + Map concatenation containing null elements: null elements will be normally retained in the concatenation result. + +3. Type conversion examples + ```sql + -- INT and DOUBLE value types + select map_concat({'a': 1, 'b': 2}, {'c': 3.5, 'd': 4.7}); + ``` + ```text + +----------------------------------------------------+ + | map_concat({'a': 1, 'b': 2}, {'c': 3.5, 'd': 4.7}) | + +----------------------------------------------------+ + | {"a":1.0, "b":2.0, "c":3.5, "d":4.7} | + +----------------------------------------------------+ + ``` + INT values are converted to DOUBLE to match the common type. + + ```sql + -- INT and VARCHAR key types + select map_concat({1: 'one', 2: 'two'}, {'a': 'apple', 'b': 'banana'}); + ``` + ```text + +-----------------------------------------------------------------+ + | map_concat({1: 'one', 2: 'two'}, {'a': 'apple', 'b': 'banana'}) | + +-----------------------------------------------------------------+ + | {"1":"one", "2":"two", "a":"apple", "b":"banana"} | + +-----------------------------------------------------------------+ + ``` + INT keys are converted to VARCHAR to match the common type. + + ```sql + -- INT and BIGINT value types + select map_concat({'small': 100}, {'large': 10000000000}); + ``` + ```text + +----------------------------------------------------+ + | map_concat({'small': 100}, {'large': 10000000000}) | + +----------------------------------------------------+ + | {"small":100, "large":10000000000} | + +----------------------------------------------------+ + ``` + INT values are converted to BIGINT to match the common type. + + ```sql + -- INT and VARCHAR value types + select map_concat({'a': 1}, {'b': '2'}); + ``` + ```text + +----------------------------------+ + | map_concat({'a': 1}, {'b': '2'}) | + +----------------------------------+ + | {"a":"1", "b":"2"} | + +----------------------------------+ + ``` + INT values are converted to VARCHAR to match the common type. + + ```sql + -- Complex types with nested arrays + select map_concat({'a':[1,2,3]},{1:['1','2']}); + ``` + ```text + +-----------------------------------------+ + | map_concat({'a':[1,2,3]},{1:['1','2']}) | + +-----------------------------------------+ + | {"a":["1", "2", "3"], "1":["1", "2"]} | + +-----------------------------------------+ + ``` + For complex types (like ARRAY), type conversion is performed recursively. + + ```sql + -- Error example: cannot find common type + select map_concat({'a':[1,2,3]},{1:3}); + ``` + ```text + ERROR 1105 (HY000): errCode = 2, detailMessage = mapconcat cannot find the common value type of map_concat(map('a', [1, 2, 3]), map(1, 3)) + ``` + When a common type cannot be found (e.g., ARRAY and INT), an error is thrown. diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md new file mode 100644 index 0000000000000..e61e422ab946c --- /dev/null +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md @@ -0,0 +1,168 @@ +--- +{ + "title": "MAP_CONCAT", + "language": "zh-CN" +} +--- + +## 描述 + +将多个 map 合并为一个 map。当合并具有不同键或值类型的 map 时,该函数会找到键和值的公共类型。 + +:::info Note +自 Apache Doris 4.0.4 版本起支持。 +::: + +## 语法 + +```sql +MAP_CONCAT( [, [, ... ]]) +``` + +## 参数 +- ``, ``, ``, ...: [`MAP`](../../../basic-element/sql-data-types/semi-structured/MAP.md) 类型,需要合并的输入 map。 + +**支持的键和值类型:** +- **键类型**:所有支持比较的基本类型(数值、字符串、日期/时间、布尔值、IP) +- **值类型**:所有基本类型加上复杂类型(ARRAY、MAP、STRUCT) + +**类型兼容性说明:** +- 当合并具有不同键类型的 map 时,函数会找到公共键类型 +- 当合并具有不同值类型的 map 时,函数会找到公共值类型 + +## 返回值 +返回一个合并后的 `MAP`,按参数从左到右合并所有输入 map 中的键值对;若存在重复键,以最后一个 map 的值为准(该键在结果中的位置以最后一次出现为准)。 + +**行为:** +- 结果 map 的键类型是所有输入 map 键类型的公共类型 +- 结果 map 的值类型是所有输入 map 值类型的公共类型 +- 如果类型无法转换(例如,不兼容的键类型),则抛出错误 + +## 使用说明 +1. 该函数接受零个或多个 map 参数。 +2. 如果任何参数为 NULL,则结果为 NULL。 +3. 重复键:如果多个 map 包含相同的键,则最后一个 map 的值生效 + +## 示例 +1. 基本用法 + ```sql + select map_concat(); + ``` + ```text + +--------------+ + | map_concat() | + +--------------+ + | {} | + +--------------+ + ``` + + ```sql + select map_concat(map('single', 'argument')); + ``` + ```text + +---------------------------------------+ + | map_concat(map('single', 'argument')) | + +---------------------------------------+ + | {"single":"argument"} | + +---------------------------------------+ + ``` + + ```sql + select map_concat({'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}); + ``` + ```text + +-----------------------------------------------------------+ + | map_concat({'a':'apple'},{'b':'banana'},{'c':'cherry'}) | + +-----------------------------------------------------------+ + | {"a":"apple", "b":"banana", "c":"cherry"} | + +-----------------------------------------------------------+ + ``` + +2. NULL 参数 + ```sql + select map_concat({'a': 'apple'}, NULL); + ``` + ```text + +---------------------------------+ + | map_concat({'a':'apple'}, NULL) | + +---------------------------------+ + | NULL | + +---------------------------------+ + ``` + + 如果任何参数为 NULL,则结果为 NULL。 + +3. 类型转换示例 + ```sql + -- INT 和 DOUBLE 值类型 + select map_concat({'a': 1, 'b': 2}, {'c': 3.5, 'd': 4.7}); + ``` + ```text + +----------------------------------------------------+ + | map_concat({'a': 1, 'b': 2}, {'c': 3.5, 'd': 4.7}) | + +----------------------------------------------------+ + | {"a":1.0, "b":2.0, "c":3.5, "d":4.7} | + +----------------------------------------------------+ + ``` + INT 值被转换为 DOUBLE 以匹配公共类型。 + + ```sql + -- INT 和 VARCHAR 键类型 + select map_concat({1: 'one', 2: 'two'}, {'a': 'apple', 'b': 'banana'}); + ``` + ```text + +-----------------------------------------------------------------+ + | map_concat({1: 'one', 2: 'two'}, {'a': 'apple', 'b': 'banana'}) | + +-----------------------------------------------------------------+ + | {"1":"one", "2":"two", "a":"apple", "b":"banana"} | + +-----------------------------------------------------------------+ + ``` + INT 键被转换为 VARCHAR 以匹配公共类型。 + + ```sql + -- INT 和 BIGINT 值类型 + select map_concat({'small': 100}, {'large': 10000000000}); + ``` + ```text + +----------------------------------------------------+ + | map_concat({'small': 100}, {'large': 10000000000}) | + +----------------------------------------------------+ + | {"small":100, "large":10000000000} | + +----------------------------------------------------+ + ``` + INT 值被转换为 BIGINT 以匹配公共类型。 + + ```sql + -- INT 和 VARCHAR 值类型 + select map_concat({'a': 1}, {'b': '2'}); + ``` + ```text + +----------------------------------+ + | map_concat({'a': 1}, {'b': '2'}) | + +----------------------------------+ + | {"a":"1", "b":"2"} | + +----------------------------------+ + ``` + INT 值被转换为 VARCHAR 以匹配公共类型。 + + ```sql + -- 包含嵌套数组的复杂类型 + select map_concat({'a':[1,2,3]},{1:['1','2']}); + ``` + ```text + +-----------------------------------------+ + | map_concat({'a':[1,2,3]},{1:['1','2']}) | + +-----------------------------------------+ + | {"a":["1", "2", "3"], "1":["1", "2"]} | + +-----------------------------------------+ + ``` + 对于复杂类型(如 ARRAY),会递归执行类型转换。 + + ```sql + -- 错误示例:找不到公共类型 + select map_concat({'a':[1,2,3]},{1:3}); + ``` + ```text + ERROR 1105 (HY000): errCode = 2, detailMessage = mapconcat cannot find the common value type of map_concat(map('a', [1, 2, 3]), map(1, 3)) + ``` + 当找不到公共类型时(例如 ARRAY 和 INT),会抛出错误。 diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md new file mode 100644 index 0000000000000..e61e422ab946c --- /dev/null +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md @@ -0,0 +1,168 @@ +--- +{ + "title": "MAP_CONCAT", + "language": "zh-CN" +} +--- + +## 描述 + +将多个 map 合并为一个 map。当合并具有不同键或值类型的 map 时,该函数会找到键和值的公共类型。 + +:::info Note +自 Apache Doris 4.0.4 版本起支持。 +::: + +## 语法 + +```sql +MAP_CONCAT( [, [, ... ]]) +``` + +## 参数 +- ``, ``, ``, ...: [`MAP`](../../../basic-element/sql-data-types/semi-structured/MAP.md) 类型,需要合并的输入 map。 + +**支持的键和值类型:** +- **键类型**:所有支持比较的基本类型(数值、字符串、日期/时间、布尔值、IP) +- **值类型**:所有基本类型加上复杂类型(ARRAY、MAP、STRUCT) + +**类型兼容性说明:** +- 当合并具有不同键类型的 map 时,函数会找到公共键类型 +- 当合并具有不同值类型的 map 时,函数会找到公共值类型 + +## 返回值 +返回一个合并后的 `MAP`,按参数从左到右合并所有输入 map 中的键值对;若存在重复键,以最后一个 map 的值为准(该键在结果中的位置以最后一次出现为准)。 + +**行为:** +- 结果 map 的键类型是所有输入 map 键类型的公共类型 +- 结果 map 的值类型是所有输入 map 值类型的公共类型 +- 如果类型无法转换(例如,不兼容的键类型),则抛出错误 + +## 使用说明 +1. 该函数接受零个或多个 map 参数。 +2. 如果任何参数为 NULL,则结果为 NULL。 +3. 重复键:如果多个 map 包含相同的键,则最后一个 map 的值生效 + +## 示例 +1. 基本用法 + ```sql + select map_concat(); + ``` + ```text + +--------------+ + | map_concat() | + +--------------+ + | {} | + +--------------+ + ``` + + ```sql + select map_concat(map('single', 'argument')); + ``` + ```text + +---------------------------------------+ + | map_concat(map('single', 'argument')) | + +---------------------------------------+ + | {"single":"argument"} | + +---------------------------------------+ + ``` + + ```sql + select map_concat({'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}); + ``` + ```text + +-----------------------------------------------------------+ + | map_concat({'a':'apple'},{'b':'banana'},{'c':'cherry'}) | + +-----------------------------------------------------------+ + | {"a":"apple", "b":"banana", "c":"cherry"} | + +-----------------------------------------------------------+ + ``` + +2. NULL 参数 + ```sql + select map_concat({'a': 'apple'}, NULL); + ``` + ```text + +---------------------------------+ + | map_concat({'a':'apple'}, NULL) | + +---------------------------------+ + | NULL | + +---------------------------------+ + ``` + + 如果任何参数为 NULL,则结果为 NULL。 + +3. 类型转换示例 + ```sql + -- INT 和 DOUBLE 值类型 + select map_concat({'a': 1, 'b': 2}, {'c': 3.5, 'd': 4.7}); + ``` + ```text + +----------------------------------------------------+ + | map_concat({'a': 1, 'b': 2}, {'c': 3.5, 'd': 4.7}) | + +----------------------------------------------------+ + | {"a":1.0, "b":2.0, "c":3.5, "d":4.7} | + +----------------------------------------------------+ + ``` + INT 值被转换为 DOUBLE 以匹配公共类型。 + + ```sql + -- INT 和 VARCHAR 键类型 + select map_concat({1: 'one', 2: 'two'}, {'a': 'apple', 'b': 'banana'}); + ``` + ```text + +-----------------------------------------------------------------+ + | map_concat({1: 'one', 2: 'two'}, {'a': 'apple', 'b': 'banana'}) | + +-----------------------------------------------------------------+ + | {"1":"one", "2":"two", "a":"apple", "b":"banana"} | + +-----------------------------------------------------------------+ + ``` + INT 键被转换为 VARCHAR 以匹配公共类型。 + + ```sql + -- INT 和 BIGINT 值类型 + select map_concat({'small': 100}, {'large': 10000000000}); + ``` + ```text + +----------------------------------------------------+ + | map_concat({'small': 100}, {'large': 10000000000}) | + +----------------------------------------------------+ + | {"small":100, "large":10000000000} | + +----------------------------------------------------+ + ``` + INT 值被转换为 BIGINT 以匹配公共类型。 + + ```sql + -- INT 和 VARCHAR 值类型 + select map_concat({'a': 1}, {'b': '2'}); + ``` + ```text + +----------------------------------+ + | map_concat({'a': 1}, {'b': '2'}) | + +----------------------------------+ + | {"a":"1", "b":"2"} | + +----------------------------------+ + ``` + INT 值被转换为 VARCHAR 以匹配公共类型。 + + ```sql + -- 包含嵌套数组的复杂类型 + select map_concat({'a':[1,2,3]},{1:['1','2']}); + ``` + ```text + +-----------------------------------------+ + | map_concat({'a':[1,2,3]},{1:['1','2']}) | + +-----------------------------------------+ + | {"a":["1", "2", "3"], "1":["1", "2"]} | + +-----------------------------------------+ + ``` + 对于复杂类型(如 ARRAY),会递归执行类型转换。 + + ```sql + -- 错误示例:找不到公共类型 + select map_concat({'a':[1,2,3]},{1:3}); + ``` + ```text + ERROR 1105 (HY000): errCode = 2, detailMessage = mapconcat cannot find the common value type of map_concat(map('a', [1, 2, 3]), map(1, 3)) + ``` + 当找不到公共类型时(例如 ARRAY 和 INT),会抛出错误。 diff --git a/sidebars.ts b/sidebars.ts index b845e4013c780..c50db9382589d 100644 --- a/sidebars.ts +++ b/sidebars.ts @@ -1613,6 +1613,7 @@ const sidebars: SidebarsConfig = { 'sql-manual/sql-functions/scalar-functions/map-functions/map-size', 'sql-manual/sql-functions/scalar-functions/map-functions/map-values', 'sql-manual/sql-functions/scalar-functions/map-functions/str-to-map', + 'sql-manual/sql-functions/scalar-functions/map-functions/map-concat', ], }, { diff --git a/versioned_docs/version-4.x/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md b/versioned_docs/version-4.x/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md new file mode 100644 index 0000000000000..e1139ffd79882 --- /dev/null +++ b/versioned_docs/version-4.x/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md @@ -0,0 +1,168 @@ +--- +{ + "title": "MAP_CONCAT", + "language": "en" +} +--- + +## Description + +Concatenates multiple maps into a single map. When concatenating maps with different key or value types, the function finds common types for keys and values. + +:::info Note +Supported since Apache Doris 4.0.4. +::: + +## Syntax + +```sql +MAP_CONCAT( [, [, ... ]]) +``` + +## Parameters +- ``, ``, ``, ...: [`MAP`](../../../basic-element/sql-data-types/semi-structured/MAP.md) type, the input maps to concatenate. + +**Supported key and value types:** +- **Key types**: All primitive types that support comparison (numeric, string, date/time, boolean, IP) +- **Value types**: All primitive types plus complex types (ARRAY, MAP, STRUCT) + +**Type compatibility notes:** +- When concatenating maps with different key types, the function finds a common key type +- When concatenating maps with different value types, the function finds a common value type + +## Return Value +Returns a concatenated `MAP` that merges all key-value pairs from the input maps from left to right. For duplicate keys, the value from the last map wins (and the key's position in the result is determined by its last occurrence). + +**Behavior:** +- The resulting map's key type is the common type of all input map key types +- The resulting map's value type is the common type of all input map value types +- If types cannot be converted (e.g., incompatible key types), an error is thrown + +## Usage Notes +1. The function accepts zero or more map arguments. +2. If any argument is NULL, the result is NULL. +3. Duplicate keys: If multiple maps contain the same key, the value from the last map wins + +## Examples +1. Basic usage + ```sql + select map_concat(); + ``` + ```text + +--------------+ + | map_concat() | + +--------------+ + | {} | + +--------------+ + ``` + + ```sql + select map_concat(map('single', 'argument')); + ``` + ```text + +---------------------------------------+ + | map_concat(map('single', 'argument')) | + +---------------------------------------+ + | {"single":"argument"} | + +---------------------------------------+ + ``` + + ```sql + select map_concat({'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}); + ``` + ```text + +-----------------------------------------------------------+ + | map_concat({'a':'apple'},{'b':'banana'},{'c':'cherry'}) | + +-----------------------------------------------------------+ + | {"a":"apple", "b":"banana", "c":"cherry"} | + +-----------------------------------------------------------+ + ``` + +2. NULL parameters + ```sql + select map_concat({'a': 'apple'}, NULL); + ``` + ```text + +---------------------------------+ + | map_concat({'a':'apple'}, NULL) | + +---------------------------------+ + | NULL | + +---------------------------------+ + ``` + + Map concatenation containing null elements: null elements will be normally retained in the concatenation result. + +3. Type conversion examples + ```sql + -- INT and DOUBLE value types + select map_concat({'a': 1, 'b': 2}, {'c': 3.5, 'd': 4.7}); + ``` + ```text + +----------------------------------------------------+ + | map_concat({'a': 1, 'b': 2}, {'c': 3.5, 'd': 4.7}) | + +----------------------------------------------------+ + | {"a":1.0, "b":2.0, "c":3.5, "d":4.7} | + +----------------------------------------------------+ + ``` + INT values are converted to DOUBLE to match the common type. + + ```sql + -- INT and VARCHAR key types + select map_concat({1: 'one', 2: 'two'}, {'a': 'apple', 'b': 'banana'}); + ``` + ```text + +-----------------------------------------------------------------+ + | map_concat({1: 'one', 2: 'two'}, {'a': 'apple', 'b': 'banana'}) | + +-----------------------------------------------------------------+ + | {"1":"one", "2":"two", "a":"apple", "b":"banana"} | + +-----------------------------------------------------------------+ + ``` + INT keys are converted to VARCHAR to match the common type. + + ```sql + -- INT and BIGINT value types + select map_concat({'small': 100}, {'large': 10000000000}); + ``` + ```text + +----------------------------------------------------+ + | map_concat({'small': 100}, {'large': 10000000000}) | + +----------------------------------------------------+ + | {"small":100, "large":10000000000} | + +----------------------------------------------------+ + ``` + INT values are converted to BIGINT to match the common type. + + ```sql + -- INT and VARCHAR value types + select map_concat({'a': 1}, {'b': '2'}); + ``` + ```text + +----------------------------------+ + | map_concat({'a': 1}, {'b': '2'}) | + +----------------------------------+ + | {"a":"1", "b":"2"} | + +----------------------------------+ + ``` + INT values are converted to VARCHAR to match the common type. + + ```sql + -- Complex types with nested arrays + select map_concat({'a':[1,2,3]},{1:['1','2']}); + ``` + ```text + +-----------------------------------------+ + | map_concat({'a':[1,2,3]},{1:['1','2']}) | + +-----------------------------------------+ + | {"a":["1", "2", "3"], "1":["1", "2"]} | + +-----------------------------------------+ + ``` + For complex types (like ARRAY), type conversion is performed recursively. + + ```sql + -- Error example: cannot find common type + select map_concat({'a':[1,2,3]},{1:3}); + ``` + ```text + ERROR 1105 (HY000): errCode = 2, detailMessage = mapconcat cannot find the common value type of map_concat(map('a', [1, 2, 3]), map(1, 3)) + ``` + When a common type cannot be found (e.g., ARRAY and INT), an error is thrown. diff --git a/versioned_sidebars/version-4.x-sidebars.json b/versioned_sidebars/version-4.x-sidebars.json index 460d4ea50f34e..cebcfc7b0d8f0 100644 --- a/versioned_sidebars/version-4.x-sidebars.json +++ b/versioned_sidebars/version-4.x-sidebars.json @@ -1624,6 +1624,7 @@ "label": "MAP Functions", "items": [ "sql-manual/sql-functions/scalar-functions/map-functions/map", + "sql-manual/sql-functions/scalar-functions/map-functions/map-concat", "sql-manual/sql-functions/scalar-functions/map-functions/map-contains-entry", "sql-manual/sql-functions/scalar-functions/map-functions/map-contains-key", "sql-manual/sql-functions/scalar-functions/map-functions/map-contains-value",