2

I'd really like to know why the IF (previous_foo IS NOT NULL) condition fails below, given that changing this line to IF (previous_foo.total IS NOT NULL) evaluates true.

CREATE OR REPLACE FUNCTION foo_total(the_bar bar)
 RETURNS numeric
 LANGUAGE plpgsql
 STABLE
AS $$
      DECALRE
        previous_foo foo;
      BEGIN
        previous_foo := foo_previous(the_bar);

        IF (previous_foo IS NOT NULL) THEN -- fails
        -- IF (previous_foo.total IS NOT NULL) -- works
          return previous_foo.total;
        END IF;
        RETURN NULL;
      END;
$$;

So how can previous_foo be null, yet previous_foo.total not be null :/

Previous foo is doing something like this FYI

create or replace function previous_foo(the_bar bar) returns foo
    LANGUAGE sql STABLE
    AS $$
      SELECT foo.* FROM foo 
        WHERE -- conditions;
$$;

1 Answer 1

2

This is conforming to the documentation and the SQL standard:

If the expression is row-valued, then IS NULL is true when the row expression itself is null or when all the row's fields are null, while IS NOT NULL is true when the row expression itself is non-null and all the row's fields are non-null.

Must be that some field in the composite is NULL.

This is not the only strange ruling of the standard committee when it comes to NULL…

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Yes ok this makes some "sense". Some of the fields in that row are null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.