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 = UInt32 or Float32 * 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 = UInt128 or Float32 * 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. Float32 or Float64.

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. Float32 or Float64.

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. Float32 or Float64.
  • y: Fallback value to return if x is not finite. Float32 or Float64.

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. Float32 or Float64.

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:

  1. Convert the base-10 integer to its equivalent hexadecimal format in big-endian format, for example 3351772109 -> C7 C7 FB CD (4 bytes).
  2. Reverse the bytes, for example C7 C7 FB CD -> CD FB C7 C7.
  3. 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                                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Updated