diff --git a/docs/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md b/docs/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md index 2214ed035ffbe..cf7112e69a893 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md +++ b/docs/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md @@ -7,34 +7,173 @@ ## Function -Sort array elements in ascending order. +If no lambda function is specified, the array elements are sorted in ascending order. Otherwise, sorting is performed according to the lambda function. ## Syntax - `ARRAY_SORT(arr)` +- `ARRAY_SORT(lambda, arr)` ## Parameters +- `lambda`: A `lambda` expression used to define sorting rules, whose return value should be -1, 0, or 1 (representing less than, less than or equal to, and greater than respectively). - `arr`: `ARRAY`, where `T` can be numeric, boolean, string, datetime, IP, etc. ## Return value -- Returns `ARRAY` of the same type as the input. -- `NULL` elements are placed at the beginning of the returned array. +- If it does not contain a `lambda` expression + - Returns `ARRAY` of the same type as the input. + - `NULL` elements are placed at the beginning of the returned array. +- If it contains a `lambda` expression, sort according to the lambda expression and return an `ARRAY` of the same type as the input. ## Usage notes -- If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. -- `ARRAY_SORT` sorts in ascending order, while `ARRAY_REVERSE_SORT` sorts in descending order. +- If it does not contain a `lambda` expression + - If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. + - `ARRAY_SORT` sorts in ascending order, while `ARRAY_REVERSE_SORT` sorts in descending order. +- If containing a `lambda` expression + - The value of the `lambda` expression shall be -1, 0, or 1. Should a `NULL` value exist, it shall be explicitly stated whether the `NULL` value is to be placed before or after the array. ## Examples -- Basic: `NULL` elements are placed at the beginning of the returned array - - `ARRAY_SORT([2,1,3,null])` -> `[null, 1, 2, 3]` - -- If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. - - `ARRAY_SORT(NULL)` -> `NULL` - - `ARRAY_SORT([])` -> `[]` - - - +1. Basic: `NULL` elements are placed at the beginning of the returned array + +```sql +SELECT ARRAY_SORT([2,1,3,null]); +``` + +```text ++--------------------------+ +| ARRAY_SORT([2,1,3,null]) | ++--------------------------+ +| [null, 1, 2, 3] | ++--------------------------+ +``` + +2. If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. + +```sql +SELECT ARRAY_SORT(NULL); +``` + +```text ++------------------+ +| ARRAY_SORT(NULL) | ++------------------+ +| NULL | ++------------------+ +``` + +```sql +SELECT ARRAY_SORT([]); +``` + +```text ++----------------+ +| ARRAY_SORT([]) | ++----------------+ +| [] | ++----------------+ +``` + +3. Use `lambda` expressions + +```sql +SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]); +``` + +```text ++-----------------------------------------------------------------------+ +| array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]) | ++-----------------------------------------------------------------------+ +| [5, 3, 2, 2, 1] | ++-----------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']); +``` + +```text ++--------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']) | ++--------------------------------------------------------------------------+ +| ["dc", "bc", "ab"] | ++--------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 + WHEN y IS NULL THEN 1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]); +``` + +```text ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 + WHEN y IS NULL THEN 1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]) | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [null, null, 5, 3, 2, 2, 1] | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 + WHEN y IS NULL THEN -1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]); +``` + +```text ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 + WHEN y IS NULL THEN -1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]) | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [5, 3, 2, 2, 1, null, null] | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(length(x) < length(y), -1, + IF(length(x) = length(y), 0, 1)), + ['a', 'abcd', 'abc']); +``` + +```text ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(length(x) < length(y), -1, + IF(length(x) = length(y), 0, 1)), + ['a', 'abcd', 'abc']) | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ["a", "abc", "abcd"] | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, + IF(cardinality(x) = cardinality(y), 0, 1)), + [[2, 3, 1], [4, 2, 1, 4], [1, 2]]); +``` + +```text ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, + IF(cardinality(x) = cardinality(y), 0, 1)), + [[2, 3, 1], [4, 2, 1, 4], [1, 2]]) | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [[1, 2], [2, 3, 1], [4, 2, 1, 4]] | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md index 5785c8e264357..c68befcfcd4e2 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md @@ -7,33 +7,173 @@ ## 功能 -对数组元素按升序排序。 +若不指定lambda函数,则对数组元素按升序排序。否则按照lambda函数进行排序。 ## 语法 - `ARRAY_SORT(arr)` +- `ARRAY_SORT(lambda, arr)` ## 参数 +- `lambda`: `lambda` 表达式,用于定义排序规则,返回值应为-1, 0, 1(分别表示小于,等于,大于)。 - `arr`:`ARRAY`,`T` 可为数值、布尔、字符串、日期时间、IP 等。 ## 返回值 -- 返回与输入同类型的 `ARRAY`。 -- `NULL` 元素放在返回的数组最前面。 +- 若不包含`lambda`表达式 + - 返回与输入同类型的 `ARRAY`。 + - `NULL` 元素放在返回的数组最前面。 +- 若包含`lambda`表达式,按照lambda表达式进行排序,返回与输入同类型的 `ARRAY`。 ## 使用说明 -- 若输入为 `NULL`,返回 `NULL`; 若输入为空数组 `[]`,返回空数组。 -- `ARRAY_SORT` 是升序排序,`ARRAY_REVERSE_SORT` 是降序排序。 +- 若不包含`lambda`表达式 + - 若输入为 `NULL`,返回 `NULL`; 若输入为空数组 `[]`,返回空数组。 + - `ARRAY_SORT` 是升序排序,`ARRAY_REVERSE_SORT` 是降序排序。 +- 若包含`lambda`表达式 + - `lambda`表达式的值应为-1,0,1,若存在`NULL`值,应明确说明`NULL`值应排在数组前还是数组后。 ## 示例 -- 基本: `NULL` 元素放在返回的数组最前面 - - `ARRAY_SORT([2,1,3,null])` -> `[null, 1, 2, 3]` +1. 基本: `NULL` 元素放在返回的数组最前面 -- 输入为 `NULL`,返回 `NULL`; 输入为空数组 `[]`,返回空数组。 - - `ARRAY_SORT(NULL)` -> `NULL` - - `ARRAY_SORT([])` -> `[]` +```sql +SELECT ARRAY_SORT([2,1,3,null]); +``` +```text ++--------------------------+ +| ARRAY_SORT([2,1,3,null]) | ++--------------------------+ +| [null, 1, 2, 3] | ++--------------------------+ +``` +2. 输入为 `NULL`,返回 `NULL`; 输入为空数组 `[]`,返回空数组。 + +```sql +SELECT ARRAY_SORT(NULL); +``` + +```text ++------------------+ +| ARRAY_SORT(NULL) | ++------------------+ +| NULL | ++------------------+ +``` + +```sql +SELECT ARRAY_SORT([]); +``` + +```text ++----------------+ +| ARRAY_SORT([]) | ++----------------+ +| [] | ++----------------+ +``` + +3. 使用`lambda`表达式 + +```sql +SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]); +``` + +```text ++-----------------------------------------------------------------------+ +| array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]) | ++-----------------------------------------------------------------------+ +| [5, 3, 2, 2, 1] | ++-----------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']); +``` + +```text ++--------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']) | ++--------------------------------------------------------------------------+ +| ["dc", "bc", "ab"] | ++--------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 + WHEN y IS NULL THEN 1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]); +``` + +```text ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 + WHEN y IS NULL THEN 1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]) | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [null, null, 5, 3, 2, 2, 1] | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 + WHEN y IS NULL THEN -1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]); +``` + +```text ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 + WHEN y IS NULL THEN -1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]) | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [5, 3, 2, 2, 1, null, null] | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(length(x) < length(y), -1, + IF(length(x) = length(y), 0, 1)), + ['a', 'abcd', 'abc']); +``` + +```text ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(length(x) < length(y), -1, + IF(length(x) = length(y), 0, 1)), + ['a', 'abcd', 'abc']) | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ["a", "abc", "abcd"] | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, + IF(cardinality(x) = cardinality(y), 0, 1)), + [[2, 3, 1], [4, 2, 1, 4], [1, 2]]); +``` + +```text ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, + IF(cardinality(x) = cardinality(y), 0, 1)), + [[2, 3, 1], [4, 2, 1, 4], [1, 2]]) | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [[1, 2], [2, 3, 1], [4, 2, 1, 4]] | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md index 5785c8e264357..c68befcfcd4e2 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md @@ -7,33 +7,173 @@ ## 功能 -对数组元素按升序排序。 +若不指定lambda函数,则对数组元素按升序排序。否则按照lambda函数进行排序。 ## 语法 - `ARRAY_SORT(arr)` +- `ARRAY_SORT(lambda, arr)` ## 参数 +- `lambda`: `lambda` 表达式,用于定义排序规则,返回值应为-1, 0, 1(分别表示小于,等于,大于)。 - `arr`:`ARRAY`,`T` 可为数值、布尔、字符串、日期时间、IP 等。 ## 返回值 -- 返回与输入同类型的 `ARRAY`。 -- `NULL` 元素放在返回的数组最前面。 +- 若不包含`lambda`表达式 + - 返回与输入同类型的 `ARRAY`。 + - `NULL` 元素放在返回的数组最前面。 +- 若包含`lambda`表达式,按照lambda表达式进行排序,返回与输入同类型的 `ARRAY`。 ## 使用说明 -- 若输入为 `NULL`,返回 `NULL`; 若输入为空数组 `[]`,返回空数组。 -- `ARRAY_SORT` 是升序排序,`ARRAY_REVERSE_SORT` 是降序排序。 +- 若不包含`lambda`表达式 + - 若输入为 `NULL`,返回 `NULL`; 若输入为空数组 `[]`,返回空数组。 + - `ARRAY_SORT` 是升序排序,`ARRAY_REVERSE_SORT` 是降序排序。 +- 若包含`lambda`表达式 + - `lambda`表达式的值应为-1,0,1,若存在`NULL`值,应明确说明`NULL`值应排在数组前还是数组后。 ## 示例 -- 基本: `NULL` 元素放在返回的数组最前面 - - `ARRAY_SORT([2,1,3,null])` -> `[null, 1, 2, 3]` +1. 基本: `NULL` 元素放在返回的数组最前面 -- 输入为 `NULL`,返回 `NULL`; 输入为空数组 `[]`,返回空数组。 - - `ARRAY_SORT(NULL)` -> `NULL` - - `ARRAY_SORT([])` -> `[]` +```sql +SELECT ARRAY_SORT([2,1,3,null]); +``` +```text ++--------------------------+ +| ARRAY_SORT([2,1,3,null]) | ++--------------------------+ +| [null, 1, 2, 3] | ++--------------------------+ +``` +2. 输入为 `NULL`,返回 `NULL`; 输入为空数组 `[]`,返回空数组。 + +```sql +SELECT ARRAY_SORT(NULL); +``` + +```text ++------------------+ +| ARRAY_SORT(NULL) | ++------------------+ +| NULL | ++------------------+ +``` + +```sql +SELECT ARRAY_SORT([]); +``` + +```text ++----------------+ +| ARRAY_SORT([]) | ++----------------+ +| [] | ++----------------+ +``` + +3. 使用`lambda`表达式 + +```sql +SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]); +``` + +```text ++-----------------------------------------------------------------------+ +| array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]) | ++-----------------------------------------------------------------------+ +| [5, 3, 2, 2, 1] | ++-----------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']); +``` + +```text ++--------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']) | ++--------------------------------------------------------------------------+ +| ["dc", "bc", "ab"] | ++--------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 + WHEN y IS NULL THEN 1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]); +``` + +```text ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 + WHEN y IS NULL THEN 1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]) | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [null, null, 5, 3, 2, 2, 1] | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 + WHEN y IS NULL THEN -1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]); +``` + +```text ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 + WHEN y IS NULL THEN -1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]) | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [5, 3, 2, 2, 1, null, null] | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(length(x) < length(y), -1, + IF(length(x) = length(y), 0, 1)), + ['a', 'abcd', 'abc']); +``` + +```text ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(length(x) < length(y), -1, + IF(length(x) = length(y), 0, 1)), + ['a', 'abcd', 'abc']) | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ["a", "abc", "abcd"] | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, + IF(cardinality(x) = cardinality(y), 0, 1)), + [[2, 3, 1], [4, 2, 1, 4], [1, 2]]); +``` + +```text ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, + IF(cardinality(x) = cardinality(y), 0, 1)), + [[2, 3, 1], [4, 2, 1, 4], [1, 2]]) | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [[1, 2], [2, 3, 1], [4, 2, 1, 4]] | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` diff --git a/versioned_docs/version-4.x/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md b/versioned_docs/version-4.x/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md index 2214ed035ffbe..cf7112e69a893 100644 --- a/versioned_docs/version-4.x/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md +++ b/versioned_docs/version-4.x/sql-manual/sql-functions/scalar-functions/array-functions/array-sort.md @@ -7,34 +7,173 @@ ## Function -Sort array elements in ascending order. +If no lambda function is specified, the array elements are sorted in ascending order. Otherwise, sorting is performed according to the lambda function. ## Syntax - `ARRAY_SORT(arr)` +- `ARRAY_SORT(lambda, arr)` ## Parameters +- `lambda`: A `lambda` expression used to define sorting rules, whose return value should be -1, 0, or 1 (representing less than, less than or equal to, and greater than respectively). - `arr`: `ARRAY`, where `T` can be numeric, boolean, string, datetime, IP, etc. ## Return value -- Returns `ARRAY` of the same type as the input. -- `NULL` elements are placed at the beginning of the returned array. +- If it does not contain a `lambda` expression + - Returns `ARRAY` of the same type as the input. + - `NULL` elements are placed at the beginning of the returned array. +- If it contains a `lambda` expression, sort according to the lambda expression and return an `ARRAY` of the same type as the input. ## Usage notes -- If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. -- `ARRAY_SORT` sorts in ascending order, while `ARRAY_REVERSE_SORT` sorts in descending order. +- If it does not contain a `lambda` expression + - If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. + - `ARRAY_SORT` sorts in ascending order, while `ARRAY_REVERSE_SORT` sorts in descending order. +- If containing a `lambda` expression + - The value of the `lambda` expression shall be -1, 0, or 1. Should a `NULL` value exist, it shall be explicitly stated whether the `NULL` value is to be placed before or after the array. ## Examples -- Basic: `NULL` elements are placed at the beginning of the returned array - - `ARRAY_SORT([2,1,3,null])` -> `[null, 1, 2, 3]` - -- If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. - - `ARRAY_SORT(NULL)` -> `NULL` - - `ARRAY_SORT([])` -> `[]` - - - +1. Basic: `NULL` elements are placed at the beginning of the returned array + +```sql +SELECT ARRAY_SORT([2,1,3,null]); +``` + +```text ++--------------------------+ +| ARRAY_SORT([2,1,3,null]) | ++--------------------------+ +| [null, 1, 2, 3] | ++--------------------------+ +``` + +2. If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. + +```sql +SELECT ARRAY_SORT(NULL); +``` + +```text ++------------------+ +| ARRAY_SORT(NULL) | ++------------------+ +| NULL | ++------------------+ +``` + +```sql +SELECT ARRAY_SORT([]); +``` + +```text ++----------------+ +| ARRAY_SORT([]) | ++----------------+ +| [] | ++----------------+ +``` + +3. Use `lambda` expressions + +```sql +SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]); +``` + +```text ++-----------------------------------------------------------------------+ +| array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]) | ++-----------------------------------------------------------------------+ +| [5, 3, 2, 2, 1] | ++-----------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']); +``` + +```text ++--------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']) | ++--------------------------------------------------------------------------+ +| ["dc", "bc", "ab"] | ++--------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 + WHEN y IS NULL THEN 1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]); +``` + +```text ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 + WHEN y IS NULL THEN 1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]) | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [null, null, 5, 3, 2, 2, 1] | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 + WHEN y IS NULL THEN -1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]); +``` + +```text ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 + WHEN y IS NULL THEN -1 + WHEN x < y THEN 1 + WHEN x = y THEN 0 + ELSE -1 END, + [3, 2, null, 5, null, 1, 2]) | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [5, 3, 2, 2, 1, null, null] | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(length(x) < length(y), -1, + IF(length(x) = length(y), 0, 1)), + ['a', 'abcd', 'abc']); +``` + +```text ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(length(x) < length(y), -1, + IF(length(x) = length(y), 0, 1)), + ['a', 'abcd', 'abc']) | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ["a", "abc", "abcd"] | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + +```sql +SELECT array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, + IF(cardinality(x) = cardinality(y), 0, 1)), + [[2, 3, 1], [4, 2, 1, 4], [1, 2]]); +``` + +```text ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, + IF(cardinality(x) = cardinality(y), 0, 1)), + [[2, 3, 1], [4, 2, 1, 4], [1, 2]]) | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| [[1, 2], [2, 3, 1], [4, 2, 1, 4]] | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +```