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.

435 lines
13 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-04-19 17:49:40 +00:00
The result type of all arithmetic functions is the smallest type which can represent all possible results. Size promotion happens for integers up to 32 bit, e.g. `UInt8 + UInt16 = UInt32`. If one of the inters has 64 or more bits, the result is of the same type as the bigger of the input integers, e.g. `UInt16 + UInt128 = UInt128`. 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.
The result of addition or multiplication of two integers is unsigned unless one of the integers is signed.
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
Arithmetic functions work for any pair of `UInt8`, `UInt16`, `UInt32`, `UInt64`, `Int8`, `Int16`, `Int32`, `Int64`, `Float32`, or `Float64` values.
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-04-19 17:49:40 +00:00
Alias: `a \* b` (operator)
## divide
2023-04-19 18:09:20 +00:00
Calculates the quotient of two values `a` and `b`. The result is always a floating-point value. If you need integer division, you can use the `intDiv` function.
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.
The result has the same type as the dividend (the first parameter).
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`.
The result type is an integer if both inputs are integers. If one of the inputs is a floating-point number, the result is a floating-point number.
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**
- The result of multiplication with given scale.
2022-11-23 22:23:59 +00:00
Type: [Decimal256](../../sql-reference/data-types/decimal.md).
**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 │
└───────────────────────────────────────────────────────────┘
┌─multiplyDecimal(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐
│ -26.8609 │
└──────────────────────────────────────────────────────────────────┘
```
```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**
- The result of division with given scale.
2022-10-19 10:55:24 +00:00
Type: [Decimal256](../../sql-reference/data-types/decimal.md).
**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 │
└─────┴─────┴────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────┘
```