copyfuncs.c 38.1 KB
Newer Older
1
/*-------------------------------------------------------------------------
2 3
 *
 * copyfuncs.c--
4
 *	  Copy functions for Postgres tree nodes.
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
10
 *	  $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.63 1999/02/08 04:29:03 momjian Exp $
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *-------------------------------------------------------------------------
 */
#include <stdio.h>
#include <string.h>

#include "postgres.h"

#include "nodes/pg_list.h"
#include "nodes/execnodes.h"
#include "nodes/plannodes.h"
#include "nodes/parsenodes.h"
#include "nodes/primnodes.h"
#include "nodes/relation.h"

#include "utils/syscache.h"
27
#include "utils/builtins.h"		/* for namecpy */
28 29 30 31
#include "utils/elog.h"
#include "utils/palloc.h"
#include "catalog/pg_type.h"
#include "storage/lmgr.h"
32
#include "optimizer/planmain.h"
33 34 35

/*
 * listCopy--
36 37
 *	  this copy function only copies the "lcons-cells" of the list but not
 *	  its contents. (good for list of pointers as well as list of integers).
38
 */
39
List *
40
listCopy(List *list)
41
{
42 43 44
	List	   *newlist = NIL;
	List	   *l,
			   *nl = NIL;
45 46 47 48 49 50 51 52 53 54

	foreach(l, list)
	{
		if (newlist == NIL)
			newlist = nl = lcons(lfirst(l), NIL);
		else
		{
			lnext(nl) = lcons(lfirst(l), NIL);
			nl = lnext(nl);
		}
55
	}
56
	return newlist;
57
}
58

59 60
/*
 * Node_Copy--
61
 *	  a macro to simplify calling of copyObject on the specified field
62 63
 */
#define Node_Copy(from, newnode, field) \
64
	newnode->field = copyObject(from->field)
65 66

/* ****************************************************************
67
 *					 plannodes.h copy functions
68 69 70 71
 * ****************************************************************
 */

/* ----------------
72
 *		CopyPlanFields
73
 *
74 75
 *		This function copies the fields of the Plan node.  It is used by
 *		all the copy functions for classes which inherit from Plan.
76 77 78
 * ----------------
 */
static void
79
CopyPlanFields(Plan *from, Plan *newnode)
80
{
81 82
	extern List *SS_pull_subplan(void *expr);

83 84 85
	newnode->cost = from->cost;
	newnode->plan_size = from->plan_size;
	newnode->plan_width = from->plan_width;
B
Bruce Momjian 已提交
86
	newnode->plan_tupperpage = from->plan_tupperpage;
87 88 89 90
	newnode->targetlist = copyObject(from->targetlist);
	newnode->qual = copyObject(from->qual);
	newnode->lefttree = copyObject(from->lefttree);
	newnode->righttree = copyObject(from->righttree);
91 92 93
	newnode->extParam = listCopy(from->extParam);
	newnode->locParam = listCopy(from->locParam);
	newnode->chgParam = listCopy(from->chgParam);
V
Vadim B. Mikheev 已提交
94
	Node_Copy(from, newnode, initPlan);
95 96
	if (from->subPlan != NULL)
		newnode->subPlan = SS_pull_subplan(newnode->qual);
V
Vadim B. Mikheev 已提交
97 98 99
	else
		newnode->subPlan = NULL;
	newnode->nParamExec = from->nParamExec;
100 101 102
}

/* ----------------
103
 *		_copyPlan
104 105
 * ----------------
 */
106
static Plan *
107
_copyPlan(Plan *from)
108
{
109
	Plan	   *newnode = makeNode(Plan);
110 111 112 113 114 115 116 117

	/* ----------------
	 *	copy the node superclass fields
	 * ----------------
	 */
	CopyPlanFields(from, newnode);

	return newnode;
118 119 120 121
}


/* ----------------
122
 *		_copyResult
123 124
 * ----------------
 */
125
static Result *
126
_copyResult(Result *from)
127
{
128
	Result	   *newnode = makeNode(Result);
129 130 131 132 133 134 135 136 137 138 139 140 141 142

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	Node_Copy(from, newnode, resconstantqual);

	return newnode;
143 144 145
}

/* ----------------
146
 *		_copyAppend
147 148
 * ----------------
 */
149
static Append *
B
Bruce Momjian 已提交
150
_copyAppend(Append *from)
151
{
152
	Append	   *newnode = makeNode(Append);
153 154 155 156 157 158 159 160 161 162 163

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
164 165 166 167
	Node_Copy(from, newnode, appendplans);
	Node_Copy(from, newnode, unionrtables);
	newnode->inheritrelid = from->inheritrelid;
	Node_Copy(from, newnode, inheritrtable);
168 169

	return newnode;
170 171 172 173
}


/* ----------------
174
 *		CopyScanFields
175
 *
176 177
 *		This function copies the fields of the Scan node.  It is used by
 *		all the copy functions for classes which inherit from Scan.
178 179 180
 * ----------------
 */
static void
181
CopyScanFields(Scan *from, Scan *newnode)
182
{
183 184
	newnode->scanrelid = from->scanrelid;
	return;
185 186 187
}

/* ----------------
188
 *		_copyScan
189 190
 * ----------------
 */
191
static Scan *
192
_copyScan(Scan *from)
193
{
194
	Scan	   *newnode = makeNode(Scan);
195 196 197 198 199 200

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
B
Bruce Momjian 已提交
201
	CopyScanFields((Scan *) from, (Scan *) newnode);
202 203

	return newnode;
204 205 206
}

/* ----------------
207
 *		_copySeqScan
208 209 210
 * ----------------
 */
static SeqScan *
211
_copySeqScan(SeqScan *from)
212
{
213
	SeqScan    *newnode = makeNode(SeqScan);
214 215 216 217 218 219 220 221 222

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyScanFields((Scan *) from, (Scan *) newnode);

	return newnode;
223 224 225
}

/* ----------------
226
 *		_copyIndexScan
227 228 229
 * ----------------
 */
static IndexScan *
230
_copyIndexScan(IndexScan *from)
231
{
232
	IndexScan  *newnode = makeNode(IndexScan);
233 234 235 236 237 238 239 240 241 242 243 244 245 246

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyScanFields((Scan *) from, (Scan *) newnode);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->indxid = listCopy(from->indxid);
	Node_Copy(from, newnode, indxqual);
V
Vadim B. Mikheev 已提交
247
	Node_Copy(from, newnode, indxqualorig);
248 249

	return newnode;
250 251 252
}

/* ----------------
253
 *		CopyJoinFields
254
 *
255 256
 *		This function copies the fields of the Join node.  It is used by
 *		all the copy functions for classes which inherit from Join.
257 258 259
 * ----------------
 */
static void
260
CopyJoinFields(Join *from, Join *newnode)
261
{
262 263
	/* nothing extra */
	return;
264 265 266 267
}


/* ----------------
268
 *		_copyJoin
269 270
 * ----------------
 */
271
static Join *
272
_copyJoin(Join *from)
273
{
274
	Join	   *newnode = makeNode(Join);
275 276 277 278 279 280 281 282 283

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyJoinFields(from, newnode);

	return newnode;
284 285 286 287
}


/* ----------------
288
 *		_copyNestLoop
289 290 291
 * ----------------
 */
static NestLoop *
292
_copyNestLoop(NestLoop *from)
293
{
294
	NestLoop   *newnode = makeNode(NestLoop);
295 296 297 298 299 300 301 302 303

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyJoinFields((Join *) from, (Join *) newnode);

	return newnode;
304 305 306 307
}


/* ----------------
308
 *		_copyMergeJoin
309 310 311
 * ----------------
 */
static MergeJoin *
312
_copyMergeJoin(MergeJoin *from)
313
{
314
	MergeJoin  *newnode = makeNode(MergeJoin);
315 316 317 318 319 320 321 322 323 324 325 326 327 328

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyJoinFields((Join *) from, (Join *) newnode);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	Node_Copy(from, newnode, mergeclauses);

329
	newnode->mergejoinop = from->mergejoinop;
330 331 332 333 334 335 336 337 338 339

	newnode->mergerightorder = (Oid *) palloc(sizeof(Oid) * 2);
	newnode->mergerightorder[0] = from->mergerightorder[0];
	newnode->mergerightorder[1] = 0;

	newnode->mergeleftorder = (Oid *) palloc(sizeof(Oid) * 2);
	newnode->mergeleftorder[0] = from->mergeleftorder[0];
	newnode->mergeleftorder[1] = 0;

	return newnode;
340 341 342
}

/* ----------------
343
 *		_copyHashJoin
344 345 346
 * ----------------
 */
static HashJoin *
347
_copyHashJoin(HashJoin *from)
348
{
349
	HashJoin   *newnode = makeNode(HashJoin);
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyJoinFields((Join *) from, (Join *) newnode);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	Node_Copy(from, newnode, hashclauses);

	newnode->hashjoinop = from->hashjoinop;

366
	/* both are unused !.. */
367 368 369 370
	newnode->hashjointablekey = from->hashjointablekey;
	newnode->hashjointablesize = from->hashjointablesize;

	return newnode;
371 372 373 374
}


/* ----------------
375
 *		CopyTempFields
376
 *
377 378
 *		This function copies the fields of the Temp node.  It is used by
 *		all the copy functions for classes which inherit from Temp.
379 380 381
 * ----------------
 */
static void
382
CopyTempFields(Temp *from, Temp *newnode)
383
{
384 385 386
	newnode->tempid = from->tempid;
	newnode->keycount = from->keycount;
	return;
387 388 389 390
}


/* ----------------
391
 *		_copyTemp
392 393
 * ----------------
 */
394
static Temp *
395
_copyTemp(Temp *from)
396
{
397
	Temp	   *newnode = makeNode(Temp);
398 399 400 401 402 403 404 405 406

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyTempFields(from, newnode);

	return newnode;
407 408 409
}

/* ----------------
410
 *		_copyMaterial
411 412 413
 * ----------------
 */
static Material *
414
_copyMaterial(Material *from)
415
{
416
	Material   *newnode = makeNode(Material);
417 418 419 420 421 422 423 424 425

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyTempFields((Temp *) from, (Temp *) newnode);

	return newnode;
426 427 428 429
}


/* ----------------
430
 *		_copySort
431 432
 * ----------------
 */
433
static Sort *
434
_copySort(Sort *from)
435
{
436
	Sort	   *newnode = makeNode(Sort);
437 438 439 440 441 442 443 444 445

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyTempFields((Temp *) from, (Temp *) newnode);

	return newnode;
446 447
}

V
Vadim B. Mikheev 已提交
448 449 450 451 452 453 454 455 456

/* ----------------
 *		_copyGroup
 * ----------------
 */
static Group *
_copyGroup(Group *from)
{
	Group	   *newnode = makeNode(Group);
457

V
Vadim B. Mikheev 已提交
458
	CopyPlanFields((Plan *) from, (Plan *) newnode);
B
Bruce Momjian 已提交
459

V
Vadim B. Mikheev 已提交
460 461
	newnode->tuplePerGroup = from->tuplePerGroup;
	newnode->numCols = from->numCols;
462 463
	newnode->grpColIdx = palloc(from->numCols * sizeof(AttrNumber));
	memcpy(newnode->grpColIdx, from->grpColIdx, from->numCols * sizeof(AttrNumber));
V
Vadim B. Mikheev 已提交
464 465 466 467

	return newnode;
}

468
/* ---------------
469
 *	_copyAgg
470 471
 * --------------
 */
472
static Agg *
B
Bruce Momjian 已提交
473
_copyAgg(Agg *from)
474
{
475
	Agg		   *newnode = makeNode(Agg);
476 477 478

	CopyPlanFields((Plan *) from, (Plan *) newnode);

479
	newnode->aggs = get_agg_tlist_references(newnode);
480 481

	return newnode;
482 483
}

484 485 486 487 488 489 490
/* ---------------
 *	_copyGroupClause
 * --------------
 */
static GroupClause *
_copyGroupClause(GroupClause *from)
{
491
	GroupClause *newnode = makeNode(GroupClause);
492

493 494
	Node_Copy(from, newnode, entry);
	newnode->grpOpoid = from->grpOpoid;
495

496
	return newnode;
497 498
}

499 500

/* ----------------
501
 *		_copyUnique
502 503
 * ----------------
 */
504
static Unique *
505
_copyUnique(Unique *from)
506
{
507
	Unique	   *newnode = makeNode(Unique);
508 509 510 511 512 513 514 515 516 517 518 519

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);
	CopyTempFields((Temp *) from, (Temp *) newnode);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
520 521 522 523
	if (newnode->uniqueAttr)
		newnode->uniqueAttr = pstrdup(from->uniqueAttr);
	else
		newnode->uniqueAttr = NULL;
B
Bruce Momjian 已提交
524
	newnode->uniqueAttrNum = from->uniqueAttrNum;
525 526

	return newnode;
527 528 529 530
}


/* ----------------
531
 *		_copyHash
532 533
 * ----------------
 */
534
static Hash *
535
_copyHash(Hash *from)
536
{
537
	Hash	   *newnode = makeNode(Hash);
538 539 540 541 542 543 544 545 546 547 548 549 550

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	CopyPlanFields((Plan *) from, (Plan *) newnode);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	Node_Copy(from, newnode, hashkey);

551
	/* both are unused !.. */
552 553 554 555
	newnode->hashtablekey = from->hashtablekey;
	newnode->hashtablesize = from->hashtablesize;

	return newnode;
556 557
}

V
Vadim B. Mikheev 已提交
558 559 560
static SubPlan *
_copySubPlan(SubPlan *from)
{
561 562
	SubPlan    *newnode = makeNode(SubPlan);

V
Vadim B. Mikheev 已提交
563 564 565
	Node_Copy(from, newnode, plan);
	newnode->plan_id = from->plan_id;
	Node_Copy(from, newnode, rtable);
566 567
	newnode->setParam = listCopy(from->setParam);
	newnode->parParam = listCopy(from->parParam);
V
Vadim B. Mikheev 已提交
568 569 570 571 572
	Node_Copy(from, newnode, sublink);

	return newnode;
}

573
/* ****************************************************************
574
 *					   primnodes.h copy functions
575 576 577 578
 * ****************************************************************
 */

/* ----------------
579
 *		_copyResdom
580 581
 * ----------------
 */
582
static Resdom *
583
_copyResdom(Resdom *from)
584
{
585
	Resdom	   *newnode = makeNode(Resdom);
586 587 588

	newnode->resno = from->resno;
	newnode->restype = from->restype;
589
	newnode->restypmod = from->restypmod;
590 591

	if (from->resname != NULL)
B
Bruce Momjian 已提交
592
		newnode->resname = pstrdup(from->resname);
593 594 595 596 597
	newnode->reskey = from->reskey;
	newnode->reskeyop = from->reskeyop;
	newnode->resjunk = from->resjunk;

	return newnode;
598 599
}

600
static Fjoin *
601
_copyFjoin(Fjoin *from)
602
{
603
	Fjoin	   *newnode = makeNode(Fjoin);
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */

	newnode->fj_initialized = from->fj_initialized;
	newnode->fj_nNodes = from->fj_nNodes;

	Node_Copy(from, newnode, fj_innerNode);

	newnode->fj_results = (DatumPtr)
		palloc((from->fj_nNodes) * sizeof(Datum));
	memmove(from->fj_results,
			newnode->fj_results,
			(from->fj_nNodes) * sizeof(Datum));

B
Bruce Momjian 已提交
621 622
	newnode->fj_alwaysDone = (BoolPtr)
		palloc((from->fj_nNodes) * sizeof(bool));
623 624 625 626 627 628
	memmove(from->fj_alwaysDone,
			newnode->fj_alwaysDone,
			(from->fj_nNodes) * sizeof(bool));


	return newnode;
629 630 631
}

/* ----------------
632
 *		_copyExpr
633 634
 * ----------------
 */
635
static Expr *
636
_copyExpr(Expr *from)
637
{
638
	Expr	   *newnode = makeNode(Expr);
639 640 641 642 643 644 645 646 647 648 649 650

	/* ----------------
	 *	copy node superclass fields
	 * ----------------
	 */
	newnode->typeOid = from->typeOid;
	newnode->opType = from->opType;

	Node_Copy(from, newnode, oper);
	Node_Copy(from, newnode, args);

	return newnode;
651 652 653
}

/* ----------------
654
 *		_copyVar
655 656
 * ----------------
 */
657
static Var *
658
_copyVar(Var *from)
659
{
660
	Var		   *newnode = makeNode(Var);
661 662 663 664 665 666 667 668

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->varno = from->varno;
	newnode->varattno = from->varattno;
	newnode->vartype = from->vartype;
669
	newnode->vartypmod = from->vartypmod;
670
	newnode->varlevelsup = from->varlevelsup;
671 672 673 674 675

	newnode->varnoold = from->varnoold;
	newnode->varoattno = from->varoattno;

	return newnode;
676 677 678
}

/* ----------------
679
 *		_copyOper
680 681
 * ----------------
 */
682
static Oper *
683
_copyOper(Oper *from)
684
{
685
	Oper	   *newnode = makeNode(Oper);
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->opno = from->opno;
	newnode->opid = from->opid;
	newnode->opresulttype = from->opresulttype;
	newnode->opsize = from->opsize;

	/*
	 * NOTE: shall we copy the cache structure or just the pointer ?
	 * Alternatively we can set 'op_fcache' to NULL, in which case the
	 * executor will initialize it when it needs it...
	 */
	newnode->op_fcache = from->op_fcache;

	return newnode;
704 705 706
}

/* ----------------
707
 *		_copyConst
708 709
 * ----------------
 */
710
static Const *
711
_copyConst(Const *from)
712
{
713 714
	static Oid	cached_type;
	static bool cached_typbyval;
715

716
	Const	   *newnode = makeNode(Const);
717

718
	/* ----------------
719
	 *	copy remainder of node
720 721
	 * ----------------
	 */
722 723 724
	newnode->consttype = from->consttype;
	newnode->constlen = from->constlen;

725
	/* ----------------
726 727
	 *	XXX super cheesy hack until parser/planner
	 *	puts in the right values here.
B
Bruce Momjian 已提交
728
	 *
729
	 *	But I like cheese.
730 731
	 * ----------------
	 */
B
Bruce Momjian 已提交
732
	if (!from->constisnull && cached_type != from->consttype)
733
	{
734
		HeapTuple	typeTuple;
735
		Form_pg_type typeStruct;
736 737 738 739 740 741 742 743 744 745 746

		/* ----------------
		 *	 get the type tuple corresponding to the paramList->type,
		 *	 If this fails, returnValue has been pre-initialized
		 *	 to "null" so we just return it.
		 * ----------------
		 */
		typeTuple = SearchSysCacheTuple(TYPOID,
										ObjectIdGetDatum(from->consttype),
										0, 0, 0);

747
		/* ----------------
748 749
		 *	 get the type length and by-value from the type tuple and
		 *	 save the information in our one element cache.
750 751
		 * ----------------
		 */
752 753
		Assert(PointerIsValid(typeTuple));

754
		typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
755 756 757 758 759 760 761 762
		cached_typbyval = (typeStruct)->typbyval ? true : false;
		cached_type = from->consttype;
	}

	from->constbyval = cached_typbyval;

	if (!from->constisnull)
	{
763
		/* ----------------
764 765 766
		 *		copying the Datum in a const node is a bit trickier
		 *	because it might be a pointer and it might also be of
		 *	variable length...
767 768
		 * ----------------
		 */
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
		if (from->constbyval == true)
		{
			/* ----------------
			 *	passed by value so just copy the datum.
			 * ----------------
			 */
			newnode->constvalue = from->constvalue;
		}
		else
		{
			/* ----------------
			 *	not passed by value. datum contains a pointer.
			 * ----------------
			 */
			if (from->constlen != -1)
			{
				/* ----------------
				 *		fixed length structure
				 * ----------------
				 */
				newnode->constvalue = PointerGetDatum(palloc(from->constlen));
				memmove((char *) newnode->constvalue,
						(char *) from->constvalue, from->constlen);
			}
			else
			{
				/* ----------------
				 *		variable length structure.	here the length is stored
				 *	in the first int pointed to by the constval.
				 * ----------------
				 */
800
				int			length;
801

B
Bruce Momjian 已提交
802
				length = VARSIZE(from->constvalue);
803 804 805 806 807 808 809 810 811 812
				newnode->constvalue = PointerGetDatum(palloc(length));
				memmove((char *) newnode->constvalue,
						(char *) from->constvalue, length);
			}
		}
	}
	else
		newnode->constvalue = from->constvalue;
	newnode->constisnull = from->constisnull;
	newnode->constbyval = from->constbyval;
B
Bruce Momjian 已提交
813 814
	newnode->constisset = from->constisset;
	newnode->constiscast = from->constiscast;
815 816

	return newnode;
817 818 819
}

/* ----------------
820
 *		_copyParam
821 822
 * ----------------
 */
823
static Param *
824
_copyParam(Param *from)
825
{
826
	Param	   *newnode = makeNode(Param);
827 828 829 830 831 832 833 834 835 836 837 838 839 840

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->paramkind = from->paramkind;
	newnode->paramid = from->paramid;

	if (from->paramname != NULL)
		newnode->paramname = pstrdup(from->paramname);
	newnode->paramtype = from->paramtype;
	Node_Copy(from, newnode, param_tlist);

	return newnode;
841 842 843
}

/* ----------------
844
 *		_copyFunc
845 846
 * ----------------
 */
847
static Func *
848
_copyFunc(Func *from)
849
{
850
	Func	   *newnode = makeNode(Func);
851 852 853 854 855 856 857 858 859 860 861 862 863 864

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->funcid = from->funcid;
	newnode->functype = from->functype;
	newnode->funcisindex = from->funcisindex;
	newnode->funcsize = from->funcsize;
	newnode->func_fcache = from->func_fcache;
	Node_Copy(from, newnode, func_tlist);
	Node_Copy(from, newnode, func_planlist);

	return newnode;
865 866 867
}

/* ----------------
B
Bruce Momjian 已提交
868
 *		_copyAggref
869 870
 * ----------------
 */
B
Bruce Momjian 已提交
871 872
static Aggref *
_copyAggref(Aggref *from)
873
{
B
Bruce Momjian 已提交
874
	Aggref	   *newnode = makeNode(Aggref);
875 876 877 878 879 880 881 882 883 884

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->aggname = pstrdup(from->aggname);
	newnode->basetype = from->basetype;
	newnode->aggtype = from->aggtype;
	Node_Copy(from, newnode, target);
	newnode->aggno = from->aggno;
B
Bruce Momjian 已提交
885
	newnode->usenulls = from->usenulls;
886 887

	return newnode;
888 889
}

890 891 892 893 894 895 896
/* ----------------
 *		_copySubLink
 * ----------------
 */
static SubLink *
_copySubLink(SubLink *from)
{
897
	SubLink    *newnode = makeNode(SubLink);
898 899 900 901 902 903 904 905

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->subLinkType = from->subLinkType;
	newnode->useor = from->useor;
	Node_Copy(from, newnode, lefthand);
B
Bruce Momjian 已提交
906
	Node_Copy(from, newnode, oper);
907 908 909 910 911
	Node_Copy(from, newnode, subselect);

	return newnode;
}

T
Thomas G. Lockhart 已提交
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
/* ----------------
 *		_copyCaseExpr
 * ----------------
 */
static CaseExpr *
_copyCaseExpr(CaseExpr *from)
{
	CaseExpr   *newnode = makeNode(CaseExpr);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->casetype = from->casetype;

	Node_Copy(from, newnode, arg);
	Node_Copy(from, newnode, args);
	Node_Copy(from, newnode, defresult);

	return newnode;
}

/* ----------------
 *		_copyCaseWhen
 * ----------------
 */
static CaseWhen *
_copyCaseWhen(CaseWhen *from)
{
	CaseWhen   *newnode = makeNode(CaseWhen);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	Node_Copy(from, newnode, expr);
	Node_Copy(from, newnode, result);

	return newnode;
}

953
static Array *
B
Bruce Momjian 已提交
954
_copyArray(Array *from)
955
{
956
	Array	   *newnode = makeNode(Array);
957 958 959 960 961 962 963 964 965 966 967 968 969 970

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->arrayelemtype = from->arrayelemtype;
	newnode->arrayelemlength = from->arrayelemlength;
	newnode->arrayelembyval = from->arrayelembyval;
	newnode->arrayndim = from->arrayndim;
	newnode->arraylow = from->arraylow;
	newnode->arrayhigh = from->arrayhigh;
	newnode->arraylen = from->arraylen;

	return newnode;
971 972 973
}

static ArrayRef *
B
Bruce Momjian 已提交
974
_copyArrayRef(ArrayRef *from)
975
{
976
	ArrayRef   *newnode = makeNode(ArrayRef);
977 978 979 980 981 982 983

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->refattrlength = from->refattrlength;
	newnode->refelemlength = from->refelemlength;
B
Bruce Momjian 已提交
984
	newnode->refelemtype = from->refelemtype;
985 986 987 988 989 990 991 992
	newnode->refelembyval = from->refelembyval;

	Node_Copy(from, newnode, refupperindexpr);
	Node_Copy(from, newnode, reflowerindexpr);
	Node_Copy(from, newnode, refexpr);
	Node_Copy(from, newnode, refassgnexpr);

	return newnode;
993 994 995
}

/* ****************************************************************
996
 *						relation.h copy functions
997 998 999 1000
 * ****************************************************************
 */

/* ----------------
B
Bruce Momjian 已提交
1001
 *		_copyRelOptInfo
1002 1003 1004
 * ----------------
 */
/*
B
Bruce Momjian 已提交
1005
 ** when you change this, also make sure to fix up xfunc_copyRelOptInfo in
1006
 ** planner/path/xfunc.c accordingly!!!
1007
 **			-- JMH, 8/2/93
1008
 */
B
Bruce Momjian 已提交
1009
static RelOptInfo *
1010
_copyRelOptInfo(RelOptInfo * from)
1011
{
1012
	RelOptInfo *newnode = makeNode(RelOptInfo);
1013 1014
	int			i,
				len;
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->relids = listCopy(from->relids);

	newnode->indexed = from->indexed;
	newnode->pages = from->pages;
	newnode->tuples = from->tuples;
	newnode->size = from->size;
	newnode->width = from->width;
	Node_Copy(from, newnode, targetlist);
	Node_Copy(from, newnode, pathlist);
	Node_Copy(from, newnode, unorderedpath);
	Node_Copy(from, newnode, cheapestpath);
	newnode->pruneable = from->pruneable;

	if (from->classlist)
	{
		for (len = 0; from->classlist[len] != 0; len++)
			;
		newnode->classlist = (Oid *) palloc(sizeof(Oid) * (len + 1));
		for (i = 0; i < len; i++)
			newnode->classlist[i] = from->classlist[i];
		newnode->classlist[len] = 0;
1041
	}
1042 1043 1044 1045 1046 1047 1048 1049 1050

	if (from->indexkeys)
	{
		for (len = 0; from->indexkeys[len] != 0; len++)
			;
		newnode->indexkeys = (int *) palloc(sizeof(int) * (len + 1));
		for (i = 0; i < len; i++)
			newnode->indexkeys[i] = from->indexkeys[i];
		newnode->indexkeys[len] = 0;
1051
	}
1052

B
Bruce Momjian 已提交
1053 1054 1055
	newnode->relam = from->relam;
	newnode->indproc = from->indproc;
	Node_Copy(from, newnode, indpred);
1056

1057 1058 1059 1060 1061 1062 1063 1064
	if (from->ordering)
	{
		for (len = 0; from->ordering[len] != 0; len++)
			;
		newnode->ordering = (Oid *) palloc(sizeof(Oid) * (len + 1));
		for (i = 0; i < len; i++)
			newnode->ordering[i] = from->ordering[i];
		newnode->ordering[len] = 0;
1065
	}
1066

1067
	Node_Copy(from, newnode, restrictinfo);
1068 1069 1070 1071 1072
	Node_Copy(from, newnode, joininfo);
	Node_Copy(from, newnode, innerjoin);
	Node_Copy(from, newnode, superrels);

	return newnode;
1073 1074 1075
}

/* ----------------
1076
 *		CopyPathFields
1077
 *
1078 1079
 *		This function copies the fields of the Path node.  It is used by
 *		all the copy functions for classes which inherit from Path.
1080 1081 1082
 * ----------------
 */
static void
1083
CopyPathFields(Path *from, Path *newnode)
1084
{
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	newnode->pathtype = from->pathtype;

	/*
	 * Modify the next line, since it causes the copying to cycle (i.e.
	 * the parent points right back here! -- JMH, 7/7/92. Old version:
	 * Node_Copy(from, newnode, parent);
	 */
	newnode->parent = from->parent;

	newnode->path_cost = from->path_cost;

B
Bruce Momjian 已提交
1096 1097
	newnode->path_order.ordtype = from->path_order.ordtype;
	if (from->path_order.ordtype == SORTOP_ORDER)
1098
	{
1099 1100
		int			len,
					i;
B
Bruce Momjian 已提交
1101
		Oid		   *ordering = from->path_order.ord.sortop;
1102 1103 1104 1105 1106

		if (ordering)
		{
			for (len = 0; ordering[len] != 0; len++)
				;
B
Bruce Momjian 已提交
1107
			newnode->path_order.ord.sortop = (Oid *) palloc(sizeof(Oid) * (len + 1));
1108
			for (i = 0; i < len; i++)
B
Bruce Momjian 已提交
1109 1110
				newnode->path_order.ord.sortop[i] = ordering[i];
			newnode->path_order.ord.sortop[len] = 0;
1111 1112 1113
		}
	}
	else
B
Bruce Momjian 已提交
1114
		Node_Copy(from, newnode, path_order.ord.merge);
1115 1116 1117 1118 1119 1120

	Node_Copy(from, newnode, keys);

	newnode->outerjoincost = from->outerjoincost;

	newnode->joinid = listCopy(from->joinid);
1121
	Node_Copy(from, newnode, loc_restrictinfo);
1122 1123 1124
}

/* ----------------
1125
 *		_copyPath
1126 1127
 * ----------------
 */
1128
static Path *
1129
_copyPath(Path *from)
1130
{
1131
	Path	   *newnode = makeNode(Path);
1132 1133 1134 1135

	CopyPathFields(from, newnode);

	return newnode;
1136 1137 1138
}

/* ----------------
1139
 *		_copyIndexPath
1140 1141 1142
 * ----------------
 */
static IndexPath *
1143
_copyIndexPath(IndexPath *from)
1144
{
1145
	IndexPath  *newnode = makeNode(IndexPath);
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161

	/* ----------------
	 *	copy the node superclass fields
	 * ----------------
	 */
	CopyPathFields((Path *) from, (Path *) newnode);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->indexid = listCopy(from->indexid);
	Node_Copy(from, newnode, indexqual);

	if (from->indexkeys)
	{
1162 1163
		int			i,
					len;
1164 1165 1166 1167 1168 1169 1170

		for (len = 0; from->indexkeys[len] != 0; len++)
			;
		newnode->indexkeys = (int *) palloc(sizeof(int) * (len + 1));
		for (i = 0; i < len; i++)
			newnode->indexkeys[i] = from->indexkeys[i];
		newnode->indexkeys[len] = 0;
1171
	}
1172 1173

	return newnode;
1174 1175 1176
}

/* ----------------
1177
 *		CopyJoinPathFields
1178
 *
1179 1180
 *		This function copies the fields of the JoinPath node.  It is used by
 *		all the copy functions for classes which inherit from JoinPath.
1181 1182 1183
 * ----------------
 */
static void
1184
CopyJoinPathFields(JoinPath *from, JoinPath *newnode)
1185
{
1186
	Node_Copy(from, newnode, pathinfo);
1187 1188
	Node_Copy(from, newnode, outerjoinpath);
	Node_Copy(from, newnode, innerjoinpath);
1189 1190 1191
}

/* ----------------
1192
 *		_copyJoinPath
1193 1194 1195
 * ----------------
 */
static JoinPath *
1196
_copyJoinPath(JoinPath *from)
1197
{
1198
	JoinPath   *newnode = makeNode(JoinPath);
1199 1200 1201 1202 1203 1204 1205 1206 1207

	/* ----------------
	 *	copy the node superclass fields
	 * ----------------
	 */
	CopyPathFields((Path *) from, (Path *) newnode);
	CopyJoinPathFields(from, newnode);

	return newnode;
1208 1209 1210
}

/* ----------------
1211
 *		_copyMergePath
1212 1213 1214
 * ----------------
 */
static MergePath *
1215
_copyMergePath(MergePath *from)
1216
{
1217
	MergePath  *newnode = makeNode(MergePath);
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234

	/* ----------------
	 *	copy the node superclass fields
	 * ----------------
	 */
	CopyPathFields((Path *) from, (Path *) newnode);
	CopyJoinPathFields((JoinPath *) from, (JoinPath *) newnode);

	/* ----------------
	 *	copy the remainder of the node
	 * ----------------
	 */
	Node_Copy(from, newnode, path_mergeclauses);
	Node_Copy(from, newnode, outersortkeys);
	Node_Copy(from, newnode, innersortkeys);

	return newnode;
1235 1236 1237
}

/* ----------------
1238
 *		_copyHashPath
1239 1240 1241
 * ----------------
 */
static HashPath *
1242
_copyHashPath(HashPath *from)
1243
{
1244
	HashPath   *newnode = makeNode(HashPath);
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261

	/* ----------------
	 *	copy the node superclass fields
	 * ----------------
	 */
	CopyPathFields((Path *) from, (Path *) newnode);
	CopyJoinPathFields((JoinPath *) from, (JoinPath *) newnode);

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	Node_Copy(from, newnode, path_hashclauses);
	Node_Copy(from, newnode, outerhashkeys);
	Node_Copy(from, newnode, innerhashkeys);

	return newnode;
1262 1263 1264
}

/* ----------------
1265
 *		_copyOrderKey
1266 1267 1268
 * ----------------
 */
static OrderKey *
1269
_copyOrderKey(OrderKey *from)
1270
{
1271
	OrderKey   *newnode = makeNode(OrderKey);
1272 1273 1274 1275 1276 1277 1278 1279 1280

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->attribute_number = from->attribute_number;
	newnode->array_index = from->array_index;

	return newnode;
1281 1282 1283 1284
}


/* ----------------
1285
 *		_copyJoinKey
1286 1287 1288
 * ----------------
 */
static JoinKey *
1289
_copyJoinKey(JoinKey *from)
1290
{
1291
	JoinKey    *newnode = makeNode(JoinKey);
1292 1293 1294 1295 1296 1297 1298 1299 1300

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	Node_Copy(from, newnode, outer);
	Node_Copy(from, newnode, inner);

	return newnode;
1301 1302 1303
}

/* ----------------
1304
 *		_copyMergeOrder
1305 1306 1307
 * ----------------
 */
static MergeOrder *
1308
_copyMergeOrder(MergeOrder *from)
1309
{
1310
	MergeOrder *newnode = makeNode(MergeOrder);
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->join_operator = from->join_operator;
	newnode->left_operator = from->left_operator;
	newnode->right_operator = from->right_operator;
	newnode->left_type = from->left_type;
	newnode->right_type = from->right_type;

	return newnode;
1323 1324 1325
}

/* ----------------
1326
 *		_copyRestrictInfo
1327 1328
 * ----------------
 */
1329 1330
static RestrictInfo *
_copyRestrictInfo(RestrictInfo * from)
1331
{
1332
	RestrictInfo *newnode = makeNode(RestrictInfo);
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	Node_Copy(from, newnode, clause);

	newnode->selectivity = from->selectivity;
	newnode->notclause = from->notclause;

	Node_Copy(from, newnode, indexids);
1344
	Node_Copy(from, newnode, mergejoinorder);
1345
	newnode->hashjoinoperator = from->hashjoinoperator;
B
Bruce Momjian 已提交
1346
	newnode->restrictinfojoinid = listCopy(from->restrictinfojoinid);
1347 1348

	return newnode;
1349 1350 1351
}

/* ----------------
1352
 *		CopyJoinMethodFields
1353
 *
1354 1355
 *		This function copies the fields of the JoinMethod node.  It is used by
 *		all the copy functions for classes which inherit from JoinMethod.
1356 1357 1358
 * ----------------
 */
static void
1359
CopyJoinMethodFields(JoinMethod *from, JoinMethod *newnode)
1360
{
1361 1362 1363
	Node_Copy(from, newnode, jmkeys);
	Node_Copy(from, newnode, clauses);
	return;
1364 1365 1366
}

/* ----------------
1367
 *		_copyJoinMethod
1368 1369 1370
 * ----------------
 */
static JoinMethod *
1371
_copyJoinMethod(JoinMethod *from)
1372
{
1373
	JoinMethod *newnode = makeNode(JoinMethod);
1374 1375 1376 1377

	CopyJoinMethodFields(from, newnode);

	return newnode;
1378 1379 1380
}

/* ----------------
1381
 *		_copyHashInfo
1382 1383
 * ----------------
 */
1384 1385
static HashInfo *
_copyHashInfo(HashInfo *from)
1386
{
1387
	HashInfo	   *newnode = makeNode(HashInfo);
1388 1389 1390 1391 1392

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
1393
	CopyJoinMethodFields((JoinMethod *) from, (JoinMethod *) newnode);
1394 1395 1396
	newnode->hashop = from->hashop;

	return newnode;
1397 1398 1399
}

/* ----------------
B
Bruce Momjian 已提交
1400
 *		_copyMergeInfo
1401 1402
 * ----------------
 */
B
Bruce Momjian 已提交
1403 1404
static MergeInfo *
_copyMergeInfo(MergeInfo *from)
1405
{
B
Bruce Momjian 已提交
1406
	MergeInfo	   *newnode = makeNode(MergeInfo);
1407 1408 1409 1410 1411

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
1412
	CopyJoinMethodFields((JoinMethod *) from, (JoinMethod *) newnode);
1413 1414 1415
	Node_Copy(from, newnode, m_ordering);

	return newnode;
1416 1417 1418
}

/* ----------------
1419
 *		_copyJoinInfo
1420 1421
 * ----------------
 */
1422
static JoinInfo *
1423
_copyJoinInfo(JoinInfo * from)
1424
{
1425
	JoinInfo   *newnode = makeNode(JoinInfo);
1426 1427 1428 1429 1430 1431

	/* ----------------
	 *	copy remainder of node
	 * ----------------
	 */
	newnode->otherrels = listCopy(from->otherrels);
1432
	Node_Copy(from, newnode, jinfo_restrictinfo);
1433

1434
	newnode->mergejoinable = from->mergejoinable;
1435 1436 1437 1438
	newnode->hashjoinable = from->hashjoinable;
	newnode->inactive = from->inactive;

	return newnode;
1439 1440
}

1441
static Iter *
1442
_copyIter(Iter *from)
1443
{
1444
	Iter	   *newnode = makeNode(Iter);
1445 1446 1447

	Node_Copy(from, newnode, iterexpr);
	newnode->itertype = from->itertype;
1448

1449
	return newnode;
1450 1451
}

1452
static Stream *
1453
_copyStream(Stream *from)
1454
{
1455
	Stream	   *newnode = makeNode(Stream);
1456 1457 1458 1459

	newnode->pathptr = from->pathptr;
	newnode->cinfo = from->cinfo;
	newnode->clausetype = from->clausetype;
B
Bruce Momjian 已提交
1460

1461 1462 1463 1464 1465 1466
	newnode->upstream = (StreamPtr) NULL;		/* only copy nodes
												 * downwards! */
	Node_Copy(from, newnode, downstream);
	if (newnode->downstream)
		((Stream *) newnode->downstream)->upstream = (Stream *) newnode;

B
Bruce Momjian 已提交
1467 1468 1469 1470
	newnode->groupup = from->groupup;
	newnode->groupcost = from->groupcost;
	newnode->groupsel = from->groupsel;

1471
	return newnode;
1472 1473 1474
}

/* ****************
1475
 *			  parsenodes.h routines have no copy functions
1476 1477 1478 1479
 * ****************
 */

static TargetEntry *
1480
_copyTargetEntry(TargetEntry *from)
1481
{
1482
	TargetEntry *newnode = makeNode(TargetEntry);
1483

1484 1485 1486 1487
	Node_Copy(from, newnode, resdom);
	Node_Copy(from, newnode, fjoin);
	Node_Copy(from, newnode, expr);
	return newnode;
1488 1489 1490
}

static RangeTblEntry *
1491
_copyRangeTblEntry(RangeTblEntry *from)
1492
{
1493
	RangeTblEntry *newnode = makeNode(RangeTblEntry);
1494 1495 1496 1497 1498

	if (from->relname)
		newnode->relname = pstrdup(from->relname);
	if (from->refname)
		newnode->refname = pstrdup(from->refname);
B
Bruce Momjian 已提交
1499 1500 1501
	newnode->relid = from->relid;
	newnode->inh = from->inh;
	newnode->inFromCl = from->inFromCl;
1502 1503
	newnode->skipAcl = from->skipAcl;

1504 1505

	return newnode;
1506 1507
}

1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
static RowMark *
_copyRowMark(RowMark *from)
{
	RowMark *newnode = makeNode(RowMark);

	newnode->rti = from->rti;
	newnode->info = from->info;

	return newnode;
}

1519
static SortClause *
1520
_copySortClause(SortClause *from)
1521
{
1522
	SortClause *newnode = makeNode(SortClause);
1523 1524 1525

	Node_Copy(from, newnode, resdom);
	newnode->opoid = from->opoid;
1526

1527
	return newnode;
1528 1529
}

1530
static A_Const *
B
Bruce Momjian 已提交
1531
_copyAConst(A_Const *from)
1532
{
1533
	A_Const    *newnode = makeNode(A_Const);
1534

1535 1536
	newnode->val = *((Value *) (copyObject(&(from->val))));
	Node_Copy(from, newnode, typename);
1537

1538
	return newnode;
1539 1540 1541
}

static TypeName *
1542
_copyTypeName(TypeName *from)
1543
{
1544
	TypeName   *newnode = makeNode(TypeName);
1545 1546 1547

	if (from->name)
		newnode->name = pstrdup(from->name);
B
Bruce Momjian 已提交
1548
	newnode->timezone = from->timezone;
1549
	newnode->setof = from->setof;
1550
	newnode->typmod = from->typmod;
1551 1552 1553
	Node_Copy(from, newnode, arrayBounds);

	return newnode;
1554 1555
}

1556
static Query *
1557
_copyQuery(Query *from)
1558
{
1559
	Query	   *newnode = makeNode(Query);
1560

1561
	newnode->commandType = from->commandType;
B
Bruce Momjian 已提交
1562 1563 1564 1565 1566
	if (from->utilityStmt && nodeTag(from->utilityStmt) == T_NotifyStmt)
	{
		NotifyStmt *from_notify = (NotifyStmt *) from->utilityStmt;
		NotifyStmt *n = makeNode(NotifyStmt);

B
Bruce Momjian 已提交
1567
		n->relname = pstrdup(from_notify->relname);
B
Bruce Momjian 已提交
1568 1569
		newnode->utilityStmt = (Node *) n;
	}
1570 1571 1572 1573
	newnode->resultRelation = from->resultRelation;
	if (from->into)
		newnode->into = pstrdup(from->into);
	newnode->isPortal = from->isPortal;
B
Bruce Momjian 已提交
1574
	newnode->isBinary = from->isBinary;
1575
	newnode->isTemp = from->isTemp;
B
Bruce Momjian 已提交
1576
	newnode->unionall = from->unionall;
1577
	if (from->uniqueFlag)
B
Bruce Momjian 已提交
1578
		newnode->uniqueFlag = pstrdup(from->uniqueFlag);
1579
	Node_Copy(from, newnode, sortClause);
B
Bruce Momjian 已提交
1580
	Node_Copy(from, newnode, rtable);
1581 1582 1583
	Node_Copy(from, newnode, targetList);
	Node_Copy(from, newnode, qual);

1584
	Node_Copy(from, newnode, groupClause);
B
Bruce Momjian 已提交
1585
	Node_Copy(from, newnode, havingQual);
1586

1587
	newnode->hasAggs = from->hasAggs;
1588
	newnode->hasSubLinks = from->hasSubLinks;
B
Bruce Momjian 已提交
1589 1590 1591

	if (from->unionClause)
	{
1592 1593
		List	   *ulist,
				   *temp_list = NIL;
B
Bruce Momjian 已提交
1594 1595

		foreach(ulist, from->unionClause)
1596
			temp_list = lappend(temp_list, copyObject(lfirst(ulist)));
B
Bruce Momjian 已提交
1597 1598
		newnode->unionClause = temp_list;
	}
1599

B
Bruce Momjian 已提交
1600 1601 1602
	Node_Copy(from, newnode, limitOffset);
	Node_Copy(from, newnode, limitCount);

1603 1604
	Node_Copy(from, newnode, rowMark);

1605
	return newnode;
1606 1607 1608 1609
}


/* ****************
1610
 *			  mnodes.h routines have no copy functions
1611 1612 1613 1614
 * ****************
 */

/* ****************************************************************
1615
 *					pg_list.h copy functions
1616 1617 1618
 * ****************************************************************
 */

1619
static Value *
1620
_copyValue(Value *from)
1621
{
1622
	Value	   *newnode = makeNode(Value);
1623 1624 1625 1626

	newnode->type = from->type;
	switch (from->type)
	{
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
		case T_String:
			newnode->val.str = pstrdup(from->val.str);
			break;
		case T_Integer:
			newnode->val.ival = from->val.ival;
			break;
		case T_Float:
			newnode->val.dval = from->val.dval;
			break;
		default:
			break;
1638 1639
	}
	return newnode;
1640 1641 1642
}

/* ----------------
1643 1644
 *		copyObject returns a copy of the node or list. If it is a list, it
 *		recursively copies its items.
1645 1646
 * ----------------
 */
1647
void *
1648 1649
copyObject(void *from)
{
1650
	void	   *retval;
1651

1652 1653 1654
	if (from == NULL)
		return NULL;
	switch (nodeTag(from))
1655
	{
1656

1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
			/*
			 * PLAN NODES
			 */
		case T_Plan:
			retval = _copyPlan(from);
			break;
		case T_Result:
			retval = _copyResult(from);
			break;
		case T_Append:
			retval = _copyAppend(from);
			break;
		case T_Scan:
			retval = _copyScan(from);
			break;
		case T_SeqScan:
			retval = _copySeqScan(from);
			break;
		case T_IndexScan:
			retval = _copyIndexScan(from);
			break;
		case T_Join:
			retval = _copyJoin(from);
			break;
		case T_NestLoop:
			retval = _copyNestLoop(from);
			break;
		case T_MergeJoin:
			retval = _copyMergeJoin(from);
			break;
		case T_HashJoin:
			retval = _copyHashJoin(from);
			break;
		case T_Temp:
			retval = _copyTemp(from);
			break;
		case T_Material:
			retval = _copyMaterial(from);
			break;
		case T_Sort:
			retval = _copySort(from);
			break;
V
Vadim B. Mikheev 已提交
1699 1700 1701
		case T_Group:
			retval = _copyGroup(from);
			break;
1702 1703 1704
		case T_Agg:
			retval = _copyAgg(from);
			break;
1705 1706 1707
		case T_GroupClause:
			retval = _copyGroupClause(from);
			break;
1708 1709 1710 1711 1712 1713
		case T_Unique:
			retval = _copyUnique(from);
			break;
		case T_Hash:
			retval = _copyHash(from);
			break;
V
Vadim B. Mikheev 已提交
1714 1715 1716
		case T_SubPlan:
			retval = _copySubPlan(from);
			break;
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750

			/*
			 * PRIMITIVE NODES
			 */
		case T_Resdom:
			retval = _copyResdom(from);
			break;
		case T_Fjoin:
			retval = _copyFjoin(from);
			break;
		case T_Expr:
			retval = _copyExpr(from);
			break;
		case T_Var:
			retval = _copyVar(from);
			break;
		case T_Oper:
			retval = _copyOper(from);
			break;
		case T_Const:
			retval = _copyConst(from);
			break;
		case T_Param:
			retval = _copyParam(from);
			break;
		case T_Func:
			retval = _copyFunc(from);
			break;
		case T_Array:
			retval = _copyArray(from);
			break;
		case T_ArrayRef:
			retval = _copyArrayRef(from);
			break;
B
Bruce Momjian 已提交
1751 1752
		case T_Aggref:
			retval = _copyAggref(from);
1753
			break;
1754 1755 1756
		case T_SubLink:
			retval = _copySubLink(from);
			break;
T
Thomas G. Lockhart 已提交
1757 1758 1759 1760 1761 1762
		case T_CaseExpr:
			retval = _copyCaseExpr(from);
			break;
		case T_CaseWhen:
			retval = _copyCaseWhen(from);
			break;
1763 1764 1765 1766

			/*
			 * RELATION NODES
			 */
B
Bruce Momjian 已提交
1767
		case T_RelOptInfo:
B
Bruce Momjian 已提交
1768
			retval = _copyRelOptInfo(from);
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
			break;
		case T_Path:
			retval = _copyPath(from);
			break;
		case T_IndexPath:
			retval = _copyIndexPath(from);
			break;
		case T_JoinPath:
			retval = _copyJoinPath(from);
			break;
		case T_MergePath:
			retval = _copyMergePath(from);
			break;
		case T_HashPath:
			retval = _copyHashPath(from);
			break;
		case T_OrderKey:
			retval = _copyOrderKey(from);
			break;
		case T_JoinKey:
			retval = _copyJoinKey(from);
			break;
		case T_MergeOrder:
			retval = _copyMergeOrder(from);
			break;
1794 1795
		case T_RestrictInfo:
			retval = _copyRestrictInfo(from);
1796 1797 1798 1799
			break;
		case T_JoinMethod:
			retval = _copyJoinMethod(from);
			break;
1800 1801
		case T_HashInfo:
			retval = _copyHashInfo(from);
1802
			break;
B
Bruce Momjian 已提交
1803 1804
		case T_MergeInfo:
			retval = _copyMergeInfo(from);
1805
			break;
1806 1807
		case T_JoinInfo:
			retval = _copyJoinInfo(from);
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
			break;
		case T_Iter:
			retval = _copyIter(from);
			break;
		case T_Stream:
			retval = _copyStream(from);
			break;

			/*
			 * PARSE NODES
			 */
		case T_Query:
			retval = _copyQuery(from);
			break;
		case T_TargetEntry:
			retval = _copyTargetEntry(from);
			break;
		case T_RangeTblEntry:
			retval = _copyRangeTblEntry(from);
			break;
1828 1829 1830
		case T_RowMark:
			retval = _copyRowMark(from);
			break;
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
		case T_SortClause:
			retval = _copySortClause(from);
			break;
		case T_A_Const:
			retval = _copyAConst(from);
			break;
		case T_TypeName:
			retval = _copyTypeName(from);
			break;

			/*
			 * VALUE NODES
			 */
		case T_Integer:
		case T_String:
		case T_Float:
			retval = _copyValue(from);
			break;
		case T_List:
			{
				List	   *list = from,
1852
						   *l;
1853
				List	   *newlist = NIL,
1854 1855
						   *nl = NIL;

1856
				foreach(l, list)
1857
				{
1858 1859 1860 1861 1862 1863 1864
					if (newlist == NIL)
						newlist = nl = lcons(copyObject(lfirst(l)), NIL);
					else
					{
						lnext(nl) = lcons(copyObject(lfirst(l)), NIL);
						nl = lnext(nl);
					}
1865
				}
1866
				retval = newlist;
1867
			}
1868 1869
			break;
		default:
1870
			elog(ERROR, "copyObject: don't know how to copy %d", nodeTag(from));
1871 1872
			retval = from;
			break;
1873
	}
1874
	return retval;
1875
}