- Check for NULL values
- Convert between Nullable and non-Nullable types
- Perform operations involving potential NULL values
- Missing data
- Optional fields
- Complex data transformations
ClickHouse function reference
isNull
Checks if the argument is NULL. Syntax:x(any non-compound data type): A value of any non-compound data type.
- 1 if
xis NULL. (UInt8) - 0 if
xis not NULL. (UInt8)
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:x(Column): A column.
1ifxallows NULL values. (UInt8)0ifxdoes not allow NULL values. (UInt8)
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:x: A value of any non-compound data type.
- 1 if
xis not NULL. (UInt8) - 0 if
xis NULL. (UInt8)
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:
isNullfunctionIS NOT NULLoperator
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:x(any type): First JOIN key.y(any type): Second JOIN key.
1(true) whenxandyare both NULL or when they are equal.0(false) otherwise.
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:x(any non-compound data type): The value to check.
1ifxis zero or NULL.0otherwise.
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:x, ...: Any number of parameters of non-compound type. All parameters must be of mutually compatible data types.
- The first non-NULL argument.
- NULL, if all arguments are NULL.
- For order 1,
preferred_salsais ‘Spicy’, so it’s returned. - For order 2,
preferred_salsais NULL, sodefault_salsa(‘Medium’) is returned. - For order 3, both
preferred_salsaanddefault_salsaare NULL, so the fallback value ‘Mild’ is returned.
ifNull
Returns an alternative value if the argument is NULL. Syntax:x: The value to check for NULL.alt: The value that the function returns ifxis NULL.
xifxis not NULL.altifxis NULL.
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:x(Any type): First value to compare.y(Any type): Second value to compare.
- NULL if the arguments are equal.
xif the arguments are not equal.
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:x(Nullable): The original value of Nullable type.
- The input value as non-Nullable type, if it is not NULL.
- An arbitrary value, if the input value is NULL.
assumeNotNull(spice_level)converts the Nullable spice level to a non-Nullable value.- For the ‘Mild Surprise’ taco, if the original
spice_levelwas 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:x(non-compound type): A value of non-compound type.
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.