Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 1A445B5DC26 for ; Mon, 20 Jun 2011 03:21:10 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 75181-01-9 for ; Mon, 20 Jun 2011 06:21:03 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-iy0-f174.google.com (mail-iy0-f174.google.com [209.85.210.174]) by mail.postgresql.org (Postfix) with ESMTP id 08565B5DE44 for ; Mon, 20 Jun 2011 03:19:20 -0300 (ADT) Received: by iyb12 with SMTP id 12so1706318iyb.19 for ; Sun, 19 Jun 2011 23:19:20 -0700 (PDT) Received: by 10.42.167.73 with SMTP id r9mr6003204icy.347.1308550760348; Sun, 19 Jun 2011 23:19:20 -0700 (PDT) Received: from [192.168.1.101] (c-69-181-249-16.hsd1.ca.comcast.net [69.181.249.16]) by mx.google.com with ESMTPS id w11sm2991642ibw.41.2011.06.19.23.19.18 (version=SSLv3 cipher=OTHER); Sun, 19 Jun 2011 23:19:19 -0700 (PDT) Subject: Re: BUG #6064: != NULL, <> NULL do not work [sec=UNCLASSIFIED] From: Jeff Davis To: "Pilling, Michael" Cc: Kevin Grittner , craig@postnewspapers.com.au, pgsql-bugs@postgresql.org In-Reply-To: References: <4DFE481F020000250003E8EA@gw.wicourts.gov> Content-Type: text/plain; charset="UTF-8" Date: Sun, 19 Jun 2011 23:19:33 -0700 Message-ID: <1308550773.2597.114.camel@jdavis> Mime-Version: 1.0 X-Mailer: Evolution 2.32.2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201106/177 X-Sequence-Number: 30556 On Mon, 2011-06-20 at 10:00 +0930, Pilling, Michael wrote: > The behaviour of the generated code may well be correct and indeed I > agree that it is but from > everything you and the detailed documentation have said column != NULL > is at least deprecated > and is highly likely to indicate a programming error. The right side of the expression may be an expression as well; e.g.: a != b (or a <> b) The DBMS would not know that one side is NULL until runtime. > It is totally normal for a parser to warn > against archaic or dangerous constructs. It's only an obvious mistake in the trivial case you show where one side is a constant NULL (therefore making the entire expression a constant NULL). The more general form "a != b" is quite common, even if somewhat dangerous in the presence of NULL. > The idea being to avoid subtle runtime bugs that are > hard to track down. This happens in Ada, Java and even C and many > other languages. NULL is one place in SQL that hides possible mistakes that could otherwise be caught by the compiler, leaving your queries in danger of subtle runtime bugs. There's not a good way to conform to the SQL spec and catch the kind of subtle NULL problems to which you're referring. It may be possible to make a static analysis "safety check" tool to warn users about dangerous constructs like that, but it would be a fairly major effort (and would probably just end up telling you to put COALESCE everywhere). Trying to only catch the kind of trivial mistakes involving constants and known operators is counterproductive, in my opinion. The bottom line is that NULLs are a little on the dangerous side. If you think your example is bad, consider the semantics of NOT IN with respect to NULL -- that's a trap even for experts. If you want to be safe, make liberal use of COALESCE and WHERE x IS NOT NULL on any expression that you think might ever evaluate to NULL. Also note that NULLs can be created by outer joins and aggregates even if your source data has no NULLs at all. PostgreSQL is a SQL DBMS, and in SQL, NULL affects everything. I'm sure there are places in the documentation that could be improved, but warnings on every page would be counterproductive. Regards, Jeff Davis