Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cb9ZA-0004uI-DP for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Feb 2017 17:28:32 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cb9Z9-0001Yc-U0 for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Feb 2017 17:28:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cb9Z9-0001YV-Bt for pgsql-hackers@postgresql.org; Tue, 07 Feb 2017 17:28:31 +0000 Received: from 173-164-140-181-sfba.hfc.comcastbusiness.net ([173.164.140.181] helo=fetter.org) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cb9Z1-0007j5-AE for pgsql-hackers@postgresql.org; Tue, 07 Feb 2017 17:28:30 +0000 Received: by fetter.org (Postfix, from userid 1000) id D3E3BE07E9; Tue, 7 Feb 2017 09:28:19 -0800 (PST) Date: Tue, 7 Feb 2017 09:28:19 -0800 From: David Fetter To: Joel Jacobson Cc: Pg Hackers Subject: Re: Idea on how to simplify comparing two sets Message-ID: <20170207172819.GD11993@fetter.org> References: <20170207171017.GB11993@fetter.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170207171017.GB11993@fetter.org> User-Agent: Mutt/1.7.1 (2016-10-04) X-Pg-Spam-Score: -0.9 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On Tue, Feb 07, 2017 at 09:10:17AM -0800, David Fetter wrote: > On Tue, Feb 07, 2017 at 04:13:40PM +0100, Joel Jacobson wrote: > > Hi hackers, > > > > Currently there is no simple way to check if two sets are equal. > > Assuming that a and b each has at least one NOT NULL column, is this > simple enough? Based on nothing much, I'm assuming here that the IS > NOT NULL test is faster than IS NULL, but you can flip that and change > the array to {0} with identical effect. > > WITH t AS ( > SELECT a AS a, b AS b, (a IS NOT NULL)::int + (b IS NOT NULL)::int AS ind > FROM a FULL JOIN b ON ... > ) > SELECT array_agg(DISTINCT ind) = '{2}' > FROM t; You don't actually need a and b in the inner target list. WITH t AS ( SELECT (a IS NOT NULL)::int + (b IS NOT NULL)::int AS ind FROM a FULL JOIN b ON ... ) SELECT array_agg(DISTINCT ind) = '{2}' FROM t; This could be shortened further to the following if we ever implement DISTINCT for window functions, which might involve implementing DISTINCT via hashing more generally, which means hashable types...whee! SELECT array_agg(DISTINCT (a IS NOT NULL)::int + (b IS NOT NULL)::int) OVER () = '{2}' FROM a FULL JOIN b ON ... Best, David. -- David Fetter http://fetter.org/ Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com Remember to vote! Consider donating to Postgres: http://www.postgresql.org/about/donate -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers