PostgreSQL Source Code  git master
readfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * readfuncs.c
4  * Reader functions for Postgres tree nodes.
5  *
6  * Portions Copyright (c) 1996-2022, 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  * Parse location fields are written out by outfuncs.c, but only for
15  * debugging use. When reading a location field, we normally discard
16  * the stored value and set the location field to -1 (ie, "unknown").
17  * This is because nodes coming from a stored rule should not be thought
18  * to have a known location in the current query's text.
19  *
20  * However, if restore_location_fields is true, we do restore location
21  * fields from the string. This is currently intended only for use by the
22  * WRITE_READ_PARSE_PLAN_TREES test code, which doesn't want to cause
23  * any change in the node contents.
24  *
25  *-------------------------------------------------------------------------
26  */
27 #include "postgres.h"
28 
29 #include <math.h>
30 
31 #include "miscadmin.h"
32 #include "nodes/bitmapset.h"
33 #include "nodes/readfuncs.h"
34 
35 
36 /*
37  * Macros to simplify reading of different kinds of fields. Use these
38  * wherever possible to reduce the chance for silly typos. Note that these
39  * hard-wire conventions about the names of the local variables in a Read
40  * routine.
41  */
42 
43 /* Macros for declaring appropriate local variables */
44 
45 /* A few guys need only local_node */
46 #define READ_LOCALS_NO_FIELDS(nodeTypeName) \
47  nodeTypeName *local_node = makeNode(nodeTypeName)
48 
49 /* And a few guys need only the pg_strtok support fields */
50 #define READ_TEMP_LOCALS() \
51  const char *token; \
52  int length
53 
54 /* ... but most need both */
55 #define READ_LOCALS(nodeTypeName) \
56  READ_LOCALS_NO_FIELDS(nodeTypeName); \
57  READ_TEMP_LOCALS()
58 
59 /* Read an integer field (anything written as ":fldname %d") */
60 #define READ_INT_FIELD(fldname) \
61  token = pg_strtok(&length); /* skip :fldname */ \
62  token = pg_strtok(&length); /* get field value */ \
63  local_node->fldname = atoi(token)
64 
65 /* Read an unsigned integer field (anything written as ":fldname %u") */
66 #define READ_UINT_FIELD(fldname) \
67  token = pg_strtok(&length); /* skip :fldname */ \
68  token = pg_strtok(&length); /* get field value */ \
69  local_node->fldname = atoui(token)
70 
71 /* Read an unsigned integer field (anything written using UINT64_FORMAT) */
72 #define READ_UINT64_FIELD(fldname) \
73  token = pg_strtok(&length); /* skip :fldname */ \
74  token = pg_strtok(&length); /* get field value */ \
75  local_node->fldname = strtou64(token, NULL, 10)
76 
77 /* Read a long integer field (anything written as ":fldname %ld") */
78 #define READ_LONG_FIELD(fldname) \
79  token = pg_strtok(&length); /* skip :fldname */ \
80  token = pg_strtok(&length); /* get field value */ \
81  local_node->fldname = atol(token)
82 
83 /* Read an OID field (don't hard-wire assumption that OID is same as uint) */
84 #define READ_OID_FIELD(fldname) \
85  token = pg_strtok(&length); /* skip :fldname */ \
86  token = pg_strtok(&length); /* get field value */ \
87  local_node->fldname = atooid(token)
88 
89 /* Read a char field (ie, one ascii character) */
90 #define READ_CHAR_FIELD(fldname) \
91  token = pg_strtok(&length); /* skip :fldname */ \
92  token = pg_strtok(&length); /* get field value */ \
93  /* avoid overhead of calling debackslash() for one char */ \
94  local_node->fldname = (length == 0) ? '\0' : (token[0] == '\\' ? token[1] : token[0])
95 
96 /* Read an enumerated-type field that was written as an integer code */
97 #define READ_ENUM_FIELD(fldname, enumtype) \
98  token = pg_strtok(&length); /* skip :fldname */ \
99  token = pg_strtok(&length); /* get field value */ \
100  local_node->fldname = (enumtype) atoi(token)
101 
102 /* Read a float field */
103 #define READ_FLOAT_FIELD(fldname) \
104  token = pg_strtok(&length); /* skip :fldname */ \
105  token = pg_strtok(&length); /* get field value */ \
106  local_node->fldname = atof(token)
107 
108 /* Read a boolean field */
109 #define READ_BOOL_FIELD(fldname) \
110  token = pg_strtok(&length); /* skip :fldname */ \
111  token = pg_strtok(&length); /* get field value */ \
112  local_node->fldname = strtobool(token)
113 
114 /* Read a character-string field */
115 #define READ_STRING_FIELD(fldname) \
116  token = pg_strtok(&length); /* skip :fldname */ \
117  token = pg_strtok(&length); /* get field value */ \
118  local_node->fldname = nullable_string(token, length)
119 
120 /* Read a parse location field (and possibly throw away the value) */
121 #ifdef WRITE_READ_PARSE_PLAN_TREES
122 #define READ_LOCATION_FIELD(fldname) \
123  token = pg_strtok(&length); /* skip :fldname */ \
124  token = pg_strtok(&length); /* get field value */ \
125  local_node->fldname = restore_location_fields ? atoi(token) : -1
126 #else
127 #define READ_LOCATION_FIELD(fldname) \
128  token = pg_strtok(&length); /* skip :fldname */ \
129  token = pg_strtok(&length); /* get field value */ \
130  (void) token; /* in case not used elsewhere */ \
131  local_node->fldname = -1 /* set field to "unknown" */
132 #endif
133 
134 /* Read a Node field */
135 #define READ_NODE_FIELD(fldname) \
136  token = pg_strtok(&length); /* skip :fldname */ \
137  (void) token; /* in case not used elsewhere */ \
138  local_node->fldname = nodeRead(NULL, 0)
139 
140 /* Read a bitmapset field */
141 #define READ_BITMAPSET_FIELD(fldname) \
142  token = pg_strtok(&length); /* skip :fldname */ \
143  (void) token; /* in case not used elsewhere */ \
144  local_node->fldname = _readBitmapset()
145 
146 /* Read an attribute number array */
147 #define READ_ATTRNUMBER_ARRAY(fldname, len) \
148  token = pg_strtok(&length); /* skip :fldname */ \
149  local_node->fldname = readAttrNumberCols(len)
150 
151 /* Read an oid array */
152 #define READ_OID_ARRAY(fldname, len) \
153  token = pg_strtok(&length); /* skip :fldname */ \
154  local_node->fldname = readOidCols(len)
155 
156 /* Read an int array */
157 #define READ_INT_ARRAY(fldname, len) \
158  token = pg_strtok(&length); /* skip :fldname */ \
159  local_node->fldname = readIntCols(len)
160 
161 /* Read a bool array */
162 #define READ_BOOL_ARRAY(fldname, len) \
163  token = pg_strtok(&length); /* skip :fldname */ \
164  local_node->fldname = readBoolCols(len)
165 
166 /* Routine exit */
167 #define READ_DONE() \
168  return local_node
169 
170 
171 /*
172  * NOTE: use atoi() to read values written with %d, or atoui() to read
173  * values written with %u in outfuncs.c. An exception is OID values,
174  * for which use atooid(). (As of 7.1, outfuncs.c writes OIDs as %u,
175  * but this will probably change in the future.)
176  */
177 #define atoui(x) ((unsigned int) strtoul((x), NULL, 10))
178 
179 #define strtobool(x) ((*(x) == 't') ? true : false)
180 
181 static char *
182 nullable_string(const char *token, int length)
183 {
184  /* outToken emits <> for NULL, and pg_strtok makes that an empty string */
185  if (length == 0)
186  return NULL;
187  /* outToken emits "" for empty string */
188  if (length == 2 && token[0] == '"' && token[1] == '"')
189  return pstrdup("");
190  /* otherwise, we must remove protective backslashes added by outToken */
191  return debackslash(token, length);
192 }
193 
194 
195 /*
196  * _readBitmapset
197  *
198  * Note: this code is used in contexts where we know that a Bitmapset
199  * is expected. There is equivalent code in nodeRead() that can read a
200  * Bitmapset when we come across one in other contexts.
201  */
202 static Bitmapset *
204 {
205  Bitmapset *result = NULL;
206 
208 
209  token = pg_strtok(&length);
210  if (token == NULL)
211  elog(ERROR, "incomplete Bitmapset structure");
212  if (length != 1 || token[0] != '(')
213  elog(ERROR, "unrecognized token: \"%.*s\"", length, token);
214 
215  token = pg_strtok(&length);
216  if (token == NULL)
217  elog(ERROR, "incomplete Bitmapset structure");
218  if (length != 1 || token[0] != 'b')
219  elog(ERROR, "unrecognized token: \"%.*s\"", length, token);
220 
221  for (;;)
222  {
223  int val;
224  char *endptr;
225 
226  token = pg_strtok(&length);
227  if (token == NULL)
228  elog(ERROR, "unterminated Bitmapset structure");
229  if (length == 1 && token[0] == ')')
230  break;
231  val = (int) strtol(token, &endptr, 10);
232  if (endptr != token + length)
233  elog(ERROR, "unrecognized integer: \"%.*s\"", length, token);
234  result = bms_add_member(result, val);
235  }
236 
237  return result;
238 }
239 
240 /*
241  * We export this function for use by extensions that define extensible nodes.
242  * That's somewhat historical, though, because calling nodeRead() will work.
243  */
244 Bitmapset *
246 {
247  return _readBitmapset();
248 }
249 
250 #include "readfuncs.funcs.c"
251 
252 
253 /*
254  * Support functions for nodes with custom_read_write attribute or
255  * special_read_write attribute
256  */
257 
258 static Const *
260 {
262 
263  READ_OID_FIELD(consttype);
264  READ_INT_FIELD(consttypmod);
265  READ_OID_FIELD(constcollid);
266  READ_INT_FIELD(constlen);
267  READ_BOOL_FIELD(constbyval);
268  READ_BOOL_FIELD(constisnull);
269  READ_LOCATION_FIELD(location);
270 
271  token = pg_strtok(&length); /* skip :constvalue */
272  if (local_node->constisnull)
273  token = pg_strtok(&length); /* skip "<>" */
274  else
275  local_node->constvalue = readDatum(local_node->constbyval);
276 
277  READ_DONE();
278 }
279 
280 static BoolExpr *
282 {
284 
285  /* do-it-yourself enum representation */
286  token = pg_strtok(&length); /* skip :boolop */
287  token = pg_strtok(&length); /* get field value */
288  if (length == 3 && strncmp(token, "and", 3) == 0)
289  local_node->boolop = AND_EXPR;
290  else if (length == 2 && strncmp(token, "or", 2) == 0)
291  local_node->boolop = OR_EXPR;
292  else if (length == 3 && strncmp(token, "not", 3) == 0)
293  local_node->boolop = NOT_EXPR;
294  else
295  elog(ERROR, "unrecognized boolop \"%.*s\"", length, token);
296 
298  READ_LOCATION_FIELD(location);
299 
300  READ_DONE();
301 }
302 
303 static A_Const *
305 {
307 
308  token = pg_strtok(&length);
309  if (length == 4 && strncmp(token, "NULL", 4) == 0)
310  local_node->isnull = true;
311  else
312  {
313  union ValUnion *tmp = nodeRead(NULL, 0);
314 
315  memcpy(&local_node->val, tmp, sizeof(*tmp));
316  }
317 
318  READ_LOCATION_FIELD(location);
319 
320  READ_DONE();
321 }
322 
323 /*
324  * _readConstraint
325  */
326 static Constraint *
328 {
330 
331  READ_STRING_FIELD(conname);
332  READ_BOOL_FIELD(deferrable);
333  READ_BOOL_FIELD(initdeferred);
334  READ_LOCATION_FIELD(location);
335 
336  token = pg_strtok(&length); /* skip :contype */
337  token = pg_strtok(&length); /* get field value */
338  if (length == 4 && strncmp(token, "NULL", 4) == 0)
339  local_node->contype = CONSTR_NULL;
340  else if (length == 8 && strncmp(token, "NOT_NULL", 8) == 0)
341  local_node->contype = CONSTR_NOTNULL;
342  else if (length == 7 && strncmp(token, "DEFAULT", 7) == 0)
343  local_node->contype = CONSTR_DEFAULT;
344  else if (length == 8 && strncmp(token, "IDENTITY", 8) == 0)
345  local_node->contype = CONSTR_IDENTITY;
346  else if (length == 9 && strncmp(token, "GENERATED", 9) == 0)
347  local_node->contype = CONSTR_GENERATED;
348  else if (length == 5 && strncmp(token, "CHECK", 5) == 0)
349  local_node->contype = CONSTR_CHECK;
350  else if (length == 11 && strncmp(token, "PRIMARY_KEY", 11) == 0)
351  local_node->contype = CONSTR_PRIMARY;
352  else if (length == 6 && strncmp(token, "UNIQUE", 6) == 0)
353  local_node->contype = CONSTR_UNIQUE;
354  else if (length == 9 && strncmp(token, "EXCLUSION", 9) == 0)
355  local_node->contype = CONSTR_EXCLUSION;
356  else if (length == 11 && strncmp(token, "FOREIGN_KEY", 11) == 0)
357  local_node->contype = CONSTR_FOREIGN;
358  else if (length == 15 && strncmp(token, "ATTR_DEFERRABLE", 15) == 0)
359  local_node->contype = CONSTR_ATTR_DEFERRABLE;
360  else if (length == 19 && strncmp(token, "ATTR_NOT_DEFERRABLE", 19) == 0)
361  local_node->contype = CONSTR_ATTR_NOT_DEFERRABLE;
362  else if (length == 13 && strncmp(token, "ATTR_DEFERRED", 13) == 0)
363  local_node->contype = CONSTR_ATTR_DEFERRED;
364  else if (length == 14 && strncmp(token, "ATTR_IMMEDIATE", 14) == 0)
365  local_node->contype = CONSTR_ATTR_IMMEDIATE;
366 
367  switch (local_node->contype)
368  {
369  case CONSTR_NULL:
370  case CONSTR_NOTNULL:
371  /* no extra fields */
372  break;
373 
374  case CONSTR_DEFAULT:
375  READ_NODE_FIELD(raw_expr);
376  READ_STRING_FIELD(cooked_expr);
377  break;
378 
379  case CONSTR_IDENTITY:
381  READ_CHAR_FIELD(generated_when);
382  break;
383 
384  case CONSTR_GENERATED:
385  READ_NODE_FIELD(raw_expr);
386  READ_STRING_FIELD(cooked_expr);
387  READ_CHAR_FIELD(generated_when);
388  break;
389 
390  case CONSTR_CHECK:
391  READ_BOOL_FIELD(is_no_inherit);
392  READ_NODE_FIELD(raw_expr);
393  READ_STRING_FIELD(cooked_expr);
394  READ_BOOL_FIELD(skip_validation);
395  READ_BOOL_FIELD(initially_valid);
396  break;
397 
398  case CONSTR_PRIMARY:
399  READ_NODE_FIELD(keys);
400  READ_NODE_FIELD(including);
402  READ_STRING_FIELD(indexname);
403  READ_STRING_FIELD(indexspace);
404  READ_BOOL_FIELD(reset_default_tblspc);
405  /* access_method and where_clause not currently used */
406  break;
407 
408  case CONSTR_UNIQUE:
409  READ_BOOL_FIELD(nulls_not_distinct);
410  READ_NODE_FIELD(keys);
411  READ_NODE_FIELD(including);
413  READ_STRING_FIELD(indexname);
414  READ_STRING_FIELD(indexspace);
415  READ_BOOL_FIELD(reset_default_tblspc);
416  /* access_method and where_clause not currently used */
417  break;
418 
419  case CONSTR_EXCLUSION:
420  READ_NODE_FIELD(exclusions);
421  READ_NODE_FIELD(including);
423  READ_STRING_FIELD(indexname);
424  READ_STRING_FIELD(indexspace);
425  READ_BOOL_FIELD(reset_default_tblspc);
426  READ_STRING_FIELD(access_method);
427  READ_NODE_FIELD(where_clause);
428  break;
429 
430  case CONSTR_FOREIGN:
431  READ_NODE_FIELD(pktable);
432  READ_NODE_FIELD(fk_attrs);
433  READ_NODE_FIELD(pk_attrs);
434  READ_CHAR_FIELD(fk_matchtype);
435  READ_CHAR_FIELD(fk_upd_action);
436  READ_CHAR_FIELD(fk_del_action);
437  READ_NODE_FIELD(fk_del_set_cols);
438  READ_NODE_FIELD(old_conpfeqop);
439  READ_OID_FIELD(old_pktable_oid);
440  READ_BOOL_FIELD(skip_validation);
441  READ_BOOL_FIELD(initially_valid);
442  break;
443 
448  /* no extra fields */
449  break;
450 
451  default:
452  elog(ERROR, "unrecognized ConstrType: %d", (int) local_node->contype);
453  break;
454  }
455 
456  READ_DONE();
457 }
458 
459 static RangeTblEntry *
461 {
463 
464  /* put alias + eref first to make dump more legible */
465  READ_NODE_FIELD(alias);
466  READ_NODE_FIELD(eref);
467  READ_ENUM_FIELD(rtekind, RTEKind);
468 
469  switch (local_node->rtekind)
470  {
471  case RTE_RELATION:
472  READ_OID_FIELD(relid);
473  READ_CHAR_FIELD(relkind);
474  READ_INT_FIELD(rellockmode);
475  READ_NODE_FIELD(tablesample);
476  break;
477  case RTE_SUBQUERY:
478  READ_NODE_FIELD(subquery);
479  READ_BOOL_FIELD(security_barrier);
480  break;
481  case RTE_JOIN:
482  READ_ENUM_FIELD(jointype, JoinType);
483  READ_INT_FIELD(joinmergedcols);
484  READ_NODE_FIELD(joinaliasvars);
485  READ_NODE_FIELD(joinleftcols);
486  READ_NODE_FIELD(joinrightcols);
487  READ_NODE_FIELD(join_using_alias);
488  break;
489  case RTE_FUNCTION:
491  READ_BOOL_FIELD(funcordinality);
492  break;
493  case RTE_TABLEFUNC:
494  READ_NODE_FIELD(tablefunc);
495  /* The RTE must have a copy of the column type info, if any */
496  if (local_node->tablefunc)
497  {
498  TableFunc *tf = local_node->tablefunc;
499 
500  local_node->coltypes = tf->coltypes;
501  local_node->coltypmods = tf->coltypmods;
502  local_node->colcollations = tf->colcollations;
503  }
504  break;
505  case RTE_VALUES:
506  READ_NODE_FIELD(values_lists);
507  READ_NODE_FIELD(coltypes);
508  READ_NODE_FIELD(coltypmods);
509  READ_NODE_FIELD(colcollations);
510  break;
511  case RTE_CTE:
512  READ_STRING_FIELD(ctename);
513  READ_UINT_FIELD(ctelevelsup);
514  READ_BOOL_FIELD(self_reference);
515  READ_NODE_FIELD(coltypes);
516  READ_NODE_FIELD(coltypmods);
517  READ_NODE_FIELD(colcollations);
518  break;
519  case RTE_NAMEDTUPLESTORE:
520  READ_STRING_FIELD(enrname);
521  READ_FLOAT_FIELD(enrtuples);
522  READ_OID_FIELD(relid);
523  READ_NODE_FIELD(coltypes);
524  READ_NODE_FIELD(coltypmods);
525  READ_NODE_FIELD(colcollations);
526  break;
527  case RTE_RESULT:
528  /* no extra fields */
529  break;
530  default:
531  elog(ERROR, "unrecognized RTE kind: %d",
532  (int) local_node->rtekind);
533  break;
534  }
535 
536  READ_BOOL_FIELD(lateral);
537  READ_BOOL_FIELD(inh);
538  READ_BOOL_FIELD(inFromCl);
539  READ_UINT_FIELD(requiredPerms);
540  READ_OID_FIELD(checkAsUser);
541  READ_BITMAPSET_FIELD(selectedCols);
542  READ_BITMAPSET_FIELD(insertedCols);
543  READ_BITMAPSET_FIELD(updatedCols);
544  READ_BITMAPSET_FIELD(extraUpdatedCols);
545  READ_NODE_FIELD(securityQuals);
546 
547  READ_DONE();
548 }
549 
550 static A_Expr *
552 {
554 
555  token = pg_strtok(&length);
556 
557  if (length == 3 && strncmp(token, "ANY", 3) == 0)
558  {
559  local_node->kind = AEXPR_OP_ANY;
561  }
562  else if (length == 3 && strncmp(token, "ALL", 3) == 0)
563  {
564  local_node->kind = AEXPR_OP_ALL;
566  }
567  else if (length == 8 && strncmp(token, "DISTINCT", 8) == 0)
568  {
569  local_node->kind = AEXPR_DISTINCT;
571  }
572  else if (length == 12 && strncmp(token, "NOT_DISTINCT", 12) == 0)
573  {
574  local_node->kind = AEXPR_NOT_DISTINCT;
576  }
577  else if (length == 6 && strncmp(token, "NULLIF", 6) == 0)
578  {
579  local_node->kind = AEXPR_NULLIF;
581  }
582  else if (length == 2 && strncmp(token, "IN", 2) == 0)
583  {
584  local_node->kind = AEXPR_IN;
586  }
587  else if (length == 4 && strncmp(token, "LIKE", 4) == 0)
588  {
589  local_node->kind = AEXPR_LIKE;
591  }
592  else if (length == 5 && strncmp(token, "ILIKE", 5) == 0)
593  {
594  local_node->kind = AEXPR_ILIKE;
596  }
597  else if (length == 7 && strncmp(token, "SIMILAR", 7) == 0)
598  {
599  local_node->kind = AEXPR_SIMILAR;
601  }
602  else if (length == 7 && strncmp(token, "BETWEEN", 7) == 0)
603  {
604  local_node->kind = AEXPR_BETWEEN;
606  }
607  else if (length == 11 && strncmp(token, "NOT_BETWEEN", 11) == 0)
608  {
609  local_node->kind = AEXPR_NOT_BETWEEN;
611  }
612  else if (length == 11 && strncmp(token, "BETWEEN_SYM", 11) == 0)
613  {
614  local_node->kind = AEXPR_BETWEEN_SYM;
616  }
617  else if (length == 15 && strncmp(token, "NOT_BETWEEN_SYM", 15) == 0)
618  {
619  local_node->kind = AEXPR_NOT_BETWEEN_SYM;
621  }
622  else if (length == 5 && strncmp(token, ":name", 5) == 0)
623  {
624  local_node->kind = AEXPR_OP;
625  local_node->name = nodeRead(NULL, 0);
626  }
627  else
628  elog(ERROR, "unrecognized A_Expr kind: \"%.*s\"", length, token);
629 
630  READ_NODE_FIELD(lexpr);
631  READ_NODE_FIELD(rexpr);
632  READ_LOCATION_FIELD(location);
633 
634  READ_DONE();
635 }
636 
637 static ExtensibleNode *
639 {
640  const ExtensibleNodeMethods *methods;
641  ExtensibleNode *local_node;
642  const char *extnodename;
643 
645 
646  token = pg_strtok(&length); /* skip :extnodename */
647  token = pg_strtok(&length); /* get extnodename */
648 
649  extnodename = nullable_string(token, length);
650  if (!extnodename)
651  elog(ERROR, "extnodename has to be supplied");
652  methods = GetExtensibleNodeMethods(extnodename, false);
653 
654  local_node = (ExtensibleNode *) newNode(methods->node_size,
655  T_ExtensibleNode);
656  local_node->extnodename = extnodename;
657 
658  /* deserialize the private fields */
659  methods->nodeRead(local_node);
660 
661  READ_DONE();
662 }
663 
664 
665 /*
666  * parseNodeString
667  *
668  * Given a character string representing a node tree, parseNodeString creates
669  * the internal node structure.
670  *
671  * The string to be read must already have been loaded into pg_strtok().
672  */
673 Node *
675 {
676  void *return_value;
677 
679 
680  /* Guard against stack overflow due to overly complex expressions */
682 
683  token = pg_strtok(&length);
684 
685 #define MATCH(tokname, namelen) \
686  (length == namelen && memcmp(token, tokname, namelen) == 0)
687 
688  if (false)
689  ;
690 #include "readfuncs.switch.c"
691  else
692  {
693  elog(ERROR, "badly formatted node string \"%.32s\"...", token);
694  return_value = NULL; /* keep compiler quiet */
695  }
696 
697  return (Node *) return_value;
698 }
699 
700 
701 /*
702  * readDatum
703  *
704  * Given a string representation of a constant, recreate the appropriate
705  * Datum. The string representation embeds length info, but not byValue,
706  * so we must be told that.
707  */
708 Datum
709 readDatum(bool typbyval)
710 {
711  Size length,
712  i;
713  int tokenLength;
714  const char *token;
715  Datum res;
716  char *s;
717 
718  /*
719  * read the actual length of the value
720  */
721  token = pg_strtok(&tokenLength);
722  length = atoui(token);
723 
724  token = pg_strtok(&tokenLength); /* read the '[' */
725  if (token == NULL || token[0] != '[')
726  elog(ERROR, "expected \"[\" to start datum, but got \"%s\"; length = %zu",
727  token ? token : "[NULL]", length);
728 
729  if (typbyval)
730  {
731  if (length > (Size) sizeof(Datum))
732  elog(ERROR, "byval datum but length = %zu", length);
733  res = (Datum) 0;
734  s = (char *) (&res);
735  for (i = 0; i < (Size) sizeof(Datum); i++)
736  {
737  token = pg_strtok(&tokenLength);
738  s[i] = (char) atoi(token);
739  }
740  }
741  else if (length <= 0)
742  res = (Datum) NULL;
743  else
744  {
745  s = (char *) palloc(length);
746  for (i = 0; i < length; i++)
747  {
748  token = pg_strtok(&tokenLength);
749  s[i] = (char) atoi(token);
750  }
751  res = PointerGetDatum(s);
752  }
753 
754  token = pg_strtok(&tokenLength); /* read the ']' */
755  if (token == NULL || token[0] != ']')
756  elog(ERROR, "expected \"]\" to end datum, but got \"%s\"; length = %zu",
757  token ? token : "[NULL]", length);
758 
759  return res;
760 }
761 
762 /*
763  * common implementation for scalar-array-reading functions
764  *
765  * The data format is either "<>" for a NULL pointer (in which case numCols
766  * is ignored) or "(item item item)" where the number of items must equal
767  * numCols. The convfunc must be okay with stopping at whitespace or a
768  * right parenthesis, since pg_strtok won't null-terminate the token.
769  */
770 #define READ_SCALAR_ARRAY(fnname, datatype, convfunc) \
771 datatype * \
772 fnname(int numCols) \
773 { \
774  datatype *vals; \
775  READ_TEMP_LOCALS(); \
776  token = pg_strtok(&length); \
777  if (token == NULL) \
778  elog(ERROR, "incomplete scalar array"); \
779  if (length == 0) \
780  return NULL; /* it was "<>", so return NULL pointer */ \
781  if (length != 1 || token[0] != '(') \
782  elog(ERROR, "unrecognized token: \"%.*s\"", length, token); \
783  vals = (datatype *) palloc(numCols * sizeof(datatype)); \
784  for (int i = 0; i < numCols; i++) \
785  { \
786  token = pg_strtok(&length); \
787  if (token == NULL || token[0] == ')') \
788  elog(ERROR, "incomplete scalar array"); \
789  vals[i] = convfunc(token); \
790  } \
791  token = pg_strtok(&length); \
792  if (token == NULL || length != 1 || token[0] != ')') \
793  elog(ERROR, "incomplete scalar array"); \
794  return vals; \
795 }
796 
797 /*
798  * Note: these functions are exported in nodes.h for possible use by
799  * extensions, so don't mess too much with their names or API.
800  */
803 /* outfuncs.c has writeIndexCols, but we don't yet need that here */
804 /* READ_SCALAR_ARRAY(readIndexCols, Index, atoui) */
805 READ_SCALAR_ARRAY(readIntCols, int, atoi)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:739
signed short int16
Definition: c.h:429
size_t Size
Definition: c.h:541
#define ERROR
Definition: elog.h:35
const char * name
Definition: encode.c:561
const ExtensibleNodeMethods * GetExtensibleNodeMethods(const char *extnodename, bool missing_ok)
Definition: extensible.c:125
long val
Definition: informix.c:664
int i
Definition: isn.c:73
char * pstrdup(const char *in)
Definition: mcxt.c:1483
void * palloc(Size size)
Definition: mcxt.c:1199
#define newNode(size, tag)
Definition: nodes.h:155
int * readIntCols(int numCols)
bool * readBoolCols(int numCols)
Oid * readOidCols(int numCols)
int16 * readAttrNumberCols(int numCols)
JoinType
Definition: nodes.h:288
@ AEXPR_BETWEEN
Definition: parsenodes.h:288
@ AEXPR_NULLIF
Definition: parsenodes.h:283
@ AEXPR_NOT_DISTINCT
Definition: parsenodes.h:282
@ AEXPR_BETWEEN_SYM
Definition: parsenodes.h:290
@ AEXPR_NOT_BETWEEN_SYM
Definition: parsenodes.h:291
@ AEXPR_ILIKE
Definition: parsenodes.h:286
@ AEXPR_IN
Definition: parsenodes.h:284
@ AEXPR_NOT_BETWEEN
Definition: parsenodes.h:289
@ AEXPR_DISTINCT
Definition: parsenodes.h:281
@ AEXPR_SIMILAR
Definition: parsenodes.h:287
@ AEXPR_LIKE
Definition: parsenodes.h:285
@ AEXPR_OP
Definition: parsenodes.h:278
@ AEXPR_OP_ANY
Definition: parsenodes.h:279
@ AEXPR_OP_ALL
Definition: parsenodes.h:280
RTEKind
Definition: parsenodes.h:1010
@ RTE_JOIN
Definition: parsenodes.h:1013
@ RTE_CTE
Definition: parsenodes.h:1017
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1018
@ RTE_VALUES
Definition: parsenodes.h:1016
@ RTE_SUBQUERY
Definition: parsenodes.h:1012
@ RTE_RESULT
Definition: parsenodes.h:1019
@ RTE_FUNCTION
Definition: parsenodes.h:1014
@ RTE_TABLEFUNC
Definition: parsenodes.h:1015
@ RTE_RELATION
Definition: parsenodes.h:1011
@ CONSTR_FOREIGN
Definition: parsenodes.h:2317
@ CONSTR_ATTR_DEFERRED
Definition: parsenodes.h:2320
@ CONSTR_IDENTITY
Definition: parsenodes.h:2311
@ CONSTR_UNIQUE
Definition: parsenodes.h:2315
@ CONSTR_ATTR_NOT_DEFERRABLE
Definition: parsenodes.h:2319
@ CONSTR_DEFAULT
Definition: parsenodes.h:2310
@ CONSTR_NOTNULL
Definition: parsenodes.h:2309
@ CONSTR_ATTR_IMMEDIATE
Definition: parsenodes.h:2321
@ CONSTR_CHECK
Definition: parsenodes.h:2313
@ CONSTR_NULL
Definition: parsenodes.h:2307
@ CONSTR_GENERATED
Definition: parsenodes.h:2312
@ CONSTR_EXCLUSION
Definition: parsenodes.h:2316
@ CONSTR_ATTR_DEFERRABLE
Definition: parsenodes.h:2318
@ CONSTR_PRIMARY
Definition: parsenodes.h:2314
void check_stack_depth(void)
Definition: postgres.c:3440
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
uintptr_t Datum
Definition: postgres.h:412
unsigned int Oid
Definition: postgres_ext.h:31
#define atooid(x)
Definition: postgres_ext.h:42
@ AND_EXPR
Definition: primnodes.h:758
@ OR_EXPR
Definition: primnodes.h:758
@ NOT_EXPR
Definition: primnodes.h:758
const char * pg_strtok(int *length)
Definition: read.c:153
char * debackslash(const char *token, int length)
Definition: read.c:214
void * nodeRead(const char *token, int tok_len)
Definition: read.c:320
static A_Const * _readA_Const(void)
Definition: readfuncs.c:304
#define READ_INT_FIELD(fldname)
Definition: readfuncs.c:60
static char * nullable_string(const char *token, int length)
Definition: readfuncs.c:182
#define READ_UINT_FIELD(fldname)
Definition: readfuncs.c:66
#define READ_NODE_FIELD(fldname)
Definition: readfuncs.c:135
static RangeTblEntry * _readRangeTblEntry(void)
Definition: readfuncs.c:460
#define READ_CHAR_FIELD(fldname)
Definition: readfuncs.c:90
static Constraint * _readConstraint(void)
Definition: readfuncs.c:327
#define READ_SCALAR_ARRAY(fnname, datatype, convfunc)
Definition: readfuncs.c:770
Node * parseNodeString(void)
Definition: readfuncs.c:674
static Const * _readConst(void)
Definition: readfuncs.c:259
#define READ_BITMAPSET_FIELD(fldname)
Definition: readfuncs.c:141
#define READ_OID_FIELD(fldname)
Definition: readfuncs.c:84
#define READ_LOCATION_FIELD(fldname)
Definition: readfuncs.c:127
#define READ_STRING_FIELD(fldname)
Definition: readfuncs.c:115
static BoolExpr * _readBoolExpr(void)
Definition: readfuncs.c:281
#define READ_FLOAT_FIELD(fldname)
Definition: readfuncs.c:103
Datum readDatum(bool typbyval)
Definition: readfuncs.c:709
static ExtensibleNode * _readExtensibleNode(void)
Definition: readfuncs.c:638
#define READ_BOOL_FIELD(fldname)
Definition: readfuncs.c:109
Bitmapset * readBitmapset(void)
Definition: readfuncs.c:245
#define atoui(x)
Definition: readfuncs.c:177
#define strtobool(x)
Definition: readfuncs.c:179
static A_Expr * _readA_Expr(void)
Definition: readfuncs.c:551
static Bitmapset * _readBitmapset(void)
Definition: readfuncs.c:203
#define READ_ENUM_FIELD(fldname, enumtype)
Definition: readfuncs.c:97
#define READ_TEMP_LOCALS()
Definition: readfuncs.c:50
#define READ_LOCALS(nodeTypeName)
Definition: readfuncs.c:55
#define READ_DONE()
Definition: readfuncs.c:167
static const struct fns functions
Definition: regcomp.c:357
void(* nodeRead)(struct ExtensibleNode *node)
Definition: extensible.h:72
const char * extnodename
Definition: extensible.h:37
Definition: nodes.h:118
List * coltypmods
Definition: primnodes.h:107
List * coltypes
Definition: primnodes.h:106
List * colcollations
Definition: primnodes.h:108