pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: tvees@davincigroep.nl
To: pgsql-bugs@postgresql.org
Subject: BUG #8441: Recursive function in plpgsql does not seem to handle null values correctly
Date: Mon, 09 Sep 2013 13:18:59 +0000
Message-ID: <E1VJ1Mp-0003Bd-UZ@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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



view thread (4+ messages)  latest in thread

Message-ID: <E1VJ1Mp-0003Bd-UZ@wrigleys.postgresql.org>
Permalink:  ../E1VJ1Mp-0003Bd-UZ@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/E1VJ1Mp-0003Bd-UZ@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tvees@davincigroep.nl
  Subject: Re: BUG #8441: Recursive function in plpgsql does not seem to handle null values correctly
  In-Reply-To: <E1VJ1Mp-0003Bd-UZ@wrigleys.postgresql.org>
  References: <E1VJ1Mp-0003Bd-UZ@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox