OSDN Git Service

Subselects in FROM clause, per ISO syntax: FROM (SELECT ...) [AS] alias.
[pg-rex/syncrep.git] / src / backend / nodes / copyfuncs.c
1 /*-------------------------------------------------------------------------
2  *
3  * copyfuncs.c
4  *        Copy functions for Postgres tree nodes.
5  *
6  * NOTE: a general convention when copying or comparing plan nodes is
7  * that we ignore the executor state subnode.  We do not need to look
8  * at it because no current uses of copyObject() or equal() need to
9  * deal with already-executing plan trees.  By leaving the state subnodes
10  * out, we avoid needing to write copy/compare routines for all the
11  * different executor state node types.
12  *
13  *
14  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
15  * Portions Copyright (c) 1994, Regents of the University of California
16  *
17  * IDENTIFICATION
18  *        $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.123 2000/09/29 18:21:29 tgl Exp $
19  *
20  *-------------------------------------------------------------------------
21  */
22
23 #include "postgres.h"
24
25 #include "optimizer/clauses.h"
26 #include "optimizer/planmain.h"
27 #include "utils/acl.h"
28
29
30 /*
31  * Node_Copy
32  *        a macro to simplify calling of copyObject on the specified field
33  */
34 #define Node_Copy(from, newnode, field) \
35         ((newnode)->field = copyObject((from)->field))
36
37
38 /*
39  * listCopy
40  *        This copy function only copies the "cons-cells" of the list, not the
41  *        pointed-to objects.  (Use copyObject if you want a "deep" copy.)
42  *
43  *        We also use this function for copying lists of integers, which is
44  *        grotty but unlikely to break --- it could fail if sizeof(pointer)
45  *        is less than sizeof(int), but I don't know any such machines...
46  *
47  *        Note that copyObject will surely coredump if applied to a list
48  *        of integers!
49  */
50 List *
51 listCopy(List *list)
52 {
53         List       *newlist,
54                            *l,
55                            *nl;
56
57         /* rather ugly coding for speed... */
58         if (list == NIL)
59                 return NIL;
60
61         newlist = nl = lcons(lfirst(list), NIL);
62
63         foreach(l, lnext(list))
64         {
65                 lnext(nl) = lcons(lfirst(l), NIL);
66                 nl = lnext(nl);
67         }
68         return newlist;
69 }
70
71 /* ****************************************************************
72  *                                       plannodes.h copy functions
73  * ****************************************************************
74  */
75
76 /* ----------------
77  *              CopyPlanFields
78  *
79  *              This function copies the fields of the Plan node.  It is used by
80  *              all the copy functions for classes which inherit from Plan.
81  * ----------------
82  */
83 static void
84 CopyPlanFields(Plan *from, Plan *newnode)
85 {
86         newnode->startup_cost = from->startup_cost;
87         newnode->total_cost = from->total_cost;
88         newnode->plan_rows = from->plan_rows;
89         newnode->plan_width = from->plan_width;
90         /* state is NOT copied */
91         Node_Copy(from, newnode, targetlist);
92         Node_Copy(from, newnode, qual);
93         Node_Copy(from, newnode, lefttree);
94         Node_Copy(from, newnode, righttree);
95         newnode->extParam = listCopy(from->extParam);
96         newnode->locParam = listCopy(from->locParam);
97         newnode->chgParam = listCopy(from->chgParam);
98         Node_Copy(from, newnode, initPlan);
99         /* subPlan list must point to subplans in the new subtree, not the old */
100         if (from->subPlan != NIL)
101                 newnode->subPlan = nconc(pull_subplans((Node *) newnode->targetlist),
102                                                                  pull_subplans((Node *) newnode->qual));
103         else
104                 newnode->subPlan = NIL;
105         newnode->nParamExec = from->nParamExec;
106 }
107
108 /* ----------------
109  *              _copyPlan
110  * ----------------
111  */
112 static Plan *
113 _copyPlan(Plan *from)
114 {
115         Plan       *newnode = makeNode(Plan);
116
117         /* ----------------
118          *      copy the node superclass fields
119          * ----------------
120          */
121         CopyPlanFields(from, newnode);
122
123         return newnode;
124 }
125
126
127 /* ----------------
128  *              _copyResult
129  * ----------------
130  */
131 static Result *
132 _copyResult(Result *from)
133 {
134         Result     *newnode = makeNode(Result);
135
136         /* ----------------
137          *      copy node superclass fields
138          * ----------------
139          */
140         CopyPlanFields((Plan *) from, (Plan *) newnode);
141
142         /* ----------------
143          *      copy remainder of node
144          * ----------------
145          */
146         Node_Copy(from, newnode, resconstantqual);
147
148         /*
149          * We must add subplans in resconstantqual to the new plan's subPlan
150          * list
151          */
152         if (from->plan.subPlan != NIL)
153                 newnode->plan.subPlan = nconc(newnode->plan.subPlan,
154                                                                 pull_subplans(newnode->resconstantqual));
155
156         return newnode;
157 }
158
159 /* ----------------
160  *              _copyAppend
161  * ----------------
162  */
163 static Append *
164 _copyAppend(Append *from)
165 {
166         Append     *newnode = makeNode(Append);
167
168         /* ----------------
169          *      copy node superclass fields
170          * ----------------
171          */
172         CopyPlanFields((Plan *) from, (Plan *) newnode);
173
174         /* ----------------
175          *      copy remainder of node
176          * ----------------
177          */
178         Node_Copy(from, newnode, appendplans);
179         Node_Copy(from, newnode, unionrtables);
180         newnode->inheritrelid = from->inheritrelid;
181         Node_Copy(from, newnode, inheritrtable);
182
183         return newnode;
184 }
185
186
187 /* ----------------
188  *              CopyScanFields
189  *
190  *              This function copies the fields of the Scan node.  It is used by
191  *              all the copy functions for classes which inherit from Scan.
192  * ----------------
193  */
194 static void
195 CopyScanFields(Scan *from, Scan *newnode)
196 {
197         newnode->scanrelid = from->scanrelid;
198         return;
199 }
200
201 /* ----------------
202  *              _copyScan
203  * ----------------
204  */
205 static Scan *
206 _copyScan(Scan *from)
207 {
208         Scan       *newnode = makeNode(Scan);
209
210         /* ----------------
211          *      copy node superclass fields
212          * ----------------
213          */
214         CopyPlanFields((Plan *) from, (Plan *) newnode);
215         CopyScanFields((Scan *) from, (Scan *) newnode);
216
217         return newnode;
218 }
219
220 /* ----------------
221  *              _copySeqScan
222  * ----------------
223  */
224 static SeqScan *
225 _copySeqScan(SeqScan *from)
226 {
227         SeqScan    *newnode = makeNode(SeqScan);
228
229         /* ----------------
230          *      copy node superclass fields
231          * ----------------
232          */
233         CopyPlanFields((Plan *) from, (Plan *) newnode);
234         CopyScanFields((Scan *) from, (Scan *) newnode);
235
236         return newnode;
237 }
238
239 /* ----------------
240  *              _copyIndexScan
241  * ----------------
242  */
243 static IndexScan *
244 _copyIndexScan(IndexScan *from)
245 {
246         IndexScan  *newnode = makeNode(IndexScan);
247
248         /* ----------------
249          *      copy node superclass fields
250          * ----------------
251          */
252         CopyPlanFields((Plan *) from, (Plan *) newnode);
253         CopyScanFields((Scan *) from, (Scan *) newnode);
254
255         /* ----------------
256          *      copy remainder of node
257          * ----------------
258          */
259         newnode->indxid = listCopy(from->indxid);
260         Node_Copy(from, newnode, indxqual);
261         Node_Copy(from, newnode, indxqualorig);
262         newnode->indxorderdir = from->indxorderdir;
263
264         /*
265          * We must add subplans in index quals to the new plan's subPlan list
266          */
267         if (from->scan.plan.subPlan != NIL)
268         {
269                 newnode->scan.plan.subPlan = nconc(newnode->scan.plan.subPlan,
270                                                           pull_subplans((Node *) newnode->indxqual));
271                 newnode->scan.plan.subPlan = nconc(newnode->scan.plan.subPlan,
272                                                   pull_subplans((Node *) newnode->indxqualorig));
273         }
274
275         return newnode;
276 }
277
278 /* ----------------
279  *                              _copyTidScan
280  * ----------------
281  */
282 static TidScan *
283 _copyTidScan(TidScan *from)
284 {
285         TidScan    *newnode = makeNode(TidScan);
286
287         /* ----------------
288          *      copy node superclass fields
289          * ----------------
290          */
291         CopyPlanFields((Plan *) from, (Plan *) newnode);
292         CopyScanFields((Scan *) from, (Scan *) newnode);
293         /* ----------------
294          *      copy remainder of node
295          * ----------------
296          */
297         newnode->needRescan = from->needRescan;
298         Node_Copy(from, newnode, tideval);
299
300         return newnode;
301 }
302
303 /* ----------------
304  *              _copySubqueryScan
305  * ----------------
306  */
307 static SubqueryScan *
308 _copySubqueryScan(SubqueryScan *from)
309 {
310         SubqueryScan  *newnode = makeNode(SubqueryScan);
311
312         /* ----------------
313          *      copy node superclass fields
314          * ----------------
315          */
316         CopyPlanFields((Plan *) from, (Plan *) newnode);
317         CopyScanFields((Scan *) from, (Scan *) newnode);
318
319         /* ----------------
320          *      copy remainder of node
321          * ----------------
322          */
323         Node_Copy(from, newnode, subplan);
324
325         return newnode;
326 }
327
328
329 /* ----------------
330  *              CopyJoinFields
331  *
332  *              This function copies the fields of the Join node.  It is used by
333  *              all the copy functions for classes which inherit from Join.
334  * ----------------
335  */
336 static void
337 CopyJoinFields(Join *from, Join *newnode)
338 {
339         newnode->jointype = from->jointype;
340         Node_Copy(from, newnode, joinqual);
341         /* subPlan list must point to subplans in the new subtree, not the old */
342         if (from->plan.subPlan != NIL)
343                 newnode->plan.subPlan = nconc(newnode->plan.subPlan,
344                                                                           pull_subplans((Node *) newnode->joinqual));
345 }
346
347
348 /* ----------------
349  *              _copyJoin
350  * ----------------
351  */
352 static Join *
353 _copyJoin(Join *from)
354 {
355         Join       *newnode = makeNode(Join);
356
357         /* ----------------
358          *      copy node superclass fields
359          * ----------------
360          */
361         CopyPlanFields((Plan *) from, (Plan *) newnode);
362         CopyJoinFields(from, newnode);
363
364         return newnode;
365 }
366
367
368 /* ----------------
369  *              _copyNestLoop
370  * ----------------
371  */
372 static NestLoop *
373 _copyNestLoop(NestLoop *from)
374 {
375         NestLoop   *newnode = makeNode(NestLoop);
376
377         /* ----------------
378          *      copy node superclass fields
379          * ----------------
380          */
381         CopyPlanFields((Plan *) from, (Plan *) newnode);
382         CopyJoinFields((Join *) from, (Join *) newnode);
383
384         return newnode;
385 }
386
387
388 /* ----------------
389  *              _copyMergeJoin
390  * ----------------
391  */
392 static MergeJoin *
393 _copyMergeJoin(MergeJoin *from)
394 {
395         MergeJoin  *newnode = makeNode(MergeJoin);
396
397         /* ----------------
398          *      copy node superclass fields
399          * ----------------
400          */
401         CopyPlanFields((Plan *) from, (Plan *) newnode);
402         CopyJoinFields((Join *) from, (Join *) newnode);
403
404         /* ----------------
405          *      copy remainder of node
406          * ----------------
407          */
408         Node_Copy(from, newnode, mergeclauses);
409
410         /*
411          * We must add subplans in mergeclauses to the new plan's subPlan list
412          */
413         if (from->join.plan.subPlan != NIL)
414                 newnode->join.plan.subPlan = nconc(newnode->join.plan.subPlan,
415                                                   pull_subplans((Node *) newnode->mergeclauses));
416
417         return newnode;
418 }
419
420 /* ----------------
421  *              _copyHashJoin
422  * ----------------
423  */
424 static HashJoin *
425 _copyHashJoin(HashJoin *from)
426 {
427         HashJoin   *newnode = makeNode(HashJoin);
428
429         /* ----------------
430          *      copy node superclass fields
431          * ----------------
432          */
433         CopyPlanFields((Plan *) from, (Plan *) newnode);
434         CopyJoinFields((Join *) from, (Join *) newnode);
435
436         /* ----------------
437          *      copy remainder of node
438          * ----------------
439          */
440         Node_Copy(from, newnode, hashclauses);
441         newnode->hashjoinop = from->hashjoinop;
442
443         /*
444          * We must add subplans in hashclauses to the new plan's subPlan list
445          */
446         if (from->join.plan.subPlan != NIL)
447                 newnode->join.plan.subPlan = nconc(newnode->join.plan.subPlan,
448                                                    pull_subplans((Node *) newnode->hashclauses));
449
450         return newnode;
451 }
452
453
454 /* ----------------
455  *              _copyMaterial
456  * ----------------
457  */
458 static Material *
459 _copyMaterial(Material *from)
460 {
461         Material   *newnode = makeNode(Material);
462
463         /* ----------------
464          *      copy node superclass fields
465          * ----------------
466          */
467         CopyPlanFields((Plan *) from, (Plan *) newnode);
468
469         return newnode;
470 }
471
472
473 /* ----------------
474  *              _copySort
475  * ----------------
476  */
477 static Sort *
478 _copySort(Sort *from)
479 {
480         Sort       *newnode = makeNode(Sort);
481
482         /* ----------------
483          *      copy node superclass fields
484          * ----------------
485          */
486         CopyPlanFields((Plan *) from, (Plan *) newnode);
487
488         newnode->keycount = from->keycount;
489
490         return newnode;
491 }
492
493
494 /* ----------------
495  *              _copyGroup
496  * ----------------
497  */
498 static Group *
499 _copyGroup(Group *from)
500 {
501         Group      *newnode = makeNode(Group);
502
503         CopyPlanFields((Plan *) from, (Plan *) newnode);
504
505         newnode->tuplePerGroup = from->tuplePerGroup;
506         newnode->numCols = from->numCols;
507         newnode->grpColIdx = palloc(from->numCols * sizeof(AttrNumber));
508         memcpy(newnode->grpColIdx, from->grpColIdx, from->numCols * sizeof(AttrNumber));
509
510         return newnode;
511 }
512
513 /* ---------------
514  *      _copyAgg
515  * --------------
516  */
517 static Agg *
518 _copyAgg(Agg *from)
519 {
520         Agg                *newnode = makeNode(Agg);
521
522         CopyPlanFields((Plan *) from, (Plan *) newnode);
523
524         return newnode;
525 }
526
527 /* ---------------
528  *      _copyGroupClause
529  * --------------
530  */
531 static GroupClause *
532 _copyGroupClause(GroupClause *from)
533 {
534         GroupClause *newnode = makeNode(GroupClause);
535
536         newnode->tleSortGroupRef = from->tleSortGroupRef;
537         newnode->sortop = from->sortop;
538
539         return newnode;
540 }
541
542 /* ----------------
543  *              _copyUnique
544  * ----------------
545  */
546 static Unique *
547 _copyUnique(Unique *from)
548 {
549         Unique     *newnode = makeNode(Unique);
550
551         /* ----------------
552          *      copy node superclass fields
553          * ----------------
554          */
555         CopyPlanFields((Plan *) from, (Plan *) newnode);
556
557         /* ----------------
558          *      copy remainder of node
559          * ----------------
560          */
561         newnode->numCols = from->numCols;
562         newnode->uniqColIdx = palloc(from->numCols * sizeof(AttrNumber));
563         memcpy(newnode->uniqColIdx, from->uniqColIdx, from->numCols * sizeof(AttrNumber));
564
565         return newnode;
566 }
567
568
569 /* ----------------
570  *              _copyHash
571  * ----------------
572  */
573 static Hash *
574 _copyHash(Hash *from)
575 {
576         Hash       *newnode = makeNode(Hash);
577
578         /* ----------------
579          *      copy node superclass fields
580          * ----------------
581          */
582         CopyPlanFields((Plan *) from, (Plan *) newnode);
583
584         /* ----------------
585          *      copy remainder of node
586          * ----------------
587          */
588         Node_Copy(from, newnode, hashkey);
589
590         return newnode;
591 }
592
593 static SubPlan *
594 _copySubPlan(SubPlan *from)
595 {
596         SubPlan    *newnode = makeNode(SubPlan);
597
598         Node_Copy(from, newnode, plan);
599         newnode->plan_id = from->plan_id;
600         Node_Copy(from, newnode, rtable);
601         newnode->setParam = listCopy(from->setParam);
602         newnode->parParam = listCopy(from->parParam);
603         Node_Copy(from, newnode, sublink);
604
605         /* do not copy execution state */
606         newnode->needShutdown = false;
607         newnode->curTuple = NULL;
608
609         return newnode;
610 }
611
612 /* ****************************************************************
613  *                                         primnodes.h copy functions
614  * ****************************************************************
615  */
616
617 /* ----------------
618  *              _copyResdom
619  * ----------------
620  */
621 static Resdom *
622 _copyResdom(Resdom *from)
623 {
624         Resdom     *newnode = makeNode(Resdom);
625
626         newnode->resno = from->resno;
627         newnode->restype = from->restype;
628         newnode->restypmod = from->restypmod;
629         if (from->resname != NULL)
630                 newnode->resname = pstrdup(from->resname);
631         newnode->ressortgroupref = from->ressortgroupref;
632         newnode->reskey = from->reskey;
633         newnode->reskeyop = from->reskeyop;
634         newnode->resjunk = from->resjunk;
635
636         return newnode;
637 }
638
639 static Fjoin *
640 _copyFjoin(Fjoin *from)
641 {
642         Fjoin      *newnode = makeNode(Fjoin);
643
644         /* ----------------
645          *      copy node superclass fields
646          * ----------------
647          */
648
649         newnode->fj_initialized = from->fj_initialized;
650         newnode->fj_nNodes = from->fj_nNodes;
651
652         Node_Copy(from, newnode, fj_innerNode);
653
654         newnode->fj_results = (DatumPtr)
655                 palloc((from->fj_nNodes) * sizeof(Datum));
656         memmove(from->fj_results,
657                         newnode->fj_results,
658                         (from->fj_nNodes) * sizeof(Datum));
659
660         newnode->fj_alwaysDone = (BoolPtr)
661                 palloc((from->fj_nNodes) * sizeof(bool));
662         memmove(from->fj_alwaysDone,
663                         newnode->fj_alwaysDone,
664                         (from->fj_nNodes) * sizeof(bool));
665
666
667         return newnode;
668 }
669
670 /* ----------------
671  *              _copyExpr
672  * ----------------
673  */
674 static Expr *
675 _copyExpr(Expr *from)
676 {
677         Expr       *newnode = makeNode(Expr);
678
679         /* ----------------
680          *      copy node superclass fields
681          * ----------------
682          */
683         newnode->typeOid = from->typeOid;
684         newnode->opType = from->opType;
685
686         Node_Copy(from, newnode, oper);
687         Node_Copy(from, newnode, args);
688
689         return newnode;
690 }
691
692 /* ----------------
693  *              _copyVar
694  * ----------------
695  */
696 static Var *
697 _copyVar(Var *from)
698 {
699         Var                *newnode = makeNode(Var);
700
701         /* ----------------
702          *      copy remainder of node
703          * ----------------
704          */
705         newnode->varno = from->varno;
706         newnode->varattno = from->varattno;
707         newnode->vartype = from->vartype;
708         newnode->vartypmod = from->vartypmod;
709         newnode->varlevelsup = from->varlevelsup;
710
711         newnode->varnoold = from->varnoold;
712         newnode->varoattno = from->varoattno;
713
714         return newnode;
715 }
716
717 static Attr *
718 _copyAttr(Attr *from)
719 {
720         Attr       *newnode = makeNode(Attr);
721
722         if (from->relname)
723                 newnode->relname = pstrdup(from->relname);
724         Node_Copy(from, newnode, attrs);
725
726         return newnode;
727 }
728
729 /* ----------------
730  *              _copyOper
731  * ----------------
732  */
733 static Oper *
734 _copyOper(Oper *from)
735 {
736         Oper       *newnode = makeNode(Oper);
737
738         /* ----------------
739          *      copy remainder of node
740          * ----------------
741          */
742         newnode->opno = from->opno;
743         newnode->opid = from->opid;
744         newnode->opresulttype = from->opresulttype;
745         /* Do not copy the run-time state, if any */
746         newnode->op_fcache = NULL;
747
748         return newnode;
749 }
750
751 /* ----------------
752  *              _copyConst
753  * ----------------
754  */
755 static Const *
756 _copyConst(Const *from)
757 {
758         Const      *newnode = makeNode(Const);
759
760         /* ----------------
761          *      copy remainder of node
762          * ----------------
763          */
764         newnode->consttype = from->consttype;
765         newnode->constlen = from->constlen;
766
767         if (from->constbyval || from->constisnull)
768         {
769                 /* ----------------
770                  *      passed by value so just copy the datum.
771                  *      Also, don't try to copy struct when value is null!
772                  * ----------------
773                  */
774                 newnode->constvalue = from->constvalue;
775         }
776         else
777         {
778                 /* ----------------
779                  *      not passed by value. datum contains a pointer.
780                  * ----------------
781                  */
782                 int                     length = from->constlen;
783
784                 if (length == -1)               /* variable-length type? */
785                         length = VARSIZE(from->constvalue);
786                 newnode->constvalue = PointerGetDatum(palloc(length));
787                 memcpy(DatumGetPointer(newnode->constvalue),
788                            DatumGetPointer(from->constvalue),
789                            length);
790         }
791
792         newnode->constisnull = from->constisnull;
793         newnode->constbyval = from->constbyval;
794         newnode->constisset = from->constisset;
795         newnode->constiscast = from->constiscast;
796
797         return newnode;
798 }
799
800 /* ----------------
801  *              _copyParam
802  * ----------------
803  */
804 static Param *
805 _copyParam(Param *from)
806 {
807         Param      *newnode = makeNode(Param);
808
809         /* ----------------
810          *      copy remainder of node
811          * ----------------
812          */
813         newnode->paramkind = from->paramkind;
814         newnode->paramid = from->paramid;
815
816         if (from->paramname != NULL)
817                 newnode->paramname = pstrdup(from->paramname);
818         newnode->paramtype = from->paramtype;
819
820         return newnode;
821 }
822
823 /* ----------------
824  *              _copyFunc
825  * ----------------
826  */
827 static Func *
828 _copyFunc(Func *from)
829 {
830         Func       *newnode = makeNode(Func);
831
832         /* ----------------
833          *      copy remainder of node
834          * ----------------
835          */
836         newnode->funcid = from->funcid;
837         newnode->functype = from->functype;
838         /* Do not copy the run-time state, if any */
839         newnode->func_fcache = NULL;
840
841         return newnode;
842 }
843
844 /* ----------------
845  *              _copyAggref
846  * ----------------
847  */
848 static Aggref *
849 _copyAggref(Aggref *from)
850 {
851         Aggref     *newnode = makeNode(Aggref);
852
853         /* ----------------
854          *      copy remainder of node
855          * ----------------
856          */
857         newnode->aggname = pstrdup(from->aggname);
858         newnode->basetype = from->basetype;
859         newnode->aggtype = from->aggtype;
860         Node_Copy(from, newnode, target);
861         newnode->aggstar = from->aggstar;
862         newnode->aggdistinct = from->aggdistinct;
863         newnode->aggno = from->aggno;           /* probably not needed */
864
865         return newnode;
866 }
867
868 /* ----------------
869  *              _copySubLink
870  * ----------------
871  */
872 static SubLink *
873 _copySubLink(SubLink *from)
874 {
875         SubLink    *newnode = makeNode(SubLink);
876
877         /* ----------------
878          *      copy remainder of node
879          * ----------------
880          */
881         newnode->subLinkType = from->subLinkType;
882         newnode->useor = from->useor;
883         Node_Copy(from, newnode, lefthand);
884         Node_Copy(from, newnode, oper);
885         Node_Copy(from, newnode, subselect);
886
887         return newnode;
888 }
889
890 /* ----------------
891  *              _copyFieldSelect
892  * ----------------
893  */
894 static FieldSelect *
895 _copyFieldSelect(FieldSelect *from)
896 {
897         FieldSelect *newnode = makeNode(FieldSelect);
898
899         /* ----------------
900          *      copy remainder of node
901          * ----------------
902          */
903         Node_Copy(from, newnode, arg);
904         newnode->fieldnum = from->fieldnum;
905         newnode->resulttype = from->resulttype;
906         newnode->resulttypmod = from->resulttypmod;
907
908         return newnode;
909 }
910
911 /* ----------------
912  *              _copyRelabelType
913  * ----------------
914  */
915 static RelabelType *
916 _copyRelabelType(RelabelType *from)
917 {
918         RelabelType *newnode = makeNode(RelabelType);
919
920         /* ----------------
921          *      copy remainder of node
922          * ----------------
923          */
924         Node_Copy(from, newnode, arg);
925         newnode->resulttype = from->resulttype;
926         newnode->resulttypmod = from->resulttypmod;
927
928         return newnode;
929 }
930
931 static RangeTblRef *
932 _copyRangeTblRef(RangeTblRef *from)
933 {
934         RangeTblRef *newnode = makeNode(RangeTblRef);
935
936         newnode->rtindex = from->rtindex;
937
938         return newnode;
939 }
940
941 static FromExpr *
942 _copyFromExpr(FromExpr *from)
943 {
944         FromExpr *newnode = makeNode(FromExpr);
945
946         Node_Copy(from, newnode, fromlist);
947         Node_Copy(from, newnode, quals);
948
949         return newnode;
950 }
951
952 static JoinExpr *
953 _copyJoinExpr(JoinExpr *from)
954 {
955         JoinExpr *newnode = makeNode(JoinExpr);
956
957         newnode->jointype = from->jointype;
958         newnode->isNatural = from->isNatural;
959         Node_Copy(from, newnode, larg);
960         Node_Copy(from, newnode, rarg);
961         Node_Copy(from, newnode, using);
962         Node_Copy(from, newnode, quals);
963         Node_Copy(from, newnode, alias);
964         Node_Copy(from, newnode, colnames);
965         Node_Copy(from, newnode, colvars);
966
967         return newnode;
968 }
969
970 /* ----------------
971  *              _copyCaseExpr
972  * ----------------
973  */
974 static CaseExpr *
975 _copyCaseExpr(CaseExpr *from)
976 {
977         CaseExpr   *newnode = makeNode(CaseExpr);
978
979         /* ----------------
980          *      copy remainder of node
981          * ----------------
982          */
983         newnode->casetype = from->casetype;
984
985         Node_Copy(from, newnode, arg);
986         Node_Copy(from, newnode, args);
987         Node_Copy(from, newnode, defresult);
988
989         return newnode;
990 }
991
992 /* ----------------
993  *              _copyCaseWhen
994  * ----------------
995  */
996 static CaseWhen *
997 _copyCaseWhen(CaseWhen *from)
998 {
999         CaseWhen   *newnode = makeNode(CaseWhen);
1000
1001         /* ----------------
1002          *      copy remainder of node
1003          * ----------------
1004          */
1005         Node_Copy(from, newnode, expr);
1006         Node_Copy(from, newnode, result);
1007
1008         return newnode;
1009 }
1010
1011 static ArrayRef *
1012 _copyArrayRef(ArrayRef *from)
1013 {
1014         ArrayRef   *newnode = makeNode(ArrayRef);
1015
1016         /* ----------------
1017          *      copy remainder of node
1018          * ----------------
1019          */
1020         newnode->refattrlength = from->refattrlength;
1021         newnode->refelemlength = from->refelemlength;
1022         newnode->refelemtype = from->refelemtype;
1023         newnode->refelembyval = from->refelembyval;
1024
1025         Node_Copy(from, newnode, refupperindexpr);
1026         Node_Copy(from, newnode, reflowerindexpr);
1027         Node_Copy(from, newnode, refexpr);
1028         Node_Copy(from, newnode, refassgnexpr);
1029
1030         return newnode;
1031 }
1032
1033 /* ****************************************************************
1034  *                                              relation.h copy functions
1035  * ****************************************************************
1036  */
1037
1038 /* ----------------
1039  *              _copyRelOptInfo
1040  * ----------------
1041  */
1042 /*
1043  *      when you change this, also make sure to fix up xfunc_copyRelOptInfo in
1044  *      planner/path/xfunc.c accordingly!!!
1045  *              -- JMH, 8/2/93
1046  */
1047 static RelOptInfo *
1048 _copyRelOptInfo(RelOptInfo *from)
1049 {
1050         RelOptInfo *newnode = makeNode(RelOptInfo);
1051
1052         newnode->relids = listCopy(from->relids);
1053
1054         newnode->rows = from->rows;
1055         newnode->width = from->width;
1056
1057         Node_Copy(from, newnode, targetlist);
1058         Node_Copy(from, newnode, pathlist);
1059         /* XXX cheapest-path fields should point to members of pathlist? */
1060         Node_Copy(from, newnode, cheapest_startup_path);
1061         Node_Copy(from, newnode, cheapest_total_path);
1062         newnode->pruneable = from->pruneable;
1063
1064         newnode->issubquery = from->issubquery;
1065         newnode->indexed = from->indexed;
1066         newnode->pages = from->pages;
1067         newnode->tuples = from->tuples;
1068         Node_Copy(from, newnode, subplan);
1069
1070         Node_Copy(from, newnode, baserestrictinfo);
1071         newnode->baserestrictcost = from->baserestrictcost;
1072         newnode->outerjoinset = listCopy(from->outerjoinset);
1073         Node_Copy(from, newnode, joininfo);
1074         Node_Copy(from, newnode, innerjoin);
1075
1076         return newnode;
1077 }
1078
1079 /* ----------------
1080  *              _copyIndexOptInfo
1081  * ----------------
1082  */
1083 static IndexOptInfo *
1084 _copyIndexOptInfo(IndexOptInfo *from)
1085 {
1086         IndexOptInfo *newnode = makeNode(IndexOptInfo);
1087         int                     i,
1088                                 len;
1089
1090         newnode->indexoid = from->indexoid;
1091         newnode->pages = from->pages;
1092         newnode->tuples = from->tuples;
1093
1094         if (from->classlist)
1095         {
1096                 for (len = 0; from->classlist[len] != 0; len++)
1097                         ;
1098                 newnode->classlist = (Oid *) palloc(sizeof(Oid) * (len + 1));
1099                 for (i = 0; i < len; i++)
1100                         newnode->classlist[i] = from->classlist[i];
1101                 newnode->classlist[len] = 0;
1102         }
1103
1104         if (from->indexkeys)
1105         {
1106                 for (len = 0; from->indexkeys[len] != 0; len++)
1107                         ;
1108                 newnode->indexkeys = (int *) palloc(sizeof(int) * (len + 1));
1109                 for (i = 0; i < len; i++)
1110                         newnode->indexkeys[i] = from->indexkeys[i];
1111                 newnode->indexkeys[len] = 0;
1112         }
1113
1114         if (from->ordering)
1115         {
1116                 for (len = 0; from->ordering[len] != 0; len++)
1117                         ;
1118                 newnode->ordering = (Oid *) palloc(sizeof(Oid) * (len + 1));
1119                 for (i = 0; i < len; i++)
1120                         newnode->ordering[i] = from->ordering[i];
1121                 newnode->ordering[len] = 0;
1122         }
1123
1124         newnode->relam = from->relam;
1125         newnode->amcostestimate = from->amcostestimate;
1126         newnode->indproc = from->indproc;
1127         Node_Copy(from, newnode, indpred);
1128         newnode->lossy = from->lossy;
1129
1130         return newnode;
1131 }
1132
1133 /* ----------------
1134  *              CopyPathFields
1135  *
1136  *              This function copies the fields of the Path node.  It is used by
1137  *              all the copy functions for classes which inherit from Path.
1138  * ----------------
1139  */
1140 static void
1141 CopyPathFields(Path *from, Path *newnode)
1142 {
1143
1144         /*
1145          * Modify the next line, since it causes the copying to cycle (i.e.
1146          * the parent points right back here! -- JMH, 7/7/92. Old version:
1147          * Node_Copy(from, newnode, parent);
1148          */
1149         newnode->parent = from->parent;
1150
1151         newnode->startup_cost = from->startup_cost;
1152         newnode->total_cost = from->total_cost;
1153
1154         newnode->pathtype = from->pathtype;
1155
1156         Node_Copy(from, newnode, pathkeys);
1157 }
1158
1159 /* ----------------
1160  *              _copyPath
1161  * ----------------
1162  */
1163 static Path *
1164 _copyPath(Path *from)
1165 {
1166         Path       *newnode = makeNode(Path);
1167
1168         CopyPathFields(from, newnode);
1169
1170         return newnode;
1171 }
1172
1173 /* ----------------
1174  *              _copyIndexPath
1175  * ----------------
1176  */
1177 static IndexPath *
1178 _copyIndexPath(IndexPath *from)
1179 {
1180         IndexPath  *newnode = makeNode(IndexPath);
1181
1182         /* ----------------
1183          *      copy the node superclass fields
1184          * ----------------
1185          */
1186         CopyPathFields((Path *) from, (Path *) newnode);
1187
1188         /* ----------------
1189          *      copy remainder of node
1190          * ----------------
1191          */
1192         newnode->indexid = listCopy(from->indexid);
1193         Node_Copy(from, newnode, indexqual);
1194         newnode->indexscandir = from->indexscandir;
1195         newnode->joinrelids = listCopy(from->joinrelids);
1196         newnode->alljoinquals = from->alljoinquals;
1197         newnode->rows = from->rows;
1198
1199         return newnode;
1200 }
1201
1202 /* ----------------
1203  *                              _copyTidPath
1204  * ----------------
1205  */
1206 static TidPath *
1207 _copyTidPath(TidPath *from)
1208 {
1209         TidPath    *newnode = makeNode(TidPath);
1210
1211         /* ----------------
1212          *      copy the node superclass fields
1213          * ----------------
1214          */
1215         CopyPathFields((Path *) from, (Path *) newnode);
1216
1217         /* ----------------
1218          *      copy remainder of node
1219          * ----------------
1220          */
1221         Node_Copy(from, newnode, tideval);
1222         newnode->unjoined_relids = listCopy(from->unjoined_relids);
1223
1224         return newnode;
1225 }
1226
1227 /* ----------------
1228  *              CopyJoinPathFields
1229  *
1230  *              This function copies the fields of the JoinPath node.  It is used by
1231  *              all the copy functions for classes which inherit from JoinPath.
1232  * ----------------
1233  */
1234 static void
1235 CopyJoinPathFields(JoinPath *from, JoinPath *newnode)
1236 {
1237         newnode->jointype = from->jointype;
1238         Node_Copy(from, newnode, outerjoinpath);
1239         Node_Copy(from, newnode, innerjoinpath);
1240         Node_Copy(from, newnode, joinrestrictinfo);
1241 }
1242
1243 /* ----------------
1244  *              _copyNestPath
1245  * ----------------
1246  */
1247 static NestPath *
1248 _copyNestPath(NestPath *from)
1249 {
1250         NestPath   *newnode = makeNode(NestPath);
1251
1252         /* ----------------
1253          *      copy the node superclass fields
1254          * ----------------
1255          */
1256         CopyPathFields((Path *) from, (Path *) newnode);
1257         CopyJoinPathFields((JoinPath *) from, (JoinPath *) newnode);
1258
1259         return newnode;
1260 }
1261
1262 /* ----------------
1263  *              _copyMergePath
1264  * ----------------
1265  */
1266 static MergePath *
1267 _copyMergePath(MergePath *from)
1268 {
1269         MergePath  *newnode = makeNode(MergePath);
1270
1271         /* ----------------
1272          *      copy the node superclass fields
1273          * ----------------
1274          */
1275         CopyPathFields((Path *) from, (Path *) newnode);
1276         CopyJoinPathFields((JoinPath *) from, (JoinPath *) newnode);
1277
1278         /* ----------------
1279          *      copy the remainder of the node
1280          * ----------------
1281          */
1282         Node_Copy(from, newnode, path_mergeclauses);
1283         Node_Copy(from, newnode, outersortkeys);
1284         Node_Copy(from, newnode, innersortkeys);
1285
1286         return newnode;
1287 }
1288
1289 /* ----------------
1290  *              _copyHashPath
1291  * ----------------
1292  */
1293 static HashPath *
1294 _copyHashPath(HashPath *from)
1295 {
1296         HashPath   *newnode = makeNode(HashPath);
1297
1298         /* ----------------
1299          *      copy the node superclass fields
1300          * ----------------
1301          */
1302         CopyPathFields((Path *) from, (Path *) newnode);
1303         CopyJoinPathFields((JoinPath *) from, (JoinPath *) newnode);
1304
1305         /* ----------------
1306          *      copy remainder of node
1307          * ----------------
1308          */
1309         Node_Copy(from, newnode, path_hashclauses);
1310
1311         return newnode;
1312 }
1313
1314 /* ----------------
1315  *              _copyPathKeyItem
1316  * ----------------
1317  */
1318 static PathKeyItem *
1319 _copyPathKeyItem(PathKeyItem *from)
1320 {
1321         PathKeyItem *newnode = makeNode(PathKeyItem);
1322
1323         /* ----------------
1324          *      copy remainder of node
1325          * ----------------
1326          */
1327         Node_Copy(from, newnode, key);
1328         newnode->sortop = from->sortop;
1329
1330         return newnode;
1331 }
1332
1333 /* ----------------
1334  *              _copyRestrictInfo
1335  * ----------------
1336  */
1337 static RestrictInfo *
1338 _copyRestrictInfo(RestrictInfo *from)
1339 {
1340         RestrictInfo *newnode = makeNode(RestrictInfo);
1341
1342         /* ----------------
1343          *      copy remainder of node
1344          * ----------------
1345          */
1346         Node_Copy(from, newnode, clause);
1347         newnode->ispusheddown = from->ispusheddown;
1348         Node_Copy(from, newnode, subclauseindices);
1349         newnode->mergejoinoperator = from->mergejoinoperator;
1350         newnode->left_sortop = from->left_sortop;
1351         newnode->right_sortop = from->right_sortop;
1352         newnode->hashjoinoperator = from->hashjoinoperator;
1353
1354         return newnode;
1355 }
1356
1357 /* ----------------
1358  *              _copyJoinInfo
1359  * ----------------
1360  */
1361 static JoinInfo *
1362 _copyJoinInfo(JoinInfo *from)
1363 {
1364         JoinInfo   *newnode = makeNode(JoinInfo);
1365
1366         /* ----------------
1367          *      copy remainder of node
1368          * ----------------
1369          */
1370         newnode->unjoined_relids = listCopy(from->unjoined_relids);
1371         Node_Copy(from, newnode, jinfo_restrictinfo);
1372
1373         return newnode;
1374 }
1375
1376 static Iter *
1377 _copyIter(Iter *from)
1378 {
1379         Iter       *newnode = makeNode(Iter);
1380
1381         Node_Copy(from, newnode, iterexpr);
1382         newnode->itertype = from->itertype;
1383
1384         return newnode;
1385 }
1386
1387 static Stream *
1388 _copyStream(Stream *from)
1389 {
1390         Stream     *newnode = makeNode(Stream);
1391
1392         newnode->pathptr = from->pathptr;
1393         newnode->cinfo = from->cinfo;
1394         newnode->clausetype = from->clausetype;
1395
1396         newnode->upstream = (StreamPtr) NULL;           /* only copy nodes
1397                                                                                                  * downwards! */
1398         Node_Copy(from, newnode, downstream);
1399         if (newnode->downstream)
1400                 ((Stream *) newnode->downstream)->upstream = (Stream *) newnode;
1401
1402         newnode->groupup = from->groupup;
1403         newnode->groupcost = from->groupcost;
1404         newnode->groupsel = from->groupsel;
1405
1406         return newnode;
1407 }
1408
1409 /* ****************************************************************
1410  *                                      parsenodes.h copy functions
1411  * ****************************************************************
1412  */
1413
1414 static TargetEntry *
1415 _copyTargetEntry(TargetEntry *from)
1416 {
1417         TargetEntry *newnode = makeNode(TargetEntry);
1418
1419         Node_Copy(from, newnode, resdom);
1420         Node_Copy(from, newnode, fjoin);
1421         Node_Copy(from, newnode, expr);
1422         return newnode;
1423 }
1424
1425 static RangeTblEntry *
1426 _copyRangeTblEntry(RangeTblEntry *from)
1427 {
1428         RangeTblEntry *newnode = makeNode(RangeTblEntry);
1429
1430         if (from->relname)
1431                 newnode->relname = pstrdup(from->relname);
1432         newnode->relid = from->relid;
1433         Node_Copy(from, newnode, subquery);
1434         Node_Copy(from, newnode, alias);
1435         Node_Copy(from, newnode, eref);
1436         newnode->inh = from->inh;
1437         newnode->inFromCl = from->inFromCl;
1438         newnode->checkForRead = from->checkForRead;
1439         newnode->checkForWrite = from->checkForWrite;
1440         newnode->checkAsUser = from->checkAsUser;
1441
1442         return newnode;
1443 }
1444
1445 static FkConstraint *
1446 _copyFkConstraint(FkConstraint *from)
1447 {
1448         FkConstraint    *newnode = makeNode(FkConstraint);
1449
1450         if (from->constr_name)
1451                 newnode->constr_name = pstrdup(from->constr_name);
1452         if (from->pktable_name)
1453                 newnode->pktable_name = pstrdup(from->pktable_name);
1454         Node_Copy(from, newnode, fk_attrs);
1455         Node_Copy(from, newnode, pk_attrs);
1456         if (from->match_type)
1457                 newnode->match_type = pstrdup(from->match_type);
1458         newnode->actions = from->actions;
1459         newnode->deferrable = from->deferrable;
1460         newnode->initdeferred = from->initdeferred;
1461         
1462         return newnode;
1463 }
1464
1465 static SortClause *
1466 _copySortClause(SortClause *from)
1467 {
1468         SortClause *newnode = makeNode(SortClause);
1469
1470         newnode->tleSortGroupRef = from->tleSortGroupRef;
1471         newnode->sortop = from->sortop;
1472
1473         return newnode;
1474 }
1475
1476 static A_Expr *
1477 _copyAExpr(A_Expr *from)
1478 {
1479         A_Expr    *newnode = makeNode(A_Expr);
1480
1481         newnode->oper = from->oper;
1482         if (from->opname)
1483                 newnode->opname = pstrdup(from->opname);
1484         Node_Copy(from, newnode, lexpr);
1485         Node_Copy(from, newnode, rexpr);
1486
1487         return newnode;
1488 }
1489
1490 static A_Const *
1491 _copyAConst(A_Const *from)
1492 {
1493         A_Const    *newnode = makeNode(A_Const);
1494
1495         newnode->val = *((Value *) (copyObject(&(from->val))));
1496         Node_Copy(from, newnode, typename);
1497
1498         return newnode;
1499 }
1500
1501 static ParamNo *
1502 _copyParamNo(ParamNo *from)
1503 {
1504         ParamNo    *newnode = makeNode(ParamNo);
1505
1506         newnode->number = from->number;
1507         Node_Copy(from, newnode, typename);
1508         Node_Copy(from, newnode, indirection);
1509
1510         return newnode;
1511 }
1512
1513 static Ident *
1514 _copyIdent(Ident *from)
1515 {
1516         Ident    *newnode = makeNode(Ident);
1517
1518         if (from->name)
1519                 newnode->name = pstrdup(from->name);
1520         Node_Copy(from, newnode, indirection);
1521         newnode->isRel = from->isRel;
1522
1523         return newnode;
1524 }
1525
1526 static FuncCall *
1527 _copyFuncCall(FuncCall *from)
1528 {
1529         FuncCall    *newnode = makeNode(FuncCall);
1530
1531         if (from->funcname)
1532                 newnode->funcname = pstrdup(from->funcname);
1533         Node_Copy(from, newnode, args);
1534         newnode->agg_star = from->agg_star;
1535         newnode->agg_distinct = from->agg_distinct;
1536
1537         return newnode;
1538 }
1539
1540 static A_Indices *
1541 _copyAIndices(A_Indices *from)
1542 {
1543         A_Indices    *newnode = makeNode(A_Indices);
1544
1545         Node_Copy(from, newnode, lidx);
1546         Node_Copy(from, newnode, uidx);
1547
1548         return newnode;
1549 }
1550
1551 static ResTarget *
1552 _copyResTarget(ResTarget *from)
1553 {
1554         ResTarget    *newnode = makeNode(ResTarget);
1555
1556         if (from->name)
1557                 newnode->name = pstrdup(from->name);
1558         Node_Copy(from, newnode, indirection);
1559         Node_Copy(from, newnode, val);
1560
1561         return newnode;
1562 }
1563
1564 static TypeName *
1565 _copyTypeName(TypeName *from)
1566 {
1567         TypeName   *newnode = makeNode(TypeName);
1568
1569         if (from->name)
1570                 newnode->name = pstrdup(from->name);
1571         newnode->timezone = from->timezone;
1572         newnode->setof = from->setof;
1573         newnode->typmod = from->typmod;
1574         Node_Copy(from, newnode, arrayBounds);
1575
1576         return newnode;
1577 }
1578
1579 static SortGroupBy *
1580 _copySortGroupBy(SortGroupBy *from)
1581 {
1582         SortGroupBy   *newnode = makeNode(SortGroupBy);
1583
1584         if (from->useOp)
1585                 newnode->useOp = pstrdup(from->useOp);
1586         Node_Copy(from, newnode, node);
1587
1588         return newnode;
1589 }
1590
1591 static RangeVar *
1592 _copyRangeVar(RangeVar *from)
1593 {
1594         RangeVar   *newnode = makeNode(RangeVar);
1595
1596         if (from->relname)
1597                 newnode->relname = pstrdup(from->relname);
1598         newnode->inh = from->inh;
1599         Node_Copy(from, newnode, name);
1600
1601         return newnode;
1602 }
1603
1604 static RangeSubselect *
1605 _copyRangeSubselect(RangeSubselect *from)
1606 {
1607         RangeSubselect   *newnode = makeNode(RangeSubselect);
1608
1609         Node_Copy(from, newnode, subquery);
1610         Node_Copy(from, newnode, name);
1611
1612         return newnode;
1613 }
1614
1615 static TypeCast *
1616 _copyTypeCast(TypeCast *from)
1617 {
1618         TypeCast   *newnode = makeNode(TypeCast);
1619
1620         Node_Copy(from, newnode, arg);
1621         Node_Copy(from, newnode, typename);
1622
1623         return newnode;
1624 }
1625
1626 static IndexElem *
1627 _copyIndexElem(IndexElem *from)
1628 {
1629         IndexElem   *newnode = makeNode(IndexElem);
1630
1631         if (from->name)
1632                 newnode->name = pstrdup(from->name);
1633         Node_Copy(from, newnode, args);
1634         if (from->class)
1635                 newnode->class = pstrdup(from->class);
1636
1637         return newnode;
1638 }
1639
1640 static ColumnDef *
1641 _copyColumnDef(ColumnDef *from)
1642 {
1643         ColumnDef   *newnode = makeNode(ColumnDef);
1644
1645         if (from->colname)
1646                 newnode->colname = pstrdup(from->colname);
1647         Node_Copy(from, newnode, typename);
1648         newnode->is_not_null = from->is_not_null;
1649         newnode->is_sequence = from->is_sequence;
1650         Node_Copy(from, newnode, raw_default);
1651         if (from->cooked_default)
1652                 newnode->cooked_default = pstrdup(from->cooked_default);
1653         Node_Copy(from, newnode, constraints);
1654
1655         return newnode;
1656 }
1657
1658 static Constraint *
1659 _copyConstraint(Constraint *from)
1660 {
1661         Constraint   *newnode = makeNode(Constraint);
1662
1663         newnode->contype = from->contype;
1664         if (from->name)
1665                 newnode->name = pstrdup(from->name);
1666         Node_Copy(from, newnode, raw_expr);
1667         if (from->cooked_expr)
1668                 newnode->cooked_expr = pstrdup(from->cooked_expr);
1669         Node_Copy(from, newnode, keys);
1670
1671         return newnode;
1672 }
1673
1674 static DefElem *
1675 _copyDefElem(DefElem *from)
1676 {
1677         DefElem   *newnode = makeNode(DefElem);
1678
1679         if (from->defname)
1680                 newnode->defname = pstrdup(from->defname);
1681         Node_Copy(from, newnode, arg);
1682
1683         return newnode;
1684 }
1685
1686 static Query *
1687 _copyQuery(Query *from)
1688 {
1689         Query      *newnode = makeNode(Query);
1690
1691         newnode->commandType = from->commandType;
1692         Node_Copy(from, newnode, utilityStmt);
1693         newnode->resultRelation = from->resultRelation;
1694         if (from->into)
1695                 newnode->into = pstrdup(from->into);
1696         newnode->isPortal = from->isPortal;
1697         newnode->isBinary = from->isBinary;
1698         newnode->isTemp = from->isTemp;
1699         newnode->unionall = from->unionall;
1700         newnode->hasAggs = from->hasAggs;
1701         newnode->hasSubLinks = from->hasSubLinks;
1702
1703         Node_Copy(from, newnode, rtable);
1704         Node_Copy(from, newnode, jointree);
1705
1706         Node_Copy(from, newnode, targetList);
1707
1708         newnode->rowMarks = listCopy(from->rowMarks);
1709
1710         Node_Copy(from, newnode, distinctClause);
1711         Node_Copy(from, newnode, sortClause);
1712         Node_Copy(from, newnode, groupClause);
1713         Node_Copy(from, newnode, havingQual);
1714
1715         /* why is intersectClause missing? */
1716         Node_Copy(from, newnode, unionClause);
1717
1718         Node_Copy(from, newnode, limitOffset);
1719         Node_Copy(from, newnode, limitCount);
1720
1721         /*
1722          * We do not copy the planner internal fields: base_rel_list,
1723          * join_rel_list, equi_key_list, query_pathkeys. Not entirely clear if
1724          * this is right?
1725          */
1726
1727         return newnode;
1728 }
1729
1730 static InsertStmt *
1731 _copyInsertStmt(InsertStmt *from)
1732 {
1733         InsertStmt *newnode = makeNode(InsertStmt);
1734         
1735         if (from->relname)
1736                 newnode->relname = pstrdup(from->relname);
1737         Node_Copy(from, newnode, distinctClause);
1738         Node_Copy(from, newnode, cols);
1739         Node_Copy(from, newnode, targetList);
1740         Node_Copy(from, newnode, fromClause);
1741         Node_Copy(from, newnode, whereClause);
1742         Node_Copy(from, newnode, groupClause);
1743         Node_Copy(from, newnode, havingClause);
1744         Node_Copy(from, newnode, unionClause);
1745         newnode->unionall = from->unionall;
1746         Node_Copy(from, newnode, intersectClause);
1747         Node_Copy(from, newnode, forUpdate);
1748
1749         return newnode;
1750 }
1751
1752 static DeleteStmt *
1753 _copyDeleteStmt(DeleteStmt *from)
1754 {
1755         DeleteStmt *newnode = makeNode(DeleteStmt);
1756         
1757         if (from->relname)
1758                 newnode->relname = pstrdup(from->relname);
1759         Node_Copy(from, newnode, whereClause);
1760         newnode->inh = from->inh;
1761
1762         return newnode;
1763 }
1764
1765 static UpdateStmt *
1766 _copyUpdateStmt(UpdateStmt *from)
1767 {
1768         UpdateStmt *newnode = makeNode(UpdateStmt);
1769         
1770         if (from->relname)
1771                 newnode->relname = pstrdup(from->relname);
1772         Node_Copy(from, newnode, targetList);
1773         Node_Copy(from, newnode, whereClause);
1774         Node_Copy(from, newnode, fromClause);
1775         newnode->inh = from->inh;
1776
1777         return newnode;
1778 }
1779
1780 static SelectStmt *
1781 _copySelectStmt(SelectStmt *from)
1782 {
1783         SelectStmt *newnode = makeNode(SelectStmt);
1784         
1785         Node_Copy(from, newnode, distinctClause);
1786         if (from->into)
1787                 newnode->into = pstrdup(from->into);
1788         Node_Copy(from, newnode, targetList);
1789         Node_Copy(from, newnode, fromClause);
1790         Node_Copy(from, newnode, whereClause);
1791         Node_Copy(from, newnode, groupClause);
1792         Node_Copy(from, newnode, havingClause);
1793         Node_Copy(from, newnode, intersectClause);
1794         Node_Copy(from, newnode, exceptClause);
1795         Node_Copy(from, newnode, unionClause);
1796         Node_Copy(from, newnode, sortClause);
1797         if (from->portalname)
1798                 newnode->portalname = pstrdup(from->portalname);
1799         newnode->binary = from->binary;
1800         newnode->istemp = from->istemp;
1801         newnode->unionall = from->unionall;
1802         Node_Copy(from, newnode, limitOffset);
1803         Node_Copy(from, newnode, limitCount);
1804         Node_Copy(from, newnode, forUpdate);
1805
1806         return newnode;
1807 }
1808
1809 static AlterTableStmt *
1810 _copyAlterTableStmt(AlterTableStmt *from)
1811 {
1812         AlterTableStmt *newnode = makeNode(AlterTableStmt);
1813         
1814         newnode->subtype = from->subtype;
1815         if (from->relname)
1816                 newnode->relname = pstrdup(from->relname);
1817         newnode->inh = from->inh;
1818         if (from->name)
1819                 newnode->name = pstrdup(from->name);
1820         Node_Copy(from, newnode, def);
1821         newnode->behavior = from->behavior;
1822
1823         return newnode;
1824 }
1825
1826 static ChangeACLStmt *
1827 _copyChangeACLStmt(ChangeACLStmt *from)
1828 {
1829         ChangeACLStmt *newnode = makeNode(ChangeACLStmt);
1830         
1831         if (from->aclitem)
1832         {
1833                 newnode->aclitem = (struct AclItem *) palloc(sizeof(struct AclItem));
1834                 memcpy(newnode->aclitem, from->aclitem, sizeof(struct AclItem));
1835         }
1836         newnode->modechg = from->modechg;
1837         Node_Copy(from, newnode, relNames);
1838
1839         return newnode;
1840 }
1841
1842 static ClosePortalStmt *
1843 _copyClosePortalStmt(ClosePortalStmt *from)
1844 {
1845         ClosePortalStmt *newnode = makeNode(ClosePortalStmt);
1846
1847         if (from->portalname)
1848                 newnode->portalname = pstrdup(from->portalname);
1849
1850         return newnode;
1851 }
1852
1853 static ClusterStmt *
1854 _copyClusterStmt(ClusterStmt *from)
1855 {
1856         ClusterStmt *newnode = makeNode(ClusterStmt);
1857         
1858         if (from->relname)
1859                 newnode->relname = pstrdup(from->relname);
1860         if (from->indexname)
1861                 newnode->indexname = pstrdup(from->indexname);
1862
1863         return newnode;
1864 }
1865
1866 static CopyStmt *
1867 _copyCopyStmt(CopyStmt *from)
1868 {
1869         CopyStmt *newnode = makeNode(CopyStmt);
1870         
1871         newnode->binary = from->binary;
1872         if (from->relname)
1873                 newnode->relname = pstrdup(from->relname);
1874         newnode->oids = from->oids;
1875         newnode->direction = from->direction;
1876         if (from->filename)
1877                 newnode->filename = pstrdup(from->filename);
1878         if (from->delimiter)
1879                 newnode->delimiter = pstrdup(from->delimiter);
1880         if (from->null_print)
1881                 newnode->null_print = pstrdup(from->null_print);
1882
1883         return newnode;
1884 }
1885
1886 static CreateStmt *
1887 _copyCreateStmt(CreateStmt *from)
1888 {
1889         CreateStmt *newnode = makeNode(CreateStmt);
1890         
1891         newnode->istemp = from->istemp;
1892         newnode->relname = pstrdup(from->relname);
1893         Node_Copy(from, newnode, tableElts);
1894         Node_Copy(from, newnode, inhRelnames);
1895         Node_Copy(from, newnode, constraints);
1896
1897         return newnode;
1898 }
1899
1900 static VersionStmt *
1901 _copyVersionStmt(VersionStmt *from)
1902 {
1903         VersionStmt *newnode = makeNode(VersionStmt);
1904         
1905         newnode->relname = pstrdup(from->relname);
1906         newnode->direction = from->direction;
1907         newnode->fromRelname = pstrdup(from->fromRelname);
1908         newnode->date = pstrdup(from->date);
1909
1910         return newnode;
1911 }
1912
1913 static DefineStmt *
1914 _copyDefineStmt(DefineStmt *from)
1915 {
1916         DefineStmt *newnode = makeNode(DefineStmt);
1917         
1918         newnode->defType = from->defType;
1919         newnode->defname = pstrdup(from->defname);
1920         Node_Copy(from, newnode, definition);
1921
1922         return newnode;
1923 }
1924
1925 static DropStmt *
1926 _copyDropStmt(DropStmt *from)
1927 {
1928         DropStmt *newnode = makeNode(DropStmt);
1929         
1930         Node_Copy(from, newnode, relNames);
1931         newnode->sequence = from->sequence;
1932
1933         return newnode;
1934 }
1935
1936 static TruncateStmt *
1937 _copyTruncateStmt(TruncateStmt *from)
1938 {
1939         TruncateStmt *newnode = makeNode(TruncateStmt);
1940
1941         newnode->relName = pstrdup(from->relName);
1942
1943         return newnode;
1944 }
1945
1946 static CommentStmt *
1947 _copyCommentStmt(CommentStmt *from)
1948 {
1949         CommentStmt *newnode = makeNode(CommentStmt);
1950         
1951         newnode->objtype = from->objtype;
1952         newnode->objname = pstrdup(from->objname);
1953         if (from->objproperty)
1954           newnode->objproperty = pstrdup(from->objproperty);
1955         Node_Copy(from, newnode, objlist);
1956         newnode->comment = pstrdup(from->comment);
1957
1958         return newnode;
1959 }
1960
1961 static ExtendStmt *
1962 _copyExtendStmt(ExtendStmt *from)
1963 {
1964         ExtendStmt *newnode = makeNode(ExtendStmt);
1965         
1966         newnode->idxname = pstrdup(from->idxname);
1967         Node_Copy(from, newnode, whereClause);
1968         Node_Copy(from, newnode, rangetable);
1969
1970         return newnode;
1971 }
1972
1973 static FetchStmt *
1974 _copyFetchStmt(FetchStmt *from)
1975 {
1976         FetchStmt *newnode = makeNode(FetchStmt);
1977         
1978         newnode->direction = from->direction;
1979         newnode->howMany = from->howMany;
1980         newnode->portalname = pstrdup(from->portalname);
1981         newnode->ismove = from->ismove;
1982
1983         return newnode;
1984 }
1985
1986 static IndexStmt *
1987 _copyIndexStmt(IndexStmt *from)
1988 {
1989         IndexStmt *newnode = makeNode(IndexStmt);
1990         
1991         newnode->idxname = pstrdup(from->idxname);
1992         newnode->relname = pstrdup(from->relname);
1993         newnode->accessMethod = pstrdup(from->accessMethod);
1994         Node_Copy(from, newnode, indexParams);
1995         Node_Copy(from, newnode, withClause);
1996         Node_Copy(from, newnode, whereClause);
1997         Node_Copy(from, newnode, rangetable);
1998         newnode->unique = from->unique;
1999         newnode->primary = from->primary;
2000
2001         return newnode;
2002 }
2003
2004 static ProcedureStmt *
2005 _copyProcedureStmt(ProcedureStmt *from)
2006 {
2007         ProcedureStmt *newnode = makeNode(ProcedureStmt);
2008         
2009         newnode->funcname = pstrdup(from->funcname);
2010         Node_Copy(from, newnode, defArgs);
2011         Node_Copy(from, newnode, returnType);
2012         Node_Copy(from, newnode, withClause);
2013         Node_Copy(from, newnode, as);
2014         newnode->language = pstrdup(from->language);
2015
2016         return newnode;
2017 }
2018
2019 static RemoveAggrStmt *
2020 _copyRemoveAggrStmt(RemoveAggrStmt *from)
2021 {
2022         RemoveAggrStmt *newnode = makeNode(RemoveAggrStmt);
2023         
2024         newnode->aggname = pstrdup(from->aggname);
2025         newnode->aggtype = pstrdup(from->aggtype);
2026
2027         return newnode;
2028 }
2029
2030 static RemoveFuncStmt *
2031 _copyRemoveFuncStmt(RemoveFuncStmt *from)
2032 {
2033         RemoveFuncStmt *newnode = makeNode(RemoveFuncStmt);
2034         
2035         newnode->funcname = pstrdup(from->funcname);
2036         Node_Copy(from, newnode, args);
2037
2038         return newnode;
2039 }
2040
2041 static RemoveOperStmt *
2042 _copyRemoveOperStmt(RemoveOperStmt *from)
2043 {
2044         RemoveOperStmt *newnode = makeNode(RemoveOperStmt);
2045         
2046         newnode->opname = pstrdup(from->opname);
2047         Node_Copy(from, newnode, args);
2048
2049         return newnode;
2050 }
2051
2052 static RemoveStmt *
2053 _copyRemoveStmt(RemoveStmt *from)
2054 {
2055         RemoveStmt *newnode = makeNode(RemoveStmt);
2056         
2057         newnode->removeType = from->removeType;
2058         newnode->name = pstrdup(from->name);
2059
2060         return newnode;
2061 }
2062
2063 static RenameStmt *
2064 _copyRenameStmt(RenameStmt *from)
2065 {
2066         RenameStmt *newnode = makeNode(RenameStmt);
2067         
2068         newnode->relname = pstrdup(from->relname);
2069         newnode->inh = from->inh;
2070         if (from->column)
2071                 newnode->column = pstrdup(from->column);
2072         if (from->newname)
2073                 newnode->newname = pstrdup(from->newname);
2074
2075         return newnode;
2076 }
2077
2078 static RuleStmt *
2079 _copyRuleStmt(RuleStmt *from)
2080 {
2081         RuleStmt *newnode = makeNode(RuleStmt);
2082         
2083         newnode->rulename = pstrdup(from->rulename);
2084         Node_Copy(from, newnode, whereClause);
2085         newnode->event = from->event;
2086         Node_Copy(from, newnode, object);
2087         newnode->instead = from->instead;
2088         Node_Copy(from, newnode, actions);
2089
2090         return newnode;
2091 }
2092
2093 static NotifyStmt *
2094 _copyNotifyStmt(NotifyStmt *from)
2095 {
2096         NotifyStmt *newnode = makeNode(NotifyStmt);
2097
2098         if (from->relname)
2099                 newnode->relname = pstrdup(from->relname);
2100
2101         return newnode;
2102 }
2103
2104 static ListenStmt *
2105 _copyListenStmt(ListenStmt *from)
2106 {
2107         ListenStmt *newnode = makeNode(ListenStmt);
2108
2109         if (from->relname)
2110                 newnode->relname = pstrdup(from->relname);
2111
2112         return newnode;
2113 }
2114
2115 static UnlistenStmt *
2116 _copyUnlistenStmt(UnlistenStmt *from)
2117 {
2118         UnlistenStmt *newnode = makeNode(UnlistenStmt);
2119
2120         if (from->relname)
2121                 newnode->relname = pstrdup(from->relname);
2122
2123         return newnode;
2124 }
2125
2126 static TransactionStmt *
2127 _copyTransactionStmt(TransactionStmt *from)
2128 {
2129         TransactionStmt *newnode = makeNode(TransactionStmt);
2130
2131         newnode->command = from->command;
2132
2133         return newnode;
2134 }
2135
2136 static ViewStmt *
2137 _copyViewStmt(ViewStmt *from)
2138 {
2139         ViewStmt   *newnode = makeNode(ViewStmt);
2140
2141         if (from->viewname)
2142                 newnode->viewname = pstrdup(from->viewname);
2143         Node_Copy(from, newnode, aliases);
2144         Node_Copy(from, newnode, query);
2145
2146         return newnode;
2147 }
2148
2149 static LoadStmt *
2150 _copyLoadStmt(LoadStmt *from)
2151 {
2152         LoadStmt   *newnode = makeNode(LoadStmt);
2153
2154         if (from->filename)
2155                 newnode->filename = pstrdup(from->filename);
2156
2157         return newnode;
2158 }
2159
2160 static CreatedbStmt *
2161 _copyCreatedbStmt(CreatedbStmt *from)
2162 {
2163         CreatedbStmt   *newnode = makeNode(CreatedbStmt);
2164
2165         if (from->dbname)
2166                 newnode->dbname = pstrdup(from->dbname);
2167         if (from->dbpath)
2168                 newnode->dbpath = pstrdup(from->dbpath);
2169         newnode->encoding = from->encoding;
2170
2171         return newnode;
2172 }
2173
2174 static DropdbStmt *
2175 _copyDropdbStmt(DropdbStmt *from)
2176 {
2177         DropdbStmt   *newnode = makeNode(DropdbStmt);
2178
2179         if (from->dbname)
2180                 newnode->dbname = pstrdup(from->dbname);
2181
2182         return newnode;
2183 }
2184
2185 static VacuumStmt *
2186 _copyVacuumStmt(VacuumStmt *from)
2187 {
2188         VacuumStmt   *newnode = makeNode(VacuumStmt);
2189
2190         newnode->verbose = from->verbose;
2191         newnode->analyze = from->analyze;
2192         if (from->vacrel)
2193                 newnode->vacrel = pstrdup(from->vacrel);
2194         Node_Copy(from, newnode, va_spec);
2195
2196         return newnode;
2197 }
2198
2199 static ExplainStmt *
2200 _copyExplainStmt(ExplainStmt *from)
2201 {
2202         ExplainStmt   *newnode = makeNode(ExplainStmt);
2203
2204         Node_Copy(from, newnode, query);
2205         newnode->verbose = from->verbose;
2206
2207         return newnode;
2208 }
2209
2210 static CreateSeqStmt *
2211 _copyCreateSeqStmt(CreateSeqStmt *from)
2212 {
2213         CreateSeqStmt   *newnode = makeNode(CreateSeqStmt);
2214
2215         if (from->seqname)
2216                 newnode->seqname = pstrdup(from->seqname);
2217         Node_Copy(from, newnode, options);
2218
2219         return newnode;
2220 }
2221
2222 static VariableSetStmt *
2223 _copyVariableSetStmt(VariableSetStmt *from)
2224 {
2225         VariableSetStmt *newnode = makeNode(VariableSetStmt);
2226
2227         if (from->name)
2228                 newnode->name = pstrdup(from->name);
2229         if (from->value)
2230                 newnode->value = pstrdup(from->value);
2231
2232         return newnode;
2233 }
2234
2235 static VariableShowStmt *
2236 _copyVariableShowStmt(VariableShowStmt *from)
2237 {
2238         VariableShowStmt *newnode = makeNode(VariableShowStmt);
2239
2240         if (from->name)
2241                 newnode->name = pstrdup(from->name);
2242
2243         return newnode;
2244 }
2245
2246 static VariableResetStmt *
2247 _copyVariableResetStmt(VariableResetStmt *from)
2248 {
2249         VariableResetStmt *newnode = makeNode(VariableResetStmt);
2250
2251         if (from->name)
2252                 newnode->name = pstrdup(from->name);
2253
2254         return newnode;
2255 }
2256
2257 static CreateTrigStmt *
2258 _copyCreateTrigStmt(CreateTrigStmt *from)
2259 {
2260         CreateTrigStmt *newnode = makeNode(CreateTrigStmt);
2261
2262         if (from->trigname)
2263                 newnode->trigname = pstrdup(from->trigname);
2264         if (from->relname)
2265                 newnode->relname = pstrdup(from->relname);
2266         if (from->funcname)
2267                 newnode->funcname = pstrdup(from->funcname);
2268         Node_Copy(from, newnode, args);
2269         newnode->before = from->before;
2270         newnode->row = from->row;
2271         memcpy(newnode->actions, from->actions, sizeof(from->actions));
2272         if (from->lang)
2273                 newnode->lang = pstrdup(from->lang);
2274         if (from->text)
2275                 newnode->text = pstrdup(from->text);
2276         Node_Copy(from, newnode, attr);
2277         if (from->when)
2278                 newnode->when = pstrdup(from->when);
2279         newnode->isconstraint = from->isconstraint;
2280         newnode->deferrable = from->deferrable;
2281         newnode->initdeferred = from->initdeferred;
2282         if (from->constrrelname)
2283                 newnode->constrrelname = pstrdup(from->constrrelname);
2284
2285         return newnode;
2286 }
2287
2288 static DropTrigStmt *
2289 _copyDropTrigStmt(DropTrigStmt *from)
2290 {
2291         DropTrigStmt *newnode = makeNode(DropTrigStmt);
2292
2293         if (from->trigname)
2294                 newnode->trigname = pstrdup(from->trigname);
2295         if (from->relname)
2296                 newnode->relname = pstrdup(from->relname);
2297
2298         return newnode;
2299 }
2300
2301 static CreatePLangStmt *
2302 _copyCreatePLangStmt(CreatePLangStmt *from)
2303 {
2304         CreatePLangStmt *newnode = makeNode(CreatePLangStmt);
2305
2306         if (from->plname)
2307                 newnode->plname = pstrdup(from->plname);
2308         if (from->plhandler)
2309                 newnode->plhandler = pstrdup(from->plhandler);
2310         if (from->plcompiler)
2311                 newnode->plcompiler = pstrdup(from->plcompiler);
2312         newnode->pltrusted = from->pltrusted;
2313
2314         return newnode;
2315 }
2316
2317 static DropPLangStmt *
2318 _copyDropPLangStmt(DropPLangStmt *from)
2319 {
2320         DropPLangStmt *newnode = makeNode(DropPLangStmt);
2321
2322         if (from->plname)
2323                 newnode->plname = pstrdup(from->plname);
2324
2325         return newnode;
2326 }
2327
2328 static CreateUserStmt *
2329 _copyCreateUserStmt(CreateUserStmt *from)
2330 {
2331         CreateUserStmt *newnode = makeNode(CreateUserStmt);
2332
2333         if (from->user)
2334                 newnode->user = pstrdup(from->user);
2335         if (from->password)
2336                 newnode->password = pstrdup(from->password);
2337         newnode->sysid = from->sysid;
2338         newnode->createdb = from->createdb;
2339         newnode->createuser = from->createuser;
2340         Node_Copy(from, newnode, groupElts);
2341         if (from->validUntil)
2342                 newnode->validUntil = pstrdup(from->validUntil);
2343
2344         return newnode;
2345 }
2346
2347 static AlterUserStmt *
2348 _copyAlterUserStmt(AlterUserStmt *from)
2349 {
2350         AlterUserStmt *newnode = makeNode(AlterUserStmt);
2351
2352         if (from->user)
2353                 newnode->user = pstrdup(from->user);
2354         if (from->password)
2355                 newnode->password = pstrdup(from->password);
2356         newnode->createdb = from->createdb;
2357         newnode->createuser = from->createuser;
2358         if (from->validUntil)
2359                 newnode->validUntil = pstrdup(from->validUntil);
2360
2361         return newnode;
2362 }
2363
2364 static DropUserStmt *
2365 _copyDropUserStmt(DropUserStmt *from)
2366 {
2367         DropUserStmt *newnode = makeNode(DropUserStmt);
2368
2369         Node_Copy(from, newnode, users);
2370
2371         return newnode;
2372 }
2373
2374 static LockStmt *
2375 _copyLockStmt(LockStmt *from)
2376 {
2377         LockStmt   *newnode = makeNode(LockStmt);
2378
2379         if (from->relname)
2380                 newnode->relname = pstrdup(from->relname);
2381         newnode->mode = from->mode;
2382
2383         return newnode;
2384 }
2385
2386 static ConstraintsSetStmt *
2387 _copyConstraintsSetStmt(ConstraintsSetStmt *from)
2388 {
2389         ConstraintsSetStmt   *newnode = makeNode(ConstraintsSetStmt);
2390
2391         Node_Copy(from, newnode, constraints);
2392         newnode->deferred = from->deferred;
2393
2394         return newnode;
2395 }
2396
2397 static CreateGroupStmt *
2398 _copyCreateGroupStmt(CreateGroupStmt *from)
2399 {
2400         CreateGroupStmt   *newnode = makeNode(CreateGroupStmt);
2401
2402         if (from->name)
2403                 newnode->name = pstrdup(from->name);
2404         newnode->sysid = from->sysid;
2405         Node_Copy(from, newnode, initUsers);
2406
2407         return newnode;
2408 }
2409
2410 static AlterGroupStmt *
2411 _copyAlterGroupStmt(AlterGroupStmt *from)
2412 {
2413         AlterGroupStmt   *newnode = makeNode(AlterGroupStmt);
2414
2415         if (from->name)
2416                 newnode->name = pstrdup(from->name);
2417         newnode->action = from->action;
2418         newnode->sysid = from->sysid;
2419         Node_Copy(from, newnode, listUsers);
2420
2421         return newnode;
2422 }
2423
2424 static DropGroupStmt *
2425 _copyDropGroupStmt(DropGroupStmt *from)
2426 {
2427         DropGroupStmt   *newnode = makeNode(DropGroupStmt);
2428
2429         if (from->name)
2430                 newnode->name = pstrdup(from->name);
2431
2432         return newnode;
2433 }
2434
2435 static ReindexStmt *
2436 _copyReindexStmt(ReindexStmt *from)
2437 {
2438         ReindexStmt   *newnode = makeNode(ReindexStmt);
2439
2440         newnode->reindexType = from->reindexType;
2441         if (from->name)
2442                 newnode->name = pstrdup(from->name);
2443         newnode->force = from->force;
2444         newnode->all = from->all;
2445
2446         return newnode;
2447 }
2448
2449 static SetSessionStmt *
2450 _copySetSessionStmt(SetSessionStmt *from)
2451 {
2452         SetSessionStmt   *newnode = makeNode(SetSessionStmt);
2453
2454         Node_Copy(from, newnode, args);
2455
2456         return newnode;
2457 }
2458
2459
2460 /* ****************************************************************
2461  *                                      pg_list.h copy functions
2462  * ****************************************************************
2463  */
2464
2465 static Value *
2466 _copyValue(Value *from)
2467 {
2468         Value      *newnode = makeNode(Value);
2469
2470         newnode->type = from->type;
2471         switch (from->type)
2472         {
2473                 case T_Integer:
2474                         newnode->val.ival = from->val.ival;
2475                         break;
2476                 case T_Float:
2477                 case T_String:
2478                         newnode->val.str = pstrdup(from->val.str);
2479                         break;
2480                 default:
2481                         break;
2482         }
2483         return newnode;
2484 }
2485
2486 /* ----------------
2487  *              copyObject returns a copy of the node or list. If it is a list, it
2488  *              recursively copies its items.
2489  * ----------------
2490  */
2491 void *
2492 copyObject(void *from)
2493 {
2494         void       *retval;
2495
2496         if (from == NULL)
2497                 return NULL;
2498
2499         switch (nodeTag(from))
2500         {
2501
2502                         /*
2503                          * PLAN NODES
2504                          */
2505                 case T_Plan:
2506                         retval = _copyPlan(from);
2507                         break;
2508                 case T_Result:
2509                         retval = _copyResult(from);
2510                         break;
2511                 case T_Append:
2512                         retval = _copyAppend(from);
2513                         break;
2514                 case T_Scan:
2515                         retval = _copyScan(from);
2516                         break;
2517                 case T_SeqScan:
2518                         retval = _copySeqScan(from);
2519                         break;
2520                 case T_IndexScan:
2521                         retval = _copyIndexScan(from);
2522                         break;
2523                 case T_TidScan:
2524                         retval = _copyTidScan(from);
2525                         break;
2526                 case T_SubqueryScan:
2527                         retval = _copySubqueryScan(from);
2528                         break;
2529                 case T_Join:
2530                         retval = _copyJoin(from);
2531                         break;
2532                 case T_NestLoop:
2533                         retval = _copyNestLoop(from);
2534                         break;
2535                 case T_MergeJoin:
2536                         retval = _copyMergeJoin(from);
2537                         break;
2538                 case T_HashJoin:
2539                         retval = _copyHashJoin(from);
2540                         break;
2541                 case T_Material:
2542                         retval = _copyMaterial(from);
2543                         break;
2544                 case T_Sort:
2545                         retval = _copySort(from);
2546                         break;
2547                 case T_Group:
2548                         retval = _copyGroup(from);
2549                         break;
2550                 case T_Agg:
2551                         retval = _copyAgg(from);
2552                         break;
2553                 case T_Unique:
2554                         retval = _copyUnique(from);
2555                         break;
2556                 case T_Hash:
2557                         retval = _copyHash(from);
2558                         break;
2559                 case T_SubPlan:
2560                         retval = _copySubPlan(from);
2561                         break;
2562
2563                         /*
2564                          * PRIMITIVE NODES
2565                          */
2566                 case T_Resdom:
2567                         retval = _copyResdom(from);
2568                         break;
2569                 case T_Fjoin:
2570                         retval = _copyFjoin(from);
2571                         break;
2572                 case T_Expr:
2573                         retval = _copyExpr(from);
2574                         break;
2575                 case T_Var:
2576                         retval = _copyVar(from);
2577                         break;
2578                 case T_Oper:
2579                         retval = _copyOper(from);
2580                         break;
2581                 case T_Const:
2582                         retval = _copyConst(from);
2583                         break;
2584                 case T_Param:
2585                         retval = _copyParam(from);
2586                         break;
2587                 case T_Aggref:
2588                         retval = _copyAggref(from);
2589                         break;
2590                 case T_SubLink:
2591                         retval = _copySubLink(from);
2592                         break;
2593                 case T_Func:
2594                         retval = _copyFunc(from);
2595                         break;
2596                 case T_ArrayRef:
2597                         retval = _copyArrayRef(from);
2598                         break;
2599                 case T_Iter:
2600                         retval = _copyIter(from);
2601                         break;
2602                 case T_FieldSelect:
2603                         retval = _copyFieldSelect(from);
2604                         break;
2605                 case T_RelabelType:
2606                         retval = _copyRelabelType(from);
2607                         break;
2608                 case T_RangeTblRef:
2609                         retval = _copyRangeTblRef(from);
2610                         break;
2611                 case T_FromExpr:
2612                         retval = _copyFromExpr(from);
2613                         break;
2614                 case T_JoinExpr:
2615                         retval = _copyJoinExpr(from);
2616                         break;
2617
2618                         /*
2619                          * RELATION NODES
2620                          */
2621                 case T_RelOptInfo:
2622                         retval = _copyRelOptInfo(from);
2623                         break;
2624                 case T_Path:
2625                         retval = _copyPath(from);
2626                         break;
2627                 case T_IndexPath:
2628                         retval = _copyIndexPath(from);
2629                         break;
2630                 case T_TidPath:
2631                         retval = _copyTidPath(from);
2632                         break;
2633                 case T_NestPath:
2634                         retval = _copyNestPath(from);
2635                         break;
2636                 case T_MergePath:
2637                         retval = _copyMergePath(from);
2638                         break;
2639                 case T_HashPath:
2640                         retval = _copyHashPath(from);
2641                         break;
2642                 case T_PathKeyItem:
2643                         retval = _copyPathKeyItem(from);
2644                         break;
2645                 case T_RestrictInfo:
2646                         retval = _copyRestrictInfo(from);
2647                         break;
2648                 case T_JoinInfo:
2649                         retval = _copyJoinInfo(from);
2650                         break;
2651                 case T_Stream:
2652                         retval = _copyStream(from);
2653                         break;
2654                 case T_IndexOptInfo:
2655                         retval = _copyIndexOptInfo(from);
2656                         break;
2657
2658                         /*
2659                          * VALUE NODES
2660                          */
2661                 case T_Integer:
2662                 case T_Float:
2663                 case T_String:
2664                         retval = _copyValue(from);
2665                         break;
2666                 case T_List:
2667                         {
2668                                 List       *list = from,
2669                                                    *l,
2670                                                    *nl;
2671
2672                                 /* rather ugly coding for speed... */
2673                                 /* Note the input list cannot be NIL if we got here. */
2674                                 nl = lcons(copyObject(lfirst(list)), NIL);
2675                                 retval = nl;
2676
2677                                 foreach(l, lnext(list))
2678                                 {
2679                                         lnext(nl) = lcons(copyObject(lfirst(l)), NIL);
2680                                         nl = lnext(nl);
2681                                 }
2682                         }
2683                         break;
2684
2685                         /*
2686                          * PARSE NODES
2687                          */
2688                 case T_Query:
2689                         retval = _copyQuery(from);
2690                         break;
2691                 case T_InsertStmt:
2692                         retval = _copyInsertStmt(from);
2693                         break;
2694                 case T_DeleteStmt:
2695                         retval = _copyDeleteStmt(from);
2696                         break;
2697                 case T_UpdateStmt:
2698                         retval = _copyUpdateStmt(from);
2699                         break;
2700                 case T_SelectStmt:
2701                         retval = _copySelectStmt(from);
2702                         break;
2703                 case T_AlterTableStmt:
2704                         retval = _copyAlterTableStmt(from);
2705                         break;
2706                 case T_ChangeACLStmt:
2707                         retval = _copyChangeACLStmt(from);
2708                         break;
2709                 case T_ClosePortalStmt:
2710                         retval = _copyClosePortalStmt(from);
2711                         break;
2712                 case T_ClusterStmt:
2713                         retval = _copyClusterStmt(from);
2714                         break;
2715                 case T_CopyStmt:
2716                         retval = _copyCopyStmt(from);
2717                         break;
2718                 case T_CreateStmt:
2719                         retval = _copyCreateStmt(from);
2720                         break;
2721                 case T_VersionStmt:
2722                         retval = _copyVersionStmt(from);
2723                         break;
2724                 case T_DefineStmt:
2725                         retval = _copyDefineStmt(from);
2726                         break;
2727                 case T_DropStmt:
2728                         retval = _copyDropStmt(from);
2729                         break;
2730                 case T_TruncateStmt:
2731                         retval = _copyTruncateStmt(from);
2732                         break;
2733                 case T_CommentStmt:
2734                         retval = _copyCommentStmt(from);
2735                         break;
2736                 case T_ExtendStmt:
2737                         retval = _copyExtendStmt(from);
2738                         break;
2739                 case T_FetchStmt:
2740                         retval = _copyFetchStmt(from);
2741                         break;
2742                 case T_IndexStmt:
2743                         retval = _copyIndexStmt(from);
2744                         break;
2745                 case T_ProcedureStmt:
2746                         retval = _copyProcedureStmt(from);
2747                         break;
2748                 case T_RemoveAggrStmt:
2749                         retval = _copyRemoveAggrStmt(from);
2750                         break;
2751                 case T_RemoveFuncStmt:
2752                         retval = _copyRemoveFuncStmt(from);
2753                         break;
2754                 case T_RemoveOperStmt:
2755                         retval = _copyRemoveOperStmt(from);
2756                         break;
2757                 case T_RemoveStmt:
2758                         retval = _copyRemoveStmt(from);
2759                         break;
2760                 case T_RenameStmt:
2761                         retval = _copyRenameStmt(from);
2762                         break;
2763                 case T_RuleStmt:
2764                         retval = _copyRuleStmt(from);
2765                         break;
2766                 case T_NotifyStmt:
2767                         retval = _copyNotifyStmt(from);
2768                         break;
2769                 case T_ListenStmt:
2770                         retval = _copyListenStmt(from);
2771                         break;
2772                 case T_UnlistenStmt:
2773                         retval = _copyUnlistenStmt(from);
2774                         break;
2775                 case T_TransactionStmt:
2776                         retval = _copyTransactionStmt(from);
2777                         break;
2778                 case T_ViewStmt:
2779                         retval = _copyViewStmt(from);
2780                         break;
2781                 case T_LoadStmt:
2782                         retval = _copyLoadStmt(from);
2783                         break;
2784                 case T_CreatedbStmt:
2785                         retval = _copyCreatedbStmt(from);
2786                         break;
2787                 case T_DropdbStmt:
2788                         retval = _copyDropdbStmt(from);
2789                         break;
2790                 case T_VacuumStmt:
2791                         retval = _copyVacuumStmt(from);
2792                         break;
2793                 case T_ExplainStmt:
2794                         retval = _copyExplainStmt(from);
2795                         break;
2796                 case T_CreateSeqStmt:
2797                         retval = _copyCreateSeqStmt(from);
2798                         break;
2799                 case T_VariableSetStmt:
2800                         retval = _copyVariableSetStmt(from);
2801                         break;
2802                 case T_VariableShowStmt:
2803                         retval = _copyVariableShowStmt(from);
2804                         break;
2805                 case T_VariableResetStmt:
2806                         retval = _copyVariableResetStmt(from);
2807                         break;
2808                 case T_CreateTrigStmt:
2809                         retval = _copyCreateTrigStmt(from);
2810                         break;
2811                 case T_DropTrigStmt:
2812                         retval = _copyDropTrigStmt(from);
2813                         break;
2814                 case T_CreatePLangStmt:
2815                         retval = _copyCreatePLangStmt(from);
2816                         break;
2817                 case T_DropPLangStmt:
2818                         retval = _copyDropPLangStmt(from);
2819                         break;
2820                 case T_CreateUserStmt:
2821                         retval = _copyCreateUserStmt(from);
2822                         break;
2823                 case T_AlterUserStmt:
2824                         retval = _copyAlterUserStmt(from);
2825                         break;
2826                 case T_DropUserStmt:
2827                         retval = _copyDropUserStmt(from);
2828                         break;
2829                 case T_LockStmt:
2830                         retval = _copyLockStmt(from);
2831                         break;
2832                 case T_ConstraintsSetStmt:
2833                         retval = _copyConstraintsSetStmt(from);
2834                         break;
2835                 case T_CreateGroupStmt:
2836                         retval = _copyCreateGroupStmt(from);
2837                         break;
2838                 case T_AlterGroupStmt:
2839                         retval = _copyAlterGroupStmt(from);
2840                         break;
2841                 case T_DropGroupStmt:
2842                         retval = _copyDropGroupStmt(from);
2843                         break;
2844                 case T_ReindexStmt:
2845                         retval = _copyReindexStmt(from);
2846                         break;
2847                 case T_SetSessionStmt:
2848                         retval = _copySetSessionStmt(from);
2849                         break;
2850
2851                 case T_A_Expr:
2852                         retval = _copyAExpr(from);
2853                         break;
2854                 case T_Attr:
2855                         retval = _copyAttr(from);
2856                         break;
2857                 case T_A_Const:
2858                         retval = _copyAConst(from);
2859                         break;
2860                 case T_ParamNo:
2861                         retval = _copyParamNo(from);
2862                         break;
2863                 case T_Ident:
2864                         retval = _copyIdent(from);
2865                         break;
2866                 case T_FuncCall:
2867                         retval = _copyFuncCall(from);
2868                         break;
2869                 case T_A_Indices:
2870                         retval = _copyAIndices(from);
2871                         break;
2872                 case T_ResTarget:
2873                         retval = _copyResTarget(from);
2874                         break;
2875                 case T_TypeCast:
2876                         retval = _copyTypeCast(from);
2877                         break;
2878                 case T_SortGroupBy:
2879                         retval = _copySortGroupBy(from);
2880                         break;
2881                 case T_RangeVar:
2882                         retval = _copyRangeVar(from);
2883                         break;
2884                 case T_RangeSubselect:
2885                         retval = _copyRangeSubselect(from);
2886                         break;
2887                 case T_TypeName:
2888                         retval = _copyTypeName(from);
2889                         break;
2890                 case T_IndexElem:
2891                         retval = _copyIndexElem(from);
2892                         break;
2893                 case T_ColumnDef:
2894                         retval = _copyColumnDef(from);
2895                         break;
2896                 case T_Constraint:
2897                         retval = _copyConstraint(from);
2898                         break;
2899                 case T_DefElem:
2900                         retval = _copyDefElem(from);
2901                         break;
2902                 case T_TargetEntry:
2903                         retval = _copyTargetEntry(from);
2904                         break;
2905                 case T_RangeTblEntry:
2906                         retval = _copyRangeTblEntry(from);
2907                         break;
2908                 case T_SortClause:
2909                         retval = _copySortClause(from);
2910                         break;
2911                 case T_GroupClause:
2912                         retval = _copyGroupClause(from);
2913                         break;
2914                 case T_CaseExpr:
2915                         retval = _copyCaseExpr(from);
2916                         break;
2917                 case T_CaseWhen:
2918                         retval = _copyCaseWhen(from);
2919                         break;
2920                 case T_FkConstraint:
2921                         retval = _copyFkConstraint(from);
2922                         break;
2923
2924                 default:
2925                         elog(ERROR, "copyObject: don't know how to copy node type %d",
2926                                  nodeTag(from));
2927                         retval = from;          /* keep compiler quiet */
2928                         break;
2929         }
2930         return retval;
2931 }