Nullable functions
Handle NULL values in queries.
Nullable functions help you handle NULL values effectively in your queries. These functions enable you to:
- Check for NULL values
- Convert between Nullable and non-Nullable types
- Perform operations involving potential NULL values
Understanding and using these functions can significantly improve your data processing capabilities, especially when dealing with:
- Missing data
- Optional fields
- Complex data transformations
ClickHouse function reference
isNull
Checks if the argument is NULL.
Syntax:
Arguments:
x
(any non-compound data type): A value of any non-compound data type.
Returns:
- 1 if
x
is NULL. (UInt8
) - 0 if
x
is not NULL. (UInt8
)
Example:
Result:
In this example, isNull
checks if the special_sauce
column contains NULL values. The result is 1 for the Veggie Delight taco, indicating it has no special sauce.
This function is equivalent to the IS NULL
operator in SQL.
isNullable
Checks if a column allows NULL values.
Syntax:
Arguments:
x
(Column
): A column.
Returns:
1
ifx
allows NULL values. (UInt8
)0
ifx
does not allow NULL values. (UInt8
)
Example:
Result:
In this example, isNullable
checks whether each column in the taco_orders
table allows NULL values. The special_request
column is defined as Nullable(String)
, so it returns 1
, while the other columns return 0
.
This function is useful for inspecting table schemas and understanding which columns can contain NULL values.
isNotNull
Checks if the argument is not NULL.
Syntax:
Arguments:
x
: A value of any non-compound data type.
Returns:
- 1 if
x
is not NULL. (UInt8
) - 0 if
x
is NULL. (UInt8
)
Example:
Result:
In this example, isNotNull
checks if each taco has a special sauce. The result 1
indicates the presence of a special sauce, while 0
indicates its absence (NULL value).
This function is often used in WHERE
clauses to filter out NULL values or in conditional expressions where NULL handling is important.
See Also:
isNull
functionIS NOT NULL
operator
isNotDistinctFrom
Performs a null-safe comparison. This function is primarily used internally for comparing JOIN keys that may contain NULL values in the JOIN ON section.
Syntax:
Arguments:
x
(any type): First JOIN key.y
(any type): Second JOIN key.
Returns:
1
(true) whenx
andy
are both NULL or when they are equal.0
(false) otherwise.
Example:
Result:
This example demonstrates how isNotDistinctFrom
handles various comparisons:
- NULL values are considered equal to each other.
- A non-NULL value is not equal to NULL.
- Equal non-NULL values return true.
- Equal strings also return true.
This function is primarily used internally for JOIN operations. It’s not recommended to use it directly in queries unless you have a specific need for null-safe comparisons.
isZeroOrNull
Checks if a value is zero or NULL.
Syntax:
Arguments:
x
(any non-compound data type): The value to check.
Returns:
1
ifx
is zero or NULL.0
otherwise.
Example:
Result:
In this example, isZeroOrNull
checks if each taco_price
is either zero (free) or NULL (unknown). The result 1
indicates a free taco or unknown price, while 0
indicates a non-zero, known price.
This function is useful for identifying items that are either free or have missing price information in a single operation.
coalesce
Returns the first non-NULL argument.
Syntax:
Arguments:
x, ...
: Any number of parameters of non-compound type. All parameters must be of mutually compatible data types.
Returns:
- The first non-NULL argument.
- NULL, if all arguments are NULL.
Example:
Result:
In this example:
- For order 1,
preferred_salsa
is ‘Spicy’, so it’s returned. - For order 2,
preferred_salsa
is NULL, sodefault_salsa
(‘Medium’) is returned. - For order 3, both
preferred_salsa
anddefault_salsa
are NULL, so the fallback value ‘Mild’ is returned.
This function is particularly useful for providing default values or fallbacks when dealing with potentially NULL fields in your data.
ifNull
Returns an alternative value if the argument is NULL.
Syntax:
Arguments:
x
: The value to check for NULL.alt
: The value that the function returns ifx
is NULL.
Returns:
x
ifx
is not NULL.alt
ifx
is NULL.
Example:
Result:
In this example, ifNull
checks if special_instructions
is NULL. If it is, it returns the default text ‘No special instructions’. This is useful for providing default values or handling missing data in taco orders.
nullIf
Returns NULL if both arguments are equal.
Syntax:
Arguments:
x
(Any type): First value to compare.y
(Any type): Second value to compare.
Both arguments must be of compatible types.
Returns:
- NULL if the arguments are equal.
x
if the arguments are not equal.
Example:
Result:
In this example, nullIf
returns NULL for tacos priced at $5.99, effectively marking them as discounted items. For tacos with different prices, it returns the original price.
This function is useful for handling special cases in data processing, such as identifying default or standard values that need to be treated differently.
assumeNotNull
Returns the corresponding non-Nullable value for a value of Nullable type. If the original value is NULL, an arbitrary result can be returned.
Syntax:
Arguments:
x
(Nullable): The original value of Nullable type.
Returns:
- The input value as non-Nullable type, if it is not NULL.
- An arbitrary value, if the input value is NULL.
Example:
Result:
In this example:
assumeNotNull(spice_level)
converts the Nullable spice level to a non-Nullable value.- For the ‘Mild Surprise’ taco, if the original
spice_level
was NULL, an arbitrary value (0 in this case) is returned.
Use assumeNotNull
with caution. It’s designed for optimizing performance in cases where you’re certain that a value isn’t NULL. Incorrect use can lead to unexpected results.
toNullable
Converts the argument type to Nullable.
Syntax:
Arguments:
x
(non-compound type): A value of non-compound type.
Returns:
The input value but of Nullable type.
Example:
Result:
In this example, we convert the taco_price
column to a Nullable type. This allows the column to contain NULL values, which can be useful when dealing with missing or unknown prices in the taco menu.
The toNullable
function is particularly useful when you need to combine non-Nullable and Nullable columns in operations or when preparing data for joins where one side might contain NULL values.
Was this page helpful?