Re: "fix" for plpgsql polymorphism

Started by Tom Laneabout 23 years ago6 messagespatches
Jump to latest
#1Tom Lane
tgl@sss.pgh.pa.us

Joe Conway <mail@joeconway.com> writes:

Tom Lane wrote:

I'm not excited about picking a notation for the long term on the
grounds that it takes the least code to implement today.

I knew you wouldn't be ;-)

I admit I have not got a clearly-better solution in my hip pocket,
but "$0" is just rubbing my design sense the wrong way.

Yeah, me too, but then I didn't particularly like plpgsql's $1, $2, ...
notation for arguments at first either. If we had named arguments, then
this wouldn't even be a consideration in my mind, but given that we
don't, I still think it is the best choice.

You can alias $0, similar to the argument variables. And, I confirmed
that you cannot change the value, similar to the argument variables:

Perhaps you shouldn't mark it isconst; then it would actually have some
usefulness (you could use it directly as a temporary variable to hold
the intended result). I can't see much value in aliasing it if it's
const, either.

regards, tom lane

#2Joe Conway
mail@joeconway.com
In reply to: Tom Lane (#1)

Tom Lane wrote:

You can alias $0, similar to the argument variables. And, I confirmed
that you cannot change the value, similar to the argument variables:

Perhaps you shouldn't mark it isconst; then it would actually have some
usefulness (you could use it directly as a temporary variable to hold
the intended result). I can't see much value in aliasing it if it's
const, either.

OK; the only change in this version is "isconst = false;". Now you can
use $0 as a result placeholder if desired. E.g.:

create or replace function tmp(anyelement, anyelement) returns anyarray as '
declare
v_ret alias for $0;
v_el1 alias for $1;
v_el2 alias for $2;
begin
v_ret := ARRAY[v_el1, v_el2];
return v_ret;
end;
' language 'plpgsql';

create table f(f1 text, f2 text, f3 int, f4 int);
insert into f values ('a','b',1,2);
insert into f values ('z','x',3,4);

select tmp(f1,f2) from f;
select tmp(f3,f4) from f;

Joe

Attachments:

plpgsql-poly-fix.02.patchtext/plain; name=plpgsql-poly-fix.02.patchDownload+36-0
#3Christopher Kings-Lynne
chriskl@familyhealth.com.au
In reply to: Tom Lane (#1)

OK; the only change in this version is "isconst = false;". Now you can
use $0 as a result placeholder if desired. E.g.:

Can we get any inspiration from Perl or bash here? eg. $$ or $* or
whatever?

Chris

#4Joe Conway
mail@joeconway.com
In reply to: Tom Lane (#1)

I'm going to resend the patches that I have outstanding since it appears
some may have been lost. Here's the first.
==========================================

Tom Lane wrote:

You can alias $0, similar to the argument variables. And, I confirmed
that you cannot change the value, similar to the argument variables:

Perhaps you shouldn't mark it isconst; then it would actually have some
usefulness (you could use it directly as a temporary variable to hold
the intended result). I can't see much value in aliasing it if it's
const, either.

OK; the only change in this version is "isconst = false;". Now you can
use $0 as a result placeholder if desired. E.g.:

create or replace function tmp(anyelement, anyelement) returns anyarray as '
declare
v_ret alias for $0;
v_el1 alias for $1;
v_el2 alias for $2;
begin
v_ret := ARRAY[v_el1, v_el2];
return v_ret;
end;
' language 'plpgsql';

create table f(f1 text, f2 text, f3 int, f4 int);
insert into f values ('a','b',1,2);
insert into f values ('z','x',3,4);

select tmp(f1,f2) from f;
select tmp(f3,f4) from f;

Joe

Attachments:

plpgsql-poly-fix.02.patchtext/plain; name=plpgsql-poly-fix.02.patchDownload+36-0
#5Bruce Momjian
bruce@momjian.us
In reply to: Joe Conway (#2)

Your patch has been added to the PostgreSQL unapplied patches list at:

http://momjian.postgresql.org/cgi-bin/pgpatches

I will try to apply it within the next 48 hours.

---------------------------------------------------------------------------

Joe Conway wrote:

Tom Lane wrote:

You can alias $0, similar to the argument variables. And, I confirmed
that you cannot change the value, similar to the argument variables:

Perhaps you shouldn't mark it isconst; then it would actually have some
usefulness (you could use it directly as a temporary variable to hold
the intended result). I can't see much value in aliasing it if it's
const, either.

OK; the only change in this version is "isconst = false;". Now you can
use $0 as a result placeholder if desired. E.g.:

create or replace function tmp(anyelement, anyelement) returns anyarray as '
declare
v_ret alias for $0;
v_el1 alias for $1;
v_el2 alias for $2;
begin
v_ret := ARRAY[v_el1, v_el2];
return v_ret;
end;
' language 'plpgsql';

create table f(f1 text, f2 text, f3 int, f4 int);
insert into f values ('a','b',1,2);
insert into f values ('z','x',3,4);

select tmp(f1,f2) from f;
select tmp(f3,f4) from f;

Joe

Index: src/pl/plpgsql/src/pl_comp.c
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/pl/plpgsql/src/pl_comp.c,v
retrieving revision 1.59
diff -c -r1.59 pl_comp.c
*** src/pl/plpgsql/src/pl_comp.c	1 Jul 2003 21:47:09 -0000	1.59
--- src/pl/plpgsql/src/pl_comp.c	3 Jul 2003 17:59:48 -0000
***************
*** 354,359 ****
--- 354,395 ----
function->fn_rettyplen = typeStruct->typlen;
function->fn_rettypelem = typeStruct->typelem;
perm_fmgr_info(typeStruct->typinput, &(function->fn_retinput));
+ 
+ 				/*
+ 				 * install $0 reference, but only for polymorphic
+ 				 * return types
+ 				 */
+ 				if (procStruct->prorettype == ANYARRAYOID ||
+ 					procStruct->prorettype == ANYELEMENTOID)
+ 				{
+ 					char		buf[32];
+ 
+ 					/* name for variable */
+ 					snprintf(buf, sizeof(buf), "$%d", 0);
+ 
+ 					/*
+ 					 * Normal return values get a var node
+ 					 */
+ 					var = malloc(sizeof(PLpgSQL_var));
+ 					memset(var, 0, sizeof(PLpgSQL_var));
+ 
+ 					var->dtype = PLPGSQL_DTYPE_VAR;
+ 					var->refname = strdup(buf);
+ 					var->lineno = 0;
+ 					var->datatype = build_datatype(typeTup, -1);
+ 					var->isconst = false;
+ 					var->notnull = false;
+ 					var->default_val = NULL;
+ 
+ 					/* preset to NULL */
+ 					var->value = 0;
+ 					var->isnull = true;
+ 					var->freeval = false;
+ 
+ 					plpgsql_adddatum((PLpgSQL_datum *) var);
+ 					plpgsql_ns_additem(PLPGSQL_NSTYPE_VAR, var->varno,
+ 									   var->refname);
+ 				}
}
ReleaseSysCache(typeTup);

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#6Bruce Momjian
bruce@momjian.us
In reply to: Joe Conway (#2)

Patch applied. Thanks.

---------------------------------------------------------------------------

Joe Conway wrote:

Tom Lane wrote:

You can alias $0, similar to the argument variables. And, I confirmed
that you cannot change the value, similar to the argument variables:

Perhaps you shouldn't mark it isconst; then it would actually have some
usefulness (you could use it directly as a temporary variable to hold
the intended result). I can't see much value in aliasing it if it's
const, either.

OK; the only change in this version is "isconst = false;". Now you can
use $0 as a result placeholder if desired. E.g.:

create or replace function tmp(anyelement, anyelement) returns anyarray as '
declare
v_ret alias for $0;
v_el1 alias for $1;
v_el2 alias for $2;
begin
v_ret := ARRAY[v_el1, v_el2];
return v_ret;
end;
' language 'plpgsql';

create table f(f1 text, f2 text, f3 int, f4 int);
insert into f values ('a','b',1,2);
insert into f values ('z','x',3,4);

select tmp(f1,f2) from f;
select tmp(f3,f4) from f;

Joe

Index: src/pl/plpgsql/src/pl_comp.c
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/pl/plpgsql/src/pl_comp.c,v
retrieving revision 1.59
diff -c -r1.59 pl_comp.c
*** src/pl/plpgsql/src/pl_comp.c	1 Jul 2003 21:47:09 -0000	1.59
--- src/pl/plpgsql/src/pl_comp.c	3 Jul 2003 17:59:48 -0000
***************
*** 354,359 ****
--- 354,395 ----
function->fn_rettyplen = typeStruct->typlen;
function->fn_rettypelem = typeStruct->typelem;
perm_fmgr_info(typeStruct->typinput, &(function->fn_retinput));
+ 
+ 				/*
+ 				 * install $0 reference, but only for polymorphic
+ 				 * return types
+ 				 */
+ 				if (procStruct->prorettype == ANYARRAYOID ||
+ 					procStruct->prorettype == ANYELEMENTOID)
+ 				{
+ 					char		buf[32];
+ 
+ 					/* name for variable */
+ 					snprintf(buf, sizeof(buf), "$%d", 0);
+ 
+ 					/*
+ 					 * Normal return values get a var node
+ 					 */
+ 					var = malloc(sizeof(PLpgSQL_var));
+ 					memset(var, 0, sizeof(PLpgSQL_var));
+ 
+ 					var->dtype = PLPGSQL_DTYPE_VAR;
+ 					var->refname = strdup(buf);
+ 					var->lineno = 0;
+ 					var->datatype = build_datatype(typeTup, -1);
+ 					var->isconst = false;
+ 					var->notnull = false;
+ 					var->default_val = NULL;
+ 
+ 					/* preset to NULL */
+ 					var->value = 0;
+ 					var->isnull = true;
+ 					var->freeval = false;
+ 
+ 					plpgsql_adddatum((PLpgSQL_datum *) var);
+ 					plpgsql_ns_additem(PLPGSQL_NSTYPE_VAR, var->varno,
+ 									   var->refname);
+ 				}
}
ReleaseSysCache(typeTup);

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073