Debian Codegraph @

src/include/nodes/primnodes.h

raw
1 /*-------------------------------------------------------------------------
2 *
3 * primnodes.h
4 * Definitions for "primitive" node types, those that are used in more
5 * than one of the parse/plan/execute stages of the query pipeline.
6 * Currently, these are mostly nodes for executable expressions
7 * and join trees.
8 *
9 *
10 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
12 *
13 * src/include/nodes/primnodes.h
14 *
15 *-------------------------------------------------------------------------
16 */
17 #ifndef PRIMNODES_H
18 #define PRIMNODES_H
19
20 #include "access/attnum.h"
21 #include "access/cmptype.h"
22 #include "nodes/bitmapset.h"
23 #include "nodes/pg_list.h"
24
25
26 typedef enum OverridingKind
27 {
28 OVERRIDING_NOT_SET = 0,
29 OVERRIDING_USER_VALUE,
30 OVERRIDING_SYSTEM_VALUE,
31 } OverridingKind;
32
33
34 /* ----------------------------------------------------------------
35 * node definitions
36 * ----------------------------------------------------------------
37 */
38
39 /*
40 * Alias -
41 * specifies an alias for a range variable; the alias might also
42 * specify renaming of columns within the table.
43 *
44 * Note: colnames is a list of String nodes. In Alias structs
45 * associated with RTEs, there may be entries corresponding to dropped
46 * columns; these are normally empty strings (""). See parsenodes.h for info.
47 */
48 typedef struct Alias
49 {
50 NodeTag type;
51 char *aliasname; /* aliased rel name (never qualified) */
52 List *colnames; /* optional list of column aliases */
53 } Alias;
54
55 /* What to do at commit time for temporary relations */
56 typedef enum OnCommitAction
57 {
58 ONCOMMIT_NOOP, /* No ON COMMIT clause (do nothing) */
59 ONCOMMIT_PRESERVE_ROWS, /* ON COMMIT PRESERVE ROWS (do nothing) */
60 ONCOMMIT_DELETE_ROWS, /* ON COMMIT DELETE ROWS */
61 ONCOMMIT_DROP, /* ON COMMIT DROP */
62 } OnCommitAction;
63
64 /*
65 * RangeVar - range variable, used in FROM clauses
66 *
67 * Also used to represent table names in utility statements; there, the alias
68 * field is not used, and inh tells whether to apply the operation
69 * recursively to child tables. In some contexts it is also useful to carry
70 * a TEMP table indication here.
71 */
72 typedef struct RangeVar
73 {
74 NodeTag type;
75
76 /* the catalog (database) name, or NULL */
77 char *catalogname;
78
79 /* the schema name, or NULL */
80 char *schemaname;
81
82 /* the relation/sequence name */
83 char *relname;
84
85 /* expand rel by inheritance? recursively act on children? */
86 bool inh;
87
88 /* see RELPERSISTENCE_* in pg_class.h */
89 char relpersistence;
90
91 /* table alias & optional column aliases */
92 Alias *alias;
93
94 /* token location, or -1 if unknown */
95 ParseLoc location;
96 } RangeVar;
97
98 typedef enum TableFuncType
99 {
100 TFT_XMLTABLE,
101 TFT_JSON_TABLE,
102 } TableFuncType;
103
104 /*
105 * TableFunc - node for a table function, such as XMLTABLE and JSON_TABLE.
106 *
107 * Entries in the ns_names list are either String nodes containing
108 * literal namespace names, or NULL pointers to represent DEFAULT.
109 */
110 typedef struct TableFunc
111 {
112 NodeTag type;
113 /* XMLTABLE or JSON_TABLE */
114 TableFuncType functype;
115 /* list of namespace URI expressions */
116 List *ns_uris pg_node_attr(query_jumble_ignore);
117 /* list of namespace names or NULL */
118 List *ns_names pg_node_attr(query_jumble_ignore);
119 /* input document expression */
120 Node *docexpr;
121 /* row filter expression */
122 Node *rowexpr;
123 /* column names (list of String) */
124 List *colnames pg_node_attr(query_jumble_ignore);
125 /* OID list of column type OIDs */
126 List *coltypes pg_node_attr(query_jumble_ignore);
127 /* integer list of column typmods */
128 List *coltypmods pg_node_attr(query_jumble_ignore);
129 /* OID list of column collation OIDs */
130 List *colcollations pg_node_attr(query_jumble_ignore);
131 /* list of column filter expressions */
132 List *colexprs;
133 /* list of column default expressions */
134 List *coldefexprs pg_node_attr(query_jumble_ignore);
135 /* JSON_TABLE: list of column value expressions */
136 List *colvalexprs pg_node_attr(query_jumble_ignore);
137 /* JSON_TABLE: list of PASSING argument expressions */
138 List *passingvalexprs pg_node_attr(query_jumble_ignore);
139 /* nullability flag for each output column */
140 Bitmapset *notnulls pg_node_attr(query_jumble_ignore);
141 /* JSON_TABLE plan */
142 Node *plan pg_node_attr(query_jumble_ignore);
143 /* counts from 0; -1 if none specified */
144 int ordinalitycol pg_node_attr(query_jumble_ignore);
145 /* token location, or -1 if unknown */
146 ParseLoc location;
147 } TableFunc;
148
149 /*
150 * IntoClause - target information for SELECT INTO, CREATE TABLE AS, and
151 * CREATE MATERIALIZED VIEW
152 *
153 * For CREATE MATERIALIZED VIEW, viewQuery is the parsed-but-not-rewritten
154 * SELECT Query for the view; otherwise it's NULL. This is irrelevant in
155 * the query jumbling as CreateTableAsStmt already includes a reference to
156 * its own Query, so ignore it. (We declare it as struct Query* to avoid a
157 * forward reference.)
158 */
159 typedef struct IntoClause
160 {
161 NodeTag type;
162
163 RangeVar *rel; /* target relation name */
164 List *colNames; /* column names to assign, or NIL */
165 char *accessMethod; /* table access method */
166 List *options; /* options from WITH clause */
167 OnCommitAction onCommit; /* what do we do at COMMIT? */
168 char *tableSpaceName; /* table space to use, or NULL */
169 /* materialized view's SELECT query */
170 struct Query *viewQuery pg_node_attr(query_jumble_ignore);
171 bool skipData; /* true for WITH NO DATA */
172 } IntoClause;
173
174
175 /* ----------------------------------------------------------------
176 * node types for executable expressions
177 * ----------------------------------------------------------------
178 */
179
180 /*
181 * Expr - generic superclass for executable-expression nodes
182 *
183 * All node types that are used in executable expression trees should derive
184 * from Expr (that is, have Expr as their first field). Since Expr only
185 * contains NodeTag, this is a formality, but it is an easy form of
186 * documentation. See also the ExprState node types in execnodes.h.
187 */
188 typedef struct Expr
189 {
190 pg_node_attr(abstract)
191
192 NodeTag type;
193 } Expr;
194
195 /*
196 * Var - expression node representing a variable (ie, a table column)
197 *
198 * In the parser and planner, varno and varattno identify the semantic
199 * referent, which is a base-relation column unless the reference is to a join
200 * USING column that isn't semantically equivalent to either join input column
201 * (because it is a FULL join or the input column requires a type coercion).
202 * In those cases varno and varattno refer to the JOIN RTE. (Early in the
203 * planner, we replace such join references by the implied expression; but up
204 * till then we want join reference Vars to keep their original identity for
205 * query-printing purposes.)
206 *
207 * At the end of planning, Var nodes appearing in upper-level plan nodes are
208 * reassigned to point to the outputs of their subplans; for example, in a
209 * join node varno becomes INNER_VAR or OUTER_VAR and varattno becomes the
210 * index of the proper element of that subplan's target list. Similarly,
211 * INDEX_VAR is used to identify Vars that reference an index column rather
212 * than a heap column. (In ForeignScan and CustomScan plan nodes, INDEX_VAR
213 * is abused to signify references to columns of a custom scan tuple type.)
214 *
215 * ROWID_VAR is used in the planner to identify nonce variables that carry
216 * row identity information during UPDATE/DELETE/MERGE. This value should
217 * never be seen outside the planner.
218 *
219 * varnullingrels is the set of RT indexes of outer joins that can force
220 * the Var's value to null (at the point where it appears in the query).
221 * See optimizer/README for discussion of that.
222 *
223 * varlevelsup is greater than zero in Vars that represent outer references.
224 * Note that it affects the meaning of all of varno, varnullingrels, and
225 * varnosyn, all of which refer to the range table of that query level.
226 *
227 * varreturningtype is used for Vars that refer to the target relation in the
228 * RETURNING list of data-modifying queries. The default behavior is to
229 * return old values for DELETE and new values for INSERT and UPDATE, but it
230 * is also possible to explicitly request old or new values.
231 *
232 * In the parser, varnosyn and varattnosyn are either identical to
233 * varno/varattno, or they specify the column's position in an aliased JOIN
234 * RTE that hides the semantic referent RTE's refname. This is a syntactic
235 * identifier as opposed to the semantic identifier; it tells ruleutils.c
236 * how to print the Var properly. varnosyn/varattnosyn retain their values
237 * throughout planning and execution, so they are particularly helpful to
238 * identify Vars when debugging. Note, however, that a Var that is generated
239 * in the planner and doesn't correspond to any simple relation column may
240 * have varnosyn = varattnosyn = 0.
241 */
242 #define INNER_VAR (-1) /* reference to inner subplan */
243 #define OUTER_VAR (-2) /* reference to outer subplan */
244 #define INDEX_VAR (-3) /* reference to index column */
245 #define ROWID_VAR (-4) /* row identity column during planning */
246
247 #define IS_SPECIAL_VARNO(varno) ((int) (varno) < 0)
248
249 /* Symbols for the indexes of the special RTE entries in rules */
250 #define PRS2_OLD_VARNO 1
251 #define PRS2_NEW_VARNO 2
252
253 /* Returning behavior for Vars in RETURNING list */
254 typedef enum VarReturningType
255 {
256 VAR_RETURNING_DEFAULT, /* return OLD for DELETE, else return NEW */
257 VAR_RETURNING_OLD, /* return OLD for DELETE/UPDATE, else NULL */
258 VAR_RETURNING_NEW, /* return NEW for INSERT/UPDATE, else NULL */
259 } VarReturningType;
260
261 typedef struct Var
262 {
263 Expr xpr;
264
265 /*
266 * index of this var's relation in the range table, or
267 * INNER_VAR/OUTER_VAR/etc
268 */
269 int varno;
270
271 /*
272 * attribute number of this var, or zero for all attrs ("whole-row Var")
273 */
274 AttrNumber varattno;
275
276 /* pg_type OID for the type of this var */
277 Oid vartype pg_node_attr(query_jumble_ignore);
278 /* pg_attribute typmod value */
279 int32 vartypmod pg_node_attr(query_jumble_ignore);
280 /* OID of collation, or InvalidOid if none */
281 Oid varcollid pg_node_attr(query_jumble_ignore);
282
283 /*
284 * RT indexes of outer joins that can replace the Var's value with null.
285 * We can omit varnullingrels in the query jumble, because it's fully
286 * determined by varno/varlevelsup plus the Var's query location.
287 */
288 Bitmapset *varnullingrels pg_node_attr(query_jumble_ignore);
289
290 /*
291 * for subquery variables referencing outer relations; 0 in a normal var,
292 * >0 means N levels up
293 */
294 Index varlevelsup;
295
296 /* returning type of this var (see above) */
297 VarReturningType varreturningtype;
298
299 /*
300 * varnosyn/varattnosyn are ignored for equality, because Vars with
301 * different syntactic identifiers are semantically the same as long as
302 * their varno/varattno match.
303 */
304 /* syntactic relation index (0 if unknown) */
305 Index varnosyn pg_node_attr(equal_ignore, query_jumble_ignore);
306 /* syntactic attribute number */
307 AttrNumber varattnosyn pg_node_attr(equal_ignore, query_jumble_ignore);
308
309 /* token location, or -1 if unknown */
310 ParseLoc location;
311 } Var;
312
313 /*
314 * Const
315 *
316 * Note: for varlena data types, we make a rule that a Const node's value
317 * must be in non-extended form (4-byte header, no compression or external
318 * references). This ensures that the Const node is self-contained and makes
319 * it more likely that equal() will see logically identical values as equal.
320 *
321 * Only the constant type OID is relevant for the query jumbling.
322 */
323 typedef struct Const
324 {
325 pg_node_attr(custom_copy_equal, custom_read_write)
326
327 Expr xpr;
328 /* pg_type OID of the constant's datatype */
329 Oid consttype;
330 /* typmod value, if any */
331 int32 consttypmod pg_node_attr(query_jumble_ignore);
332 /* OID of collation, or InvalidOid if none */
333 Oid constcollid pg_node_attr(query_jumble_ignore);
334 /* typlen of the constant's datatype */
335 int constlen pg_node_attr(query_jumble_ignore);
336 /* the constant's value */
337 Datum constvalue pg_node_attr(query_jumble_ignore);
338 /* whether the constant is null (if true, constvalue is undefined) */
339 bool constisnull pg_node_attr(query_jumble_ignore);
340
341 /*
342 * Whether this datatype is passed by value. If true, then all the
343 * information is stored in the Datum. If false, then the Datum contains
344 * a pointer to the information.
345 */
346 bool constbyval pg_node_attr(query_jumble_ignore);
347
348 /*
349 * token location, or -1 if unknown. All constants are tracked as
350 * locations in query jumbling, to be marked as parameters.
351 */
352 ParseLoc location pg_node_attr(query_jumble_location);
353 } Const;
354
355 /*
356 * Param
357 *
358 * paramkind specifies the kind of parameter. The possible values
359 * for this field are:
360 *
361 * PARAM_EXTERN: The parameter value is supplied from outside the plan.
362 * Such parameters are numbered from 1 to n.
363 *
364 * PARAM_EXEC: The parameter is an internal executor parameter, used
365 * for passing values into and out of sub-queries or from
366 * nestloop joins to their inner scans.
367 * For historical reasons, such parameters are numbered from 0.
368 * These numbers are independent of PARAM_EXTERN numbers.
369 *
370 * PARAM_SUBLINK: The parameter represents an output column of a SubLink
371 * node's sub-select. The column number is contained in the
372 * `paramid' field. (This type of Param is converted to
373 * PARAM_EXEC during planning.)
374 *
375 * PARAM_MULTIEXPR: Like PARAM_SUBLINK, the parameter represents an
376 * output column of a SubLink node's sub-select, but here, the
377 * SubLink is always a MULTIEXPR SubLink. The high-order 16 bits
378 * of the `paramid' field contain the SubLink's subLinkId, and
379 * the low-order 16 bits contain the column number. (This type
380 * of Param is also converted to PARAM_EXEC during planning.)
381 */
382 typedef enum ParamKind
383 {
384 PARAM_EXTERN,
385 PARAM_EXEC,
386 PARAM_SUBLINK,
387 PARAM_MULTIEXPR,
388 } ParamKind;
389
390 typedef struct Param
391 {
392 pg_node_attr(custom_query_jumble)
393
394 Expr xpr;
395 ParamKind paramkind; /* kind of parameter. See above */
396 int paramid; /* numeric ID for parameter */
397 Oid paramtype; /* pg_type OID of parameter's datatype */
398 /* typmod value, if known */
399 int32 paramtypmod;
400 /* OID of collation, or InvalidOid if none */
401 Oid paramcollid;
402 /* token location, or -1 if unknown */
403 ParseLoc location;
404 } Param;
405
406 /*
407 * Aggref
408 *
409 * The aggregate's args list is a targetlist, ie, a list of TargetEntry nodes.
410 *
411 * For a normal (non-ordered-set) aggregate, the non-resjunk TargetEntries
412 * represent the aggregate's regular arguments (if any) and resjunk TLEs can
413 * be added at the end to represent ORDER BY expressions that are not also
414 * arguments. As in a top-level Query, the TLEs can be marked with
415 * ressortgroupref indexes to let them be referenced by SortGroupClause
416 * entries in the aggorder and/or aggdistinct lists. This represents ORDER BY
417 * and DISTINCT operations to be applied to the aggregate input rows before
418 * they are passed to the transition function. The grammar only allows a
419 * simple "DISTINCT" specifier for the arguments, but we use the full
420 * query-level representation to allow more code sharing.
421 *
422 * For an ordered-set aggregate, the args list represents the WITHIN GROUP
423 * (aggregated) arguments, all of which will be listed in the aggorder list.
424 * DISTINCT is not supported in this case, so aggdistinct will be NIL.
425 * The direct arguments appear in aggdirectargs (as a list of plain
426 * expressions, not TargetEntry nodes).
427 *
428 * aggtranstype is the data type of the state transition values for this
429 * aggregate (resolved to an actual type, if agg's transtype is polymorphic).
430 * This is determined during planning and is InvalidOid before that.
431 *
432 * aggargtypes is an OID list of the data types of the direct and regular
433 * arguments. Normally it's redundant with the aggdirectargs and args lists,
434 * but in a combining aggregate, it's not because the args list has been
435 * replaced with a single argument representing the partial-aggregate
436 * transition values.
437 *
438 * aggpresorted is set by the query planner for ORDER BY and DISTINCT
439 * aggregates where the chosen plan provides presorted input for this
440 * aggregate during execution.
441 *
442 * aggsplit indicates the expected partial-aggregation mode for the Aggref's
443 * parent plan node. It's always set to AGGSPLIT_SIMPLE in the parser, but
444 * the planner might change it to something else. We use this mainly as
445 * a crosscheck that the Aggrefs match the plan; but note that when aggsplit
446 * indicates a non-final mode, aggtype reflects the transition data type
447 * not the SQL-level output type of the aggregate.
448 *
449 * aggno and aggtransno are -1 in the parse stage, and are set in planning.
450 * Aggregates with the same 'aggno' represent the same aggregate expression,
451 * and can share the result. Aggregates with same 'transno' but different
452 * 'aggno' can share the same transition state, only the final function needs
453 * to be called separately.
454 *
455 * Information related to collations, transition types and internal states
456 * are irrelevant for the query jumbling.
457 */
458 typedef struct Aggref
459 {
460 Expr xpr;
461
462 /* pg_proc Oid of the aggregate */
463 Oid aggfnoid;
464
465 /* type Oid of result of the aggregate */
466 Oid aggtype pg_node_attr(query_jumble_ignore);
467
468 /* OID of collation of result */
469 Oid aggcollid pg_node_attr(query_jumble_ignore);
470
471 /* OID of collation that function should use */
472 Oid inputcollid pg_node_attr(query_jumble_ignore);
473
474 /*
475 * type Oid of aggregate's transition value; ignored for equal since it
476 * might not be set yet
477 */
478 Oid aggtranstype pg_node_attr(equal_ignore, query_jumble_ignore);
479
480 /* type Oids of direct and aggregated args */
481 List *aggargtypes pg_node_attr(query_jumble_ignore);
482
483 /* direct arguments, if an ordered-set agg */
484 List *aggdirectargs;
485
486 /* aggregated arguments and sort expressions */
487 List *args;
488
489 /* ORDER BY (list of SortGroupClause) */
490 List *aggorder;
491
492 /* DISTINCT (list of SortGroupClause) */
493 List *aggdistinct;
494
495 /* FILTER expression, if any */
496 Expr *aggfilter;
497
498 /* true if argument list was really '*' */
499 bool aggstar pg_node_attr(query_jumble_ignore);
500
501 /*
502 * true if variadic arguments have been combined into an array last
503 * argument
504 */
505 bool aggvariadic pg_node_attr(query_jumble_ignore);
506
507 /* aggregate kind (see pg_aggregate.h) */
508 char aggkind pg_node_attr(query_jumble_ignore);
509
510 /* aggregate input already sorted */
511 bool aggpresorted pg_node_attr(equal_ignore, query_jumble_ignore);
512
513 /* > 0 if agg belongs to outer query */
514 Index agglevelsup pg_node_attr(query_jumble_ignore);
515
516 /* expected agg-splitting mode of parent Agg */
517 AggSplit aggsplit pg_node_attr(query_jumble_ignore);
518
519 /* unique ID within the Agg node */
520 int aggno pg_node_attr(query_jumble_ignore);
521
522 /* unique ID of transition state in the Agg */
523 int aggtransno pg_node_attr(query_jumble_ignore);
524
525 /* token location, or -1 if unknown */
526 ParseLoc location;
527 } Aggref;
528
529 /*
530 * GroupingFunc
531 *
532 * A GroupingFunc is a GROUPING(...) expression, which behaves in many ways
533 * like an aggregate function (e.g. it "belongs" to a specific query level,
534 * which might not be the one immediately containing it), but also differs in
535 * an important respect: it never evaluates its arguments, they merely
536 * designate expressions from the GROUP BY clause of the query level to which
537 * it belongs.
538 *
539 * The spec defines the evaluation of GROUPING() purely by syntactic
540 * replacement, but we make it a real expression for optimization purposes so
541 * that one Agg node can handle multiple grouping sets at once. Evaluating the
542 * result only needs the column positions to check against the grouping set
543 * being projected. However, for EXPLAIN to produce meaningful output, we have
544 * to keep the original expressions around, since expression deparse does not
545 * give us any feasible way to get at the GROUP BY clause.
546 *
547 * Also, we treat two GroupingFunc nodes as equal if they have equal arguments
548 * lists and agglevelsup, without comparing the refs and cols annotations.
549 *
550 * In raw parse output we have only the args list; parse analysis fills in the
551 * refs list, and the planner fills in the cols list.
552 *
553 * All the fields used as information for an internal state are irrelevant
554 * for the query jumbling.
555 */
556 typedef struct GroupingFunc
557 {
558 Expr xpr;
559
560 /* arguments, not evaluated but kept for benefit of EXPLAIN etc. */
561 List *args pg_node_attr(query_jumble_ignore);
562
563 /* ressortgrouprefs of arguments */
564 List *refs pg_node_attr(equal_ignore);
565
566 /* actual column positions set by planner */
567 List *cols pg_node_attr(equal_ignore, query_jumble_ignore);
568
569 /* same as Aggref.agglevelsup */
570 Index agglevelsup;
571
572 /* token location */
573 ParseLoc location;
574 } GroupingFunc;
575
576 /*
577 * WindowFunc
578 *
579 * Collation information is irrelevant for the query jumbling, as is the
580 * internal state information of the node like "winstar" and "winagg".
581 */
582 typedef struct WindowFunc
583 {
584 Expr xpr;
585 /* pg_proc Oid of the function */
586 Oid winfnoid;
587 /* type Oid of result of the window function */
588 Oid wintype pg_node_attr(query_jumble_ignore);
589 /* OID of collation of result */
590 Oid wincollid pg_node_attr(query_jumble_ignore);
591 /* OID of collation that function should use */
592 Oid inputcollid pg_node_attr(query_jumble_ignore);
593 /* arguments to the window function */
594 List *args;
595 /* FILTER expression, if any */
596 Expr *aggfilter;
597 /* List of WindowFuncRunConditions to help short-circuit execution */
598 List *runCondition pg_node_attr(query_jumble_ignore);
599 /* index of associated WindowClause */
600 Index winref;
601 /* true if argument list was really '*' */
602 bool winstar pg_node_attr(query_jumble_ignore);
603 /* is function a simple aggregate? */
604 bool winagg pg_node_attr(query_jumble_ignore);
605 /* token location, or -1 if unknown */
606 ParseLoc location;
607 } WindowFunc;
608
609 /*
610 * WindowFuncRunCondition
611 *
612 * Represents intermediate OpExprs which will be used by WindowAgg to
613 * short-circuit execution.
614 */
615 typedef struct WindowFuncRunCondition
616 {
617 Expr xpr;
618
619 /* PG_OPERATOR OID of the operator */
620 Oid opno;
621 /* OID of collation that operator should use */
622 Oid inputcollid pg_node_attr(query_jumble_ignore);
623
624 /*
625 * true of WindowFunc belongs on the left of the resulting OpExpr or false
626 * if the WindowFunc is on the right.
627 */
628 bool wfunc_left;
629
630 /*
631 * The Expr being compared to the WindowFunc to use in the OpExpr in the
632 * WindowAgg's runCondition
633 */
634 Expr *arg;
635 } WindowFuncRunCondition;
636
637 /*
638 * MergeSupportFunc
639 *
640 * A MergeSupportFunc is a merge support function expression that can only
641 * appear in the RETURNING list of a MERGE command. It returns information
642 * about the currently executing merge action.
643 *
644 * Currently, the only supported function is MERGE_ACTION(), which returns the
645 * command executed ("INSERT", "UPDATE", or "DELETE").
646 */
647 typedef struct MergeSupportFunc
648 {
649 Expr xpr;
650 /* type Oid of result */
651 Oid msftype;
652 /* OID of collation, or InvalidOid if none */
653 Oid msfcollid;
654 /* token location, or -1 if unknown */
655 ParseLoc location;
656 } MergeSupportFunc;
657
658 /*
659 * SubscriptingRef: describes a subscripting operation over a container
660 * (array, etc).
661 *
662 * A SubscriptingRef can describe fetching a single element from a container,
663 * fetching a part of a container (e.g. an array slice), storing a single
664 * element into a container, or storing a slice. The "store" cases work with
665 * an initial container value and a source value that is inserted into the
666 * appropriate part of the container; the result of the operation is an
667 * entire new modified container value.
668 *
669 * If reflowerindexpr = NIL, then we are fetching or storing a single container
670 * element at the subscripts given by refupperindexpr. Otherwise we are
671 * fetching or storing a container slice, that is a rectangular subcontainer
672 * with lower and upper bounds given by the index expressions.
673 * reflowerindexpr must be the same length as refupperindexpr when it
674 * is not NIL.
675 *
676 * In the slice case, individual expressions in the subscript lists can be
677 * NULL, meaning "substitute the array's current lower or upper bound".
678 * (Non-array containers may or may not support this.)
679 *
680 * refcontainertype is the actual container type that determines the
681 * subscripting semantics. (This will generally be either the exposed type of
682 * refexpr, or the base type if that is a domain.) refelemtype is the type of
683 * the container's elements; this is saved for the use of the subscripting
684 * functions, but is not used by the core code. refrestype, reftypmod, and
685 * refcollid describe the type of the SubscriptingRef's result. In a store
686 * expression, refrestype will always match refcontainertype; in a fetch,
687 * it could be refelemtype for an element fetch, or refcontainertype for a
688 * slice fetch, or possibly something else as determined by type-specific
689 * subscripting logic. Likewise, reftypmod and refcollid will match the
690 * container's properties in a store, but could be different in a fetch.
691 *
692 * Any internal state data is ignored for the query jumbling.
693 *
694 * Note: for the cases where a container is returned, if refexpr yields a R/W
695 * expanded container, then the implementation is allowed to modify that
696 * object in-place and return the same object.
697 */
698 typedef struct SubscriptingRef
699 {
700 Expr xpr;
701 /* type of the container proper */
702 Oid refcontainertype pg_node_attr(query_jumble_ignore);
703 /* the container type's pg_type.typelem */
704 Oid refelemtype pg_node_attr(query_jumble_ignore);
705 /* type of the SubscriptingRef's result */
706 Oid refrestype pg_node_attr(query_jumble_ignore);
707 /* typmod of the result */
708 int32 reftypmod pg_node_attr(query_jumble_ignore);
709 /* collation of result, or InvalidOid if none */
710 Oid refcollid pg_node_attr(query_jumble_ignore);
711 /* expressions that evaluate to upper container indexes */
712 List *refupperindexpr;
713
714 /*
715 * expressions that evaluate to lower container indexes, or NIL for single
716 * container element.
717 */
718 List *reflowerindexpr;
719 /* the expression that evaluates to a container value */
720 Expr *refexpr;
721 /* expression for the source value, or NULL if fetch */
722 Expr *refassgnexpr;
723 } SubscriptingRef;
724
725 /*
726 * CoercionContext - distinguishes the allowed set of type casts
727 *
728 * NB: ordering of the alternatives is significant; later (larger) values
729 * allow more casts than earlier ones.
730 */
731 typedef enum CoercionContext
732 {
733 COERCION_IMPLICIT, /* coercion in context of expression */
734 COERCION_ASSIGNMENT, /* coercion in context of assignment */
735 COERCION_PLPGSQL, /* if no assignment cast, use CoerceViaIO */
736 COERCION_EXPLICIT, /* explicit cast operation */
737 } CoercionContext;
738
739 /*
740 * CoercionForm - how to display a FuncExpr or related node
741 *
742 * "Coercion" is a bit of a misnomer, since this value records other
743 * special syntaxes besides casts, but for now we'll keep this naming.
744 *
745 * NB: equal() ignores CoercionForm fields, therefore this *must* not carry
746 * any semantically significant information. We need that behavior so that
747 * the planner will consider equivalent implicit and explicit casts to be
748 * equivalent. In cases where those actually behave differently, the coercion
749 * function's arguments will be different.
750 */
751 typedef enum CoercionForm
752 {
753 COERCE_EXPLICIT_CALL, /* display as a function call */
754 COERCE_EXPLICIT_CAST, /* display as an explicit cast */
755 COERCE_IMPLICIT_CAST, /* implicit cast, so hide it */
756 COERCE_SQL_SYNTAX, /* display with SQL-mandated special syntax */
757 } CoercionForm;
758
759 /*
760 * FuncExpr - expression node for a function call
761 *
762 * Collation information is irrelevant for the query jumbling, only the
763 * arguments and the function OID matter.
764 */
765 typedef struct FuncExpr
766 {
767 Expr xpr;
768 /* PG_PROC OID of the function */
769 Oid funcid;
770 /* PG_TYPE OID of result value */
771 Oid funcresulttype pg_node_attr(query_jumble_ignore);
772 /* true if function returns set */
773 bool funcretset pg_node_attr(query_jumble_ignore);
774
775 /*
776 * true if variadic arguments have been combined into an array last
777 * argument
778 */
779 bool funcvariadic pg_node_attr(query_jumble_ignore);
780 /* how to display this function call */
781 CoercionForm funcformat pg_node_attr(query_jumble_ignore);
782 /* OID of collation of result */
783 Oid funccollid pg_node_attr(query_jumble_ignore);
784 /* OID of collation that function should use */
785 Oid inputcollid pg_node_attr(query_jumble_ignore);
786 /* arguments to the function */
787 List *args;
788 /* token location, or -1 if unknown */
789 ParseLoc location;
790 } FuncExpr;
791
792 /*
793 * NamedArgExpr - a named argument of a function
794 *
795 * This node type can only appear in the args list of a FuncCall or FuncExpr
796 * node. We support pure positional call notation (no named arguments),
797 * named notation (all arguments are named), and mixed notation (unnamed
798 * arguments followed by named ones).
799 *
800 * Parse analysis sets argnumber to the positional index of the argument,
801 * but doesn't rearrange the argument list.
802 *
803 * The planner will convert argument lists to pure positional notation
804 * during expression preprocessing, so execution never sees a NamedArgExpr.
805 */
806 typedef struct NamedArgExpr
807 {
808 Expr xpr;
809 /* the argument expression */
810 Expr *arg;
811 /* the name */
812 char *name pg_node_attr(query_jumble_ignore);
813 /* argument's number in positional notation */
814 int argnumber;
815 /* argument name location, or -1 if unknown */
816 ParseLoc location;
817 } NamedArgExpr;
818
819 /*
820 * OpExpr - expression node for an operator invocation
821 *
822 * Semantically, this is essentially the same as a function call.
823 *
824 * Note that opfuncid is not necessarily filled in immediately on creation
825 * of the node. The planner makes sure it is valid before passing the node
826 * tree to the executor, but during parsing/planning opfuncid can be 0.
827 * Therefore, equal() will accept a zero value as being equal to other values.
828 *
829 * Internal state information and collation data is irrelevant for the query
830 * jumbling.
831 */
832 typedef struct OpExpr
833 {
834 Expr xpr;
835
836 /* PG_OPERATOR OID of the operator */
837 Oid opno;
838
839 /* PG_PROC OID of underlying function */
840 Oid opfuncid pg_node_attr(equal_ignore_if_zero, query_jumble_ignore);
841
842 /* PG_TYPE OID of result value */
843 Oid opresulttype pg_node_attr(query_jumble_ignore);
844
845 /* true if operator returns set */
846 bool opretset pg_node_attr(query_jumble_ignore);
847
848 /* OID of collation of result */
849 Oid opcollid pg_node_attr(query_jumble_ignore);
850
851 /* OID of collation that operator should use */
852 Oid inputcollid pg_node_attr(query_jumble_ignore);
853
854 /* arguments to the operator (1 or 2) */
855 List *args;
856
857 /* token location, or -1 if unknown */
858 ParseLoc location;
859 } OpExpr;
860
861 /*
862 * DistinctExpr - expression node for "x IS DISTINCT FROM y"
863 *
864 * Except for the nodetag, this is represented identically to an OpExpr
865 * referencing the "=" operator for x and y.
866 * We use "=", not the more obvious "<>", because more datatypes have "="
867 * than "<>". This means the executor must invert the operator result.
868 * Note that the operator function won't be called at all if either input
869 * is NULL, since then the result can be determined directly.
870 */
871 typedef OpExpr DistinctExpr;
872
873 /*
874 * NullIfExpr - a NULLIF expression
875 *
876 * Like DistinctExpr, this is represented the same as an OpExpr referencing
877 * the "=" operator for x and y.
878 */
879 typedef OpExpr NullIfExpr;
880
881 /*
882 * ScalarArrayOpExpr - expression node for "scalar op ANY/ALL (array)"
883 *
884 * The operator must yield boolean. It is applied to the left operand
885 * and each element of the righthand array, and the results are combined
886 * with OR or AND (for ANY or ALL respectively). The node representation
887 * is almost the same as for the underlying operator, but we need a useOr
888 * flag to remember whether it's ANY or ALL, and we don't have to store
889 * the result type (or the collation) because it must be boolean.
890 *
891 * A ScalarArrayOpExpr with a valid hashfuncid is evaluated during execution
892 * by building a hash table containing the Const values from the RHS arg.
893 * This table is probed during expression evaluation. The planner will set
894 * hashfuncid to the hash function which must be used to build and probe the
895 * hash table. The executor determines if it should use hash-based checks or
896 * the more traditional means based on if the hashfuncid is set or not.
897 *
898 * When performing hashed NOT IN, the negfuncid will also be set to the
899 * equality function which the hash table must use to build and probe the hash
900 * table. opno and opfuncid will remain set to the <> operator and its
901 * corresponding function and won't be used during execution. For
902 * non-hashtable based NOT INs, negfuncid will be set to InvalidOid. See
903 * convert_saop_to_hashed_saop().
904 *
905 * Similar to OpExpr, opfuncid, hashfuncid, and negfuncid are not necessarily
906 * filled in right away, so will be ignored for equality if they are not set
907 * yet.
908 *
909 * OID entries of the internal function types are irrelevant for the query
910 * jumbling, but the operator OID and the arguments are.
911 */
912 typedef struct ScalarArrayOpExpr
913 {
914 Expr xpr;
915
916 /* PG_OPERATOR OID of the operator */
917 Oid opno;
918
919 /* PG_PROC OID of comparison function */
920 Oid opfuncid pg_node_attr(equal_ignore_if_zero, query_jumble_ignore);
921
922 /* PG_PROC OID of hash func or InvalidOid */
923 Oid hashfuncid pg_node_attr(equal_ignore_if_zero, query_jumble_ignore);
924
925 /* PG_PROC OID of negator of opfuncid function or InvalidOid. See above */
926 Oid negfuncid pg_node_attr(equal_ignore_if_zero, query_jumble_ignore);
927
928 /* true for ANY, false for ALL */
929 bool useOr;
930
931 /* OID of collation that operator should use */
932 Oid inputcollid pg_node_attr(query_jumble_ignore);
933
934 /* the scalar and array operands */
935 List *args;
936
937 /* token location, or -1 if unknown */
938 ParseLoc location;
939 } ScalarArrayOpExpr;
940
941 /*
942 * BoolExpr - expression node for the basic Boolean operators AND, OR, NOT
943 *
944 * Notice the arguments are given as a List. For NOT, of course the list
945 * must always have exactly one element. For AND and OR, there can be two
946 * or more arguments.
947 */
948 typedef enum BoolExprType
949 {
950 AND_EXPR, OR_EXPR, NOT_EXPR
951 } BoolExprType;
952
953 typedef struct BoolExpr
954 {
955 pg_node_attr(custom_read_write)
956
957 Expr xpr;
958 BoolExprType boolop;
959 List *args; /* arguments to this expression */
960 ParseLoc location; /* token location, or -1 if unknown */
961 } BoolExpr;
962
963 /*
964 * SubLink
965 *
966 * A SubLink represents a subselect appearing in an expression, and in some
967 * cases also the combining operator(s) just above it. The subLinkType
968 * indicates the form of the expression represented:
969 * EXISTS_SUBLINK EXISTS(SELECT ...)
970 * ALL_SUBLINK (lefthand) op ALL (SELECT ...)
971 * ANY_SUBLINK (lefthand) op ANY (SELECT ...)
972 * ROWCOMPARE_SUBLINK (lefthand) op (SELECT ...)
973 * EXPR_SUBLINK (SELECT with single targetlist item ...)
974 * MULTIEXPR_SUBLINK (SELECT with multiple targetlist items ...)
975 * ARRAY_SUBLINK ARRAY(SELECT with single targetlist item ...)
976 * CTE_SUBLINK WITH query (never actually part of an expression)
977 * For ALL, ANY, and ROWCOMPARE, the lefthand is a list of expressions of the
978 * same length as the subselect's targetlist. ROWCOMPARE will *always* have
979 * a list with more than one entry; if the subselect has just one target
980 * then the parser will create an EXPR_SUBLINK instead (and any operator
981 * above the subselect will be represented separately).
982 * ROWCOMPARE, EXPR, and MULTIEXPR require the subselect to deliver at most
983 * one row (if it returns no rows, the result is NULL).
984 * ALL, ANY, and ROWCOMPARE require the combining operators to deliver boolean
985 * results. ALL and ANY combine the per-row results using AND and OR
986 * semantics respectively.
987 * ARRAY requires just one target column, and creates an array of the target
988 * column's type using any number of rows resulting from the subselect.
989 *
990 * SubLink is classed as an Expr node, but it is not actually executable;
991 * it must be replaced in the expression tree by a SubPlan node during
992 * planning.
993 *
994 * NOTE: in the raw output of gram.y, testexpr contains just the raw form
995 * of the lefthand expression (if any), and operName is the String name of
996 * the combining operator. Also, subselect is a raw parsetree. During parse
997 * analysis, the parser transforms testexpr into a complete boolean expression
998 * that compares the lefthand value(s) to PARAM_SUBLINK nodes representing the
999 * output columns of the subselect. And subselect is transformed to a Query.
1000 * This is the representation seen in saved rules and in the rewriter.
1001 *
1002 * In EXISTS, EXPR, MULTIEXPR, and ARRAY SubLinks, testexpr and operName
1003 * are unused and are always null.
1004 *
1005 * subLinkId is currently used only for MULTIEXPR SubLinks, and is zero in
1006 * other SubLinks. This number identifies different multiple-assignment
1007 * subqueries within an UPDATE statement's SET list. It is unique only
1008 * within a particular targetlist. The output column(s) of the MULTIEXPR
1009 * are referenced by PARAM_MULTIEXPR Params appearing elsewhere in the tlist.
1010 *
1011 * The CTE_SUBLINK case never occurs in actual SubLink nodes, but it is used
1012 * in SubPlans generated for WITH subqueries.
1013 */
1014 typedef enum SubLinkType
1015 {
1016 EXISTS_SUBLINK,
1017 ALL_SUBLINK,
1018 ANY_SUBLINK,
1019 ROWCOMPARE_SUBLINK,
1020 EXPR_SUBLINK,
1021 MULTIEXPR_SUBLINK,
1022 ARRAY_SUBLINK,
1023 CTE_SUBLINK, /* for SubPlans only */
1024 } SubLinkType;
1025
1026
1027 typedef struct SubLink
1028 {
1029 Expr xpr;
1030 SubLinkType subLinkType; /* see above */
1031 int subLinkId; /* ID (1..n); 0 if not MULTIEXPR */
1032 Node *testexpr; /* outer-query test for ALL/ANY/ROWCOMPARE */
1033 /* originally specified operator name */
1034 List *operName pg_node_attr(query_jumble_ignore);
1035 /* subselect as Query* or raw parsetree */
1036 Node *subselect;
1037 ParseLoc location; /* token location, or -1 if unknown */
1038 } SubLink;
1039
1040 /*
1041 * SubPlan - executable expression node for a subplan (sub-SELECT)
1042 *
1043 * The planner replaces SubLink nodes in expression trees with SubPlan
1044 * nodes after it has finished planning the subquery. SubPlan references
1045 * a sub-plantree stored in the subplans list of the toplevel PlannedStmt.
1046 * (We avoid a direct link to make it easier to copy expression trees
1047 * without causing multiple processing of the subplan.)
1048 *
1049 * In an ordinary subplan, testexpr points to an executable expression
1050 * (OpExpr, an AND/OR tree of OpExprs, or RowCompareExpr) for the combining
1051 * operator(s); the left-hand arguments are the original lefthand expressions,
1052 * and the right-hand arguments are PARAM_EXEC Param nodes representing the
1053 * outputs of the sub-select. (NOTE: runtime coercion functions may be
1054 * inserted as well.) This is just the same expression tree as testexpr in
1055 * the original SubLink node, but the PARAM_SUBLINK nodes are replaced by
1056 * suitably numbered PARAM_EXEC nodes.
1057 *
1058 * If the sub-select becomes an initplan rather than a subplan, the executable
1059 * expression is part of the outer plan's expression tree (and the SubPlan
1060 * node itself is not, but rather is found in the outer plan's initPlan
1061 * list). In this case testexpr is NULL to avoid duplication.
1062 *
1063 * The planner also derives lists of the values that need to be passed into
1064 * and out of the subplan. Input values are represented as a list "args" of
1065 * expressions to be evaluated in the outer-query context (currently these
1066 * args are always just Vars, but in principle they could be any expression).
1067 * The values are assigned to the global PARAM_EXEC params indexed by parParam
1068 * (the parParam and args lists must have the same ordering). setParam is a
1069 * list of the PARAM_EXEC params that are computed by the sub-select, if it
1070 * is an initplan or MULTIEXPR plan; they are listed in order by sub-select
1071 * output column position. (parParam and setParam are integer Lists, not
1072 * Bitmapsets, because their ordering is significant.)
1073 *
1074 * Also, the planner computes startup and per-call costs for use of the
1075 * SubPlan. Note that these include the cost of the subquery proper,
1076 * evaluation of the testexpr if any, and any hashtable management overhead.
1077 */
1078 typedef struct SubPlan
1079 {
1080 pg_node_attr(no_query_jumble)
1081
1082 Expr xpr;
1083 /* Fields copied from original SubLink: */
1084 SubLinkType subLinkType; /* see above */
1085 /* The combining operators, transformed to an executable expression: */
1086 Node *testexpr; /* OpExpr or RowCompareExpr expression tree */
1087 List *paramIds; /* IDs of Params embedded in the above */
1088 /* Identification of the Plan tree to use: */
1089 int plan_id; /* Index (from 1) in PlannedStmt.subplans */
1090 /* Identification of the SubPlan for EXPLAIN and debugging purposes: */
1091 char *plan_name; /* A name assigned during planning */
1092 /* Extra data useful for determining subplan's output type: */
1093 Oid firstColType; /* Type of first column of subplan result */
1094 int32 firstColTypmod; /* Typmod of first column of subplan result */
1095 Oid firstColCollation; /* Collation of first column of subplan
1096 * result */
1097 /* Information about execution strategy: */
1098 bool useHashTable; /* true to store subselect output in a hash
1099 * table (implies we are doing "IN") */
1100 bool unknownEqFalse; /* true if it's okay to return FALSE when the
1101 * spec result is UNKNOWN; this allows much
1102 * simpler handling of null values */
1103 bool parallel_safe; /* is the subplan parallel-safe? */
1104 /* Note: parallel_safe does not consider contents of testexpr or args */
1105 /* Information for passing params into and out of the subselect: */
1106 /* setParam and parParam are lists of integers (param IDs) */
1107 List *setParam; /* initplan and MULTIEXPR subqueries have to
1108 * set these Params for parent plan */
1109 List *parParam; /* indices of input Params from parent plan */
1110 List *args; /* exprs to pass as parParam values */
1111 /* Estimated execution costs: */
1112 Cost startup_cost; /* one-time setup cost */
1113 Cost per_call_cost; /* cost for each subplan evaluation */
1114 } SubPlan;
1115
1116 /*
1117 * AlternativeSubPlan - expression node for a choice among SubPlans
1118 *
1119 * This is used only transiently during planning: by the time the plan
1120 * reaches the executor, all AlternativeSubPlan nodes have been removed.
1121 *
1122 * The subplans are given as a List so that the node definition need not
1123 * change if there's ever more than two alternatives. For the moment,
1124 * though, there are always exactly two; and the first one is the fast-start
1125 * plan.
1126 */
1127 typedef struct AlternativeSubPlan
1128 {
1129 pg_node_attr(no_query_jumble)
1130
1131 Expr xpr;
1132 List *subplans; /* SubPlan(s) with equivalent results */
1133 } AlternativeSubPlan;
1134
1135 /* ----------------
1136 * FieldSelect
1137 *
1138 * FieldSelect represents the operation of extracting one field from a tuple
1139 * value. At runtime, the input expression is expected to yield a rowtype
1140 * Datum. The specified field number is extracted and returned as a Datum.
1141 * ----------------
1142 */
1143
1144 typedef struct FieldSelect
1145 {
1146 Expr xpr;
1147 Expr *arg; /* input expression */
1148 AttrNumber fieldnum; /* attribute number of field to extract */
1149 /* type of the field (result type of this node) */
1150 Oid resulttype pg_node_attr(query_jumble_ignore);
1151 /* output typmod (usually -1) */
1152 int32 resulttypmod pg_node_attr(query_jumble_ignore);
1153 /* OID of collation of the field */
1154 Oid resultcollid pg_node_attr(query_jumble_ignore);
1155 } FieldSelect;
1156
1157 /* ----------------
1158 * FieldStore
1159 *
1160 * FieldStore represents the operation of modifying one field in a tuple
1161 * value, yielding a new tuple value (the input is not touched!). Like
1162 * the assign case of SubscriptingRef, this is used to implement UPDATE of a
1163 * portion of a column.
1164 *
1165 * resulttype is always a named composite type (not a domain). To update
1166 * a composite domain value, apply CoerceToDomain to the FieldStore.
1167 *
1168 * A single FieldStore can actually represent updates of several different
1169 * fields. The parser only generates FieldStores with single-element lists,
1170 * but the planner will collapse multiple updates of the same base column
1171 * into one FieldStore.
1172 * ----------------
1173 */
1174
1175 typedef struct FieldStore
1176 {
1177 Expr xpr;
1178 Expr *arg; /* input tuple value */
1179 List *newvals; /* new value(s) for field(s) */
1180 /* integer list of field attnums */
1181 List *fieldnums pg_node_attr(query_jumble_ignore);
1182 /* type of result (same as type of arg) */
1183 Oid resulttype pg_node_attr(query_jumble_ignore);
1184 /* Like RowExpr, we deliberately omit a typmod and collation here */
1185 } FieldStore;
1186
1187 /* ----------------
1188 * RelabelType
1189 *
1190 * RelabelType represents a "dummy" type coercion between two binary-
1191 * compatible datatypes, such as reinterpreting the result of an OID
1192 * expression as an int4. It is a no-op at runtime; we only need it
1193 * to provide a place to store the correct type to be attributed to
1194 * the expression result during type resolution. (We can't get away
1195 * with just overwriting the type field of the input expression node,
1196 * so we need a separate node to show the coercion's result type.)
1197 * ----------------
1198 */
1199
1200 typedef struct RelabelType
1201 {
1202 Expr xpr;
1203 Expr *arg; /* input expression */
1204 Oid resulttype; /* output type of coercion expression */
1205 /* output typmod (usually -1) */
1206 int32 resulttypmod pg_node_attr(query_jumble_ignore);
1207 /* OID of collation, or InvalidOid if none */
1208 Oid resultcollid pg_node_attr(query_jumble_ignore);
1209 /* how to display this node */
1210 CoercionForm relabelformat pg_node_attr(query_jumble_ignore);
1211 ParseLoc location; /* token location, or -1 if unknown */
1212 } RelabelType;
1213
1214 /* ----------------
1215 * CoerceViaIO
1216 *
1217 * CoerceViaIO represents a type coercion between two types whose textual
1218 * representations are compatible, implemented by invoking the source type's
1219 * typoutput function then the destination type's typinput function.
1220 * ----------------
1221 */
1222
1223 typedef struct CoerceViaIO
1224 {
1225 Expr xpr;
1226 Expr *arg; /* input expression */
1227 Oid resulttype; /* output type of coercion */
1228 /* output typmod is not stored, but is presumed -1 */
1229 /* OID of collation, or InvalidOid if none */
1230 Oid resultcollid pg_node_attr(query_jumble_ignore);
1231 /* how to display this node */
1232 CoercionForm coerceformat pg_node_attr(query_jumble_ignore);
1233 ParseLoc location; /* token location, or -1 if unknown */
1234 } CoerceViaIO;
1235
1236 /* ----------------
1237 * ArrayCoerceExpr
1238 *
1239 * ArrayCoerceExpr represents a type coercion from one array type to another,
1240 * which is implemented by applying the per-element coercion expression
1241 * "elemexpr" to each element of the source array. Within elemexpr, the
1242 * source element is represented by a CaseTestExpr node. Note that even if
1243 * elemexpr is a no-op (that is, just CaseTestExpr + RelabelType), the
1244 * coercion still requires some effort: we have to fix the element type OID
1245 * stored in the array header.
1246 * ----------------
1247 */
1248
1249 typedef struct ArrayCoerceExpr
1250 {
1251 Expr xpr;
1252 Expr *arg; /* input expression (yields an array) */
1253 Expr *elemexpr; /* expression representing per-element work */
1254 Oid resulttype; /* output type of coercion (an array type) */
1255 /* output typmod (also element typmod) */
1256 int32 resulttypmod pg_node_attr(query_jumble_ignore);
1257 /* OID of collation, or InvalidOid if none */
1258 Oid resultcollid pg_node_attr(query_jumble_ignore);
1259 /* how to display this node */
1260 CoercionForm coerceformat pg_node_attr(query_jumble_ignore);
1261 ParseLoc location; /* token location, or -1 if unknown */
1262 } ArrayCoerceExpr;
1263
1264 /* ----------------
1265 * ConvertRowtypeExpr
1266 *
1267 * ConvertRowtypeExpr represents a type coercion from one composite type
1268 * to another, where the source type is guaranteed to contain all the columns
1269 * needed for the destination type plus possibly others; the columns need not
1270 * be in the same positions, but are matched up by name. This is primarily
1271 * used to convert a whole-row value of an inheritance child table into a
1272 * valid whole-row value of its parent table's rowtype. Both resulttype
1273 * and the exposed type of "arg" must be named composite types (not domains).
1274 * ----------------
1275 */
1276
1277 typedef struct ConvertRowtypeExpr
1278 {
1279 Expr xpr;
1280 Expr *arg; /* input expression */
1281 Oid resulttype; /* output type (always a composite type) */
1282 /* Like RowExpr, we deliberately omit a typmod and collation here */
1283 /* how to display this node */
1284 CoercionForm convertformat pg_node_attr(query_jumble_ignore);
1285 ParseLoc location; /* token location, or -1 if unknown */
1286 } ConvertRowtypeExpr;
1287
1288 /*----------
1289 * CollateExpr - COLLATE
1290 *
1291 * The planner replaces CollateExpr with RelabelType during expression
1292 * preprocessing, so execution never sees a CollateExpr.
1293 *----------
1294 */
1295 typedef struct CollateExpr
1296 {
1297 Expr xpr;
1298 Expr *arg; /* input expression */
1299 Oid collOid; /* collation's OID */
1300 ParseLoc location; /* token location, or -1 if unknown */
1301 } CollateExpr;
1302
1303 /*----------
1304 * CaseExpr - a CASE expression
1305 *
1306 * We support two distinct forms of CASE expression:
1307 * CASE WHEN boolexpr THEN expr [ WHEN boolexpr THEN expr ... ]
1308 * CASE testexpr WHEN compexpr THEN expr [ WHEN compexpr THEN expr ... ]
1309 * These are distinguishable by the "arg" field being NULL in the first case
1310 * and the testexpr in the second case.
1311 *
1312 * In the raw grammar output for the second form, the condition expressions
1313 * of the WHEN clauses are just the comparison values. Parse analysis
1314 * converts these to valid boolean expressions of the form
1315 * CaseTestExpr '=' compexpr
1316 * where the CaseTestExpr node is a placeholder that emits the correct
1317 * value at runtime. This structure is used so that the testexpr need be
1318 * evaluated only once. Note that after parse analysis, the condition
1319 * expressions always yield boolean.
1320 *
1321 * Note: we can test whether a CaseExpr has been through parse analysis
1322 * yet by checking whether casetype is InvalidOid or not.
1323 *----------
1324 */
1325 typedef struct CaseExpr
1326 {
1327 Expr xpr;
1328 /* type of expression result */
1329 Oid casetype pg_node_attr(query_jumble_ignore);
1330 /* OID of collation, or InvalidOid if none */
1331 Oid casecollid pg_node_attr(query_jumble_ignore);
1332 Expr *arg; /* implicit equality comparison argument */
1333 List *args; /* the arguments (list of WHEN clauses) */
1334 Expr *defresult; /* the default result (ELSE clause) */
1335 ParseLoc location; /* token location, or -1 if unknown */
1336 } CaseExpr;
1337
1338 /*
1339 * CaseWhen - one arm of a CASE expression
1340 */
1341 typedef struct CaseWhen
1342 {
1343 Expr xpr;
1344 Expr *expr; /* condition expression */
1345 Expr *result; /* substitution result */
1346 ParseLoc location; /* token location, or -1 if unknown */
1347 } CaseWhen;
1348
1349 /*
1350 * Placeholder node for the test value to be processed by a CASE expression.
1351 * This is effectively like a Param, but can be implemented more simply
1352 * since we need only one replacement value at a time.
1353 *
1354 * We also abuse this node type for some other purposes, including:
1355 * * Placeholder for the current array element value in ArrayCoerceExpr;
1356 * see build_coercion_expression().
1357 * * Nested FieldStore/SubscriptingRef assignment expressions in INSERT/UPDATE;
1358 * see transformAssignmentIndirection().
1359 * * Placeholder for intermediate results in some SQL/JSON expression nodes,
1360 * such as JsonConstructorExpr.
1361 *
1362 * The uses in CaseExpr and ArrayCoerceExpr are safe only to the extent that
1363 * there is not any other CaseExpr or ArrayCoerceExpr between the value source
1364 * node and its child CaseTestExpr(s). This is true in the parse analysis
1365 * output, but the planner's function-inlining logic has to be careful not to
1366 * break it.
1367 *
1368 * The nested-assignment-expression case is safe because the only node types
1369 * that can be above such CaseTestExprs are FieldStore and SubscriptingRef.
1370 */
1371 typedef struct CaseTestExpr
1372 {
1373 Expr xpr;
1374 Oid typeId; /* type for substituted value */
1375 /* typemod for substituted value */
1376 int32 typeMod pg_node_attr(query_jumble_ignore);
1377 /* collation for the substituted value */
1378 Oid collation pg_node_attr(query_jumble_ignore);
1379 } CaseTestExpr;
1380
1381 /*
1382 * ArrayExpr - an ARRAY[] expression
1383 *
1384 * Note: if multidims is false, the constituent expressions all yield the
1385 * scalar type identified by element_typeid. If multidims is true, the
1386 * constituent expressions all yield arrays of element_typeid (ie, the same
1387 * type as array_typeid); at runtime we must check for compatible subscripts.
1388 */
1389 typedef struct ArrayExpr
1390 {
1391 Expr xpr;
1392 /* type of expression result */
1393 Oid array_typeid pg_node_attr(query_jumble_ignore);
1394 /* OID of collation, or InvalidOid if none */
1395 Oid array_collid pg_node_attr(query_jumble_ignore);
1396 /* common type of array elements */
1397 Oid element_typeid pg_node_attr(query_jumble_ignore);
1398 /* the array elements or sub-arrays */
1399 List *elements pg_node_attr(query_jumble_squash);
1400 /* true if elements are sub-arrays */
1401 bool multidims pg_node_attr(query_jumble_ignore);
1402 /* location of the start of the elements list */
1403 ParseLoc list_start;
1404 /* location of the end of the elements list */
1405 ParseLoc list_end;
1406 /* token location, or -1 if unknown */
1407 ParseLoc location;
1408 } ArrayExpr;
1409
1410 /*
1411 * RowExpr - a ROW() expression
1412 *
1413 * Note: the list of fields must have a one-for-one correspondence with
1414 * physical fields of the associated rowtype, although it is okay for it
1415 * to be shorter than the rowtype. That is, the N'th list element must
1416 * match up with the N'th physical field. When the N'th physical field
1417 * is a dropped column (attisdropped) then the N'th list element can just
1418 * be a NULL constant. (This case can only occur for named composite types,
1419 * not RECORD types, since those are built from the RowExpr itself rather
1420 * than vice versa.) It is important not to assume that length(args) is
1421 * the same as the number of columns logically present in the rowtype.
1422 *
1423 * colnames provides field names if the ROW() result is of type RECORD.
1424 * Names *must* be provided if row_typeid is RECORDOID; but if it is a
1425 * named composite type, colnames will be ignored in favor of using the
1426 * type's cataloged field names, so colnames should be NIL. Like the
1427 * args list, colnames is defined to be one-for-one with physical fields
1428 * of the rowtype (although dropped columns shouldn't appear in the
1429 * RECORD case, so this fine point is currently moot).
1430 */
1431 typedef struct RowExpr
1432 {
1433 Expr xpr;
1434 List *args; /* the fields */
1435
1436 /* RECORDOID or a composite type's ID */
1437 Oid row_typeid pg_node_attr(query_jumble_ignore);
1438
1439 /*
1440 * row_typeid cannot be a domain over composite, only plain composite. To
1441 * create a composite domain value, apply CoerceToDomain to the RowExpr.
1442 *
1443 * Note: we deliberately do NOT store a typmod. Although a typmod will be
1444 * associated with specific RECORD types at runtime, it will differ for
1445 * different backends, and so cannot safely be stored in stored
1446 * parsetrees. We must assume typmod -1 for a RowExpr node.
1447 *
1448 * We don't need to store a collation either. The result type is
1449 * necessarily composite, and composite types never have a collation.
1450 */
1451
1452 /* how to display this node */
1453 CoercionForm row_format pg_node_attr(query_jumble_ignore);
1454
1455 /* list of String, or NIL */
1456 List *colnames pg_node_attr(query_jumble_ignore);
1457
1458 ParseLoc location; /* token location, or -1 if unknown */
1459 } RowExpr;
1460
1461 /*
1462 * RowCompareExpr - row-wise comparison, such as (a, b) <= (1, 2)
1463 *
1464 * We support row comparison for any operator that can be determined to
1465 * act like =, <>, <, <=, >, or >= (we determine this by looking for the
1466 * operator in btree opfamilies). Note that the same operator name might
1467 * map to a different operator for each pair of row elements, since the
1468 * element datatypes can vary.
1469 *
1470 * A RowCompareExpr node is only generated for the < <= > >= cases;
1471 * the = and <> cases are translated to simple AND or OR combinations
1472 * of the pairwise comparisons.
1473 */
1474 typedef struct RowCompareExpr
1475 {
1476 Expr xpr;
1477
1478 /* LT LE GE or GT, never EQ or NE */
1479 CompareType cmptype;
1480 /* OID list of pairwise comparison ops */
1481 List *opnos pg_node_attr(query_jumble_ignore);
1482 /* OID list of containing operator families */
1483 List *opfamilies pg_node_attr(query_jumble_ignore);
1484 /* OID list of collations for comparisons */
1485 List *inputcollids pg_node_attr(query_jumble_ignore);
1486 /* the left-hand input arguments */
1487 List *largs;
1488 /* the right-hand input arguments */
1489 List *rargs;
1490 } RowCompareExpr;
1491
1492 /*
1493 * CoalesceExpr - a COALESCE expression
1494 */
1495 typedef struct CoalesceExpr
1496 {
1497 Expr xpr;
1498 /* type of expression result */
1499 Oid coalescetype pg_node_attr(query_jumble_ignore);
1500 /* OID of collation, or InvalidOid if none */
1501 Oid coalescecollid pg_node_attr(query_jumble_ignore);
1502 /* the arguments */
1503 List *args;
1504 /* token location, or -1 if unknown */
1505 ParseLoc location;
1506 } CoalesceExpr;
1507
1508 /*
1509 * MinMaxExpr - a GREATEST or LEAST function
1510 */
1511 typedef enum MinMaxOp
1512 {
1513 IS_GREATEST,
1514 IS_LEAST
1515 } MinMaxOp;
1516
1517 typedef struct MinMaxExpr
1518 {
1519 Expr xpr;
1520 /* common type of arguments and result */
1521 Oid minmaxtype pg_node_attr(query_jumble_ignore);
1522 /* OID of collation of result */
1523 Oid minmaxcollid pg_node_attr(query_jumble_ignore);
1524 /* OID of collation that function should use */
1525 Oid inputcollid pg_node_attr(query_jumble_ignore);
1526 /* function to execute */
1527 MinMaxOp op;
1528 /* the arguments */
1529 List *args;
1530 /* token location, or -1 if unknown */
1531 ParseLoc location;
1532 } MinMaxExpr;
1533
1534 /*
1535 * SQLValueFunction - parameterless functions with special grammar productions
1536 *
1537 * The SQL standard categorizes some of these as <datetime value function>
1538 * and others as <general value specification>. We call 'em SQLValueFunctions
1539 * for lack of a better term. We store type and typmod of the result so that
1540 * some code doesn't need to know each function individually, and because
1541 * we would need to store typmod anyway for some of the datetime functions.
1542 * Note that currently, all variants return non-collating datatypes, so we do
1543 * not need a collation field; also, all these functions are stable.
1544 */
1545 typedef enum SQLValueFunctionOp
1546 {
1547 SVFOP_CURRENT_DATE,
1548 SVFOP_CURRENT_TIME,
1549 SVFOP_CURRENT_TIME_N,
1550 SVFOP_CURRENT_TIMESTAMP,
1551 SVFOP_CURRENT_TIMESTAMP_N,
1552 SVFOP_LOCALTIME,
1553 SVFOP_LOCALTIME_N,
1554 SVFOP_LOCALTIMESTAMP,
1555 SVFOP_LOCALTIMESTAMP_N,
1556 SVFOP_CURRENT_ROLE,
1557 SVFOP_CURRENT_USER,
1558 SVFOP_USER,
1559 SVFOP_SESSION_USER,
1560 SVFOP_CURRENT_CATALOG,
1561 SVFOP_CURRENT_SCHEMA,
1562 } SQLValueFunctionOp;
1563
1564 typedef struct SQLValueFunction
1565 {
1566 Expr xpr;
1567 SQLValueFunctionOp op; /* which function this is */
1568
1569 /*
1570 * Result type/typmod. Type is fully determined by "op", so no need to
1571 * include this Oid in the query jumbling.
1572 */
1573 Oid type pg_node_attr(query_jumble_ignore);
1574 int32 typmod;
1575 ParseLoc location; /* token location, or -1 if unknown */
1576 } SQLValueFunction;
1577
1578 /*
1579 * XmlExpr - various SQL/XML functions requiring special grammar productions
1580 *
1581 * 'name' carries the "NAME foo" argument (already XML-escaped).
1582 * 'named_args' and 'arg_names' represent an xml_attribute list.
1583 * 'args' carries all other arguments.
1584 *
1585 * Note: result type/typmod/collation are not stored, but can be deduced
1586 * from the XmlExprOp. The type/typmod fields are just used for display
1587 * purposes, and are NOT necessarily the true result type of the node.
1588 */
1589 typedef enum XmlExprOp
1590 {
1591 IS_XMLCONCAT, /* XMLCONCAT(args) */
1592 IS_XMLELEMENT, /* XMLELEMENT(name, xml_attributes, args) */
1593 IS_XMLFOREST, /* XMLFOREST(xml_attributes) */
1594 IS_XMLPARSE, /* XMLPARSE(text, is_doc, preserve_ws) */
1595 IS_XMLPI, /* XMLPI(name [, args]) */
1596 IS_XMLROOT, /* XMLROOT(xml, version, standalone) */
1597 IS_XMLSERIALIZE, /* XMLSERIALIZE(is_document, xmlval, indent) */
1598 IS_DOCUMENT, /* xmlval IS DOCUMENT */
1599 } XmlExprOp;
1600
1601 typedef enum XmlOptionType
1602 {
1603 XMLOPTION_DOCUMENT,
1604 XMLOPTION_CONTENT,
1605 } XmlOptionType;
1606
1607 typedef struct XmlExpr
1608 {
1609 Expr xpr;
1610 /* xml function ID */
1611 XmlExprOp op;
1612 /* name in xml(NAME foo ...) syntaxes */
1613 char *name pg_node_attr(query_jumble_ignore);
1614 /* non-XML expressions for xml_attributes */
1615 List *named_args;
1616 /* parallel list of String values */
1617 List *arg_names pg_node_attr(query_jumble_ignore);
1618 /* list of expressions */
1619 List *args;
1620 /* DOCUMENT or CONTENT */
1621 XmlOptionType xmloption pg_node_attr(query_jumble_ignore);
1622 /* INDENT option for XMLSERIALIZE */
1623 bool indent;
1624 /* target type/typmod for XMLSERIALIZE */
1625 Oid type pg_node_attr(query_jumble_ignore);
1626 int32 typmod pg_node_attr(query_jumble_ignore);
1627 /* token location, or -1 if unknown */
1628 ParseLoc location;
1629 } XmlExpr;
1630
1631 /*
1632 * JsonEncoding -
1633 * representation of JSON ENCODING clause
1634 */
1635 typedef enum JsonEncoding
1636 {
1637 JS_ENC_DEFAULT, /* unspecified */
1638 JS_ENC_UTF8,
1639 JS_ENC_UTF16,
1640 JS_ENC_UTF32,
1641 } JsonEncoding;
1642
1643 /*
1644 * JsonFormatType -
1645 * enumeration of JSON formats used in JSON FORMAT clause
1646 */
1647 typedef enum JsonFormatType
1648 {
1649 JS_FORMAT_DEFAULT, /* unspecified */
1650 JS_FORMAT_JSON, /* FORMAT JSON [ENCODING ...] */
1651 JS_FORMAT_JSONB, /* implicit internal format for RETURNING
1652 * jsonb */
1653 } JsonFormatType;
1654
1655 /*
1656 * JsonFormat -
1657 * representation of JSON FORMAT clause
1658 */
1659 typedef struct JsonFormat
1660 {
1661 NodeTag type;
1662 JsonFormatType format_type; /* format type */
1663 JsonEncoding encoding; /* JSON encoding */
1664 ParseLoc location; /* token location, or -1 if unknown */
1665 } JsonFormat;
1666
1667 /*
1668 * JsonReturning -
1669 * transformed representation of JSON RETURNING clause
1670 */
1671 typedef struct JsonReturning
1672 {
1673 NodeTag type;
1674 JsonFormat *format; /* output JSON format */
1675 Oid typid; /* target type Oid */
1676 int32 typmod; /* target type modifier */
1677 } JsonReturning;
1678
1679 /*
1680 * JsonValueExpr -
1681 * representation of JSON value expression (expr [FORMAT JsonFormat])
1682 *
1683 * raw_expr is the user-specified value, while formatted_expr is the value
1684 * obtained by coercing raw_expr to the type required by either the FORMAT
1685 * clause or an enclosing node's RETURNING clause.
1686 *
1687 * When deparsing a JsonValueExpr, get_rule_expr() prints raw_expr. However,
1688 * during the evaluation of a JsonValueExpr, the value of formatted_expr
1689 * takes precedence over that of raw_expr.
1690 */
1691 typedef struct JsonValueExpr
1692 {
1693 NodeTag type;
1694 Expr *raw_expr; /* user-specified expression */
1695 Expr *formatted_expr; /* coerced formatted expression */
1696 JsonFormat *format; /* FORMAT clause, if specified */
1697 } JsonValueExpr;
1698
1699 typedef enum JsonConstructorType
1700 {
1701 JSCTOR_JSON_OBJECT = 1,
1702 JSCTOR_JSON_ARRAY = 2,
1703 JSCTOR_JSON_OBJECTAGG = 3,
1704 JSCTOR_JSON_ARRAYAGG = 4,
1705 JSCTOR_JSON_PARSE = 5,
1706 JSCTOR_JSON_SCALAR = 6,
1707 JSCTOR_JSON_SERIALIZE = 7,
1708 } JsonConstructorType;
1709
1710 /*
1711 * JsonConstructorExpr -
1712 * wrapper over FuncExpr/Aggref/WindowFunc for SQL/JSON constructors
1713 */
1714 typedef struct JsonConstructorExpr
1715 {
1716 Expr xpr;
1717 JsonConstructorType type; /* constructor type */
1718 List *args;
1719 Expr *func; /* underlying json[b]_xxx() function call */
1720 Expr *coercion; /* coercion to RETURNING type */
1721 JsonReturning *returning; /* RETURNING clause */
1722 bool absent_on_null; /* ABSENT ON NULL? */
1723 bool unique; /* WITH UNIQUE KEYS? (JSON_OBJECT[AGG] only) */
1724 ParseLoc location;
1725 } JsonConstructorExpr;
1726
1727 /*
1728 * JsonValueType -
1729 * representation of JSON item type in IS JSON predicate
1730 */
1731 typedef enum JsonValueType
1732 {
1733 JS_TYPE_ANY, /* IS JSON [VALUE] */
1734 JS_TYPE_OBJECT, /* IS JSON OBJECT */
1735 JS_TYPE_ARRAY, /* IS JSON ARRAY */
1736 JS_TYPE_SCALAR, /* IS JSON SCALAR */
1737 } JsonValueType;
1738
1739 /*
1740 * JsonIsPredicate -
1741 * representation of IS JSON predicate
1742 */
1743 typedef struct JsonIsPredicate
1744 {
1745 NodeTag type;
1746 Node *expr; /* subject expression */
1747 JsonFormat *format; /* FORMAT clause, if specified */
1748 JsonValueType item_type; /* JSON item type */
1749 bool unique_keys; /* check key uniqueness? */
1750 ParseLoc location; /* token location, or -1 if unknown */
1751 } JsonIsPredicate;
1752
1753 /* Nodes used in SQL/JSON query functions */
1754
1755 /*
1756 * JsonWrapper -
1757 * representation of WRAPPER clause for JSON_QUERY()
1758 */
1759 typedef enum JsonWrapper
1760 {
1761 JSW_UNSPEC,
1762 JSW_NONE,
1763 JSW_CONDITIONAL,
1764 JSW_UNCONDITIONAL,
1765 } JsonWrapper;
1766
1767 /*
1768 * JsonBehaviorType -
1769 * enumeration of behavior types used in SQL/JSON ON ERROR/EMPTY clauses
1770 *
1771 * If enum members are reordered, get_json_behavior() from ruleutils.c
1772 * must be updated accordingly.
1773 */
1774 typedef enum JsonBehaviorType
1775 {
1776 JSON_BEHAVIOR_NULL = 0,
1777 JSON_BEHAVIOR_ERROR,
1778 JSON_BEHAVIOR_EMPTY,
1779 JSON_BEHAVIOR_TRUE,
1780 JSON_BEHAVIOR_FALSE,
1781 JSON_BEHAVIOR_UNKNOWN,
1782 JSON_BEHAVIOR_EMPTY_ARRAY,
1783 JSON_BEHAVIOR_EMPTY_OBJECT,
1784 JSON_BEHAVIOR_DEFAULT,
1785 } JsonBehaviorType;
1786
1787 /*
1788 * JsonBehavior
1789 * Specifications for ON ERROR / ON EMPTY behaviors of SQL/JSON
1790 * query functions specified by a JsonExpr
1791 *
1792 * 'expr' is the expression to emit when a given behavior (EMPTY or ERROR)
1793 * occurs on evaluating the SQL/JSON query function. 'coerce' is set to true
1794 * if 'expr' isn't already of the expected target type given by
1795 * JsonExpr.returning.
1796 */
1797 typedef struct JsonBehavior
1798 {
1799 NodeTag type;
1800
1801 JsonBehaviorType btype;
1802 Node *expr;
1803 bool coerce;
1804 ParseLoc location; /* token location, or -1 if unknown */
1805 } JsonBehavior;
1806
1807 /*
1808 * JsonExprOp -
1809 * enumeration of SQL/JSON query function types
1810 */
1811 typedef enum JsonExprOp
1812 {
1813 JSON_EXISTS_OP, /* JSON_EXISTS() */
1814 JSON_QUERY_OP, /* JSON_QUERY() */
1815 JSON_VALUE_OP, /* JSON_VALUE() */
1816 JSON_TABLE_OP, /* JSON_TABLE() */
1817 } JsonExprOp;
1818
1819 /*
1820 * JsonExpr -
1821 * Transformed representation of JSON_VALUE(), JSON_QUERY(), and
1822 * JSON_EXISTS()
1823 */
1824 typedef struct JsonExpr
1825 {
1826 Expr xpr;
1827
1828 JsonExprOp op;
1829
1830 char *column_name; /* JSON_TABLE() column name or NULL if this is
1831 * not for a JSON_TABLE() */
1832
1833 /* jsonb-valued expression to query */
1834 Node *formatted_expr;
1835
1836 /* Format of the above expression needed by ruleutils.c */
1837 JsonFormat *format;
1838
1839 /* jsonpath-valued expression containing the query pattern */
1840 Node *path_spec;
1841
1842 /* Expected type/format of the output. */
1843 JsonReturning *returning;
1844
1845 /* Information about the PASSING argument expressions */
1846 List *passing_names;
1847 List *passing_values;
1848
1849 /* User-specified or default ON EMPTY and ON ERROR behaviors */
1850 JsonBehavior *on_empty;
1851 JsonBehavior *on_error;
1852
1853 /*
1854 * Information about converting the result of jsonpath functions
1855 * JsonPathQuery() and JsonPathValue() to the RETURNING type.
1856 */
1857 bool use_io_coercion;
1858 bool use_json_coercion;
1859
1860 /* WRAPPER specification for JSON_QUERY */
1861 JsonWrapper wrapper;
1862
1863 /* KEEP or OMIT QUOTES for singleton scalars returned by JSON_QUERY() */
1864 bool omit_quotes;
1865
1866 /* JsonExpr's collation. */
1867 Oid collation;
1868
1869 /* Original JsonFuncExpr's location */
1870 ParseLoc location;
1871 } JsonExpr;
1872
1873 /*
1874 * JsonTablePath
1875 * A JSON path expression to be computed as part of evaluating
1876 * a JSON_TABLE plan node
1877 */
1878 typedef struct JsonTablePath
1879 {
1880 NodeTag type;
1881
1882 Const *value;
1883 char *name;
1884 } JsonTablePath;
1885
1886 /*
1887 * JsonTablePlan -
1888 * Abstract class to represent different types of JSON_TABLE "plans".
1889 * A plan is used to generate a "row pattern" value by evaluating a JSON
1890 * path expression against an input JSON document, which is then used for
1891 * populating JSON_TABLE() columns
1892 */
1893 typedef struct JsonTablePlan
1894 {
1895 pg_node_attr(abstract)
1896
1897 NodeTag type;
1898 } JsonTablePlan;
1899
1900 /*
1901 * JSON_TABLE plan to evaluate a JSON path expression and NESTED paths, if
1902 * any.
1903 */
1904 typedef struct JsonTablePathScan
1905 {
1906 JsonTablePlan plan;
1907
1908 /* JSON path to evaluate */
1909 JsonTablePath *path;
1910
1911 /*
1912 * ERROR/EMPTY ON ERROR behavior; only significant in the plan for the
1913 * top-level path.
1914 */
1915 bool errorOnError;
1916
1917 /* Plan(s) for nested columns, if any. */
1918 JsonTablePlan *child;
1919
1920 /*
1921 * 0-based index in TableFunc.colvalexprs of the 1st and the last column
1922 * covered by this plan. Both are -1 if all columns are nested and thus
1923 * computed by the child plan(s).
1924 */
1925 int colMin;
1926 int colMax;
1927 } JsonTablePathScan;
1928
1929 /*
1930 * JsonTableSiblingJoin -
1931 * Plan to join rows of sibling NESTED COLUMNS clauses in the same parent
1932 * COLUMNS clause
1933 */
1934 typedef struct JsonTableSiblingJoin
1935 {
1936 JsonTablePlan plan;
1937
1938 JsonTablePlan *lplan;
1939 JsonTablePlan *rplan;
1940 } JsonTableSiblingJoin;
1941
1942 /* ----------------
1943 * NullTest
1944 *
1945 * NullTest represents the operation of testing a value for NULLness.
1946 * The appropriate test is performed and returned as a boolean Datum.
1947 *
1948 * When argisrow is false, this simply represents a test for the null value.
1949 *
1950 * When argisrow is true, the input expression must yield a rowtype, and
1951 * the node implements "row IS [NOT] NULL" per the SQL standard. This
1952 * includes checking individual fields for NULLness when the row datum
1953 * itself isn't NULL.
1954 *
1955 * NOTE: the combination of a rowtype input and argisrow==false does NOT
1956 * correspond to the SQL notation "row IS [NOT] NULL"; instead, this case
1957 * represents the SQL notation "row IS [NOT] DISTINCT FROM NULL".
1958 * ----------------
1959 */
1960
1961 typedef enum NullTestType
1962 {
1963 IS_NULL, IS_NOT_NULL
1964 } NullTestType;
1965
1966 typedef struct NullTest
1967 {
1968 Expr xpr;
1969 Expr *arg; /* input expression */
1970 NullTestType nulltesttype; /* IS NULL, IS NOT NULL */
1971 /* T to perform field-by-field null checks */
1972 bool argisrow pg_node_attr(query_jumble_ignore);
1973 ParseLoc location; /* token location, or -1 if unknown */
1974 } NullTest;
1975
1976 /*
1977 * BooleanTest
1978 *
1979 * BooleanTest represents the operation of determining whether a boolean
1980 * is TRUE, FALSE, or UNKNOWN (ie, NULL). All six meaningful combinations
1981 * are supported. Note that a NULL input does *not* cause a NULL result.
1982 * The appropriate test is performed and returned as a boolean Datum.
1983 */
1984
1985 typedef enum BoolTestType
1986 {
1987 IS_TRUE, IS_NOT_TRUE, IS_FALSE, IS_NOT_FALSE, IS_UNKNOWN, IS_NOT_UNKNOWN
1988 } BoolTestType;
1989
1990 typedef struct BooleanTest
1991 {
1992 Expr xpr;
1993 Expr *arg; /* input expression */
1994 BoolTestType booltesttype; /* test type */
1995 ParseLoc location; /* token location, or -1 if unknown */
1996 } BooleanTest;
1997
1998
1999 /*
2000 * MergeAction
2001 *
2002 * Transformed representation of a WHEN clause in a MERGE statement
2003 */
2004
2005 typedef enum MergeMatchKind
2006 {
2007 MERGE_WHEN_MATCHED,
2008 MERGE_WHEN_NOT_MATCHED_BY_SOURCE,
2009 MERGE_WHEN_NOT_MATCHED_BY_TARGET
2010 } MergeMatchKind;
2011
2012 #define NUM_MERGE_MATCH_KINDS (MERGE_WHEN_NOT_MATCHED_BY_TARGET + 1)
2013
2014 typedef struct MergeAction
2015 {
2016 NodeTag type;
2017 MergeMatchKind matchKind; /* MATCHED/NOT MATCHED BY SOURCE/TARGET */
2018 CmdType commandType; /* INSERT/UPDATE/DELETE/DO NOTHING */
2019 /* OVERRIDING clause */
2020 OverridingKind override pg_node_attr(query_jumble_ignore);
2021 Node *qual; /* transformed WHEN conditions */
2022 List *targetList; /* the target list (of TargetEntry) */
2023 /* target attribute numbers of an UPDATE */
2024 List *updateColnos pg_node_attr(query_jumble_ignore);
2025 } MergeAction;
2026
2027 /*
2028 * CoerceToDomain
2029 *
2030 * CoerceToDomain represents the operation of coercing a value to a domain
2031 * type. At runtime (and not before) the precise set of constraints to be
2032 * checked will be determined. If the value passes, it is returned as the
2033 * result; if not, an error is raised. Note that this is equivalent to
2034 * RelabelType in the scenario where no constraints are applied.
2035 */
2036 typedef struct CoerceToDomain
2037 {
2038 Expr xpr;
2039 Expr *arg; /* input expression */
2040 Oid resulttype; /* domain type ID (result type) */
2041 /* output typmod (currently always -1) */
2042 int32 resulttypmod pg_node_attr(query_jumble_ignore);
2043 /* OID of collation, or InvalidOid if none */
2044 Oid resultcollid pg_node_attr(query_jumble_ignore);
2045 /* how to display this node */
2046 CoercionForm coercionformat pg_node_attr(query_jumble_ignore);
2047 ParseLoc location; /* token location, or -1 if unknown */
2048 } CoerceToDomain;
2049
2050 /*
2051 * Placeholder node for the value to be processed by a domain's check
2052 * constraint. This is effectively like a Param, but can be implemented more
2053 * simply since we need only one replacement value at a time.
2054 *
2055 * Note: the typeId/typeMod/collation will be set from the domain's base type,
2056 * not the domain itself. This is because we shouldn't consider the value
2057 * to be a member of the domain if we haven't yet checked its constraints.
2058 */
2059 typedef struct CoerceToDomainValue
2060 {
2061 Expr xpr;
2062 /* type for substituted value */
2063 Oid typeId;
2064 /* typemod for substituted value */
2065 int32 typeMod pg_node_attr(query_jumble_ignore);
2066 /* collation for the substituted value */
2067 Oid collation pg_node_attr(query_jumble_ignore);
2068 /* token location, or -1 if unknown */
2069 ParseLoc location;
2070 } CoerceToDomainValue;
2071
2072 /*
2073 * Placeholder node for a DEFAULT marker in an INSERT or UPDATE command.
2074 *
2075 * This is not an executable expression: it must be replaced by the actual
2076 * column default expression during rewriting. But it is convenient to
2077 * treat it as an expression node during parsing and rewriting.
2078 */
2079 typedef struct SetToDefault
2080 {
2081 Expr xpr;
2082 /* type for substituted value */
2083 Oid typeId;
2084 /* typemod for substituted value */
2085 int32 typeMod pg_node_attr(query_jumble_ignore);
2086 /* collation for the substituted value */
2087 Oid collation pg_node_attr(query_jumble_ignore);
2088 /* token location, or -1 if unknown */
2089 ParseLoc location;
2090 } SetToDefault;
2091
2092 /*
2093 * Node representing [WHERE] CURRENT OF cursor_name
2094 *
2095 * CURRENT OF is a bit like a Var, in that it carries the rangetable index
2096 * of the target relation being constrained; this aids placing the expression
2097 * correctly during planning. We can assume however that its "levelsup" is
2098 * always zero, due to the syntactic constraints on where it can appear.
2099 * Also, cvarno will always be a true RT index, never INNER_VAR etc.
2100 *
2101 * The referenced cursor can be represented either as a hardwired string
2102 * or as a reference to a run-time parameter of type REFCURSOR. The latter
2103 * case is for the convenience of plpgsql.
2104 */
2105 typedef struct CurrentOfExpr
2106 {
2107 Expr xpr;
2108 Index cvarno; /* RT index of target relation */
2109 char *cursor_name; /* name of referenced cursor, or NULL */
2110 int cursor_param; /* refcursor parameter number, or 0 */
2111 } CurrentOfExpr;
2112
2113 /*
2114 * NextValueExpr - get next value from sequence
2115 *
2116 * This has the same effect as calling the nextval() function, but it does not
2117 * check permissions on the sequence. This is used for identity columns,
2118 * where the sequence is an implicit dependency without its own permissions.
2119 */
2120 typedef struct NextValueExpr
2121 {
2122 Expr xpr;
2123 Oid seqid;
2124 Oid typeId;
2125 } NextValueExpr;
2126
2127 /*
2128 * InferenceElem - an element of a unique index inference specification
2129 *
2130 * This mostly matches the structure of IndexElems, but having a dedicated
2131 * primnode allows for a clean separation between the use of index parameters
2132 * by utility commands, and this node.
2133 */
2134 typedef struct InferenceElem
2135 {
2136 Expr xpr;
2137 Node *expr; /* expression to infer from, or NULL */
2138 Oid infercollid; /* OID of collation, or InvalidOid */
2139 Oid inferopclass; /* OID of att opclass, or InvalidOid */
2140 } InferenceElem;
2141
2142 /*
2143 * ReturningExpr - return OLD/NEW.(expression) in RETURNING list
2144 *
2145 * This is used when updating an auto-updatable view and returning a view
2146 * column that is not simply a Var referring to the base relation. In such
2147 * cases, OLD/NEW.viewcol can expand to an arbitrary expression, but the
2148 * result is required to be NULL if the OLD/NEW row doesn't exist. To handle
2149 * this, the rewriter wraps the expanded expression in a ReturningExpr, which
2150 * is equivalent to "CASE WHEN (OLD/NEW row exists) THEN (expr) ELSE NULL".
2151 *
2152 * A similar situation can arise when rewriting the RETURNING clause of a
2153 * rule, which may also contain arbitrary expressions.
2154 *
2155 * ReturningExpr nodes never appear in a parsed Query --- they are only ever
2156 * inserted by the rewriter and the planner.
2157 */
2158 typedef struct ReturningExpr
2159 {
2160 Expr xpr;
2161 int retlevelsup; /* > 0 if it belongs to outer query */
2162 bool retold; /* true for OLD, false for NEW */
2163 Expr *retexpr; /* expression to be returned */
2164 } ReturningExpr;
2165
2166 /*--------------------
2167 * TargetEntry -
2168 * a target entry (used in query target lists)
2169 *
2170 * Strictly speaking, a TargetEntry isn't an expression node (since it can't
2171 * be evaluated by ExecEvalExpr). But we treat it as one anyway, since in
2172 * very many places it's convenient to process a whole query targetlist as a
2173 * single expression tree.
2174 *
2175 * In a SELECT's targetlist, resno should always be equal to the item's
2176 * ordinal position (counting from 1). However, in an INSERT or UPDATE
2177 * targetlist, resno represents the attribute number of the destination
2178 * column for the item; so there may be missing or out-of-order resnos.
2179 * It is even legal to have duplicated resnos; consider
2180 * UPDATE table SET arraycol[1] = ..., arraycol[2] = ..., ...
2181 * In an INSERT, the rewriter and planner will normalize the tlist by
2182 * reordering it into physical column order and filling in default values
2183 * for any columns not assigned values by the original query. In an UPDATE,
2184 * after the rewriter merges multiple assignments for the same column, the
2185 * planner extracts the target-column numbers into a separate "update_colnos"
2186 * list, and then renumbers the tlist elements serially. Thus, tlist resnos
2187 * match ordinal position in all tlists seen by the executor; but it is wrong
2188 * to assume that before planning has happened.
2189 *
2190 * resname is required to represent the correct column name in non-resjunk
2191 * entries of top-level SELECT targetlists, since it will be used as the
2192 * column title sent to the frontend. In most other contexts it is only
2193 * a debugging aid, and may be wrong or even NULL. (In particular, it may
2194 * be wrong in a tlist from a stored rule, if the referenced column has been
2195 * renamed by ALTER TABLE since the rule was made. Also, the planner tends
2196 * to store NULL rather than look up a valid name for tlist entries in
2197 * non-toplevel plan nodes.) In resjunk entries, resname should be either
2198 * a specific system-generated name (such as "ctid") or NULL; anything else
2199 * risks confusing ExecGetJunkAttribute!
2200 *
2201 * ressortgroupref is used in the representation of ORDER BY, GROUP BY, and
2202 * DISTINCT items. Targetlist entries with ressortgroupref=0 are not
2203 * sort/group items. If ressortgroupref>0, then this item is an ORDER BY,
2204 * GROUP BY, and/or DISTINCT target value. No two entries in a targetlist
2205 * may have the same nonzero ressortgroupref --- but there is no particular
2206 * meaning to the nonzero values, except as tags. (For example, one must
2207 * not assume that lower ressortgroupref means a more significant sort key.)
2208 * The order of the associated SortGroupClause lists determine the semantics.
2209 *
2210 * resorigtbl/resorigcol identify the source of the column, if it is a
2211 * simple reference to a column of a base table (or view). If it is not
2212 * a simple reference, these fields are zeroes.
2213 *
2214 * If resjunk is true then the column is a working column (such as a sort key)
2215 * that should be removed from the final output of the query. Resjunk columns
2216 * must have resnos that cannot duplicate any regular column's resno. Also
2217 * note that there are places that assume resjunk columns come after non-junk
2218 * columns.
2219 *--------------------
2220 */
2221 typedef struct TargetEntry
2222 {
2223 Expr xpr;
2224 /* expression to evaluate */
2225 Expr *expr;
2226 /* attribute number (see notes above) */
2227 AttrNumber resno;
2228 /* name of the column (could be NULL) */
2229 char *resname pg_node_attr(query_jumble_ignore);
2230 /* nonzero if referenced by a sort/group clause */
2231 Index ressortgroupref;
2232 /* OID of column's source table */
2233 Oid resorigtbl pg_node_attr(query_jumble_ignore);
2234 /* column's number in source table */
2235 AttrNumber resorigcol pg_node_attr(query_jumble_ignore);
2236 /* set to true to eliminate the attribute from final target list */
2237 bool resjunk pg_node_attr(query_jumble_ignore);
2238 } TargetEntry;
2239
2240
2241 /* ----------------------------------------------------------------
2242 * node types for join trees
2243 *
2244 * The leaves of a join tree structure are RangeTblRef nodes. Above
2245 * these, JoinExpr nodes can appear to denote a specific kind of join
2246 * or qualified join. Also, FromExpr nodes can appear to denote an
2247 * ordinary cross-product join ("FROM foo, bar, baz WHERE ...").
2248 * FromExpr is like a JoinExpr of jointype JOIN_INNER, except that it
2249 * may have any number of child nodes, not just two.
2250 *
2251 * NOTE: the top level of a Query's jointree is always a FromExpr.
2252 * Even if the jointree contains no rels, there will be a FromExpr.
2253 *
2254 * NOTE: the qualification expressions present in JoinExpr nodes are
2255 * *in addition to* the query's main WHERE clause, which appears as the
2256 * qual of the top-level FromExpr. The reason for associating quals with
2257 * specific nodes in the jointree is that the position of a qual is critical
2258 * when outer joins are present. (If we enforce a qual too soon or too late,
2259 * that may cause the outer join to produce the wrong set of NULL-extended
2260 * rows.) If all joins are inner joins then all the qual positions are
2261 * semantically interchangeable.
2262 *
2263 * NOTE: in the raw output of gram.y, a join tree contains RangeVar,
2264 * RangeSubselect, and RangeFunction nodes, which are all replaced by
2265 * RangeTblRef nodes during the parse analysis phase. Also, the top-level
2266 * FromExpr is added during parse analysis; the grammar regards FROM and
2267 * WHERE as separate.
2268 * ----------------------------------------------------------------
2269 */
2270
2271 /*
2272 * RangeTblRef - reference to an entry in the query's rangetable
2273 *
2274 * We could use direct pointers to the RT entries and skip having these
2275 * nodes, but multiple pointers to the same node in a querytree cause
2276 * lots of headaches, so it seems better to store an index into the RT.
2277 */
2278 typedef struct RangeTblRef
2279 {
2280 NodeTag type;
2281 int rtindex;
2282 } RangeTblRef;
2283
2284 /*----------
2285 * JoinExpr - for SQL JOIN expressions
2286 *
2287 * isNatural, usingClause, and quals are interdependent. The user can write
2288 * only one of NATURAL, USING(), or ON() (this is enforced by the grammar).
2289 * If he writes NATURAL then parse analysis generates the equivalent USING()
2290 * list, and from that fills in "quals" with the right equality comparisons.
2291 * If he writes USING() then "quals" is filled with equality comparisons.
2292 * If he writes ON() then only "quals" is set. Note that NATURAL/USING
2293 * are not equivalent to ON() since they also affect the output column list.
2294 *
2295 * alias is an Alias node representing the AS alias-clause attached to the
2296 * join expression, or NULL if no clause. NB: presence or absence of the
2297 * alias has a critical impact on semantics, because a join with an alias
2298 * restricts visibility of the tables/columns inside it.
2299 *
2300 * join_using_alias is an Alias node representing the join correlation
2301 * name that SQL:2016 and later allow to be attached to JOIN/USING.
2302 * Its column alias list includes only the common column names from USING,
2303 * and it does not restrict visibility of the join's input tables.
2304 *
2305 * During parse analysis, an RTE is created for the Join, and its index
2306 * is filled into rtindex. This RTE is present mainly so that Vars can
2307 * be created that refer to the outputs of the join. The planner sometimes
2308 * generates JoinExprs internally; these can have rtindex = 0 if there are
2309 * no join alias variables referencing such joins.
2310 *----------
2311 */
2312 typedef struct JoinExpr
2313 {
2314 NodeTag type;
2315 JoinType jointype; /* type of join */
2316 bool isNatural; /* Natural join? Will need to shape table */
2317 Node *larg; /* left subtree */
2318 Node *rarg; /* right subtree */
2319 /* USING clause, if any (list of String) */
2320 List *usingClause pg_node_attr(query_jumble_ignore);
2321 /* alias attached to USING clause, if any */
2322 Alias *join_using_alias pg_node_attr(query_jumble_ignore);
2323 /* qualifiers on join, if any */
2324 Node *quals;
2325 /* user-written alias clause, if any */
2326 Alias *alias pg_node_attr(query_jumble_ignore);
2327 /* RT index assigned for join, or 0 */
2328 int rtindex;
2329 } JoinExpr;
2330
2331 /*----------
2332 * FromExpr - represents a FROM ... WHERE ... construct
2333 *
2334 * This is both more flexible than a JoinExpr (it can have any number of
2335 * children, including zero) and less so --- we don't need to deal with
2336 * aliases and so on. The output column set is implicitly just the union
2337 * of the outputs of the children.
2338 *----------
2339 */
2340 typedef struct FromExpr
2341 {
2342 NodeTag type;
2343 List *fromlist; /* List of join subtrees */
2344 Node *quals; /* qualifiers on join, if any */
2345 } FromExpr;
2346
2347 /*----------
2348 * OnConflictExpr - represents an ON CONFLICT DO ... expression
2349 *
2350 * The optimizer requires a list of inference elements, and optionally a WHERE
2351 * clause to infer a unique index. The unique index (or, occasionally,
2352 * indexes) inferred are used to arbitrate whether or not the alternative ON
2353 * CONFLICT path is taken.
2354 *----------
2355 */
2356 typedef struct OnConflictExpr
2357 {
2358 NodeTag type;
2359 OnConflictAction action; /* DO NOTHING or UPDATE? */
2360
2361 /* Arbiter */
2362 List *arbiterElems; /* unique index arbiter list (of
2363 * InferenceElem's) */
2364 Node *arbiterWhere; /* unique index arbiter WHERE clause */
2365 Oid constraint; /* pg_constraint OID for arbiter */
2366
2367 /* ON CONFLICT UPDATE */
2368 List *onConflictSet; /* List of ON CONFLICT SET TargetEntrys */
2369 Node *onConflictWhere; /* qualifiers to restrict UPDATE to */
2370 int exclRelIndex; /* RT index of 'excluded' relation */
2371 List *exclRelTlist; /* tlist of the EXCLUDED pseudo relation */
2372 } OnConflictExpr;
2373
2374 #endif /* PRIMNODES_H */