Functions
There are at least* two types of functions - regular functions (they are just called “functions”) and aggregate functions. These are completely different concepts. Regular functions work as if they are applied to each row separately (for each row, the result of the function does not depend on the other rows). Aggregate functions accumulate a set of values from various rows (i.e. they depend on the entire set of rows).
In this section we discuss regular functions. For aggregate functions, see the section “Aggregate functions”.
* - There is a third type of function that the ‘arrayJoin’ function belongs to; table functions can also be mentioned separately.*
ARITHMETIC
For all arithmetic functions, the result type is calculated as the smallest number type that the result fits in, if there is such a type. The minimum is taken simultaneously based on the number of bits, whether it is signed, and whether it floats. If there are not enough bits, the highest bit type is taken.
Example
Arithmetic functions work for any pair of types from UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, or Float64.
Overflow is produced the same way as in C++.
plus(a, b), a + b operator
Calculates the sum of the numbers. You can also add integer numbers with a date or date and time. In the case of a date, adding an integer means adding the corresponding number of days. For a date with time, it means adding the corresponding number of seconds.
Example
minus(a, b), a - b operator
Calculates the difference. The result is always signed.
You can also calculate integer numbers from a date or date with time. The idea is the same – see above for ‘plus’.
Example
multiply(a, b), a * b operator
Calculates the product of the numbers.
Example
divide(a, b), a / b operator
Calculates the quotient of the numbers. The result type is always a floating-point type. It is not integer division. For integer division, use the ‘intDiv’ function. When dividing by zero you get ‘inf’, ‘-inf’, or ‘nan’.
Example
intDiv(a, b)
Calculates the quotient of the numbers. Divides into integers, rounding down (by the absolute value). An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
Example
intDivOrZero(a, b)
Differs from ‘intDiv’ in that it returns zero when dividing by zero or when dividing a minimal negative number by minus one.
Example
modulo(a, b), a % b operator
Calculates the remainder when dividing a
by b
. The result type is an integer if both inputs are integers. If one of the inputs is a floating-point number, the result is a floating-point number. The remainder is computed like in C++. Truncated division is used for negative numbers. An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
Example
moduloOrZero(a, b)
Differs from modulo in that it returns zero when the divisor is zero.
Example
negate(a), -a operator
Calculates a number with the reverse sign. The result is always signed.
Example
abs(a)
Calculates the absolute value of the number (a). That is, if a \< 0, it returns -a. For unsigned types it does not do anything. For signed integer types, it returns an unsigned number.
Example
gcd(a, b)
Returns the greatest common divisor of the numbers. An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
Example
lcm(a, b)
Returns the least common multiple of the numbers. An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
Example
ARRAY
empty
Checks whether the input array is empty.
Syntax
An array is considered empty if it does not contain any elements.
NOTE
Can be optimized by enabling the optimize_functions_to_subcolumns setting. With optimize_functions_to_subcolumns = 1
the function reads only size0 subcolumn instead of reading and processing the whole array column. The query SELECT empty(arr) FROM TABLE;
transforms to SELECT arr.size0 = 0 FROM TABLE;
.
The function also works for strings or UUID.
Arguments
[x]
— Input array. Array.
Returned value
Returns
1
for an empty array or0
for a non-empty array.
Type: UInt8.
Example
Query:
Result:
notEmpty
Checks whether the input array is non-empty.
Syntax
An array is considered non-empty if it contains at least one element.
NOTE
Can be optimized by enabling the optimize_functions_to_subcolumns setting. With optimize_functions_to_subcolumns = 1
the function reads only size0 subcolumn instead of reading and processing the whole array column. The query SELECT notEmpty(arr) FROM table
transforms to SELECT arr.size0 != 0 FROM TABLE
.
The function also works for strings or UUID.
Arguments
[x]
— Input array. Array.
Returned value
Returns
1
for a non-empty array or0
for an empty array.
Type: UInt8.
Example
Query:
Result:
length
Returns the number of items in the array. The result type is UInt64. The function also works for strings.
Syntax
Can be optimized by enabling the optimize_functions_to_subcolumns setting. With optimize_functions_to_subcolumns = 1
the function reads only size0 subcolumn instead of reading and processing the whole array column. The query SELECT length(arr) FROM table
transforms to SELECT arr.size0 FROM TABLE
.
Example
emptyArrayUInt8, emptyArrayUInt16, emptyArrayUInt32, emptyArrayUInt64
emptyArrayInt8, emptyArrayInt16, emptyArrayInt32, emptyArrayInt64
emptyArrayFloat32, emptyArrayFloat64
emptyArrayDate, emptyArrayDateTime
emptyArrayString
Accepts zero arguments and returns an empty array of the appropriate type.
emptyArrayToSingle
Accepts an empty array and returns a one-element array that is equal to the default value.
range(end), range([start, ] end [, step])
Returns an array of UInt
numbers from start
to end - 1
by step
.
Syntax
Arguments
start
— The first element of the array. Optional, required ifstep
is used. Default value: 0. UIntend
— The number before which the array is constructed. Required. UIntstep
— Determines the incremental step between each element in the array. Optional. Default value: 1. UInt
Returned value
Array of
UInt
numbers fromstart
toend - 1
bystep
.
Implementation details
All arguments must be positive values:
start
,end
,step
areUInt
data types, as well as elements of the returned array.An exception is thrown if query results in arrays with a total length of more than number of elements specified by the function_range_max_elements_in_block setting.
Examples
Query:
Result:
array(x1, …), operator [x1, …]
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.
Example
arrayConcat
Combines arrays passed as arguments.
Arguments
arrays
– Arbitrary number of arguments of Array type. Example
has(arr, elem)
Checks whether the ‘arr’ array has the ‘elem’ element. Returns 0 if the element is not in the array, or 1 if it is.
NULL
is processed as a value.
hasAll
Checks whether one array is a subset of another.
Arguments
set
– Array of any type with a set of elements.subset
– Array of any type with elements that should be tested to be a subset ofset
.
Return values
1
, ifset
contains all of the elements fromsubset
.0
, otherwise.
Peculiar properties
An empty array is a subset of any array.
Null
processed as a value.Order of values in both of arrays does not matter.
Examples
SELECT hasAll([], [])
returns 1.
SELECT hasAll([1, Null], [Null])
returns 1.
SELECT hasAll([1.0, 2, 3, 4], [1, 3])
returns 1.
SELECT hasAll(['a', 'b'], ['a'])
returns 1.
SELECT hasAll([1], ['a'])
returns 0.
SELECT hasAll([[1, 2], [3, 4]], [[1, 2], [3, 5]])
returns 0.
hasAny
Checks whether two arrays have intersection by some elements.
Arguments
array1
– Array of any type with a set of elements.array2
– Array of any type with a set of elements.
Return values
1
, ifarray1
andarray2
have one similar element at least.0
, otherwise.
Peculiar properties
Null
processed as a value.Order of values in both of arrays does not matter.
Examples
SELECT hasAny([1], [])
returns 0
.
SELECT hasAny([Null], [Null, 1])
returns 1
.
SELECT hasAny([-128, 1., 512], [1])
returns 1
.
SELECT hasAny([[1, 2], [3, 4]], ['a', 'c'])
returns 0
.
SELECT hasAll([[1, 2], [3, 4]], [[1, 2], [1, 2]])
returns 1
.
hasSubstr
Checks whether all the elements of array2 appear in array1 in the same exact order. Therefore, the function will return 1, if and only if array1 = prefix + array2 + suffix
.
In other words, the functions will check whether all the elements of array2
are contained in array1
like the hasAll
function. In addition, it will check that the elements are observed in the same order in both array1
and array2
.
For Example:
hasSubstr([1,2,3,4], [2,3])
returns 1. However,hasSubstr([1,2,3,4], [3,2])
will return0
.hasSubstr([1,2,3,4], [1,2,3])
returns 1. However,hasSubstr([1,2,3,4], [1,2,4])
will return0
.
Arguments
array1
– Array of any type with a set of elements.array2
– Array of any type with a set of elements.
Return values
1
, ifarray1
containsarray2
.0
, otherwise.
Peculiar properties
The function will return
1
ifarray2
is empty.Null
processed as a value. In other wordshasSubstr([1, 2, NULL, 3, 4], [2,3])
will return0
. However,hasSubstr([1, 2, NULL, 3, 4], [2,NULL,3])
will return1
Order of values in both of arrays does matter.
Examples
SELECT hasSubstr([], [])
returns 1.
SELECT hasSubstr([1, Null], [Null])
returns 1.
SELECT hasSubstr([1.0, 2, 3, 4], [1, 3])
returns 0.
SELECT hasSubstr(['a', 'b'], ['a'])
returns 1.
SELECT hasSubstr(['a', 'b' , 'c'], ['a', 'b'])
returns 1.
SELECT hasSubstr(['a', 'b' , 'c'], ['a', 'c'])
returns 0.
SELECT hasSubstr([[1, 2], [3, 4], [5, 6]], [[1, 2], [3, 4]])
returns 1.
indexOf(arr, x)
Returns the index of the first ‘x’ element (starting from 1) if it is in the array, or 0 if it is not.
Example:
Elements set to NULL
are handled as normal values.
arrayCount([func,] arr1, …)
Returns the number of elements for which func(arr1[i], …, arrN[i])
returns something other than 0. If func
is not specified, it returns the number of non-zero elements in the array.
Note that the arrayCount
is a higher-order function. You can pass a lambda function to it as the first argument.
Example
countEqual(arr, x)
Returns the number of elements in the array equal to x. Equivalent to arrayCount (elem -> elem = x, arr).
NULL
elements are handled as separate values.
Example
arrayEnumerate(arr)
Returns the array [1, 2, 3, …, length (arr) ]
This function is normally used with ARRAY JOIN. It allows counting something just once for each array after applying ARRAY JOIN. Example:
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:
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.
arrayEnumerateUniq(arr, …)
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].
This function is useful when using ARRAY JOIN and aggregation of array elements. Example:
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, we apply a condition to the value of the arrayEnumerateUniq(Goals.ID) function.
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.
This is necessary when using ARRAY JOIN with a nested data structure and further aggregation across multiple elements in this structure.
arrayPopBack
Removes the last item from the array.
Arguments
array
– Array.
Example
arrayPopFront
Removes the first item from the array.
Arguments
array
– Array.
Example
arrayPushBack
Adds one item to the end of the array.
Arguments
array
– Array.single_value
– A single value. Only numbers can be added to an array with numbers, and only strings can be added to an array of strings. When adding numbers, ClickHouse automatically sets thesingle_value
type for the data type of the array. For more information about the types of data in ClickHouse, see “Data types”. Can beNULL
. The function adds aNULL
element to an array, and the type of array elements converts toNullable
.
Example
arrayPushFront
Adds one element to the beginning of the array.
Arguments
array
– Array.single_value
– A single value. Only numbers can be added to an array with numbers, and only strings can be added to an array of strings. When adding numbers, ClickHouse automatically sets thesingle_value
type for the data type of the array. For more information about the types of data in ClickHouse, see “Data types”. Can beNULL
. The function adds aNULL
element to an array, and the type of array elements converts toNullable
.
Example
arrayResize
Changes the length of the array.
Arguments:
array
— Array.size
— Required length of the array.If
size
is less than the original size of the array, the array is truncated from the right.
If
size
is larger than the initial size of the array, the array is extended to the right withextender
values or default values for the data type of the array items.extender
— Value for extending an array. Can beNULL
.
Returned value:
An array of length size
.
Examples of calls
arraySlice
Returns a slice of the array.
Arguments
array
– Array of data.offset
– Indent from the edge of the array. A positive value indicates an offset on the left, and a negative value is an indent on the right. Numbering of the array items begins with 1.length
– The length of the required slice. If you specify a negative value, the function returns an open slice[offset, array_length - length]
. If you omit the value, the function returns the slice[offset, the_end_of_array]
.
Example
Array elements set to NULL
are handled as normal values.
arraySort([func,] arr, …)
Sorts the elements of the arr
array in ascending order. If the func
function is specified, sorting order is determined by the result of the func
function applied to the elements of the array. If func
accepts multiple arguments, the arraySort
function is passed several arrays that the arguments of func
will correspond to. Detailed examples are shown at the end of arraySort
description.
Example of integer values sorting:
Example of string values sorting:
Consider the following sorting order for the NULL
, NaN
and Inf
values:
-Inf
values are first in the array.NULL
values are last in the array.NaN
values are right beforeNULL
.Inf
values are right beforeNaN
.
Note that arraySort
is a higher-order function. 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:
For each element of the source array, the lambda function returns the sorting key, that is, [1 –> -1, 2 –> -2, 3 –> -3]. Since the arraySort
function sorts the keys in ascending order, the result is [3, 2, 1]. Thus, the (x) –> -x
lambda function sets the descending order in a sorting.
The lambda function can accept multiple arguments. In this case, you need to pass the arraySort
function several arrays of identical length that the arguments of lambda function will correspond to. The resulting array will consist of elements from the first input array; elements from the next input array(s) specify the sorting keys. For example:
Here, the elements that are passed in the second array ([2, 1]) define a sorting key for the corresponding element from the source array ([‘hello’, ‘world’]), that is, [‘hello’ –> 2, ‘world’ –> 1]. Since the lambda function does not use x
, actual values of the source array do not affect the order in the result. So, ‘hello’ will be the second element in the result, and ‘world’ will be the first.
Other examples are shown below.
NOTE
To improve sorting efficiency, the Schwartzian transform is used.
arrayReverseSort([func,] arr, …)
Sorts the elements of the arr
array in descending order. If the func
function is specified, arr
is sorted according to the result of the func
function applied to the elements of the array, and then the sorted array is reversed. If func
accepts multiple arguments, the arrayReverseSort
function is passed several arrays that the arguments of func
will correspond to. Detailed examples are shown at the end of arrayReverseSort
description.
Example of integer values sorting:
Example of string values sorting:
Consider the following sorting order for the NULL
, NaN
and Inf
values:
Inf
values are first in the array.NULL
values are last in the array.NaN
values are right beforeNULL
.-Inf
values are right beforeNaN
.
Note that the arrayReverseSort
is a higher-order function. You can pass a lambda function to it as the first argument. Example is shown below.
The array is sorted in the following way:
At first, the source array ([1, 2, 3]) is sorted according to the result of the lambda function applied to the elements of the array. The result is an array [3, 2, 1].
Array that is obtained on the previous step, is reversed. So, the final result is [1, 2, 3].
The lambda function can accept multiple arguments. In this case, you need to pass the arrayReverseSort
function several arrays of identical length that the arguments of lambda function will correspond to. The resulting array will consist of elements from the first input array; elements from the next input array(s) specify the sorting keys. For example:
In this example, the array is sorted in the following way:
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 the sorting keys for corresponding elements from the source array. The result is an array [‘world’, ‘hello’].
Array that was sorted on the previous step, is reversed. So, the final result is [‘hello’, ‘world’].
Other examples are shown below.
arrayUniq(arr, …)
If one argument is passed, it counts the number of different elements in the array. If multiple arguments are passed, it counts the number of different tuples of elements at corresponding positions in multiple arrays.
If you want to get a list of unique items in an array, you can use arrayReduce(‘groupUniqArray’, arr).
Example
arrayJoin(arr)
A special function. See the section “ArrayJoin function”.
arrayDifference
Calculates the difference between adjacent array elements. Returns an array where the first element will be 0, the second is the difference between a[1] - a[0]
, etc. The type of elements in the resulting array is determined by the type inference rules for subtraction (e.g. UInt8
- UInt8
= Int16
).
Syntax
Arguments
array
– Array.
Returned values
Returns an array of differences between adjacent elements.
Example
Query:
Result:
Example of the overflow due to result type Int64:
Query:
Result:
arrayDistinct
Takes an array, returns an array containing the distinct elements only.
Syntax
Arguments
array
– Array.
Returned values
Returns an array containing the distinct elements.
Example
Query:
Result:
arrayEnumerateDense(arr)
Returns an array of the same size as the source array, indicating where each element first appears in the source array.
Example
arrayIntersect(arr)
Takes multiple arrays, returns an array with elements that are present in all source arrays.
Example
arrayReduce
Applies an aggregate function to array elements and returns its result. The name of the aggregation function is passed as a string in single quotes 'max'
, 'sum'
. When using parametric aggregate functions, the parameter is indicated after the function name in parentheses 'uniqUpTo(6)'
.
Syntax
Arguments
agg_func
— The name of an aggregate function which should be a constant string.arr
— Any number of array type columns as the parameters of the aggregation function.
Returned value
Example
Query:
Result:
If an aggregate function takes multiple arguments, then this function must be applied to multiple arrays of the same size.
Query:
Result:
Example with a parametric aggregate function:
Query:
Result:
arrayReduceInRanges
Applies an aggregate function to array elements in given ranges and returns an array containing the result corresponding to each range. The function will return the same result as multiple arrayReduce(agg_func, arraySlice(arr1, index, length), ...)
.
Syntax
Arguments
agg_func
— The name of an aggregate function which should be a constant string.arr
— Any number of Array type columns as the parameters of the aggregation function.
Returned value
Array containing results of the aggregate function over specified ranges.
Type: Array.
Example
Query:
Result:
arrayReverse(arr)
Returns an array of the same size as the original array containing the elements in reverse order.
Example:
reverse(arr)
Synonym for “arrayReverse”
arrayFlatten
Converts an array of arrays to a flat array.
Function:
Applies to any depth of nested arrays.
Does not change arrays that are already flat.
The flattened array contains all the elements from all source arrays.
Syntax
Alias: flatten
.
Arguments
array_of_arrays
— Array of arrays. For example,[[1,2,3], [4,5]]
.
Examples
arrayCompact
Removes consecutive duplicate elements from an array. The order of result values is determined by the order in the source array.
Syntax
Arguments
arr
— The array to inspect.
Returned value
The array without duplicate.
Type: Array
.
Example
Query:
Result:
arrayZip
Combines multiple arrays into a single array. The resulting array contains the corresponding elements of the source arrays grouped into tuples in the listed order of arguments.
Syntax
Arguments
arrN
— Array.
The function can take any number of arrays of different types. All the input arrays must be of equal size.
Returned value
Array with elements from the source arrays grouped into tuples. Data types in the tuple are the same as types of the input arrays and in the same order as arrays are passed.
Type: Array.
Example
Query:
Result:
arrayAUC
Calculate AUC (Area Under the Curve, which is a concept in machine learning, see more details: https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve).
Syntax
Arguments
arr_scores
— scores prediction model gives.arr_labels
— labels of samples, usually 1 for positive sample and 0 for negtive sample.
Returned value
Returns AUC value with type Float64.
Example
Query:
Result:
arrayMap(func, arr1, …)
Returns an array obtained from the original arrays by application of func(arr1[i], …, arrN[i])
for each element. Arrays arr1
… arrN
must have the same number of elements.
Examples
The following example shows how to create a tuple of elements from different arrays:
Note that the arrayMap
is a higher-order function. You must pass a lambda function to it as the first argument, and it can’t be omitted.
arrayFilter(func, arr1, …)
Returns an array containing only the elements in arr1
for which func(arr1[i], …, arrN[i])
returns something other than 0.
Examples
Note that the arrayFilter
is a higher-order function. You must pass a lambda function to it as the first argument, and it can’t be omitted.
arrayFill(func, arr1, …)
Scan through arr1
from the first element to the last element and replace arr1[i]
by arr1[i - 1]
if func(arr1[i], …, arrN[i])
returns 0. The first element of arr1
will not be replaced.
Examples
Note that the arrayFill
is a higher-order function. You must pass a lambda function to it as the first argument, and it can’t be omitted.
arrayReverseFill(func, arr1, …)
Scan through arr1
from the last element to the first element and replace arr1[i]
by arr1[i + 1]
if func(arr1[i], …, arrN[i])
returns 0. The last element of arr1
will not be replaced.
Examples:
Note that the arrayReverseFill
is a higher-order function. You must pass a lambda function to it as the first argument, and it can’t be omitted.
arraySplit(func, arr1, …)
Split arr1
into multiple arrays. When func(arr1[i], …, arrN[i])
returns something other than 0, the array will be split on the left hand side of the element. The array will not be split before the first element.
Examples:
Note that the arraySplit
is a higher-order function. You must pass a lambda function to it as the first argument, and it can’t be omitted.
arrayReverseSplit(func, arr1, …)
Split arr1
into multiple arrays. When func(arr1[i], …, arrN[i])
returns something other than 0, the array will be split on the right hand side of the element. The array will not be split after the last element.
Examples:
Note that the arrayReverseSplit
is a higher-order function. You must pass a lambda function to it as the first argument, and it can’t be omitted.
arrayExists([func,] arr1, …)
Returns 1 if there is at least one element in arr
for which func(arr1[i], …, arrN[i])
returns something other than 0. Otherwise, it returns 0.
Note that the arrayExists
is a higher-order function. You can pass a lambda function to it as the first argument.
Example
arrayAll([func,] arr1, …)
Returns 1 if func(arr1[i], …, arrN[i])
returns something other than 0 for all the elements in arrays. Otherwise, it returns 0.
Note that the arrayAll
is a higher-order function. You can pass a lambda function to it as the first argument.
Example
arrayFirst(func, arr1, …)
Returns the first element in the arr1
array for which func(arr1[i], …, arrN[i])
returns something other than 0.
Note that the arrayFirst
is a higher-order function. You must pass a lambda function to it as the first argument, and it can’t be omitted.
Example
arrayFirstIndex(func, arr1, …)
Returns the index of the first element in the arr1
array for which func(arr1[i], …, arrN[i])
returns something other than 0.
Note that the arrayFirstIndex
is a higher-order function. You must pass a lambda function to it as the first argument, and it can’t be omitted.
Example
arrayMin
Returns the minimum of elements in the source array.
If the func
function is specified, returns the mininum of elements converted by this function.
Note that the arrayMin
is a higher-order function. You can pass a lambda function to it as the first argument.
Syntax
Arguments
func
— Function. Expression.arr
— Array. Array.
Returned value
The minimum of function values (or the array minimum).
Type: if func
is specified, matches func
return value type, else matches the array elements type.
Examples
Query:
Result:
Query:
Result:
arrayMax
Returns the maximum of elements in the source array.
If the func
function is specified, returns the maximum of elements converted by this function.
Note that the arrayMax
is a higher-order function. You can pass a lambda function to it as the first argument.
Syntax
Arguments
func
— Function. Expression.arr
— Array. Array.
Returned value
The maximum of function values (or the array maximum).
Type: if func
is specified, matches func
return value type, else matches the array elements type.
Examples
Query:
Result:
Query:
Result:
arraySum
Returns the sum of elements in the source array.
If the func
function is specified, returns the sum of elements converted by this function.
Note that the arraySum
is a higher-order function. You can pass a lambda function to it as the first argument.
Syntax
Arguments
func
— Function. Expression.arr
— Array. Array.
Returned value
The sum of the function values (or the array sum).
Type: for decimal numbers in source array (or for converted values, if func
is specified) — Decimal128, for floating point numbers — Float64, for numeric unsigned — UInt64, and for numeric signed — Int64.
Examples
Query:
Result:
Query:
Result:
arrayAvg
Returns the average of elements in the source array.
If the func
function is specified, returns the average of elements converted by this function.
Note that the arrayAvg
is a higher-order function. You can pass a lambda function to it as the first argument.
Syntax
Arguments
func
— Function. Expression.arr
— Array. Array.
Returned value
The average of function values (or the array average).
Type: Float64.
Examples
Query:
Result:
Query:
Result:
arrayCumSum([func,] arr1, …)
Returns an array of partial sums of elements in the source array (a running sum). If the func
function is specified, then the values of the array elements are converted by func(arr1[i], …, arrN[i])
before summing.
Example:
Note that the arrayCumSum
is a higher-order function. You can pass a lambda function to it as the first argument.
arrayCumSumNonNegative(arr)
Same as arrayCumSum
, returns an array of partial sums of elements in the source array (a running sum). Different arrayCumSum
, when then returned value contains a value less than zero, the value is replace with zero and the subsequent calculation is performed with zero parameters. For example:
Note that the arraySumNonNegative
is a higher-order function. You can pass a lambda function to it as the first argument.
arrayProduct
Multiplies elements of an array.
Syntax
Arguments
arr
— Array of numeric values.
Returned value
A product of array's elements.
Type: Float64.
Examples
Query:
Result:
Query:
Return value type is always Float64. Result:
BIT
Bit functions work for any pair of types from UInt8
, UInt16
, UInt32
, UInt64
, Int8
, Int16
, Int32
, Int64
, Float32
, or Float64
. Some functions support String
and FixedString
types.
The result type is an integer with bits equal to the maximum bits of its arguments. If at least one of the arguments is signed, the result is a signed number. If an argument is a floating-point number, it is cast to Int64.
bitAnd(a, b)
bitOr(a, b)
bitXor(a, b)
bitNot(a)
bitShiftLeft(a, b)
Shifts the binary representation of a value to the left by a specified number of bit positions.
A FixedString
or a String
is treated as a single multibyte value.
Bits of a FixedString
value are lost as they are shifted out. On the contrary, a String
value is extended with additional bytes, so no bits are lost.
Syntax
Arguments
a
— A value to shift. Integer types, String or FixedString.b
— The number of shift positions. Unsigned integer types, 64 bit types or less are allowed.
Returned value
Shifted value.
The type of the returned value is the same as the type of the input value.
Example
In the following queries bin and hex functions are used to show bits of shifted values.
Result:
bitShiftRight(a, b)
Shifts the binary representation of a value to the right by a specified number of bit positions.
A FixedString
or a String
is treated as a single multibyte value. Note that the length of a String
value is reduced as bits are shifted out.
Syntax
Arguments
a
— A value to shift. Integer types, String or FixedString.b
— The number of shift positions. Unsigned integer types, 64 bit types or less are allowed.
Returned value
Shifted value.
The type of the returned value is the same as the type of the input value.
Example
Query:
Result:
bitRotateLeft(a, b)
bitRotateRight(a, b)
bitTest
Takes any integer and converts it into binary form, returns the value of a bit at specified position. The countdown starts from 0 from the right to the left.
Syntax
Arguments
number
– Integer number.index
– Position of bit.
Returned values
Returns a value of bit at specified position.
Type: UInt8
.
Example
For example, the number 43 in base-2 (binary) numeral system is 101011.
Query:
Result:
Another example:
Query:
Result:
bitTestAll
Returns result of logical conjuction (AND operator) of all bits at given positions. The countdown starts from 0 from the right to the left.
The conjuction for bitwise operations:
0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1
Syntax
Arguments
number
– Integer number.index1
,index2
,index3
,index4
– Positions of bit. For example, for set of positions (index1
,index2
,index3
,index4
) is true if and only if all of its positions are true (index1
⋀index2
, ⋀index3
⋀index4
).
Returned values
Returns result of logical conjuction.
Type: UInt8
.
Example
For example, the number 43 in base-2 (binary) numeral system is 101011.
Query:
Result:
Another example:
Query:
Result:
bitTestAny
Returns result of logical disjunction (OR operator) of all bits at given positions. The countdown starts from 0 from the right to the left.
The disjunction for bitwise operations:
0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1
Syntax
Arguments
number
– Integer number.index1
,index2
,index3
,index4
– Positions of bit.
Returned values
Returns result of logical disjuction.
Type: UInt8
.
Example
For example, the number 43 in base-2 (binary) numeral system is 101011.
Query:
Result:
Another example:
Query:
Result:
bitCount
Calculates the number of bits set to one in the binary representation of a number.
Syntax
Arguments
x
— Integer or floating-point number. The function uses the value representation in memory. It allows supporting floating-point numbers.
Returned value
Number of bits set to one in the input number.
The function does not convert input value to a larger type (sign extension). So, for example, bitCount(toUInt8(-1)) = 8
.
Type: UInt8
.
Example
Take for example the number 333. Its binary representation: 0000000101001101.
Query:
Result:
bitHammingDistance
Returns the Hamming Distance between the bit representations of two integer values. Can be used with SimHash functions for detection of semi-duplicate strings. The smaller is the distance, the more likely those strings are the same.
Syntax
Arguments
Returned value
The Hamming distance.
Type: UInt8.
Examples
Query:
Result:
With SimHash:
Result:
BITMAP
Bitmap functions work for two bitmaps Object value calculation, it is to return new bitmap or cardinality while using formula calculation, such as and, or, xor, and not, etc.
There are 2 kinds of construction methods for Bitmap Object. One is to be constructed by aggregation function groupBitmap with -State, the other is to be constructed by Array Object. It is also to convert Bitmap Object to Array Object.
RoaringBitmap is wrapped into a data structure while actual storage of Bitmap objects. When the cardinality is less than or equal to 32, it uses Set objet. When the cardinality is greater than 32, it uses RoaringBitmap object. That is why storage of low cardinality set is faster.
For more information on RoaringBitmap, see: CRoaring.
bitmapBuild
Build a bitmap from unsigned integer array.
Arguments
array
– Unsigned integer array.
Example
bitmapToArray
Convert bitmap to integer array.
Arguments
bitmap
– Bitmap object.
Example
bitmapSubsetInRange
Return subset in specified range (not include the range_end).
Arguments
bitmap
– Bitmap object.range_start
– Range start point. Type: UInt32.range_end
– Range end point (excluded). Type: UInt32.
Example
bitmapSubsetLimit
Creates a subset of bitmap with n elements taken between range_start
and cardinality_limit
.
Syntax
Arguments
bitmap
– Bitmap object.range_start
– The subset starting point. Type: UInt32.cardinality_limit
– The subset cardinality upper limit. Type: UInt32.
Returned value
The subset.
Type: Bitmap object.
Example
Query:
Result:
subBitmap
Returns the bitmap elements, starting from the offset
position. The number of returned elements is limited by the cardinality_limit
parameter. Analog of the substring) string function, but for bitmap.
Syntax
Arguments
bitmap
– The bitmap. Type: Bitmap object.offset
– The position of the first element of the subset. Type: UInt32.cardinality_limit
– The maximum number of elements in the subset. Type: UInt32.
Returned value
The subset.
Type: Bitmap object.
Example
Query:
Result: