2017-04-26 19:16:38 +00:00
Functions for working with arrays
2017-06-20 14:19:03 +00:00
---------------------------------
2017-04-03 19:49:50 +00:00
empty
~~~~~
2017-04-26 19:16:38 +00:00
Returns 1 for an empty array, or 0 for a non-empty array.
The result type is UInt8.
The function also works for strings.
2017-04-03 19:49:50 +00:00
notEmpty
~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns 0 for an empty array, or 1 for a non-empty array.
The result type is UInt8.
The function also works for strings.
2017-04-03 19:49:50 +00:00
length
~~~~~~
2017-04-26 19:16:38 +00:00
Returns the number of items in the array.
The result type is UInt64.
The function also works for strings.
2017-04-03 19:49:50 +00:00
emptyArrayUInt8, emptyArrayUInt16, emptyArrayUInt32, emptyArrayUInt64
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-04-03 19:49:50 +00:00
emptyArrayInt8, emptyArrayInt16, emptyArrayInt32, emptyArrayInt64
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-04-03 19:49:50 +00:00
emptyArrayFloat32, emptyArrayFloat64
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-04-03 19:49:50 +00:00
emptyArrayDate, emptyArrayDateTime
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-04-03 19:49:50 +00:00
emptyArrayString
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Accepts zero arguments and returns an empty array of the appropriate type.
2017-04-03 19:49:50 +00:00
emptyArrayToSingle
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Accepts an empty array as argument and returns an array of one element equal to the default value.
2017-04-03 19:49:50 +00:00
range(N)
2017-06-20 14:19:03 +00:00
~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns an array of numbers from 0 to N-1.
Just in case, an exception is thrown if arrays with a total length of more than 100,000,000 elements are created in a data block.
2017-04-03 19:49:50 +00:00
2017-06-20 14:19:03 +00:00
array(x1, ...), operator [x1, ...]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Creates an array from the function arguments.
The arguments must be constants and have types that have the smallest common type. At least one argument must be passed, because otherwise it isn't clear which type of array to create. That is, you can't use this function to create an empty array (to do that, use the 'emptyArray*' function described above).
Returns an 'Array(T)' type result, where 'T' is the smallest common type out of the passed arguments.
2017-04-03 19:49:50 +00:00
2017-06-20 14:19:03 +00:00
arrayElement(arr, n), operator arr[n]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Get the element with the index 'n' from the array 'arr'.
'n' should be any integer type.
Indexes in an array begin from one.
Negative indexes are supported - in this case, it selects the corresponding element numbered from the end. For example, 'arr[-1]' is the last item in the array.
2017-04-03 19:49:50 +00:00
2017-06-07 01:58:21 +00:00
If the index goes beyond the array bounds, a default value is returned (0 for numbers, an empty string for strings, etc.).
2017-04-03 19:49:50 +00:00
has(arr, elem)
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Checks whether the 'arr' array has the 'elem' element.
Returns 0 if the the element is not in the array, or 1 if it is.
'elem' must be a constant.
2017-04-03 19:49:50 +00:00
indexOf(arr, x)
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns the index of the 'x' element (starting from 1) if it is in the array, or 0 if it is not.
2017-04-03 19:49:50 +00:00
countEqual(arr, x)
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns the number of elements in the array equal to 'x'. Equivalent to `` arrayCount(elem -> elem = x, arr) `` .
2017-04-03 19:49:50 +00:00
arrayEnumerate(arr)
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns the array `` [1, 2, 3, ..., length(arr)] ``
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
This function is normally used together with ARRAY JOIN. It allows counting something just once for each array after applying ARRAY JOIN. Example:
2017-04-03 19:49:50 +00:00
.. code-block :: sql
SELECT
count() AS Reaches,
countIf(num = 1) AS Hits
FROM test.hits
ARRAY JOIN
GoalsReached,
arrayEnumerate(GoalsReached) AS num
WHERE CounterID = 160656
LIMIT 10
2017-06-07 01:58:21 +00:00
2017-06-13 04:15:47 +00:00
.. code-block :: text
2017-04-03 19:49:50 +00:00
┌─Reaches─┬──Hits─┐
│ 95606 │ 31406 │
└─────────┴───────┘
2017-04-26 19:16:38 +00:00
In this example, Reaches is the number of conversions (the strings received after applying ARRAY JOIN), and Hits is the number of pageviews (strings before ARRAY JOIN). In this particular case, you can get the same result in an easier way:
2017-04-03 19:49:50 +00:00
.. code-block :: sql
SELECT
sum(length(GoalsReached)) AS Reaches,
count() AS Hits
FROM test.hits
WHERE (CounterID = 160656) AND notEmpty(GoalsReached)
2017-06-07 01:58:21 +00:00
2017-06-13 04:15:47 +00:00
.. code-block :: text
2017-04-03 19:49:50 +00:00
┌─Reaches─┬──Hits─┐
│ 95606 │ 31406 │
└─────────┴───────┘
2017-04-26 19:16:38 +00:00
This function can also be used in higher-order functions. For example, you can use it to get array indexes for elements that match a condition.
2017-04-03 19:49:50 +00:00
arrayEnumerateUniq(arr, ...)
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Returns an array the same size as the source array, indicating for each element what its position is among elements with the same value.
For example: `` arrayEnumerateUniq([10, 20, 10, 30]) = [1, 1, 2, 1] `` .
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
This function is useful when using ARRAY JOIN and aggregation of array elements. Example:
2017-04-03 19:49:50 +00:00
.. code-block :: sql
2017-06-07 01:58:21 +00:00
2017-04-03 19:49:50 +00:00
SELECT
Goals.ID AS GoalID,
sum(Sign) AS Reaches,
sumIf(Sign, num = 1) AS Visits
FROM test.visits
ARRAY JOIN
Goals,
arrayEnumerateUniq(Goals.ID) AS num
WHERE CounterID = 160656
GROUP BY GoalID
ORDER BY Reaches DESC
LIMIT 10
2017-06-07 01:58:21 +00:00
2017-06-13 04:15:47 +00:00
.. code-block :: text
2017-04-03 19:49:50 +00:00
┌──GoalID─┬─Reaches─┬─Visits─┐
│ 53225 │ 3214 │ 1097 │
│ 2825062 │ 3188 │ 1097 │
│ 56600 │ 2803 │ 488 │
│ 1989037 │ 2401 │ 365 │
│ 2830064 │ 2396 │ 910 │
│ 1113562 │ 2372 │ 373 │
│ 3270895 │ 2262 │ 812 │
│ 1084657 │ 2262 │ 345 │
│ 56599 │ 2260 │ 799 │
│ 3271094 │ 2256 │ 812 │
└─────────┴─────────┴────────┘
2017-06-07 01:58:21 +00:00
In this example, each goal ID has a calculation of the number of conversions (each element in the Goals nested data structure is a goal that was reached, which we refer to as a conversion) and the number of sessions.
Without ARRAY JOIN, we would have counted the number of sessions as `` sum(Sign) `` . But in this particular case, the rows were multiplied by the nested Goals structure, so in order to count each session one time after this,
2017-04-26 19:16:38 +00:00
we apply a condition to the value of the `` arrayEnumerateUniq(Goals.ID) `` function.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
The arrayEnumerateUniq function can take multiple arrays of the same size as arguments. In this case, uniqueness is considered for tuples of elements in the same positions in all the arrays.
2017-04-03 19:49:50 +00:00
.. code-block :: sql
2017-06-07 01:58:21 +00:00
2017-04-03 19:49:50 +00:00
SELECT arrayEnumerateUniq([1, 1, 1, 2, 2, 2], [1, 1, 2, 1, 1, 2]) AS res
2017-06-07 01:58:21 +00:00
2017-06-13 04:15:47 +00:00
.. code-block :: text
2017-04-03 19:49:50 +00:00
┌─res───────────┐
│ [1,2,1,1,2,1] │
└───────────────┘
2017-04-26 19:16:38 +00:00
This is necessary when using ARRAY JOIN with a nested data structure and further aggregation across multiple elements in this structure.
2017-04-03 19:49:50 +00:00
arrayUniq(arr, ...)
~~~~~~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
If a single array is passed, returns a number of unique elements in that array.
If multiple arrays of the same size are passed as arguments to the function, returns a number of unique tuples of elements in the same positions in all the arrays.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
If you need an array of the unique elements, you can use `` arrayReduce('groupUniqArray', arr) `` .
2017-04-03 19:49:50 +00:00
arrayJoin(arr)
2017-06-20 14:19:03 +00:00
~~~~~~~~~~~~~~
2017-04-26 19:16:38 +00:00
A special function. See the section "arrayJoin function".