Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VJ39x-0007pp-Ar for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Sep 2013 15:13:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VJ39w-0002fx-Qb for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Sep 2013 15:13:48 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VJ1Mz-0002fN-4E for pgsql-bugs@postgresql.org; Mon, 09 Sep 2013 13:19:09 +0000 Received: from mahout.postgresql.org ([2001:4800:7903:4::123]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VJ1Mw-0001my-4B for pgsql-bugs@postgresql.org; Mon, 09 Sep 2013 13:19:08 +0000 Received: from [204.145.124.246] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VJ1Mv-0005je-8U for pgsql-bugs@postgresql.org; Mon, 09 Sep 2013 13:19:05 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.80) (envelope-from ) id 1VJ1Mp-0003Bd-UZ for pgsql-bugs@postgresql.org; Mon, 09 Sep 2013 13:19:00 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: BUG #8441: Recursive function in plpgsql does not seem to handle null values correctly To: pgsql-bugs@postgresql.org From: tvees@davincigroep.nl Message-Id: Date: Mon, 09 Sep 2013 13:18:59 +0000 X-Pg-Spam-Score: -0.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 The following bug has been logged on the website: Bug reference: 8441 Logged by: Tom van Ees Email address: tvees@davincigroep.nl PostgreSQL version: 9.0.4 Operating system: Windows Server 2008 R2 Description: The Levenshtein function can only handle strings with length 255 or less. I needed a Levenshtein function that could handle longer strings. Therefore I wrote the following udf: CREATE OR REPLACE FUNCTION longlevenshtein (string1 character varying (1000000), string2 character varying (1000000)) RETURNS integer AS $$ BEGIN IF (length(coalesce($1, '')) = 0 AND length(coalesce($2, '')) = 0) THEN RETURN 0; ELSEIF ($1 IS NULL and $2 IS NOT NULL and length($2) > 0) THEN RETURN length($2); ELSEIF ($2 IS NULL and $1 IS NOT NULL and length($1)> 0) THEN RETURN length($1); ELSEIF length($1) = 0 AND length(coalesce($2, '')) > 0 THEN RETURN length(coalesce($2, '')); ELSEIF length($1) > 0 AND (length($2) = 0 or $2 is null) THEN RETURN length(coalesce($1, '')); ELSE RETURN (Levenshtein(SUBSTRING($1 FROM 1 FOR 254), SUBSTRING($2 FROM 1 for 254)) + longlevenshtein(coalesce(SUBSTRING($1 FROM 255), ''), coalesce(SUBSTRING($2 FROM 255), ''))); END IF; END; $$ LANGUAGE plpgsql; When I invoke this function with SELECT longlevenshtein(null, 'foobar') I get a ERROR: stack depth limit exceeded while I expected the return value 6 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs