Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ts1H1-0002dG-92 for pgsql-bugs@arkaria.postgresql.org; Mon, 07 Jan 2013 01:13:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Ts1H0-0003SU-BO for pgsql-bugs@arkaria.postgresql.org; Mon, 07 Jan 2013 01:13:06 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ts1Gy-0003SM-BI for pgsql-bugs@postgresql.org; Mon, 07 Jan 2013 01:13:04 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Ts1Gx-0008NZ-59 for pgsql-bugs@postgresql.org; Mon, 07 Jan 2013 01:13:03 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id r071D1sO010865; Sun, 6 Jan 2013 20:13:01 -0500 (EST) From: Tom Lane To: luisa.j.francisco@gmail.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #7790: null filters in CTEs don't work In-reply-to: References: Comments: In-reply-to luisa.j.francisco@gmail.com message dated "Sun, 06 Jan 2013 04:58:07 +0000" Date: Sun, 06 Jan 2013 20:13:01 -0500 Message-ID: <10864.1357521181@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) 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 luisa.j.francisco@gmail.com writes: > Expected output should have no nulls in it, but it does: It's not apparent to me why you think the first query shouldn't produce any rows with null parent_id? AFAICS, the recursive query will "crawl up the tree" producing a row for every parent level above the given base-case rows. Eventually you'll get up to a match to the row ('body', null), and there's nothing to stop that from being displayed. It's a bit easier to see what's happening if you leave off the "ORDER BY" so that the rows are printed in generation order: regression=# SELECT id, parent_id FROM item_tree i WHERE parent_id IS NOT NULL AND id NOT IN ( SELECT parent_id FROM item_tree WHERE parent_id IS NOT NULL); id | parent_id --------+----------- tooth | mouth tongue | mouth sclera | eye cornea | eye (4 rows) regression=# WITH RECURSIVE t(id, parent_id) AS ( SELECT id, parent_id FROM item_tree i WHERE parent_id IS NOT NULL AND id NOT IN ( SELECT parent_id FROM item_tree WHERE parent_id IS NOT NULL) UNION ALL SELECT t.id, i.parent_id FROM item_tree i JOIN t ON i.id = t.parent_id ) SELECT * FROM t; id | parent_id --------+----------- tooth | mouth tongue | mouth sclera | eye cornea | eye tooth | head tongue | head sclera | head cornea | head tooth | body tongue | body sclera | body cornea | body tooth | tongue | sclera | cornea | (16 rows) regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs