This patch refactors execTuples.c in two ways:

     (1) ExecInitXXXResultTupleSlot() used a macro to avoid some
         duplicated code, whereas calling ExecInitExtraTupleSlot() would
         make the code more clear.

     (2) ExecTypeFromTL() and ExecCleanTypeFromTL() duplicated a bunch
         of code; I added a new function ExecTypeFromTLInternal() and
         re-implemented these functions in terms of calls to it.

As a result, ExecInitScanTupleSlot(), ExecInitResultTupleSlot(),
ExecTypeFromTL(), and ExecCleanTypeFromTL() are now all trivial
(1 line) functions. I could have replaced these with macros, but I
didn't: does anyone thinks that would be worth doing?

-Neil
Index: src/backend/executor/execTuples.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/backend/executor/execTuples.c,v
retrieving revision 1.72
diff -c -r1.72 execTuples.c
*** src/backend/executor/execTuples.c	29 Sep 2003 18:22:48 -0000	1.72
--- src/backend/executor/execTuples.c	20 Nov 2003 02:42:17 -0000
***************
*** 112,117 ****
--- 112,119 ----
  #include "executor/executor.h"
  #include "utils/lsyscache.h"
  
+ static TupleDesc ExecTypeFromTLInternal(List *targetList,
+ 										bool hasoid, bool skipjunk);
  
  /* ----------------------------------------------------------------
   *				  tuple table create/delete functions
***************
*** 469,481 ****
   *		is used for initializing special-purpose slots.
   * --------------------------------
   */
- #define INIT_SLOT_DEFS \
- 	TupleTable	   tupleTable; \
- 	TupleTableSlot*   slot
- 
- #define INIT_SLOT_ALLOC \
- 	tupleTable = (TupleTable) estate->es_tupleTable; \
- 	slot =		 ExecAllocTableSlot(tupleTable);
  
  /* ----------------
   *		ExecInitResultTupleSlot
--- 471,476 ----
***************
*** 484,492 ****
  void
  ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
  {
! 	INIT_SLOT_DEFS;
! 	INIT_SLOT_ALLOC;
! 	planstate->ps_ResultTupleSlot = slot;
  }
  
  /* ----------------
--- 479,485 ----
  void
  ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
  {
! 	planstate->ps_ResultTupleSlot = ExecInitExtraTupleSlot(estate);
  }
  
  /* ----------------
***************
*** 496,504 ****
  void
  ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
  {
! 	INIT_SLOT_DEFS;
! 	INIT_SLOT_ALLOC;
! 	scanstate->ss_ScanTupleSlot = slot;
  }
  
  /* ----------------
--- 489,495 ----
  void
  ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
  {
! 	scanstate->ss_ScanTupleSlot = ExecInitExtraTupleSlot(estate);
  }
  
  /* ----------------
***************
*** 508,516 ****
  TupleTableSlot *
  ExecInitExtraTupleSlot(EState *estate)
  {
! 	INIT_SLOT_DEFS;
! 	INIT_SLOT_ALLOC;
! 	return slot;
  }
  
  /* ----------------
--- 499,505 ----
  TupleTableSlot *
  ExecInitExtraTupleSlot(EState *estate)
  {
! 	return ExecAllocTableSlot(estate->es_tupleTable);
  }
  
  /* ----------------
***************
*** 560,593 ****
  TupleDesc
  ExecTypeFromTL(List *targetList, bool hasoid)
  {
! 	TupleDesc	typeInfo;
! 	List	   *tlitem;
! 	int			len;
! 
! 	/*
! 	 * allocate a new typeInfo
! 	 */
! 	len = ExecTargetListLength(targetList);
! 	typeInfo = CreateTemplateTupleDesc(len, hasoid);
! 
! 	/*
! 	 * scan list, generate type info for each entry
! 	 */
! 	foreach(tlitem, targetList)
! 	{
! 		TargetEntry *tle = lfirst(tlitem);
! 		Resdom	   *resdom = tle->resdom;
! 
! 		TupleDescInitEntry(typeInfo,
! 						   resdom->resno,
! 						   resdom->resname,
! 						   resdom->restype,
! 						   resdom->restypmod,
! 						   0,
! 						   false);
! 	}
! 
! 	return typeInfo;
  }
  
  /* ----------------------------------------------------------------
--- 549,555 ----
  TupleDesc
  ExecTypeFromTL(List *targetList, bool hasoid)
  {
! 	return ExecTypeFromTLInternal(targetList, hasoid, false);
  }
  
  /* ----------------------------------------------------------------
***************
*** 599,628 ****
  TupleDesc
  ExecCleanTypeFromTL(List *targetList, bool hasoid)
  {
! 	TupleDesc	typeInfo;
! 	List	   *tlitem;
! 	int			len;
! 	int			cleanresno;
  
! 	/*
! 	 * allocate a new typeInfo
! 	 */
! 	len = ExecCleanTargetListLength(targetList);
  	typeInfo = CreateTemplateTupleDesc(len, hasoid);
  
! 	/*
! 	 * scan list, generate type info for each entry
! 	 */
! 	cleanresno = 1;
! 	foreach(tlitem, targetList)
  	{
! 		TargetEntry *tle = lfirst(tlitem);
! 		Resdom	   *resdom = tle->resdom;
  
! 		if (resdom->resjunk)
  			continue;
  		TupleDescInitEntry(typeInfo,
! 						   cleanresno++,
  						   resdom->resname,
  						   resdom->restype,
  						   resdom->restypmod,
--- 561,592 ----
  TupleDesc
  ExecCleanTypeFromTL(List *targetList, bool hasoid)
  {
! 	return ExecTypeFromTLInternal(targetList, hasoid, true);
! }
  
! static TupleDesc
! ExecTypeFromTLInternal(List *targetList, bool hasoid, bool skipjunk)
! {
! 	TupleDesc	 typeInfo;
! 	List		*l;
! 	int			 len;
! 	int			 cur_resno = 1;
! 
! 	if (skipjunk)
! 		len = ExecCleanTargetListLength(targetList);
! 	else
! 		len = ExecTargetListLength(targetList);
  	typeInfo = CreateTemplateTupleDesc(len, hasoid);
  
! 	foreach(l, targetList)
  	{
! 		TargetEntry	*tle = lfirst(l);
! 		Resdom		*resdom = tle->resdom;
  
! 		if (skipjunk && resdom->resjunk)
  			continue;
  		TupleDescInitEntry(typeInfo,
! 						   cur_resno++,
  						   resdom->resname,
  						   resdom->restype,
  						   resdom->restypmod,
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

               http://archives.postgresql.org

Reply via email to