ClickHouse/docs/en/sql-reference/functions/arithmetic-functions.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

477 lines
15 KiB
Markdown
Raw Normal View History

2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/arithmetic-functions
2023-04-19 17:05:55 +00:00
sidebar_position: 5
sidebar_label: Arithmetic
2020-04-03 13:23:32 +00:00
---
2022-06-02 10:55:18 +00:00
# Arithmetic Functions
2023-07-20 14:17:33 +00:00
Arithmetic functions work for any two operands of type `UInt8`, `UInt16`, `UInt32`, `UInt64`, `Int8`, `Int16`, `Int32`, `Int64`, `Float32`, or `Float64`.
2023-04-19 17:49:40 +00:00
2023-07-20 14:17:33 +00:00
Before performing the operation, both operands are casted to the result type. The result type is determined as follows (unless specified
differently in the function documentation below):
- If both operands are up to 32 bits wide, the size of the result type will be the size of the next bigger type following the bigger of the
2023-07-20 14:17:33 +00:00
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 will be 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 will also be signed, otherwise it will be signed. For example, `UInt32 * Int32 = Int64`.
These rules make sure that the result type will be 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.
Example:
2020-03-20 10:10:48 +00:00
``` sql
SELECT toTypeName(0), toTypeName(0 + 0), toTypeName(0 + 0 + 0), toTypeName(0 + 0 + 0 + 0)
```
2020-03-20 10:10:48 +00:00
``` text
┌─toTypeName(0)─┬─toTypeName(plus(0, 0))─┬─toTypeName(plus(plus(0, 0), 0))─┬─toTypeName(plus(plus(plus(0, 0), 0), 0))─┐
│ UInt8 │ UInt16 │ UInt32 │ UInt64 │
└───────────────┴────────────────────────┴─────────────────────────────────┴──────────────────────────────────────────┘
```
2023-04-19 17:49:40 +00:00
Overflows are produced the same way as in C++.
## plus
2023-04-19 18:09:20 +00:00
Calculates the sum of two values `a` and `b`.
2023-04-19 17:49:40 +00:00
**Syntax**
```sql
plus(a, b)
```
2023-04-19 18:11:50 +00:00
It is possible to add an integer and a date or date with time. The former operation increments the number of days in the date, the latter operation increments the number of seconds in the date with time.
2023-04-19 17:49:40 +00:00
Alias: `a + b` (operator)
2023-04-19 17:49:40 +00:00
## minus
2023-04-19 18:09:20 +00:00
Calculates the difference of two values `a` and `b`. The result is always signed.
Similar to `plus`, it is possible to subtract an integer from a date or date with time.
2023-04-19 17:49:40 +00:00
**Syntax**
2023-04-19 17:49:40 +00:00
```sql
minus(a, b)
```
2023-04-19 17:49:40 +00:00
Alias: `a - b` (operator)
2023-04-19 17:49:40 +00:00
## multiply
2023-04-19 18:09:20 +00:00
Calculates the product of two values `a` and `b`.
2023-04-19 17:49:40 +00:00
**Syntax**
2023-04-19 17:49:40 +00:00
```sql
multiply(a, b)
```
2023-10-01 10:53:41 +00:00
Alias: `a * b` (operator)
2023-04-19 17:49:40 +00:00
## divide
2023-07-20 14:17:33 +00:00
Calculates the quotient of two values `a` and `b`. The result type is always [Float64](../../sql-reference/data-types/float.md). Integer division is provided by the `intDiv` function.
2023-04-19 18:09:20 +00:00
Division by 0 returns `inf`, `-inf`, or `nan`.
2023-04-19 17:49:40 +00:00
**Syntax**
```sql
divide(a, b)
```
Alias: `a / b` (operator)
## intDiv
2023-04-19 18:09:20 +00:00
Performs an integer division of two values `a` by `b`, i.e. computes the quotient rounded down to the next smallest integer.
2023-07-20 14:17:33 +00:00
The result has the same width as the dividend (the first parameter).
2023-04-19 18:09:20 +00:00
An exception is thrown when dividing by zero, when the quotient does not fit in the range of the dividend, or when dividing a minimal negative number by minus one.
2023-04-19 17:49:40 +00:00
**Syntax**
```sql
intDiv(a, b)
```
**Example**
Query:
```sql
SELECT
intDiv(toFloat64(1), 0.001) AS res,
toTypeName(res)
```
2023-04-19 17:49:40 +00:00
```response
┌──res─┬─toTypeName(intDiv(toFloat64(1), 0.001))─┐
│ 1000 │ Int64 │
└──────┴─────────────────────────────────────────┘
```
```sql
SELECT
intDiv(1, 0.001) AS res,
toTypeName(res)
```
2023-04-19 17:49:40 +00:00
```response
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)
```
2023-04-19 17:49:40 +00:00
## intDivOrZero
2023-04-19 18:09:20 +00:00
Same as `intDiv` but returns zero when dividing by zero or when dividing a minimal negative number by minus one.
2023-04-19 17:49:40 +00:00
**Syntax**
```sql
intDivOrZero(a, b)
```
## modulo
2023-04-19 18:09:20 +00:00
Calculates the remainder of the division of two values `a` by `b`.
2023-07-20 14:17:33 +00:00
The result type is an integer if both inputs are integers. If one of the inputs is a floating-point number, the result type is [Float64](../../sql-reference/data-types/float.md).
2023-04-19 17:49:40 +00:00
The remainder is computed like in C++. Truncated division is used for negative numbers.
2023-04-19 17:49:40 +00:00
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
2023-04-19 18:09:20 +00:00
**Syntax**
```sql
modulo(a, b)
```
2023-04-19 17:49:40 +00:00
Alias: `a % b` (operator)
## moduloOrZero
2023-04-19 18:09:20 +00:00
Like [modulo](#modulo) but returns zero when the divisor is zero.
2023-04-19 17:49:40 +00:00
**Syntax**
```sql
moduloOrZero(a, b)
```
## positiveModulo(a, b)
2020-02-25 10:15:24 +00:00
2023-04-19 18:09:20 +00:00
Like [modulo](#modulo) but always returns a non-negative number.
This function is 4-5 times slower than `modulo`.
2023-04-19 17:49:40 +00:00
**Syntax**
```sql
positiveModulo(a, b)
```
2020-02-25 10:15:24 +00:00
2023-04-19 17:49:40 +00:00
Alias:
- `positive_modulo(a, b)`
- `pmod(a, b)`
2023-01-05 16:10:20 +00:00
**Example**
Query:
```sql
SELECT positiveModulo(-1, 10)
```
Result:
2023-04-19 17:49:40 +00:00
```result
2023-01-05 16:10:20 +00:00
┌─positiveModulo(-1, 10)─┐
│ 9 │
└────────────────────────┘
```
2023-04-19 17:49:40 +00:00
## negate
2023-04-19 18:09:20 +00:00
Negates a value `a`. The result is always signed.
2023-04-19 17:49:40 +00:00
**Syntax**
2023-04-19 17:49:40 +00:00
```sql
negate(a)
```
2023-04-19 17:49:40 +00:00
Alias: `-a`
2023-04-19 17:49:40 +00:00
## abs
2023-04-19 18:09:20 +00:00
Calculates the absolute value of `a`. Has no effect if `a` is of an unsigned type. If `a` is of a signed type, it returns an unsigned number.
2023-04-19 17:49:40 +00:00
**Syntax**
2023-04-19 17:49:40 +00:00
```sql
abs(a)
```
2023-04-19 17:49:40 +00:00
## gcd
2021-10-19 22:42:05 +00:00
2023-04-19 18:09:20 +00:00
Returns the greatest common divisor of two values `a` and `b`.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
2021-10-19 22:42:05 +00:00
**Syntax**
```sql
2023-04-19 17:49:40 +00:00
gcd(a, b)
2021-10-19 22:42:05 +00:00
```
2023-04-19 17:49:40 +00:00
## lcm(a, b)
2021-10-19 22:42:05 +00:00
2023-04-19 18:09:20 +00:00
Returns the least common multiple of two values `a` and `b`.
2021-10-19 22:42:05 +00:00
2023-04-19 17:49:40 +00:00
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
**Syntax**
```sql
lcm(a, b)
```
## max2
2023-04-19 18:09:20 +00:00
Returns the bigger of two values `a` and `b`. The returned value is of type [Float64](../../sql-reference/data-types/float.md).
2023-04-19 17:49:40 +00:00
**Syntax**
```sql
max2(a, b)
```
2021-10-19 22:42:05 +00:00
**Example**
Query:
```sql
SELECT max2(-1, 2);
```
Result:
2023-04-19 17:49:40 +00:00
```result
2021-10-19 22:42:05 +00:00
┌─max2(-1, 2)─┐
│ 2 │
└─────────────┘
```
2022-06-02 10:55:18 +00:00
## min2
2021-10-19 22:42:05 +00:00
2023-04-19 18:09:20 +00:00
Returns the smaller of two values `a` and `b`. The returned value is of type [Float64](../../sql-reference/data-types/float.md).
2021-10-19 22:42:05 +00:00
**Syntax**
```sql
2023-04-19 17:49:40 +00:00
min2(a, b)
2021-10-19 22:42:05 +00:00
```
**Example**
Query:
```sql
2021-10-22 14:56:10 +00:00
SELECT min2(-1, 2);
2021-10-19 22:42:05 +00:00
```
Result:
2023-04-19 17:49:40 +00:00
```result
2021-10-22 14:56:10 +00:00
┌─min2(-1, 2)─┐
│ -1 │
2021-10-19 22:42:05 +00:00
└─────────────┘
```
2022-10-19 10:55:24 +00:00
2023-04-19 17:49:40 +00:00
## multiplyDecimal
2022-11-01 13:40:26 +00:00
2023-04-19 18:11:50 +00:00
Multiplies two decimals `a` and `b`. The result value will be of type [Decimal256](../../sql-reference/data-types/decimal.md).
2023-04-19 18:09:20 +00:00
The scale of the result can be explicitly specified by `result_scale`. If `result_scale` is not specified, it is assumed to be the maximum scale of the input values.
This function work significantly slower than usual `multiply`. In case no control over the result precision is needed and/or fast computation is desired, consider using `multiply`.
2022-10-19 10:55:24 +00:00
**Syntax**
```sql
multiplyDecimal(a, b[, result_scale])
2022-11-23 22:23:59 +00:00
```
**Arguments**
- `a` — First value: [Decimal](../../sql-reference/data-types/decimal.md).
- `b` — Second value: [Decimal](../../sql-reference/data-types/decimal.md).
- `result_scale` — Scale of result: [Int/UInt](../../sql-reference/data-types/int-uint.md).
2022-11-23 22:23:59 +00:00
**Returned value**
2024-05-23 13:48:20 +00:00
- The result of multiplication with given scale. [Decimal256](../../sql-reference/data-types/decimal.md).
2022-11-23 22:23:59 +00:00
**Example**
2023-04-19 17:49:40 +00:00
```result
2022-11-23 22:23:59 +00:00
┌─multiplyDecimal(toDecimal256(-12, 0), toDecimal32(-2.1, 1), 1)─┐
│ 25.2 │
└────────────────────────────────────────────────────────────────┘
```
2023-04-19 17:49:40 +00:00
**Differences compared to regular multiplication:**
2022-11-23 22:23:59 +00:00
```sql
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);
```
2023-04-19 18:09:20 +00:00
Result:
2023-04-19 17:49:40 +00:00
```result
2022-11-23 22:23:59 +00:00
┌─multiply(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐
│ -26.8609633 │
└───────────────────────────────────────────────────────────┘
2023-10-01 10:53:41 +00:00
┌───────a─┬──────b─┬─multiplyDecimal(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐
│ -12.647 │ 2.1239 │ -26.8609 │
└─────────┴────────┴──────────────────────────────────────────────────────────────────┘
2022-11-23 22:23:59 +00:00
```
```sql
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;
```
2023-04-19 18:09:20 +00:00
Result:
2023-04-19 17:49:40 +00:00
```result
2022-11-23 22:23:59 +00:00
┌─────────────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)
```
2023-04-19 17:49:40 +00:00
## divideDecimal
2022-11-23 22:23:59 +00:00
2022-10-19 10:55:24 +00:00
2023-04-19 18:11:50 +00:00
Divides two decimals `a` and `b`. The result value will be of type [Decimal256](../../sql-reference/data-types/decimal.md).
2023-04-19 17:49:40 +00:00
The scale of the result can be explicitly specified by `result_scale`. If `result_scale` is not specified, it is assumed to be the maximum scale of the input values.
This function work significantly slower than usual `divide`. In case no control over the result precision is needed and/or fast computation is desired, consider using `divide`.
2023-04-19 18:09:20 +00:00
**Syntax**
```sql
divideDecimal(a, b[, result_scale])
```
2022-10-19 10:55:24 +00:00
**Arguments**
- `a` — First value: [Decimal](../../sql-reference/data-types/decimal.md).
- `b` — Second value: [Decimal](../../sql-reference/data-types/decimal.md).
- `result_scale` — Scale of result: [Int/UInt](../../sql-reference/data-types/int-uint.md).
2022-10-19 10:55:24 +00:00
**Returned value**
2024-05-23 13:48:20 +00:00
- The result of division with given scale. [Decimal256](../../sql-reference/data-types/decimal.md).
2022-10-19 10:55:24 +00:00
**Example**
2023-04-19 17:49:40 +00:00
```result
2022-10-19 10:55:24 +00:00
┌─divideDecimal(toDecimal256(-12, 0), toDecimal32(2.1, 1), 10)─┐
│ -5.7142857142 │
└──────────────────────────────────────────────────────────────┘
```
2022-11-22 14:30:44 +00:00
2023-04-19 17:49:40 +00:00
**Differences compared to regular division:**
2022-11-22 14:30:44 +00:00
```sql
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);
```
2023-04-19 18:09:20 +00:00
Result:
2023-04-19 17:49:40 +00:00
```result
2022-11-22 14:30:44 +00:00
┌─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 │
└─────┴─────┴────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────┘
```
```sql
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);
```
2023-04-19 18:09:20 +00:00
Result:
2023-04-19 17:49:40 +00:00
```result
2022-11-22 14:30:44 +00:00
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 │
└─────┴─────┴────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────┘
```
2023-10-10 09:54:00 +00:00
## byteSwap
2023-10-13 14:51:13 +00:00
Reverses the bytes of an integer, i.e. changes its [endianness](https://en.wikipedia.org/wiki/Endianness).
2023-10-10 09:54:00 +00:00
**Syntax**
```sql
byteSwap(a)
```
**Example**
```sql
byteSwap(3351772109)
```
Result:
```result
┌─byteSwap(3351772109)─┐
│ 3455829959 │
└──────────────────────┘
```
The above example can be worked out in the following manner:
1. Convert the base-10 integer to its equivalent hexadecimal format in big-endian format, i.e. 3351772109 -> C7 C7 FB CD (4 bytes)
2. Reverse the bytes, i.e. C7 C7 FB CD -> CD FB C7 C7
3. Convert the result back to an integer assuming big-endian, i.e. CD FB C7 C7 -> 3455829959
One use case of this function is reversing IPv4s:
```result
┌─toIPv4(byteSwap(toUInt32(toIPv4('205.251.199.199'))))─┐
│ 199.199.251.205 │
└───────────────────────────────────────────────────────┘
```