OSDN Git Service

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