- Newest
- Most votes
- Most comments
This appears to be a genuine bug in Amazon Redshift's handling of boolean expressions in SELECT lists versus WHERE clauses. Your analysis is correct: when a column is NULL, the expression x IS NOT NULL AND x <> '' should evaluate to FALSE (since FALSE AND <anything> is always FALSE in standard SQL three-valued logic), but Redshift is incorrectly returning NULL in certain contexts.
In Amazon Redshift, NULL values represent missing, unknown, or not applicable data. Any arithmetic expression containing a null evaluates to null, and all operators return null when given a null argument. However, the AND operator has special semantics: when one operand is FALSE, the result should be FALSE regardless of the other operand, even if it's NULL.
Your reproduction clearly demonstrates the inconsistency: the same predicate behaves correctly in a WHERE clause (filtering as expected) but incorrectly in SELECT list expressions, CASE expressions, and aggregates like BOOL_OR. This suggests the query optimizer may be incorrectly simplifying the expression when it appears in a projection context, possibly by removing the IS NOT NULL check as redundant when it sees a comparison operator on the same column.
Your workarounds are solid approaches until this is resolved:
- Using
COALESCE(x, '') <> ''avoids the NULL entirely - Wrapping in a CASE expression forces correct evaluation
- Using the logically equivalent
NOT (x IS NULL OR x = '')avoids the problematic pattern
This is the type of issue that should be reported through the AWS Support Center so the Redshift team can investigate and prioritize a fix. The behavior violates SQL standard three-valued logic and creates unexpected results when the same predicate is used in different query contexts.
Sources
Nulls - Amazon Redshift
answered 14 days ago
Your repro looks like a real bug, and the reasoning holds under standard three-valued logic: when x is NULL, x IS NOT NULL is FALSE, and FALSE AND <anything> is FALSE, including FALSE AND NULL. So x IS NOT NULL AND x <> '' must never return NULL. The WHERE clause filters correctly because WHERE treats NULL and FALSE the same way; in the SELECT list the difference shows.
A few things to make the Support case airtight:
-
Rule out the result cache:
SET enable_result_cache_for_session TO off;Then rerun the repro. If it still returns NULL, the bug is in planning/execution.
-
Show the rewritten expression:
EXPLAIN VERBOSE SELECT x IS NOT NULL AND x <> '' FROM nullbug;If
IS NOT NULLis missing from the projection, that supports your guess that the planner drops it as redundant (x <> ''already implies non-NULL in a filter, but not in a projection). -
Include in the case: the cluster version (1.0.520420), the full repro script, the cache-off result, the EXPLAIN VERBOSE output, and the variants that return correctly (constant, COALESCE, CASE,
x IS NULL OR x = '', two different columns). That last list is what the engineering team will use to narrow it down.
While waiting for a fix, the risk is in SELECT lists and in expressions stored in columns (CREATE TABLE AS, INSERT ... SELECT, flags computed in ETL), not in WHERE.
-
Search your SQL and dbt models for the pattern:
IS NOT NULL AND .* <> ''(and!= ''). -
Rewrite to a form that does not depend on the optimization:
COALESCE(x, '') <> '' -- or CASE WHEN x IS NULL THEN FALSE ELSE x <> '' END -
For boolean aggregates like
bool_or, wrap the argument the same way, since a NULL input changes the aggregate result.
answered 8 days ago
Relevant content
asked 5 months ago
