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

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

194 lines
5.6 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/logical-functions
2023-04-19 17:05:55 +00:00
sidebar_position: 110
sidebar_label: Logical
2020-04-03 13:23:32 +00:00
---
2022-06-02 10:55:18 +00:00
# Logical Functions
2023-04-19 18:51:38 +00:00
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`.
2023-04-19 18:51:38 +00:00
Zero as an argument is considered `false`, non-zero values are considered `true`.
2022-06-02 10:55:18 +00:00
## and
2023-04-19 18:51:38 +00:00
Calculates the logical conjunction between two or more values.
2021-06-28 18:25:52 +00:00
**Syntax**
``` sql
and(val1, val2...)
```
2023-04-19 18:51:38 +00:00
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).
2021-06-28 18:25:52 +00:00
**Arguments**
- `val1, val2, ...` — List of at least two values. [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).
2021-06-28 18:25:52 +00:00
**Returned value**
2023-04-19 18:51:38 +00:00
- `0`, if there at least one argument evaluates to `false`,
- `NULL`, if no argumetn evaluates to `false` and at least one argument is `NULL`,
- `1`, otherwise.
2021-06-28 18:25:52 +00:00
2021-06-28 18:44:59 +00:00
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)).
2021-06-28 18:25:52 +00:00
**Example**
``` sql
SELECT and(0, 1, -2);
```
Result:
``` text
┌─and(0, 1, -2)─┐
│ 0 │
└───────────────┘
```
With `NULL`:
``` sql
SELECT and(NULL, 1, 10, -2);
```
Result:
``` text
┌─and(NULL, 1, 10, -2)─┐
│ ᴺᵁᴸᴸ │
└──────────────────────┘
```
2022-06-02 10:55:18 +00:00
## or
2021-06-28 18:25:52 +00:00
2023-04-19 18:51:38 +00:00
Calculates the logical disjunction between two or more values.
2021-06-28 18:25:52 +00:00
**Syntax**
``` sql
or(val1, val2...)
2021-06-28 18:25:52 +00:00
```
2023-04-19 18:51:38 +00:00
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).
2021-06-28 18:25:52 +00:00
**Arguments**
- `val1, val2, ...` — List of at least two values. [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).
2021-06-28 18:25:52 +00:00
**Returned value**
2023-04-19 18:51:38 +00:00
- `1`, if at least one argument evalutes to `true`,
- `0`, if all arguments evaluate to `false`,
- `NULL`, if all arguments evaluate to `false` and at least one argument is `NULL`.
2021-06-28 18:25:52 +00:00
2021-06-28 18:44:59 +00:00
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)).
2021-06-28 18:25:52 +00:00
**Example**
``` sql
SELECT or(1, 0, 0, 2, NULL);
```
Result:
``` text
┌─or(1, 0, 0, 2, NULL)─┐
│ 1 │
└──────────────────────┘
```
With `NULL`:
``` sql
SELECT or(0, NULL);
```
Result:
``` text
┌─or(0, NULL)─┐
│ ᴺᵁᴸᴸ │
└─────────────┘
```
2022-06-02 10:55:18 +00:00
## not
2021-06-28 18:25:52 +00:00
2023-04-19 18:51:38 +00:00
Calculates logical negation of a value.
2021-06-28 18:25:52 +00:00
**Syntax**
``` sql
not(val);
```
2023-04-19 18:51:38 +00:00
Alias: The [Negation Operator](../../sql-reference/operators/index.md#logical-negation-operator).
2021-06-28 18:25:52 +00:00
**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).
2021-06-28 18:25:52 +00:00
**Returned value**
2023-04-19 18:51:38 +00:00
- `1`, if `val` evaluates to `false`,
- `0`, if `val` evaluates to `true`,
- `NULL`, if `val` is `NULL`.
2021-06-28 18:25:52 +00:00
2021-06-28 18:44:59 +00:00
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)).
2021-06-28 18:25:52 +00:00
**Example**
``` sql
SELECT NOT(1);
```
Result:
``` test
┌─not(1)─┐
│ 0 │
└────────┘
```
2018-04-23 06:20:21 +00:00
2022-06-02 10:55:18 +00:00
## xor
2023-04-19 18:51:38 +00:00
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.
2021-06-28 18:25:52 +00:00
**Syntax**
``` sql
xor(val1, val2...)
```
**Arguments**
- `val1, val2, ...` — List of at least two values. [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).
2021-06-28 18:25:52 +00:00
**Returned value**
2023-04-19 18:51:38 +00:00
- `1`, for two values: if one of the values evaluates to `false` and other does not,
- `0`, for two values: if both values evalute to `false` or to both `true`,
- `NULL`, if at least one of the inputs is `NULL`
2021-06-28 18:25:52 +00:00
2021-06-28 18:44:59 +00:00
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)).
2021-06-28 18:25:52 +00:00
**Example**
``` sql
SELECT xor(0, 1, 1);
```
Result:
2021-06-28 18:25:52 +00:00
``` text
┌─xor(0, 1, 1)─┐
│ 0 │
└──────────────┘
```