]> granicus.if.org Git - postgresql/blob - src/backend/nodes/readfuncs.c
Allow aggregate functions to be VARIADIC.
[postgresql] / src / backend / nodes / readfuncs.c
1 /*-------------------------------------------------------------------------
2  *
3  * readfuncs.c
4  *        Reader functions for Postgres tree nodes.
5  *
6  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/nodes/readfuncs.c
12  *
13  * NOTES
14  *        Path and Plan nodes do not have any readfuncs support, because we
15  *        never have occasion to read them in.  (There was once code here that
16  *        claimed to read them, but it was broken as well as unused.)  We
17  *        never read executor state trees, either.
18  *
19  *        Parse location fields are written out by outfuncs.c, but only for
20  *        possible debugging use.  When reading a location field, we discard
21  *        the stored value and set the location field to -1 (ie, "unknown").
22  *        This is because nodes coming from a stored rule should not be thought
23  *        to have a known location in the current query's text.
24  *
25  *-------------------------------------------------------------------------
26  */
27 #include "postgres.h"
28
29 #include <math.h>
30
31 #include "nodes/parsenodes.h"
32 #include "nodes/readfuncs.h"
33
34
35 /*
36  * Macros to simplify reading of different kinds of fields.  Use these
37  * wherever possible to reduce the chance for silly typos.      Note that these
38  * hard-wire conventions about the names of the local variables in a Read
39  * routine.
40  */
41
42 /* Macros for declaring appropriate local variables */
43
44 /* A few guys need only local_node */
45 #define READ_LOCALS_NO_FIELDS(nodeTypeName) \
46         nodeTypeName *local_node = makeNode(nodeTypeName)
47
48 /* And a few guys need only the pg_strtok support fields */
49 #define READ_TEMP_LOCALS()      \
50         char       *token;              \
51         int                     length
52
53 /* ... but most need both */
54 #define READ_LOCALS(nodeTypeName)                       \
55         READ_LOCALS_NO_FIELDS(nodeTypeName);    \
56         READ_TEMP_LOCALS()
57
58 /* Read an integer field (anything written as ":fldname %d") */
59 #define READ_INT_FIELD(fldname) \
60         token = pg_strtok(&length);             /* skip :fldname */ \
61         token = pg_strtok(&length);             /* get field value */ \
62         local_node->fldname = atoi(token)
63
64 /* Read an unsigned integer field (anything written as ":fldname %u") */
65 #define READ_UINT_FIELD(fldname) \
66         token = pg_strtok(&length);             /* skip :fldname */ \
67         token = pg_strtok(&length);             /* get field value */ \
68         local_node->fldname = atoui(token)
69
70 /* Read an OID field (don't hard-wire assumption that OID is same as uint) */
71 #define READ_OID_FIELD(fldname) \
72         token = pg_strtok(&length);             /* skip :fldname */ \
73         token = pg_strtok(&length);             /* get field value */ \
74         local_node->fldname = atooid(token)
75
76 /* Read a char field (ie, one ascii character) */
77 #define READ_CHAR_FIELD(fldname) \
78         token = pg_strtok(&length);             /* skip :fldname */ \
79         token = pg_strtok(&length);             /* get field value */ \
80         local_node->fldname = token[0]
81
82 /* Read an enumerated-type field that was written as an integer code */
83 #define READ_ENUM_FIELD(fldname, enumtype) \
84         token = pg_strtok(&length);             /* skip :fldname */ \
85         token = pg_strtok(&length);             /* get field value */ \
86         local_node->fldname = (enumtype) atoi(token)
87
88 /* Read a float field */
89 #define READ_FLOAT_FIELD(fldname) \
90         token = pg_strtok(&length);             /* skip :fldname */ \
91         token = pg_strtok(&length);             /* get field value */ \
92         local_node->fldname = atof(token)
93
94 /* Read a boolean field */
95 #define READ_BOOL_FIELD(fldname) \
96         token = pg_strtok(&length);             /* skip :fldname */ \
97         token = pg_strtok(&length);             /* get field value */ \
98         local_node->fldname = strtobool(token)
99
100 /* Read a character-string field */
101 #define READ_STRING_FIELD(fldname) \
102         token = pg_strtok(&length);             /* skip :fldname */ \
103         token = pg_strtok(&length);             /* get field value */ \
104         local_node->fldname = nullable_string(token, length)
105
106 /* Read a parse location field (and throw away the value, per notes above) */
107 #define READ_LOCATION_FIELD(fldname) \
108         token = pg_strtok(&length);             /* skip :fldname */ \
109         token = pg_strtok(&length);             /* get field value */ \
110         local_node->fldname = -1        /* set field to "unknown" */
111
112 /* Read a Node field */
113 #define READ_NODE_FIELD(fldname) \
114         token = pg_strtok(&length);             /* skip :fldname */ \
115         (void) token;                           /* in case not used elsewhere */ \
116         local_node->fldname = nodeRead(NULL, 0)
117
118 /* Read a bitmapset field */
119 #define READ_BITMAPSET_FIELD(fldname) \
120         token = pg_strtok(&length);             /* skip :fldname */ \
121         local_node->fldname = _readBitmapset()
122
123 /* Routine exit */
124 #define READ_DONE() \
125         return local_node
126
127
128 /*
129  * NOTE: use atoi() to read values written with %d, or atoui() to read
130  * values written with %u in outfuncs.c.  An exception is OID values,
131  * for which use atooid().      (As of 7.1, outfuncs.c writes OIDs as %u,
132  * but this will probably change in the future.)
133  */
134 #define atoui(x)  ((unsigned int) strtoul((x), NULL, 10))
135
136 #define atooid(x)  ((Oid) strtoul((x), NULL, 10))
137
138 #define strtobool(x)  ((*(x) == 't') ? true : false)
139
140 #define nullable_string(token,length)  \
141         ((length) == 0 ? NULL : debackslash(token, length))
142
143
144 static Datum readDatum(bool typbyval);
145
146 /*
147  * _readBitmapset
148  */
149 static Bitmapset *
150 _readBitmapset(void)
151 {
152         Bitmapset  *result = NULL;
153
154         READ_TEMP_LOCALS();
155
156         token = pg_strtok(&length);
157         if (token == NULL)
158                 elog(ERROR, "incomplete Bitmapset structure");
159         if (length != 1 || token[0] != '(')
160                 elog(ERROR, "unrecognized token: \"%.*s\"", length, token);
161
162         token = pg_strtok(&length);
163         if (token == NULL)
164                 elog(ERROR, "incomplete Bitmapset structure");
165         if (length != 1 || token[0] != 'b')
166                 elog(ERROR, "unrecognized token: \"%.*s\"", length, token);
167
168         for (;;)
169         {
170                 int                     val;
171                 char       *endptr;
172
173                 token = pg_strtok(&length);
174                 if (token == NULL)
175                         elog(ERROR, "unterminated Bitmapset structure");
176                 if (length == 1 && token[0] == ')')
177                         break;
178                 val = (int) strtol(token, &endptr, 10);
179                 if (endptr != token + length)
180                         elog(ERROR, "unrecognized integer: \"%.*s\"", length, token);
181                 result = bms_add_member(result, val);
182         }
183
184         return result;
185 }
186
187
188 /*
189  * _readQuery
190  */
191 static Query *
192 _readQuery(void)
193 {
194         READ_LOCALS(Query);
195
196         READ_ENUM_FIELD(commandType, CmdType);
197         READ_ENUM_FIELD(querySource, QuerySource);
198         local_node->queryId = 0;        /* not saved in output format */
199         READ_BOOL_FIELD(canSetTag);
200         READ_NODE_FIELD(utilityStmt);
201         READ_INT_FIELD(resultRelation);
202         READ_BOOL_FIELD(hasAggs);
203         READ_BOOL_FIELD(hasWindowFuncs);
204         READ_BOOL_FIELD(hasSubLinks);
205         READ_BOOL_FIELD(hasDistinctOn);
206         READ_BOOL_FIELD(hasRecursive);
207         READ_BOOL_FIELD(hasModifyingCTE);
208         READ_BOOL_FIELD(hasForUpdate);
209         READ_NODE_FIELD(cteList);
210         READ_NODE_FIELD(rtable);
211         READ_NODE_FIELD(jointree);
212         READ_NODE_FIELD(targetList);
213         READ_NODE_FIELD(withCheckOptions);
214         READ_NODE_FIELD(returningList);
215         READ_NODE_FIELD(groupClause);
216         READ_NODE_FIELD(havingQual);
217         READ_NODE_FIELD(windowClause);
218         READ_NODE_FIELD(distinctClause);
219         READ_NODE_FIELD(sortClause);
220         READ_NODE_FIELD(limitOffset);
221         READ_NODE_FIELD(limitCount);
222         READ_NODE_FIELD(rowMarks);
223         READ_NODE_FIELD(setOperations);
224         READ_NODE_FIELD(constraintDeps);
225
226         READ_DONE();
227 }
228
229 /*
230  * _readNotifyStmt
231  */
232 static NotifyStmt *
233 _readNotifyStmt(void)
234 {
235         READ_LOCALS(NotifyStmt);
236
237         READ_STRING_FIELD(conditionname);
238         READ_STRING_FIELD(payload);
239
240         READ_DONE();
241 }
242
243 /*
244  * _readDeclareCursorStmt
245  */
246 static DeclareCursorStmt *
247 _readDeclareCursorStmt(void)
248 {
249         READ_LOCALS(DeclareCursorStmt);
250
251         READ_STRING_FIELD(portalname);
252         READ_INT_FIELD(options);
253         READ_NODE_FIELD(query);
254
255         READ_DONE();
256 }
257
258 /*
259  * _readWithCheckOption
260  */
261 static WithCheckOption *
262 _readWithCheckOption(void)
263 {
264         READ_LOCALS(WithCheckOption);
265
266         READ_STRING_FIELD(viewname);
267         READ_NODE_FIELD(qual);
268         READ_BOOL_FIELD(cascaded);
269
270         READ_DONE();
271 }
272
273 /*
274  * _readSortGroupClause
275  */
276 static SortGroupClause *
277 _readSortGroupClause(void)
278 {
279         READ_LOCALS(SortGroupClause);
280
281         READ_UINT_FIELD(tleSortGroupRef);
282         READ_OID_FIELD(eqop);
283         READ_OID_FIELD(sortop);
284         READ_BOOL_FIELD(nulls_first);
285         READ_BOOL_FIELD(hashable);
286
287         READ_DONE();
288 }
289
290 /*
291  * _readWindowClause
292  */
293 static WindowClause *
294 _readWindowClause(void)
295 {
296         READ_LOCALS(WindowClause);
297
298         READ_STRING_FIELD(name);
299         READ_STRING_FIELD(refname);
300         READ_NODE_FIELD(partitionClause);
301         READ_NODE_FIELD(orderClause);
302         READ_INT_FIELD(frameOptions);
303         READ_NODE_FIELD(startOffset);
304         READ_NODE_FIELD(endOffset);
305         READ_UINT_FIELD(winref);
306         READ_BOOL_FIELD(copiedOrder);
307
308         READ_DONE();
309 }
310
311 /*
312  * _readRowMarkClause
313  */
314 static RowMarkClause *
315 _readRowMarkClause(void)
316 {
317         READ_LOCALS(RowMarkClause);
318
319         READ_UINT_FIELD(rti);
320         READ_ENUM_FIELD(strength, LockClauseStrength);
321         READ_BOOL_FIELD(noWait);
322         READ_BOOL_FIELD(pushedDown);
323
324         READ_DONE();
325 }
326
327 /*
328  * _readCommonTableExpr
329  */
330 static CommonTableExpr *
331 _readCommonTableExpr(void)
332 {
333         READ_LOCALS(CommonTableExpr);
334
335         READ_STRING_FIELD(ctename);
336         READ_NODE_FIELD(aliascolnames);
337         READ_NODE_FIELD(ctequery);
338         READ_LOCATION_FIELD(location);
339         READ_BOOL_FIELD(cterecursive);
340         READ_INT_FIELD(cterefcount);
341         READ_NODE_FIELD(ctecolnames);
342         READ_NODE_FIELD(ctecoltypes);
343         READ_NODE_FIELD(ctecoltypmods);
344         READ_NODE_FIELD(ctecolcollations);
345
346         READ_DONE();
347 }
348
349 /*
350  * _readSetOperationStmt
351  */
352 static SetOperationStmt *
353 _readSetOperationStmt(void)
354 {
355         READ_LOCALS(SetOperationStmt);
356
357         READ_ENUM_FIELD(op, SetOperation);
358         READ_BOOL_FIELD(all);
359         READ_NODE_FIELD(larg);
360         READ_NODE_FIELD(rarg);
361         READ_NODE_FIELD(colTypes);
362         READ_NODE_FIELD(colTypmods);
363         READ_NODE_FIELD(colCollations);
364         READ_NODE_FIELD(groupClauses);
365
366         READ_DONE();
367 }
368
369
370 /*
371  *      Stuff from primnodes.h.
372  */
373
374 static Alias *
375 _readAlias(void)
376 {
377         READ_LOCALS(Alias);
378
379         READ_STRING_FIELD(aliasname);
380         READ_NODE_FIELD(colnames);
381
382         READ_DONE();
383 }
384
385 static RangeVar *
386 _readRangeVar(void)
387 {
388         READ_LOCALS(RangeVar);
389
390         local_node->catalogname = NULL;         /* not currently saved in output
391                                                                                  * format */
392
393         READ_STRING_FIELD(schemaname);
394         READ_STRING_FIELD(relname);
395         READ_ENUM_FIELD(inhOpt, InhOption);
396         READ_CHAR_FIELD(relpersistence);
397         READ_NODE_FIELD(alias);
398         READ_LOCATION_FIELD(location);
399
400         READ_DONE();
401 }
402
403 static IntoClause *
404 _readIntoClause(void)
405 {
406         READ_LOCALS(IntoClause);
407
408         READ_NODE_FIELD(rel);
409         READ_NODE_FIELD(colNames);
410         READ_NODE_FIELD(options);
411         READ_ENUM_FIELD(onCommit, OnCommitAction);
412         READ_STRING_FIELD(tableSpaceName);
413         READ_NODE_FIELD(viewQuery);
414         READ_BOOL_FIELD(skipData);
415
416         READ_DONE();
417 }
418
419 /*
420  * _readVar
421  */
422 static Var *
423 _readVar(void)
424 {
425         READ_LOCALS(Var);
426
427         READ_UINT_FIELD(varno);
428         READ_INT_FIELD(varattno);
429         READ_OID_FIELD(vartype);
430         READ_INT_FIELD(vartypmod);
431         READ_OID_FIELD(varcollid);
432         READ_UINT_FIELD(varlevelsup);
433         READ_UINT_FIELD(varnoold);
434         READ_INT_FIELD(varoattno);
435         READ_LOCATION_FIELD(location);
436
437         READ_DONE();
438 }
439
440 /*
441  * _readConst
442  */
443 static Const *
444 _readConst(void)
445 {
446         READ_LOCALS(Const);
447
448         READ_OID_FIELD(consttype);
449         READ_INT_FIELD(consttypmod);
450         READ_OID_FIELD(constcollid);
451         READ_INT_FIELD(constlen);
452         READ_BOOL_FIELD(constbyval);
453         READ_BOOL_FIELD(constisnull);
454         READ_LOCATION_FIELD(location);
455
456         token = pg_strtok(&length); /* skip :constvalue */
457         if (local_node->constisnull)
458                 token = pg_strtok(&length);             /* skip "<>" */
459         else
460                 local_node->constvalue = readDatum(local_node->constbyval);
461
462         READ_DONE();
463 }
464
465 /*
466  * _readParam
467  */
468 static Param *
469 _readParam(void)
470 {
471         READ_LOCALS(Param);
472
473         READ_ENUM_FIELD(paramkind, ParamKind);
474         READ_INT_FIELD(paramid);
475         READ_OID_FIELD(paramtype);
476         READ_INT_FIELD(paramtypmod);
477         READ_OID_FIELD(paramcollid);
478         READ_LOCATION_FIELD(location);
479
480         READ_DONE();
481 }
482
483 /*
484  * _readAggref
485  */
486 static Aggref *
487 _readAggref(void)
488 {
489         READ_LOCALS(Aggref);
490
491         READ_OID_FIELD(aggfnoid);
492         READ_OID_FIELD(aggtype);
493         READ_OID_FIELD(aggcollid);
494         READ_OID_FIELD(inputcollid);
495         READ_NODE_FIELD(args);
496         READ_NODE_FIELD(aggorder);
497         READ_NODE_FIELD(aggdistinct);
498         READ_NODE_FIELD(aggfilter);
499         READ_BOOL_FIELD(aggstar);
500         READ_BOOL_FIELD(aggvariadic);
501         READ_UINT_FIELD(agglevelsup);
502         READ_LOCATION_FIELD(location);
503
504         READ_DONE();
505 }
506
507 /*
508  * _readWindowFunc
509  */
510 static WindowFunc *
511 _readWindowFunc(void)
512 {
513         READ_LOCALS(WindowFunc);
514
515         READ_OID_FIELD(winfnoid);
516         READ_OID_FIELD(wintype);
517         READ_OID_FIELD(wincollid);
518         READ_OID_FIELD(inputcollid);
519         READ_NODE_FIELD(args);
520         READ_NODE_FIELD(aggfilter);
521         READ_UINT_FIELD(winref);
522         READ_BOOL_FIELD(winstar);
523         READ_BOOL_FIELD(winagg);
524         READ_LOCATION_FIELD(location);
525
526         READ_DONE();
527 }
528
529 /*
530  * _readArrayRef
531  */
532 static ArrayRef *
533 _readArrayRef(void)
534 {
535         READ_LOCALS(ArrayRef);
536
537         READ_OID_FIELD(refarraytype);
538         READ_OID_FIELD(refelemtype);
539         READ_INT_FIELD(reftypmod);
540         READ_OID_FIELD(refcollid);
541         READ_NODE_FIELD(refupperindexpr);
542         READ_NODE_FIELD(reflowerindexpr);
543         READ_NODE_FIELD(refexpr);
544         READ_NODE_FIELD(refassgnexpr);
545
546         READ_DONE();
547 }
548
549 /*
550  * _readFuncExpr
551  */
552 static FuncExpr *
553 _readFuncExpr(void)
554 {
555         READ_LOCALS(FuncExpr);
556
557         READ_OID_FIELD(funcid);
558         READ_OID_FIELD(funcresulttype);
559         READ_BOOL_FIELD(funcretset);
560         READ_BOOL_FIELD(funcvariadic);
561         READ_ENUM_FIELD(funcformat, CoercionForm);
562         READ_OID_FIELD(funccollid);
563         READ_OID_FIELD(inputcollid);
564         READ_NODE_FIELD(args);
565         READ_LOCATION_FIELD(location);
566
567         READ_DONE();
568 }
569
570 /*
571  * _readNamedArgExpr
572  */
573 static NamedArgExpr *
574 _readNamedArgExpr(void)
575 {
576         READ_LOCALS(NamedArgExpr);
577
578         READ_NODE_FIELD(arg);
579         READ_STRING_FIELD(name);
580         READ_INT_FIELD(argnumber);
581         READ_LOCATION_FIELD(location);
582
583         READ_DONE();
584 }
585
586 /*
587  * _readOpExpr
588  */
589 static OpExpr *
590 _readOpExpr(void)
591 {
592         READ_LOCALS(OpExpr);
593
594         READ_OID_FIELD(opno);
595         READ_OID_FIELD(opfuncid);
596
597         /*
598          * The opfuncid is stored in the textual format primarily for debugging
599          * and documentation reasons.  We want to always read it as zero to force
600          * it to be re-looked-up in the pg_operator entry.      This ensures that
601          * stored rules don't have hidden dependencies on operators' functions.
602          * (We don't currently support an ALTER OPERATOR command, but might
603          * someday.)
604          */
605         local_node->opfuncid = InvalidOid;
606
607         READ_OID_FIELD(opresulttype);
608         READ_BOOL_FIELD(opretset);
609         READ_OID_FIELD(opcollid);
610         READ_OID_FIELD(inputcollid);
611         READ_NODE_FIELD(args);
612         READ_LOCATION_FIELD(location);
613
614         READ_DONE();
615 }
616
617 /*
618  * _readDistinctExpr
619  */
620 static DistinctExpr *
621 _readDistinctExpr(void)
622 {
623         READ_LOCALS(DistinctExpr);
624
625         READ_OID_FIELD(opno);
626         READ_OID_FIELD(opfuncid);
627
628         /*
629          * The opfuncid is stored in the textual format primarily for debugging
630          * and documentation reasons.  We want to always read it as zero to force
631          * it to be re-looked-up in the pg_operator entry.      This ensures that
632          * stored rules don't have hidden dependencies on operators' functions.
633          * (We don't currently support an ALTER OPERATOR command, but might
634          * someday.)
635          */
636         local_node->opfuncid = InvalidOid;
637
638         READ_OID_FIELD(opresulttype);
639         READ_BOOL_FIELD(opretset);
640         READ_OID_FIELD(opcollid);
641         READ_OID_FIELD(inputcollid);
642         READ_NODE_FIELD(args);
643         READ_LOCATION_FIELD(location);
644
645         READ_DONE();
646 }
647
648 /*
649  * _readNullIfExpr
650  */
651 static NullIfExpr *
652 _readNullIfExpr(void)
653 {
654         READ_LOCALS(NullIfExpr);
655
656         READ_OID_FIELD(opno);
657         READ_OID_FIELD(opfuncid);
658
659         /*
660          * The opfuncid is stored in the textual format primarily for debugging
661          * and documentation reasons.  We want to always read it as zero to force
662          * it to be re-looked-up in the pg_operator entry.      This ensures that
663          * stored rules don't have hidden dependencies on operators' functions.
664          * (We don't currently support an ALTER OPERATOR command, but might
665          * someday.)
666          */
667         local_node->opfuncid = InvalidOid;
668
669         READ_OID_FIELD(opresulttype);
670         READ_BOOL_FIELD(opretset);
671         READ_OID_FIELD(opcollid);
672         READ_OID_FIELD(inputcollid);
673         READ_NODE_FIELD(args);
674         READ_LOCATION_FIELD(location);
675
676         READ_DONE();
677 }
678
679 /*
680  * _readScalarArrayOpExpr
681  */
682 static ScalarArrayOpExpr *
683 _readScalarArrayOpExpr(void)
684 {
685         READ_LOCALS(ScalarArrayOpExpr);
686
687         READ_OID_FIELD(opno);
688         READ_OID_FIELD(opfuncid);
689
690         /*
691          * The opfuncid is stored in the textual format primarily for debugging
692          * and documentation reasons.  We want to always read it as zero to force
693          * it to be re-looked-up in the pg_operator entry.      This ensures that
694          * stored rules don't have hidden dependencies on operators' functions.
695          * (We don't currently support an ALTER OPERATOR command, but might
696          * someday.)
697          */
698         local_node->opfuncid = InvalidOid;
699
700         READ_BOOL_FIELD(useOr);
701         READ_OID_FIELD(inputcollid);
702         READ_NODE_FIELD(args);
703         READ_LOCATION_FIELD(location);
704
705         READ_DONE();
706 }
707
708 /*
709  * _readBoolExpr
710  */
711 static BoolExpr *
712 _readBoolExpr(void)
713 {
714         READ_LOCALS(BoolExpr);
715
716         /* do-it-yourself enum representation */
717         token = pg_strtok(&length); /* skip :boolop */
718         token = pg_strtok(&length); /* get field value */
719         if (strncmp(token, "and", 3) == 0)
720                 local_node->boolop = AND_EXPR;
721         else if (strncmp(token, "or", 2) == 0)
722                 local_node->boolop = OR_EXPR;
723         else if (strncmp(token, "not", 3) == 0)
724                 local_node->boolop = NOT_EXPR;
725         else
726                 elog(ERROR, "unrecognized boolop \"%.*s\"", length, token);
727
728         READ_NODE_FIELD(args);
729         READ_LOCATION_FIELD(location);
730
731         READ_DONE();
732 }
733
734 /*
735  * _readSubLink
736  */
737 static SubLink *
738 _readSubLink(void)
739 {
740         READ_LOCALS(SubLink);
741
742         READ_ENUM_FIELD(subLinkType, SubLinkType);
743         READ_NODE_FIELD(testexpr);
744         READ_NODE_FIELD(operName);
745         READ_NODE_FIELD(subselect);
746         READ_LOCATION_FIELD(location);
747
748         READ_DONE();
749 }
750
751 /*
752  * _readSubPlan is not needed since it doesn't appear in stored rules.
753  */
754
755 /*
756  * _readFieldSelect
757  */
758 static FieldSelect *
759 _readFieldSelect(void)
760 {
761         READ_LOCALS(FieldSelect);
762
763         READ_NODE_FIELD(arg);
764         READ_INT_FIELD(fieldnum);
765         READ_OID_FIELD(resulttype);
766         READ_INT_FIELD(resulttypmod);
767         READ_OID_FIELD(resultcollid);
768
769         READ_DONE();
770 }
771
772 /*
773  * _readFieldStore
774  */
775 static FieldStore *
776 _readFieldStore(void)
777 {
778         READ_LOCALS(FieldStore);
779
780         READ_NODE_FIELD(arg);
781         READ_NODE_FIELD(newvals);
782         READ_NODE_FIELD(fieldnums);
783         READ_OID_FIELD(resulttype);
784
785         READ_DONE();
786 }
787
788 /*
789  * _readRelabelType
790  */
791 static RelabelType *
792 _readRelabelType(void)
793 {
794         READ_LOCALS(RelabelType);
795
796         READ_NODE_FIELD(arg);
797         READ_OID_FIELD(resulttype);
798         READ_INT_FIELD(resulttypmod);
799         READ_OID_FIELD(resultcollid);
800         READ_ENUM_FIELD(relabelformat, CoercionForm);
801         READ_LOCATION_FIELD(location);
802
803         READ_DONE();
804 }
805
806 /*
807  * _readCoerceViaIO
808  */
809 static CoerceViaIO *
810 _readCoerceViaIO(void)
811 {
812         READ_LOCALS(CoerceViaIO);
813
814         READ_NODE_FIELD(arg);
815         READ_OID_FIELD(resulttype);
816         READ_OID_FIELD(resultcollid);
817         READ_ENUM_FIELD(coerceformat, CoercionForm);
818         READ_LOCATION_FIELD(location);
819
820         READ_DONE();
821 }
822
823 /*
824  * _readArrayCoerceExpr
825  */
826 static ArrayCoerceExpr *
827 _readArrayCoerceExpr(void)
828 {
829         READ_LOCALS(ArrayCoerceExpr);
830
831         READ_NODE_FIELD(arg);
832         READ_OID_FIELD(elemfuncid);
833         READ_OID_FIELD(resulttype);
834         READ_INT_FIELD(resulttypmod);
835         READ_OID_FIELD(resultcollid);
836         READ_BOOL_FIELD(isExplicit);
837         READ_ENUM_FIELD(coerceformat, CoercionForm);
838         READ_LOCATION_FIELD(location);
839
840         READ_DONE();
841 }
842
843 /*
844  * _readConvertRowtypeExpr
845  */
846 static ConvertRowtypeExpr *
847 _readConvertRowtypeExpr(void)
848 {
849         READ_LOCALS(ConvertRowtypeExpr);
850
851         READ_NODE_FIELD(arg);
852         READ_OID_FIELD(resulttype);
853         READ_ENUM_FIELD(convertformat, CoercionForm);
854         READ_LOCATION_FIELD(location);
855
856         READ_DONE();
857 }
858
859 /*
860  * _readCollateExpr
861  */
862 static CollateExpr *
863 _readCollateExpr(void)
864 {
865         READ_LOCALS(CollateExpr);
866
867         READ_NODE_FIELD(arg);
868         READ_OID_FIELD(collOid);
869         READ_LOCATION_FIELD(location);
870
871         READ_DONE();
872 }
873
874 /*
875  * _readCaseExpr
876  */
877 static CaseExpr *
878 _readCaseExpr(void)
879 {
880         READ_LOCALS(CaseExpr);
881
882         READ_OID_FIELD(casetype);
883         READ_OID_FIELD(casecollid);
884         READ_NODE_FIELD(arg);
885         READ_NODE_FIELD(args);
886         READ_NODE_FIELD(defresult);
887         READ_LOCATION_FIELD(location);
888
889         READ_DONE();
890 }
891
892 /*
893  * _readCaseWhen
894  */
895 static CaseWhen *
896 _readCaseWhen(void)
897 {
898         READ_LOCALS(CaseWhen);
899
900         READ_NODE_FIELD(expr);
901         READ_NODE_FIELD(result);
902         READ_LOCATION_FIELD(location);
903
904         READ_DONE();
905 }
906
907 /*
908  * _readCaseTestExpr
909  */
910 static CaseTestExpr *
911 _readCaseTestExpr(void)
912 {
913         READ_LOCALS(CaseTestExpr);
914
915         READ_OID_FIELD(typeId);
916         READ_INT_FIELD(typeMod);
917         READ_OID_FIELD(collation);
918
919         READ_DONE();
920 }
921
922 /*
923  * _readArrayExpr
924  */
925 static ArrayExpr *
926 _readArrayExpr(void)
927 {
928         READ_LOCALS(ArrayExpr);
929
930         READ_OID_FIELD(array_typeid);
931         READ_OID_FIELD(array_collid);
932         READ_OID_FIELD(element_typeid);
933         READ_NODE_FIELD(elements);
934         READ_BOOL_FIELD(multidims);
935         READ_LOCATION_FIELD(location);
936
937         READ_DONE();
938 }
939
940 /*
941  * _readRowExpr
942  */
943 static RowExpr *
944 _readRowExpr(void)
945 {
946         READ_LOCALS(RowExpr);
947
948         READ_NODE_FIELD(args);
949         READ_OID_FIELD(row_typeid);
950         READ_ENUM_FIELD(row_format, CoercionForm);
951         READ_NODE_FIELD(colnames);
952         READ_LOCATION_FIELD(location);
953
954         READ_DONE();
955 }
956
957 /*
958  * _readRowCompareExpr
959  */
960 static RowCompareExpr *
961 _readRowCompareExpr(void)
962 {
963         READ_LOCALS(RowCompareExpr);
964
965         READ_ENUM_FIELD(rctype, RowCompareType);
966         READ_NODE_FIELD(opnos);
967         READ_NODE_FIELD(opfamilies);
968         READ_NODE_FIELD(inputcollids);
969         READ_NODE_FIELD(largs);
970         READ_NODE_FIELD(rargs);
971
972         READ_DONE();
973 }
974
975 /*
976  * _readCoalesceExpr
977  */
978 static CoalesceExpr *
979 _readCoalesceExpr(void)
980 {
981         READ_LOCALS(CoalesceExpr);
982
983         READ_OID_FIELD(coalescetype);
984         READ_OID_FIELD(coalescecollid);
985         READ_NODE_FIELD(args);
986         READ_LOCATION_FIELD(location);
987
988         READ_DONE();
989 }
990
991 /*
992  * _readMinMaxExpr
993  */
994 static MinMaxExpr *
995 _readMinMaxExpr(void)
996 {
997         READ_LOCALS(MinMaxExpr);
998
999         READ_OID_FIELD(minmaxtype);
1000         READ_OID_FIELD(minmaxcollid);
1001         READ_OID_FIELD(inputcollid);
1002         READ_ENUM_FIELD(op, MinMaxOp);
1003         READ_NODE_FIELD(args);
1004         READ_LOCATION_FIELD(location);
1005
1006         READ_DONE();
1007 }
1008
1009 /*
1010  * _readXmlExpr
1011  */
1012 static XmlExpr *
1013 _readXmlExpr(void)
1014 {
1015         READ_LOCALS(XmlExpr);
1016
1017         READ_ENUM_FIELD(op, XmlExprOp);
1018         READ_STRING_FIELD(name);
1019         READ_NODE_FIELD(named_args);
1020         READ_NODE_FIELD(arg_names);
1021         READ_NODE_FIELD(args);
1022         READ_ENUM_FIELD(xmloption, XmlOptionType);
1023         READ_OID_FIELD(type);
1024         READ_INT_FIELD(typmod);
1025         READ_LOCATION_FIELD(location);
1026
1027         READ_DONE();
1028 }
1029
1030 /*
1031  * _readNullTest
1032  */
1033 static NullTest *
1034 _readNullTest(void)
1035 {
1036         READ_LOCALS(NullTest);
1037
1038         READ_NODE_FIELD(arg);
1039         READ_ENUM_FIELD(nulltesttype, NullTestType);
1040         READ_BOOL_FIELD(argisrow);
1041
1042         READ_DONE();
1043 }
1044
1045 /*
1046  * _readBooleanTest
1047  */
1048 static BooleanTest *
1049 _readBooleanTest(void)
1050 {
1051         READ_LOCALS(BooleanTest);
1052
1053         READ_NODE_FIELD(arg);
1054         READ_ENUM_FIELD(booltesttype, BoolTestType);
1055
1056         READ_DONE();
1057 }
1058
1059 /*
1060  * _readCoerceToDomain
1061  */
1062 static CoerceToDomain *
1063 _readCoerceToDomain(void)
1064 {
1065         READ_LOCALS(CoerceToDomain);
1066
1067         READ_NODE_FIELD(arg);
1068         READ_OID_FIELD(resulttype);
1069         READ_INT_FIELD(resulttypmod);
1070         READ_OID_FIELD(resultcollid);
1071         READ_ENUM_FIELD(coercionformat, CoercionForm);
1072         READ_LOCATION_FIELD(location);
1073
1074         READ_DONE();
1075 }
1076
1077 /*
1078  * _readCoerceToDomainValue
1079  */
1080 static CoerceToDomainValue *
1081 _readCoerceToDomainValue(void)
1082 {
1083         READ_LOCALS(CoerceToDomainValue);
1084
1085         READ_OID_FIELD(typeId);
1086         READ_INT_FIELD(typeMod);
1087         READ_OID_FIELD(collation);
1088         READ_LOCATION_FIELD(location);
1089
1090         READ_DONE();
1091 }
1092
1093 /*
1094  * _readSetToDefault
1095  */
1096 static SetToDefault *
1097 _readSetToDefault(void)
1098 {
1099         READ_LOCALS(SetToDefault);
1100
1101         READ_OID_FIELD(typeId);
1102         READ_INT_FIELD(typeMod);
1103         READ_OID_FIELD(collation);
1104         READ_LOCATION_FIELD(location);
1105
1106         READ_DONE();
1107 }
1108
1109 /*
1110  * _readCurrentOfExpr
1111  */
1112 static CurrentOfExpr *
1113 _readCurrentOfExpr(void)
1114 {
1115         READ_LOCALS(CurrentOfExpr);
1116
1117         READ_UINT_FIELD(cvarno);
1118         READ_STRING_FIELD(cursor_name);
1119         READ_INT_FIELD(cursor_param);
1120
1121         READ_DONE();
1122 }
1123
1124 /*
1125  * _readTargetEntry
1126  */
1127 static TargetEntry *
1128 _readTargetEntry(void)
1129 {
1130         READ_LOCALS(TargetEntry);
1131
1132         READ_NODE_FIELD(expr);
1133         READ_INT_FIELD(resno);
1134         READ_STRING_FIELD(resname);
1135         READ_UINT_FIELD(ressortgroupref);
1136         READ_OID_FIELD(resorigtbl);
1137         READ_INT_FIELD(resorigcol);
1138         READ_BOOL_FIELD(resjunk);
1139
1140         READ_DONE();
1141 }
1142
1143 /*
1144  * _readRangeTblRef
1145  */
1146 static RangeTblRef *
1147 _readRangeTblRef(void)
1148 {
1149         READ_LOCALS(RangeTblRef);
1150
1151         READ_INT_FIELD(rtindex);
1152
1153         READ_DONE();
1154 }
1155
1156 /*
1157  * _readJoinExpr
1158  */
1159 static JoinExpr *
1160 _readJoinExpr(void)
1161 {
1162         READ_LOCALS(JoinExpr);
1163
1164         READ_ENUM_FIELD(jointype, JoinType);
1165         READ_BOOL_FIELD(isNatural);
1166         READ_NODE_FIELD(larg);
1167         READ_NODE_FIELD(rarg);
1168         READ_NODE_FIELD(usingClause);
1169         READ_NODE_FIELD(quals);
1170         READ_NODE_FIELD(alias);
1171         READ_INT_FIELD(rtindex);
1172
1173         READ_DONE();
1174 }
1175
1176 /*
1177  * _readFromExpr
1178  */
1179 static FromExpr *
1180 _readFromExpr(void)
1181 {
1182         READ_LOCALS(FromExpr);
1183
1184         READ_NODE_FIELD(fromlist);
1185         READ_NODE_FIELD(quals);
1186
1187         READ_DONE();
1188 }
1189
1190
1191 /*
1192  *      Stuff from parsenodes.h.
1193  */
1194
1195 /*
1196  * _readRangeTblEntry
1197  */
1198 static RangeTblEntry *
1199 _readRangeTblEntry(void)
1200 {
1201         READ_LOCALS(RangeTblEntry);
1202
1203         /* put alias + eref first to make dump more legible */
1204         READ_NODE_FIELD(alias);
1205         READ_NODE_FIELD(eref);
1206         READ_ENUM_FIELD(rtekind, RTEKind);
1207
1208         switch (local_node->rtekind)
1209         {
1210                 case RTE_RELATION:
1211                         READ_OID_FIELD(relid);
1212                         READ_CHAR_FIELD(relkind);
1213                         break;
1214                 case RTE_SUBQUERY:
1215                         READ_NODE_FIELD(subquery);
1216                         READ_BOOL_FIELD(security_barrier);
1217                         break;
1218                 case RTE_JOIN:
1219                         READ_ENUM_FIELD(jointype, JoinType);
1220                         READ_NODE_FIELD(joinaliasvars);
1221                         break;
1222                 case RTE_FUNCTION:
1223                         READ_NODE_FIELD(funcexpr);
1224                         READ_NODE_FIELD(funccoltypes);
1225                         READ_NODE_FIELD(funccoltypmods);
1226                         READ_NODE_FIELD(funccolcollations);
1227                         READ_BOOL_FIELD(funcordinality);
1228                         break;
1229                 case RTE_VALUES:
1230                         READ_NODE_FIELD(values_lists);
1231                         READ_NODE_FIELD(values_collations);
1232                         break;
1233                 case RTE_CTE:
1234                         READ_STRING_FIELD(ctename);
1235                         READ_UINT_FIELD(ctelevelsup);
1236                         READ_BOOL_FIELD(self_reference);
1237                         READ_NODE_FIELD(ctecoltypes);
1238                         READ_NODE_FIELD(ctecoltypmods);
1239                         READ_NODE_FIELD(ctecolcollations);
1240                         break;
1241                 default:
1242                         elog(ERROR, "unrecognized RTE kind: %d",
1243                                  (int) local_node->rtekind);
1244                         break;
1245         }
1246
1247         READ_BOOL_FIELD(lateral);
1248         READ_BOOL_FIELD(inh);
1249         READ_BOOL_FIELD(inFromCl);
1250         READ_UINT_FIELD(requiredPerms);
1251         READ_OID_FIELD(checkAsUser);
1252         READ_BITMAPSET_FIELD(selectedCols);
1253         READ_BITMAPSET_FIELD(modifiedCols);
1254
1255         READ_DONE();
1256 }
1257
1258
1259 /*
1260  * parseNodeString
1261  *
1262  * Given a character string representing a node tree, parseNodeString creates
1263  * the internal node structure.
1264  *
1265  * The string to be read must already have been loaded into pg_strtok().
1266  */
1267 Node *
1268 parseNodeString(void)
1269 {
1270         void       *return_value;
1271
1272         READ_TEMP_LOCALS();
1273
1274         token = pg_strtok(&length);
1275
1276 #define MATCH(tokname, namelen) \
1277         (length == namelen && memcmp(token, tokname, namelen) == 0)
1278
1279         if (MATCH("QUERY", 5))
1280                 return_value = _readQuery();
1281         else if (MATCH("WITHCHECKOPTION", 15))
1282                 return_value = _readWithCheckOption();
1283         else if (MATCH("SORTGROUPCLAUSE", 15))
1284                 return_value = _readSortGroupClause();
1285         else if (MATCH("WINDOWCLAUSE", 12))
1286                 return_value = _readWindowClause();
1287         else if (MATCH("ROWMARKCLAUSE", 13))
1288                 return_value = _readRowMarkClause();
1289         else if (MATCH("COMMONTABLEEXPR", 15))
1290                 return_value = _readCommonTableExpr();
1291         else if (MATCH("SETOPERATIONSTMT", 16))
1292                 return_value = _readSetOperationStmt();
1293         else if (MATCH("ALIAS", 5))
1294                 return_value = _readAlias();
1295         else if (MATCH("RANGEVAR", 8))
1296                 return_value = _readRangeVar();
1297         else if (MATCH("INTOCLAUSE", 10))
1298                 return_value = _readIntoClause();
1299         else if (MATCH("VAR", 3))
1300                 return_value = _readVar();
1301         else if (MATCH("CONST", 5))
1302                 return_value = _readConst();
1303         else if (MATCH("PARAM", 5))
1304                 return_value = _readParam();
1305         else if (MATCH("AGGREF", 6))
1306                 return_value = _readAggref();
1307         else if (MATCH("WINDOWFUNC", 10))
1308                 return_value = _readWindowFunc();
1309         else if (MATCH("ARRAYREF", 8))
1310                 return_value = _readArrayRef();
1311         else if (MATCH("FUNCEXPR", 8))
1312                 return_value = _readFuncExpr();
1313         else if (MATCH("NAMEDARGEXPR", 12))
1314                 return_value = _readNamedArgExpr();
1315         else if (MATCH("OPEXPR", 6))
1316                 return_value = _readOpExpr();
1317         else if (MATCH("DISTINCTEXPR", 12))
1318                 return_value = _readDistinctExpr();
1319         else if (MATCH("NULLIFEXPR", 10))
1320                 return_value = _readNullIfExpr();
1321         else if (MATCH("SCALARARRAYOPEXPR", 17))
1322                 return_value = _readScalarArrayOpExpr();
1323         else if (MATCH("BOOLEXPR", 8))
1324                 return_value = _readBoolExpr();
1325         else if (MATCH("SUBLINK", 7))
1326                 return_value = _readSubLink();
1327         else if (MATCH("FIELDSELECT", 11))
1328                 return_value = _readFieldSelect();
1329         else if (MATCH("FIELDSTORE", 10))
1330                 return_value = _readFieldStore();
1331         else if (MATCH("RELABELTYPE", 11))
1332                 return_value = _readRelabelType();
1333         else if (MATCH("COERCEVIAIO", 11))
1334                 return_value = _readCoerceViaIO();
1335         else if (MATCH("ARRAYCOERCEEXPR", 15))
1336                 return_value = _readArrayCoerceExpr();
1337         else if (MATCH("CONVERTROWTYPEEXPR", 18))
1338                 return_value = _readConvertRowtypeExpr();
1339         else if (MATCH("COLLATE", 7))
1340                 return_value = _readCollateExpr();
1341         else if (MATCH("CASE", 4))
1342                 return_value = _readCaseExpr();
1343         else if (MATCH("WHEN", 4))
1344                 return_value = _readCaseWhen();
1345         else if (MATCH("CASETESTEXPR", 12))
1346                 return_value = _readCaseTestExpr();
1347         else if (MATCH("ARRAY", 5))
1348                 return_value = _readArrayExpr();
1349         else if (MATCH("ROW", 3))
1350                 return_value = _readRowExpr();
1351         else if (MATCH("ROWCOMPARE", 10))
1352                 return_value = _readRowCompareExpr();
1353         else if (MATCH("COALESCE", 8))
1354                 return_value = _readCoalesceExpr();
1355         else if (MATCH("MINMAX", 6))
1356                 return_value = _readMinMaxExpr();
1357         else if (MATCH("XMLEXPR", 7))
1358                 return_value = _readXmlExpr();
1359         else if (MATCH("NULLTEST", 8))
1360                 return_value = _readNullTest();
1361         else if (MATCH("BOOLEANTEST", 11))
1362                 return_value = _readBooleanTest();
1363         else if (MATCH("COERCETODOMAIN", 14))
1364                 return_value = _readCoerceToDomain();
1365         else if (MATCH("COERCETODOMAINVALUE", 19))
1366                 return_value = _readCoerceToDomainValue();
1367         else if (MATCH("SETTODEFAULT", 12))
1368                 return_value = _readSetToDefault();
1369         else if (MATCH("CURRENTOFEXPR", 13))
1370                 return_value = _readCurrentOfExpr();
1371         else if (MATCH("TARGETENTRY", 11))
1372                 return_value = _readTargetEntry();
1373         else if (MATCH("RANGETBLREF", 11))
1374                 return_value = _readRangeTblRef();
1375         else if (MATCH("JOINEXPR", 8))
1376                 return_value = _readJoinExpr();
1377         else if (MATCH("FROMEXPR", 8))
1378                 return_value = _readFromExpr();
1379         else if (MATCH("RTE", 3))
1380                 return_value = _readRangeTblEntry();
1381         else if (MATCH("NOTIFY", 6))
1382                 return_value = _readNotifyStmt();
1383         else if (MATCH("DECLARECURSOR", 13))
1384                 return_value = _readDeclareCursorStmt();
1385         else
1386         {
1387                 elog(ERROR, "badly formatted node string \"%.32s\"...", token);
1388                 return_value = NULL;    /* keep compiler quiet */
1389         }
1390
1391         return (Node *) return_value;
1392 }
1393
1394
1395 /*
1396  * readDatum
1397  *
1398  * Given a string representation of a constant, recreate the appropriate
1399  * Datum.  The string representation embeds length info, but not byValue,
1400  * so we must be told that.
1401  */
1402 static Datum
1403 readDatum(bool typbyval)
1404 {
1405         Size            length,
1406                                 i;
1407         int                     tokenLength;
1408         char       *token;
1409         Datum           res;
1410         char       *s;
1411
1412         /*
1413          * read the actual length of the value
1414          */
1415         token = pg_strtok(&tokenLength);
1416         length = atoui(token);
1417
1418         token = pg_strtok(&tokenLength);        /* read the '[' */
1419         if (token == NULL || token[0] != '[')
1420                 elog(ERROR, "expected \"[\" to start datum, but got \"%s\"; length = %lu",
1421                          token ? (const char *) token : "[NULL]",
1422                          (unsigned long) length);
1423
1424         if (typbyval)
1425         {
1426                 if (length > (Size) sizeof(Datum))
1427                         elog(ERROR, "byval datum but length = %lu",
1428                                  (unsigned long) length);
1429                 res = (Datum) 0;
1430                 s = (char *) (&res);
1431                 for (i = 0; i < (Size) sizeof(Datum); i++)
1432                 {
1433                         token = pg_strtok(&tokenLength);
1434                         s[i] = (char) atoi(token);
1435                 }
1436         }
1437         else if (length <= 0)
1438                 res = (Datum) NULL;
1439         else
1440         {
1441                 s = (char *) palloc(length);
1442                 for (i = 0; i < length; i++)
1443                 {
1444                         token = pg_strtok(&tokenLength);
1445                         s[i] = (char) atoi(token);
1446                 }
1447                 res = PointerGetDatum(s);
1448         }
1449
1450         token = pg_strtok(&tokenLength);        /* read the ']' */
1451         if (token == NULL || token[0] != ']')
1452                 elog(ERROR, "expected \"]\" to end datum, but got \"%s\"; length = %lu",
1453                          token ? (const char *) token : "[NULL]",
1454                          (unsigned long) length);
1455
1456         return res;
1457 }