Skip to content

[Bug Report] Redshift returns NULL for `x IS NOT NULL AND x <> ''` when x is NULL

-1

Observed on Redshift 1.0.520420.

Summary

When x is NULL, Redshift evaluates x IS NOT NULL AND x <> '' to NULL in a SELECT list, CASE expression, or aggregate. It should be FALSE because FALSE AND <anything> is FALSE in standard SQL and in PostgreSQL. The same predicate in a WHERE clause gives the right answer. Please confirm whether this is a known issue and whether a fix is planned.

Reproduction

Run as one session on a fresh temp table:

create temp table nullbug (a varchar(20));
insert into nullbug values (null), (null), ('x'), ('');

-- 1. Per-row value of the predicate
select a is null as a_null,
       (a is not null and a <> '') is null as and_is_null,
       count(*)
from nullbug group by 1, 2 order by 1;

-- 2. Same predicate under NOT, in WHERE vs in SELECT
select (select count(*) from nullbug where not (a is not null and a <> '')) as where_not,
       count(case when not (a is not null and a <> '') then 1 end)        as select_not
from nullbug;

-- 3. Inside BOOL_OR
select bool_or(a is not null and a <> '') is null as bool_or_is_null
from nullbug where a is null;

drop table nullbug;

Expected vs actual

QueryExpectedActual
1, rows where a is NULLand_is_null = false (value is FALSE)and_is_null = true for both NULL rows
2where_not = 3, select_not = 3where_not = 3, select_not = 1
3falsetrue

Query 2 shows the problem best. The same predicate gives 3 in WHERE and 1 in SELECT on the same 4 rows.

What we narrowed down

  • It needs a stored column. select ... from (select null::varchar a union all select null::varchar) t gives the correct FALSE, so constant folding hides it.
  • Swapping the order (x <> '' AND x IS NOT NULL) still returns NULL.
  • These forms give the correct result:
    • COALESCE(x, '') <> ''
    • CASE WHEN x IS NOT NULL AND x <> '' THEN true ELSE false END
    • x IS NULL OR x = ''
    • y IS NULL AND x <> '' (two different columns)
  • Our guess is that the planner treats x IS NOT NULL as redundant next to a comparison on x and drops it. That would be safe in WHERE but changes the result of a boolean value in a projection.

asked 14 days ago96 views

2 Answers
0

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

AWS
SUPPORT ENGINEER

reviewed 14 days ago

EXPERT

reviewed 14 days ago

0

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:

  1. 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.

  2. Show the rewritten expression:

    EXPLAIN VERBOSE
    SELECT x IS NOT NULL AND x <> '' FROM nullbug;

    If IS NOT NULL is 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).

  3. 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

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.