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