mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-24 08:32:02 +00:00
Merge pull request #48956 from ClickHouse/rs/docs-arithmetic
Docs: Cleanup some function docs
This commit is contained in:
commit
ec12bbf456
@ -6,7 +6,9 @@ sidebar_label: Arithmetic
|
|||||||
|
|
||||||
# Arithmetic Functions
|
# Arithmetic Functions
|
||||||
|
|
||||||
For all arithmetic functions, the result type is calculated as the smallest number type that the result fits in, if there is such a type. The minimum is taken simultaneously based on the number of bits, whether it is signed, and whether it floats. If there are not enough bits, the highest bit type is taken.
|
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:
|
Example:
|
||||||
|
|
||||||
@ -20,39 +22,78 @@ SELECT toTypeName(0), toTypeName(0 + 0), toTypeName(0 + 0 + 0), toTypeName(0 + 0
|
|||||||
└───────────────┴────────────────────────┴─────────────────────────────────┴──────────────────────────────────────────┘
|
└───────────────┴────────────────────────┴─────────────────────────────────┴──────────────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
Arithmetic functions work for any pair of types from UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, or Float64.
|
Arithmetic functions work for any pair of `UInt8`, `UInt16`, `UInt32`, `UInt64`, `Int8`, `Int16`, `Int32`, `Int64`, `Float32`, or `Float64` values.
|
||||||
|
|
||||||
Overflow is produced the same way as in C++.
|
Overflows are produced the same way as in C++.
|
||||||
|
|
||||||
## plus(a, b), a + b operator
|
## plus
|
||||||
|
|
||||||
Calculates the sum of the numbers.
|
Calculates the sum of two values `a` and `b`.
|
||||||
You can also add integer numbers with a date or date and time. In the case of a date, adding an integer means adding the corresponding number of days. For a date with time, it means adding the corresponding number of seconds.
|
|
||||||
|
|
||||||
## minus(a, b), a - b operator
|
**Syntax**
|
||||||
|
|
||||||
Calculates the difference. The result is always signed.
|
```sql
|
||||||
|
plus(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
You can also calculate integer numbers from a date or date with time. The idea is the same – see above for ‘plus’.
|
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.
|
||||||
|
|
||||||
## multiply(a, b), a \* b operator
|
Alias: `a + b` (operator)
|
||||||
|
|
||||||
Calculates the product of the numbers.
|
## minus
|
||||||
|
|
||||||
## divide(a, b), a / b operator
|
Calculates the difference of two values `a` and `b`. The result is always signed.
|
||||||
|
|
||||||
Calculates the quotient of the numbers. The result type is always a floating-point type.
|
Similar to `plus`, it is possible to subtract an integer from a date or date with time.
|
||||||
It is not integer division. For integer division, use the ‘intDiv’ function.
|
|
||||||
When dividing by zero you get ‘inf’, ‘-inf’, or ‘nan’.
|
|
||||||
|
|
||||||
## intDiv(a, b)
|
**Syntax**
|
||||||
|
|
||||||
Calculates the quotient of the numbers. Divides into integers, rounding down (by the absolute value).
|
```sql
|
||||||
|
minus(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
Returns an integer of the type of the dividend (the first parameter).
|
Alias: `a - b` (operator)
|
||||||
|
|
||||||
|
## multiply
|
||||||
|
|
||||||
|
Calculates the product of two values `a` and `b`.
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
multiply(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
|
Alias: `a \* b` (operator)
|
||||||
|
|
||||||
|
## divide
|
||||||
|
|
||||||
|
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`.
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
divide(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
|
Alias: `a / b` (operator)
|
||||||
|
|
||||||
|
## intDiv
|
||||||
|
|
||||||
|
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.
|
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.
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
intDiv(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
Query:
|
||||||
@ -62,6 +103,7 @@ SELECT
|
|||||||
intDiv(toFloat64(1), 0.001) AS res,
|
intDiv(toFloat64(1), 0.001) AS res,
|
||||||
toTypeName(res)
|
toTypeName(res)
|
||||||
```
|
```
|
||||||
|
|
||||||
```response
|
```response
|
||||||
┌──res─┬─toTypeName(intDiv(toFloat64(1), 0.001))─┐
|
┌──res─┬─toTypeName(intDiv(toFloat64(1), 0.001))─┐
|
||||||
│ 1000 │ Int64 │
|
│ 1000 │ Int64 │
|
||||||
@ -73,30 +115,65 @@ SELECT
|
|||||||
intDiv(1, 0.001) AS res,
|
intDiv(1, 0.001) AS res,
|
||||||
toTypeName(res)
|
toTypeName(res)
|
||||||
```
|
```
|
||||||
|
|
||||||
```response
|
```response
|
||||||
Received exception from server (version 23.2.1):
|
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)
|
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(a, b)
|
## intDivOrZero
|
||||||
|
|
||||||
Differs from ‘intDiv’ in that it returns zero when dividing by zero or when dividing a minimal negative number by minus one.
|
Same as `intDiv` but returns zero when dividing by zero or when dividing a minimal negative number by minus one.
|
||||||
|
|
||||||
## modulo(a, b), a % b operator
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
intDivOrZero(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
|
## modulo
|
||||||
|
|
||||||
|
Calculates the remainder of the division of two values `a` by `b`.
|
||||||
|
|
||||||
Calculates the remainder when dividing `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.
|
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.
|
||||||
|
|
||||||
The remainder is computed like in C++. Truncated division is used for negative numbers.
|
The remainder is computed like in C++. Truncated division is used for negative numbers.
|
||||||
|
|
||||||
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
|
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
|
||||||
|
|
||||||
## moduloOrZero(a, b)
|
**Syntax**
|
||||||
|
|
||||||
Differs from [modulo](#modulo) in that it returns zero when the divisor is zero.
|
```sql
|
||||||
|
modulo(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
## positiveModulo(a, b), positive_modulo(a, b), pmod(a, b)
|
Alias: `a % b` (operator)
|
||||||
Calculates the remainder when dividing `a` by `b`. Similar to the function `modulo` except that `positive_modulo` always returns a non-negative number.
|
|
||||||
|
|
||||||
Notice that `positive_modulo` is 4-5 times slower than `modulo`. You should not use `positive_modulo` unless you want to get a positive result and don't care about performance too much.
|
## moduloOrZero
|
||||||
|
|
||||||
|
Like [modulo](#modulo) but returns zero when the divisor is zero.
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
moduloOrZero(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
|
## positiveModulo(a, b)
|
||||||
|
|
||||||
|
Like [modulo](#modulo) but always returns a non-negative number.
|
||||||
|
|
||||||
|
This function is 4-5 times slower than `modulo`.
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
positiveModulo(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
|
Alias:
|
||||||
|
- `positive_modulo(a, b)`
|
||||||
|
- `pmod(a, b)`
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
@ -108,51 +185,67 @@ SELECT positiveModulo(-1, 10)
|
|||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
```text
|
```result
|
||||||
|
|
||||||
┌─positiveModulo(-1, 10)─┐
|
┌─positiveModulo(-1, 10)─┐
|
||||||
│ 9 │
|
│ 9 │
|
||||||
└────────────────────────┘
|
└────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
## negate(a), -a operator
|
## negate
|
||||||
|
|
||||||
Calculates a number with the reverse sign. The result is always signed.
|
Negates a value `a`. The result is always signed.
|
||||||
|
|
||||||
## abs(a)
|
|
||||||
|
|
||||||
Calculates the absolute value of the number (a). That is, if a \< 0, it returns -a. For unsigned types it does not do anything. For signed integer types, it returns an unsigned number.
|
|
||||||
|
|
||||||
## gcd(a, b)
|
|
||||||
|
|
||||||
Returns the greatest common divisor of the numbers.
|
|
||||||
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
|
|
||||||
|
|
||||||
## lcm(a, b)
|
|
||||||
|
|
||||||
Returns the least common multiple of the numbers.
|
|
||||||
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
|
|
||||||
|
|
||||||
## max2
|
|
||||||
|
|
||||||
Compares two values and returns the maximum. The returned value is converted to [Float64](../../sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
max2(value1, value2)
|
negate(a)
|
||||||
```
|
```
|
||||||
|
|
||||||
**Arguments**
|
Alias: `-a`
|
||||||
|
|
||||||
- `value1` — First value. [Int/UInt](../../sql-reference/data-types/int-uint.md) or [Float](../../sql-reference/data-types/float.md).
|
## abs
|
||||||
- `value2` — Second value. [Int/UInt](../../sql-reference/data-types/int-uint.md) or [Float](../../sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Returned value**
|
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.
|
||||||
|
|
||||||
- The maximum of two values.
|
**Syntax**
|
||||||
|
|
||||||
Type: [Float](../../sql-reference/data-types/float.md).
|
```sql
|
||||||
|
abs(a)
|
||||||
|
```
|
||||||
|
|
||||||
|
## gcd
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
gcd(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
|
## lcm(a, b)
|
||||||
|
|
||||||
|
Returns the least common multiple of two values `a` and `b`.
|
||||||
|
|
||||||
|
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
lcm(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
|
## max2
|
||||||
|
|
||||||
|
Returns the bigger of two values `a` and `b`. The returned value is of type [Float64](../../sql-reference/data-types/float.md).
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
max2(a, b)
|
||||||
|
```
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
@ -164,7 +257,7 @@ SELECT max2(-1, 2);
|
|||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
```text
|
```result
|
||||||
┌─max2(-1, 2)─┐
|
┌─max2(-1, 2)─┐
|
||||||
│ 2 │
|
│ 2 │
|
||||||
└─────────────┘
|
└─────────────┘
|
||||||
@ -172,25 +265,14 @@ Result:
|
|||||||
|
|
||||||
## min2
|
## min2
|
||||||
|
|
||||||
Compares two values and returns the minimum. The returned value is converted to [Float64](../../sql-reference/data-types/float.md).
|
Returns the smaller of two values `a` and `b`. The returned value is of type [Float64](../../sql-reference/data-types/float.md).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
min2(value1, value2)
|
min2(a, b)
|
||||||
```
|
```
|
||||||
|
|
||||||
**Arguments**
|
|
||||||
|
|
||||||
- `value1` — First value. [Int/UInt](../../sql-reference/data-types/int-uint.md) or [Float](../../sql-reference/data-types/float.md).
|
|
||||||
- `value2` — Second value. [Int/UInt](../../sql-reference/data-types/int-uint.md) or [Float](../../sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Returned value**
|
|
||||||
|
|
||||||
- The minimum of two values.
|
|
||||||
|
|
||||||
Type: [Float](../../sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
Query:
|
||||||
@ -201,21 +283,19 @@ SELECT min2(-1, 2);
|
|||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
```text
|
```result
|
||||||
┌─min2(-1, 2)─┐
|
┌─min2(-1, 2)─┐
|
||||||
│ -1 │
|
│ -1 │
|
||||||
└─────────────┘
|
└─────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
## multiplyDecimal(a, b[, result_scale])
|
## multiplyDecimal
|
||||||
|
|
||||||
Performs multiplication on two decimals. Result value will be of type [Decimal256](../../sql-reference/data-types/decimal.md).
|
Multiplies two decimals `a` and `b`. The result value will be of type [Decimal256](../../sql-reference/data-types/decimal.md).
|
||||||
Result scale can be explicitly specified by `result_scale` argument (const Integer in range `[0, 76]`). If not specified, the result scale is the max scale of given arguments.
|
|
||||||
|
|
||||||
:::note
|
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.
|
||||||
These functions work significantly slower than usual `multiply`.
|
|
||||||
In case you don't really need controlled precision and/or need fast computation, consider using [multiply](#multiply)
|
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`.
|
||||||
:::
|
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -237,19 +317,22 @@ Type: [Decimal256](../../sql-reference/data-types/decimal.md).
|
|||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
```text
|
```result
|
||||||
┌─multiplyDecimal(toDecimal256(-12, 0), toDecimal32(-2.1, 1), 1)─┐
|
┌─multiplyDecimal(toDecimal256(-12, 0), toDecimal32(-2.1, 1), 1)─┐
|
||||||
│ 25.2 │
|
│ 25.2 │
|
||||||
└────────────────────────────────────────────────────────────────┘
|
└────────────────────────────────────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
**Difference from regular multiplication:**
|
**Differences compared to regular multiplication:**
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT toDecimal64(-12.647, 3) * toDecimal32(2.1239, 4);
|
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);
|
SELECT toDecimal64(-12.647, 3) as a, toDecimal32(2.1239, 4) as b, multiplyDecimal(a, b);
|
||||||
```
|
```
|
||||||
|
|
||||||
```text
|
Result:
|
||||||
|
|
||||||
|
```result
|
||||||
┌─multiply(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐
|
┌─multiply(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐
|
||||||
│ -26.8609633 │
|
│ -26.8609633 │
|
||||||
└───────────────────────────────────────────────────────────┘
|
└───────────────────────────────────────────────────────────┘
|
||||||
@ -270,7 +353,9 @@ SELECT
|
|||||||
a * b;
|
a * b;
|
||||||
```
|
```
|
||||||
|
|
||||||
```text
|
Result:
|
||||||
|
|
||||||
|
```result
|
||||||
┌─────────────a─┬─────────────b─┬─multiplyDecimal(toDecimal64(-12.647987876, 9), toDecimal64(123.967645643, 9))─┐
|
┌─────────────a─┬─────────────b─┬─multiplyDecimal(toDecimal64(-12.647987876, 9), toDecimal64(123.967645643, 9))─┐
|
||||||
│ -12.647987876 │ 123.967645643 │ -1567.941279108 │
|
│ -12.647987876 │ 123.967645643 │ -1567.941279108 │
|
||||||
└───────────────┴───────────────┴───────────────────────────────────────────────────────────────────────────────┘
|
└───────────────┴───────────────┴───────────────────────────────────────────────────────────────────────────────┘
|
||||||
@ -279,15 +364,14 @@ 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)
|
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(a, b[, result_scale])
|
## divideDecimal
|
||||||
|
|
||||||
Performs division on two decimals. Result value will be of type [Decimal256](../../sql-reference/data-types/decimal.md).
|
|
||||||
Result scale can be explicitly specified by `result_scale` argument (const Integer in range `[0, 76]`). If not specified, the result scale is the max scale of given arguments.
|
|
||||||
|
|
||||||
:::note
|
Divides two decimals `a` and `b`. The result value will be of type [Decimal256](../../sql-reference/data-types/decimal.md).
|
||||||
These function work significantly slower than usual `divide`.
|
|
||||||
In case you don't really need controlled precision and/or need fast computation, consider using [divide](#divide).
|
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`.
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -309,19 +393,22 @@ Type: [Decimal256](../../sql-reference/data-types/decimal.md).
|
|||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
```text
|
```result
|
||||||
┌─divideDecimal(toDecimal256(-12, 0), toDecimal32(2.1, 1), 10)─┐
|
┌─divideDecimal(toDecimal256(-12, 0), toDecimal32(2.1, 1), 10)─┐
|
||||||
│ -5.7142857142 │
|
│ -5.7142857142 │
|
||||||
└──────────────────────────────────────────────────────────────┘
|
└──────────────────────────────────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
**Difference from regular division:**
|
**Differences compared to regular division:**
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT toDecimal64(-12, 1) / toDecimal32(2.1, 1);
|
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);
|
SELECT toDecimal64(-12, 1) as a, toDecimal32(2.1, 1) as b, divideDecimal(a, b, 1), divideDecimal(a, b, 5);
|
||||||
```
|
```
|
||||||
|
|
||||||
```text
|
Result:
|
||||||
|
|
||||||
|
```result
|
||||||
┌─divide(toDecimal64(-12, 1), toDecimal32(2.1, 1))─┐
|
┌─divide(toDecimal64(-12, 1), toDecimal32(2.1, 1))─┐
|
||||||
│ -5.7 │
|
│ -5.7 │
|
||||||
└──────────────────────────────────────────────────┘
|
└──────────────────────────────────────────────────┘
|
||||||
@ -336,7 +423,9 @@ 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);
|
SELECT toDecimal64(-12, 0) as a, toDecimal32(2.1, 1) as b, divideDecimal(a, b, 1), divideDecimal(a, b, 5);
|
||||||
```
|
```
|
||||||
|
|
||||||
```text
|
Result:
|
||||||
|
|
||||||
|
```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)
|
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)─┐
|
┌───a─┬───b─┬─divideDecimal(toDecimal64(-12, 0), toDecimal32(2.1, 1), 1)─┬─divideDecimal(toDecimal64(-12, 0), toDecimal32(2.1, 1), 5)─┐
|
||||||
|
@ -6,13 +6,13 @@ sidebar_label: Logical
|
|||||||
|
|
||||||
# Logical Functions
|
# Logical Functions
|
||||||
|
|
||||||
Performs logical operations on arguments of any numeric types, but returns a [UInt8](../../sql-reference/data-types/int-uint.md) number equal to 0, 1 or `NULL` in some cases.
|
Below functions perform logical operations on arguments of arbitrary numeric types. They return either 0 or 1 as [UInt8](../../sql-reference/data-types/int-uint.md) or in some cases `NULL`.
|
||||||
|
|
||||||
Zero as an argument is considered `false`, while any non-zero value is considered `true`.
|
Zero as an argument is considered `false`, non-zero values are considered `true`.
|
||||||
|
|
||||||
## and
|
## and
|
||||||
|
|
||||||
Calculates the result of the logical conjunction between two or more values. Corresponds to [Logical AND Operator](../../sql-reference/operators/index.md#logical-and-operator).
|
Calculates the logical conjunction between two or more values.
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -20,7 +20,9 @@ Calculates the result of the logical conjunction between two or more values. Cor
|
|||||||
and(val1, val2...)
|
and(val1, val2...)
|
||||||
```
|
```
|
||||||
|
|
||||||
You can use the [short_circuit_function_evaluation](../../operations/settings/settings.md#short-circuit-function-evaluation) setting to calculate the `and` function according to a short scheme. If this setting is enabled, `vali` is evaluated only on rows where `(val1 AND val2 AND ... AND val{i-1})` is true. For example, an exception about division by zero is not thrown when executing the query `SELECT and(number = 2, intDiv(1, number)) FROM numbers(10)`.
|
Setting [short_circuit_function_evaluation](../../operations/settings/settings.md#short-circuit-function-evaluation) controls whether short-circuit evaluation is used. If enabled, `val_i` is evaluated only if `(val_1 AND val_2 AND ... AND val_{i-1})` is `true`. For example, with short-circuit evaluation, no division-by-zero exception is thrown when executing the query `SELECT and(number = 2, intDiv(1, number)) FROM numbers(5)`.
|
||||||
|
|
||||||
|
Alias: The [AND Operator](../../sql-reference/operators/index.md#logical-and-operator).
|
||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
@ -28,16 +30,14 @@ You can use the [short_circuit_function_evaluation](../../operations/settings/se
|
|||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- `0`, if there is at least one zero value argument.
|
- `0`, if there at least one argument evaluates to `false`,
|
||||||
- `NULL`, if there are no zero values arguments and there is at least one `NULL` argument.
|
- `NULL`, if no argumetn evaluates to `false` and at least one argument is `NULL`,
|
||||||
- `1`, otherwise.
|
- `1`, otherwise.
|
||||||
|
|
||||||
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
|
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT and(0, 1, -2);
|
SELECT and(0, 1, -2);
|
||||||
```
|
```
|
||||||
@ -66,7 +66,7 @@ Result:
|
|||||||
|
|
||||||
## or
|
## or
|
||||||
|
|
||||||
Calculates the result of the logical disjunction between two or more values. Corresponds to [Logical OR Operator](../../sql-reference/operators/index.md#logical-or-operator).
|
Calculates the logical disjunction between two or more values.
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -74,7 +74,9 @@ Calculates the result of the logical disjunction between two or more values. Cor
|
|||||||
or(val1, val2...)
|
or(val1, val2...)
|
||||||
```
|
```
|
||||||
|
|
||||||
You can use the [short_circuit_function_evaluation](../../operations/settings/settings.md#short-circuit-function-evaluation) setting to calculate the `or` function according to a short scheme. If this setting is enabled, `vali` is evaluated only on rows where `((NOT val1) AND (NOT val2) AND ... AND (NOT val{i-1}))` is true. For example, an exception about division by zero is not thrown when executing the query `SELECT or(number = 0, intDiv(1, number) != 0) FROM numbers(10)`.
|
Setting [short_circuit_function_evaluation](../../operations/settings/settings.md#short-circuit-function-evaluation) controls whether short-circuit evaluation is used. If enabled, `val_i` is evaluated only if `((NOT val_1) AND (NOT val_2) AND ... AND (NOT val_{i-1}))` is `true`. For example, with short-circuit evaluation, no division-by-zero exception is thrown when executing the query `SELECT or(number = 0, intDiv(1, number) != 0) FROM numbers(5)`.
|
||||||
|
|
||||||
|
Alias: The [OR Operator](../../sql-reference/operators/index.md#logical-or-operator).
|
||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
@ -82,16 +84,14 @@ You can use the [short_circuit_function_evaluation](../../operations/settings/se
|
|||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- `1`, if there is at least one non-zero value.
|
- `1`, if at least one argument evalutes to `true`,
|
||||||
- `0`, if there are only zero values.
|
- `0`, if all arguments evaluate to `false`,
|
||||||
- `NULL`, if there are only zero values and `NULL`.
|
- `NULL`, if all arguments evaluate to `false` and at least one argument is `NULL`.
|
||||||
|
|
||||||
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
|
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT or(1, 0, 0, 2, NULL);
|
SELECT or(1, 0, 0, 2, NULL);
|
||||||
```
|
```
|
||||||
@ -120,7 +120,7 @@ Result:
|
|||||||
|
|
||||||
## not
|
## not
|
||||||
|
|
||||||
Calculates the result of the logical negation of the value. Corresponds to [Logical Negation Operator](../../sql-reference/operators/index.md#logical-negation-operator).
|
Calculates logical negation of a value.
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -128,22 +128,22 @@ Calculates the result of the logical negation of the value. Corresponds to [Logi
|
|||||||
not(val);
|
not(val);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Alias: The [Negation Operator](../../sql-reference/operators/index.md#logical-negation-operator).
|
||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
- `val` — The value. [Int](../../sql-reference/data-types/int-uint.md), [UInt](../../sql-reference/data-types/int-uint.md), [Float](../../sql-reference/data-types/float.md) or [Nullable](../../sql-reference/data-types/nullable.md).
|
- `val` — The value. [Int](../../sql-reference/data-types/int-uint.md), [UInt](../../sql-reference/data-types/int-uint.md), [Float](../../sql-reference/data-types/float.md) or [Nullable](../../sql-reference/data-types/nullable.md).
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- `1`, if the `val` is `0`.
|
- `1`, if `val` evaluates to `false`,
|
||||||
- `0`, if the `val` is a non-zero value.
|
- `0`, if `val` evaluates to `true`,
|
||||||
- `NULL`, if the `val` is a `NULL` value.
|
- `NULL`, if `val` is `NULL`.
|
||||||
|
|
||||||
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
|
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT NOT(1);
|
SELECT NOT(1);
|
||||||
```
|
```
|
||||||
@ -158,7 +158,7 @@ Result:
|
|||||||
|
|
||||||
## xor
|
## xor
|
||||||
|
|
||||||
Calculates the result of the logical exclusive disjunction between two or more values. For more than two values the function works as if it calculates `XOR` of the first two values and then uses the result with the next value to calculate `XOR` and so on.
|
Calculates the logical exclusive disjunction between two or more values. For more than two values the function first xor-s the first two values, then xor-s the result with the third value etc.
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -172,16 +172,14 @@ xor(val1, val2...)
|
|||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- `1`, for two values: if one of the values is zero and other is not.
|
- `1`, for two values: if one of the values evaluates to `false` and other does not,
|
||||||
- `0`, for two values: if both values are zero or non-zero at the same time.
|
- `0`, for two values: if both values evalute to `false` or to both `true`,
|
||||||
- `NULL`, if there is at least one `NULL` value.
|
- `NULL`, if at least one of the inputs is `NULL`
|
||||||
|
|
||||||
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
|
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT xor(0, 1, 1);
|
SELECT xor(0, 1, 1);
|
||||||
```
|
```
|
||||||
|
@ -6,53 +6,39 @@ sidebar_label: Random Numbers
|
|||||||
|
|
||||||
# Functions for Generating Random Numbers
|
# Functions for Generating Random Numbers
|
||||||
|
|
||||||
All the functions accept zero arguments or one argument. If an argument is passed, it can be any type, and its value is not used for anything. The only purpose of this argument is to prevent common subexpression elimination, so that two different instances of the same function return different columns with different random numbers.
|
All functions in this section accept zero or one arguments. The only use of the argument (if provided) is to prevent prevent [common subexpression
|
||||||
|
elimination](../../sql-reference/functions/index.md#common-subexpression-elimination) such that two different execution of the same random
|
||||||
|
function in a query return different random values.
|
||||||
|
|
||||||
|
Related content
|
||||||
|
- Blog: [Generating random data in ClickHouse](https://clickhouse.com/blog/generating-random-test-distribution-data-for-clickhouse)
|
||||||
|
|
||||||
:::note
|
:::note
|
||||||
Non-cryptographic generators of random numbers are used.
|
The random numbers are generated by non-cryptographic algorithms.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
## rand, rand32
|
## rand, rand32
|
||||||
|
|
||||||
Returns a random UInt32 number, evenly distributed among all UInt32-type numbers.
|
Returns a random UInt32 number, evenly distributed accross the range of all possible UInt32 numbers.
|
||||||
|
|
||||||
Uses a linear congruential generator.
|
Uses a linear congruential generator.
|
||||||
|
|
||||||
## rand64
|
## rand64
|
||||||
|
|
||||||
Returns a random UInt64 number, evenly distributed among all UInt64-type numbers.
|
Returns a random UInt64 number, evenly distributed accross the range of all possible UInt64 numbers.
|
||||||
|
|
||||||
Uses a linear congruential generator.
|
Uses a linear congruential generator.
|
||||||
|
|
||||||
## randCanonical
|
## randCanonical
|
||||||
The function generates results with independent and identically distributed uniformly distributed values in [0, 1).
|
|
||||||
|
|
||||||
Non-deterministic. Return type is Float64.
|
Returns a Float64 value, evenly distributed in [0, 1).
|
||||||
|
|
||||||
## randConstant
|
## randConstant
|
||||||
|
|
||||||
Produces a constant column with a random value.
|
Like `rand` but produces a constant column with a random value.
|
||||||
|
|
||||||
**Syntax**
|
|
||||||
|
|
||||||
``` sql
|
|
||||||
randConstant([x])
|
|
||||||
```
|
|
||||||
|
|
||||||
**Arguments**
|
|
||||||
|
|
||||||
- `x` — [Expression](../../sql-reference/syntax.md#syntax-expressions) resulting in any of the [supported data types](../../sql-reference/data-types/index.md#data_types). The resulting value is discarded, but the expression itself if used for bypassing [common subexpression elimination](../../sql-reference/functions/index.md#common-subexpression-elimination) if the function is called multiple times in one query. Optional parameter.
|
|
||||||
|
|
||||||
**Returned value**
|
|
||||||
|
|
||||||
- Random number.
|
|
||||||
|
|
||||||
Type: [UInt32](../../sql-reference/data-types/int-uint.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT rand(), rand(1), rand(number), randConstant(), randConstant(1), randConstant(number)
|
SELECT rand(), rand(1), rand(number), randConstant(), randConstant(1), randConstant(number)
|
||||||
FROM numbers(3)
|
FROM numbers(3)
|
||||||
@ -60,7 +46,7 @@ FROM numbers(3)
|
|||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─────rand()─┬────rand(1)─┬─rand(number)─┬─randConstant()─┬─randConstant(1)─┬─randConstant(number)─┐
|
┌─────rand()─┬────rand(1)─┬─rand(number)─┬─randConstant()─┬─randConstant(1)─┬─randConstant(number)─┐
|
||||||
│ 3047369878 │ 4132449925 │ 4044508545 │ 2740811946 │ 4229401477 │ 1924032898 │
|
│ 3047369878 │ 4132449925 │ 4044508545 │ 2740811946 │ 4229401477 │ 1924032898 │
|
||||||
│ 2938880146 │ 1267722397 │ 4154983056 │ 2740811946 │ 4229401477 │ 1924032898 │
|
│ 2938880146 │ 1267722397 │ 4154983056 │ 2740811946 │ 4229401477 │ 1924032898 │
|
||||||
@ -68,17 +54,11 @@ Result:
|
|||||||
└────────────┴────────────┴──────────────┴────────────────┴─────────────────┴──────────────────────┘
|
└────────────┴────────────┴──────────────┴────────────────┴─────────────────┴──────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
# Functions for Generating Random Numbers based on Distributions
|
# Functions for Generating Random Numbers based on a Distribution
|
||||||
|
|
||||||
:::note
|
|
||||||
These functions are available starting from 22.10.
|
|
||||||
:::
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randUniform
|
## randUniform
|
||||||
|
|
||||||
Return random number based on [continuous uniform distribution](https://en.wikipedia.org/wiki/Continuous_uniform_distribution) in a specified range from `min` to `max`.
|
Returns a Float64 drawn uniformly from the interval between `min` and `max` ([continuous uniform distribution](https://en.wikipedia.org/wiki/Continuous_uniform_distribution)).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -93,21 +73,17 @@ randUniform(min, max)
|
|||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
||||||
|
|
||||||
Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randUniform(5.5, 10) FROM numbers(5)
|
SELECT randUniform(5.5, 10) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─randUniform(5.5, 10)─┐
|
┌─randUniform(5.5, 10)─┐
|
||||||
│ 8.094978491443102 │
|
│ 8.094978491443102 │
|
||||||
│ 7.3181248914450885 │
|
│ 7.3181248914450885 │
|
||||||
@ -117,40 +93,34 @@ Result:
|
|||||||
└──────────────────────┘
|
└──────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randNormal
|
## randNormal
|
||||||
|
|
||||||
Return random number based on [normal distribution](https://en.wikipedia.org/wiki/Normal_distribution).
|
Returns a Float64 drawn from a [normal distribution](https://en.wikipedia.org/wiki/Normal_distribution).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
randNormal(meam, variance)
|
randNormal(mean, variance)
|
||||||
```
|
```
|
||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
- `meam` - `Float64` mean value of distribution,
|
- `mean` - `Float64` - mean value of distribution,
|
||||||
- `variance` - `Float64` - [variance](https://en.wikipedia.org/wiki/Variance).
|
- `variance` - `Float64` - [variance](https://en.wikipedia.org/wiki/Variance).
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
||||||
|
|
||||||
Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randNormal(10, 2) FROM numbers(5)
|
SELECT randNormal(10, 2) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌──randNormal(10, 2)─┐
|
┌──randNormal(10, 2)─┐
|
||||||
│ 13.389228911709653 │
|
│ 13.389228911709653 │
|
||||||
│ 8.622949707401295 │
|
│ 8.622949707401295 │
|
||||||
@ -160,40 +130,34 @@ Result:
|
|||||||
└────────────────────┘
|
└────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randLogNormal
|
## randLogNormal
|
||||||
|
|
||||||
Return random number based on [log-normal distribution](https://en.wikipedia.org/wiki/Log-normal_distribution).
|
Returns a Float64 drawn from a [log-normal distribution](https://en.wikipedia.org/wiki/Log-normal_distribution).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
randLogNormal(meam, variance)
|
randLogNormal(mean, variance)
|
||||||
```
|
```
|
||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
- `meam` - `Float64` mean value of distribution,
|
- `mean` - `Float64` - mean value of distribution,
|
||||||
- `variance` - `Float64` - [variance](https://en.wikipedia.org/wiki/Variance).
|
- `variance` - `Float64` - [variance](https://en.wikipedia.org/wiki/Variance).
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
||||||
|
|
||||||
Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randLogNormal(100, 5) FROM numbers(5)
|
SELECT randLogNormal(100, 5) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─randLogNormal(100, 5)─┐
|
┌─randLogNormal(100, 5)─┐
|
||||||
│ 1.295699673937363e48 │
|
│ 1.295699673937363e48 │
|
||||||
│ 9.719869109186684e39 │
|
│ 9.719869109186684e39 │
|
||||||
@ -203,11 +167,9 @@ Result:
|
|||||||
└───────────────────────┘
|
└───────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randBinomial
|
## randBinomial
|
||||||
|
|
||||||
Return random number based on [binomial distribution](https://en.wikipedia.org/wiki/Binomial_distribution).
|
Returns a UInt64 drawn from a [binomial distribution](https://en.wikipedia.org/wiki/Binomial_distribution).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -217,26 +179,22 @@ randBinomial(experiments, probability)
|
|||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
- `experiments` - `UInt64` number of experiments,
|
- `experiments` - `UInt64` - number of experiments,
|
||||||
- `probability` - `Float64` - probability of success in each experiment (values in `0...1` range only).
|
- `probability` - `Float64` - probability of success in each experiment (values in `0...1` range only).
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [UInt64](/docs/en/sql-reference/data-types/int-uint.md).
|
||||||
|
|
||||||
Type: [UInt64](/docs/en/sql-reference/data-types/int-uint.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randBinomial(100, .75) FROM numbers(5)
|
SELECT randBinomial(100, .75) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─randBinomial(100, 0.75)─┐
|
┌─randBinomial(100, 0.75)─┐
|
||||||
│ 74 │
|
│ 74 │
|
||||||
│ 78 │
|
│ 78 │
|
||||||
@ -246,11 +204,9 @@ Result:
|
|||||||
└─────────────────────────┘
|
└─────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randNegativeBinomial
|
## randNegativeBinomial
|
||||||
|
|
||||||
Return random number based on [negative binomial distribution](https://en.wikipedia.org/wiki/Negative_binomial_distribution).
|
Returns a UInt64 drawn from a [negative binomial distribution](https://en.wikipedia.org/wiki/Negative_binomial_distribution).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -260,26 +216,22 @@ randNegativeBinomial(experiments, probability)
|
|||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
- `experiments` - `UInt64` number of experiments,
|
- `experiments` - `UInt64` - number of experiments,
|
||||||
- `probability` - `Float64` - probability of failure in each experiment (values in `0...1` range only).
|
- `probability` - `Float64` - probability of failure in each experiment (values in `0...1` range only).
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [UInt64](/docs/en/sql-reference/data-types/int-uint.md).
|
||||||
|
|
||||||
Type: [UInt64](/docs/en/sql-reference/data-types/int-uint.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randNegativeBinomial(100, .75) FROM numbers(5)
|
SELECT randNegativeBinomial(100, .75) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─randNegativeBinomial(100, 0.75)─┐
|
┌─randNegativeBinomial(100, 0.75)─┐
|
||||||
│ 33 │
|
│ 33 │
|
||||||
│ 32 │
|
│ 32 │
|
||||||
@ -289,11 +241,9 @@ Result:
|
|||||||
└─────────────────────────────────┘
|
└─────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randPoisson
|
## randPoisson
|
||||||
|
|
||||||
Return random number based on [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution).
|
Returns a UInt64 drawn from a [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -303,25 +253,21 @@ randPoisson(n)
|
|||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
- `n` - `UInt64` mean number of occurrences.
|
- `n` - `UInt64` - mean number of occurrences.
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [UInt64](/docs/en/sql-reference/data-types/int-uint.md).
|
||||||
|
|
||||||
Type: [UInt64](/docs/en/sql-reference/data-types/int-uint.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randPoisson(10) FROM numbers(5)
|
SELECT randPoisson(10) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─randPoisson(10)─┐
|
┌─randPoisson(10)─┐
|
||||||
│ 8 │
|
│ 8 │
|
||||||
│ 8 │
|
│ 8 │
|
||||||
@ -331,11 +277,9 @@ Result:
|
|||||||
└─────────────────┘
|
└─────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randBernoulli
|
## randBernoulli
|
||||||
|
|
||||||
Return random number based on [Bernoulli distribution](https://en.wikipedia.org/wiki/Bernoulli_distribution).
|
Returns a UInt64 drawn from a [Bernoulli distribution](https://en.wikipedia.org/wiki/Bernoulli_distribution).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -349,21 +293,17 @@ randBernoulli(probability)
|
|||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [UInt64](/docs/en/sql-reference/data-types/int-uint.md).
|
||||||
|
|
||||||
Type: [UInt64](/docs/en/sql-reference/data-types/int-uint.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randBernoulli(.75) FROM numbers(5)
|
SELECT randBernoulli(.75) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─randBernoulli(0.75)─┐
|
┌─randBernoulli(0.75)─┐
|
||||||
│ 1 │
|
│ 1 │
|
||||||
│ 1 │
|
│ 1 │
|
||||||
@ -373,11 +313,9 @@ Result:
|
|||||||
└─────────────────────┘
|
└─────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randExponential
|
## randExponential
|
||||||
|
|
||||||
Return random number based on [exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution).
|
Returns a Float64 drawn from a [exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -387,25 +325,21 @@ randExponential(lambda)
|
|||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
- `lambda` - `Float64` lambda value.
|
- `lambda` - `Float64` - lambda value.
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
||||||
|
|
||||||
Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randExponential(1/10) FROM numbers(5)
|
SELECT randExponential(1/10) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─randExponential(divide(1, 10))─┐
|
┌─randExponential(divide(1, 10))─┐
|
||||||
│ 44.71628934340778 │
|
│ 44.71628934340778 │
|
||||||
│ 4.211013337903262 │
|
│ 4.211013337903262 │
|
||||||
@ -415,11 +349,9 @@ Result:
|
|||||||
└────────────────────────────────┘
|
└────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randChiSquared
|
## randChiSquared
|
||||||
|
|
||||||
Return random number based on [Chi-square distribution](https://en.wikipedia.org/wiki/Chi-squared_distribution) - a distribution of a sum of the squares of k independent standard normal random variables.
|
Returns a Float64 drawn from a [Chi-square distribution](https://en.wikipedia.org/wiki/Chi-squared_distribution) - a distribution of a sum of the squares of k independent standard normal random variables.
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -429,25 +361,21 @@ randChiSquared(degree_of_freedom)
|
|||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
- `degree_of_freedom` - `Float64` degree of freedom.
|
- `degree_of_freedom` - `Float64` - degree of freedom.
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
||||||
|
|
||||||
Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randChiSquared(10) FROM numbers(5)
|
SELECT randChiSquared(10) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─randChiSquared(10)─┐
|
┌─randChiSquared(10)─┐
|
||||||
│ 10.015463656521543 │
|
│ 10.015463656521543 │
|
||||||
│ 9.621799919882768 │
|
│ 9.621799919882768 │
|
||||||
@ -457,11 +385,9 @@ Result:
|
|||||||
└────────────────────┘
|
└────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randStudentT
|
## randStudentT
|
||||||
|
|
||||||
Return random number based on [Student's t-distribution](https://en.wikipedia.org/wiki/Student%27s_t-distribution).
|
Returns a Float64 drawn from a [Student's t-distribution](https://en.wikipedia.org/wiki/Student%27s_t-distribution).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -471,25 +397,21 @@ randStudentT(degree_of_freedom)
|
|||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
- `degree_of_freedom` - `Float64` degree of freedom.
|
- `degree_of_freedom` - `Float64` - degree of freedom.
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
||||||
|
|
||||||
Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randStudentT(10) FROM numbers(5)
|
SELECT randStudentT(10) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─────randStudentT(10)─┐
|
┌─────randStudentT(10)─┐
|
||||||
│ 1.2217309938538725 │
|
│ 1.2217309938538725 │
|
||||||
│ 1.7941971681200541 │
|
│ 1.7941971681200541 │
|
||||||
@ -499,11 +421,9 @@ Result:
|
|||||||
└──────────────────────┘
|
└──────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## randFisherF
|
## randFisherF
|
||||||
|
|
||||||
Return random number based on [F-distribution](https://en.wikipedia.org/wiki/F-distribution).
|
Returns a Float64 drawn from a [F-distribution](https://en.wikipedia.org/wiki/F-distribution).
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
@ -513,26 +433,22 @@ randFisherF(d1, d2)
|
|||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
|
|
||||||
- `d1` - `Float64` d1 degree of freedom in `X = (S1 / d1) / (S2 / d2)`,
|
- `d1` - `Float64` - d1 degree of freedom in `X = (S1 / d1) / (S2 / d2)`,
|
||||||
- `d2` - `Float64` d2 degree of freedom in `X = (S1 / d1) / (S2 / d2)`,
|
- `d2` - `Float64` - d2 degree of freedom in `X = (S1 / d1) / (S2 / d2)`,
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
|
|
||||||
- Random number.
|
- Random number. Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
||||||
|
|
||||||
Type: [Float64](/docs/en/sql-reference/data-types/float.md).
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
Query:
|
|
||||||
|
|
||||||
``` sql
|
``` sql
|
||||||
SELECT randFisherF(10, 3) FROM numbers(5)
|
SELECT randFisherF(10, 3) FROM numbers(5)
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌──randFisherF(10, 3)─┐
|
┌──randFisherF(10, 3)─┐
|
||||||
│ 7.286287504216609 │
|
│ 7.286287504216609 │
|
||||||
│ 0.26590779413050386 │
|
│ 0.26590779413050386 │
|
||||||
@ -542,35 +458,61 @@ Result:
|
|||||||
└─────────────────────┘
|
└─────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
# Functions for Generating Random Strings
|
||||||
|
|
||||||
|
|
||||||
# Random Functions for Working with Strings
|
|
||||||
|
|
||||||
## randomString
|
## randomString
|
||||||
|
|
||||||
|
Returns a random String of specified `length`. Not all characters may be printable.
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
randomString(length)
|
||||||
|
```
|
||||||
|
|
||||||
## randomFixedString
|
## randomFixedString
|
||||||
|
|
||||||
|
Like `randomString` but returns a FixedString.
|
||||||
|
|
||||||
## randomPrintableASCII
|
## randomPrintableASCII
|
||||||
|
|
||||||
|
Returns a random String of specified `length`. All characters are printable.
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
randomPrintableASCII(length)
|
||||||
|
```
|
||||||
|
|
||||||
## randomStringUTF8
|
## randomStringUTF8
|
||||||
|
|
||||||
|
Returns a random String containing `length` many UTF8 codepoints. Not all characters may be printable
|
||||||
|
|
||||||
|
**Syntax**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
randomStringUTF8(length)
|
||||||
|
```
|
||||||
|
|
||||||
## fuzzBits
|
## fuzzBits
|
||||||
|
|
||||||
**Syntax**
|
**Syntax**
|
||||||
|
|
||||||
``` sql
|
Inverts the bits of String or FixedString `s`, each with probability `prob`.
|
||||||
fuzzBits([s], [prob])
|
|
||||||
```
|
|
||||||
|
|
||||||
Inverts bits of `s`, each with probability `prob`.
|
**Syntax**
|
||||||
|
|
||||||
|
``` sql
|
||||||
|
fuzzBits(s, prob)
|
||||||
|
```
|
||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
- `s` - `String` or `FixedString`
|
- `s` - `String` or `FixedString`
|
||||||
- `prob` - constant `Float32/64`
|
- `prob` - constant `Float32/64`
|
||||||
|
|
||||||
**Returned value**
|
**Returned value**
|
||||||
Fuzzed string with same as s type.
|
|
||||||
|
Fuzzed string with same type as `s`.
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
@ -581,13 +523,10 @@ FROM numbers(3)
|
|||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
``` text
|
``` result
|
||||||
┌─fuzzBits(materialize('abacaba'), 0.1)─┐
|
┌─fuzzBits(materialize('abacaba'), 0.1)─┐
|
||||||
│ abaaaja │
|
│ abaaaja │
|
||||||
│ a*cjab+ │
|
│ a*cjab+ │
|
||||||
│ aeca2A │
|
│ aeca2A │
|
||||||
└───────────────────────────────────────┘
|
└───────────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
## Related content
|
|
||||||
- Blog: [Generating random data in ClickHouse](https://clickhouse.com/blog/generating-random-test-distribution-data-for-clickhouse)
|
|
||||||
|
@ -8,9 +8,7 @@ sidebar_label: Type Conversion
|
|||||||
|
|
||||||
## Common Issues with Data Conversion
|
## Common Issues with Data Conversion
|
||||||
|
|
||||||
ClickHouse generally uses the [same behavior as C++ programs](https://en.cppreference.com/w/cpp/language/implicit_conversion). In particular,
|
ClickHouse generally uses the [same behavior as C++ programs](https://en.cppreference.com/w/cpp/language/implicit_conversion).
|
||||||
- the result of addition or multiplication of two integers is unsigned unless one of the integers is signed,
|
|
||||||
- the result of addition or multiplication of two integers is of the next bigger integer type such that every possible result can be represented (e.g. `UInt8 + UInt16 = UInt32`). Size promotion only happens for integers up to 32 bit. If one of the integers has 64 or more bits, the result of addition or multiplication 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.
|
|
||||||
|
|
||||||
`to<type>` functions and [cast](#castx-t) behave differently in some cases, for example in case of [LowCardinality](../data-types/lowcardinality.md): [cast](#castx-t) removes [LowCardinality](../data-types/lowcardinality.md) trait `to<type>` functions don't. The same with [Nullable](../data-types/nullable.md), this behaviour is not compatible with SQL standard, and it can be changed using [cast_keep_nullable](../../operations/settings/settings.md/#cast_keep_nullable) setting.
|
`to<type>` functions and [cast](#castx-t) behave differently in some cases, for example in case of [LowCardinality](../data-types/lowcardinality.md): [cast](#castx-t) removes [LowCardinality](../data-types/lowcardinality.md) trait `to<type>` functions don't. The same with [Nullable](../data-types/nullable.md), this behaviour is not compatible with SQL standard, and it can be changed using [cast_keep_nullable](../../operations/settings/settings.md/#cast_keep_nullable) setting.
|
||||||
|
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
---
|
---
|
||||||
slug: /en/sql-reference/functions/uuid-functions
|
slug: /en/sql-reference/functions/uuid-functions
|
||||||
sidebar_position: 205
|
sidebar_position: 205
|
||||||
sidebar_label: UUID
|
sidebar_label: UUIDs
|
||||||
---
|
---
|
||||||
|
|
||||||
# Functions for Working with UUID
|
# Functions for Working with UUIDs
|
||||||
|
|
||||||
The functions for working with UUID are listed below.
|
|
||||||
|
|
||||||
## generateUUIDv4
|
## generateUUIDv4
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user