Arithmetic functions
Perform basic and advanced mathematical operations.
Arithmetic functions in ClickHouse perform mathematical operations on numeric data types.
These functions work with operands of types UInt8
, UInt16
, UInt32
, UInt64
, Int8
, Int16
, Int32
, Int64
, Float32
, or Float64
.
Type casting and result types
Before performing an operation, both operands are cast to the result type. The result type is determined as follows:
- For operands up to 32 bits wide, the result type is the next larger type after the larger of the two operands (integer size promotion).
- For operands of 64 bits or more, the result type matches the size of the larger operand.
- If one operand is signed, the result type will be signed; otherwise, it will be unsigned.
This approach ensures quick calculations using the maximum native integer width of 64 bits while minimizing the risk of overflows.
ClickHouse function reference
plus
Calculates the sum of two values. It can be used with numeric types, dates, and timestamps.
Syntax:
Attributes:
a
(numeric, date, or timestamp): The first value to be added.b
(numeric, date, or timestamp): The second value to be added.
Returns:
A numeric value, date, or timestamp, depending on the input types.
Alternatively, you can use the +
operator:
Example:
Result:
In this example:
tacos_with_extras
adds 2 extra tacos to each order.delivery_date
calculates the delivery date by adding 1 day to the order date.latest_pickup_time
adds 1800 seconds (30 minutes) to the original pickup time.
When adding an integer to a date, it increments the number of days. When adding an integer to a timestamp, it increments the number of seconds.
minus
Calculates the difference between two values. It can be used with numeric types, dates, and timestamps.
Syntax:
Attributes:
a
(numeric, date, or timestamp): The value to subtract from.b
(numeric, date, or timestamp): The value to subtract.
Returns:
A numeric value, date, or timestamp, depending on the input types. The result is always signed.
Alternatively, you can use the -
operator:
Example:
Result:
In this example:
tacos_without_extras
subtracts 2 tacos from each order.prep_date
calculates the preparation date by subtracting 1 day from the order date.earliest_pickup_time
subtracts 900 seconds (15 minutes) from the original pickup time.
When subtracting an integer from a date, it decrements the number of days. When subtracting an integer from a timestamp, it decrements the number of seconds.
multiply
Calculates the product of two numeric values.
Syntax:
Alternatively, you can use the *
operator:
Arguments:
a
(numeric): The first value to be multiplied.b
(numeric): The second value to be multiplied.
Returns:
A numeric value representing the product of a
and b
.
Example:
Result:
In this example, multiply
calculates the total price for each taco order by multiplying the number of tacos by the price per taco.
divide
Calculates the quotient of two numeric values. The result is always of type Float64
.
Syntax:
Alternatively, you can use the /
operator:
Arguments:
a
(numeric): The dividend.b
(numeric): The divisor.
Returns:
A Float64
value representing the quotient.
- Division by 0 returns
inf
,-inf
, ornan
. - For integer division, use the
intDiv
function instead.
Example:
Result:
In this example, divide
calculates the price per taco by dividing the total price by the number of tacos for each order.
intDiv
Performs integer division of two numeric values, rounding down to the nearest integer.
Syntax:
Arguments:
a
(numeric): The dividend.b
(numeric): The divisor.
Returns:
An integer result of the division, rounded down.
Example:
Result:
In this example, intDiv
calculates the price per taco by dividing the total price by the number of tacos, rounding down to the nearest integer.
Note that for order 2, the actual price per taco would be 6.67, but intDiv
rounds it down to 6.
intDivOrZero
Performs integer division of two values, returning zero when dividing by zero or when dividing the minimum negative number by -1.
Syntax:
Arguments:
a
(Integer): The dividend.b
(Integer): The divisor.
Returns:
The integer quotient of a
divided by b
, or zero if b
is zero or if dividing the minimum negative number by -1. [Integer]
Example:
Result:
In this example, intDivOrZero
calculates how many tacos each person can have based on the total number of tacos and available people. For order 3, where there are no available people (division by zero), the result is 0 instead of throwing an error.
isFinite
Checks whether a floating-point value is finite (not infinite and not NaN).
Syntax:
Arguments:
x
(Float32
orFloat64
): The value to check.
Returns:
1
if the value is finite, 0
otherwise. [ UInt8
]
Example:
Result:
In this example, isFinite
checks if each taco_price
is a valid finite number. The result 1
indicates a finite price, while 0
indicates an infinite or NaN price.
isInfinite
Checks if a floating-point value is infinite.
Syntax:
Arguments:
x
(Float32
orFloat64
): The value to check.
Returns:
1
if x
is infinite, 0
otherwise. Note that 0
is returned for NaN values. [ UInt8
]
Example:
Result:
In this example, isInfinite
checks if each taco_price
is infinite. The result 1
indicates an infinite price, while 0
indicates a finite price or NaN.
ifNotFinite
Checks whether a floating-point value is finite and returns either the original value or a fallback value.
Syntax:
Arguments:
x
(Float32
orFloat64
): Value to check for finiteness.y
(Float32
orFloat64
): Fallback value to return ifx
is not finite.
Returns:
x
ifx
is finitey
ifx
is not finite (infinity or NaN)
Example:
Result:
In this example, ifNotFinite
is used to handle potential division by zero when calculating the taco price. If the division results in infinity (due to division by zero), the function returns the fallback price of 9.99.
isNaN
Checks if a floating-point value is NaN (Not a Number).
Syntax:
Arguments:
x
(Float32
orFloat64
): The value to check.
Returns:
1
if the argument is NaN, 0
otherwise. [ UInt8
]
Example:
Result:
In this example, isNaN
checks if each taco_price
is NaN. The result 1
indicates that the price is NaN, while 0
indicates a valid numeric price.
modulo
Calculates the remainder of the division of two values.
Syntax:
Alternatively, you can use the % operator:
Arguments:
a
(numeric): The dividend.b
(numeric): The divisor.
Returns:
The remainder of a
divided by b
. The result type is:
- Integer if both inputs are integers
- Float64 if one of the inputs is a floating-point number
Uses truncated division for negative numbers (like C++). Throws an exception when dividing by zero or when dividing the minimum negative number by -1.
Example:
Result:
In this example, modulo
calculates how many tacos are left over when dividing the order quantity by 3 (e.g. for packaging in boxes of 3).
moduloOrZero
Calculates the remainder of the division of two values, returning zero if the divisor is zero.
Syntax:
Arguments:
a
(numeric): The dividend.b
(numeric): The divisor.
Returns:
The remainder of a
divided by b
, or zero if b
is zero. [Numeric]
Example:
Result:
In this example, moduloOrZero
calculates how many tacos are left over when dividing the number of tacos by 3 (e.g., for grouping into sets of 3). If num_tacos
were 0, the result would be 0 instead of throwing an error.
positiveModulo
Calculates the positive modulo of two values. It always returns a non-negative number.
Syntax:
Arguments:
a
(numeric): The dividend.b
(numeric): The divisor.
Returns:
The positive remainder of a
divided by b
. [Numeric]
Aliases:
positive_modulo(a, b)
pmod(a, b)
Example:
Result:
In this example, positiveModulo
calculates how many tacos are left over when dividing the order into groups of 3. The result is always non-negative, even for negative inputs.
This function is 4-5 times slower than the regular modulo
function.
negate
Negates a numeric value. The result is always signed.
Syntax:
Alternatively, you can use the unary minus operator:
Arguments:
a
(numeric): The numeric value to negate.
Returns:
The negated value of a
. [Numeric]
Example:
Result:
In this example, negate
and the unary minus operator are used to calculate the negative value of each taco price.
abs
Calculates the absolute value of a numeric input.
Syntax:
Arguments:
a
(numeric): The numeric value to calculate the absolute value of.
Returns:
The absolute value of the input. [Numeric]
Example:
Result:
In this example, abs
calculates the absolute value of taco_price
, removing the negative sign for orders 1 and 3 while leaving the positive value unchanged for order 2.
gcd
Calculates the greatest common divisor (GCD) of two numeric values.
Syntax:
Arguments:
a
(numeric): First numeric value.b
(numeric): Second numeric value.
Returns:
The greatest common divisor of a
and b
. [Numeric]
Example:
Result:
In this example, gcd
calculates the greatest common divisor between the number of tacos and sides for each order. A common divisor of 1 indicates the numbers are coprime.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
lcm
Calculates the least common multiple of two numeric values.
Syntax:
Arguments:
a
(numeric): First numeric value.b
(numeric): Second numeric value.
Returns:
The least common multiple of a
and b
. [Numeric]
Example:
Result:
In this example, lcm
calculates the least common multiple of the number of tacos and sides for each order. This could be useful for determining how many complete “sets” of the order can be made without leftover items.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
max2
Returns the larger of two numeric values.
Syntax:
Arguments:
a
(numeric): First numeric value.b
(numeric): Second numeric value.
Returns:
The larger of a
and b
. [Float64
]
Example:
Result:
In this example, max2
compares num_tacos
and extra_tacos
for each order, returning the larger value as max_tacos
.
min2
Returns the smaller of two numeric values.
Syntax:
Arguments:
a
(numeric): First numeric value.b
(numeric): Second numeric value.
Returns:
The smaller of the two input values. [Float64
]
Example:
Result:
In this example, min2
returns the smaller value between num_tacos
and extra_tacos
for each order. The result is stored in the min_tacos
column.
multiplyDecimal
Multiplies two decimal values with precise control over the result scale.
Syntax:
Arguments:
a
(Decimal): First decimal value.b
(Decimal): Second decimal value.result_scale
(Int/UInt): (Optional) Scale of the result.
Returns:
The product of a
and b
as a Decimal256
value with the specified scale.
Example:
Result:
In this example, multiplyDecimal
calculates the total price by multiplying the taco price by the quantity, with the result rounded to 2 decimal places.
This function is slower than regular multiplication but provides precise control over the result’s scale. Use regular multiplication when speed is more important than exact decimal precision.
divideDecimal
Divides two decimal values with precise control over the result scale.
Syntax:
Arguments:
a
(Decimal): The dividend (numerator).b
(Decimal): The divisor (denominator).result_scale
(Int/UInt): (Optional) The scale of the result.
Returns:
The result of division with the specified scale. [ Decimal256
]
Example:
Result:
In this example, divideDecimal
calculates the price per taco by dividing the total price by the number of tacos, with the result rounded to 2 decimal places.
This function is significantly slower than regular division. Use the standard divide
function if precise control over the result scale is not required or if faster computation is needed.
byteSwap
Reverses the bytes of an integer, changing its endianness.
Syntax:
Arguments:
a
(Integer): The integer value to reverse.
Returns:
The integer with reversed byte order. [Integer]
Example:
Result:
In this example, byteSwap
reverses the bytes of the taco_order_id
. The process can be understood as:
- Convert 3351772109 to hexadecimal: C7 C7 FB CD
- Reverse the bytes: CD FB C7 C7
- Convert back to decimal: 3455829959
This function can be useful for tasks like reversing IPv4 addresses stored as integers