NULL Semantics

Description

A table consists of a set of rows and each row contains a set of columns. A column is associated with a data type and represents a specific attribute of an entity (for example, age is a column of an entity called person). Sometimes, the value of a column specific to a row is not known at the time the row comes into existence. In SQL, such values are represented as NULL. This section details the semantics of NULL values handling in various operators, expressions and other SQL constructs.

  1. Null handling in comparison operators
  2. Null handling in Logical operators
  3. Null handling in Expressions
    1. Null handling in null-intolerant expressions
    2. Null handling Expressions that can process null value operands
    3. Null handling in built-in aggregate expressions
  4. Null handling in WHERE, HAVING and JOIN conditions
  5. Null handling in GROUP BY and DISTINCT
  6. Null handling in ORDER BY
  7. Null handling in UNION, INTERSECT, EXCEPT
  8. Null handling in EXISTS and NOT EXISTS subquery
  9. Null handling in IN and NOT IN subquery

The following illustrates the schema layout and data of a table named person. The data contains NULL values in the age column and this table will be used in various examples in the sections below.
TABLE: person

IdNameAge
100Joe30
200MarryNULL
300Mike18
400Fred50
500AlbertNULL
600Michelle30
700Dan50

Comparison Operators

Apache spark supports the standard comparison operators such as ‘>’, ‘>=’, ‘=’, ‘<’ and ‘<=’. The result of these operators is unknown or NULL when one of the operands or both the operands are unknown or NULL. In order to compare the NULL values for equality, Spark provides a null-safe equal operator (‘<=>’), which returns False when one of the operand is NULL and returns ‘True when both the operands are NULL. The following table illustrates the behaviour of comparison operators when one or both operands are NULL`:

Left OperandRight Operand>>==<<=<=>
NULLAny valueNULLNULLNULLNULLNULLFalse
Any valueNULLNULLNULLNULLNULLNULLFalse
NULLNULLNULLNULLNULLNULLNULLTrue

Examples

  1. -- Normal comparison operators return `NULL` when one of the operand is `NULL`.
  2. SELECT 5 > null AS expression_output;
  3. +-----------------+
  4. |expression_output|
  5. +-----------------+
  6. | null|
  7. +-----------------+
  8. -- Normal comparison operators return `NULL` when both the operands are `NULL`.
  9. SELECT null = null AS expression_output;
  10. +-----------------+
  11. |expression_output|
  12. +-----------------+
  13. | null|
  14. +-----------------+
  15. -- Null-safe equal operator return `False` when one of the operand is `NULL`
  16. SELECT 5 <=> null AS expression_output;
  17. +-----------------+
  18. |expression_output|
  19. +-----------------+
  20. | false|
  21. +-----------------+
  22. -- Null-safe equal operator return `True` when one of the operand is `NULL`
  23. SELECT NULL <=> NULL;
  24. +-----------------+
  25. |expression_output|
  26. +-----------------+
  27. | true|
  28. +-----------------+

Logical Operators

Spark supports standard logical operators such as AND, OR and NOT. These operators take Boolean expressions as the arguments and return a Boolean value.

The following tables illustrate the behavior of logical operators when one or both operands are NULL.

Left OperandRight OperandORAND
TrueNULLTrueNULL
FalseNULLNULLFalse
NULLTrueTrueNULL
NULLFalseNULLFalse
NULLNULLNULLNULL
operandNOT
NULLNULL

Examples

  1. -- Normal comparison operators return `NULL` when one of the operands is `NULL`.
  2. SELECT (true OR null) AS expression_output;
  3. +-----------------+
  4. |expression_output|
  5. +-----------------+
  6. | true|
  7. +-----------------+
  8. -- Normal comparison operators return `NULL` when both the operands are `NULL`.
  9. SELECT (null OR false) AS expression_output
  10. +-----------------+
  11. |expression_output|
  12. +-----------------+
  13. | null|
  14. +-----------------+
  15. -- Null-safe equal operator returns `False` when one of the operands is `NULL`
  16. SELECT NOT(null) AS expression_output;
  17. +-----------------+
  18. |expression_output|
  19. +-----------------+
  20. | null|
  21. +-----------------+

Expressions

The comparison operators and logical operators are treated as expressions in Spark. Other than these two kinds of expressions, Spark supports other form of expressions such as function expressions, cast expressions, etc. The expressions in Spark can be broadly classified as :

  • Null intolerant expressions
  • Expressions that can process NULL value operands
    • The result of these expressions depends on the expression itself.

Null Intolerant Expressions

Null intolerant expressions return NULL when one or more arguments of expression are NULL and most of the expressions fall in this category.

Examples
  1. SELECT concat('John', null) AS expression_output;
  2. +-----------------+
  3. |expression_output|
  4. +-----------------+
  5. | null|
  6. +-----------------+
  7. SELECT positive(null) AS expression_output;
  8. +-----------------+
  9. |expression_output|
  10. +-----------------+
  11. | null|
  12. +-----------------+
  13. SELECT to_date(null) AS expression_output;
  14. +-----------------+
  15. |expression_output|
  16. +-----------------+
  17. | null|
  18. +-----------------+

Expressions That Can Process Null Value Operands

This class of expressions are designed to handle NULL values. The result of the expressions depends on the expression itself. As an example, function expression isnull returns a true on null input and false on non null input where as function coalesce returns the first non NULL value in its list of operands. However, coalesce returns NULL when all its operands are NULL. Below is an incomplete list of expressions of this category.

  • COALESCE
  • NULLIF
  • IFNULL
  • NVL
  • NVL2
  • ISNAN
  • NANVL
  • ISNULL
  • ISNOTNULL
  • ATLEASTNNONNULLS
  • IN
Examples
  1. SELECT isnull(null) AS expression_output;
  2. +-----------------+
  3. |expression_output|
  4. +-----------------+
  5. | true|
  6. +-----------------+
  7. -- Returns the first occurrence of non `NULL` value.
  8. SELECT coalesce(null, null, 3, null) AS expression_output;
  9. +-----------------+
  10. |expression_output|
  11. +-----------------+
  12. | 3|
  13. +-----------------+
  14. -- Returns `NULL` as all its operands are `NULL`.
  15. SELECT coalesce(null, null, null, null) AS expression_output;
  16. +-----------------+
  17. |expression_output|
  18. +-----------------+
  19. | null|
  20. +-----------------+
  21. SELECT isnan(null) AS expression_output;
  22. +-----------------+
  23. |expression_output|
  24. +-----------------+
  25. | false|
  26. +-----------------+

Builtin Aggregate Expressions

Aggregate functions compute a single result by processing a set of input rows. Below are the rules of how NULL values are handled by aggregate functions.

  • NULL values are ignored from processing by all the aggregate functions.
    • Only exception to this rule is COUNT(*) function.
  • Some aggregate functions return NULL when all input values are NULL or the input data set is empty.
    The list of these functions is:
    • MAX
    • MIN
    • SUM
    • AVG
    • EVERY
    • ANY
    • SOME

Examples

  1. -- `count(*)` does not skip `NULL` values.
  2. SELECT count(*) FROM person;
  3. +--------+
  4. |count(1)|
  5. +--------+
  6. | 7|
  7. +--------+
  8. -- `NULL` values in column `age` are skipped from processing.
  9. SELECT count(age) FROM person;
  10. +----------+
  11. |count(age)|
  12. +----------+
  13. | 5|
  14. +----------+
  15. -- `count(*)` on an empty input set returns 0. This is unlike the other
  16. -- aggregate functions, such as `max`, which return `NULL`.
  17. SELECT count(*) FROM person where 1 = 0;
  18. +--------+
  19. |count(1)|
  20. +--------+
  21. | 0|
  22. +--------+
  23. -- `NULL` values are excluded from computation of maximum value.
  24. SELECT max(age) FROM person;
  25. +--------+
  26. |max(age)|
  27. +--------+
  28. | 50|
  29. +--------+
  30. -- `max` returns `NULL` on an empty input set.
  31. SELECT max(age) FROM person where 1 = 0;
  32. +--------+
  33. |max(age)|
  34. +--------+
  35. | null|
  36. +--------+

Condition Expressions in WHERE, HAVING and JOIN Clauses

WHERE, HAVING operators filter rows based on the user specified condition. A JOIN operator is used to combine rows from two tables based on a join condition. For all the three operators, a condition expression is a boolean expression and can return True, False or Unknown (NULL). They are “satisfied” if the result of the condition is True.

Examples

  1. -- Persons whose age is unknown (`NULL`) are filtered out from the result set.
  2. SELECT * FROM person WHERE age > 0;
  3. +--------+---+
  4. | name|age|
  5. +--------+---+
  6. |Michelle| 30|
  7. | Fred| 50|
  8. | Mike| 18|
  9. | Dan| 50|
  10. | Joe| 30|
  11. +--------+---+
  12. -- `IS NULL` expression is used in disjunction to select the persons
  13. -- with unknown (`NULL`) records.
  14. SELECT * FROM person WHERE age > 0 OR age IS NULL;
  15. +--------+----+
  16. | name| age|
  17. +--------+----+
  18. | Albert|null|
  19. |Michelle| 30|
  20. | Fred| 50|
  21. | Mike| 18|
  22. | Dan| 50|
  23. | Marry|null|
  24. | Joe| 30|
  25. +--------+----+
  26. -- Person with unknown(`NULL`) ages are skipped from processing.
  27. SELECT age, count(*) FROM person GROUP BY age HAVING max(age) > 18;
  28. +---+--------+
  29. |age|count(1)|
  30. +---+--------+
  31. | 50| 2|
  32. | 30| 2|
  33. +---+--------+
  34. -- A self join case with a join condition `p1.age = p2.age AND p1.name = p2.name`.
  35. -- The persons with unknown age (`NULL`) are filtered out by the join operator.
  36. SELECT * FROM person p1, person p2
  37. WHERE p1.age = p2.age
  38. AND p1.name = p2.name;
  39. +--------+---+--------+---+
  40. | name|age| name|age|
  41. +--------+---+--------+---+
  42. |Michelle| 30|Michelle| 30|
  43. | Fred| 50| Fred| 50|
  44. | Mike| 18| Mike| 18|
  45. | Dan| 50| Dan| 50|
  46. | Joe| 30| Joe| 30|
  47. +--------+---+--------+---+
  48. -- The age column from both legs of join are compared using null-safe equal which
  49. -- is why the persons with unknown age (`NULL`) are qualified by the join.
  50. SELECT * FROM person p1, person p2
  51. WHERE p1.age <=> p2.age
  52. AND p1.name = p2.name;
  53. +--------+----+--------+----+
  54. | name| age| name| age|
  55. +--------+----+--------+----+
  56. | Albert|null| Albert|null|
  57. |Michelle| 30|Michelle| 30|
  58. | Fred| 50| Fred| 50|
  59. | Mike| 18| Mike| 18|
  60. | Dan| 50| Dan| 50|
  61. | Marry|null| Marry|null|
  62. | Joe| 30| Joe| 30|
  63. +--------+----+--------+----+

Aggregate Operator (GROUP BY, DISTINCT)

As discussed in the previous section comparison operator, two NULL values are not equal. However, for the purpose of grouping and distinct processing, the two or more values with NULL dataare grouped together into the same bucket. This behaviour is conformant with SQL standard and with other enterprise database management systems.

Examples

  1. -- `NULL` values are put in one bucket in `GROUP BY` processing.
  2. SELECT age, count(*) FROM person GROUP BY age;
  3. +----+--------+
  4. | age|count(1)|
  5. +----+--------+
  6. |null| 2|
  7. | 50| 2|
  8. | 30| 2|
  9. | 18| 1|
  10. +----+--------+
  11. -- All `NULL` ages are considered one distinct value in `DISTINCT` processing.
  12. SELECT DISTINCT age FROM person;
  13. +----+
  14. | age|
  15. +----+
  16. |null|
  17. | 50|
  18. | 30|
  19. | 18|
  20. +----+

Sort Operator (ORDER BY Clause)

Spark SQL supports null ordering specification in ORDER BY clause. Spark processes the ORDER BY clause by placing all the NULL values at first or at last depending on the null ordering specification. By default, all the NULL values are placed at first.

Examples

  1. -- `NULL` values are shown at first and other values
  2. -- are sorted in ascending way.
  3. SELECT age, name FROM person ORDER BY age;
  4. +----+--------+
  5. | age| name|
  6. +----+--------+
  7. |null| Marry|
  8. |null| Albert|
  9. | 18| Mike|
  10. | 30|Michelle|
  11. | 30| Joe|
  12. | 50| Fred|
  13. | 50| Dan|
  14. +----+--------+
  15. -- Column values other than `NULL` are sorted in ascending
  16. -- way and `NULL` values are shown at the last.
  17. SELECT age, name FROM person ORDER BY age NULLS LAST;
  18. +----+--------+
  19. | age| name|
  20. +----+--------+
  21. | 18| Mike|
  22. | 30|Michelle|
  23. | 30| Joe|
  24. | 50| Dan|
  25. | 50| Fred|
  26. |null| Marry|
  27. |null| Albert|
  28. +----+--------+
  29. -- Columns other than `NULL` values are sorted in descending
  30. -- and `NULL` values are shown at the last.
  31. SELECT age, name FROM person ORDER BY age DESC NULLS LAST;
  32. +----+--------+
  33. | age| name|
  34. +----+--------+
  35. | 50| Fred|
  36. | 50| Dan|
  37. | 30|Michelle|
  38. | 30| Joe|
  39. | 18| Mike|
  40. |null| Marry|
  41. |null| Albert|
  42. +----+--------+

Set Operators (UNION, INTERSECT, EXCEPT)

NULL values are compared in a null-safe manner for equality in the context of set operations. That means when comparing rows, two NULL values are considered equal unlike the regular EqualTo(=) operator.

Examples

  1. CREATE VIEW unknown_age SELECT * FROM person WHERE age IS NULL;
  2. -- Only common rows between two legs of `INTERSECT` are in the
  3. -- result set. The comparison between columns of the row are done
  4. -- in a null-safe manner.
  5. SELECT name, age FROM person
  6. INTERSECT
  7. SELECT name, age from unknown_age;
  8. +------+----+
  9. | name| age|
  10. +------+----+
  11. |Albert|null|
  12. | Marry|null|
  13. +------+----+
  14. -- `NULL` values from two legs of the `EXCEPT` are not in output.
  15. -- This basically shows that the comparison happens in a null-safe manner.
  16. SELECT age, name FROM person
  17. EXCEPT
  18. SELECT age FROM unknown_age;
  19. +---+--------+
  20. |age| name|
  21. +---+--------+
  22. | 30| Joe|
  23. | 50| Fred|
  24. | 30|Michelle|
  25. | 18| Mike|
  26. | 50| Dan|
  27. +---+--------+
  28. -- Performs `UNION` operation between two sets of data.
  29. -- The comparison between columns of the row ae done in
  30. -- null-safe manner.
  31. SELECT name, age FROM person
  32. UNION
  33. SELECT name, age FROM unknown_age;
  34. +--------+----+
  35. | name| age|
  36. +--------+----+
  37. | Albert|null|
  38. | Joe| 30|
  39. |Michelle| 30|
  40. | Marry|null|
  41. | Fred| 50|
  42. | Mike| 18|
  43. | Dan| 50|
  44. +--------+----+

EXISTS/NOT EXISTS Subquery

In Spark, EXISTS and NOT EXISTS expressions are allowed inside a WHERE clause. These are boolean expressions which return either TRUE or FALSE. In other words, EXISTS is a membership condition and returns TRUE when the subquery it refers to returns one or more rows. Similarly, NOT EXISTS is a non-membership condition and returns TRUE when no rows or zero rows are returned from the subquery.

These two expressions are not affected by presence of NULL in the result of the subquery. They are normally faster because they can be converted to semijoins / anti-semijoins without special provisions for null awareness.

Examples

  1. -- Even if subquery produces rows with `NULL` values, the `EXISTS` expression
  2. -- evaluates to `TRUE` as the subquery produces 1 row.
  3. SELECT * FROM person WHERE EXISTS (SELECT null);
  4. +--------+----+
  5. | name| age|
  6. +--------+----+
  7. | Albert|null|
  8. |Michelle| 30|
  9. | Fred| 50|
  10. | Mike| 18|
  11. | Dan| 50|
  12. | Marry|null|
  13. | Joe| 30|
  14. +--------+----+
  15. -- `NOT EXISTS` expression returns `FALSE`. It returns `TRUE` only when
  16. -- subquery produces no rows. In this case, it returns 1 row.
  17. SELECT * FROM person WHERE NOT EXISTS (SELECT null);
  18. +----+---+
  19. |name|age|
  20. +----+---+
  21. +----+---+
  22. -- `NOT EXISTS` expression returns `TRUE`.
  23. SELECT * FROM person WHERE NOT EXISTS (SELECT 1 WHERE 1 = 0);
  24. +--------+----+
  25. | name| age|
  26. +--------+----+
  27. | Albert|null|
  28. |Michelle| 30|
  29. | Fred| 50|
  30. | Mike| 18|
  31. | Dan| 50|
  32. | Marry|null|
  33. | Joe| 30|
  34. +--------+----+

IN/NOT IN Subquery

In Spark, IN and NOT IN expressions are allowed inside a WHERE clause of a query. Unlike the EXISTS expression, IN expression can return a TRUE, FALSE or UNKNOWN (NULL) value. Conceptually a IN expression is semantically equivalent to a set of equality condition separated by a disjunctive operator (OR). For example, c1 IN (1, 2, 3) is semantically equivalent to (C1 = 1 OR c1 = 2 OR c1 = 3).

As far as handling NULL values are concerned, the semantics can be deduced from the NULL value handling in comparison operators(=) and logical operators(OR). To summarize, below are the rules for computing the result of an IN expression.

  • TRUE is returned when the non-NULL value in question is found in the list
  • FALSE is returned when the non-NULL value is not found in the list and the list does not contain NULL values
  • UNKNOWN is returned when the value is NULL, or the non-NULL value is not found in the list and the list contains at least one NULL value

NOT IN always returns UNKNOWN when the list contains NULL, regardless of the input value. This is because IN returns UNKNOWN if the value is not in the list containing NULL, and because NOT UNKNOWN is again UNKNOWN.

Examples

  1. -- The subquery has only `NULL` value in its result set. Therefore,
  2. -- the result of `IN` predicate is UNKNOWN.
  3. SELECT * FROM person WHERE age IN (SELECT null);
  4. +----+---+
  5. |name|age|
  6. +----+---+
  7. +----+---+
  8. -- The subquery has `NULL` value in the result set as well as a valid
  9. -- value `50`. Rows with age = 50 are returned.
  10. SELECT * FROM person
  11. WHERE age IN (SELECT age FROM VALUES (50), (null) sub(age));
  12. +----+---+
  13. |name|age|
  14. +----+---+
  15. |Fred| 50|
  16. | Dan| 50|
  17. +----+---+
  18. -- Since subquery has `NULL` value in the result set, the `NOT IN`
  19. -- predicate would return UNKNOWN. Hence, no rows are
  20. -- qualified for this query.
  21. SELECT * FROM person
  22. WHERE age NOT IN (SELECT age FROM VALUES (50), (null) sub(age));
  23. +----+---+
  24. |name|age|
  25. +----+---+
  26. +----+---+