pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #6064: != NULL, <> NULL do not work
4+ messages / 4 participants
[nested] [flat]

* BUG #6064: != NULL, <> NULL do not work
@ 2011-06-17 07:39  Michael Pilling <Michael.Pilling@dsto.defence.gov.au>
  0 siblings, 2 replies; 4+ messages in thread

From: Michael Pilling @ 2011-06-17 07:39 UTC (permalink / raw)
  To: pgsql-bugs


The following bug has been logged online:

Bug reference:      6064
Logged by:          Michael Pilling
Email address:      Michael.Pilling@dsto.defence.gov.au
PostgreSQL version: PostgreSQL 9.0
Operating system:   Windows XP (server) Ubuntu 10.4 (Client)
Description:        != NULL, <> NULL do not work
Details: 

Server version was PostgreSQL 9.0.1, compiled by Visual C++ build 1500,
32-bit running on Windows XP 32 bit.

It is arguable whether this bug is in the documentation, parser or
implementation. Personally I think it is in the implementation.

A reasonable programmer would expect != NULL, <> NULL and IS NOT NULL to be
synonyms. However IS NOT NULL works and the others don't.

At the very least the documentation for comparison operators should state
that != and <> will not work with NULL but this would be an obscure fix. 
Ideally the compiler would implement != NULL and <> NULL like it implements
IS NOT NULL, failing that the parser should at least flag the combinations
with != and <> as syntax or semantic errors.

Reproducing the bug:

Execute the following code:


DROP TABLE example;

CREATE TABLE example (				
	id		SERIAL PRIMARY KEY,
	name		varchar(40),
	content		varchar(40)
);

INSERT INTO example ( name, content ) VALUES ( 'Test 1', 'Test 1' );
INSERT INTO example ( name ) VALUES ( 'Test 2' );

CREATE OR REPLACE FUNCTION  show_problem() RETURNS SETOF example AS

$$
DECLARE
  result_name varchar(40);
  result_content varchar(40);
BEGIN
    SELECT example.name, example.content INTO result_name, result_content
FROM example WHERE id=1;
    IF result_content != NULL THEN
    	RAISE NOTICE '!= THEN part id=1';
    ELSE
    	RAISE NOTICE '!= ELSE part id=1';
    END IF;

    SELECT example.name, example.content INTO result_name, result_content
FROM example WHERE id=2;
    IF result_content != NULL THEN
    	RAISE NOTICE '!= THEN part id=2';
    ELSE
    	RAISE NOTICE '!= ELSE part id=2';
    END IF;

    SELECT example.name, example.content INTO result_name, result_content
FROM example WHERE id=1;
    IF result_content <> NULL THEN
    	RAISE NOTICE '<> THEN part id=1';
    ELSE
    	RAISE NOTICE '<> ELSE part id=1';
    END IF;

    SELECT example.name, example.content INTO result_name, result_content
FROM example WHERE id=2;
    IF result_content <> NULL THEN
    	RAISE NOTICE '<> THEN part id=2';
    ELSE
    	RAISE NOTICE '<> ELSE part id=2';
    END IF;
    SELECT example.name, example.content INTO result_name, result_content
FROM example WHERE id=1;
    IF result_content IS NOT NULL THEN
    	RAISE NOTICE 'IS NOT THEN part id=1';
    ELSE
    	RAISE NOTICE 'IS NOT ELSE part id=1';
    END IF;

    SELECT example.name, example.content INTO result_name, result_content
FROM example WHERE id=2;
    IF result_content IS NOT NULL THEN
    	RAISE NOTICE 'IS NOT THEN part id=2';
    ELSE
    	RAISE NOTICE 'IS NOT ELSE part id=2';
    END IF;
RETURN QUERY Select * from example;
RETURN;
END;
$$ LANGUAGE plpgsql;

select * from show_problem();

The last two NOTICEs are what I would regard to be correct. The if statement
has executed according to whether the data was NULL or not. For != and <>
the IF statements always execute the ELSE part regardless of the data
value.

Regards,
Michael



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #6064: != NULL, <> NULL do not work
@ 2011-06-17 07:43  Abel Abraham Camarillo Ojeda <acamari@verlet.org>
  parent: Michael Pilling <Michael.Pilling@dsto.defence.gov.au>
  1 sibling, 0 replies; 4+ messages in thread

From: Abel Abraham Camarillo Ojeda @ 2011-06-17 07:43 UTC (permalink / raw)
  To: Michael Pilling <Michael.Pilling@dsto.defence.gov.au>; +Cc: pgsql-bugs

Do not write expression = NULL because NULL is not "equal to" NULL. (The
null value represents an unknown value, and it is not known whether two
unknown values are equal.) This behavior conforms to the SQL standard.

http://www.postgresql.org/docs/9.1/static/functions-comparison.html


On Fri, Jun 17, 2011 at 2:39 AM, Michael Pilling
<Michael.Pilling@dsto.defence.gov.au> wrote:
>
> The following bug has been logged online:
>
> Bug reference:      6064
> Logged by:          Michael Pilling
> Email address:      Michael.Pilling@dsto.defence.gov.au
> PostgreSQL version: PostgreSQL 9.0
> Operating system:   Windows XP (server) Ubuntu 10.4 (Client)
> Description:        != NULL, <> NULL do not work
> Details:
>
> Server version was PostgreSQL 9.0.1, compiled by Visual C++ build 1500,
> 32-bit running on Windows XP 32 bit.
>
> It is arguable whether this bug is in the documentation, parser or
> implementation. Personally I think it is in the implementation.
>
> A reasonable programmer would expect != NULL, <> NULL and IS NOT NULL to be
> synonyms. However IS NOT NULL works and the others don't.
>
> At the very least the documentation for comparison operators should state
> that != and <> will not work with NULL but this would be an obscure fix.
> Ideally the compiler would implement != NULL and <> NULL like it implements
> IS NOT NULL, failing that the parser should at least flag the combinations
> with != and <> as syntax or semantic errors.
>
> Reproducing the bug:
>
> Execute the following code:
>
>
> DROP TABLE example;
>
> CREATE TABLE example (
>        id              SERIAL PRIMARY KEY,
>        name            varchar(40),
>        content         varchar(40)
> );
>
> INSERT INTO example ( name, content ) VALUES ( 'Test 1', 'Test 1' );
> INSERT INTO example ( name ) VALUES ( 'Test 2' );
>
> CREATE OR REPLACE FUNCTION  show_problem() RETURNS SETOF example AS
>
> $$
> DECLARE
>  result_name varchar(40);
>  result_content varchar(40);
> BEGIN
>    SELECT example.name, example.content INTO result_name, result_content
> FROM example WHERE id=1;
>    IF result_content != NULL THEN
>        RAISE NOTICE '!= THEN part id=1';
>    ELSE
>        RAISE NOTICE '!= ELSE part id=1';
>    END IF;
>
>    SELECT example.name, example.content INTO result_name, result_content
> FROM example WHERE id=2;
>    IF result_content != NULL THEN
>        RAISE NOTICE '!= THEN part id=2';
>    ELSE
>        RAISE NOTICE '!= ELSE part id=2';
>    END IF;
>
>    SELECT example.name, example.content INTO result_name, result_content
> FROM example WHERE id=1;
>    IF result_content <> NULL THEN
>        RAISE NOTICE '<> THEN part id=1';
>    ELSE
>        RAISE NOTICE '<> ELSE part id=1';
>    END IF;
>
>    SELECT example.name, example.content INTO result_name, result_content
> FROM example WHERE id=2;
>    IF result_content <> NULL THEN
>        RAISE NOTICE '<> THEN part id=2';
>    ELSE
>        RAISE NOTICE '<> ELSE part id=2';
>    END IF;
>    SELECT example.name, example.content INTO result_name, result_content
> FROM example WHERE id=1;
>    IF result_content IS NOT NULL THEN
>        RAISE NOTICE 'IS NOT THEN part id=1';
>    ELSE
>        RAISE NOTICE 'IS NOT ELSE part id=1';
>    END IF;
>
>    SELECT example.name, example.content INTO result_name, result_content
> FROM example WHERE id=2;
>    IF result_content IS NOT NULL THEN
>        RAISE NOTICE 'IS NOT THEN part id=2';
>    ELSE
>        RAISE NOTICE 'IS NOT ELSE part id=2';
>    END IF;
> RETURN QUERY Select * from example;
> RETURN;
> END;
> $$ LANGUAGE plpgsql;
>
> select * from show_problem();
>
> The last two NOTICEs are what I would regard to be correct. The if statement
> has executed according to whether the data was NULL or not. For != and <>
> the IF statements always execute the ELSE part regardless of the data
> value.
>
> Regards,
> Michael
>
> --
> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-bugs
>



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #6064: != NULL, <> NULL do not work
@ 2011-06-17 14:20  Kevin Grittner <Kevin.Grittner@wicourts.gov>
  parent: Michael Pilling <Michael.Pilling@dsto.defence.gov.au>
  1 sibling, 1 reply; 4+ messages in thread

From: Kevin Grittner @ 2011-06-17 14:20 UTC (permalink / raw)
  To: Michael Pilling <Michael.Pilling@dsto.defence.gov.au>; pgsql-bugs

"Michael Pilling" <Michael.Pilling@dsto.defence.gov.au> wrote:
 
> A reasonable programmer would expect != NULL, <> NULL and IS NOT
> NULL to be synonyms.
 
Only if that programmer was not aware of the SQL standard and had
not worked much with a standard-conforming database.
 
NULL is conceptually intended to indicate "unknown" or "not
applicable".  If you have a person table with a date_of_birth
column, which contains NULL for a number of rows for which the date
of birth is unknown, can you say that all such people have the same
date of birth?  No; for any such person, the result of comparing
their date of birth to anyone else's (whether or not the other one
is NULL) is UNKNOWN.
 
You might want to read up on IS [NOT] DISTINCT FROM.  In the SQL
language, while NULL is not known to be equal to NULL, you *can* say
that NULL IS NOT DISTINCT FROM NULL.
 
This is most definitely not a bug in the software.  The
documentation does cover it here:
 
http://www.postgresql.org/docs/9.0/interactive/functions-comparison.html
 
Is there something you would add to that?
 
-Kevin



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #6064: != NULL, <> NULL do not work
@ 2011-06-19 09:00  Craig Ringer <craig@postnewspapers.com.au>
  parent: Kevin Grittner <Kevin.Grittner@wicourts.gov>
  0 siblings, 0 replies; 4+ messages in thread

From: Craig Ringer @ 2011-06-19 09:00 UTC (permalink / raw)
  To: Kevin Grittner <Kevin.Grittner@wicourts.gov>; +Cc: Michael Pilling <Michael.Pilling@dsto.defence.gov.au>; pgsql-bugs

On 06/17/2011 10:20 PM, Kevin Grittner wrote:
> "Michael Pilling"<Michael.Pilling@dsto.defence.gov.au>  wrote:
>
>> A reasonable programmer would expect != NULL,<>  NULL and IS NOT
>> NULL to be synonyms.
>
> Only if that programmer was not aware of the SQL standard and had
> not worked much with a standard-conforming database.

Yep, and if they want to continue working that way, they can use the 
flag intended for compatibility with Microsoft Access that makes NULL = 
NULL result in 't' instead of NULL.

http://www.postgresql.org/docs/current/static/runtime-config-compatible.html#GUC-TRANSFORM-NULL-EQUA...

Note that this flag is very specifically limited to equality 
comparisions using the '=' operator. It won't make NULL behave as a 
value in any other way. For example, 1 > NULL will still return NULL.

--
Craig Ringer



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2011-06-19 09:00 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-06-17 07:39 BUG #6064: != NULL, <> NULL do not work Michael Pilling <Michael.Pilling@dsto.defence.gov.au>
2011-06-17 07:43 ` Abel Abraham Camarillo Ojeda <acamari@verlet.org>
2011-06-17 14:20 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2011-06-19 09:00   ` Craig Ringer <craig@postnewspapers.com.au>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox