Aggregate Functions¶
Aggregate functions operate on a set of values to compute a single result.
Except for count()
, count_if()
, max_by()
, min_by()
and
approx_distinct()
, all of these aggregate functions ignore null values
and return null for no input rows or when all values are null. For example,
sum()
returns null rather than zero and avg()
does not include null
values in the count. The coalesce
function can be used to convert null into
zero.
Some aggregate functions such as array_agg()
produce different results
depending on the order of input values.
General Aggregate Functions¶
- arbitrary(x) [same as input] ¶
Returns an arbitrary non-null value of
x
, if one exists.
- array_agg(x) array<[same as input]> ¶
Returns an array created from the input
x
elements.
- avg(x) double | real ¶
Returns the average (arithmetic mean) of all input values. When x is of type REAL, the result type is REAL. For all other input types, the result type is DOUBLE.
- bool_and(boolean) boolean ¶
Returns
TRUE
if every input value isTRUE
, otherwiseFALSE
.
- bool_or(boolean) boolean ¶
Returns
TRUE
if any input value isTRUE
, otherwiseFALSE
.
- checksum(x) varbinary ¶
Returns an order-insensitive checksum of the given values.
- count(*) bigint ¶
Returns the number of input rows.
- count(x) bigint ¶
Returns the number of non-null input values.
- count_if(x) bigint ¶
Returns the number of
TRUE
input values. This function is equivalent tocount(CASE WHEN x THEN 1 END)
.
- every(boolean) boolean ¶
This is an alias for
bool_and()
.
- histogram(x)¶
Returns a map containing the count of the number of times each input value occurs. Supports integral, floating-point, boolean, timestamp, and date input types.
- max_by(x, y) [same as x] ¶
Returns the value of
x
associated with the maximum value ofy
over all input values.
- min_by(x, y) [same as x] ¶
Returns the value of
x
associated with the minimum value ofy
over all input values.
- max(x) [same as input] ¶
Returns the maximum value of all input values.
- min(x) [same as input] ¶
Returns the minimum value of all input values.
- sum(x) [same as input] ¶
Returns the sum of all input values.
Bitwise Aggregate Functions¶
- bitwise_and_agg(x) bigint ¶
Returns the bitwise AND of all input values in 2’s complement representation.
- bitwise_or_agg(x) bigint ¶
Returns the bitwise OR of all input values in 2’s complement representation.
Map Aggregate Functions¶
- map_agg(key, value)¶
Returns a map created from the input
key
/value
pairs.
- map_union(map(K, V)) -> map(K, V)¶
Returns the union of all the input
maps
. If akey
is found in multiple inputmaps
, thatkey’s
value
in the resultingmap
comes from an arbitrary inputmap
.
Approximate Aggregate Functions¶
- approx_distinct(x) bigint ¶
Returns the approximate number of distinct input values. This function provides an approximation of
count(DISTINCT x)
. Zero is returned if all input values are null.This function should produce a standard error of 2.3%, which is the standard deviation of the (approximately normal) error distribution over all possible sets. It does not guarantee an upper bound on the error for any specific input set.
- approx_distinct(x, e) bigint ¶
Returns the approximate number of distinct input values. This function provides an approximation of
count(DISTINCT x)
. Zero is returned if all input values are null.This function should produce a standard error of no more than
e
, which is the standard deviation of the (approximately normal) error distribution over all possible sets. It does not guarantee an upper bound on the error for any specific input set. The current implementation of this function requires thate
be in the range of[0.0040625, 0.26000]
.
- approx_most_frequent(buckets, value, capacity) map<[same as value], bigint> ¶
Computes the top frequent values up to
buckets
elements approximately. Approximate estimation of the function enables us to pick up the frequent values with less memory. Largercapacity
improves the accuracy of underlying algorithm with sacrificing the memory capacity. The returned value is a map containing the top elements with corresponding estimated frequency.The error of the function depends on the permutation of the values and its cardinality. We can set the capacity same as the cardinality of the underlying data to achieve the least error.
buckets
andcapacity
must bebigint
.value
can be numeric or string type.The function uses the stream summary data structure proposed in the paper Efficient computation of frequent and top-k elements in data streams by A. Metwally, D. Agrawal and A. Abbadi.
- approx_percentile(x, percentage) [same as x] ¶
Returns the approximate percentile for all input values of
x
at the givenpercentage
. The value ofpercentage
must be between zero and one and must be constant for all input rows.
- approx_percentile(x, percentage, accuracy) [same as x] ¶
As
approx_percentile(x, percentage)
, but with a maximum rank error ofaccuracy
. The value ofaccuracy
must be between zero and one (exclusive) and must be constant for all input rows. Note that a lower “accuracy” is really a lower error threshold, and thus more accurate. The default accuracy is 0.0133. The underlying implementation is KLL sketch thus has a stronger guarantee for accuracy than T-Digest.
- approx_percentile(x, percentages) array<[same as x]> ¶
Returns the approximate percentile for all input values of
x
at each of the specified percentages. Each element of thepercentages
array must be between zero and one, and the array must be constant for all input rows.
- approx_percentile(x, percentages, accuracy) array<[same as x]> ¶
As
approx_percentile(x, percentages)
, but with a maximum rank error ofaccuracy
.
- approx_percentile(x, w, percentage) [same as x] ¶
Returns the approximate weighed percentile for all input values of
x
using the per-item weightw
at the percentagep
. The weight must be an integer value of at least one. It is effectively a replication count for the valuex
in the percentile set. The value ofp
must be between zero and one and must be constant for all input rows.
- approx_percentile(x, w, percentage, accuracy) [same as x] ¶
As
approx_percentile(x, w, percentage)
, but with a maximum rank error ofaccuracy
.
- approx_percentile(x, w, percentages) array<[same as x]> ¶
Returns the approximate weighed percentile for all input values of
x
using the per-item weightw
at each of the given percentages specified in the array. The weight must be an integer value of at least one. It is effectively a replication count for the valuex
in the percentile set. Each element of the array must be between zero and one, and the array must be constant for all input rows.
- approx_percentile(x, w, percentages, accuracy) array<[same as x]> ¶
As
approx_percentile(x, w, percentages)
, but with a maximum rank error ofaccuracy
.
Statistical Aggregate Functions¶
- corr(y, x) double ¶
Returns correlation coefficient of input values.
- covar_pop(y, x) double ¶
Returns the population covariance of input values.
- covar_samp(y, x) double ¶
Returns the sample covariance of input values.
- stddev(x) double ¶
This is an alias for stddev_samp().
- stddev_pop(x) double ¶
Returns the population standard deviation of all input values.
- stddev_samp(x) double ¶
Returns the sample standard deviation of all input values.
- variance(x) double ¶
This is an alias for var_samp().
- var_pop(x) double ¶
Returns the population variance of all input values.
- var_samp(x) double ¶
Returns the sample variance of all input values.
Miscellaneous¶
- max_data_size_for_stats(x) bigint ¶
Returns an estimate of the the maximum in-memory size in bytes of
x
.