diff --git a/docs/en/query_language/functions/array_functions.md b/docs/en/query_language/functions/array_functions.md index fc51339acf5fbeec5e86ff1735102206b3263712..dd6a4392e58fab80da0a7dfd39703dc5b9cf2a80 100644 --- a/docs/en/query_language/functions/array_functions.md +++ b/docs/en/query_language/functions/array_functions.md @@ -500,7 +500,9 @@ SELECT arraySort([1, nan, 2, NULL, 3, nan, -4, NULL, inf, -inf]); - `NaN` values are right before `NULL`. - `Inf` values are right before `NaN`. -Note that `arraySort` is a [higher-order function](higher_order_functions.md). You can pass a lambda function to it as the first argument. In this case, sorting order is determined by the result of the lambda function applied to the elements of the array. Example is shown below. +Note that `arraySort` is a [higher-order function](higher_order_functions.md). You can pass a lambda function to it as the first argument. In this case, sorting order is determined by the result of the lambda function applied to the elements of the array. + +Let's consider the following example: ``` sql SELECT arraySort((x) -> -x, [1, 2, 3]) as res; @@ -527,7 +529,7 @@ SELECT arraySort((x, y) -> y, ['hello', 'world'], [2, 1]) as res; Here, the elements that are passed in the second array ([2, 1]) define a new position of each corresponding element from the source array (['hello', 'world']), that is, ['hello' –> 2, 'world' –> 1]. So, 'hello' will be the second element in a result, and 'world' will be the first. -Another example is shown below. In this example, sorting order is specified by the string values: +Another examples are shown below. ``` sql SELECT arraySort((x, y) -> y, [0, 1, 2], ['c', 'b', 'a']) as res; @@ -538,6 +540,15 @@ SELECT arraySort((x, y) -> y, [0, 1, 2], ['c', 'b', 'a']) as res; └─────────┘ ``` +``` sql +SELECT arraySort((x, y) -> -y, [0, 1, 2], [1, 2, 3]) as res; +``` +``` sql +┌─res─────┐ +│ [2,1,0] │ +└─────────┘ +``` + !!! note To improve sorting efficiency, the [Schwartzian transform](https://en.wikipedia.org/wiki/Schwartzian_transform) is used. @@ -583,9 +594,7 @@ SELECT arrayReverseSort([1, nan, 2, NULL, 3, nan, -4, NULL, inf, -inf]) as res; - `NaN` values are right before `NULL`. - `-Inf` values are right before `NaN`. -Note that the `arrayReverseSort` is a [higher-order function](higher_order_functions.md). You can pass a lambda function to it as the first argument. - -Let's consider the following example: +Note that the `arrayReverseSort` is a [higher-order function](higher_order_functions.md). You can pass a lambda function to it as the first argument. Example is shown below. ``` sql SELECT arrayReverseSort((x) -> -x, [1, 2, 3]) as res; @@ -617,7 +626,7 @@ In this example, the array is sorted in the following way: 1. At first, the source array (['hello', 'world']) is sorted according to the result of the lambda function applied to the elements of the arrays. The elements that are passed in the second array ([2, 1]), define a new position of corresponding elements from the source array. The result is an array ['world', 'hello']. 2. Array that was sorted on the previous step, is reversed. So, the final result is ['hello', 'world']. -Another example is shown below. In this example, sorting order is specified by the string values: +Another examples are shown below. ``` sql SELECT arrayReverseSort((x, y) -> y, [4, 3, 5], ['a', 'b', 'c']) AS res; @@ -627,6 +636,14 @@ SELECT arrayReverseSort((x, y) -> y, [4, 3, 5], ['a', 'b', 'c']) AS res; │ [5,3,4] │ └─────────┘ ``` +``` sql +SELECT arrayReverseSort((x, y) -> -y, [4, 3, 5], [1, 2, 3]) AS res; +``` +``` sql +┌─res─────┐ +│ [4,3,5] │ +└─────────┘ +``` ## arrayUniq(arr, ...) diff --git a/docs/ru/query_language/functions/array_functions.md b/docs/ru/query_language/functions/array_functions.md index e3a0eaa411a9cb504aae25715c8133c00959dd0f..e6f4b337dc50138257bb1dd41ad2a2e67828495d 100644 --- a/docs/ru/query_language/functions/array_functions.md +++ b/docs/ru/query_language/functions/array_functions.md @@ -529,7 +529,7 @@ SELECT arraySort((x, y) -> y, ['hello', 'world'], [2, 1]) as res; Элементы, указанные во втором массиве ([2,1]), определяют позицию соответствующих элементов из исходного массива (['hello', 'world']), то есть ['hello' –> 2, 'world' –> 1]. Таким образом, 'hello' будет вторым элементом в отсортированном массиве, а 'world' — первым. -Ниже приведен ещё один пример, в котором порядок сортировки устанавливается строковыми значениями: +Ниже приведены другие примеры. ``` sql SELECT arraySort((x, y) -> y, [0, 1, 2], ['c', 'b', 'a']) as res; @@ -539,6 +539,14 @@ SELECT arraySort((x, y) -> y, [0, 1, 2], ['c', 'b', 'a']) as res; │ [2,1,0] │ └─────────┘ ``` +``` sql +SELECT arraySort((x, y) -> -y, [0, 1, 2], [1, 2, 3]) as res; +``` +``` sql +┌─res─────┐ +│ [2,1,0] │ +└─────────┘ +``` !!! note "Примечание" Для улучшения эффективности сортировки применяется [Преобразование Шварца](https://ru.wikipedia.org/wiki/%D0%9F%D1%80%D0%B5%D0%BE%D0%B1%D1%80%D0%B0%D0%B7%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5_%D0%A8%D0%B2%D0%B0%D1%80%D1%86%D0%B0). @@ -601,7 +609,7 @@ SELECT arrayReverseSort((x, y) -> y, ['hello', 'world'], [2, 1]) as res; 1. Сначала массив сортируется в том порядке, который определяется лямбда-функцией. Элементы, указанные во втором массиве ([2,1]), определяют позицию соответствующих элементов из исходного массива (['hello', 'world']). То есть, промежуточным результатом будет массив ['world', 'hello']. 2. Массив, который был отсортирован на предыдущем шаге, переворачивается. Получается массив ['hello', 'world']. -Ниже приведён еще один пример, в котором порядок сортировки устанавливается строковыми значениями: +Ниже приведены ещё примеры. ``` sql SELECT arrayReverseSort((x, y) -> y, [0, 1, 2], ['c', 'b', 'a']) as res; @@ -611,6 +619,14 @@ SELECT arrayReverseSort((x, y) -> y, [0, 1, 2], ['c', 'b', 'a']) as res; │ [0,1,2] │ └─────────┘ ``` +``` sql +SELECT arrayReverseSort((x, y) -> -y, [4, 3, 5], [1, 2, 3]) AS res; +``` +``` sql +┌─res─────┐ +│ [4,3,5] │ +└─────────┘ +``` ## arrayUniq(arr, ...)