Work with composite data types.
Tuple functions enable efficient creation, manipulation, and analysis of composite data types. They support operations ranging from basic tuple creation to advanced element-wise arithmetic, facilitating complex data handling and structured analysis.
Creates a tuple from the given arguments.
Syntax:
Arguments:
x
, y
, … (any type): Values of any type.Returns:
A tuple containing the provided arguments.
Example:
Result:
This function allows you to group multiple values into a single tuple. It’s often used as an intermediate step for operations like IN
clauses or as arguments for lambda functions. Tuples cannot be directly written to tables.
The function implements the operator (x, y, ...)
, so you can also create tuples using parentheses:
Returns the element at the specified index or name in a tuple.
Syntax:
Arguments:
tuple
(Tuple
): The input tuple.index
(numeric): The 1-based index of the element to return.name
(String
): The name of the element to return.default_value
(optional): The value to return if the index is out of bounds or the name doesn’t exist.Returns:
The element at the specified index or name in the tuple.
Example:
Result:
In this example:
second_filling
returns the second element of the tuple.salsa_type
returns the value associated with the ‘salsa’ key in the named tuple.x.index
and x.name
for tuple access.Arguments:
x
(Tuple
): A tuple function, column, or tuple of elements.Returns:
Example:
Result:
In this example, untuple
expands the items
tuple into separate columns.
Example using EXCEPT:
Result:
This query excludes the quantity
field from the result.
The untuple
function is particularly useful when working with complex nested structures or when you need to flatten tuple data for further processing or analysis.
Calculates the Hamming distance between two tuples of the same size.
Syntax:
Arguments:
tuple1
(Tuple
): First tuple.tuple2
(Tuple
): Second tuple.Tuples should have the same type of elements.
Returns:
The result type is calculated the same way as for Arithmetic functions, based on the number of elements in the input tuples.
Example:
Result:
This function can be used with MinHash functions for detection of semi-duplicate strings:
Result:
In this example, we compare the MinHash of a taco menu item description with its case-insensitive version to detect potential duplicates or variations in menu listings.
Converts a named tuple into an array of (name, value) pairs.
Syntax:
Arguments:
tuple
(Tuple
): A named tuple.Returns:
An array of (name, value) pairs, where:
Type: Array(Tuple(String, T))
Example:
Result:
This function can be useful for transforming columns to rows:
Result:
If you pass a simple tuple without named elements, ClickHouse uses the element indices as names:
Result:
This function is particularly useful when working with complex nested structures or when you need to dynamically process tuple elements without knowing their names in advance.
Returns an array of column names from a tuple.
Syntax:
Arguments:
tuple
(Tuple
): A named tuple.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.
Type: Array(String)
.
Example:
Result:
If you pass a simple tuple without named elements to the function, ClickHouse uses the indices of the values as their names:
Result:
This function is useful when working with complex tuple structures or when you need to dynamically access the names of tuple elements in your queries.
Calculates the sum of corresponding elements in two tuples of the same size.
Syntax:
Alias:
Arguments:
tuple1
(Tuple
): First tuple.tuple2
(Tuple
): Second tuple.Returns:
Tuple
).Example:
Result:
In this example, tuplePlus
adds the corresponding elements of two tuples:
The function can be useful for vector operations or when you need to perform element-wise addition on multiple values simultaneously.
Both input tuples must have the same number of elements and compatible types for addition.
Calculates the subtraction of corresponding values of two tuples of the same size.
Syntax:
Alias:
Arguments:
tuple1
(Tuple
): First tuple.tuple2
(Tuple
): Second tuple.Returns:
A tuple with the result of subtraction. (Tuple
)
Example:
Result:
In this example, we subtract the values of the second tuple from the first tuple, simulating a taco shop calculation:
The tupleMinus
function performs element-wise subtraction, allowing for quick calculations on multiple values simultaneously.
Arguments:
tuple1
(Tuple
): First tuple.tuple2
(Tuple
): Second tuple.Returns:
Tuple
).Example:
Result:
In this example, we multiply two tuples element-wise:
The function can be useful for performing element-wise operations on multiple values simultaneously, such as calculating the total price of different taco ingredients:
This query would return a tuple with the cost of each ingredient based on its quantity and price.
Calculates the division of corresponding values of two tuples of the same size.
Syntax:
Arguments:
tuple1
(Tuple
): First tuple.tuple2
(Tuple
): Second tuple.Returns:
Tuple
).Division by zero will return inf
.
Example:
Result:
In this example, we divide the values of the first tuple (10, 4) by the corresponding values of the second tuple (2, 2), resulting in (5, 2).
Example:
This query compares the number and average price of spicy tacos to mild tacos. The result might look like:
This indicates that there are 75% as many spicy tacos as mild tacos, and spicy tacos are on average 1.2 times more expensive than mild tacos.
Calculates the negation of the tuple values.
Syntax:
Arguments:
tuple
(Tuple
): A tuple containing numeric values.Returns:
A tuple with the negated values of the input tuple. (Tuple
).
Example:
Result:
In this example, tupleNegate
calculates the negation of each value in the input tuple (1, 2, -3)
, resulting in (-1, -2, 3)
.
This function is useful when you need to invert the signs of all values in a tuple simultaneously, such as when working with coordinate systems or financial calculations involving debits and credits.
The tupleNegate
function only works with tuples containing numeric values. Attempting to negate non-numeric values will result in an error.
Multiplies all elements of a tuple by a given number.
Syntax:
Arguments:
tuple
(Tuple
): A tuple containing numeric values.number
(numeric): The multiplier.Returns:
Tuple
).Example:
Result:
In this example, each element of the tuple (1, 2, 3)
is multiplied by 2
, resulting in a new tuple (2, 4, 6)
.
Example:
Result:
This example simulates a 10% price increase for a taco and its extras. The original prices (3.50 for a basic taco, 1.25 for extra cheese, and 0.75 for extra salsa) are multiplied by 1.1 to calculate the new prices after the increase.
Divides each element of a tuple by a given number.
Syntax:
Arguments:
tuple
(Tuple
): A tuple containing numeric values.number
(numeric): The divisor.Returns:
Tuple
).inf
.Example:
Result:
In this example, each element of the tuple (10, 20, 30)
is divided by 5, resulting in the tuple (2, 4, 6)
.
Example:
Result:
In this example, we calculate half-price tacos by dividing each taco price in the taco_prices
tuple by 2.
Combines multiple tuples into a single tuple.
Syntax:
Arguments:
tuple1
, tuple2
, … (Tuple
): Two or more tuples of any type.Returns:
A new tuple containing all elements from the input tuples in the order they were provided.
Example:
Result:
In this example, tupleConcat
combines three tuples: one with a number and a taco filling, another with a different number and filling, and a third with a boolean and a salsa type. The result is a single tuple containing all these elements.
This function is useful for combining data from different sources or for creating more complex tuple structures from simpler ones.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numerators. Tuple of numeric type.tuple_div
(Tuple
): Tuple of divisors. Tuple of numeric type.Returns:
Tuple
of integer values.tuple_num
or tuple_div
contain non-integer values, the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor.Example:
Result:
In this example:
15 / 3 = 5
10 / 2 = 5
5 / 2 = 2
(integer division)This function is useful for performing element-wise integer division on tuples, which can be handy in various data processing scenarios, such as calculating ratios or normalizing data in tuple format.
Performs integer division of corresponding elements in two tuples, returning a tuple of the quotients. Unlike tupleIntDiv
, this function returns 0 for division by zero instead of throwing an error.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numerators. Tuple of numeric type.tuple_div
(Tuple
): Tuple of divisors. Tuple of numeric type.Returns:
Tuple
of integer values.tuple_num
or tuple_div
contain non-integer values, the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor.Example:
Result:
In this example:
15 / 3 = 5
10 / 0 = 0
(division by zero)5 / 2 = 2
(integer division)This function is particularly useful when you need to perform element-wise division on tuples and want to handle potential division by zero gracefully without causing errors.
Performs integer division of each element in a tuple by a given number.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numerator values. Tuple of numeric type.div
(numeric): The divisor value. Numeric type.Returns:
Tuple
of integer values.Implementation details:
tuple_num
or div
contain non-integer values, the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor.Example:
Result:
In this example, each element of the tuple (15, 10, 5)
is divided by 3, resulting in (5, 3, 1)
.
Example:
Result:
This query divides the lengths of taco filling names by 3, showing how many “thirds” each name length represents.
Performs 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 division by zero, but instead returns 0 for those cases.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numerator values. Tuple of numeric type.div
(numeric): The divisor value.Returns:
tuple_num
and div
. Tuple
of integer values.Implementation details:
tuple_num
or div
contain non-integer values, the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor.Examples:
Result:
Example dividing by zero:
Result:
This function is particularly useful when you need to perform division operations on tuples of values and want to handle division by zero gracefully without throwing errors.
Calculates the element-wise modulo operation between two tuples of the same size.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numerator values. Tuple of numeric type.tuple_mod
(Tuple
): Tuple of modulus values. Tuple of numeric type.Returns:
tuple_num
and tuple_mod
. Tuple
of non-zero integer values.Exception:
Example:
Result:
In this example, we calculate the remainders when dividing taco quantities by different serving sizes:
This function is useful for operations involving cyclic or periodic data, such as calculating remaining inventory after packaging tacos into different sized orders.
Calculates the element-wise modulo of a tuple by a number.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numeric values.div
(numeric): The divisor value.Returns:
Tuple
of numeric values.Example:
Result:
In this example:
15 % 3 = 0
10 % 3 = 1
5 % 3 = 2
The function calculates the modulo for each element in the input tuple using the provided divisor.
An error is thrown if division by zero occurs.
This function is useful for performing element-wise modulo operations on tuples, which can be handy in various data processing and analysis tasks involving cyclic or periodic data.
Flattens a nested named tuple into a single-level tuple.
Syntax:
Arguments:
input
(Tuple
): A nested named tuple to flatten.Returns:
A flattened tuple where elements are paths from the original input. (Tuple
).
This function can be used to select all paths from a nested tuple as separate columns.
Example:
Result:
In this example, the nested order
tuple is flattened into a single-level tuple, making it easier to access individual elements or use them in further calculations.
Work with composite data types.
Tuple functions enable efficient creation, manipulation, and analysis of composite data types. They support operations ranging from basic tuple creation to advanced element-wise arithmetic, facilitating complex data handling and structured analysis.
Creates a tuple from the given arguments.
Syntax:
Arguments:
x
, y
, … (any type): Values of any type.Returns:
A tuple containing the provided arguments.
Example:
Result:
This function allows you to group multiple values into a single tuple. It’s often used as an intermediate step for operations like IN
clauses or as arguments for lambda functions. Tuples cannot be directly written to tables.
The function implements the operator (x, y, ...)
, so you can also create tuples using parentheses:
Returns the element at the specified index or name in a tuple.
Syntax:
Arguments:
tuple
(Tuple
): The input tuple.index
(numeric): The 1-based index of the element to return.name
(String
): The name of the element to return.default_value
(optional): The value to return if the index is out of bounds or the name doesn’t exist.Returns:
The element at the specified index or name in the tuple.
Example:
Result:
In this example:
second_filling
returns the second element of the tuple.salsa_type
returns the value associated with the ‘salsa’ key in the named tuple.x.index
and x.name
for tuple access.Arguments:
x
(Tuple
): A tuple function, column, or tuple of elements.Returns:
Example:
Result:
In this example, untuple
expands the items
tuple into separate columns.
Example using EXCEPT:
Result:
This query excludes the quantity
field from the result.
The untuple
function is particularly useful when working with complex nested structures or when you need to flatten tuple data for further processing or analysis.
Calculates the Hamming distance between two tuples of the same size.
Syntax:
Arguments:
tuple1
(Tuple
): First tuple.tuple2
(Tuple
): Second tuple.Tuples should have the same type of elements.
Returns:
The result type is calculated the same way as for Arithmetic functions, based on the number of elements in the input tuples.
Example:
Result:
This function can be used with MinHash functions for detection of semi-duplicate strings:
Result:
In this example, we compare the MinHash of a taco menu item description with its case-insensitive version to detect potential duplicates or variations in menu listings.
Converts a named tuple into an array of (name, value) pairs.
Syntax:
Arguments:
tuple
(Tuple
): A named tuple.Returns:
An array of (name, value) pairs, where:
Type: Array(Tuple(String, T))
Example:
Result:
This function can be useful for transforming columns to rows:
Result:
If you pass a simple tuple without named elements, ClickHouse uses the element indices as names:
Result:
This function is particularly useful when working with complex nested structures or when you need to dynamically process tuple elements without knowing their names in advance.
Returns an array of column names from a tuple.
Syntax:
Arguments:
tuple
(Tuple
): A named tuple.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.
Type: Array(String)
.
Example:
Result:
If you pass a simple tuple without named elements to the function, ClickHouse uses the indices of the values as their names:
Result:
This function is useful when working with complex tuple structures or when you need to dynamically access the names of tuple elements in your queries.
Calculates the sum of corresponding elements in two tuples of the same size.
Syntax:
Alias:
Arguments:
tuple1
(Tuple
): First tuple.tuple2
(Tuple
): Second tuple.Returns:
Tuple
).Example:
Result:
In this example, tuplePlus
adds the corresponding elements of two tuples:
The function can be useful for vector operations or when you need to perform element-wise addition on multiple values simultaneously.
Both input tuples must have the same number of elements and compatible types for addition.
Calculates the subtraction of corresponding values of two tuples of the same size.
Syntax:
Alias:
Arguments:
tuple1
(Tuple
): First tuple.tuple2
(Tuple
): Second tuple.Returns:
A tuple with the result of subtraction. (Tuple
)
Example:
Result:
In this example, we subtract the values of the second tuple from the first tuple, simulating a taco shop calculation:
The tupleMinus
function performs element-wise subtraction, allowing for quick calculations on multiple values simultaneously.
Arguments:
tuple1
(Tuple
): First tuple.tuple2
(Tuple
): Second tuple.Returns:
Tuple
).Example:
Result:
In this example, we multiply two tuples element-wise:
The function can be useful for performing element-wise operations on multiple values simultaneously, such as calculating the total price of different taco ingredients:
This query would return a tuple with the cost of each ingredient based on its quantity and price.
Calculates the division of corresponding values of two tuples of the same size.
Syntax:
Arguments:
tuple1
(Tuple
): First tuple.tuple2
(Tuple
): Second tuple.Returns:
Tuple
).Division by zero will return inf
.
Example:
Result:
In this example, we divide the values of the first tuple (10, 4) by the corresponding values of the second tuple (2, 2), resulting in (5, 2).
Example:
This query compares the number and average price of spicy tacos to mild tacos. The result might look like:
This indicates that there are 75% as many spicy tacos as mild tacos, and spicy tacos are on average 1.2 times more expensive than mild tacos.
Calculates the negation of the tuple values.
Syntax:
Arguments:
tuple
(Tuple
): A tuple containing numeric values.Returns:
A tuple with the negated values of the input tuple. (Tuple
).
Example:
Result:
In this example, tupleNegate
calculates the negation of each value in the input tuple (1, 2, -3)
, resulting in (-1, -2, 3)
.
This function is useful when you need to invert the signs of all values in a tuple simultaneously, such as when working with coordinate systems or financial calculations involving debits and credits.
The tupleNegate
function only works with tuples containing numeric values. Attempting to negate non-numeric values will result in an error.
Multiplies all elements of a tuple by a given number.
Syntax:
Arguments:
tuple
(Tuple
): A tuple containing numeric values.number
(numeric): The multiplier.Returns:
Tuple
).Example:
Result:
In this example, each element of the tuple (1, 2, 3)
is multiplied by 2
, resulting in a new tuple (2, 4, 6)
.
Example:
Result:
This example simulates a 10% price increase for a taco and its extras. The original prices (3.50 for a basic taco, 1.25 for extra cheese, and 0.75 for extra salsa) are multiplied by 1.1 to calculate the new prices after the increase.
Divides each element of a tuple by a given number.
Syntax:
Arguments:
tuple
(Tuple
): A tuple containing numeric values.number
(numeric): The divisor.Returns:
Tuple
).inf
.Example:
Result:
In this example, each element of the tuple (10, 20, 30)
is divided by 5, resulting in the tuple (2, 4, 6)
.
Example:
Result:
In this example, we calculate half-price tacos by dividing each taco price in the taco_prices
tuple by 2.
Combines multiple tuples into a single tuple.
Syntax:
Arguments:
tuple1
, tuple2
, … (Tuple
): Two or more tuples of any type.Returns:
A new tuple containing all elements from the input tuples in the order they were provided.
Example:
Result:
In this example, tupleConcat
combines three tuples: one with a number and a taco filling, another with a different number and filling, and a third with a boolean and a salsa type. The result is a single tuple containing all these elements.
This function is useful for combining data from different sources or for creating more complex tuple structures from simpler ones.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numerators. Tuple of numeric type.tuple_div
(Tuple
): Tuple of divisors. Tuple of numeric type.Returns:
Tuple
of integer values.tuple_num
or tuple_div
contain non-integer values, the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor.Example:
Result:
In this example:
15 / 3 = 5
10 / 2 = 5
5 / 2 = 2
(integer division)This function is useful for performing element-wise integer division on tuples, which can be handy in various data processing scenarios, such as calculating ratios or normalizing data in tuple format.
Performs integer division of corresponding elements in two tuples, returning a tuple of the quotients. Unlike tupleIntDiv
, this function returns 0 for division by zero instead of throwing an error.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numerators. Tuple of numeric type.tuple_div
(Tuple
): Tuple of divisors. Tuple of numeric type.Returns:
Tuple
of integer values.tuple_num
or tuple_div
contain non-integer values, the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor.Example:
Result:
In this example:
15 / 3 = 5
10 / 0 = 0
(division by zero)5 / 2 = 2
(integer division)This function is particularly useful when you need to perform element-wise division on tuples and want to handle potential division by zero gracefully without causing errors.
Performs integer division of each element in a tuple by a given number.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numerator values. Tuple of numeric type.div
(numeric): The divisor value. Numeric type.Returns:
Tuple
of integer values.Implementation details:
tuple_num
or div
contain non-integer values, the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor.Example:
Result:
In this example, each element of the tuple (15, 10, 5)
is divided by 3, resulting in (5, 3, 1)
.
Example:
Result:
This query divides the lengths of taco filling names by 3, showing how many “thirds” each name length represents.
Performs 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 division by zero, but instead returns 0 for those cases.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numerator values. Tuple of numeric type.div
(numeric): The divisor value.Returns:
tuple_num
and div
. Tuple
of integer values.Implementation details:
tuple_num
or div
contain non-integer values, the result is calculated by rounding to the nearest integer for each non-integer numerator or divisor.Examples:
Result:
Example dividing by zero:
Result:
This function is particularly useful when you need to perform division operations on tuples of values and want to handle division by zero gracefully without throwing errors.
Calculates the element-wise modulo operation between two tuples of the same size.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numerator values. Tuple of numeric type.tuple_mod
(Tuple
): Tuple of modulus values. Tuple of numeric type.Returns:
tuple_num
and tuple_mod
. Tuple
of non-zero integer values.Exception:
Example:
Result:
In this example, we calculate the remainders when dividing taco quantities by different serving sizes:
This function is useful for operations involving cyclic or periodic data, such as calculating remaining inventory after packaging tacos into different sized orders.
Calculates the element-wise modulo of a tuple by a number.
Syntax:
Arguments:
tuple_num
(Tuple
): Tuple of numeric values.div
(numeric): The divisor value.Returns:
Tuple
of numeric values.Example:
Result:
In this example:
15 % 3 = 0
10 % 3 = 1
5 % 3 = 2
The function calculates the modulo for each element in the input tuple using the provided divisor.
An error is thrown if division by zero occurs.
This function is useful for performing element-wise modulo operations on tuples, which can be handy in various data processing and analysis tasks involving cyclic or periodic data.
Flattens a nested named tuple into a single-level tuple.
Syntax:
Arguments:
input
(Tuple
): A nested named tuple to flatten.Returns:
A flattened tuple where elements are paths from the original input. (Tuple
).
This function can be used to select all paths from a nested tuple as separate columns.
Example:
Result:
In this example, the nested order
tuple is flattened into a single-level tuple, making it easier to access individual elements or use them in further calculations.