Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YDgXU-0005Vl-41 for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Jan 2015 21:40:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YDgXS-0006TN-VC for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Jan 2015 21:40:43 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YDgXR-0006TG-UW for pgsql-bugs@postgresql.org; Tue, 20 Jan 2015 21:40:42 +0000 Received: from mwork.nabble.com ([162.253.133.43]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YDgXO-0002F6-Cu for pgsql-bugs@postgresql.org; Tue, 20 Jan 2015 21:40:39 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mwork.nabble.com (Postfix) with ESMTP id B5F121129761 for ; Tue, 20 Jan 2015 13:40:37 -0800 (PST) Date: Tue, 20 Jan 2015 14:40:37 -0700 (MST) From: David G Johnston To: pgsql-bugs@postgresql.org Message-ID: <1421790037168-5834795.post@n5.nabble.com> In-Reply-To: <20150120204740.5767.84374@wrigleys.postgresql.org> References: <20150120204740.5767.84374@wrigleys.postgresql.org> Subject: Re: BUG #12609: use of regexp_matches drops rows when there is no match MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.3 (/) 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 trevor wrote > The following bug has been logged on the website: > > Bug reference: 12609 > Logged by: Trevor J Peschek > Email address: > trevor@ > PostgreSQL version: 9.3.3 > Operating system: Red Hat 4.6.3-2 > Description: > > When using regexp_matches while referencing a location in the array, if > the > match fails, the entire row is skipped instead of just a null for the > column > using regexp_matches. Example: > > select > aColumn, > (regexp_matches(queryCol, '(https?)://(.*?)/(.*?)\?(.*?)'))[3] > from myTable > > Even if the column with the regexp_matches returns a null, we would expect > the row to be populated with aColumn and then a null. What happens though > is nothing is returned. Working as designed though I'll agree that it is somewhat surprising. The main problem is that it doesn't "return NULL" but instead returns the empty set which suppresses the row it is attached to. You must wrap the regexp_matches into a scalar subquery to get this to work: SELECT aColumn, (SELECT regexp_matches(queryCo, '...')) FROM myTable Personally I would suggest writing a simple function, call it regexp_matches_single(), that returns a single array instead of a set. In almost all cases when you are doing stuff like this you expect one match or you wish to return null. Returning multiple rows is typically a problem and you will need to decide what to do inside your function if it occurs (e.g. return first match or throw an exception). David J. -- View this message in context: http://postgresql.nabble.com/BUG-12609-use-of-regexp-matches-drops-rows-when-there-is-no-match-tp5834794p5834795.html Sent from the PostgreSQL - bugs mailing list archive at Nabble.com. -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs