Aggregate Functions
Aggregate functions perform calculations on sets of values and return a single result.
aggregate
any_value
any_value(col: AggColT) -> Func
Returns the ANY_VALUE aggregate SQL function for the given column or expression.
The ANY_VALUE function returns an arbitrary value from the specified column or expression. It is useful when you do not care which particular value is returned, as long as it comes from one of the rows in the group.
Parameters:
-
col(str | Column | ColumnExpr | Func) βThe column, column expression, or DataChain expression from which to return an arbitrary value.
Returns:
-
Func(Func) βA Func object that represents the ANY_VALUE aggregate function.
Example
Notes
- The
any_valuefunction can be used with any type of input. - The result column will have the same type as the input expression.
- The result of
any_valueis non-deterministic, meaning it may return different values for different executions.
Source code in datachain/func/aggregate.py
avg
avg(col: AggColT) -> Func
Returns the AVG aggregate SQL function for the specified column.
The AVG function returns the average of a numeric column or expression in a table. It calculates the mean of all values in the specified input.
Parameters:
-
col(str | Column | ColumnExpr | Func) βThe numeric column, column expression, or DataChain expression for which to calculate the average.
Returns:
-
Func(Func) βA Func object that represents the AVG aggregate function.
Example
Notes
- The
avgfunction should be used on numeric columns or expressions. - The result column will always be of type float.
Source code in datachain/func/aggregate.py
collect
collect(col: AggColT) -> Func
Returns the COLLECT aggregate SQL function for the given column or expression.
The COLLECT function gathers all values from the specified column or expression into an array or similar structure. It is useful for combining values into a collection, often for further processing or aggregation.
Parameters:
-
col(str | Column | ColumnExpr | Func) βThe column, column expression, or DataChain expression from which to collect values.
Returns:
-
Func(Func) βA Func object that represents the COLLECT aggregate function.
Example
Notes
- The
collectfunction can be used with any input whose values can be collected. - The result column will have an array type derived from the input expression type.
Source code in datachain/func/aggregate.py
concat
concat(col: AggColT, separator='') -> Func
Returns the CONCAT aggregate SQL function for the given column or expression.
The CONCAT function concatenates values from the specified column or expression into a single string. It is useful for merging text values from multiple rows into a single combined value.
Parameters:
-
col(str | Column | ColumnExpr | Func) βThe string-valued column, column expression, or DataChain expression from which to concatenate values.
-
separator(str, default:'') βThe separator to use between concatenated values. Defaults to an empty string.
Returns:
-
Func(Func) βA Func object that represents the CONCAT aggregate function.
Example
Notes
- The
concatfunction should be used with string-valued inputs. - The result column will have a string type.
Source code in datachain/func/aggregate.py
count
count(col: AggColT | None = None) -> Func
Returns a COUNT aggregate SQL function for the specified column.
The COUNT function returns the number of rows. If a column or expression is provided, it counts the rows where that input evaluates to a non-NULL value.
Parameters:
-
col(str | Column | ColumnExpr | Func, default:None) βThe column, column expression, or DataChain expression to count. If omitted, counts all rows.
Returns:
-
Func(Func) βA
Funcobject representing the COUNT aggregate function.
Example
Notes
- The result column will always have an integer type.
Source code in datachain/func/aggregate.py
dense_rank
dense_rank() -> Func
Returns the DENSE_RANK window function for SQL queries.
The DENSE_RANK function assigns a rank to each row within a partition of a result set, without gaps in the ranking for ties. Rows with equal values receive the same rank, but the next rank is assigned consecutively (i.e., if two rows are ranked 1, the next row will be ranked 2).
Returns:
-
Func(Func) βA Func object that represents the DENSE_RANK window function.
Example
Notes
- The result column will always be of type int.
- The DENSE_RANK function differs from RANK in that it does not leave gaps in the ranking for tied values.
Source code in datachain/func/aggregate.py
first
first(col: AggColT) -> Func
Returns the FIRST_VALUE window function for SQL queries.
The FIRST_VALUE function returns the first value in an ordered set of values within a partition. The first value is determined by the specified order and can be useful for retrieving the leading value of a column or expression in a group of rows.
Parameters:
-
col(str | Column | ColumnExpr | Func) βThe column, column expression, or DataChain expression from which to retrieve the first value.
Returns:
-
Func(Func) βA Func object that represents the FIRST_VALUE window function.
Example
Note
- The result of
first_valuewill always reflect the value of the first row in the specified order. - The result column will have the same type as the input expression.
Source code in datachain/func/aggregate.py
max
max(col: AggColT) -> Func
Returns the MAX aggregate SQL function for the given column or expression.
The MAX function returns the largest value in the specified column or expression. It can be used on both numeric and non-numeric inputs.
Parameters:
-
col(str | Column | ColumnExpr | Func) βThe column, column expression, or DataChain expression for which to find the maximum value.
Returns:
-
Func(Func) βA Func object that represents the MAX aggregate function.
Example
Notes
- The
maxfunction can be used with numeric, date, and string inputs. - The result column will have the same type as the input expression.
Source code in datachain/func/aggregate.py
min
min(col: AggColT) -> Func
Returns the MIN aggregate SQL function for the specified column.
The MIN function returns the smallest value in the specified column or expression. It can be used on both numeric and non-numeric inputs.
Parameters:
-
col(str | Column | ColumnExpr | Func) βThe column, column expression, or DataChain expression for which to find the minimum value.
Returns:
-
Func(Func) βA Func object that represents the MIN aggregate function.
Example
Notes
- The
minfunction can be used with numeric, date, and string inputs. - The result column will have the same type as the input expression.
Source code in datachain/func/aggregate.py
rank
rank() -> Func
Returns the RANK window function for SQL queries.
The RANK function assigns a rank to each row within a partition of a result set, with gaps in the ranking for ties. Rows with equal values receive the same rank, and the next rank is skipped (i.e., if two rows are ranked 1, the next row is ranked 3).
Returns:
-
Func(Func) βA Func object that represents the RANK window function.
Example
Notes
- The result column will always be of type int.
- The RANK function differs from ROW_NUMBER in that rows with the same value in the ordering column(s) receive the same rank.
Source code in datachain/func/aggregate.py
row_number
row_number() -> Func
Returns the ROW_NUMBER window function for SQL queries.
The ROW_NUMBER function assigns a unique sequential integer to rows within a partition of a result set, starting from 1 for the first row in each partition. It is commonly used to generate row numbers within partitions or ordered results.
Returns:
-
Func(Func) βA Func object that represents the ROW_NUMBER window function.
Example
Note
- The result column will always be of type int.
Source code in datachain/func/aggregate.py
sum
sum(col: AggColT) -> Func
Returns the SUM aggregate SQL function for the specified column.
The SUM function returns the total sum of a numeric column or expression in a table. It sums up all the values for the specified input.
Parameters:
-
col(str | Column | ColumnExpr | Func) βThe numeric column, column expression, or DataChain expression for which to calculate the sum.
Returns:
-
Func(Func) βA
Funcobject that represents the SUM aggregate function.
Example
Notes
- The
sumfunction should be used on numeric columns or expressions. - The result column type will be inferred from the input expression type.
Source code in datachain/func/aggregate.py
xor_agg
Returns the XOR aggregate SQL function for the specified column.
Computes the bitwise XOR of all values. Order-independent, so the
result is the same regardless of row ordering. Useful for computing
content fingerprints when combined with func.string.string_hash.
Parameters:
Returns:
-
Func(Func) βA
Funcobject that represents the XOR aggregate function.