Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oKokA-0008L6-Se for pgsql-sql@arkaria.postgresql.org; Sun, 07 Aug 2022 22:27:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oKok9-0002Qf-NS for pgsql-sql@arkaria.postgresql.org; Sun, 07 Aug 2022 22:27:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oKmXn-0004Qe-0F for pgsql-sql@lists.postgresql.org; Sun, 07 Aug 2022 20:06:39 +0000 Received: from mail-4317.proton.ch ([185.70.43.17]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oKmXf-00018L-4P for pgsql-sql@lists.postgresql.org; Sun, 07 Aug 2022 20:06:36 +0000 Date: Sun, 07 Aug 2022 20:06:21 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mackler.email; s=protonmail3; t=1659902788; x=1660161988; bh=FRpUcvyYLdtORc81hhENgD5/07F3f/wjHCkku+A5acA=; h=Date:To:From:Reply-To:Subject:Message-ID:Feedback-ID:From:To:Cc: Date:Subject:Reply-To:Feedback-ID:Message-ID; b=NiyKvaxx5fEucI72PRyPtms5NC6GcbadkcJiyaIGKPPnIGNXHBYpeCoEqTKRsa5J9 9mdMzWngRbKRTyYTIoNk6OctCyhrcXiKUZR33iW7B/yxz7s5T/f5RicIycBvqYmSj5 9lluvPDHm2gJLo8gOKWosb+ZVLRvFzEP0RNjlpXEEaaJYH0uOxnAhlK1xot+fmmED2 OoGeAZNA3Dz/08+j76FpU+iT3B85y+qacb9EUyB32Rw3qPxc2vSBM3y5y4juI2FppV B0QoDvx9uSSAJMzLKLcEzbBQAZX0uHknQI0eSXF19IMF70nL+pgZZxxya8luhvjwI4 Ku0arcY1b7jtg== To: "pgsql-sql@lists.postgresql.org" From: Adam Mackler Reply-To: Adam Mackler Subject: Possible bug (or at least unexpected behavior) Message-ID: Feedback-ID: 17471936:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, forgive me if I should be posting this somewhere else.=C2=A0 I asked th= e following question on stackoverflow, and the first response suggests a po= ssible bug: https://stackoverflow.com/questions/73261240/recursive-sql-function-returni= ng-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) =3D 0 THEN output ELSE runs(input[2:], array_append(output, CASE WHEN input[1] =3D 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 o= f 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 wo= uld be extremely interested in why it's returning an array with more elemen= ts than the input array has. Thanks in advance, -- Adam Mackler