Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * readfuncs.c
4 : : * Reader functions for Postgres tree nodes.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, 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 : : * debug_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 "miscadmin.h"
30 : : #include "nodes/bitmapset.h"
31 : : #include "nodes/readfuncs.h"
32 : :
33 : :
34 : : /*
35 : : * Macros to simplify reading of different kinds of fields. Use these
36 : : * wherever possible to reduce the chance for silly typos. Note that these
37 : : * hard-wire conventions about the names of the local variables in a Read
38 : : * routine.
39 : : */
40 : :
41 : : /* Macros for declaring appropriate local variables */
42 : :
43 : : /* A few guys need only local_node */
44 : : #define READ_LOCALS_NO_FIELDS(nodeTypeName) \
45 : : nodeTypeName *local_node = makeNode(nodeTypeName)
46 : :
47 : : /* And a few guys need only the pg_strtok support fields */
48 : : #define READ_TEMP_LOCALS() \
49 : : const char *token; \
50 : : int length
51 : :
52 : : /* ... but most need both */
53 : : #define READ_LOCALS(nodeTypeName) \
54 : : READ_LOCALS_NO_FIELDS(nodeTypeName); \
55 : : READ_TEMP_LOCALS()
56 : :
57 : : /* Read an integer field (anything written as ":fldname %d") */
58 : : #define READ_INT_FIELD(fldname) \
59 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
60 : : token = pg_strtok(ctx, &length); /* get field value */ \
61 : : local_node->fldname = atoi(token)
62 : :
63 : : /* Read an unsigned integer field (anything written as ":fldname %u") */
64 : : #define READ_UINT_FIELD(fldname) \
65 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
66 : : token = pg_strtok(ctx, &length); /* get field value */ \
67 : : local_node->fldname = atoui(token)
68 : :
69 : : /* Read a signed integer field (anything written using INT64_FORMAT) */
70 : : #define READ_INT64_FIELD(fldname) \
71 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
72 : : token = pg_strtok(ctx, &length); /* get field value */ \
73 : : local_node->fldname = strtoi64(token, NULL, 10)
74 : :
75 : : /* Read an unsigned integer field (anything written using UINT64_FORMAT) */
76 : : #define READ_UINT64_FIELD(fldname) \
77 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
78 : : token = pg_strtok(ctx, &length); /* get field value */ \
79 : : local_node->fldname = strtou64(token, NULL, 10)
80 : :
81 : : /* Read a long integer field (anything written as ":fldname %ld") */
82 : : #define READ_LONG_FIELD(fldname) \
83 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
84 : : token = pg_strtok(ctx, &length); /* get field value */ \
85 : : local_node->fldname = atol(token)
86 : :
87 : : /* Read an OID field (don't hard-wire assumption that OID is same as uint) */
88 : : #define READ_OID_FIELD(fldname) \
89 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
90 : : token = pg_strtok(ctx, &length); /* get field value */ \
91 : : local_node->fldname = atooid(token)
92 : :
93 : : /* Read a char field (ie, one ascii character) */
94 : : #define READ_CHAR_FIELD(fldname) \
95 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
96 : : token = pg_strtok(ctx, &length); /* get field value */ \
97 : : /* avoid overhead of calling debackslash() for one char */ \
98 : : local_node->fldname = (length == 0) ? '\0' : (token[0] == '\\' ? token[1] : token[0])
99 : :
100 : : /* Read an enumerated-type field that was written as an integer code */
101 : : #define READ_ENUM_FIELD(fldname, enumtype) \
102 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
103 : : token = pg_strtok(ctx, &length); /* get field value */ \
104 : : local_node->fldname = (enumtype) atoi(token)
105 : :
106 : : /* Read a float field */
107 : : #define READ_FLOAT_FIELD(fldname) \
108 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
109 : : token = pg_strtok(ctx, &length); /* get field value */ \
110 : : local_node->fldname = atof(token)
111 : :
112 : : /* Read a boolean field */
113 : : #define READ_BOOL_FIELD(fldname) \
114 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
115 : : token = pg_strtok(ctx, &length); /* get field value */ \
116 : : local_node->fldname = strtobool(token)
117 : :
118 : : /* Read a character-string field */
119 : : #define READ_STRING_FIELD(fldname) \
120 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
121 : : token = pg_strtok(ctx, &length); /* get field value */ \
122 : : local_node->fldname = nullable_string(token, length)
123 : :
124 : : /* Read a parse location field (and possibly throw away the value) */
125 : : #ifdef DEBUG_NODE_TESTS_ENABLED
126 : : #define READ_LOCATION_FIELD(fldname) \
127 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
128 : : token = pg_strtok(ctx, &length); /* get field value */ \
129 : : local_node->fldname = ctx->restore_location_fields ? atoi(token) : -1
130 : : #else
131 : : #define READ_LOCATION_FIELD(fldname) \
132 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
133 : : token = pg_strtok(ctx, &length); /* get field value */ \
134 : : (void) token; /* in case not used elsewhere */ \
135 : : local_node->fldname = -1 /* set field to "unknown" */
136 : : #endif
137 : :
138 : : /* Read a Node field */
139 : : #define READ_NODE_FIELD(fldname) \
140 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
141 : : (void) token; /* in case not used elsewhere */ \
142 : : local_node->fldname = nodeRead(ctx, NULL, 0)
143 : :
144 : : /* Read a bitmapset field */
145 : : #define READ_BITMAPSET_FIELD(fldname) \
146 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
147 : : (void) token; /* in case not used elsewhere */ \
148 : : local_node->fldname = _readBitmapset(ctx)
149 : :
150 : : /* Read an attribute number array */
151 : : #define READ_ATTRNUMBER_ARRAY(fldname, len) \
152 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
153 : : local_node->fldname = readAttrNumberCols(ctx, len)
154 : :
155 : : /* Read an oid array */
156 : : #define READ_OID_ARRAY(fldname, len) \
157 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
158 : : local_node->fldname = readOidCols(ctx, len)
159 : :
160 : : /* Read an int array */
161 : : #define READ_INT_ARRAY(fldname, len) \
162 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
163 : : local_node->fldname = readIntCols(ctx, len)
164 : :
165 : : /* Read a bool array */
166 : : #define READ_BOOL_ARRAY(fldname, len) \
167 : : token = pg_strtok(ctx, &length); /* skip :fldname */ \
168 : : local_node->fldname = readBoolCols(ctx, len)
169 : :
170 : : /* Routine exit */
171 : : #define READ_DONE() \
172 : : return local_node
173 : :
174 : :
175 : : /*
176 : : * NOTE: use atoi() to read values written with %d, or atoui() to read
177 : : * values written with %u in outfuncs.c. An exception is OID values,
178 : : * for which use atooid(). (As of 7.1, outfuncs.c writes OIDs as %u,
179 : : * but this will probably change in the future.)
180 : : */
181 : : #define atoui(x) ((unsigned int) strtoul((x), NULL, 10))
182 : :
183 : : #define strtobool(x) ((*(x) == 't') ? true : false)
184 : :
185 : : static char *
186 : 12981944 : nullable_string(const char *token, int length)
187 : : {
188 : : /* outToken emits <> for NULL, and pg_strtok makes that an empty string */
189 [ + + ]: 12981944 : if (length == 0)
190 : 6752860 : return NULL;
191 : : /* outToken emits "" for empty string */
192 [ + + + + : 6229084 : if (length == 2 && token[0] == '"' && token[1] == '"')
+ - ]
193 : 124 : return pstrdup("");
194 : : /* otherwise, we must remove protective backslashes added by outToken */
195 : 6228960 : return debackslash(token, length);
196 : : }
197 : :
198 : :
199 : : /*
200 : : * _readBitmapset
201 : : *
202 : : * Note: this code is used in contexts where we know that a Bitmapset
203 : : * is expected. There is equivalent code in nodeRead() that can read a
204 : : * Bitmapset when we come across one in other contexts.
205 : : */
206 : : static Bitmapset *
207 : 14374899 : _readBitmapset(ReadNodeContext *ctx)
208 : : {
209 : 14374899 : Bitmapset *result = NULL;
210 : :
211 : : READ_TEMP_LOCALS();
212 : :
213 : 14374899 : token = pg_strtok(ctx, &length);
214 [ - + ]: 14374899 : if (token == NULL)
215 [ # # ]: 0 : elog(ERROR, "incomplete Bitmapset structure");
216 [ + - - + ]: 14374899 : if (length != 1 || token[0] != '(')
217 [ # # ]: 0 : elog(ERROR, "unrecognized token: \"%.*s\"", length, token);
218 : :
219 : 14374899 : token = pg_strtok(ctx, &length);
220 [ - + ]: 14374899 : if (token == NULL)
221 [ # # ]: 0 : elog(ERROR, "incomplete Bitmapset structure");
222 [ + - - + ]: 14374899 : if (length != 1 || token[0] != 'b')
223 [ # # ]: 0 : elog(ERROR, "unrecognized token: \"%.*s\"", length, token);
224 : :
225 : : for (;;)
226 : 3997586 : {
227 : : int val;
228 : : char *endptr;
229 : :
230 : 18372485 : token = pg_strtok(ctx, &length);
231 [ - + ]: 18372485 : if (token == NULL)
232 [ # # ]: 0 : elog(ERROR, "unterminated Bitmapset structure");
233 [ + + + + ]: 18372485 : if (length == 1 && token[0] == ')')
234 : 14374899 : break;
235 : 3997586 : val = (int) strtol(token, &endptr, 10);
236 [ - + ]: 3997586 : if (endptr != token + length)
237 [ # # ]: 0 : elog(ERROR, "unrecognized integer: \"%.*s\"", length, token);
238 : 3997586 : result = bms_add_member(result, val);
239 : : }
240 : :
241 : 14374899 : return result;
242 : : }
243 : :
244 : : /*
245 : : * We export this function for use by extensions that define extensible nodes.
246 : : * That's somewhat historical, though, because calling nodeRead() will work.
247 : : */
248 : : Bitmapset *
249 : 0 : readBitmapset(ReadNodeContext *ctx)
250 : : {
251 : 0 : return _readBitmapset(ctx);
252 : : }
253 : :
254 : : #include "readfuncs.funcs.c"
255 : :
256 : :
257 : : /*
258 : : * Support functions for nodes with custom_read_write attribute or
259 : : * special_read_write attribute
260 : : */
261 : :
262 : : static Const *
263 : 2123804 : _readConst(ReadNodeContext *ctx)
264 : : {
265 : 2123804 : READ_LOCALS(Const);
266 : :
267 : 2123804 : READ_OID_FIELD(consttype);
268 : 2123804 : READ_INT_FIELD(consttypmod);
269 : 2123804 : READ_OID_FIELD(constcollid);
270 : 2123804 : READ_INT_FIELD(constlen);
271 : 2123804 : READ_BOOL_FIELD(constbyval);
272 : 2123804 : READ_BOOL_FIELD(constisnull);
273 [ + + ]: 2123804 : READ_LOCATION_FIELD(location);
274 : :
275 : 2123804 : token = pg_strtok(ctx, &length); /* skip :constvalue */
276 [ + + ]: 2123804 : if (local_node->constisnull)
277 : 194210 : token = pg_strtok(ctx, &length); /* skip "<>" */
278 : : else
279 : 1929594 : local_node->constvalue = readDatum(ctx, local_node->constbyval);
280 : :
281 : 2123804 : READ_DONE();
282 : : }
283 : :
284 : : static BoolExpr *
285 : 297208 : _readBoolExpr(ReadNodeContext *ctx)
286 : : {
287 : 297208 : READ_LOCALS(BoolExpr);
288 : :
289 : : /* do-it-yourself enum representation */
290 : 297208 : token = pg_strtok(ctx, &length); /* skip :boolop */
291 : 297208 : token = pg_strtok(ctx, &length); /* get field value */
292 [ + + + + ]: 297208 : if (length == 3 && strncmp(token, "and", 3) == 0)
293 : 207700 : local_node->boolop = AND_EXPR;
294 [ + + + - ]: 89508 : else if (length == 2 && strncmp(token, "or", 2) == 0)
295 : 39587 : local_node->boolop = OR_EXPR;
296 [ + - + - ]: 49921 : else if (length == 3 && strncmp(token, "not", 3) == 0)
297 : 49921 : local_node->boolop = NOT_EXPR;
298 : : else
299 [ # # ]: 0 : elog(ERROR, "unrecognized boolop \"%.*s\"", length, token);
300 : :
301 : 297208 : READ_NODE_FIELD(args);
302 [ + + ]: 297208 : READ_LOCATION_FIELD(location);
303 : :
304 : 297208 : READ_DONE();
305 : : }
306 : :
307 : : static A_Const *
308 : 937756 : _readA_Const(ReadNodeContext *ctx)
309 : : {
310 : 937756 : READ_LOCALS(A_Const);
311 : :
312 : : /* We expect either NULL or :val here */
313 : 937756 : token = pg_strtok(ctx, &length);
314 [ + - + + ]: 937756 : if (length == 4 && strncmp(token, "NULL", 4) == 0)
315 : 54946 : local_node->isnull = true;
316 : : else
317 : : {
318 : 882810 : union ValUnion *tmp = nodeRead(ctx, NULL, 0);
319 : :
320 : : /* To forestall valgrind complaints, copy only the valid data */
321 [ + + + + : 882810 : switch (nodeTag(tmp))
+ - ]
322 : : {
323 : 292350 : case T_Integer:
324 : 292350 : memcpy(&local_node->val, tmp, sizeof(Integer));
325 : 292350 : break;
326 : 8180 : case T_Float:
327 : 8180 : memcpy(&local_node->val, tmp, sizeof(Float));
328 : 8180 : break;
329 : 40135 : case T_Boolean:
330 : 40135 : memcpy(&local_node->val, tmp, sizeof(Boolean));
331 : 40135 : break;
332 : 539410 : case T_String:
333 : 539410 : memcpy(&local_node->val, tmp, sizeof(String));
334 : 539410 : break;
335 : 2735 : case T_BitString:
336 : 2735 : memcpy(&local_node->val, tmp, sizeof(BitString));
337 : 2735 : break;
338 : 0 : default:
339 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
340 : : (int) nodeTag(tmp));
341 : : break;
342 : : }
343 : : }
344 : :
345 [ + - ]: 937756 : READ_LOCATION_FIELD(location);
346 : :
347 : 937756 : READ_DONE();
348 : : }
349 : :
350 : : static RangeTblEntry *
351 : 1211463 : _readRangeTblEntry(ReadNodeContext *ctx)
352 : : {
353 : 1211463 : READ_LOCALS(RangeTblEntry);
354 : :
355 : 1211463 : READ_NODE_FIELD(alias);
356 : 1211463 : READ_NODE_FIELD(eref);
357 : 1211463 : READ_ENUM_FIELD(rtekind, RTEKind);
358 : :
359 [ + + + + : 1211463 : switch (local_node->rtekind)
+ + + + +
+ + - ]
360 : : {
361 : 722306 : case RTE_RELATION:
362 : 722306 : READ_OID_FIELD(relid);
363 : 722306 : READ_BOOL_FIELD(inh);
364 [ + - - + ]: 722306 : READ_CHAR_FIELD(relkind);
365 : 722306 : READ_INT_FIELD(rellockmode);
366 : 722306 : READ_UINT_FIELD(perminfoindex);
367 : 722306 : READ_NODE_FIELD(tablesample);
368 : 722306 : break;
369 : 116961 : case RTE_SUBQUERY:
370 : 116961 : READ_NODE_FIELD(subquery);
371 : 116961 : READ_BOOL_FIELD(security_barrier);
372 : : /* we re-use these RELATION fields, too: */
373 : 116961 : READ_OID_FIELD(relid);
374 : 116961 : READ_BOOL_FIELD(inh);
375 [ + + - + ]: 116961 : READ_CHAR_FIELD(relkind);
376 : 116961 : READ_INT_FIELD(rellockmode);
377 : 116961 : READ_UINT_FIELD(perminfoindex);
378 : 116961 : break;
379 : 143773 : case RTE_JOIN:
380 : 143773 : READ_ENUM_FIELD(jointype, JoinType);
381 : 143773 : READ_INT_FIELD(joinmergedcols);
382 : 143773 : READ_NODE_FIELD(joinaliasvars);
383 : 143773 : READ_NODE_FIELD(joinleftcols);
384 : 143773 : READ_NODE_FIELD(joinrightcols);
385 : 143773 : READ_NODE_FIELD(join_using_alias);
386 : 143773 : break;
387 : 69645 : case RTE_FUNCTION:
388 : 69645 : READ_NODE_FIELD(functions);
389 : 69645 : READ_BOOL_FIELD(funcordinality);
390 : 69645 : break;
391 : 1154 : case RTE_TABLEFUNC:
392 : 1154 : READ_NODE_FIELD(tablefunc);
393 : : /* The RTE must have a copy of the column type info, if any */
394 [ + + ]: 1154 : if (local_node->tablefunc)
395 : : {
396 : 670 : TableFunc *tf = local_node->tablefunc;
397 : :
398 : 670 : local_node->coltypes = tf->coltypes;
399 : 670 : local_node->coltypmods = tf->coltypmods;
400 : 670 : local_node->colcollations = tf->colcollations;
401 : : }
402 : 1154 : break;
403 : 15558 : case RTE_VALUES:
404 : 15558 : READ_NODE_FIELD(values_lists);
405 : 15558 : READ_NODE_FIELD(coltypes);
406 : 15558 : READ_NODE_FIELD(coltypmods);
407 : 15558 : READ_NODE_FIELD(colcollations);
408 : 15558 : break;
409 : 7966 : case RTE_CTE:
410 : 7966 : READ_STRING_FIELD(ctename);
411 : 7966 : READ_UINT_FIELD(ctelevelsup);
412 : 7966 : READ_BOOL_FIELD(self_reference);
413 : 7966 : READ_NODE_FIELD(coltypes);
414 : 7966 : READ_NODE_FIELD(coltypmods);
415 : 7966 : READ_NODE_FIELD(colcollations);
416 : 7966 : break;
417 : 700 : case RTE_NAMEDTUPLESTORE:
418 : 700 : READ_STRING_FIELD(enrname);
419 : 700 : READ_FLOAT_FIELD(enrtuples);
420 : 700 : READ_NODE_FIELD(coltypes);
421 : 700 : READ_NODE_FIELD(coltypmods);
422 : 700 : READ_NODE_FIELD(colcollations);
423 : : /* we re-use these RELATION fields, too: */
424 : 700 : READ_OID_FIELD(relid);
425 : 700 : break;
426 : 108 : case RTE_GRAPH_TABLE:
427 : 108 : READ_NODE_FIELD(graph_pattern);
428 : 108 : READ_NODE_FIELD(graph_table_columns);
429 : : /* we re-use these RELATION fields, too: */
430 : 108 : READ_OID_FIELD(relid);
431 [ + - - + ]: 108 : READ_CHAR_FIELD(relkind);
432 : 108 : READ_INT_FIELD(rellockmode);
433 : 108 : READ_UINT_FIELD(perminfoindex);
434 : 108 : break;
435 : 125062 : case RTE_RESULT:
436 : : /* no extra fields */
437 : 125062 : break;
438 : 8230 : case RTE_GROUP:
439 : 8230 : READ_NODE_FIELD(groupexprs);
440 : 8230 : break;
441 : 0 : default:
442 [ # # ]: 0 : elog(ERROR, "unrecognized RTE kind: %d",
443 : : (int) local_node->rtekind);
444 : : break;
445 : : }
446 : :
447 : 1211463 : READ_BOOL_FIELD(lateral);
448 : 1211463 : READ_BOOL_FIELD(inFromCl);
449 : 1211463 : READ_NODE_FIELD(securityQuals);
450 : :
451 : 1211463 : READ_DONE();
452 : : }
453 : :
454 : : static A_Expr *
455 : 429867 : _readA_Expr(ReadNodeContext *ctx)
456 : : {
457 : 429867 : READ_LOCALS(A_Expr);
458 : :
459 : 429867 : token = pg_strtok(ctx, &length);
460 : :
461 [ + + + + ]: 429867 : if (length == 3 && strncmp(token, "ANY", 3) == 0)
462 : : {
463 : 10753 : local_node->kind = AEXPR_OP_ANY;
464 : 10753 : READ_NODE_FIELD(name);
465 : : }
466 [ + + + - ]: 419114 : else if (length == 3 && strncmp(token, "ALL", 3) == 0)
467 : : {
468 : 96 : local_node->kind = AEXPR_OP_ALL;
469 : 96 : READ_NODE_FIELD(name);
470 : : }
471 [ + + + - ]: 419018 : else if (length == 8 && strncmp(token, "DISTINCT", 8) == 0)
472 : : {
473 : 651 : local_node->kind = AEXPR_DISTINCT;
474 : 651 : READ_NODE_FIELD(name);
475 : : }
476 [ + + + - ]: 418367 : else if (length == 12 && strncmp(token, "NOT_DISTINCT", 12) == 0)
477 : : {
478 : 88 : local_node->kind = AEXPR_NOT_DISTINCT;
479 : 88 : READ_NODE_FIELD(name);
480 : : }
481 [ + + + - ]: 418279 : else if (length == 6 && strncmp(token, "NULLIF", 6) == 0)
482 : : {
483 : 587 : local_node->kind = AEXPR_NULLIF;
484 : 587 : READ_NODE_FIELD(name);
485 : : }
486 [ + + + - ]: 417692 : else if (length == 2 && strncmp(token, "IN", 2) == 0)
487 : : {
488 : 16095 : local_node->kind = AEXPR_IN;
489 : 16095 : READ_NODE_FIELD(name);
490 : : }
491 [ + + + - ]: 401597 : else if (length == 4 && strncmp(token, "LIKE", 4) == 0)
492 : : {
493 : 1602 : local_node->kind = AEXPR_LIKE;
494 : 1602 : READ_NODE_FIELD(name);
495 : : }
496 [ + + + + ]: 399995 : else if (length == 5 && strncmp(token, "ILIKE", 5) == 0)
497 : : {
498 : 136 : local_node->kind = AEXPR_ILIKE;
499 : 136 : READ_NODE_FIELD(name);
500 : : }
501 [ + + + + ]: 399859 : else if (length == 7 && strncmp(token, "SIMILAR", 7) == 0)
502 : : {
503 : 82 : local_node->kind = AEXPR_SIMILAR;
504 : 82 : READ_NODE_FIELD(name);
505 : : }
506 [ + + + - ]: 399777 : else if (length == 7 && strncmp(token, "BETWEEN", 7) == 0)
507 : : {
508 : 370 : local_node->kind = AEXPR_BETWEEN;
509 : 370 : READ_NODE_FIELD(name);
510 : : }
511 [ + + + + ]: 399407 : else if (length == 11 && strncmp(token, "NOT_BETWEEN", 11) == 0)
512 : : {
513 : 8 : local_node->kind = AEXPR_NOT_BETWEEN;
514 : 8 : READ_NODE_FIELD(name);
515 : : }
516 [ + + + - ]: 399399 : else if (length == 11 && strncmp(token, "BETWEEN_SYM", 11) == 0)
517 : : {
518 : 8 : local_node->kind = AEXPR_BETWEEN_SYM;
519 : 8 : READ_NODE_FIELD(name);
520 : : }
521 [ + + + - ]: 399391 : else if (length == 15 && strncmp(token, "NOT_BETWEEN_SYM", 15) == 0)
522 : : {
523 : 8 : local_node->kind = AEXPR_NOT_BETWEEN_SYM;
524 : 8 : READ_NODE_FIELD(name);
525 : : }
526 [ + - + - ]: 399383 : else if (length == 5 && strncmp(token, ":name", 5) == 0)
527 : : {
528 : 399383 : local_node->kind = AEXPR_OP;
529 : 399383 : local_node->name = nodeRead(ctx, NULL, 0);
530 : : }
531 : : else
532 [ # # ]: 0 : elog(ERROR, "unrecognized A_Expr kind: \"%.*s\"", length, token);
533 : :
534 : 429867 : READ_NODE_FIELD(lexpr);
535 : 429867 : READ_NODE_FIELD(rexpr);
536 [ + - ]: 429867 : READ_LOCATION_FIELD(rexpr_list_start);
537 [ + - ]: 429867 : READ_LOCATION_FIELD(rexpr_list_end);
538 [ + - ]: 429867 : READ_LOCATION_FIELD(location);
539 : :
540 : 429867 : READ_DONE();
541 : : }
542 : :
543 : : static ExtensibleNode *
544 : 0 : _readExtensibleNode(ReadNodeContext *ctx)
545 : : {
546 : : const ExtensibleNodeMethods *methods;
547 : : ExtensibleNode *local_node;
548 : : const char *extnodename;
549 : :
550 : : READ_TEMP_LOCALS();
551 : :
552 : 0 : token = pg_strtok(ctx, &length); /* skip :extnodename */
553 : 0 : token = pg_strtok(ctx, &length); /* get extnodename */
554 : :
555 : 0 : extnodename = nullable_string(token, length);
556 [ # # ]: 0 : if (!extnodename)
557 [ # # ]: 0 : elog(ERROR, "extnodename has to be supplied");
558 : 0 : methods = GetExtensibleNodeMethods(extnodename, false);
559 : :
560 : 0 : local_node = (ExtensibleNode *) newNode(methods->node_size,
561 : : T_ExtensibleNode);
562 : 0 : local_node->extnodename = extnodename;
563 : :
564 : : /* deserialize the private fields */
565 : 0 : methods->nodeRead(ctx, local_node);
566 : :
567 : 0 : READ_DONE();
568 : : }
569 : :
570 : :
571 : : /*
572 : : * parseNodeString
573 : : *
574 : : * Given a character string representing a node tree, parseNodeString creates
575 : : * the internal node structure.
576 : : */
577 : : Node *
578 : 34288566 : parseNodeString(ReadNodeContext *ctx)
579 : : {
580 : : READ_TEMP_LOCALS();
581 : :
582 : : /* Guard against stack overflow due to overly complex expressions */
583 : 34288566 : check_stack_depth();
584 : :
585 : 34288566 : token = pg_strtok(ctx, &length);
586 : :
587 : : #define MATCH(tokname, namelen) \
588 : : (length == namelen && memcmp(token, tokname, namelen) == 0)
589 : :
590 : : #include "readfuncs.switch.c"
591 : :
592 [ # # ]: 0 : elog(ERROR, "badly formatted node string \"%.32s\"...", token);
593 : : return NULL; /* keep compiler quiet */
594 : : }
595 : :
596 : :
597 : : /*
598 : : * readDatum
599 : : *
600 : : * Given a string representation of a constant, recreate the appropriate
601 : : * Datum. The string representation embeds length info, but not byValue,
602 : : * so we must be told that.
603 : : */
604 : : Datum
605 : 1929594 : readDatum(ReadNodeContext *ctx, bool typbyval)
606 : : {
607 : : Size length;
608 : : int tokenLength;
609 : : const char *token;
610 : : Datum res;
611 : : char *s;
612 : :
613 : : /*
614 : : * read the actual length of the value
615 : : */
616 : 1929594 : token = pg_strtok(ctx, &tokenLength);
617 : 1929594 : length = atoui(token);
618 : :
619 : 1929594 : token = pg_strtok(ctx, &tokenLength); /* read the '[' */
620 [ + - - + ]: 1929594 : if (token == NULL || token[0] != '[')
621 [ # # # # ]: 0 : elog(ERROR, "expected \"[\" to start datum, but got \"%s\"; length = %zu",
622 : : token ? token : "[NULL]", length);
623 : :
624 [ + + ]: 1929594 : if (typbyval)
625 : : {
626 [ - + ]: 1281590 : if (length > (Size) sizeof(Datum))
627 [ # # ]: 0 : elog(ERROR, "byval datum but length = %zu", length);
628 : 1281590 : res = (Datum) 0;
629 : 1281590 : s = (char *) (&res);
630 [ + + ]: 11534310 : for (Size i = 0; i < (Size) sizeof(Datum); i++)
631 : : {
632 : 10252720 : token = pg_strtok(ctx, &tokenLength);
633 : 10252720 : s[i] = (char) atoi(token);
634 : : }
635 : : }
636 [ - + ]: 648004 : else if (length <= 0)
637 : 0 : res = (Datum) 0;
638 : : else
639 : : {
640 : 648004 : s = (char *) palloc(length);
641 [ + + ]: 80118134 : for (Size i = 0; i < length; i++)
642 : : {
643 : 79470130 : token = pg_strtok(ctx, &tokenLength);
644 : 79470130 : s[i] = (char) atoi(token);
645 : : }
646 : 648004 : res = PointerGetDatum(s);
647 : : }
648 : :
649 : 1929594 : token = pg_strtok(ctx, &tokenLength); /* read the ']' */
650 [ + - - + ]: 1929594 : if (token == NULL || token[0] != ']')
651 [ # # # # ]: 0 : elog(ERROR, "expected \"]\" to end datum, but got \"%s\"; length = %zu",
652 : : token ? token : "[NULL]", length);
653 : :
654 : 1929594 : return res;
655 : : }
656 : :
657 : : /*
658 : : * common implementation for scalar-array-reading functions
659 : : *
660 : : * The data format is either "<>" for a NULL pointer (in which case numCols
661 : : * is ignored) or "(item item item)" where the number of items must equal
662 : : * numCols. The convfunc must be okay with stopping at whitespace or a
663 : : * right parenthesis, since pg_strtok won't null-terminate the token.
664 : : */
665 : : #define READ_SCALAR_ARRAY(fnname, datatype, convfunc) \
666 : : datatype * \
667 : : fnname(ReadNodeContext *ctx, int numCols) \
668 : : { \
669 : : datatype *vals; \
670 : : READ_TEMP_LOCALS(); \
671 : : token = pg_strtok(ctx, &length); \
672 : : if (token == NULL) \
673 : : elog(ERROR, "incomplete scalar array"); \
674 : : if (length == 0) \
675 : : return NULL; /* it was "<>", so return NULL pointer */ \
676 : : if (length != 1 || token[0] != '(') \
677 : : elog(ERROR, "unrecognized token: \"%.*s\"", length, token); \
678 : : vals = palloc_array(datatype, numCols); \
679 : : for (int i = 0; i < numCols; i++) \
680 : : { \
681 : : token = pg_strtok(ctx, &length); \
682 : : if (token == NULL || token[0] == ')') \
683 : : elog(ERROR, "incomplete scalar array"); \
684 : : vals[i] = convfunc(token); \
685 : : } \
686 : : token = pg_strtok(ctx, &length); \
687 : : if (token == NULL || length != 1 || token[0] != ')') \
688 : : elog(ERROR, "incomplete scalar array"); \
689 : : return vals; \
690 : : }
691 : :
692 : : /*
693 : : * Note: these functions are exported in nodes.h for possible use by
694 : : * extensions, so don't mess too much with their names or API.
695 : : */
696 [ - + - - : 358167 : READ_SCALAR_ARRAY(readAttrNumberCols, int16, atoi)
+ + + - -
+ - - + -
- + - - +
+ + - + -
- + - - ]
697 [ - + - - : 445231 : READ_SCALAR_ARRAY(readOidCols, Oid, atooid)
+ + + - -
+ - - + -
- + - - +
+ + - + -
- + - - ]
698 : : /* outfuncs.c has writeIndexCols, but we don't yet need that here */
699 : : /* READ_SCALAR_ARRAY(readIndexCols, Index, atoui) */
700 [ - + - - : 10773 : READ_SCALAR_ARRAY(readIntCols, int, atoi)
- + + - -
+ - - + -
- + - - +
+ + - + -
- + - - ]
701 [ - + - - : 162480 : READ_SCALAR_ARRAY(readBoolCols, bool, strtobool)
+ + + - -
+ - - + -
- + - - +
+ + - + -
- + - - ]
|