Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fROZv-00018P-Mp for pgsql-hackers@arkaria.postgresql.org; Fri, 08 Jun 2018 21:05:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fROZu-0005Xc-GC for pgsql-hackers@arkaria.postgresql.org; Fri, 08 Jun 2018 21:05:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fROZu-0005XT-8t for pgsql-hackers@lists.postgresql.org; Fri, 08 Jun 2018 21:05:46 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fROZr-0004BX-6T for pgsql-hackers@lists.postgresql.org; Fri, 08 Jun 2018 21:05:45 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w58L5TcK032315; Fri, 8 Jun 2018 17:05:29 -0400 From: Tom Lane To: Peter Eisentraut cc: ilmari@ilmari.org (Dagfinn Ilmari =?utf-8?Q?Manns=C3=A5ker?=), Alvaro Herrera , Pavel Stehule , Nikita Glukhov , PostgreSQL Hackers Subject: Re: Transform for pl/perl In-reply-to: <27213.1528475947@sss.pgh.pa.us> References: <20180606161453.tvp4ltavlcvejmmz@alvherre.pgsql> <28336.1528393969@sss.pgh.pa.us> <27213.1528475947@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Fri, 08 Jun 2018 12:39:07 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <32101.1528491756.0@sss.pgh.pa.us> Date: Fri, 08 Jun 2018 17:05:29 -0400 Message-ID: <32314.1528491929@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <32101.1528491756.1@sss.pgh.pa.us> I wrote: > I'm inclined to think that auto-dereference is indeed a good idea, > and am tempted to go make that change to make all this consistent. > Comments? Here's a draft patch for that. I ended up only changing plperl_sv_to_datum. There is maybe a case for doing something similar in plperl_return_next_internal's fn_retistuple code path, so that you can return a reference-to-reference-to-hash there; but I was unable to muster much enthusiasm for touching that. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="allow-scalar-ref-dereference-in-plperl.patch"; charset="us-ascii" Content-ID: <32101.1528491756.2@sss.pgh.pa.us> Content-Description: allow-scalar-ref-dereference-in-plperl.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/pl/plperl/expected/plperl.out b/src/pl/plperl/expected/pl= perl.out index ebfba3e..d8a1ff5 100644 *** a/src/pl/plperl/expected/plperl.out --- b/src/pl/plperl/expected/plperl.out *************** $$ LANGUAGE plperl; *** 763,776 **** SELECT text_obj(); ERROR: cannot convert Perl hash to non-composite type text CONTEXT: PL/Perl function "text_obj" ! ----- make sure we can't return a scalar ref CREATE OR REPLACE FUNCTION text_scalarref() RETURNS text AS $$ my $str =3D 'str'; return \$str; $$ LANGUAGE plperl; SELECT text_scalarref(); ! ERROR: PL/Perl function must return reference to hash or array ! CONTEXT: PL/Perl function "text_scalarref" -- check safe behavior when a function body is replaced during execution CREATE OR REPLACE FUNCTION self_modify(INTEGER) RETURNS INTEGER AS $$ spi_exec_query('CREATE OR REPLACE FUNCTION self_modify(INTEGER) RETUR= NS INTEGER AS \'return $_[0] * 3;\' LANGUAGE plperl;'); --- 763,779 ---- SELECT text_obj(); ERROR: cannot convert Perl hash to non-composite type text CONTEXT: PL/Perl function "text_obj" ! -- test looking through a scalar ref CREATE OR REPLACE FUNCTION text_scalarref() RETURNS text AS $$ my $str =3D 'str'; return \$str; $$ LANGUAGE plperl; SELECT text_scalarref(); ! text_scalarref = ! ---------------- ! str ! (1 row) ! = -- check safe behavior when a function body is replaced during execution CREATE OR REPLACE FUNCTION self_modify(INTEGER) RETURNS INTEGER AS $$ spi_exec_query('CREATE OR REPLACE FUNCTION self_modify(INTEGER) RETUR= NS INTEGER AS \'return $_[0] * 3;\' LANGUAGE plperl;'); diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c index 4342c02..4cfc506 100644 *** a/src/pl/plperl/plperl.c --- b/src/pl/plperl/plperl.c *************** plperl_sv_to_datum(SV *sv, Oid typid, in *** 1402,1412 **** return ret; } = ! /* Reference, but not reference to hash or array ... */ ! ereport(ERROR, ! (errcode(ERRCODE_DATATYPE_MISMATCH), ! errmsg("PL/Perl function must return reference to hash or array")))= ; ! return (Datum) 0; /* shut up compiler */ } else { --- 1402,1414 ---- return ret; } = ! /* ! * If it's a reference to something else, such as a scalar, just ! * recursively look through the reference. ! */ ! return plperl_sv_to_datum(SvRV(sv), typid, typmod, ! fcinfo, finfo, typioparam, ! isnull); } else { diff --git a/src/pl/plperl/sql/plperl.sql b/src/pl/plperl/sql/plperl.sql index c36da0f..b0d950b 100644 *** a/src/pl/plperl/sql/plperl.sql --- b/src/pl/plperl/sql/plperl.sql *************** $$ LANGUAGE plperl; *** 504,510 **** = SELECT text_obj(); = ! ----- make sure we can't return a scalar ref CREATE OR REPLACE FUNCTION text_scalarref() RETURNS text AS $$ my $str =3D 'str'; return \$str; --- 504,510 ---- = SELECT text_obj(); = ! -- test looking through a scalar ref CREATE OR REPLACE FUNCTION text_scalarref() RETURNS text AS $$ my $str =3D 'str'; return \$str; ------- =_aaaaaaaaaa0--