Tuple Functions
tuple
A function that allows grouping multiple columns.
For columns C1, C2, ...
with the types T1, T2, ...,
it returns Tuple(T1, T2, ...)
. If setting enable_named_columns_in_function_tuple
is enabled then it returns Tuple(C1 T1, C2 T2, ...)
containing these columns if their names are unique and can be treated as unquoted identifiers. There is no cost to execute the function.
Tuples are normally used as intermediate values for an argument of IN operators, or for creating a list of formal parameters of lambda functions. Tuples can't be written to a table.
The function implements the operator (x, y, ...)
.
Syntax
tupleElement
A function that allows getting a column from a tuple.
If the second argument is a number index
, it is the column index, starting from 1. If the second argument is a string name
, it represents the name of the element. Besides, we can provide the third optional argument, such that when index out of bounds or no element exist for the name, the default value returned instead of throwing an exception. The second and third arguments, if provided, must be constants. There is no cost to execute the function.
The function implements operators x.index
and x.name
.
Syntax
untuple
Performs syntactic substitution of tuple elements in the call location.
The names of the result columns are implementation-specific and subject to change. Do not assume specific column names after untuple
.
Syntax
You can use the EXCEPT
expression to skip columns as a result of the query.
Arguments
x
— Atuple
function, column, or tuple of elements. Tuple.
Returned value
- None.
Examples
Input table:
Example of using a Tuple
-type column as the untuple
function parameter:
Query:
Result:
Example of using an EXCEPT
expression:
Query:
Result:
See Also
tupleHammingDistance
Returns the Hamming Distance between two tuples of the same size.
Syntax
Arguments
Tuples should have the same type of the elements.
Returned value
- The Hamming distance.
The result type is calculated the same way it is for Arithmetic functions, based on the number of elements in the input tuples.
Examples
Query:
Result:
Can be used with MinHash functions for detection of semi-duplicate strings:
Result:
tupleToNameValuePairs
Turns a named tuple into an array of (name, value) pairs. For a Tuple(a T, b T, ..., c T)
returns Array(Tuple(String, T), ...)
in which the Strings
represents the named fields of the tuple and T
are the values associated with those names. All values in the tuple should be of the same type.
Syntax
Arguments
tuple
— Named tuple. Tuple with any types of values.
Returned value
Example
Query:
Result:
It is possible to transform columns to rows using this function:
Result:
If you pass a simple tuple to the function, ClickHouse uses the indexes of the values as their names:
Result:
tupleNames
Converts a tuple into an array of column names. For a tuple in the form Tuple(a T, b T, ...)
, it returns an array of strings representing the named columns of the tuple. If the tuple elements do not have explicit names, their indices will be used as the column names instead.
Syntax
Arguments
tuple
— Named tuple. Tuple with any types of values.
Returned value
- An array with strings.
Type: Array(Tuple(String, ...)).
Example
Query:
Result:
If you pass a simple tuple to the function, ClickHouse uses the indexes of the columns as their names:
Result:
tuplePlus
Calculates the sum of corresponding values of two tuples of the same size.
Syntax
Alias: vectorSum
.
Arguments
Returned value
- Tuple with the sum. Tuple.
Example
Query:
Result:
tupleMinus
Calculates the subtraction of corresponding values of two tuples of the same size.
Syntax
Alias: vectorDifference
.
Arguments
Returned value
- Tuple with the result of subtraction. Tuple.
Example
Query:
Result:
tupleMultiply
Calculates the multiplication of corresponding values of two tuples of the same size.
Syntax
Arguments
Returned value
- Tuple with the multiplication. Tuple.
Example
Query:
Result:
tupleDivide
Calculates the division of corresponding values of two tuples of the same size. Note that division by zero will return inf
.
Syntax
Arguments
Returned value
- Tuple with the result of division. Tuple.
Example
Query:
Result:
tupleNegate
Calculates the negation of the tuple values.
Syntax
Arguments
tuple
— Tuple.
Returned value
- Tuple with the result of negation. Tuple.
Example
Query:
Result:
tupleMultiplyByNumber
Returns a tuple with all values multiplied by a number.
Syntax
Arguments
Returned value
- Tuple with multiplied values. Tuple.
Example
Query:
Result:
tupleDivideByNumber
Returns a tuple with all values divided by a number. Note that division by zero will return inf
.
Syntax
Arguments
Returned value
- Tuple with divided values. Tuple.
Example
Query:
Result:
tupleConcat
Combines tuples passed as arguments.
Arguments
tuples
– Arbitrary number of arguments of Tuple type.
Example
tupleIntDiv
Does integer division of a tuple of numerators and a tuple of denominators, and returns a tuple of the quotients.
Syntax
Parameters
tuple_num
: Tuple of numerator values. Tuple of numeric type.tuple_div
: Tuple of divisor values. Tuple of numeric type.
Returned value
- Tuple of the quotients of
tuple_num
andtuple_div
. Tuple of integer values.
Implementation details
- If either
tuple_num
ortuple_div
contain non-integer values then the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor. - An error will be thrown for division by 0.
Examples
Query:
Result:
Query:
Result:
tupleIntDivOrZero
Like tupleIntDiv it does integer division of a tuple of numerators and a tuple of denominators, and returns a tuple of the quotients. It does not throw an error for 0 divisors, but rather returns the quotient as 0.
Syntax
tuple_num
: Tuple of numerator values. Tuple of numeric type.tuple_div
: Tuple of divisor values. Tuple of numeric type.
Returned value
- Tuple of the quotients of
tuple_num
andtuple_div
. Tuple of integer values. - Returns 0 for quotients where the divisor is 0.
Implementation details
- If either
tuple_num
ortuple_div
contain non-integer values then the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor as in tupleIntDiv.
Examples
Query:
Result:
tupleIntDivByNumber
Does integer division of a tuple of numerators by a given denominator, and returns a tuple of the quotients.
Syntax
Parameters
Returned value
- Tuple of the quotients of
tuple_num
anddiv
. Tuple of integer values.
Implementation details
- If either
tuple_num
ordiv
contain non-integer values then the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor. - An error will be thrown for division by 0.
Examples
Query:
Result:
Query:
Result:
tupleIntDivOrZeroByNumber
Like tupleIntDivByNumber it does integer division of a tuple of numerators by a given denominator, and returns a tuple of the quotients. It does not throw an error for 0 divisors, but rather returns the quotient as 0.
Syntax
Parameters
Returned value
- Tuple of the quotients of
tuple_num
anddiv
. Tuple of integer values. - Returns 0 for quotients where the divisor is 0.
Implementation details
- If either
tuple_num
ordiv
contain non-integer values then the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor as in tupleIntDivByNumber.
Examples
Query:
Result:
Query:
Result:
tupleModulo
Returns a tuple of the moduli (remainders) of division operations of two tuples.
Syntax
Parameters
tuple_num
: Tuple of numerator values. Tuple of numeric type.tuple_div
: Tuple of modulus values. Tuple of numeric type.
Returned value
- Tuple of the remainders of division of
tuple_num
andtuple_div
. Tuple of non-zero integer values. - An error is thrown for division by zero.
Examples
Query:
Result:
tupleModuloByNumber
Returns a tuple of the moduli (remainders) of division operations of a tuple and a given divisor.
Syntax
Parameters
Returned value
- Tuple of the remainders of division of
tuple_num
anddiv
. Tuple of non-zero integer values. - An error is thrown for division by zero.
Examples
Query:
Result:
flattenTuple
Returns a flattened output
tuple from a nested named input
tuple. Elements of the output
tuple are the paths from the original input
tuple. For instance: Tuple(a Int, Tuple(b Int, c Int)) -> Tuple(a Int, b Int, c Int)
. flattenTuple
can be used to select all paths from type Object
as separate columns.
Syntax
Parameters
input
: Nested named tuple to flatten. Tuple.
Returned value
output
tuple whose elements are paths from the originalinput
. Tuple.
Example
Query:
Result:
Distance functions
All supported functions are described in distance functions documentation.
flattenTuple
Introduced in: v22.6
Flattens a named and nested tuple. The elements of the returned tuple are the paths of the input tuple.
Syntax
Arguments
input
— Named and nested tuple to flatten.Tuple(n1 T1[, n2 T2, ... ])
Returned value
Returns an output tuple whose elements are paths from the original input. Tuple(T)
Examples
Usage example
tuple
Introduced in: v
Returns a tuple by grouping input arguments.
For columns C1, C2, ... with the types T1, T2, ..., it returns a named Tuple(C1 T1, C2 T2, ...) type tuple containing these columns if their names are unique and can be treated as unquoted identifiers, otherwise a Tuple(T1, T2, ...) is returned. There is no cost to execute the function. Tuples are normally used as intermediate values for an argument of IN operators, or for creating a list of formal parameters of lambda functions. Tuples can't be written to a table.
The function implements the operator (x, y, ...)
.
Syntax
Arguments
- None. Returned value
Examples
typical
tupleConcat
Introduced in: v23.8
Combines tuples passed as arguments.
Syntax
Arguments
tupleN
— Arbitrary number of arguments of Tuple type.Tuple(T)
Returned value
Returns a tuple containing all elements from the input tuples. Tuple(T)
Examples
Usage example
tupleDivide
Introduced in: v21.11
Calculates the division of corresponding elements of two tuples of the same size.
Division by zero will return inf
.
Syntax
Arguments
t1
— First tuple.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
t2
— Second tuple.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
Returned value
Returns tuple with the result of division. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
tupleDivideByNumber
Introduced in: v21.11
Returns a tuple with all elements divided by a number.
Division by zero will return inf
.
Syntax
Arguments
tuple
— Tuple to divide.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
number
— Divider.(U)Int*
orFloat*
orDecimal
Returned value
Returns a tuple with divided elements. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
tupleElement
Introduced in: v1.1
Extracts an element from a tuple by index or name.
For access by index, an 1-based numeric index is expected. For access by name, the element name can be provided as a string (works only for named tuples).
An optional third argument specifies a default value which is returned instead of throwing an exception when the accessed element does not exist. All arguments must be constants.
This function has zero runtime cost and implements the operators x.index
and x.name
.
Syntax
Arguments
tuple
— A tuple or array of tuples.Tuple(T)
orArray(Tuple(T))
index
— Column index, starting from 1.const UInt8/16/32/64
name
— Name of the element.const String
default_value
— Default value returned when index is out of bounds or element doesn't exist.Any
Returned value
Returns the element at the specified index or name. Any
Examples
Index access
Named tuple with table
With default value
Operator syntax
tupleHammingDistance
Introduced in: v21.1
Returns the Hamming Distance between two tuples of the same size.
The result type is determined the same way it is for Arithmetic functions, based on the number of elements in the input tuples.
Syntax
Arguments
Returned value
Returns the Hamming distance. UInt8/16/32/64
Examples
Usage example
With MinHash to detect semi-duplicate strings
tupleIntDiv
Introduced in: v23.8
Performs an integer division with a tuple of numerators and a tuple of denominators. Returns a tuple of quotients. If either tuple contains non-integer elements then the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor. Division by 0 causes an error to be thrown.
Syntax
Arguments
tuple_num
— Tuple of numerator values.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
tuple_div
— Tuple of divisor values.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
Returned value
Returns a tuple of the quotients. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
With decimals
tupleIntDivByNumber
Introduced in: v23.8
Performs integer division of a tuple of numerators by a given denominator, and returns a tuple of the quotients. If either of the input parameters contain non-integer elements then the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor. An error will be thrown for division by 0.
Syntax
Arguments
tuple_num
— Tuple of numerator values.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
div
— The divisor value.(U)Int*
orFloat*
orDecimal
Returned value
Returns a tuple of the quotients. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
With decimals
tupleIntDivOrZero
Introduced in: v23.8
Like tupleIntDiv
performs integer division of a tuple of numerators and a tuple of denominators, and returns a tuple of the quotients.
In case of division by 0, returns the quotient as 0 instead of throwing an exception.
If either tuple contains non-integer elements then the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor.
Syntax
Arguments
tuple_num
— Tuple of numerator values.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
tuple_div
— Tuple of divisor values.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
Returned value
Returns tuple of the quotients. Returns 0 for quotients where the divisor is 0. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
With zero divisors
tupleIntDivOrZeroByNumber
Introduced in: v23.8
Like tupleIntDivByNumber
it does integer division of a tuple of numerators by a given denominator, and returns a tuple of the quotients.
It does not throw an error for zero divisors, but rather returns the quotient as zero.
If either the tuple or div contain non-integer elements then the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor.
Syntax
Arguments
tuple_num
— Tuple of numerator values.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
div
— The divisor value.(U)Int*
orFloat*
orDecimal
Returned value
Returns a tuple of the quotients with 0
for quotients where the divisor is 0
. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
With zero divisor
tupleMinus
Introduced in: v21.11
Calculates the difference between corresponding elements of two tuples of the same size.
Syntax
Arguments
t1
— First tuple.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
t2
— Second tuple.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
Returned value
Returns a tuple containing the results of the subtractions. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
tupleModulo
Introduced in: v23.8
Returns a tuple of the remainders (moduli) of division operations of two tuples.
Syntax
Arguments
tuple_num
— Tuple of numerator values.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
tuple_mod
— Tuple of modulus values.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
Returned value
Returns tuple of the remainders of division. An error is thrown for division by zero. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
tupleModuloByNumber
Introduced in: v23.8
Returns a tuple of the moduli (remainders) of division operations of a tuple and a given divisor.
Syntax
Arguments
tuple_num
— Tuple of numerator elements.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
div
— The divisor value.(U)Int*
orFloat*
orDecimal
Returned value
Returns tuple of the remainders of division. An error is thrown for division by zero. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
tupleMultiply
Introduced in: v21.11
Calculates the multiplication of corresponding elements of two tuples of the same size.
Syntax
Arguments
t1
— First tuple.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
t2
— Second tuple.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
Returned value
Returns a tuple with the results of the multiplications. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
tupleMultiplyByNumber
Introduced in: v21.11
Returns a tuple with all elements multiplied by a number.
Syntax
Arguments
tuple
— Tuple to multiply.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
number
— Multiplier.(U)Int*
orFloat*
orDecimal
Returned value
Returns a tuple with multiplied elements. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
tupleNames
Introduced in: v
Converts a tuple into an array of column names. For a tuple in the form Tuple(a T, b T, ...)
, it returns an array of strings representing the named columns of the tuple. If the tuple elements do not have explicit names, their indices will be used as the column names instead.
Syntax
Arguments
- None. Returned value
Examples
typical
tupleNegate
Introduced in: v21.11
Calculates the negation of the tuple elements.
Syntax
Arguments
t
— Tuple to negate.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
Returned value
Returns a tuple with the result of negation. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
tuplePlus
Introduced in: v21.11
Calculates the sum of corresponding elements of two tuples of the same size.
Syntax
Arguments
t1
— First tuple.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
t2
— Second tuple.Tuple((U)Int*)
orTuple(Float*)
orTuple(Decimal)
Returned value
Returns a tuple containing the sums of corresponding input tuple arguments. Tuple((U)Int*)
or Tuple(Float*)
or Tuple(Decimal)
Examples
Basic usage
tupleToNameValuePairs
Introduced in: v21.9
Converts a tuple to an array of (name, value)
pairs.
For example, tuple Tuple(n1 T1, n2 T2, ...)
is converted to Array(Tuple('n1', T1), Tuple('n2', T2), ...)
.
All values in the tuple must be of the same type.
Syntax
Arguments
tuple
— Named tuple with any types of values.Tuple(n1 T1[, n2 T2, ...])
Returned value
Returns an array with (name, value)
pairs. Array(Tuple(String, T))
Examples
Named tuple
Unnamed tuple