Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fJAaf-0002hF-6A for pgsql-general@arkaria.postgresql.org; Thu, 17 May 2018 04:32:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fJAab-0004JM-P4 for pgsql-general@arkaria.postgresql.org; Thu, 17 May 2018 04:32:29 +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.89) (envelope-from ) id 1fJAab-0004JC-HE for pgsql-general@lists.postgresql.org; Thu, 17 May 2018 04:32:29 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fJAaT-0005Bi-AL for pgsql-general@postgresql.org; Thu, 17 May 2018 04:32:28 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 4012B47D08; Thu, 17 May 2018 06:32:19 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter01.heinlein-hosting.de (spamfilter01.heinlein-hosting.de [80.241.56.115]) (amavisd-new, port 10030) with ESMTP id V7G0h3g8Vmhd; Thu, 17 May 2018 06:32:16 +0200 (CEST) Message-ID: <1526531535.2430.3.camel@cybertec.at> Subject: Re: sql function with empty row From: Laurenz Albe To: Philipp Kraus , "pgsql-general@postgresql.org" Date: Thu, 17 May 2018 06:32:15 +0200 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Philipp Kraus wrote: > I have defined a SQL function > > CREATE OR REPLACE FUNCTION substancetrivialname(text) > RETURNS substance > LANGUAGE 'sql' > COST 100 > VOLATILE > AS $BODY$ > select s.* from substancetrivialname n > join substance s on s.id = n.idsubstance > where lower(btrim(n.name)) = lower(btrim($1)); > $BODY$; > > substance and substancetrivialname have got a 1-to-N relationship (for each substance can exist multiple trivial names). > If I call the function with a non-existing trivial name it returns a single row with all fields are set to NULL. > > If I run the join query directly it returns an empty record set on a non-existing trivial name. > I expected equal behavior on my function, so my question is, how can I fix this? The difference is that the function has to return exactly one value, while the query it at liberty to return 0, 1 or more rows. Since there is no result, it returns a NULL value. What you are seeing is a valid composite NULL value: SELECT ROW(NULL, NULL) IS NULL; ?column? ---------- t (1 row) It looks weird, but the SQL standard wants it that way. NULLs and composite types is a topic that can really twist your brain. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com