Support for %TYPE in CREATE FUNCTION

Поиск
Список
Период
Сортировка
Искать

Support for %TYPE in CREATE FUNCTION

От:
Ian Lance Taylor <ian@airs.com>
Дата:
This patch adds support for %TYPE in CREATE FUNCTION argument and
return types.

%TYPE is already supported by PL/pgSQL when declaring variables.
However, that does not help with the argument and return types in
CREATE FUNCTION.

Using %TYPE makes it easier to write a function which is independent
of the definition of a table.  That is, minor changes to the types
used in the table may not require changes to the function.

For example, this trivial function will work whenever `table' which
has columns named `name' and `value', no matter what the types of the
columns are.

CREATE FUNCTION lookup (table.name%TYPE)
   RETURNS table.value%TYPE
   AS 'select value from table where name = $1'
   LANGUAGE 'sql';

This patch includes changes to the testsuite and the documentation.

This work was sponsored by Zembu.

Ian

Index: src/include/nodes/parsenodes.h
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/include/nodes/parsenodes.h,v
retrieving revision 1.126
diff -p -u -r1.126 parsenodes.h
--- src/include/nodes/parsenodes.h	2001/03/23 04:49:56	1.126
+++ src/include/nodes/parsenodes.h	2001/04/28 03:38:21
@@ -945,6 +945,7 @@ typedef struct TypeName
 	bool		setof;			/* is a set? */
 	int32		typmod;			/* type modifier */
 	List	   *arrayBounds;	/* array bounds */
+	char	   *attrname;		/* field name when using %TYPE */
 } TypeName;
 
 /*
Index: src/backend/parser/analyze.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/parser/analyze.c,v
retrieving revision 1.183
diff -p -u -r1.183 analyze.c
--- src/backend/parser/analyze.c	2001/03/22 06:16:15	1.183
+++ src/backend/parser/analyze.c	2001/04/28 03:38:23
@@ -27,6 +27,7 @@
 #include "parser/parse_relation.h"
 #include "parser/parse_target.h"
 #include "parser/parse_type.h"
+#include "parser/parse_expr.h"
 #include "rewrite/rewriteManip.h"
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
@@ -49,7 +50,10 @@ static Node *transformSetOperationTree(P
 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
 static Query *transformCreateStmt(ParseState *pstate, CreateStmt *stmt);
 static Query *transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt);
+static Node *transformTypeRefs(ParseState *pstate, Node *stmt);
 
+static void transformTypeRefsList(ParseState *pstate, List *l);
+static void transformTypeRef(ParseState *pstate, TypeName *tn);
 static List *getSetColTypes(ParseState *pstate, Node *node);
 static void transformForUpdate(Query *qry, List *forUpdate);
 static void transformFkeyGetPrimaryKey(FkConstraint *fkconstraint);
@@ -230,6 +234,18 @@ transformStmt(ParseState *pstate, Node *
 											   (SelectStmt *) parseTree);
 			break;
 
+			/*
+			 * Convert use of %TYPE in statements where it is permitted.
+			 */
+		case T_ProcedureStmt:
+		case T_CommentStmt:
+		case T_RemoveFuncStmt:
+		case T_DefineStmt:
+			result = makeNode(Query);
+			result->commandType = CMD_UTILITY;
+			result->utilityStmt = transformTypeRefs(pstate, parseTree);
+			break;
+
 		default:
 
 			/*
@@ -2607,6 +2623,104 @@ transformAlterTableStmt(ParseState *psta
 	}
 	qry->utilityStmt = (Node *) stmt;
 	return qry;
+}
+
+/* 
+ * Transform uses of %TYPE in a statement.
+ */
+static Node *
+transformTypeRefs(ParseState *pstate, Node *stmt)
+{
+	switch (nodeTag(stmt))
+	{
+		case T_ProcedureStmt:
+		{
+			ProcedureStmt  *ps = (ProcedureStmt *) stmt;
+
+			transformTypeRefsList(pstate, ps->argTypes);
+			transformTypeRef(pstate, (TypeName *) ps->returnType);
+			transformTypeRefsList(pstate, ps->withClause);
+		}
+		break;
+
+		case T_CommentStmt:
+		{
+			CommentStmt	   *cs = (CommentStmt *) stmt;
+
+			transformTypeRefsList(pstate, cs->objlist);
+		}
+		break;
+
+		case T_RemoveFuncStmt:
+		{
+			RemoveFuncStmt *rs = (RemoveFuncStmt *) stmt;
+
+			transformTypeRefsList(pstate, rs->args);
+		}
+		break;
+
+		case T_DefineStmt:
+		{
+			DefineStmt *ds = (DefineStmt *) stmt;
+			List	   *ele;
+
+			foreach(ele, ds->definition)
+			{
+				DefElem	   *de = (DefElem *) lfirst(ele);
+
+				if (de->arg != NULL
+					&& IsA(de->arg, TypeName))
+				{
+					transformTypeRef(pstate, (TypeName *) de->arg);
+				}
+			}
+		}
+		break;
+
+		default:
+			elog(ERROR, "Unsupported type %d in transformTypeRefs",
+				 nodeTag(stmt));
+			break;
+	}
+
+	return stmt;
+}
+
+/*
+ * Transform uses of %TYPE in a list.
+ */
+static void
+transformTypeRefsList(ParseState *pstate, List *l)
+{
+	List	   *ele;
+
+	foreach(ele, l)
+	{
+		if (IsA(lfirst(ele), TypeName))
+			transformTypeRef(pstate, (TypeName *) lfirst(ele));
+	}
+}
+
+/*
+ * Transform a TypeName to not use %TYPE.
+ */
+static void
+transformTypeRef(ParseState *pstate, TypeName *tn)
+{
+	Attr   *att;
+	Node   *n;
+	Var	   *v;
+
+	if (tn->attrname == NULL)
+		return;
+	att = makeAttr(tn->name, tn->attrname);
+	n = transformExpr(pstate, (Node *) att, EXPR_COLUMN_FIRST);
+	if (! IsA(n, Var))
+		elog(ERROR, "unsupported expression in %%TYPE");
+	v = (Var *) n;
+	tn->name = typeidTypeName(v->vartype);
+	tn->typmod = v->vartypmod;
+	tn->attrname = NULL;
 }
 
 /* exported so planner can check again after rewriting, query pullup, etc */
Index: src/backend/parser/gram.y
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/parser/gram.y,v
retrieving revision 2.221
diff -p -u -r2.221 gram.y
--- src/backend/parser/gram.y	2001/02/18 18:06:10	2.221
+++ src/backend/parser/gram.y	2001/04/28 03:38:26
@@ -192,7 +192,7 @@ static void doNegateFloat(Value *v);
 		def_list, opt_indirection, group_clause, TriggerFuncArgs,
 		select_limit, opt_select_limit
 
-%type 	func_arg, func_return, aggr_argtype
+%type 	func_arg, func_return, func_type, aggr_argtype
 
 %type 	opt_arg, TriggerForOpt, TriggerForType, OptTemp
 
@@ -2462,7 +2462,7 @@ func_args_list:  func_arg
 				{	$$ = lappend($1, $3); }
 		;
 
-func_arg:  opt_arg Typename
+func_arg:  opt_arg func_type
 				{
 					/* We can catch over-specified arguments here if we want to,
 					 * but for now better to silently swallow typmod, etc.
@@ -2470,7 +2470,7 @@ func_arg:  opt_arg Typename
 					 */
 					$$ = $2;
 				}
-		| Typename
+		| func_type
 				{
 					$$ = $1;
 				}
@@ -2498,7 +2498,7 @@ func_as: Sconst
 				{ 	$$ = makeList2(makeString($1), makeString($3)); }
 		;
 
-func_return:  Typename
+func_return:  func_type
 				{
 					/* We can catch over-specified arguments here if we want to,
 					 * but for now better to silently swallow typmod, etc.
@@ -2508,6 +2508,18 @@ func_return:  Typename
 				}
 		;
 
+func_type:	Typename
+				{
+					$$ = $1;
+				}
+		| IDENT '.' ColId '%' TYPE_P
+				{
+					$$ = makeNode(TypeName);
+					$$->name = $1;
+					$$->typmod = -1;
+					$$->attrname = $3;
+				}
+		;
 
 /*****************************************************************************
  *
Index: src/backend/parser/parse_expr.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/parser/parse_expr.c,v
retrieving revision 1.92
diff -p -u -r1.92 parse_expr.c
--- src/backend/parser/parse_expr.c	2001/03/22 03:59:41	1.92
+++ src/backend/parser/parse_expr.c	2001/04/28 03:38:26
@@ -939,6 +939,7 @@ parser_typecast_expression(ParseState *p
 char *
 TypeNameToInternalName(TypeName *typename)
 {
+	Assert(typename->attrname == NULL);
 	if (typename->arrayBounds != NIL)
 	{
 
Index: src/test/regress/input/create_function_2.source
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/test/regress/input/create_function_2.source,v
retrieving revision 1.12
diff -p -u -r1.12 create_function_2.source
--- src/test/regress/input/create_function_2.source	2000/11/20 20:36:54	1.12
+++ src/test/regress/input/create_function_2.source	2001/04/28 03:38:27
@@ -13,6 +13,12 @@ CREATE FUNCTION hobby_construct(text, te
    LANGUAGE 'sql';
 
 
+CREATE FUNCTION hobbies_by_name(hobbies_r.name%TYPE)
+   RETURNS hobbies_r.person%TYPE
+   AS 'select person from hobbies_r where name = $1'
+   LANGUAGE 'sql';
+
+
 CREATE FUNCTION equipment(hobbies_r)
    RETURNS setof equipment_r
    AS 'select * from equipment_r where hobby = $1.name'
Index: src/test/regress/input/misc.source
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/test/regress/input/misc.source,v
retrieving revision 1.14
diff -p -u -r1.14 misc.source
--- src/test/regress/input/misc.source	2000/11/20 20:36:54	1.14
+++ src/test/regress/input/misc.source	2001/04/28 03:38:28
@@ -214,6 +214,7 @@ SELECT user_relns() AS user_relns
 
 --SELECT name(equipment(hobby_construct(text 'skywalking', text 'mer'))) AS equip_name;
 
+SELECT hobbies_by_name('basketball');
 
 --
 -- check that old-style C functions work properly with TOASTed values
Index: src/test/regress/output/create_function_2.source
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/test/regress/output/create_function_2.source,v
retrieving revision 1.13
diff -p -u -r1.13 create_function_2.source
--- src/test/regress/output/create_function_2.source	2000/11/20 20:36:54	1.13
+++ src/test/regress/output/create_function_2.source	2001/04/28 03:38:28
@@ -9,6 +9,10 @@ CREATE FUNCTION hobby_construct(text, te
    RETURNS hobbies_r
    AS 'select $1 as name, $2 as hobby'
    LANGUAGE 'sql';
+CREATE FUNCTION hobbies_by_name(hobbies_r.name%TYPE)
+   RETURNS hobbies_r.person%TYPE
+   AS 'select person from hobbies_r where name = $1'
+   LANGUAGE 'sql';
 CREATE FUNCTION equipment(hobbies_r)
    RETURNS setof equipment_r
    AS 'select * from equipment_r where hobby = $1.name'
Index: src/test/regress/output/misc.source
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/test/regress/output/misc.source,v
retrieving revision 1.27
diff -p -u -r1.27 misc.source
--- src/test/regress/output/misc.source	2000/11/20 20:36:54	1.27
+++ src/test/regress/output/misc.source	2001/04/28 03:38:28
@@ -656,6 +656,12 @@ SELECT user_relns() AS user_relns
 (90 rows)
 
 --SELECT name(equipment(hobby_construct(text 'skywalking', text 'mer'))) AS equip_name;
+SELECT hobbies_by_name('basketball');
+ hobbies_by_name 
+-----------------
+ joe
+(1 row)
+
 --
 -- check that old-style C functions work properly with TOASTed values
 --
Index: doc/src/sgml/ref/create_function.sgml
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/ref/create_function.sgml,v
retrieving revision 1.21
diff -p -u -r1.21 create_function.sgml
--- doc/src/sgml/ref/create_function.sgml	2000/12/25 23:15:26	1.21
+++ doc/src/sgml/ref/create_function.sgml	2001/04/28 03:38:31
@@ -58,10 +58,16 @@ CREATE FUNCTION tablename.columnname%TYPE;
+	using this can sometimes help make a function independent from
+	changes to the definition of a table.
        
       
      
@@ -72,7 +78,8 @@ CREATE FUNCTION 

Re: [HACKERS] Support for %TYPE in CREATE FUNCTION

От:
Roberto Mello <rmello@cc.usu.edu>
Дата:
On Fri, Apr 27, 2001 at 08:45:25PM -0700, Ian Lance Taylor wrote:
> This patch adds support for %TYPE in CREATE FUNCTION argument and
> return types.
> 
> %TYPE is already supported by PL/pgSQL when declaring variables.
> However, that does not help with the argument and return types in
> CREATE FUNCTION.
> 
> Using %TYPE makes it easier to write a function which is independent
> of the definition of a table.  That is, minor changes to the types
> used in the table may not require changes to the function.

	Wow! This would be _very_ useful! It's something I wish PostgreSQL 
had and I miss it everytime I write functions and remember PL/SQL.

	Thanks a lot Ian, I hope this one makes it in (hopefully for 7.1.1)

	-Roberto
-- 
+----| http://fslc.usu.edu USU Free Software & GNU/Linux Club |------+
  Roberto Mello - Computer Science, USU - http://www.brasileiro.net 
       http://www.sdl.usu.edu - Space Dynamics Lab, Developer    
������������������������-----�*'.     (Explosive Tagline)

Re: Re: [HACKERS] Support for %TYPE in CREATE FUNCTION

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:
> > Using %TYPE makes it easier to write a function which is independent
> > of the definition of a table.  That is, minor changes to the types
> > used in the table may not require changes to the function.
> 
> 	Wow! This would be _very_ useful! It's something I wish PostgreSQL 
> had and I miss it everytime I write functions and remember PL/SQL.
> 
> 	Thanks a lot Ian, I hope this one makes it in (hopefully for 7.1.1)

Sorry, only in 7.2.  No new features in minor releases unless they are
very safe.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Re: Re: [HACKERS] Support for %TYPE in CREATE FUNCTION

От:
Roberto Mello <rmello@cc.usu.edu>
Дата:
On Sat, Apr 28, 2001 at 06:45:39PM -0400, Bruce Momjian wrote:
> 
> Sorry, only in 7.2.  No new features in minor releases unless they are
> very safe.

	So how was that patch not safe?
	It sure would make porting Oracle apps to PostgreSQL _much_ easier.
	How far down the line is 7.2 (my guess is a few months away at least)? 
Is there a doc with what's planned for 7.2 somewhere? I know Jan Wieck
mentioned improvements in the procedural languages.

	-Roberto
-- 
+----| http://fslc.usu.edu USU Free Software & GNU/Linux Club |------+
  Roberto Mello - Computer Science, USU - http://www.brasileiro.net 
       http://www.sdl.usu.edu - Space Dynamics Lab, Developer    
Keyboard not connected, press F1 to continue.

Re: [HACKERS] Support for %TYPE in CREATE FUNCTION

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Sorry, looks like this patch has to be rejected because it can not
handle table changes.

> This patch adds support for %TYPE in CREATE FUNCTION argument and
> return types.
> 
> %TYPE is already supported by PL/pgSQL when declaring variables.
> However, that does not help with the argument and return types in
> CREATE FUNCTION.
> 
> Using %TYPE makes it easier to write a function which is independent
> of the definition of a table.  That is, minor changes to the types
> used in the table may not require changes to the function.
> 
> For example, this trivial function will work whenever `table' which
> has columns named `name' and `value', no matter what the types of the
> columns are.
> 
> CREATE FUNCTION lookup (table.name%TYPE)
>    RETURNS table.value%TYPE
>    AS 'select value from table where name = $1'
>    LANGUAGE 'sql';
> 
> This patch includes changes to the testsuite and the documentation.
> 
> This work was sponsored by Zembu.
> 
> Ian
> 
> Index: src/include/nodes/parsenodes.h
> ===================================================================
> RCS file: /home/projects/pgsql/cvsroot/pgsql/src/include/nodes/parsenodes.h,v
> retrieving revision 1.126
> diff -p -u -r1.126 parsenodes.h
> --- src/include/nodes/parsenodes.h	2001/03/23 04:49:56	1.126
> +++ src/include/nodes/parsenodes.h	2001/04/28 03:38:21
> @@ -945,6 +945,7 @@ typedef struct TypeName
>  	bool		setof;			/* is a set? */
>  	int32		typmod;			/* type modifier */
>  	List	   *arrayBounds;	/* array bounds */
> +	char	   *attrname;		/* field name when using %TYPE */
>  } TypeName;
>  
>  /*
> Index: src/backend/parser/analyze.c
> ===================================================================
> RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/parser/analyze.c,v
> retrieving revision 1.183
> diff -p -u -r1.183 analyze.c
> --- src/backend/parser/analyze.c	2001/03/22 06:16:15	1.183
> +++ src/backend/parser/analyze.c	2001/04/28 03:38:23
> @@ -27,6 +27,7 @@
>  #include "parser/parse_relation.h"
>  #include "parser/parse_target.h"
>  #include "parser/parse_type.h"
> +#include "parser/parse_expr.h"
>  #include "rewrite/rewriteManip.h"
>  #include "utils/builtins.h"
>  #include "utils/fmgroids.h"
> @@ -49,7 +50,10 @@ static Node *transformSetOperationTree(P
>  static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
>  static Query *transformCreateStmt(ParseState *pstate, CreateStmt *stmt);
>  static Query *transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt);
> +static Node *transformTypeRefs(ParseState *pstate, Node *stmt);
>  
> +static void transformTypeRefsList(ParseState *pstate, List *l);
> +static void transformTypeRef(ParseState *pstate, TypeName *tn);
>  static List *getSetColTypes(ParseState *pstate, Node *node);
>  static void transformForUpdate(Query *qry, List *forUpdate);
>  static void transformFkeyGetPrimaryKey(FkConstraint *fkconstraint);
> @@ -230,6 +234,18 @@ transformStmt(ParseState *pstate, Node *
>  											   (SelectStmt *) parseTree);
>  			break;
>  
> +			/*
> +			 * Convert use of %TYPE in statements where it is permitted.
> +			 */
> +		case T_ProcedureStmt:
> +		case T_CommentStmt:
> +		case T_RemoveFuncStmt:
> +		case T_DefineStmt:
> +			result = makeNode(Query);
> +			result->commandType = CMD_UTILITY;
> +			result->utilityStmt = transformTypeRefs(pstate, parseTree);
> +			break;
> +
>  		default:
>  
>  			/*
> @@ -2607,6 +2623,104 @@ transformAlterTableStmt(ParseState *psta
>  	}
>  	qry->utilityStmt = (Node *) stmt;
>  	return qry;
> +}
> +
> +/* 
> + * Transform uses of %TYPE in a statement.
> + */
> +static Node *
> +transformTypeRefs(ParseState *pstate, Node *stmt)
> +{
> +	switch (nodeTag(stmt))
> +	{
> +		case T_ProcedureStmt:
> +		{
> +			ProcedureStmt  *ps = (ProcedureStmt *) stmt;
> +
> +			transformTypeRefsList(pstate, ps->argTypes);
> +			transformTypeRef(pstate, (TypeName *) ps->returnType);
> +			transformTypeRefsList(pstate, ps->withClause);
> +		}
> +		break;
> +
> +		case T_CommentStmt:
> +		{
> +			CommentStmt	   *cs = (CommentStmt *) stmt;
> +
> +			transformTypeRefsList(pstate, cs->objlist);
> +		}
> +		break;
> +
> +		case T_RemoveFuncStmt:
> +		{
> +			RemoveFuncStmt *rs = (RemoveFuncStmt *) stmt;
> +
> +			transformTypeRefsList(pstate, rs->args);
> +		}
> +		break;
> +
> +		case T_DefineStmt:
> +		{
> +			DefineStmt *ds = (DefineStmt *) stmt;
> +			List	   *ele;
> +
> +			foreach(ele, ds->definition)
> +			{
> +				DefElem	   *de = (DefElem *) lfirst(ele);
> +
> +				if (de->arg != NULL
> +					&& IsA(de->arg, TypeName))
> +				{
> +					transformTypeRef(pstate, (TypeName *) de->arg);
> +				}
> +			}
> +		}
> +		break;
> +
> +		default:
> +			elog(ERROR, "Unsupported type %d in transformTypeRefs",
> +				 nodeTag(stmt));
> +			break;
> +	}
> +
> +	return stmt;
> +}
> +
> +/*
> + * Transform uses of %TYPE in a list.
> + */
> +static void
> +transformTypeRefsList(ParseState *pstate, List *l)
> +{
> +	List	   *ele;
> +
> +	foreach(ele, l)
> +	{
> +		if (IsA(lfirst(ele), TypeName))
> +			transformTypeRef(pstate, (TypeName *) lfirst(ele));
> +	}
> +}
> +
> +/*
> + * Transform a TypeName to not use %TYPE.
> + */
> +static void
> +transformTypeRef(ParseState *pstate, TypeName *tn)
> +{
> +	Attr   *att;
> +	Node   *n;
> +	Var	   *v;
> +
> +	if (tn->attrname == NULL)
> +		return;
> +	att = makeAttr(tn->name, tn->attrname);
> +	n = transformExpr(pstate, (Node *) att, EXPR_COLUMN_FIRST);
> +	if (! IsA(n, Var))
> +		elog(ERROR, "unsupported expression in %%TYPE");
> +	v = (Var *) n;
> +	tn->name = typeidTypeName(v->vartype);
> +	tn->typmod = v->vartypmod;
> +	tn->attrname = NULL;
>  }
>  
>  /* exported so planner can check again after rewriting, query pullup, etc */
> Index: src/backend/parser/gram.y
> ===================================================================
> RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/parser/gram.y,v
> retrieving revision 2.221
> diff -p -u -r2.221 gram.y
> --- src/backend/parser/gram.y	2001/02/18 18:06:10	2.221
> +++ src/backend/parser/gram.y	2001/04/28 03:38:26
> @@ -192,7 +192,7 @@ static void doNegateFloat(Value *v);
>  		def_list, opt_indirection, group_clause, TriggerFuncArgs,
>  		select_limit, opt_select_limit
>  
> -%type 	func_arg, func_return, aggr_argtype
> +%type 	func_arg, func_return, func_type, aggr_argtype
>  
>  %type 	opt_arg, TriggerForOpt, TriggerForType, OptTemp
>  
> @@ -2462,7 +2462,7 @@ func_args_list:  func_arg
>  				{	$$ = lappend($1, $3); }
>  		;
>  
> -func_arg:  opt_arg Typename
> +func_arg:  opt_arg func_type
>  				{
>  					/* We can catch over-specified arguments here if we want to,
>  					 * but for now better to silently swallow typmod, etc.
> @@ -2470,7 +2470,7 @@ func_arg:  opt_arg Typename
>  					 */
>  					$$ = $2;
>  				}
> -		| Typename
> +		| func_type
>  				{
>  					$$ = $1;
>  				}
> @@ -2498,7 +2498,7 @@ func_as: Sconst
>  				{ 	$$ = makeList2(makeString($1), makeString($3)); }
>  		;
>  
> -func_return:  Typename
> +func_return:  func_type
>  				{
>  					/* We can catch over-specified arguments here if we want to,
>  					 * but for now better to silently swallow typmod, etc.
> @@ -2508,6 +2508,18 @@ func_return:  Typename
>  				}
>  		;
>  
> +func_type:	Typename
> +				{
> +					$$ = $1;
> +				}
> +		| IDENT '.' ColId '%' TYPE_P
> +				{
> +					$$ = makeNode(TypeName);
> +					$$->name = $1;
> +					$$->typmod = -1;
> +					$$->attrname = $3;
> +				}
> +		;
>  
>  /*****************************************************************************
>   *
> Index: src/backend/parser/parse_expr.c
> ===================================================================
> RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/parser/parse_expr.c,v
> retrieving revision 1.92
> diff -p -u -r1.92 parse_expr.c
> --- src/backend/parser/parse_expr.c	2001/03/22 03:59:41	1.92
> +++ src/backend/parser/parse_expr.c	2001/04/28 03:38:26
> @@ -939,6 +939,7 @@ parser_typecast_expression(ParseState *p
>  char *
>  TypeNameToInternalName(TypeName *typename)
>  {
> +	Assert(typename->attrname == NULL);
>  	if (typename->arrayBounds != NIL)
>  	{
>  
> Index: src/test/regress/input/create_function_2.source
> ===================================================================
> RCS file: /home/projects/pgsql/cvsroot/pgsql/src/test/regress/input/create_function_2.source,v
> retrieving revision 1.12
> diff -p -u -r1.12 create_function_2.source
> --- src/test/regress/input/create_function_2.source	2000/11/20 20:36:54	1.12
> +++ src/test/regress/input/create_function_2.source	2001/04/28 03:38:27
> @@ -13,6 +13,12 @@ CREATE FUNCTION hobby_construct(text, te
>     LANGUAGE 'sql';
>  
>  
> +CREATE FUNCTION hobbies_by_name(hobbies_r.name%TYPE)
> +   RETURNS hobbies_r.person%TYPE
> +   AS 'select person from hobbies_r where name = $1'
> +   LANGUAGE 'sql';
> +
> +
>  CREATE FUNCTION equipment(hobbies_r)
>     RETURNS setof equipment_r
>     AS 'select * from equipment_r where hobby = $1.name'
> Index: src/test/regress/input/misc.source
> ===================================================================
> RCS file: /home/projects/pgsql/cvsroot/pgsql/src/test/regress/input/misc.source,v
> retrieving revision 1.14
> diff -p -u -r1.14 misc.source
> --- src/test/regress/input/misc.source	2000/11/20 20:36:54	1.14
> +++ src/test/regress/input/misc.source	2001/04/28 03:38:28
> @@ -214,6 +214,7 @@ SELECT user_relns() AS user_relns
>  
>  --SELECT name(equipment(hobby_construct(text 'skywalking', text 'mer'))) AS equip_name;
>  
> +SELECT hobbies_by_name('basketball');
>  
>  --
>  -- check that old-style C functions work properly with TOASTed values
> Index: src/test/regress/output/create_function_2.source
> ===================================================================
> RCS file: /home/projects/pgsql/cvsroot/pgsql/src/test/regress/output/create_function_2.source,v
> retrieving revision 1.13
> diff -p -u -r1.13 create_function_2.source
> --- src/test/regress/output/create_function_2.source	2000/11/20 20:36:54	1.13
> +++ src/test/regress/output/create_function_2.source	2001/04/28 03:38:28
> @@ -9,6 +9,10 @@ CREATE FUNCTION hobby_construct(text, te
>     RETURNS hobbies_r
>     AS 'select $1 as name, $2 as hobby'
>     LANGUAGE 'sql';
> +CREATE FUNCTION hobbies_by_name(hobbies_r.name%TYPE)
> +   RETURNS hobbies_r.person%TYPE
> +   AS 'select person from hobbies_r where name = $1'
> +   LANGUAGE 'sql';
>  CREATE FUNCTION equipment(hobbies_r)
>     RETURNS setof equipment_r
>     AS 'select * from equipment_r where hobby = $1.name'
> Index: src/test/regress/output/misc.source
> ===================================================================
> RCS file: /home/projects/pgsql/cvsroot/pgsql/src/test/regress/output/misc.source,v
> retrieving revision 1.27
> diff -p -u -r1.27 misc.source
> --- src/test/regress/output/misc.source	2000/11/20 20:36:54	1.27
> +++ src/test/regress/output/misc.source	2001/04/28 03:38:28
> @@ -656,6 +656,12 @@ SELECT user_relns() AS user_relns
>  (90 rows)
>  
>  --SELECT name(equipment(hobby_construct(text 'skywalking', text 'mer'))) AS equip_name;
> +SELECT hobbies_by_name('basketball');
> + hobbies_by_name 
> +-----------------
> + joe
> +(1 row)
> +
>  --
>  -- check that old-style C functions work properly with TOASTed values
>  --
> Index: doc/src/sgml/ref/create_function.sgml
> ===================================================================
> RCS file: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/ref/create_function.sgml,v
> retrieving revision 1.21
> diff -p -u -r1.21 create_function.sgml
> --- doc/src/sgml/ref/create_function.sgml	2000/12/25 23:15:26	1.21
> +++ doc/src/sgml/ref/create_function.sgml	2001/04/28 03:38:31
> @@ -58,10 +58,16 @@ CREATE FUNCTION tablename. +	class="parameter">columnname%TYPE;
> +	using this can sometimes help make a function independent from
> +	changes to the definition of a table.
>         
>        
>       
> @@ -72,7 +78,8 @@ CREATE FUNCTION 

Re: [HACKERS] Support for %TYPE in CREATE FUNCTION

От:
Ian Lance Taylor <ian@airs.com>
Дата:
Bruce Momjian  writes:

> Sorry, looks like this patch has to be rejected because it can not
> handle table changes.

> > This patch adds support for %TYPE in CREATE FUNCTION argument and
> > return types.

Does anybody want to suggest how to handle table changes?  Does
anybody want to work with me to make this patch acceptable?  Or is
this functionality of no interest to the Postgres development team?

Ian

Re: [HACKERS] Support for %TYPE in CREATE FUNCTION

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:
> Bruce Momjian  writes:
> 
> > Sorry, looks like this patch has to be rejected because it can not
> > handle table changes.
> 
> > > This patch adds support for %TYPE in CREATE FUNCTION argument and
> > > return types.
> 
> Does anybody want to suggest how to handle table changes?  Does
> anybody want to work with me to make this patch acceptable?  Or is
> this functionality of no interest to the Postgres development team?

I think the major problem was that our pg_proc table doesn't have any
way of handling arg changes.  In fact, we need a ALTER FUNCTION
capability first so we can recreate functions in place with the same
OID.  We may then be able to recreate the function on table change, but
I think we will need this TODO item done also:

	* Add pg_depend table to track object dependencies

So it seems we need two items done first, then we would have the tools
to properly implement this functionality.

So, yes, the functionality is desired, but it has to be done with the
proper groundwork already in place.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Re: [HACKERS] Support for %TYPE in CREATE FUNCTION

От:
Don Baccus <dhogaza@pacifier.com>
Дата:
At 03:47 PM 5/28/01 -0700, Ian Lance Taylor wrote:
>Bruce Momjian  writes:
>
>> Sorry, looks like this patch has to be rejected because it can not
>> handle table changes.
>
>> > This patch adds support for %TYPE in CREATE FUNCTION argument and
>> > return types.
>
>Does anybody want to suggest how to handle table changes?  Does
>anybody want to work with me to make this patch acceptable?  Or is
>this functionality of no interest to the Postgres development team?

I don't know about the Postgres development team, but it is of great
interest to the OpenACS project team.  We've got hundreds or perhaps
thousands of PL/SQL procs and funcs in our code base that use this
notation and it would be very, very nice if we could use this construct
in our PostgreSQL code base.

I suspect any organization or project attempting to either migrate
from Oracle to Postgres or trying to support both databases (as we
do at OpenACS) will find this very useful.

We're deep in the midst of our rewrite of the Ars Digita code base that
we've inherited so don't have any resources to offer to help solve the
problem.  

But we can offer encouragement and appreciation!



- Don Baccus, Portland OR 
  Nature photos, on-line guides, Pacific Northwest
  Rare Bird Alert Service and other goodies at
  http://donb.photo.net.
FAQ