ClickHouse/docs/en/functions/higher_order_functions.rst

68 lines
2.6 KiB
ReStructuredText
Raw Normal View History

2017-04-26 19:16:38 +00:00
Higher-order functions
2017-04-03 19:49:50 +00:00
-----------------------
2017-04-26 19:16:38 +00:00
-> operator, lambda(params, expr) function
2017-04-03 19:49:50 +00:00
~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Allows describing a lambda function for passing to a higher-order function. The left side of the arrow has a formal parameter - any ID, or multiple formal parameters - any IDs in a tuple. The right side of the arrow has an expression that can use these formal parameters, as well as any table columns.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Examples: ``x -> 2 * x, str -> str != Referer.``
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Higher-order functions can only accept lambda functions as their functional argument.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
A lambda function that accepts multiple arguments can be passed to a higher-order function. In this case, the higher-order function is passed several arrays of identical length that these arguments will correspond to.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
For all functions other than 'arrayMap' and 'arrayFilter', the first argument (the lambda function) can be omitted. In this case, identical mapping is assumed.
2017-04-03 19:49:50 +00:00
arrayMap(func, arr1, ...)
~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns an array obtained from the original application of the 'func' function to each element in the 'arr' array.
2017-04-03 19:49:50 +00:00
arrayFilter(func, arr1, ...)
~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns an array containing only the elements in 'arr1' for which 'func' returns something other than 0.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Examples:
2017-04-03 19:49:50 +00:00
.. code-block:: sql
SELECT arrayFilter(x -> x LIKE '%World%', ['Hello', 'abc World']) AS res
┌─res───────────┐
│ ['abc World'] │
└───────────────┘
SELECT
arrayFilter(
(i, x) -> x LIKE '%World%',
arrayEnumerate(arr),
['Hello', 'abc World'] AS arr)
AS res
┌─res─┐
│ [2] │
└─────┘
arrayCount([func,] arr1, ...)
~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns the number of elements in 'arr' for which 'func' returns something other than 0. If 'func' is not specified, it returns the number of non-zero items in the array.
2017-04-03 19:49:50 +00:00
arrayExists([func,] arr1, ...)
~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns 1 if there is at least one element in 'arr' for which 'func' returns something other than 0. Otherwise, it returns 0.
2017-04-03 19:49:50 +00:00
arrayAll([func,] arr1, ...)
~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns 1 if 'func' returns something other than 0 for all the elements in 'arr'. Otherwise, it returns 0.
2017-04-03 19:49:50 +00:00
arraySum([func,] arr1, ...)
~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns the sum of the 'func' values. If the function is omitted, it just returns the sum of the array elements.
2017-04-03 19:49:50 +00:00
arrayFirst(func, arr1, ...)
~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns the first element in the 'arr1' array for which 'func' returns something other than 0.
2017-04-03 19:49:50 +00:00
arrayFirstIndex(func, arr1, ...)
~~~~~~~
2017-04-26 19:16:38 +00:00
Returns the index of the first element in the 'arr1' array for which 'func' returns something other than 0.