2018-09-04 11:18:59 +00:00
|
|
|
# Functions for working with Nullable aggregates
|
|
|
|
|
|
|
|
## isNull
|
|
|
|
|
2018-12-12 17:28:00 +00:00
|
|
|
Checks whether the argument is [NULL](../syntax.md#null).
|
2018-09-04 11:18:59 +00:00
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
isNull(x)
|
|
|
|
```
|
|
|
|
|
2018-10-19 11:25:22 +00:00
|
|
|
**Parameters**
|
2018-09-04 11:18:59 +00:00
|
|
|
|
|
|
|
- `x` — A value with a non-compound data type.
|
|
|
|
|
|
|
|
**Returned value**
|
|
|
|
|
|
|
|
- `1` if `x` is `NULL`.
|
|
|
|
- `0` if `x` is not `NULL`.
|
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
|
|
|
Input table
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─x─┬────y─┐
|
|
|
|
│ 1 │ ᴺᵁᴸᴸ │
|
|
|
|
│ 2 │ 3 │
|
|
|
|
└───┴──────┘
|
|
|
|
```
|
|
|
|
|
|
|
|
Query
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
|
|
|
SELECT x FROM t_null WHERE isNull(y)
|
2018-09-04 11:18:59 +00:00
|
|
|
```
|
2019-09-23 15:31:46 +00:00
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─x─┐
|
|
|
|
│ 1 │
|
|
|
|
└───┘
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## isNotNull
|
|
|
|
|
2018-12-12 17:28:00 +00:00
|
|
|
Checks whether the argument is [NULL](../syntax.md#null).
|
2018-09-04 11:18:59 +00:00
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
isNotNull(x)
|
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `x` — A value with a non-compound data type.
|
|
|
|
|
|
|
|
**Returned value**
|
|
|
|
|
|
|
|
- `0` if `x` is `NULL`.
|
|
|
|
- `1` if `x` is not `NULL`.
|
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
|
|
|
Input table
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─x─┬────y─┐
|
|
|
|
│ 1 │ ᴺᵁᴸᴸ │
|
|
|
|
│ 2 │ 3 │
|
|
|
|
└───┴──────┘
|
|
|
|
```
|
|
|
|
|
|
|
|
Query
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
|
|
|
SELECT x FROM t_null WHERE isNotNull(y)
|
2018-09-04 11:18:59 +00:00
|
|
|
```
|
2019-09-23 15:31:46 +00:00
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─x─┐
|
|
|
|
│ 2 │
|
|
|
|
└───┘
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## coalesce
|
|
|
|
|
|
|
|
Checks from left to right whether `NULL` arguments were passed and returns the first non-`NULL` argument.
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
coalesce(x,...)
|
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- Any number of parameters of a non-compound type. All parameters must be compatible by data type.
|
|
|
|
|
|
|
|
**Returned values**
|
|
|
|
|
2018-12-25 15:25:43 +00:00
|
|
|
- The first non-`NULL` argument.
|
2018-09-04 11:18:59 +00:00
|
|
|
- `NULL`, if all arguments are `NULL`.
|
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
|
|
|
Consider a list of contacts that may specify multiple ways to contact a customer.
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─name─────┬─mail─┬─phone─────┬──icq─┐
|
|
|
|
│ client 1 │ ᴺᵁᴸᴸ │ 123-45-67 │ 123 │
|
|
|
|
│ client 2 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
|
|
|
|
└──────────┴──────┴───────────┴──────┘
|
|
|
|
```
|
|
|
|
|
|
|
|
The `mail` and `phone` fields are of type String, but the `icq` field is `UInt32`, so it needs to be converted to `String`.
|
|
|
|
|
|
|
|
Get the first available contact method for the customer from the contact list:
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
|
|
|
SELECT coalesce(mail, phone, CAST(icq,'Nullable(String)')) FROM aBook
|
2018-09-04 11:18:59 +00:00
|
|
|
```
|
2019-09-23 15:31:46 +00:00
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─name─────┬─coalesce(mail, phone, CAST(icq, 'Nullable(String)'))─┐
|
|
|
|
│ client 1 │ 123-45-67 │
|
|
|
|
│ client 2 │ ᴺᵁᴸᴸ │
|
|
|
|
└──────────┴──────────────────────────────────────────────────────┘
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## ifNull
|
|
|
|
|
|
|
|
Returns an alternative value if the main argument is `NULL`.
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
ifNull(x,alt)
|
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `x` — The value to check for `NULL`.
|
|
|
|
- `alt` — The value that the function returns if `x` is `NULL`.
|
|
|
|
|
|
|
|
**Returned values**
|
|
|
|
|
|
|
|
- The value `x`, if `x` is not `NULL`.
|
|
|
|
- The value `alt`, if `x` is `NULL`.
|
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
SELECT ifNull('a', 'b')
|
2019-09-23 15:31:46 +00:00
|
|
|
```
|
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─ifNull('a', 'b')─┐
|
|
|
|
│ a │
|
|
|
|
└──────────────────┘
|
|
|
|
```
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
SELECT ifNull(NULL, 'b')
|
2019-09-23 15:31:46 +00:00
|
|
|
```
|
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─ifNull(NULL, 'b')─┐
|
|
|
|
│ b │
|
|
|
|
└───────────────────┘
|
|
|
|
```
|
|
|
|
|
|
|
|
## nullIf
|
|
|
|
|
|
|
|
Returns `NULL` if the arguments are equal.
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
nullIf(x, y)
|
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
`x`, `y` — Values for comparison. They must be compatible types, or ClickHouse will generate an exception.
|
|
|
|
|
|
|
|
**Returned values**
|
|
|
|
|
|
|
|
- `NULL`, if the arguments are equal.
|
|
|
|
- The `x` value, if the arguments are not equal.
|
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
SELECT nullIf(1, 1)
|
2019-09-23 15:31:46 +00:00
|
|
|
```
|
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─nullIf(1, 1)─┐
|
|
|
|
│ ᴺᵁᴸᴸ │
|
|
|
|
└──────────────┘
|
|
|
|
```
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
SELECT nullIf(1, 2)
|
2019-09-23 15:31:46 +00:00
|
|
|
```
|
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─nullIf(1, 2)─┐
|
|
|
|
│ 1 │
|
|
|
|
└──────────────┘
|
|
|
|
```
|
|
|
|
|
|
|
|
## assumeNotNull
|
|
|
|
|
2018-12-25 15:25:43 +00:00
|
|
|
Results in a value of type [Nullable](../../data_types/nullable.md) for a non- `Nullable`, if the value is not `NULL`.
|
2018-09-04 11:18:59 +00:00
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
assumeNotNull(x)
|
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `x` — The original value.
|
|
|
|
|
|
|
|
**Returned values**
|
|
|
|
|
|
|
|
- The original value from the non-`Nullable` type, if it is not `NULL`.
|
|
|
|
- The default value for the non-`Nullable` type if the original value was `NULL`.
|
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
|
|
|
Consider the `t_null` table.
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
SHOW CREATE TABLE t_null
|
2019-09-23 15:31:46 +00:00
|
|
|
```
|
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─statement─────────────────────────────────────────────────────────────────┐
|
|
|
|
│ CREATE TABLE default.t_null ( x Int8, y Nullable(Int8)) ENGINE = TinyLog │
|
|
|
|
└───────────────────────────────────────────────────────────────────────────┘
|
|
|
|
```
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─x─┬────y─┐
|
|
|
|
│ 1 │ ᴺᵁᴸᴸ │
|
|
|
|
│ 2 │ 3 │
|
|
|
|
└───┴──────┘
|
|
|
|
```
|
|
|
|
|
2019-08-19 09:57:53 +00:00
|
|
|
Apply the `assumeNotNull` function to the `y` column.
|
2018-09-04 11:18:59 +00:00
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
SELECT assumeNotNull(y) FROM t_null
|
2019-09-23 15:31:46 +00:00
|
|
|
```
|
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─assumeNotNull(y)─┐
|
|
|
|
│ 0 │
|
|
|
|
│ 3 │
|
|
|
|
└──────────────────┘
|
|
|
|
```
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
SELECT toTypeName(assumeNotNull(y)) FROM t_null
|
2019-09-23 15:31:46 +00:00
|
|
|
```
|
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─toTypeName(assumeNotNull(y))─┐
|
|
|
|
│ Int8 │
|
|
|
|
│ Int8 │
|
|
|
|
└──────────────────────────────┘
|
|
|
|
```
|
|
|
|
|
|
|
|
## toNullable
|
|
|
|
|
|
|
|
Converts the argument type to `Nullable`.
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
toNullable(x)
|
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `x` — The value of any non-compound type.
|
|
|
|
|
|
|
|
**Returned value**
|
|
|
|
|
2019-05-05 17:38:05 +00:00
|
|
|
- The input value with a `Nullable` type.
|
2018-09-04 11:18:59 +00:00
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
2019-09-23 15:31:46 +00:00
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
SELECT toTypeName(10)
|
2019-09-23 15:31:46 +00:00
|
|
|
```
|
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─toTypeName(10)─┐
|
|
|
|
│ UInt8 │
|
|
|
|
└────────────────┘
|
2019-09-23 15:31:46 +00:00
|
|
|
```
|
|
|
|
```sql
|
2018-09-04 11:18:59 +00:00
|
|
|
SELECT toTypeName(toNullable(10))
|
2019-09-23 15:31:46 +00:00
|
|
|
```
|
|
|
|
```text
|
2018-09-04 11:18:59 +00:00
|
|
|
┌─toTypeName(toNullable(10))─┐
|
|
|
|
│ Nullable(UInt8) │
|
|
|
|
└────────────────────────────┘
|
|
|
|
```
|
|
|
|
|
2018-10-16 10:47:17 +00:00
|
|
|
|
2020-01-30 10:34:55 +00:00
|
|
|
[Original article](https://clickhouse.tech/docs/en/query_language/functions/functions_for_nulls/) <!--hide-->
|