Possible bug (or at least unexpected behavior)

From: Adam Mackler <adam(at)mackler(dot)email>
To: "pgsql-sql(at)lists(dot)postgresql(dot)org" <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Possible bug (or at least unexpected behavior)
Date: 2022-08-07 20:06:21
Message-ID: WScDU5qfoZ7PB2gXwNqwGGgDPmWzz08VdydcPFLhOwUKZcdWbblbo-0Lku-qhuEiZoXJ82jpiQU4hOjOcrevYEDeoAvz6nR0IU4IHhXnaCA=@mackler.email
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi, forgive me if I should be posting this somewhere else.  I asked the following question on stackoverflow, and the first response suggests a possible bug:

https://stackoverflow.com/questions/73261240/recursive-sql-function-returning-array-has-extra-elements-when-self-invocation-u#73261240

Briefly, given the following function:

CREATE FUNCTION runs(input int[], output int[] DEFAULT '{}')
RETURNS int[] AS $$
SELECT
CASE WHEN cardinality(input) = 0 THEN output
ELSE runs(input[2:],
array_append(output, CASE
WHEN input[1] = 0 THEN 0
ELSE output[cardinality(output)] + input[1]
END)
)
END
$$ LANGUAGE SQL;

I expect the following invocation to return an array with the same number of elements as the passed-in argument array:

# select runs('{0,1,1,1,1,0,-1,-1,-1,0}');
runs
----------------------------------------
{0,1,2,3,4,5,6,0,0,0,-1,-2,-3,-4,-5,0}
(1 row)

which it does not with PostgreSQL version 14.4. If it not a bug, then I would be extremely interested in why it's returning an array with more elements than the input array has.

Thanks in advance,
--
Adam Mackler

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2022-08-07 23:31:41 Re: Possible bug (or at least unexpected behavior)
Previous Message Karsten Hilbert 2022-08-05 13:15:08 Re: JDBC Driver for postgres 9.6