]> granicus.if.org Git - postgresql/blob - src/backend/nodes/readfuncs.c
Support ordered-set (WITHIN GROUP) aggregates.
[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(aggdirectargs);
496         READ_NODE_FIELD(args);
497         READ_NODE_FIELD(aggorder);
498         READ_NODE_FIELD(aggdistinct);
499         READ_NODE_FIELD(aggfilter);
500         READ_BOOL_FIELD(aggstar);
501         READ_BOOL_FIELD(aggvariadic);
502         READ_CHAR_FIELD(aggkind);
503         READ_UINT_FIELD(agglevelsup);
504         READ_LOCATION_FIELD(location);
505
506         READ_DONE();
507 }
508
509 /*
510  * _readWindowFunc
511  */
512 static WindowFunc *
513 _readWindowFunc(void)
514 {
515         READ_LOCALS(WindowFunc);
516
517         READ_OID_FIELD(winfnoid);
518         READ_OID_FIELD(wintype);
519         READ_OID_FIELD(wincollid);
520         READ_OID_FIELD(inputcollid);
521         READ_NODE_FIELD(args);
522         READ_NODE_FIELD(aggfilter);
523         READ_UINT_FIELD(winref);
524         READ_BOOL_FIELD(winstar);
525         READ_BOOL_FIELD(winagg);
526         READ_LOCATION_FIELD(location);
527
528         READ_DONE();
529 }
530
531 /*
532  * _readArrayRef
533  */
534 static ArrayRef *
535 _readArrayRef(void)
536 {
537         READ_LOCALS(ArrayRef);
538
539         READ_OID_FIELD(refarraytype);
540         READ_OID_FIELD(refelemtype);
541         READ_INT_FIELD(reftypmod);
542         READ_OID_FIELD(refcollid);
543         READ_NODE_FIELD(refupperindexpr);
544         READ_NODE_FIELD(reflowerindexpr);
545         READ_NODE_FIELD(refexpr);
546         READ_NODE_FIELD(refassgnexpr);
547
548         READ_DONE();
549 }
550
551 /*
552  * _readFuncExpr
553  */
554 static FuncExpr *
555 _readFuncExpr(void)
556 {
557         READ_LOCALS(FuncExpr);
558
559         READ_OID_FIELD(funcid);
560         READ_OID_FIELD(funcresulttype);
561         READ_BOOL_FIELD(funcretset);
562         READ_BOOL_FIELD(funcvariadic);
563         READ_ENUM_FIELD(funcformat, CoercionForm);
564         READ_OID_FIELD(funccollid);
565         READ_OID_FIELD(inputcollid);
566         READ_NODE_FIELD(args);
567         READ_LOCATION_FIELD(location);
568
569         READ_DONE();
570 }
571
572 /*
573  * _readNamedArgExpr
574  */
575 static NamedArgExpr *
576 _readNamedArgExpr(void)
577 {
578         READ_LOCALS(NamedArgExpr);
579
580         READ_NODE_FIELD(arg);
581         READ_STRING_FIELD(name);
582         READ_INT_FIELD(argnumber);
583         READ_LOCATION_FIELD(location);
584
585         READ_DONE();
586 }
587
588 /*
589  * _readOpExpr
590  */
591 static OpExpr *
592 _readOpExpr(void)
593 {
594         READ_LOCALS(OpExpr);
595
596         READ_OID_FIELD(opno);
597         READ_OID_FIELD(opfuncid);
598
599         /*
600          * The opfuncid is stored in the textual format primarily for debugging
601          * and documentation reasons.  We want to always read it as zero to force
602          * it to be re-looked-up in the pg_operator entry.      This ensures that
603          * stored rules don't have hidden dependencies on operators' functions.
604          * (We don't currently support an ALTER OPERATOR command, but might
605          * someday.)
606          */
607         local_node->opfuncid = InvalidOid;
608
609         READ_OID_FIELD(opresulttype);
610         READ_BOOL_FIELD(opretset);
611         READ_OID_FIELD(opcollid);
612         READ_OID_FIELD(inputcollid);
613         READ_NODE_FIELD(args);
614         READ_LOCATION_FIELD(location);
615
616         READ_DONE();
617 }
618
619 /*
620  * _readDistinctExpr
621  */
622 static DistinctExpr *
623 _readDistinctExpr(void)
624 {
625         READ_LOCALS(DistinctExpr);
626
627         READ_OID_FIELD(opno);
628         READ_OID_FIELD(opfuncid);
629
630         /*
631          * The opfuncid is stored in the textual format primarily for debugging
632          * and documentation reasons.  We want to always read it as zero to force
633          * it to be re-looked-up in the pg_operator entry.      This ensures that
634          * stored rules don't have hidden dependencies on operators' functions.
635          * (We don't currently support an ALTER OPERATOR command, but might
636          * someday.)
637          */
638         local_node->opfuncid = InvalidOid;
639
640         READ_OID_FIELD(opresulttype);
641         READ_BOOL_FIELD(opretset);
642         READ_OID_FIELD(opcollid);
643         READ_OID_FIELD(inputcollid);
644         READ_NODE_FIELD(args);
645         READ_LOCATION_FIELD(location);
646
647         READ_DONE();
648 }
649
650 /*
651  * _readNullIfExpr
652  */
653 static NullIfExpr *
654 _readNullIfExpr(void)
655 {
656         READ_LOCALS(NullIfExpr);
657
658         READ_OID_FIELD(opno);
659         READ_OID_FIELD(opfuncid);
660
661         /*
662          * The opfuncid is stored in the textual format primarily for debugging
663          * and documentation reasons.  We want to always read it as zero to force
664          * it to be re-looked-up in the pg_operator entry.      This ensures that
665          * stored rules don't have hidden dependencies on operators' functions.
666          * (We don't currently support an ALTER OPERATOR command, but might
667          * someday.)
668          */
669         local_node->opfuncid = InvalidOid;
670
671         READ_OID_FIELD(opresulttype);
672         READ_BOOL_FIELD(opretset);
673         READ_OID_FIELD(opcollid);
674         READ_OID_FIELD(inputcollid);
675         READ_NODE_FIELD(args);
676         READ_LOCATION_FIELD(location);
677
678         READ_DONE();
679 }
680
681 /*
682  * _readScalarArrayOpExpr
683  */
684 static ScalarArrayOpExpr *
685 _readScalarArrayOpExpr(void)
686 {
687         READ_LOCALS(ScalarArrayOpExpr);
688
689         READ_OID_FIELD(opno);
690         READ_OID_FIELD(opfuncid);
691
692         /*
693          * The opfuncid is stored in the textual format primarily for debugging
694          * and documentation reasons.  We want to always read it as zero to force
695          * it to be re-looked-up in the pg_operator entry.      This ensures that
696          * stored rules don't have hidden dependencies on operators' functions.
697          * (We don't currently support an ALTER OPERATOR command, but might
698          * someday.)
699          */
700         local_node->opfuncid = InvalidOid;
701
702         READ_BOOL_FIELD(useOr);
703         READ_OID_FIELD(inputcollid);
704         READ_NODE_FIELD(args);
705         READ_LOCATION_FIELD(location);
706
707         READ_DONE();
708 }
709
710 /*
711  * _readBoolExpr
712  */
713 static BoolExpr *
714 _readBoolExpr(void)
715 {
716         READ_LOCALS(BoolExpr);
717
718         /* do-it-yourself enum representation */
719         token = pg_strtok(&length); /* skip :boolop */
720         token = pg_strtok(&length); /* get field value */
721         if (strncmp(token, "and", 3) == 0)
722                 local_node->boolop = AND_EXPR;
723         else if (strncmp(token, "or", 2) == 0)
724                 local_node->boolop = OR_EXPR;
725         else if (strncmp(token, "not", 3) == 0)
726                 local_node->boolop = NOT_EXPR;
727         else
728                 elog(ERROR, "unrecognized boolop \"%.*s\"", length, token);
729
730         READ_NODE_FIELD(args);
731         READ_LOCATION_FIELD(location);
732
733         READ_DONE();
734 }
735
736 /*
737  * _readSubLink
738  */
739 static SubLink *
740 _readSubLink(void)
741 {
742         READ_LOCALS(SubLink);
743
744         READ_ENUM_FIELD(subLinkType, SubLinkType);
745         READ_NODE_FIELD(testexpr);
746         READ_NODE_FIELD(operName);
747         READ_NODE_FIELD(subselect);
748         READ_LOCATION_FIELD(location);
749
750         READ_DONE();
751 }
752
753 /*
754  * _readSubPlan is not needed since it doesn't appear in stored rules.
755  */
756
757 /*
758  * _readFieldSelect
759  */
760 static FieldSelect *
761 _readFieldSelect(void)
762 {
763         READ_LOCALS(FieldSelect);
764
765         READ_NODE_FIELD(arg);
766         READ_INT_FIELD(fieldnum);
767         READ_OID_FIELD(resulttype);
768         READ_INT_FIELD(resulttypmod);
769         READ_OID_FIELD(resultcollid);
770
771         READ_DONE();
772 }
773
774 /*
775  * _readFieldStore
776  */
777 static FieldStore *
778 _readFieldStore(void)
779 {
780         READ_LOCALS(FieldStore);
781
782         READ_NODE_FIELD(arg);
783         READ_NODE_FIELD(newvals);
784         READ_NODE_FIELD(fieldnums);
785         READ_OID_FIELD(resulttype);
786
787         READ_DONE();
788 }
789
790 /*
791  * _readRelabelType
792  */
793 static RelabelType *
794 _readRelabelType(void)
795 {
796         READ_LOCALS(RelabelType);
797
798         READ_NODE_FIELD(arg);
799         READ_OID_FIELD(resulttype);
800         READ_INT_FIELD(resulttypmod);
801         READ_OID_FIELD(resultcollid);
802         READ_ENUM_FIELD(relabelformat, CoercionForm);
803         READ_LOCATION_FIELD(location);
804
805         READ_DONE();
806 }
807
808 /*
809  * _readCoerceViaIO
810  */
811 static CoerceViaIO *
812 _readCoerceViaIO(void)
813 {
814         READ_LOCALS(CoerceViaIO);
815
816         READ_NODE_FIELD(arg);
817         READ_OID_FIELD(resulttype);
818         READ_OID_FIELD(resultcollid);
819         READ_ENUM_FIELD(coerceformat, CoercionForm);
820         READ_LOCATION_FIELD(location);
821
822         READ_DONE();
823 }
824
825 /*
826  * _readArrayCoerceExpr
827  */
828 static ArrayCoerceExpr *
829 _readArrayCoerceExpr(void)
830 {
831         READ_LOCALS(ArrayCoerceExpr);
832
833         READ_NODE_FIELD(arg);
834         READ_OID_FIELD(elemfuncid);
835         READ_OID_FIELD(resulttype);
836         READ_INT_FIELD(resulttypmod);
837         READ_OID_FIELD(resultcollid);
838         READ_BOOL_FIELD(isExplicit);
839         READ_ENUM_FIELD(coerceformat, CoercionForm);
840         READ_LOCATION_FIELD(location);
841
842         READ_DONE();
843 }
844
845 /*
846  * _readConvertRowtypeExpr
847  */
848 static ConvertRowtypeExpr *
849 _readConvertRowtypeExpr(void)
850 {
851         READ_LOCALS(ConvertRowtypeExpr);
852
853         READ_NODE_FIELD(arg);
854         READ_OID_FIELD(resulttype);
855         READ_ENUM_FIELD(convertformat, CoercionForm);
856         READ_LOCATION_FIELD(location);
857
858         READ_DONE();
859 }
860
861 /*
862  * _readCollateExpr
863  */
864 static CollateExpr *
865 _readCollateExpr(void)
866 {
867         READ_LOCALS(CollateExpr);
868
869         READ_NODE_FIELD(arg);
870         READ_OID_FIELD(collOid);
871         READ_LOCATION_FIELD(location);
872
873         READ_DONE();
874 }
875
876 /*
877  * _readCaseExpr
878  */
879 static CaseExpr *
880 _readCaseExpr(void)
881 {
882         READ_LOCALS(CaseExpr);
883
884         READ_OID_FIELD(casetype);
885         READ_OID_FIELD(casecollid);
886         READ_NODE_FIELD(arg);
887         READ_NODE_FIELD(args);
888         READ_NODE_FIELD(defresult);
889         READ_LOCATION_FIELD(location);
890
891         READ_DONE();
892 }
893
894 /*
895  * _readCaseWhen
896  */
897 static CaseWhen *
898 _readCaseWhen(void)
899 {
900         READ_LOCALS(CaseWhen);
901
902         READ_NODE_FIELD(expr);
903         READ_NODE_FIELD(result);
904         READ_LOCATION_FIELD(location);
905
906         READ_DONE();
907 }
908
909 /*
910  * _readCaseTestExpr
911  */
912 static CaseTestExpr *
913 _readCaseTestExpr(void)
914 {
915         READ_LOCALS(CaseTestExpr);
916
917         READ_OID_FIELD(typeId);
918         READ_INT_FIELD(typeMod);
919         READ_OID_FIELD(collation);
920
921         READ_DONE();
922 }
923
924 /*
925  * _readArrayExpr
926  */
927 static ArrayExpr *
928 _readArrayExpr(void)
929 {
930         READ_LOCALS(ArrayExpr);
931
932         READ_OID_FIELD(array_typeid);
933         READ_OID_FIELD(array_collid);
934         READ_OID_FIELD(element_typeid);
935         READ_NODE_FIELD(elements);
936         READ_BOOL_FIELD(multidims);
937         READ_LOCATION_FIELD(location);
938
939         READ_DONE();
940 }
941
942 /*
943  * _readRowExpr
944  */
945 static RowExpr *
946 _readRowExpr(void)
947 {
948         READ_LOCALS(RowExpr);
949
950         READ_NODE_FIELD(args);
951         READ_OID_FIELD(row_typeid);
952         READ_ENUM_FIELD(row_format, CoercionForm);
953         READ_NODE_FIELD(colnames);
954         READ_LOCATION_FIELD(location);
955
956         READ_DONE();
957 }
958
959 /*
960  * _readRowCompareExpr
961  */
962 static RowCompareExpr *
963 _readRowCompareExpr(void)
964 {
965         READ_LOCALS(RowCompareExpr);
966
967         READ_ENUM_FIELD(rctype, RowCompareType);
968         READ_NODE_FIELD(opnos);
969         READ_NODE_FIELD(opfamilies);
970         READ_NODE_FIELD(inputcollids);
971         READ_NODE_FIELD(largs);
972         READ_NODE_FIELD(rargs);
973
974         READ_DONE();
975 }
976
977 /*
978  * _readCoalesceExpr
979  */
980 static CoalesceExpr *
981 _readCoalesceExpr(void)
982 {
983         READ_LOCALS(CoalesceExpr);
984
985         READ_OID_FIELD(coalescetype);
986         READ_OID_FIELD(coalescecollid);
987         READ_NODE_FIELD(args);
988         READ_LOCATION_FIELD(location);
989
990         READ_DONE();
991 }
992
993 /*
994  * _readMinMaxExpr
995  */
996 static MinMaxExpr *
997 _readMinMaxExpr(void)
998 {
999         READ_LOCALS(MinMaxExpr);
1000
1001         READ_OID_FIELD(minmaxtype);
1002         READ_OID_FIELD(minmaxcollid);
1003         READ_OID_FIELD(inputcollid);
1004         READ_ENUM_FIELD(op, MinMaxOp);
1005         READ_NODE_FIELD(args);
1006         READ_LOCATION_FIELD(location);
1007
1008         READ_DONE();
1009 }
1010
1011 /*
1012  * _readXmlExpr
1013  */
1014 static XmlExpr *
1015 _readXmlExpr(void)
1016 {
1017         READ_LOCALS(XmlExpr);
1018
1019         READ_ENUM_FIELD(op, XmlExprOp);
1020         READ_STRING_FIELD(name);
1021         READ_NODE_FIELD(named_args);
1022         READ_NODE_FIELD(arg_names);
1023         READ_NODE_FIELD(args);
1024         READ_ENUM_FIELD(xmloption, XmlOptionType);
1025         READ_OID_FIELD(type);
1026         READ_INT_FIELD(typmod);
1027         READ_LOCATION_FIELD(location);
1028
1029         READ_DONE();
1030 }
1031
1032 /*
1033  * _readNullTest
1034  */
1035 static NullTest *
1036 _readNullTest(void)
1037 {
1038         READ_LOCALS(NullTest);
1039
1040         READ_NODE_FIELD(arg);
1041         READ_ENUM_FIELD(nulltesttype, NullTestType);
1042         READ_BOOL_FIELD(argisrow);
1043
1044         READ_DONE();
1045 }
1046
1047 /*
1048  * _readBooleanTest
1049  */
1050 static BooleanTest *
1051 _readBooleanTest(void)
1052 {
1053         READ_LOCALS(BooleanTest);
1054
1055         READ_NODE_FIELD(arg);
1056         READ_ENUM_FIELD(booltesttype, BoolTestType);
1057
1058         READ_DONE();
1059 }
1060
1061 /*
1062  * _readCoerceToDomain
1063  */
1064 static CoerceToDomain *
1065 _readCoerceToDomain(void)
1066 {
1067         READ_LOCALS(CoerceToDomain);
1068
1069         READ_NODE_FIELD(arg);
1070         READ_OID_FIELD(resulttype);
1071         READ_INT_FIELD(resulttypmod);
1072         READ_OID_FIELD(resultcollid);
1073         READ_ENUM_FIELD(coercionformat, CoercionForm);
1074         READ_LOCATION_FIELD(location);
1075
1076         READ_DONE();
1077 }
1078
1079 /*
1080  * _readCoerceToDomainValue
1081  */
1082 static CoerceToDomainValue *
1083 _readCoerceToDomainValue(void)
1084 {
1085         READ_LOCALS(CoerceToDomainValue);
1086
1087         READ_OID_FIELD(typeId);
1088         READ_INT_FIELD(typeMod);
1089         READ_OID_FIELD(collation);
1090         READ_LOCATION_FIELD(location);
1091
1092         READ_DONE();
1093 }
1094
1095 /*
1096  * _readSetToDefault
1097  */
1098 static SetToDefault *
1099 _readSetToDefault(void)
1100 {
1101         READ_LOCALS(SetToDefault);
1102
1103         READ_OID_FIELD(typeId);
1104         READ_INT_FIELD(typeMod);
1105         READ_OID_FIELD(collation);
1106         READ_LOCATION_FIELD(location);
1107
1108         READ_DONE();
1109 }
1110
1111 /*
1112  * _readCurrentOfExpr
1113  */
1114 static CurrentOfExpr *
1115 _readCurrentOfExpr(void)
1116 {
1117         READ_LOCALS(CurrentOfExpr);
1118
1119         READ_UINT_FIELD(cvarno);
1120         READ_STRING_FIELD(cursor_name);
1121         READ_INT_FIELD(cursor_param);
1122
1123         READ_DONE();
1124 }
1125
1126 /*
1127  * _readTargetEntry
1128  */
1129 static TargetEntry *
1130 _readTargetEntry(void)
1131 {
1132         READ_LOCALS(TargetEntry);
1133
1134         READ_NODE_FIELD(expr);
1135         READ_INT_FIELD(resno);
1136         READ_STRING_FIELD(resname);
1137         READ_UINT_FIELD(ressortgroupref);
1138         READ_OID_FIELD(resorigtbl);
1139         READ_INT_FIELD(resorigcol);
1140         READ_BOOL_FIELD(resjunk);
1141
1142         READ_DONE();
1143 }
1144
1145 /*
1146  * _readRangeTblRef
1147  */
1148 static RangeTblRef *
1149 _readRangeTblRef(void)
1150 {
1151         READ_LOCALS(RangeTblRef);
1152
1153         READ_INT_FIELD(rtindex);
1154
1155         READ_DONE();
1156 }
1157
1158 /*
1159  * _readJoinExpr
1160  */
1161 static JoinExpr *
1162 _readJoinExpr(void)
1163 {
1164         READ_LOCALS(JoinExpr);
1165
1166         READ_ENUM_FIELD(jointype, JoinType);
1167         READ_BOOL_FIELD(isNatural);
1168         READ_NODE_FIELD(larg);
1169         READ_NODE_FIELD(rarg);
1170         READ_NODE_FIELD(usingClause);
1171         READ_NODE_FIELD(quals);
1172         READ_NODE_FIELD(alias);
1173         READ_INT_FIELD(rtindex);
1174
1175         READ_DONE();
1176 }
1177
1178 /*
1179  * _readFromExpr
1180  */
1181 static FromExpr *
1182 _readFromExpr(void)
1183 {
1184         READ_LOCALS(FromExpr);
1185
1186         READ_NODE_FIELD(fromlist);
1187         READ_NODE_FIELD(quals);
1188
1189         READ_DONE();
1190 }
1191
1192
1193 /*
1194  *      Stuff from parsenodes.h.
1195  */
1196
1197 /*
1198  * _readRangeTblEntry
1199  */
1200 static RangeTblEntry *
1201 _readRangeTblEntry(void)
1202 {
1203         READ_LOCALS(RangeTblEntry);
1204
1205         /* put alias + eref first to make dump more legible */
1206         READ_NODE_FIELD(alias);
1207         READ_NODE_FIELD(eref);
1208         READ_ENUM_FIELD(rtekind, RTEKind);
1209
1210         switch (local_node->rtekind)
1211         {
1212                 case RTE_RELATION:
1213                         READ_OID_FIELD(relid);
1214                         READ_CHAR_FIELD(relkind);
1215                         break;
1216                 case RTE_SUBQUERY:
1217                         READ_NODE_FIELD(subquery);
1218                         READ_BOOL_FIELD(security_barrier);
1219                         break;
1220                 case RTE_JOIN:
1221                         READ_ENUM_FIELD(jointype, JoinType);
1222                         READ_NODE_FIELD(joinaliasvars);
1223                         break;
1224                 case RTE_FUNCTION:
1225                         READ_NODE_FIELD(functions);
1226                         READ_BOOL_FIELD(funcordinality);
1227                         break;
1228                 case RTE_VALUES:
1229                         READ_NODE_FIELD(values_lists);
1230                         READ_NODE_FIELD(values_collations);
1231                         break;
1232                 case RTE_CTE:
1233                         READ_STRING_FIELD(ctename);
1234                         READ_UINT_FIELD(ctelevelsup);
1235                         READ_BOOL_FIELD(self_reference);
1236                         READ_NODE_FIELD(ctecoltypes);
1237                         READ_NODE_FIELD(ctecoltypmods);
1238                         READ_NODE_FIELD(ctecolcollations);
1239                         break;
1240                 default:
1241                         elog(ERROR, "unrecognized RTE kind: %d",
1242                                  (int) local_node->rtekind);
1243                         break;
1244         }
1245
1246         READ_BOOL_FIELD(lateral);
1247         READ_BOOL_FIELD(inh);
1248         READ_BOOL_FIELD(inFromCl);
1249         READ_UINT_FIELD(requiredPerms);
1250         READ_OID_FIELD(checkAsUser);
1251         READ_BITMAPSET_FIELD(selectedCols);
1252         READ_BITMAPSET_FIELD(modifiedCols);
1253
1254         READ_DONE();
1255 }
1256
1257 /*
1258  * _readRangeTblFunction
1259  */
1260 static RangeTblFunction *
1261 _readRangeTblFunction(void)
1262 {
1263         READ_LOCALS(RangeTblFunction);
1264
1265         READ_NODE_FIELD(funcexpr);
1266         READ_INT_FIELD(funccolcount);
1267         READ_NODE_FIELD(funccolnames);
1268         READ_NODE_FIELD(funccoltypes);
1269         READ_NODE_FIELD(funccoltypmods);
1270         READ_NODE_FIELD(funccolcollations);
1271         READ_BITMAPSET_FIELD(funcparams);
1272
1273         READ_DONE();
1274 }
1275
1276
1277 /*
1278  * parseNodeString
1279  *
1280  * Given a character string representing a node tree, parseNodeString creates
1281  * the internal node structure.
1282  *
1283  * The string to be read must already have been loaded into pg_strtok().
1284  */
1285 Node *
1286 parseNodeString(void)
1287 {
1288         void       *return_value;
1289
1290         READ_TEMP_LOCALS();
1291
1292         token = pg_strtok(&length);
1293
1294 #define MATCH(tokname, namelen) \
1295         (length == namelen && memcmp(token, tokname, namelen) == 0)
1296
1297         if (MATCH("QUERY", 5))
1298                 return_value = _readQuery();
1299         else if (MATCH("WITHCHECKOPTION", 15))
1300                 return_value = _readWithCheckOption();
1301         else if (MATCH("SORTGROUPCLAUSE", 15))
1302                 return_value = _readSortGroupClause();
1303         else if (MATCH("WINDOWCLAUSE", 12))
1304                 return_value = _readWindowClause();
1305         else if (MATCH("ROWMARKCLAUSE", 13))
1306                 return_value = _readRowMarkClause();
1307         else if (MATCH("COMMONTABLEEXPR", 15))
1308                 return_value = _readCommonTableExpr();
1309         else if (MATCH("SETOPERATIONSTMT", 16))
1310                 return_value = _readSetOperationStmt();
1311         else if (MATCH("ALIAS", 5))
1312                 return_value = _readAlias();
1313         else if (MATCH("RANGEVAR", 8))
1314                 return_value = _readRangeVar();
1315         else if (MATCH("INTOCLAUSE", 10))
1316                 return_value = _readIntoClause();
1317         else if (MATCH("VAR", 3))
1318                 return_value = _readVar();
1319         else if (MATCH("CONST", 5))
1320                 return_value = _readConst();
1321         else if (MATCH("PARAM", 5))
1322                 return_value = _readParam();
1323         else if (MATCH("AGGREF", 6))
1324                 return_value = _readAggref();
1325         else if (MATCH("WINDOWFUNC", 10))
1326                 return_value = _readWindowFunc();
1327         else if (MATCH("ARRAYREF", 8))
1328                 return_value = _readArrayRef();
1329         else if (MATCH("FUNCEXPR", 8))
1330                 return_value = _readFuncExpr();
1331         else if (MATCH("NAMEDARGEXPR", 12))
1332                 return_value = _readNamedArgExpr();
1333         else if (MATCH("OPEXPR", 6))
1334                 return_value = _readOpExpr();
1335         else if (MATCH("DISTINCTEXPR", 12))
1336                 return_value = _readDistinctExpr();
1337         else if (MATCH("NULLIFEXPR", 10))
1338                 return_value = _readNullIfExpr();
1339         else if (MATCH("SCALARARRAYOPEXPR", 17))
1340                 return_value = _readScalarArrayOpExpr();
1341         else if (MATCH("BOOLEXPR", 8))
1342                 return_value = _readBoolExpr();
1343         else if (MATCH("SUBLINK", 7))
1344                 return_value = _readSubLink();
1345         else if (MATCH("FIELDSELECT", 11))
1346                 return_value = _readFieldSelect();
1347         else if (MATCH("FIELDSTORE", 10))
1348                 return_value = _readFieldStore();
1349         else if (MATCH("RELABELTYPE", 11))
1350                 return_value = _readRelabelType();
1351         else if (MATCH("COERCEVIAIO", 11))
1352                 return_value = _readCoerceViaIO();
1353         else if (MATCH("ARRAYCOERCEEXPR", 15))
1354                 return_value = _readArrayCoerceExpr();
1355         else if (MATCH("CONVERTROWTYPEEXPR", 18))
1356                 return_value = _readConvertRowtypeExpr();
1357         else if (MATCH("COLLATE", 7))
1358                 return_value = _readCollateExpr();
1359         else if (MATCH("CASE", 4))
1360                 return_value = _readCaseExpr();
1361         else if (MATCH("WHEN", 4))
1362                 return_value = _readCaseWhen();
1363         else if (MATCH("CASETESTEXPR", 12))
1364                 return_value = _readCaseTestExpr();
1365         else if (MATCH("ARRAY", 5))
1366                 return_value = _readArrayExpr();
1367         else if (MATCH("ROW", 3))
1368                 return_value = _readRowExpr();
1369         else if (MATCH("ROWCOMPARE", 10))
1370                 return_value = _readRowCompareExpr();
1371         else if (MATCH("COALESCE", 8))
1372                 return_value = _readCoalesceExpr();
1373         else if (MATCH("MINMAX", 6))
1374                 return_value = _readMinMaxExpr();
1375         else if (MATCH("XMLEXPR", 7))
1376                 return_value = _readXmlExpr();
1377         else if (MATCH("NULLTEST", 8))
1378                 return_value = _readNullTest();
1379         else if (MATCH("BOOLEANTEST", 11))
1380                 return_value = _readBooleanTest();
1381         else if (MATCH("COERCETODOMAIN", 14))
1382                 return_value = _readCoerceToDomain();
1383         else if (MATCH("COERCETODOMAINVALUE", 19))
1384                 return_value = _readCoerceToDomainValue();
1385         else if (MATCH("SETTODEFAULT", 12))
1386                 return_value = _readSetToDefault();
1387         else if (MATCH("CURRENTOFEXPR", 13))
1388                 return_value = _readCurrentOfExpr();
1389         else if (MATCH("TARGETENTRY", 11))
1390                 return_value = _readTargetEntry();
1391         else if (MATCH("RANGETBLREF", 11))
1392                 return_value = _readRangeTblRef();
1393         else if (MATCH("JOINEXPR", 8))
1394                 return_value = _readJoinExpr();
1395         else if (MATCH("FROMEXPR", 8))
1396                 return_value = _readFromExpr();
1397         else if (MATCH("RTE", 3))
1398                 return_value = _readRangeTblEntry();
1399         else if (MATCH("RANGETBLFUNCTION", 16))
1400                 return_value = _readRangeTblFunction();
1401         else if (MATCH("NOTIFY", 6))
1402                 return_value = _readNotifyStmt();
1403         else if (MATCH("DECLARECURSOR", 13))
1404                 return_value = _readDeclareCursorStmt();
1405         else
1406         {
1407                 elog(ERROR, "badly formatted node string \"%.32s\"...", token);
1408                 return_value = NULL;    /* keep compiler quiet */
1409         }
1410
1411         return (Node *) return_value;
1412 }
1413
1414
1415 /*
1416  * readDatum
1417  *
1418  * Given a string representation of a constant, recreate the appropriate
1419  * Datum.  The string representation embeds length info, but not byValue,
1420  * so we must be told that.
1421  */
1422 static Datum
1423 readDatum(bool typbyval)
1424 {
1425         Size            length,
1426                                 i;
1427         int                     tokenLength;
1428         char       *token;
1429         Datum           res;
1430         char       *s;
1431
1432         /*
1433          * read the actual length of the value
1434          */
1435         token = pg_strtok(&tokenLength);
1436         length = atoui(token);
1437
1438         token = pg_strtok(&tokenLength);        /* read the '[' */
1439         if (token == NULL || token[0] != '[')
1440                 elog(ERROR, "expected \"[\" to start datum, but got \"%s\"; length = %lu",
1441                          token ? (const char *) token : "[NULL]",
1442                          (unsigned long) length);
1443
1444         if (typbyval)
1445         {
1446                 if (length > (Size) sizeof(Datum))
1447                         elog(ERROR, "byval datum but length = %lu",
1448                                  (unsigned long) length);
1449                 res = (Datum) 0;
1450                 s = (char *) (&res);
1451                 for (i = 0; i < (Size) sizeof(Datum); i++)
1452                 {
1453                         token = pg_strtok(&tokenLength);
1454                         s[i] = (char) atoi(token);
1455                 }
1456         }
1457         else if (length <= 0)
1458                 res = (Datum) NULL;
1459         else
1460         {
1461                 s = (char *) palloc(length);
1462                 for (i = 0; i < length; i++)
1463                 {
1464                         token = pg_strtok(&tokenLength);
1465                         s[i] = (char) atoi(token);
1466                 }
1467                 res = PointerGetDatum(s);
1468         }
1469
1470         token = pg_strtok(&tokenLength);        /* read the ']' */
1471         if (token == NULL || token[0] != ']')
1472                 elog(ERROR, "expected \"]\" to end datum, but got \"%s\"; length = %lu",
1473                          token ? (const char *) token : "[NULL]",
1474                          (unsigned long) length);
1475
1476         return res;
1477 }