Arithmetic functionsΒΆ
Use the following functions to perform arithmetic operations.
Arithmetic functions work for any two operands of type UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, or Float64.
Before performing the operation, both operands are cast to the result type. The result type is determined as follows:
- If both operands are up to 32 bits wide, the size of the result type is the size of the next bigger type following the bigger of the two operands (integer size promotion). For example,
UInt8 + UInt16 = UInt32orFloat32 * Float32 = Float64. - If one of the operands has 64 or more bits, the size of the result type is the same size as the bigger of the two operands. For example,
UInt32 + UInt128 = UInt128orFloat32 * Float64 = Float64. - If one of the operands is signed, the result type is also signed, otherwise it is signed. For example,
UInt32 * Int32 = Int64.
These rules make sure that the result type is the smallest type which can represent all possible results. While this introduces a risk of overflows around the value range boundary, it ensures that calculations are performed quickly using the maximum native integer width of 64 bit. This behavior also guarantees compatibility with many other databases which provide 64 bit integers (BIGINT) as the biggest integer type.
For example:
SELECT toTypeName(0), toTypeName(0 + 0), toTypeName(0 + 0 + 0), toTypeName(0 + 0 + 0 + 0)
ββtoTypeName(0)ββ¬βtoTypeName(plus(0, 0))ββ¬βtoTypeName(plus(plus(0, 0), 0))ββ¬βtoTypeName(plus(plus(plus(0, 0), 0), 0))ββ
β UInt8 β UInt16 β UInt32 β UInt64 β
βββββββββββββββββ΄βββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββ
Overflows are produced the same way as in C++.
plusΒΆ
Calculates the sum of two numeric values. This function can also add an integer to a Date or DateTime, incrementing days or seconds respectively.
SyntaxΒΆ
plus(a, b)
ArgumentsΒΆ
a: The first numeric value, or a Date/DateTime.b: The second numeric value, or an integer for Date/DateTime arithmetic.
ReturnsΒΆ
The sum of a and b. The return type depends on the input types, following type promotion rules.
Alias: a + b (operator)
ExampleΒΆ
SELECT plus(10, 5), 10 + 5, plus(toDate('2023-01-01'), 5)
Result:
ββplus(10, 5)ββ¬βplus(10, 5)ββ¬βplus(toDate('2023-01-01'), 5)ββ
β 15 β 15 β 2023-01-06 β
βββββββββββββββ΄ββββββββββββββ΄βββββββββββββββββββββββββββββββ
minusΒΆ
Calculates the difference between two numeric values. This function can also subtract an integer from a Date or DateTime, or subtract two Date/DateTime values to get a time difference. The result is always signed.
SyntaxΒΆ
minus(a, b)
ArgumentsΒΆ
a: The first numeric value, or a Date/DateTime.b: The second numeric value, an integer for Date/DateTime arithmetic, or another Date/DateTime.
ReturnsΒΆ
The difference between a and b. The return type depends on the input types, following type promotion rules.
Alias: a - b (operator)
ExampleΒΆ
SELECT minus(10, 5), 10 - 5, minus(toDate('2023-01-06'), 5), minus(toDateTime('2023-01-01 10:00:00'), toDateTime('2023-01-01 09:00:00'))
Result:
ββminus(10, 5)ββ¬βminus(10, 5)ββ¬βminus(toDate('2023-01-06'), 5)ββ¬βminus(toDateTime('2023-01-01 10:00:00'), toDateTime('2023-01-01 09:00:00'))ββ
β 5 β 5 β 2023-01-01 β 3600 β
ββββββββββββββββ΄βββββββββββββββ΄βββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
multiplyΒΆ
Calculates the product of two numeric values.
SyntaxΒΆ
multiply(a, b)
ArgumentsΒΆ
a: The first numeric value.b: The second numeric value.
ReturnsΒΆ
The product of a and b. The return type depends on the input types, following type promotion rules.
Alias: a * b (operator)
ExampleΒΆ
SELECT multiply(10, 5), 10 * 5, multiply(2.5, 4)
Result:
ββmultiply(10, 5)ββ¬βmultiply(10, 5)ββ¬βmultiply(2.5, 4)ββ
β 50 β 50 β 10.0 β
βββββββββββββββββββ΄ββββββββββββββββββ΄βββββββββββββββββββ
divideΒΆ
Calculates the quotient of two numeric values. The result type is always Float64. For integer division, use intDiv. Division by zero results in inf, -inf, or nan.
SyntaxΒΆ
divide(a, b)
ArgumentsΒΆ
a: The dividend.b: The divisor.
ReturnsΒΆ
The quotient of a divided by b. Float64.
Alias: a / b (operator)
ExampleΒΆ
SELECT divide(10, 4), 10 / 4, divide(10, 0)
Result:
ββdivide(10, 4)ββ¬βdivide(10, 4)ββ¬βdivide(10, 0)ββ
β 2.5 β 2.5 β inf β
βββββββββββββββββ΄ββββββββββββββββ΄ββββββββββββββββ
intDivΒΆ
Performs integer division, returning the quotient rounded down to the nearest whole number. The result type has the same width as the dividend. This function throws an exception if the divisor is zero, the quotient exceeds the dividend's range, or if dividing the minimum negative number by -1.
SyntaxΒΆ
intDiv(a, b)
ArgumentsΒΆ
a: The dividend.b: The divisor.
ReturnsΒΆ
The integer quotient of a divided by b. The return type matches the width of a.
ExampleΒΆ
Query:
SELECT
intDiv(toFloat64(1), 0.001) AS res,
toTypeName(res)
βββresββ¬βtoTypeName(intDiv(toFloat64(1), 0.001))ββ
β 1000 β Int64 β
ββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ
SELECT
intDiv(1, 0.001) AS res,
toTypeName(res)
Received exception from server (version 23.2.1):
Code: 153. DB::Exception: Received from localhost:9000. DB::Exception: Cannot perform integer division, because it will produce infinite or too large number: While processing intDiv(1, 0.001) AS res, toTypeName(res). (ILLEGAL_DIVISION)
intDivOrZeroΒΆ
Performs integer division similar to intDiv, but returns 0 instead of throwing an exception if the divisor is zero or if dividing the minimum negative number by -1.
SyntaxΒΆ
intDivOrZero(a, b)
ArgumentsΒΆ
a: The dividend.b: The divisor.
ReturnsΒΆ
The integer quotient of a divided by b, or 0 in case of division by zero or overflow. The return type matches the width of a.
ExampleΒΆ
SELECT intDivOrZero(10, 3), intDivOrZero(10, 0)
Result:
ββintDivOrZero(10, 3)ββ¬βintDivOrZero(10, 0)ββ
β 3 β 0 β
βββββββββββββββββββββββ΄ββββββββββββββββββββββ
isFiniteΒΆ
Checks if a floating-point number is finite (not infinite and not NaN).
SyntaxΒΆ
isFinite(x)
ArgumentsΒΆ
x: The floating-point number to check.Float32orFloat64.
ReturnsΒΆ
1 if x is finite, 0 otherwise. UInt8.
ExampleΒΆ
SELECT isFinite(1.0/3.0), isFinite(1.0/0.0), isFinite(0.0/0.0)
Result:
ββisFinite(1 / 3)ββ¬βisFinite(1 / 0)ββ¬βisFinite(0 / 0)ββ
β 1 β 0 β 0 β
βββββββββββββββββββ΄ββββββββββββββββββ΄ββββββββββββββββββ
isInfiniteΒΆ
Checks if a floating-point number is infinite.
SyntaxΒΆ
isInfinite(x)
ArgumentsΒΆ
x: The floating-point number to check.Float32orFloat64.
ReturnsΒΆ
1 if x is infinite, 0 otherwise. UInt8. Returns 0 for NaN values.
ExampleΒΆ
SELECT isInfinite(1.0/0.0), isInfinite(-1.0/0.0), isInfinite(0.0/0.0), isInfinite(1.0)
Result:
ββisInfinite(1 / 0)ββ¬βisInfinite(-1 / 0)ββ¬βisInfinite(0 / 0)ββ¬βisInfinite(1)ββ
β 1 β 1 β 0 β 0 β
βββββββββββββββββββββ΄βββββββββββββββββββββ΄ββββββββββββββββββββ΄ββββββββββββββββ
ifNotFiniteΒΆ
Returns the original value if it is finite; otherwise, returns a specified fallback value.
SyntaxΒΆ
ifNotFinite(x, y)
ArgumentsΒΆ
x: Value to check for finiteness.Float32orFloat64.y: Fallback value to return ifxis not finite.Float32orFloat64.
ReturnsΒΆ
x if x is finite, otherwise y. Float32 or Float64.
ExampleΒΆ
Query:
SELECT 1/0 as infimum, ifNotFinite(infimum,42)
Result:
ββinfimumββ¬βifNotFinite(divide(1, 0), 42)ββ β inf β 42 β βββββββββββ΄ββββββββββββββββββββββββββββββββ
You can get similar result by using the ternary operator: isFinite(x) ? x : y.
isNaNΒΆ
Checks if a floating-point number is "Not a Number" (NaN).
SyntaxΒΆ
isNaN(x)
ArgumentsΒΆ
x: The floating-point number to check.Float32orFloat64.
ReturnsΒΆ
1 if x is NaN, 0 otherwise. UInt8.
ExampleΒΆ
SELECT isNaN(0.0/0.0), isNaN(1.0/0.0), isNaN(1.0)
Result:
ββisNaN(0 / 0)ββ¬βisNaN(1 / 0)ββ¬βisNaN(1)ββ
β 1 β 0 β 0 β
ββββββββββββββββ΄βββββββββββββββ΄βββββββββββ
moduloΒΆ
Calculates the remainder of the division of a by b. The behavior for negative numbers follows C++ semantics (truncated division). An exception is thrown if the divisor is zero or if dividing the minimum negative number by -1.
SyntaxΒΆ
modulo(a, b)
ArgumentsΒΆ
a: The dividend.b: The divisor.
ReturnsΒΆ
The remainder of a divided by b. The return type is an integer if both inputs are integers, otherwise Float64.
Alias: a % b (operator)
ExampleΒΆ
SELECT modulo(10, 3), modulo(-10, 3), modulo(10, -3), modulo(-10, -3)
Result:
ββmodulo(10, 3)ββ¬βmodulo(-10, 3)ββ¬βmodulo(10, -3)ββ¬βmodulo(-10, -3)ββ
β 1 β -1 β 1 β -1 β
βββββββββββββββββ΄βββββββββββββββββ΄βββββββββββββββββ΄ββββββββββββββββββ
moduloOrZeroΒΆ
Calculates the remainder of the division of a by b, similar to modulo. If the divisor b is zero, it returns 0 instead of throwing an exception.
SyntaxΒΆ
moduloOrZero(a, b)
ArgumentsΒΆ
a: The dividend.b: The divisor.
ReturnsΒΆ
The remainder of a divided by b, or 0 if b is zero. The return type is an integer if both inputs are integers, otherwise Float64.
ExampleΒΆ
SELECT moduloOrZero(10, 3), moduloOrZero(10, 0)
Result:
ββmoduloOrZero(10, 3)ββ¬βmoduloOrZero(10, 0)ββ
β 1 β 0 β
βββββββββββββββββββββββ΄ββββββββββββββββββββββ
positiveModuloΒΆ
Calculates the remainder of the division of a by b, always returning a non-negative result. This function is generally slower than modulo.
SyntaxΒΆ
positiveModulo(a, b)
ArgumentsΒΆ
a: The dividend.b: The divisor.
ReturnsΒΆ
The non-negative remainder of a divided by b. The return type is an integer if both inputs are integers, otherwise Float64.
Alias:
positive_modulo(a, b)pmod(a, b)
ExampleΒΆ
Query:
SELECT positiveModulo(-1, 10)
Result:
ββpositiveModulo(-1, 10)ββ
β 9 β
ββββββββββββββββββββββββββ
negateΒΆ
Returns the negative of a numeric value. The result is always signed.
SyntaxΒΆ
negate(a)
ArgumentsΒΆ
a: The numeric value to negate.
ReturnsΒΆ
The negated value of a. The return type is a signed version of the input type, or the same type if already signed.
Alias: -a
ExampleΒΆ
SELECT negate(5), negate(-5), -10
Result:
ββnegate(5)ββ¬βnegate(-5)ββ¬βnegate(10)ββ
β -5 β 5 β -10 β
βββββββββββββ΄βββββββββββββ΄βββββββββββββ
absΒΆ
Calculates the absolute value of a numeric input. For unsigned types, it has no effect. For signed types, it returns an unsigned number.
SyntaxΒΆ
abs(a)
ArgumentsΒΆ
a: The numeric value.
ReturnsΒΆ
The absolute value of a. The return type is an unsigned version of the input type if a is signed, otherwise the same type.
ExampleΒΆ
SELECT abs(5), abs(-5), abs(0)
Result:
ββabs(5)ββ¬βabs(-5)ββ¬βabs(0)ββ
β 5 β 5 β 0 β
ββββββββββ΄ββββββββββ΄βββββββββ
gcdΒΆ
Returns the greatest common divisor (GCD) of two integer values. An exception is thrown if the divisor is zero or if dividing the minimum negative number by -1.
SyntaxΒΆ
gcd(a, b)
ArgumentsΒΆ
a: The first integer.b: The second integer.
ReturnsΒΆ
The greatest common divisor of a and b. Int64.
ExampleΒΆ
SELECT gcd(12, 18), gcd(7, 5)
Result:
ββgcd(12, 18)ββ¬βgcd(7, 5)ββ
β 6 β 1 β
βββββββββββββββ΄ββββββββββββ
lcmΒΆ
Returns the least common multiple (LCM) of two integer values. An exception is thrown if the divisor is zero or if dividing the minimum negative number by -1.
SyntaxΒΆ
lcm(a, b)
ArgumentsΒΆ
a: The first integer.b: The second integer.
ReturnsΒΆ
The least common multiple of a and b. Int64.
ExampleΒΆ
SELECT lcm(12, 18), lcm(7, 5)
Result:
ββlcm(12, 18)ββ¬βlcm(7, 5)ββ
β 36 β 35 β
βββββββββββββββ΄ββββββββββββ
max2ΒΆ
Compares two values and returns the larger one.
SyntaxΒΆ
max2(a, b)
ArgumentsΒΆ
a: The first value.b: The second value.
ReturnsΒΆ
The greater of a and b. Float64.
ExampleΒΆ
Query:
SELECT max2(-1, 2)
Result:
ββmax2(-1, 2)ββ
β 2 β
βββββββββββββββ
min2ΒΆ
Compares two values and returns the smaller one.
SyntaxΒΆ
min2(a, b)
ArgumentsΒΆ
a: The first value.b: The second value.
ReturnsΒΆ
The lesser of a and b. Float64.
ExampleΒΆ
Query:
SELECT min2(-1, 2)
Result:
ββmin2(-1, 2)ββ
β -1 β
βββββββββββββββ
multiplyDecimalΒΆ
Multiplies two Decimal values, a and b. The result is a Decimal256. You can optionally specify the result_scale to control the precision of the output. If result_scale is not provided, the maximum scale of the input values is used. This function is slower than the standard multiply function but offers precise control over decimal arithmetic.
SyntaxΒΆ
multiplyDecimal(a, b[, result_scale])
ArgumentsΒΆ
a: The first value.Decimal.b: The second value.Decimal.result_scale: (Optional) The desired scale (number of decimal places) for the result.Int/UInt.
ReturnsΒΆ
The product of a and b with the specified or inferred scale. Decimal256.
ExampleΒΆ
ββmultiplyDecimal(toDecimal256(-12, 0), toDecimal32(-2.1, 1), 1)ββ
β 25.2 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Differences compared to regular multiplicationΒΆ
SELECT toDecimal64(-12.647, 3) * toDecimal32(2.1239, 4)
SELECT toDecimal64(-12.647, 3) as a, toDecimal32(2.1239, 4) as b, multiplyDecimal(a, b)
Result:
ββmultiply(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))ββ
β -26.8609633 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββaββ¬ββββββbββ¬βmultiplyDecimal(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))ββ
β -12.647 β 2.1239 β -26.8609 β
βββββββββββ΄βββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
SELECT
toDecimal64(-12.647987876, 9) AS a,
toDecimal64(123.967645643, 9) AS b,
multiplyDecimal(a, b)
SELECT
toDecimal64(-12.647987876, 9) AS a,
toDecimal64(123.967645643, 9) AS b,
a * b
Result:
ββββββββββββββaββ¬βββββββββββββbββ¬βmultiplyDecimal(toDecimal64(-12.647987876, 9), toDecimal64(123.967645643, 9))ββ
β -12.647987876 β 123.967645643 β -1567.941279108 β
βββββββββββββββββ΄ββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Received exception from server (version 22.11.1):
Code: 407. DB::Exception: Received from localhost:9000. DB::Exception: Decimal math overflow: While processing toDecimal64(-12.647987876, 9) AS a, toDecimal64(123.967645643, 9) AS b, a * b. (DECIMAL_OVERFLOW)
divideDecimalΒΆ
Divides two Decimal values, a by b. The result is a Decimal256. You can optionally specify the result_scale to control the precision of the output. If result_scale is not provided, the maximum scale of the input values is used. This function is slower than the standard divide function but offers precise control over decimal arithmetic.
SyntaxΒΆ
divideDecimal(a, b[, result_scale])
ArgumentsΒΆ
a: The dividend.Decimal.b: The divisor.Decimal.result_scale: (Optional) The desired scale (number of decimal places) for the result.Int/UInt.
ReturnsΒΆ
The quotient of a divided by b with the specified or inferred scale. Decimal256.
ExampleΒΆ
ββdivideDecimal(toDecimal256(-12, 0), toDecimal32(2.1, 1), 10)ββ
β -5.7142857142 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Differences compared to regular divisionΒΆ
SELECT toDecimal64(-12, 1) / toDecimal32(2.1, 1)
SELECT toDecimal64(-12, 1) as a, toDecimal32(2.1, 1) as b, divideDecimal(a, b, 1), divideDecimal(a, b, 5)
Result:
ββdivide(toDecimal64(-12, 1), toDecimal32(2.1, 1))ββ
β -5.7 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββaββ¬βββbββ¬βdivideDecimal(toDecimal64(-12, 1), toDecimal32(2.1, 1), 1)ββ¬βdivideDecimal(toDecimal64(-12, 1), toDecimal32(2.1, 1), 5)ββ
β -12 β 2.1 β -5.7 β -5.71428 β
βββββββ΄ββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
SELECT toDecimal64(-12, 0) / toDecimal32(2.1, 1)
SELECT toDecimal64(-12, 0) as a, toDecimal32(2.1, 1) as b, divideDecimal(a, b, 1), divideDecimal(a, b, 5)
Result:
DB::Exception: Decimal result's scale is less than argument's one: While processing toDecimal64(-12, 0) / toDecimal32(2.1, 1). (ARGUMENT_OUT_OF_BOUND)
ββββaββ¬βββbββ¬βdivideDecimal(toDecimal64(-12, 0), toDecimal32(2.1, 1), 1)ββ¬βdivideDecimal(toDecimal64(-12, 0), toDecimal32(2.1, 1), 5)ββ
β -12 β 2.1 β -5.7 β -5.71428 β
βββββββ΄ββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
byteSwapΒΆ
Reverses the byte order (endianness) of an integer value.
SyntaxΒΆ
byteSwap(a)
ArgumentsΒΆ
a: The integer value whose bytes are to be swapped.
ReturnsΒΆ
The integer with its bytes reversed. The return type is the same as the input type.
ExampleΒΆ
byteSwap(3351772109)
Result:
ββbyteSwap(3351772109)ββ
β 3455829959 β
ββββββββββββββββββββββββ
Follow these steps to reproduce the previous example:
- Convert the base-10 integer to its equivalent hexadecimal format in big-endian format, for example
3351772109 -> C7 C7 FB CD (4 bytes). - Reverse the bytes, for example
C7 C7 FB CD -> CD FB C7 C7. - Convert the result back to an integer assuming big-endian, for example
CD FB C7 C7 -> 3455829959.
A use case of this function is reversing IPv4s:
ββtoIPv4(byteSwap(toUInt32(toIPv4('205.251.199.199'))))ββ
β 199.199.251.205 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ