Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YBnWp-0001za-5Z for pgsql-bugs@arkaria.postgresql.org; Thu, 15 Jan 2015 16:44:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YBnWn-0001Bm-TF for pgsql-bugs@arkaria.postgresql.org; Thu, 15 Jan 2015 16:44:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YBiav-0001At-My for pgsql-bugs@postgresql.org; Thu, 15 Jan 2015 11:28:09 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YBiap-0008FK-5J for pgsql-bugs@postgresql.org; Thu, 15 Jan 2015 11:28:08 +0000 MIME-Version: 1.0 Subject: BUG #12556: Clause IN and NOT IN buggy To: pgsql-bugs@postgresql.org From: kevin.perais@trivia-marketing.com Date: Thu, 15 Jan 2015 11:27:20 +0000 Message-ID: <20150115112720.2502.55131@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 12556 Logged by: Kevin PERAIS Email address: kevin.perais@trivia-marketing.com PostgreSQL version: 9.3.5 Operating system: Ubuntu 12.04 Description: Hi everybody. I've been noticing several times that clauses IN and NOT IN are often buggy. I'll show you 2 concrete cases to illustrate that. --------------------------------------------- - 1 - IN and NOT IN not coherent with JOIN - --------------------------------------------- I have 2 tables here: Account(id BIGINT PRIMARY KEY NOT NULL, ...) Contact(contact_id VARCHAR(11) PRIMARY KEY NOT NULL, account_id BIGINT, ...) I the contacts are linked to an account. However, I know I do not have all the account, so I want to know how many contacts are linked to an account and how many are not. Here is a set of queries: SELECT count(*) FROM Account WHERE id IS NULL; --> 0 SELECT count(*) FROM Contact WHERE account_id IS NULL; --> 0 SELECT count(*) FROM Contact; --> 257726 SELECT count(*) FROM Contact WHERE account_id IN ( SELECT id FROM Account ); --> 257726 (result very very suspect) SELECT count(*) FROM Contact WHERE account_id NOT IN ( SELECT id FROM Account ); --> 0 (coherent with previous query, but result very very suspect) SELECT count(*) FROM Contact JOIN Account ON Account.id = Contact.account_id; --> 135664 SELECT count(Account.id) FROM Contact LEFT JOIN Account ON Account.id = Contact.account_id; --> 135664 So I decided to take a random account_id in Contact table and run the following queries: SELECT count(*) FROM Contact WHERE account_id = 3074054072; --> 6 SELECT count(*) FROM Account WHERE id = 3074054072; --> 0 So that proves that IN and NOT IN queries give me wrong results. ------------------------------------------------- - 2 - NOT IN clause not coherent with IN clause - ------------------------------------------------- I have 2 tables: Ref(num INTEGER UNIQUE, ...) Sample(num VARCHAR(9), ...) Here is a set of queries: SELECT count(*) FROM Sample; --> 692 SELECT count(DISTINCT num) FROM Sample; --> 673 SELECT count(*) FROM Sample WHERE num IS NULL; --> 19 --> 673 + 19 = 692 --> So we now know there are 673 distinct num and 19 NULL in the Sample table. SELECT count(*) FROM Ref; --> 8232 SELECT count(*) FROM Ref WHERE num IS NULL; --> 151 SELECT count(*) FROM Sample WHERE num::INTEGER IN ( SELECT num FROM Ref ); --> 2 SELECT count(*) FROM Sample WHERE num::INTEGER NOT IN ( SELECT num FROM Ref ); --> 0 (Well that is very strange and not coherent at all with the previous query). I might be able to provide a data set to test case 2 (but not case 1 as data are confidential). Hope I gave enough info on the bug. Kevin -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs