-
Notifications
You must be signed in to change notification settings - Fork 409
[Feature](docs) Impl the function map_concat like spark #3220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nagisa-kunhah
wants to merge
8
commits into
apache:master
Choose a base branch
from
nagisa-kunhah:feat_mapconcat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
572ff4e
feat: mapconcat
nagisa-kunhah 7e70ef4
feat: map-concat doc
nagisa-kunhah 999d44b
feat: sidebar of map-concat
nagisa-kunhah 27a40b8
Merge branch 'master' into feat_mapconcat
nagisa-kunhah 2587fab
fix: map concat
nagisa-kunhah e35ac65
fix: info
nagisa-kunhah a16e18a
fix: doc
nagisa-kunhah f046b3b
fix: doc
nagisa-kunhah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
docs/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(<map1> [, <map2> [, <map3> ... ]]) | ||
| ``` | ||
|
|
||
| ## Parameters | ||
| - `<map1>`, `<map2>`, `<map3>`, ...: [`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` containing all key-value pairs from the input maps. | ||
|
|
||
| **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. |
164 changes: 164 additions & 0 deletions
164
...rsion-4.x/sql-manual/sql-functions/scalar-functions/map-functions/map-concat.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| --- | ||
| { | ||
| "title": "MAP_CONCAT", | ||
| "language": "zh-CN" | ||
| } | ||
| --- | ||
|
|
||
| ## 描述 | ||
|
|
||
| 将多个 map 合并为一个 map。当合并具有不同键或值类型的 map 时,该函数会找到键和值的公共类型。 | ||
|
|
||
| ## 语法 | ||
|
|
||
| ```sql | ||
| MAP_CONCAT(<map1> [, <map2> [, <map3> ... ]]) | ||
| ``` | ||
|
|
||
| ## 参数 | ||
| - `<map1>`, `<map2>`, `<map3>`, ...: [`MAP`](../../../basic-element/sql-data-types/semi-structured/MAP.md) 类型,需要合并的输入 map。 | ||
|
|
||
| **支持的键和值类型:** | ||
| - **键类型**:所有支持比较的基本类型(数值、字符串、日期/时间、布尔值、IP) | ||
| - **值类型**:所有基本类型加上复杂类型(ARRAY、MAP、STRUCT) | ||
|
|
||
| **类型兼容性说明:** | ||
| - 当合并具有不同键类型的 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 元素的 map 合并: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),会抛出错误。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.