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 1psSg6-000222-QL for pgsql-bugs@arkaria.postgresql.org; Fri, 28 Apr 2023 18:18:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1psSg5-0000t6-CJ for pgsql-bugs@arkaria.postgresql.org; Fri, 28 Apr 2023 18:18:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1psSg5-0000sw-3R for pgsql-bugs@lists.postgresql.org; Fri, 28 Apr 2023 18:18:41 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1psSg2-005d4z-Dy for pgsql-bugs@lists.postgresql.org; Fri, 28 Apr 2023 18:18:40 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 33SIIaqN1440310; Fri, 28 Apr 2023 14:18:36 -0400 From: Tom Lane To: Alexander Lakhin cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17912: Invalid memory access when converting plpython' array containing empty array In-reply-to: References: <17912-82ceed78731d9cdc@postgresql.org> <1392974.1682694846@sss.pgh.pa.us> Comments: In-reply-to Alexander Lakhin message dated "Fri, 28 Apr 2023 21:00:00 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1440308.1682705916.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 28 Apr 2023 14:18:36 -0400 Message-ID: <1440309.1682705916@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Alexander Lakhin writes: > Thank you! I've read the message of the commit you just pushed, > and I confirm that there are another oddities in that area. For example: > CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$ > return [[1], 2] > $$ LANGUAGE plpython3u; > SELECT test(); > ERROR:=C2=A0 wrong length of inner sequence: has length -1, but 1 was ex= pected > DETAIL:=C2=A0 To construct a multidimensional array, the inner sequences= must all have the same length. Yeah. AFAICT, the idea of the existing code is to descend through the first item at each nest level till we hit a non-list, then take the lengths of those lists as the array dimensions, and then complain if we find any later items that don't fit those dimensions. That leads to some symmetry problems, in that the error you get for inconsistent sequence lengths depends on the order in which the items are presented. Also, it seems like there is something odd here: regression=3D# CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$ regression$# return ["abc"] regression$# $$ LANGUAGE plpython3u; CREATE FUNCTION regression=3D# select test(); test = ------- {abc} (1 row) regression=3D# CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$ return [[1,2,3], "abc"] $$ LANGUAGE plpython3u; CREATE FUNCTION regression=3D# select test(); test = ------------------- {{1,2,3},{a,b,c}} (1 row) I think it's weird that "abc" is taken as a scalar in the first case and a list in the second case. Even worse, regression=3D# CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$ return ["abc", [1,2,3]] $$ LANGUAGE plpython3u; CREATE FUNCTION regression=3D# select test(); test = ------------------- {abc,"[1, 2, 3]"} (1 row) This might be something that used to work more sanely with Python 2 and got broken in Python 3 for the same reasons discussed in bug #17908. I've not poked at it more though. I don't think I have a Python 2 installation to test with anymore :-( regards, tom lane