You need to enable JavaScript to run this app.
ByteHouse Cloud Data Warehouse Version

ByteHouse Cloud Data Warehouse Version

Copy page
Download PDF
Function reference
Arithmetic functions
Copy page
Download PDF
Arithmetic functions

For all arithmetic functions, the result Type is the Min Type suitable for the result (if such a Type exists). The Min Type is determined according to the number of Digits of the Numerical value, whether it is signed, and whether it is a floating-point Type. If there are not enough Digits, then the highest Digits Type is adopted.
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                                   │ 
└───────────────┴────────────────────────┴─────────────────────────────────┴──────────────────────────────────────────┘

Arithmetic functions apply to any Type in UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, or Float64.
The method of generating overflow is the same as in C++.

plus

Calculate the Sum of Numerical values. You can also add Date or DateTime and Integer. In the case of Date, adding an Integer means adding the corresponding number of days. For DateTime, this means adding the corresponding number of seconds.
Syntax

select plus(a, b) -- a + b operator

Param

  • a – Number.
  • b – Number.

Return value

  • Returns the Sum between Numerical values.

Example

select plus(1,2);

Result

┌─plus(1, 2)─┐ 
│ 3          │ 
└────────────┘

minus

Calculate the Poor between Numerical values. The result is always signed. You can also subtract Integer from Date or DateTime. See 'plus' above.
Syntax

minus(a, b) -- a-b operator

Param

  • a – Number.
  • b – Number.

Return value

  • Returns the Poor between Numerical values.

Example

SELECT minus(10, 3);

Result

┌─minus(10, 3)─┐ 
│ 7            │ 
└──────────────┘

multiply

Calculate the product of Numerical values.
Syntax

multiply(a, b) -- a * b operator

Param

  • a – Number.
  • b – Number.

Return value

  • Returns the product of Numerical values.

Example

SELECT multiply(3,12);

Result

┌─multiply(3, 12)─┐ 
│ 36              │ 
└─────────────────┘

divide

Calculate the quotient of Numerical values. The result Type is always a floating-point Type. This is not integer division. For integer division, use the 'intDiv' function. When dividing by Zero, you get 'inf', '-inf', or 'nan'.
Syntax

divide(a, b) -- a / b operator

Param

  • a – Number.
  • b – Number.

Return value

  • Returns floating-point Type.

Example

SELECT divide(50, 2);

Result

┌─divide(50, 2)─┐ 
│ 2.5e+01       │ 
└───────────────┘

intDiv

Calculate the quotient of Numerical values, rounding down (by Absolute value). An Abnormal is thrown when dividing by Zero or dividing the minimum negative Number by -1.
Syntax

intDiv(a, b)

Param

  • a – Number.
  • b – Number.

Return value

  • Returns the quotient of Numerical values.

Example

SELECT intDiv(10, 2);

Result

┌─intDiv(10, 2)─┐ 
│ 5             │ 
└───────────────┘

intDivOrZero

The difference from 'intDiv' is that it returns Zero when dividing by Zero or dividing the minimum negative Number by -1.
Syntax

intDivOrZero(a, b)

Param

  • a – Number.
  • b – Number.

Return value

  • Returns the quotient of Numerical values.

Example

SELECT intDivOrZero(10, -2);

Result

┌─intDivOrZero(10, -2)─┐ 
│ -5                   │ 
└──────────────────────┘

modulo

Calculate the remainder after division. If both inputs are Integer, the result Type is Integer. If one of the inputs is a Floating-point number, the result is a Floating-point number. The rest is the same as in C++. Truncating division is used for negative numbers. An exception is thrown when dividing by zero or dividing the minimum negative number by -1.
Syntax

modulo(a, b) --a % b operator

Parameters

  • a – Number.
  • b – Number.

Return value

  • Returns the remainder after division.

Example

SELECT modulo(10, 3);

Result

┌─modulo(10, 3)─┐ 
│ 1             │ 
└───────────────┘

moduloOrZero

The difference from modulo is that when divided by zero, the result returns 0.
Syntax

moduloOrZero(a, b) --a % b operator

Parameters

  • a – Number.
  • b – Number.

Return value

  • Returns the remainder after division.

Example

SELECT moduloOrZero(10, 0);

Result

┌─moduloOrZero(10, 0)─┐ 
│ 0             │ 
└───────────────┘

negate

Negates the value by changing its sign bit; the result is always signed.
Syntax

negate(a) -- -a operator

Parameters

  • x – Number.

Return value

  • Returns the opposite of this number.

Example

SELECT negate(20);

Result

┌─negate(20)─┐ 
│ -20        │ 
└────────────┘

abs

Calculates the absolute value of the numerical value (a). That is, if a < 0, it returns -a. For unsigned types, it does nothing. For signed integer types, it returns an unsigned number.
Syntax

abs(x)

Parameters

  • x – Number.

Return value

  • Returns the absolute value of this number.

Example

SELECT abs(-2);

Result

┌─abs(-2)─┐ 
│ 2       │ 
└─────────┘

gcd

Returns the greatest common divisor of the numerical value. An exception is thrown when dividing by zero or dividing the minimum negative number by -1.
Syntax

gcd(a, b)

Parameters

  • a – Number.
  • b – Number.

Return value

  • Returns the greatest common divisor of the numerical value.

Example

SELECT gcd(27,18);

Result

┌─gcd(27, 18)─┐ 
│ 9           │ 
└─────────────┘

lcm

Returns the least common multiple of the numerical value. An exception is thrown when dividing by zero or dividing the minimum negative number by -1.
Syntax

lcm(a, b)

Parameters

  • a – Number.
  • b – Number.

Return value

  • Returns the least common multiple of the numerical value.

Example

SELECT lcm(27,18);

Result

┌─lcm(27, 18)─┐ 
│ 54          │ 
└─────────────┘

max2

Compares two values and returns the maximum value. The return value is converted to Float64.
Syntax

max2(value1, value2)

Parameters

  • value1 — The first value, Type: Int/UInt or Float.
  • value2 — The second value, Type: Int/UInt or Float.

Return value

  • The maximum of the two values.

Type: Float.
Example
Query statement:

SELECT max2(-1, 2);

Result:

┌─max2(-1, 2)─┐ 
│           2 │ 
└─────────────┘

min2

Compares two values and returns the minimum value. The return value is converted to Float64.
Syntax

min2(value1, value2)

Parameters

  • value1 — The first value, Type: Int/UInt or Float.
  • value2 — The second value, Type: Int/UInt or Float.

Return value

  • The minimum of the two values.

Type: Float.
Example
Query statement:

SELECT min2(-1, 2);

Result:

┌─min2(-1, 2)─┐ 
│          -1 │ 
└─────────────┘
Last updated: 2026.03.31 16:13:52