PostgreSQL Source Code  git master
parse_expr.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * parse_expr.c
4  * handle expressions in parser
5  *
6  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/parser/parse_expr.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "catalog/pg_aggregate.h"
19 #include "catalog/pg_proc.h"
20 #include "catalog/pg_type.h"
21 #include "commands/dbcommands.h"
22 #include "miscadmin.h"
23 #include "nodes/makefuncs.h"
24 #include "nodes/nodeFuncs.h"
25 #include "optimizer/optimizer.h"
26 #include "parser/analyze.h"
27 #include "parser/parse_agg.h"
28 #include "parser/parse_clause.h"
29 #include "parser/parse_coerce.h"
30 #include "parser/parse_collate.h"
31 #include "parser/parse_expr.h"
32 #include "parser/parse_func.h"
33 #include "parser/parse_oper.h"
34 #include "parser/parse_relation.h"
35 #include "parser/parse_target.h"
36 #include "parser/parse_type.h"
37 #include "utils/builtins.h"
38 #include "utils/date.h"
39 #include "utils/fmgroids.h"
40 #include "utils/jsonb.h"
41 #include "utils/lsyscache.h"
42 #include "utils/timestamp.h"
43 #include "utils/xml.h"
44 
45 /* GUC parameters */
46 bool Transform_null_equals = false;
47 
48 
49 static Node *transformExprRecurse(ParseState *pstate, Node *expr);
50 static Node *transformParamRef(ParseState *pstate, ParamRef *pref);
51 static Node *transformAExprOp(ParseState *pstate, A_Expr *a);
52 static Node *transformAExprOpAny(ParseState *pstate, A_Expr *a);
53 static Node *transformAExprOpAll(ParseState *pstate, A_Expr *a);
54 static Node *transformAExprDistinct(ParseState *pstate, A_Expr *a);
55 static Node *transformAExprNullIf(ParseState *pstate, A_Expr *a);
56 static Node *transformAExprIn(ParseState *pstate, A_Expr *a);
57 static Node *transformAExprBetween(ParseState *pstate, A_Expr *a);
59 static Node *transformBoolExpr(ParseState *pstate, BoolExpr *a);
60 static Node *transformFuncCall(ParseState *pstate, FuncCall *fn);
61 static Node *transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref);
62 static Node *transformCaseExpr(ParseState *pstate, CaseExpr *c);
63 static Node *transformSubLink(ParseState *pstate, SubLink *sublink);
65  Oid array_type, Oid element_type, int32 typmod);
66 static Node *transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault);
68 static Node *transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m);
70  SQLValueFunction *svf);
71 static Node *transformXmlExpr(ParseState *pstate, XmlExpr *x);
74 static Node *transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr);
75 static Node *transformColumnRef(ParseState *pstate, ColumnRef *cref);
76 static Node *transformWholeRowRef(ParseState *pstate,
77  ParseNamespaceItem *nsitem,
78  int sublevels_up, int location);
80 static Node *transformTypeCast(ParseState *pstate, TypeCast *tc);
83  JsonObjectConstructor *ctor);
85  JsonArrayConstructor *ctor);
89 static Node *transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg);
91 static Node *transformJsonParseExpr(ParseState *pstate, JsonParseExpr *expr);
94  JsonSerializeExpr *expr);
96 static void transformJsonPassingArgs(ParseState *pstate, const char *constructName,
98  List **passing_values, List **passing_names);
99 static void coerceJsonExprOutput(ParseState *pstate, JsonExpr *jsexpr);
100 static JsonBehavior *transformJsonBehavior(ParseState *pstate, JsonBehavior *behavior,
101  JsonBehaviorType default_behavior,
102  JsonReturning *returning);
103 static Node *GetJsonBehaviorConst(JsonBehaviorType btype, int location);
104 static Node *make_row_comparison_op(ParseState *pstate, List *opname,
105  List *largs, List *rargs, int location);
106 static Node *make_row_distinct_op(ParseState *pstate, List *opname,
107  RowExpr *lrow, RowExpr *rrow, int location);
108 static Expr *make_distinct_op(ParseState *pstate, List *opname,
109  Node *ltree, Node *rtree, int location);
111  A_Expr *distincta, Node *arg);
112 
113 
114 /*
115  * transformExpr -
116  * Analyze and transform expressions. Type checking and type casting is
117  * done here. This processing converts the raw grammar output into
118  * expression trees with fully determined semantics.
119  */
120 Node *
121 transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
122 {
123  Node *result;
124  ParseExprKind sv_expr_kind;
125 
126  /* Save and restore identity of expression type we're parsing */
127  Assert(exprKind != EXPR_KIND_NONE);
128  sv_expr_kind = pstate->p_expr_kind;
129  pstate->p_expr_kind = exprKind;
130 
131  result = transformExprRecurse(pstate, expr);
132 
133  pstate->p_expr_kind = sv_expr_kind;
134 
135  return result;
136 }
137 
138 static Node *
140 {
141  Node *result;
142 
143  if (expr == NULL)
144  return NULL;
145 
146  /* Guard against stack overflow due to overly complex expressions */
148 
149  switch (nodeTag(expr))
150  {
151  case T_ColumnRef:
152  result = transformColumnRef(pstate, (ColumnRef *) expr);
153  break;
154 
155  case T_ParamRef:
156  result = transformParamRef(pstate, (ParamRef *) expr);
157  break;
158 
159  case T_A_Const:
160  result = (Node *) make_const(pstate, (A_Const *) expr);
161  break;
162 
163  case T_A_Indirection:
164  result = transformIndirection(pstate, (A_Indirection *) expr);
165  break;
166 
167  case T_A_ArrayExpr:
168  result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
169  InvalidOid, InvalidOid, -1);
170  break;
171 
172  case T_TypeCast:
173  result = transformTypeCast(pstate, (TypeCast *) expr);
174  break;
175 
176  case T_CollateClause:
177  result = transformCollateClause(pstate, (CollateClause *) expr);
178  break;
179 
180  case T_A_Expr:
181  {
182  A_Expr *a = (A_Expr *) expr;
183 
184  switch (a->kind)
185  {
186  case AEXPR_OP:
187  result = transformAExprOp(pstate, a);
188  break;
189  case AEXPR_OP_ANY:
190  result = transformAExprOpAny(pstate, a);
191  break;
192  case AEXPR_OP_ALL:
193  result = transformAExprOpAll(pstate, a);
194  break;
195  case AEXPR_DISTINCT:
196  case AEXPR_NOT_DISTINCT:
197  result = transformAExprDistinct(pstate, a);
198  break;
199  case AEXPR_NULLIF:
200  result = transformAExprNullIf(pstate, a);
201  break;
202  case AEXPR_IN:
203  result = transformAExprIn(pstate, a);
204  break;
205  case AEXPR_LIKE:
206  case AEXPR_ILIKE:
207  case AEXPR_SIMILAR:
208  /* we can transform these just like AEXPR_OP */
209  result = transformAExprOp(pstate, a);
210  break;
211  case AEXPR_BETWEEN:
212  case AEXPR_NOT_BETWEEN:
213  case AEXPR_BETWEEN_SYM:
215  result = transformAExprBetween(pstate, a);
216  break;
217  default:
218  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
219  result = NULL; /* keep compiler quiet */
220  break;
221  }
222  break;
223  }
224 
225  case T_BoolExpr:
226  result = transformBoolExpr(pstate, (BoolExpr *) expr);
227  break;
228 
229  case T_FuncCall:
230  result = transformFuncCall(pstate, (FuncCall *) expr);
231  break;
232 
233  case T_MultiAssignRef:
234  result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
235  break;
236 
237  case T_GroupingFunc:
238  result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
239  break;
240 
241  case T_MergeSupportFunc:
242  result = transformMergeSupportFunc(pstate,
243  (MergeSupportFunc *) expr);
244  break;
245 
246  case T_NamedArgExpr:
247  {
248  NamedArgExpr *na = (NamedArgExpr *) expr;
249 
250  na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
251  result = expr;
252  break;
253  }
254 
255  case T_SubLink:
256  result = transformSubLink(pstate, (SubLink *) expr);
257  break;
258 
259  case T_CaseExpr:
260  result = transformCaseExpr(pstate, (CaseExpr *) expr);
261  break;
262 
263  case T_RowExpr:
264  result = transformRowExpr(pstate, (RowExpr *) expr, false);
265  break;
266 
267  case T_CoalesceExpr:
268  result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
269  break;
270 
271  case T_MinMaxExpr:
272  result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
273  break;
274 
275  case T_SQLValueFunction:
276  result = transformSQLValueFunction(pstate,
277  (SQLValueFunction *) expr);
278  break;
279 
280  case T_XmlExpr:
281  result = transformXmlExpr(pstate, (XmlExpr *) expr);
282  break;
283 
284  case T_XmlSerialize:
285  result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
286  break;
287 
288  case T_NullTest:
289  {
290  NullTest *n = (NullTest *) expr;
291 
292  n->arg = (Expr *) transformExprRecurse(pstate, (Node *) n->arg);
293  /* the argument can be any type, so don't coerce it */
294  n->argisrow = type_is_rowtype(exprType((Node *) n->arg));
295  result = expr;
296  break;
297  }
298 
299  case T_BooleanTest:
300  result = transformBooleanTest(pstate, (BooleanTest *) expr);
301  break;
302 
303  case T_CurrentOfExpr:
304  result = transformCurrentOfExpr(pstate, (CurrentOfExpr *) expr);
305  break;
306 
307  /*
308  * In all places where DEFAULT is legal, the caller should have
309  * processed it rather than passing it to transformExpr().
310  */
311  case T_SetToDefault:
312  ereport(ERROR,
313  (errcode(ERRCODE_SYNTAX_ERROR),
314  errmsg("DEFAULT is not allowed in this context"),
315  parser_errposition(pstate,
316  ((SetToDefault *) expr)->location)));
317  break;
318 
319  /*
320  * CaseTestExpr doesn't require any processing; it is only
321  * injected into parse trees in a fully-formed state.
322  *
323  * Ordinarily we should not see a Var here, but it is convenient
324  * for transformJoinUsingClause() to create untransformed operator
325  * trees containing already-transformed Vars. The best
326  * alternative would be to deconstruct and reconstruct column
327  * references, which seems expensively pointless. So allow it.
328  */
329  case T_CaseTestExpr:
330  case T_Var:
331  {
332  result = (Node *) expr;
333  break;
334  }
335 
336  case T_JsonObjectConstructor:
337  result = transformJsonObjectConstructor(pstate, (JsonObjectConstructor *) expr);
338  break;
339 
340  case T_JsonArrayConstructor:
341  result = transformJsonArrayConstructor(pstate, (JsonArrayConstructor *) expr);
342  break;
343 
344  case T_JsonArrayQueryConstructor:
346  break;
347 
348  case T_JsonObjectAgg:
349  result = transformJsonObjectAgg(pstate, (JsonObjectAgg *) expr);
350  break;
351 
352  case T_JsonArrayAgg:
353  result = transformJsonArrayAgg(pstate, (JsonArrayAgg *) expr);
354  break;
355 
356  case T_JsonIsPredicate:
357  result = transformJsonIsPredicate(pstate, (JsonIsPredicate *) expr);
358  break;
359 
360  case T_JsonParseExpr:
361  result = transformJsonParseExpr(pstate, (JsonParseExpr *) expr);
362  break;
363 
364  case T_JsonScalarExpr:
365  result = transformJsonScalarExpr(pstate, (JsonScalarExpr *) expr);
366  break;
367 
368  case T_JsonSerializeExpr:
369  result = transformJsonSerializeExpr(pstate, (JsonSerializeExpr *) expr);
370  break;
371 
372  case T_JsonFuncExpr:
373  result = transformJsonFuncExpr(pstate, (JsonFuncExpr *) expr);
374  break;
375 
376  default:
377  /* should not reach here */
378  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
379  result = NULL; /* keep compiler quiet */
380  break;
381  }
382 
383  return result;
384 }
385 
386 /*
387  * helper routine for delivering "column does not exist" error message
388  *
389  * (Usually we don't have to work this hard, but the general case of field
390  * selection from an arbitrary node needs it.)
391  */
392 static void
393 unknown_attribute(ParseState *pstate, Node *relref, const char *attname,
394  int location)
395 {
396  RangeTblEntry *rte;
397 
398  if (IsA(relref, Var) &&
399  ((Var *) relref)->varattno == InvalidAttrNumber)
400  {
401  /* Reference the RTE by alias not by actual table name */
402  rte = GetRTEByRangeTablePosn(pstate,
403  ((Var *) relref)->varno,
404  ((Var *) relref)->varlevelsup);
405  ereport(ERROR,
406  (errcode(ERRCODE_UNDEFINED_COLUMN),
407  errmsg("column %s.%s does not exist",
408  rte->eref->aliasname, attname),
409  parser_errposition(pstate, location)));
410  }
411  else
412  {
413  /* Have to do it by reference to the type of the expression */
414  Oid relTypeId = exprType(relref);
415 
416  if (ISCOMPLEX(relTypeId))
417  ereport(ERROR,
418  (errcode(ERRCODE_UNDEFINED_COLUMN),
419  errmsg("column \"%s\" not found in data type %s",
420  attname, format_type_be(relTypeId)),
421  parser_errposition(pstate, location)));
422  else if (relTypeId == RECORDOID)
423  ereport(ERROR,
424  (errcode(ERRCODE_UNDEFINED_COLUMN),
425  errmsg("could not identify column \"%s\" in record data type",
426  attname),
427  parser_errposition(pstate, location)));
428  else
429  ereport(ERROR,
430  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
431  errmsg("column notation .%s applied to type %s, "
432  "which is not a composite type",
433  attname, format_type_be(relTypeId)),
434  parser_errposition(pstate, location)));
435  }
436 }
437 
438 static Node *
440 {
441  Node *last_srf = pstate->p_last_srf;
442  Node *result = transformExprRecurse(pstate, ind->arg);
443  List *subscripts = NIL;
444  int location = exprLocation(result);
445  ListCell *i;
446 
447  /*
448  * We have to split any field-selection operations apart from
449  * subscripting. Adjacent A_Indices nodes have to be treated as a single
450  * multidimensional subscript operation.
451  */
452  foreach(i, ind->indirection)
453  {
454  Node *n = lfirst(i);
455 
456  if (IsA(n, A_Indices))
457  subscripts = lappend(subscripts, n);
458  else if (IsA(n, A_Star))
459  {
460  ereport(ERROR,
461  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
462  errmsg("row expansion via \"*\" is not supported here"),
463  parser_errposition(pstate, location)));
464  }
465  else
466  {
467  Node *newresult;
468 
469  Assert(IsA(n, String));
470 
471  /* process subscripts before this field selection */
472  if (subscripts)
473  result = (Node *) transformContainerSubscripts(pstate,
474  result,
475  exprType(result),
476  exprTypmod(result),
477  subscripts,
478  false);
479  subscripts = NIL;
480 
481  newresult = ParseFuncOrColumn(pstate,
482  list_make1(n),
483  list_make1(result),
484  last_srf,
485  NULL,
486  false,
487  location);
488  if (newresult == NULL)
489  unknown_attribute(pstate, result, strVal(n), location);
490  result = newresult;
491  }
492  }
493  /* process trailing subscripts, if any */
494  if (subscripts)
495  result = (Node *) transformContainerSubscripts(pstate,
496  result,
497  exprType(result),
498  exprTypmod(result),
499  subscripts,
500  false);
501 
502  return result;
503 }
504 
505 /*
506  * Transform a ColumnRef.
507  *
508  * If you find yourself changing this code, see also ExpandColumnRefStar.
509  */
510 static Node *
512 {
513  Node *node = NULL;
514  char *nspname = NULL;
515  char *relname = NULL;
516  char *colname = NULL;
517  ParseNamespaceItem *nsitem;
518  int levels_up;
519  enum
520  {
521  CRERR_NO_COLUMN,
522  CRERR_NO_RTE,
523  CRERR_WRONG_DB,
524  CRERR_TOO_MANY
525  } crerr = CRERR_NO_COLUMN;
526  const char *err;
527 
528  /*
529  * Check to see if the column reference is in an invalid place within the
530  * query. We allow column references in most places, except in default
531  * expressions and partition bound expressions.
532  */
533  err = NULL;
534  switch (pstate->p_expr_kind)
535  {
536  case EXPR_KIND_NONE:
537  Assert(false); /* can't happen */
538  break;
539  case EXPR_KIND_OTHER:
540  case EXPR_KIND_JOIN_ON:
544  case EXPR_KIND_WHERE:
545  case EXPR_KIND_POLICY:
546  case EXPR_KIND_HAVING:
547  case EXPR_KIND_FILTER:
558  case EXPR_KIND_GROUP_BY:
559  case EXPR_KIND_ORDER_BY:
561  case EXPR_KIND_LIMIT:
562  case EXPR_KIND_OFFSET:
563  case EXPR_KIND_RETURNING:
565  case EXPR_KIND_VALUES:
581  /* okay */
582  break;
583 
585  err = _("cannot use column reference in DEFAULT expression");
586  break;
588  err = _("cannot use column reference in partition bound expression");
589  break;
590 
591  /*
592  * There is intentionally no default: case here, so that the
593  * compiler will warn if we add a new ParseExprKind without
594  * extending this switch. If we do see an unrecognized value at
595  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
596  * which is sane anyway.
597  */
598  }
599  if (err)
600  ereport(ERROR,
601  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
602  errmsg_internal("%s", err),
603  parser_errposition(pstate, cref->location)));
604 
605  /*
606  * Give the PreParseColumnRefHook, if any, first shot. If it returns
607  * non-null then that's all, folks.
608  */
609  if (pstate->p_pre_columnref_hook != NULL)
610  {
611  node = pstate->p_pre_columnref_hook(pstate, cref);
612  if (node != NULL)
613  return node;
614  }
615 
616  /*----------
617  * The allowed syntaxes are:
618  *
619  * A First try to resolve as unqualified column name;
620  * if no luck, try to resolve as unqualified table name (A.*).
621  * A.B A is an unqualified table name; B is either a
622  * column or function name (trying column name first).
623  * A.B.C schema A, table B, col or func name C.
624  * A.B.C.D catalog A, schema B, table C, col or func D.
625  * A.* A is an unqualified table name; means whole-row value.
626  * A.B.* whole-row value of table B in schema A.
627  * A.B.C.* whole-row value of table C in schema B in catalog A.
628  *
629  * We do not need to cope with bare "*"; that will only be accepted by
630  * the grammar at the top level of a SELECT list, and transformTargetList
631  * will take care of it before it ever gets here. Also, "A.*" etc will
632  * be expanded by transformTargetList if they appear at SELECT top level,
633  * so here we are only going to see them as function or operator inputs.
634  *
635  * Currently, if a catalog name is given then it must equal the current
636  * database name; we check it here and then discard it.
637  *----------
638  */
639  switch (list_length(cref->fields))
640  {
641  case 1:
642  {
643  Node *field1 = (Node *) linitial(cref->fields);
644 
645  colname = strVal(field1);
646 
647  /* Try to identify as an unqualified column */
648  node = colNameToVar(pstate, colname, false, cref->location);
649 
650  if (node == NULL)
651  {
652  /*
653  * Not known as a column of any range-table entry.
654  *
655  * Try to find the name as a relation. Note that only
656  * relations already entered into the rangetable will be
657  * recognized.
658  *
659  * This is a hack for backwards compatibility with
660  * PostQUEL-inspired syntax. The preferred form now is
661  * "rel.*".
662  */
663  nsitem = refnameNamespaceItem(pstate, NULL, colname,
664  cref->location,
665  &levels_up);
666  if (nsitem)
667  node = transformWholeRowRef(pstate, nsitem, levels_up,
668  cref->location);
669  }
670  break;
671  }
672  case 2:
673  {
674  Node *field1 = (Node *) linitial(cref->fields);
675  Node *field2 = (Node *) lsecond(cref->fields);
676 
677  relname = strVal(field1);
678 
679  /* Locate the referenced nsitem */
680  nsitem = refnameNamespaceItem(pstate, nspname, relname,
681  cref->location,
682  &levels_up);
683  if (nsitem == NULL)
684  {
685  crerr = CRERR_NO_RTE;
686  break;
687  }
688 
689  /* Whole-row reference? */
690  if (IsA(field2, A_Star))
691  {
692  node = transformWholeRowRef(pstate, nsitem, levels_up,
693  cref->location);
694  break;
695  }
696 
697  colname = strVal(field2);
698 
699  /* Try to identify as a column of the nsitem */
700  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
701  cref->location);
702  if (node == NULL)
703  {
704  /* Try it as a function call on the whole row */
705  node = transformWholeRowRef(pstate, nsitem, levels_up,
706  cref->location);
707  node = ParseFuncOrColumn(pstate,
708  list_make1(makeString(colname)),
709  list_make1(node),
710  pstate->p_last_srf,
711  NULL,
712  false,
713  cref->location);
714  }
715  break;
716  }
717  case 3:
718  {
719  Node *field1 = (Node *) linitial(cref->fields);
720  Node *field2 = (Node *) lsecond(cref->fields);
721  Node *field3 = (Node *) lthird(cref->fields);
722 
723  nspname = strVal(field1);
724  relname = strVal(field2);
725 
726  /* Locate the referenced nsitem */
727  nsitem = refnameNamespaceItem(pstate, nspname, relname,
728  cref->location,
729  &levels_up);
730  if (nsitem == NULL)
731  {
732  crerr = CRERR_NO_RTE;
733  break;
734  }
735 
736  /* Whole-row reference? */
737  if (IsA(field3, A_Star))
738  {
739  node = transformWholeRowRef(pstate, nsitem, levels_up,
740  cref->location);
741  break;
742  }
743 
744  colname = strVal(field3);
745 
746  /* Try to identify as a column of the nsitem */
747  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
748  cref->location);
749  if (node == NULL)
750  {
751  /* Try it as a function call on the whole row */
752  node = transformWholeRowRef(pstate, nsitem, levels_up,
753  cref->location);
754  node = ParseFuncOrColumn(pstate,
755  list_make1(makeString(colname)),
756  list_make1(node),
757  pstate->p_last_srf,
758  NULL,
759  false,
760  cref->location);
761  }
762  break;
763  }
764  case 4:
765  {
766  Node *field1 = (Node *) linitial(cref->fields);
767  Node *field2 = (Node *) lsecond(cref->fields);
768  Node *field3 = (Node *) lthird(cref->fields);
769  Node *field4 = (Node *) lfourth(cref->fields);
770  char *catname;
771 
772  catname = strVal(field1);
773  nspname = strVal(field2);
774  relname = strVal(field3);
775 
776  /*
777  * We check the catalog name and then ignore it.
778  */
779  if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
780  {
781  crerr = CRERR_WRONG_DB;
782  break;
783  }
784 
785  /* Locate the referenced nsitem */
786  nsitem = refnameNamespaceItem(pstate, nspname, relname,
787  cref->location,
788  &levels_up);
789  if (nsitem == NULL)
790  {
791  crerr = CRERR_NO_RTE;
792  break;
793  }
794 
795  /* Whole-row reference? */
796  if (IsA(field4, A_Star))
797  {
798  node = transformWholeRowRef(pstate, nsitem, levels_up,
799  cref->location);
800  break;
801  }
802 
803  colname = strVal(field4);
804 
805  /* Try to identify as a column of the nsitem */
806  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
807  cref->location);
808  if (node == NULL)
809  {
810  /* Try it as a function call on the whole row */
811  node = transformWholeRowRef(pstate, nsitem, levels_up,
812  cref->location);
813  node = ParseFuncOrColumn(pstate,
814  list_make1(makeString(colname)),
815  list_make1(node),
816  pstate->p_last_srf,
817  NULL,
818  false,
819  cref->location);
820  }
821  break;
822  }
823  default:
824  crerr = CRERR_TOO_MANY; /* too many dotted names */
825  break;
826  }
827 
828  /*
829  * Now give the PostParseColumnRefHook, if any, a chance. We pass the
830  * translation-so-far so that it can throw an error if it wishes in the
831  * case that it has a conflicting interpretation of the ColumnRef. (If it
832  * just translates anyway, we'll throw an error, because we can't undo
833  * whatever effects the preceding steps may have had on the pstate.) If it
834  * returns NULL, use the standard translation, or throw a suitable error
835  * if there is none.
836  */
837  if (pstate->p_post_columnref_hook != NULL)
838  {
839  Node *hookresult;
840 
841  hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
842  if (node == NULL)
843  node = hookresult;
844  else if (hookresult != NULL)
845  ereport(ERROR,
846  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
847  errmsg("column reference \"%s\" is ambiguous",
848  NameListToString(cref->fields)),
849  parser_errposition(pstate, cref->location)));
850  }
851 
852  /*
853  * Throw error if no translation found.
854  */
855  if (node == NULL)
856  {
857  switch (crerr)
858  {
859  case CRERR_NO_COLUMN:
860  errorMissingColumn(pstate, relname, colname, cref->location);
861  break;
862  case CRERR_NO_RTE:
863  errorMissingRTE(pstate, makeRangeVar(nspname, relname,
864  cref->location));
865  break;
866  case CRERR_WRONG_DB:
867  ereport(ERROR,
868  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
869  errmsg("cross-database references are not implemented: %s",
870  NameListToString(cref->fields)),
871  parser_errposition(pstate, cref->location)));
872  break;
873  case CRERR_TOO_MANY:
874  ereport(ERROR,
875  (errcode(ERRCODE_SYNTAX_ERROR),
876  errmsg("improper qualified name (too many dotted names): %s",
877  NameListToString(cref->fields)),
878  parser_errposition(pstate, cref->location)));
879  break;
880  }
881  }
882 
883  return node;
884 }
885 
886 static Node *
888 {
889  Node *result;
890 
891  /*
892  * The core parser knows nothing about Params. If a hook is supplied,
893  * call it. If not, or if the hook returns NULL, throw a generic error.
894  */
895  if (pstate->p_paramref_hook != NULL)
896  result = pstate->p_paramref_hook(pstate, pref);
897  else
898  result = NULL;
899 
900  if (result == NULL)
901  ereport(ERROR,
902  (errcode(ERRCODE_UNDEFINED_PARAMETER),
903  errmsg("there is no parameter $%d", pref->number),
904  parser_errposition(pstate, pref->location)));
905 
906  return result;
907 }
908 
909 /* Test whether an a_expr is a plain NULL constant or not */
910 static bool
912 {
913  if (arg && IsA(arg, A_Const))
914  {
915  A_Const *con = (A_Const *) arg;
916 
917  if (con->isnull)
918  return true;
919  }
920  return false;
921 }
922 
923 static Node *
925 {
926  Node *lexpr = a->lexpr;
927  Node *rexpr = a->rexpr;
928  Node *result;
929 
930  /*
931  * Special-case "foo = NULL" and "NULL = foo" for compatibility with
932  * standards-broken products (like Microsoft's). Turn these into IS NULL
933  * exprs. (If either side is a CaseTestExpr, then the expression was
934  * generated internally from a CASE-WHEN expression, and
935  * transform_null_equals does not apply.)
936  */
937  if (Transform_null_equals &&
938  list_length(a->name) == 1 &&
939  strcmp(strVal(linitial(a->name)), "=") == 0 &&
940  (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
941  (!IsA(lexpr, CaseTestExpr) && !IsA(rexpr, CaseTestExpr)))
942  {
943  NullTest *n = makeNode(NullTest);
944 
945  n->nulltesttype = IS_NULL;
946  n->location = a->location;
947 
948  if (exprIsNullConstant(lexpr))
949  n->arg = (Expr *) rexpr;
950  else
951  n->arg = (Expr *) lexpr;
952 
953  result = transformExprRecurse(pstate, (Node *) n);
954  }
955  else if (lexpr && IsA(lexpr, RowExpr) &&
956  rexpr && IsA(rexpr, SubLink) &&
957  ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
958  {
959  /*
960  * Convert "row op subselect" into a ROWCOMPARE sublink. Formerly the
961  * grammar did this, but now that a row construct is allowed anywhere
962  * in expressions, it's easier to do it here.
963  */
964  SubLink *s = (SubLink *) rexpr;
965 
967  s->testexpr = lexpr;
968  s->operName = a->name;
969  s->location = a->location;
970  result = transformExprRecurse(pstate, (Node *) s);
971  }
972  else if (lexpr && IsA(lexpr, RowExpr) &&
973  rexpr && IsA(rexpr, RowExpr))
974  {
975  /* ROW() op ROW() is handled specially */
976  lexpr = transformExprRecurse(pstate, lexpr);
977  rexpr = transformExprRecurse(pstate, rexpr);
978 
979  result = make_row_comparison_op(pstate,
980  a->name,
981  castNode(RowExpr, lexpr)->args,
982  castNode(RowExpr, rexpr)->args,
983  a->location);
984  }
985  else
986  {
987  /* Ordinary scalar operator */
988  Node *last_srf = pstate->p_last_srf;
989 
990  lexpr = transformExprRecurse(pstate, lexpr);
991  rexpr = transformExprRecurse(pstate, rexpr);
992 
993  result = (Node *) make_op(pstate,
994  a->name,
995  lexpr,
996  rexpr,
997  last_srf,
998  a->location);
999  }
1000 
1001  return result;
1002 }
1003 
1004 static Node *
1006 {
1007  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1008  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1009 
1010  return (Node *) make_scalar_array_op(pstate,
1011  a->name,
1012  true,
1013  lexpr,
1014  rexpr,
1015  a->location);
1016 }
1017 
1018 static Node *
1020 {
1021  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1022  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1023 
1024  return (Node *) make_scalar_array_op(pstate,
1025  a->name,
1026  false,
1027  lexpr,
1028  rexpr,
1029  a->location);
1030 }
1031 
1032 static Node *
1034 {
1035  Node *lexpr = a->lexpr;
1036  Node *rexpr = a->rexpr;
1037  Node *result;
1038 
1039  /*
1040  * If either input is an undecorated NULL literal, transform to a NullTest
1041  * on the other input. That's simpler to process than a full DistinctExpr,
1042  * and it avoids needing to require that the datatype have an = operator.
1043  */
1044  if (exprIsNullConstant(rexpr))
1045  return make_nulltest_from_distinct(pstate, a, lexpr);
1046  if (exprIsNullConstant(lexpr))
1047  return make_nulltest_from_distinct(pstate, a, rexpr);
1048 
1049  lexpr = transformExprRecurse(pstate, lexpr);
1050  rexpr = transformExprRecurse(pstate, rexpr);
1051 
1052  if (lexpr && IsA(lexpr, RowExpr) &&
1053  rexpr && IsA(rexpr, RowExpr))
1054  {
1055  /* ROW() op ROW() is handled specially */
1056  result = make_row_distinct_op(pstate, a->name,
1057  (RowExpr *) lexpr,
1058  (RowExpr *) rexpr,
1059  a->location);
1060  }
1061  else
1062  {
1063  /* Ordinary scalar operator */
1064  result = (Node *) make_distinct_op(pstate,
1065  a->name,
1066  lexpr,
1067  rexpr,
1068  a->location);
1069  }
1070 
1071  /*
1072  * If it's NOT DISTINCT, we first build a DistinctExpr and then stick a
1073  * NOT on top.
1074  */
1075  if (a->kind == AEXPR_NOT_DISTINCT)
1076  result = (Node *) makeBoolExpr(NOT_EXPR,
1077  list_make1(result),
1078  a->location);
1079 
1080  return result;
1081 }
1082 
1083 static Node *
1085 {
1086  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1087  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1088  OpExpr *result;
1089 
1090  result = (OpExpr *) make_op(pstate,
1091  a->name,
1092  lexpr,
1093  rexpr,
1094  pstate->p_last_srf,
1095  a->location);
1096 
1097  /*
1098  * The comparison operator itself should yield boolean ...
1099  */
1100  if (result->opresulttype != BOOLOID)
1101  ereport(ERROR,
1102  (errcode(ERRCODE_DATATYPE_MISMATCH),
1103  errmsg("NULLIF requires = operator to yield boolean"),
1104  parser_errposition(pstate, a->location)));
1105  if (result->opretset)
1106  ereport(ERROR,
1107  (errcode(ERRCODE_DATATYPE_MISMATCH),
1108  /* translator: %s is name of a SQL construct, eg NULLIF */
1109  errmsg("%s must not return a set", "NULLIF"),
1110  parser_errposition(pstate, a->location)));
1111 
1112  /*
1113  * ... but the NullIfExpr will yield the first operand's type.
1114  */
1115  result->opresulttype = exprType((Node *) linitial(result->args));
1116 
1117  /*
1118  * We rely on NullIfExpr and OpExpr being the same struct
1119  */
1120  NodeSetTag(result, T_NullIfExpr);
1121 
1122  return (Node *) result;
1123 }
1124 
1125 static Node *
1127 {
1128  Node *result = NULL;
1129  Node *lexpr;
1130  List *rexprs;
1131  List *rvars;
1132  List *rnonvars;
1133  bool useOr;
1134  ListCell *l;
1135 
1136  /*
1137  * If the operator is <>, combine with AND not OR.
1138  */
1139  if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1140  useOr = false;
1141  else
1142  useOr = true;
1143 
1144  /*
1145  * We try to generate a ScalarArrayOpExpr from IN/NOT IN, but this is only
1146  * possible if there is a suitable array type available. If not, we fall
1147  * back to a boolean condition tree with multiple copies of the lefthand
1148  * expression. Also, any IN-list items that contain Vars are handled as
1149  * separate boolean conditions, because that gives the planner more scope
1150  * for optimization on such clauses.
1151  *
1152  * First step: transform all the inputs, and detect whether any contain
1153  * Vars.
1154  */
1155  lexpr = transformExprRecurse(pstate, a->lexpr);
1156  rexprs = rvars = rnonvars = NIL;
1157  foreach(l, (List *) a->rexpr)
1158  {
1159  Node *rexpr = transformExprRecurse(pstate, lfirst(l));
1160 
1161  rexprs = lappend(rexprs, rexpr);
1162  if (contain_vars_of_level(rexpr, 0))
1163  rvars = lappend(rvars, rexpr);
1164  else
1165  rnonvars = lappend(rnonvars, rexpr);
1166  }
1167 
1168  /*
1169  * ScalarArrayOpExpr is only going to be useful if there's more than one
1170  * non-Var righthand item.
1171  */
1172  if (list_length(rnonvars) > 1)
1173  {
1174  List *allexprs;
1175  Oid scalar_type;
1176  Oid array_type;
1177 
1178  /*
1179  * Try to select a common type for the array elements. Note that
1180  * since the LHS' type is first in the list, it will be preferred when
1181  * there is doubt (eg, when all the RHS items are unknown literals).
1182  *
1183  * Note: use list_concat here not lcons, to avoid damaging rnonvars.
1184  */
1185  allexprs = list_concat(list_make1(lexpr), rnonvars);
1186  scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1187 
1188  /* We have to verify that the selected type actually works */
1189  if (OidIsValid(scalar_type) &&
1190  !verify_common_type(scalar_type, allexprs))
1191  scalar_type = InvalidOid;
1192 
1193  /*
1194  * Do we have an array type to use? Aside from the case where there
1195  * isn't one, we don't risk using ScalarArrayOpExpr when the common
1196  * type is RECORD, because the RowExpr comparison logic below can cope
1197  * with some cases of non-identical row types.
1198  */
1199  if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
1200  array_type = get_array_type(scalar_type);
1201  else
1202  array_type = InvalidOid;
1203  if (array_type != InvalidOid)
1204  {
1205  /*
1206  * OK: coerce all the right-hand non-Var inputs to the common type
1207  * and build an ArrayExpr for them.
1208  */
1209  List *aexprs;
1210  ArrayExpr *newa;
1211 
1212  aexprs = NIL;
1213  foreach(l, rnonvars)
1214  {
1215  Node *rexpr = (Node *) lfirst(l);
1216 
1217  rexpr = coerce_to_common_type(pstate, rexpr,
1218  scalar_type,
1219  "IN");
1220  aexprs = lappend(aexprs, rexpr);
1221  }
1222  newa = makeNode(ArrayExpr);
1223  newa->array_typeid = array_type;
1224  /* array_collid will be set by parse_collate.c */
1225  newa->element_typeid = scalar_type;
1226  newa->elements = aexprs;
1227  newa->multidims = false;
1228  newa->location = -1;
1229 
1230  result = (Node *) make_scalar_array_op(pstate,
1231  a->name,
1232  useOr,
1233  lexpr,
1234  (Node *) newa,
1235  a->location);
1236 
1237  /* Consider only the Vars (if any) in the loop below */
1238  rexprs = rvars;
1239  }
1240  }
1241 
1242  /*
1243  * Must do it the hard way, ie, with a boolean expression tree.
1244  */
1245  foreach(l, rexprs)
1246  {
1247  Node *rexpr = (Node *) lfirst(l);
1248  Node *cmp;
1249 
1250  if (IsA(lexpr, RowExpr) &&
1251  IsA(rexpr, RowExpr))
1252  {
1253  /* ROW() op ROW() is handled specially */
1254  cmp = make_row_comparison_op(pstate,
1255  a->name,
1256  copyObject(((RowExpr *) lexpr)->args),
1257  ((RowExpr *) rexpr)->args,
1258  a->location);
1259  }
1260  else
1261  {
1262  /* Ordinary scalar operator */
1263  cmp = (Node *) make_op(pstate,
1264  a->name,
1265  copyObject(lexpr),
1266  rexpr,
1267  pstate->p_last_srf,
1268  a->location);
1269  }
1270 
1271  cmp = coerce_to_boolean(pstate, cmp, "IN");
1272  if (result == NULL)
1273  result = cmp;
1274  else
1275  result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
1276  list_make2(result, cmp),
1277  a->location);
1278  }
1279 
1280  return result;
1281 }
1282 
1283 static Node *
1285 {
1286  Node *aexpr;
1287  Node *bexpr;
1288  Node *cexpr;
1289  Node *result;
1290  Node *sub1;
1291  Node *sub2;
1292  List *args;
1293 
1294  /* Deconstruct A_Expr into three subexprs */
1295  aexpr = a->lexpr;
1296  args = castNode(List, a->rexpr);
1297  Assert(list_length(args) == 2);
1298  bexpr = (Node *) linitial(args);
1299  cexpr = (Node *) lsecond(args);
1300 
1301  /*
1302  * Build the equivalent comparison expression. Make copies of
1303  * multiply-referenced subexpressions for safety. (XXX this is really
1304  * wrong since it results in multiple runtime evaluations of what may be
1305  * volatile expressions ...)
1306  *
1307  * Ideally we would not use hard-wired operators here but instead use
1308  * opclasses. However, mixed data types and other issues make this
1309  * difficult:
1310  * http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
1311  */
1312  switch (a->kind)
1313  {
1314  case AEXPR_BETWEEN:
1316  aexpr, bexpr,
1317  a->location),
1318  makeSimpleA_Expr(AEXPR_OP, "<=",
1319  copyObject(aexpr), cexpr,
1320  a->location));
1321  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1322  break;
1323  case AEXPR_NOT_BETWEEN:
1325  aexpr, bexpr,
1326  a->location),
1328  copyObject(aexpr), cexpr,
1329  a->location));
1330  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1331  break;
1332  case AEXPR_BETWEEN_SYM:
1334  aexpr, bexpr,
1335  a->location),
1336  makeSimpleA_Expr(AEXPR_OP, "<=",
1337  copyObject(aexpr), cexpr,
1338  a->location));
1339  sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1341  copyObject(aexpr), copyObject(cexpr),
1342  a->location),
1343  makeSimpleA_Expr(AEXPR_OP, "<=",
1344  copyObject(aexpr), copyObject(bexpr),
1345  a->location));
1346  sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1347  args = list_make2(sub1, sub2);
1348  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1349  break;
1350  case AEXPR_NOT_BETWEEN_SYM:
1352  aexpr, bexpr,
1353  a->location),
1355  copyObject(aexpr), cexpr,
1356  a->location));
1357  sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1359  copyObject(aexpr), copyObject(cexpr),
1360  a->location),
1362  copyObject(aexpr), copyObject(bexpr),
1363  a->location));
1364  sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1365  args = list_make2(sub1, sub2);
1366  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1367  break;
1368  default:
1369  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
1370  result = NULL; /* keep compiler quiet */
1371  break;
1372  }
1373 
1374  return transformExprRecurse(pstate, result);
1375 }
1376 
1377 static Node *
1379 {
1380  /*
1381  * All we need to do is check that we're in the RETURNING list of a MERGE
1382  * command. If so, we just return the node as-is.
1383  */
1384  if (pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
1385  {
1386  ParseState *parent_pstate = pstate->parentParseState;
1387 
1388  while (parent_pstate &&
1389  parent_pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
1390  parent_pstate = parent_pstate->parentParseState;
1391 
1392  if (!parent_pstate)
1393  ereport(ERROR,
1394  errcode(ERRCODE_SYNTAX_ERROR),
1395  errmsg("MERGE_ACTION() can only be used in the RETURNING list of a MERGE command"),
1396  parser_errposition(pstate, f->location));
1397  }
1398 
1399  return (Node *) f;
1400 }
1401 
1402 static Node *
1404 {
1405  List *args = NIL;
1406  const char *opname;
1407  ListCell *lc;
1408 
1409  switch (a->boolop)
1410  {
1411  case AND_EXPR:
1412  opname = "AND";
1413  break;
1414  case OR_EXPR:
1415  opname = "OR";
1416  break;
1417  case NOT_EXPR:
1418  opname = "NOT";
1419  break;
1420  default:
1421  elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
1422  opname = NULL; /* keep compiler quiet */
1423  break;
1424  }
1425 
1426  foreach(lc, a->args)
1427  {
1428  Node *arg = (Node *) lfirst(lc);
1429 
1430  arg = transformExprRecurse(pstate, arg);
1431  arg = coerce_to_boolean(pstate, arg, opname);
1432  args = lappend(args, arg);
1433  }
1434 
1435  return (Node *) makeBoolExpr(a->boolop, args, a->location);
1436 }
1437 
1438 static Node *
1440 {
1441  Node *last_srf = pstate->p_last_srf;
1442  List *targs;
1443  ListCell *args;
1444 
1445  /* Transform the list of arguments ... */
1446  targs = NIL;
1447  foreach(args, fn->args)
1448  {
1449  targs = lappend(targs, transformExprRecurse(pstate,
1450  (Node *) lfirst(args)));
1451  }
1452 
1453  /*
1454  * When WITHIN GROUP is used, we treat its ORDER BY expressions as
1455  * additional arguments to the function, for purposes of function lookup
1456  * and argument type coercion. So, transform each such expression and add
1457  * them to the targs list. We don't explicitly mark where each argument
1458  * came from, but ParseFuncOrColumn can tell what's what by reference to
1459  * list_length(fn->agg_order).
1460  */
1461  if (fn->agg_within_group)
1462  {
1463  Assert(fn->agg_order != NIL);
1464  foreach(args, fn->agg_order)
1465  {
1466  SortBy *arg = (SortBy *) lfirst(args);
1467 
1468  targs = lappend(targs, transformExpr(pstate, arg->node,
1470  }
1471  }
1472 
1473  /* ... and hand off to ParseFuncOrColumn */
1474  return ParseFuncOrColumn(pstate,
1475  fn->funcname,
1476  targs,
1477  last_srf,
1478  fn,
1479  false,
1480  fn->location);
1481 }
1482 
1483 static Node *
1485 {
1486  SubLink *sublink;
1487  RowExpr *rexpr;
1488  Query *qtree;
1489  TargetEntry *tle;
1490 
1491  /* We should only see this in first-stage processing of UPDATE tlists */
1493 
1494  /* We only need to transform the source if this is the first column */
1495  if (maref->colno == 1)
1496  {
1497  /*
1498  * For now, we only allow EXPR SubLinks and RowExprs as the source of
1499  * an UPDATE multiassignment. This is sufficient to cover interesting
1500  * cases; at worst, someone would have to write (SELECT * FROM expr)
1501  * to expand a composite-returning expression of another form.
1502  */
1503  if (IsA(maref->source, SubLink) &&
1504  ((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
1505  {
1506  /* Relabel it as a MULTIEXPR_SUBLINK */
1507  sublink = (SubLink *) maref->source;
1508  sublink->subLinkType = MULTIEXPR_SUBLINK;
1509  /* And transform it */
1510  sublink = (SubLink *) transformExprRecurse(pstate,
1511  (Node *) sublink);
1512 
1513  qtree = castNode(Query, sublink->subselect);
1514 
1515  /* Check subquery returns required number of columns */
1516  if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
1517  ereport(ERROR,
1518  (errcode(ERRCODE_SYNTAX_ERROR),
1519  errmsg("number of columns does not match number of values"),
1520  parser_errposition(pstate, sublink->location)));
1521 
1522  /*
1523  * Build a resjunk tlist item containing the MULTIEXPR SubLink,
1524  * and add it to pstate->p_multiassign_exprs, whence it will later
1525  * get appended to the completed targetlist. We needn't worry
1526  * about selecting a resno for it; transformUpdateStmt will do
1527  * that.
1528  */
1529  tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
1531  tle);
1532 
1533  /*
1534  * Assign a unique-within-this-targetlist ID to the MULTIEXPR
1535  * SubLink. We can just use its position in the
1536  * p_multiassign_exprs list.
1537  */
1538  sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
1539  }
1540  else if (IsA(maref->source, RowExpr))
1541  {
1542  /* Transform the RowExpr, allowing SetToDefault items */
1543  rexpr = (RowExpr *) transformRowExpr(pstate,
1544  (RowExpr *) maref->source,
1545  true);
1546 
1547  /* Check it returns required number of columns */
1548  if (list_length(rexpr->args) != maref->ncolumns)
1549  ereport(ERROR,
1550  (errcode(ERRCODE_SYNTAX_ERROR),
1551  errmsg("number of columns does not match number of values"),
1552  parser_errposition(pstate, rexpr->location)));
1553 
1554  /*
1555  * Temporarily append it to p_multiassign_exprs, so we can get it
1556  * back when we come back here for additional columns.
1557  */
1558  tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
1560  tle);
1561  }
1562  else
1563  ereport(ERROR,
1564  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1565  errmsg("source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression"),
1566  parser_errposition(pstate, exprLocation(maref->source))));
1567  }
1568  else
1569  {
1570  /*
1571  * Second or later column in a multiassignment. Re-fetch the
1572  * transformed SubLink or RowExpr, which we assume is still the last
1573  * entry in p_multiassign_exprs.
1574  */
1575  Assert(pstate->p_multiassign_exprs != NIL);
1576  tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
1577  }
1578 
1579  /*
1580  * Emit the appropriate output expression for the current column
1581  */
1582  if (IsA(tle->expr, SubLink))
1583  {
1584  Param *param;
1585 
1586  sublink = (SubLink *) tle->expr;
1587  Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
1588  qtree = castNode(Query, sublink->subselect);
1589 
1590  /* Build a Param representing the current subquery output column */
1591  tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
1592  Assert(!tle->resjunk);
1593 
1594  param = makeNode(Param);
1595  param->paramkind = PARAM_MULTIEXPR;
1596  param->paramid = (sublink->subLinkId << 16) | maref->colno;
1597  param->paramtype = exprType((Node *) tle->expr);
1598  param->paramtypmod = exprTypmod((Node *) tle->expr);
1599  param->paramcollid = exprCollation((Node *) tle->expr);
1600  param->location = exprLocation((Node *) tle->expr);
1601 
1602  return (Node *) param;
1603  }
1604 
1605  if (IsA(tle->expr, RowExpr))
1606  {
1607  Node *result;
1608 
1609  rexpr = (RowExpr *) tle->expr;
1610 
1611  /* Just extract and return the next element of the RowExpr */
1612  result = (Node *) list_nth(rexpr->args, maref->colno - 1);
1613 
1614  /*
1615  * If we're at the last column, delete the RowExpr from
1616  * p_multiassign_exprs; we don't need it anymore, and don't want it in
1617  * the finished UPDATE tlist. We assume this is still the last entry
1618  * in p_multiassign_exprs.
1619  */
1620  if (maref->colno == maref->ncolumns)
1621  pstate->p_multiassign_exprs =
1623 
1624  return result;
1625  }
1626 
1627  elog(ERROR, "unexpected expr type in multiassign list");
1628  return NULL; /* keep compiler quiet */
1629 }
1630 
1631 static Node *
1633 {
1634  CaseExpr *newc = makeNode(CaseExpr);
1635  Node *last_srf = pstate->p_last_srf;
1636  Node *arg;
1637  CaseTestExpr *placeholder;
1638  List *newargs;
1639  List *resultexprs;
1640  ListCell *l;
1641  Node *defresult;
1642  Oid ptype;
1643 
1644  /* transform the test expression, if any */
1645  arg = transformExprRecurse(pstate, (Node *) c->arg);
1646 
1647  /* generate placeholder for test expression */
1648  if (arg)
1649  {
1650  /*
1651  * If test expression is an untyped literal, force it to text. We have
1652  * to do something now because we won't be able to do this coercion on
1653  * the placeholder. This is not as flexible as what was done in 7.4
1654  * and before, but it's good enough to handle the sort of silly coding
1655  * commonly seen.
1656  */
1657  if (exprType(arg) == UNKNOWNOID)
1658  arg = coerce_to_common_type(pstate, arg, TEXTOID, "CASE");
1659 
1660  /*
1661  * Run collation assignment on the test expression so that we know
1662  * what collation to mark the placeholder with. In principle we could
1663  * leave it to parse_collate.c to do that later, but propagating the
1664  * result to the CaseTestExpr would be unnecessarily complicated.
1665  */
1666  assign_expr_collations(pstate, arg);
1667 
1668  placeholder = makeNode(CaseTestExpr);
1669  placeholder->typeId = exprType(arg);
1670  placeholder->typeMod = exprTypmod(arg);
1671  placeholder->collation = exprCollation(arg);
1672  }
1673  else
1674  placeholder = NULL;
1675 
1676  newc->arg = (Expr *) arg;
1677 
1678  /* transform the list of arguments */
1679  newargs = NIL;
1680  resultexprs = NIL;
1681  foreach(l, c->args)
1682  {
1683  CaseWhen *w = lfirst_node(CaseWhen, l);
1684  CaseWhen *neww = makeNode(CaseWhen);
1685  Node *warg;
1686 
1687  warg = (Node *) w->expr;
1688  if (placeholder)
1689  {
1690  /* shorthand form was specified, so expand... */
1691  warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1692  (Node *) placeholder,
1693  warg,
1694  w->location);
1695  }
1696  neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1697 
1698  neww->expr = (Expr *) coerce_to_boolean(pstate,
1699  (Node *) neww->expr,
1700  "CASE/WHEN");
1701 
1702  warg = (Node *) w->result;
1703  neww->result = (Expr *) transformExprRecurse(pstate, warg);
1704  neww->location = w->location;
1705 
1706  newargs = lappend(newargs, neww);
1707  resultexprs = lappend(resultexprs, neww->result);
1708  }
1709 
1710  newc->args = newargs;
1711 
1712  /* transform the default clause */
1713  defresult = (Node *) c->defresult;
1714  if (defresult == NULL)
1715  {
1716  A_Const *n = makeNode(A_Const);
1717 
1718  n->isnull = true;
1719  n->location = -1;
1720  defresult = (Node *) n;
1721  }
1722  newc->defresult = (Expr *) transformExprRecurse(pstate, defresult);
1723 
1724  /*
1725  * Note: default result is considered the most significant type in
1726  * determining preferred type. This is how the code worked before, but it
1727  * seems a little bogus to me --- tgl
1728  */
1729  resultexprs = lcons(newc->defresult, resultexprs);
1730 
1731  ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
1732  Assert(OidIsValid(ptype));
1733  newc->casetype = ptype;
1734  /* casecollid will be set by parse_collate.c */
1735 
1736  /* Convert default result clause, if necessary */
1737  newc->defresult = (Expr *)
1738  coerce_to_common_type(pstate,
1739  (Node *) newc->defresult,
1740  ptype,
1741  "CASE/ELSE");
1742 
1743  /* Convert when-clause results, if necessary */
1744  foreach(l, newc->args)
1745  {
1746  CaseWhen *w = (CaseWhen *) lfirst(l);
1747 
1748  w->result = (Expr *)
1749  coerce_to_common_type(pstate,
1750  (Node *) w->result,
1751  ptype,
1752  "CASE/WHEN");
1753  }
1754 
1755  /* if any subexpression contained a SRF, complain */
1756  if (pstate->p_last_srf != last_srf)
1757  ereport(ERROR,
1758  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1759  /* translator: %s is name of a SQL construct, eg GROUP BY */
1760  errmsg("set-returning functions are not allowed in %s",
1761  "CASE"),
1762  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
1763  parser_errposition(pstate,
1764  exprLocation(pstate->p_last_srf))));
1765 
1766  newc->location = c->location;
1767 
1768  return (Node *) newc;
1769 }
1770 
1771 static Node *
1773 {
1774  Node *result = (Node *) sublink;
1775  Query *qtree;
1776  const char *err;
1777 
1778  /*
1779  * Check to see if the sublink is in an invalid place within the query. We
1780  * allow sublinks everywhere in SELECT/INSERT/UPDATE/DELETE/MERGE, but
1781  * generally not in utility statements.
1782  */
1783  err = NULL;
1784  switch (pstate->p_expr_kind)
1785  {
1786  case EXPR_KIND_NONE:
1787  Assert(false); /* can't happen */
1788  break;
1789  case EXPR_KIND_OTHER:
1790  /* Accept sublink here; caller must throw error if wanted */
1791  break;
1792  case EXPR_KIND_JOIN_ON:
1793  case EXPR_KIND_JOIN_USING:
1796  case EXPR_KIND_WHERE:
1797  case EXPR_KIND_POLICY:
1798  case EXPR_KIND_HAVING:
1799  case EXPR_KIND_FILTER:
1809  case EXPR_KIND_MERGE_WHEN:
1810  case EXPR_KIND_GROUP_BY:
1811  case EXPR_KIND_ORDER_BY:
1812  case EXPR_KIND_DISTINCT_ON:
1813  case EXPR_KIND_LIMIT:
1814  case EXPR_KIND_OFFSET:
1815  case EXPR_KIND_RETURNING:
1817  case EXPR_KIND_VALUES:
1819  case EXPR_KIND_CYCLE_MARK:
1820  /* okay */
1821  break;
1824  err = _("cannot use subquery in check constraint");
1825  break;
1828  err = _("cannot use subquery in DEFAULT expression");
1829  break;
1831  err = _("cannot use subquery in index expression");
1832  break;
1834  err = _("cannot use subquery in index predicate");
1835  break;
1837  err = _("cannot use subquery in statistics expression");
1838  break;
1840  err = _("cannot use subquery in transform expression");
1841  break;
1843  err = _("cannot use subquery in EXECUTE parameter");
1844  break;
1846  err = _("cannot use subquery in trigger WHEN condition");
1847  break;
1849  err = _("cannot use subquery in partition bound");
1850  break;
1852  err = _("cannot use subquery in partition key expression");
1853  break;
1855  err = _("cannot use subquery in CALL argument");
1856  break;
1857  case EXPR_KIND_COPY_WHERE:
1858  err = _("cannot use subquery in COPY FROM WHERE condition");
1859  break;
1861  err = _("cannot use subquery in column generation expression");
1862  break;
1863 
1864  /*
1865  * There is intentionally no default: case here, so that the
1866  * compiler will warn if we add a new ParseExprKind without
1867  * extending this switch. If we do see an unrecognized value at
1868  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
1869  * which is sane anyway.
1870  */
1871  }
1872  if (err)
1873  ereport(ERROR,
1874  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1875  errmsg_internal("%s", err),
1876  parser_errposition(pstate, sublink->location)));
1877 
1878  pstate->p_hasSubLinks = true;
1879 
1880  /*
1881  * OK, let's transform the sub-SELECT.
1882  */
1883  qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
1884 
1885  /*
1886  * Check that we got a SELECT. Anything else should be impossible given
1887  * restrictions of the grammar, but check anyway.
1888  */
1889  if (!IsA(qtree, Query) ||
1890  qtree->commandType != CMD_SELECT)
1891  elog(ERROR, "unexpected non-SELECT command in SubLink");
1892 
1893  sublink->subselect = (Node *) qtree;
1894 
1895  if (sublink->subLinkType == EXISTS_SUBLINK)
1896  {
1897  /*
1898  * EXISTS needs no test expression or combining operator. These fields
1899  * should be null already, but make sure.
1900  */
1901  sublink->testexpr = NULL;
1902  sublink->operName = NIL;
1903  }
1904  else if (sublink->subLinkType == EXPR_SUBLINK ||
1905  sublink->subLinkType == ARRAY_SUBLINK)
1906  {
1907  /*
1908  * Make sure the subselect delivers a single column (ignoring resjunk
1909  * targets).
1910  */
1911  if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
1912  ereport(ERROR,
1913  (errcode(ERRCODE_SYNTAX_ERROR),
1914  errmsg("subquery must return only one column"),
1915  parser_errposition(pstate, sublink->location)));
1916 
1917  /*
1918  * EXPR and ARRAY need no test expression or combining operator. These
1919  * fields should be null already, but make sure.
1920  */
1921  sublink->testexpr = NULL;
1922  sublink->operName = NIL;
1923  }
1924  else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1925  {
1926  /* Same as EXPR case, except no restriction on number of columns */
1927  sublink->testexpr = NULL;
1928  sublink->operName = NIL;
1929  }
1930  else
1931  {
1932  /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
1933  Node *lefthand;
1934  List *left_list;
1935  List *right_list;
1936  ListCell *l;
1937 
1938  /*
1939  * If the source was "x IN (select)", convert to "x = ANY (select)".
1940  */
1941  if (sublink->operName == NIL)
1942  sublink->operName = list_make1(makeString("="));
1943 
1944  /*
1945  * Transform lefthand expression, and convert to a list
1946  */
1947  lefthand = transformExprRecurse(pstate, sublink->testexpr);
1948  if (lefthand && IsA(lefthand, RowExpr))
1949  left_list = ((RowExpr *) lefthand)->args;
1950  else
1951  left_list = list_make1(lefthand);
1952 
1953  /*
1954  * Build a list of PARAM_SUBLINK nodes representing the output columns
1955  * of the subquery.
1956  */
1957  right_list = NIL;
1958  foreach(l, qtree->targetList)
1959  {
1960  TargetEntry *tent = (TargetEntry *) lfirst(l);
1961  Param *param;
1962 
1963  if (tent->resjunk)
1964  continue;
1965 
1966  param = makeNode(Param);
1967  param->paramkind = PARAM_SUBLINK;
1968  param->paramid = tent->resno;
1969  param->paramtype = exprType((Node *) tent->expr);
1970  param->paramtypmod = exprTypmod((Node *) tent->expr);
1971  param->paramcollid = exprCollation((Node *) tent->expr);
1972  param->location = -1;
1973 
1974  right_list = lappend(right_list, param);
1975  }
1976 
1977  /*
1978  * We could rely on make_row_comparison_op to complain if the list
1979  * lengths differ, but we prefer to generate a more specific error
1980  * message.
1981  */
1982  if (list_length(left_list) < list_length(right_list))
1983  ereport(ERROR,
1984  (errcode(ERRCODE_SYNTAX_ERROR),
1985  errmsg("subquery has too many columns"),
1986  parser_errposition(pstate, sublink->location)));
1987  if (list_length(left_list) > list_length(right_list))
1988  ereport(ERROR,
1989  (errcode(ERRCODE_SYNTAX_ERROR),
1990  errmsg("subquery has too few columns"),
1991  parser_errposition(pstate, sublink->location)));
1992 
1993  /*
1994  * Identify the combining operator(s) and generate a suitable
1995  * row-comparison expression.
1996  */
1997  sublink->testexpr = make_row_comparison_op(pstate,
1998  sublink->operName,
1999  left_list,
2000  right_list,
2001  sublink->location);
2002  }
2003 
2004  return result;
2005 }
2006 
2007 /*
2008  * transformArrayExpr
2009  *
2010  * If the caller specifies the target type, the resulting array will
2011  * be of exactly that type. Otherwise we try to infer a common type
2012  * for the elements using select_common_type().
2013  */
2014 static Node *
2016  Oid array_type, Oid element_type, int32 typmod)
2017 {
2018  ArrayExpr *newa = makeNode(ArrayExpr);
2019  List *newelems = NIL;
2020  List *newcoercedelems = NIL;
2021  ListCell *element;
2022  Oid coerce_type;
2023  bool coerce_hard;
2024 
2025  /*
2026  * Transform the element expressions
2027  *
2028  * Assume that the array is one-dimensional unless we find an array-type
2029  * element expression.
2030  */
2031  newa->multidims = false;
2032  foreach(element, a->elements)
2033  {
2034  Node *e = (Node *) lfirst(element);
2035  Node *newe;
2036 
2037  /*
2038  * If an element is itself an A_ArrayExpr, recurse directly so that we
2039  * can pass down any target type we were given.
2040  */
2041  if (IsA(e, A_ArrayExpr))
2042  {
2043  newe = transformArrayExpr(pstate,
2044  (A_ArrayExpr *) e,
2045  array_type,
2046  element_type,
2047  typmod);
2048  /* we certainly have an array here */
2049  Assert(array_type == InvalidOid || array_type == exprType(newe));
2050  newa->multidims = true;
2051  }
2052  else
2053  {
2054  newe = transformExprRecurse(pstate, e);
2055 
2056  /*
2057  * Check for sub-array expressions, if we haven't already found
2058  * one.
2059  */
2060  if (!newa->multidims && type_is_array(exprType(newe)))
2061  newa->multidims = true;
2062  }
2063 
2064  newelems = lappend(newelems, newe);
2065  }
2066 
2067  /*
2068  * Select a target type for the elements.
2069  *
2070  * If we haven't been given a target array type, we must try to deduce a
2071  * common type based on the types of the individual elements present.
2072  */
2073  if (OidIsValid(array_type))
2074  {
2075  /* Caller must ensure array_type matches element_type */
2076  Assert(OidIsValid(element_type));
2077  coerce_type = (newa->multidims ? array_type : element_type);
2078  coerce_hard = true;
2079  }
2080  else
2081  {
2082  /* Can't handle an empty array without a target type */
2083  if (newelems == NIL)
2084  ereport(ERROR,
2085  (errcode(ERRCODE_INDETERMINATE_DATATYPE),
2086  errmsg("cannot determine type of empty array"),
2087  errhint("Explicitly cast to the desired type, "
2088  "for example ARRAY[]::integer[]."),
2089  parser_errposition(pstate, a->location)));
2090 
2091  /* Select a common type for the elements */
2092  coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
2093 
2094  if (newa->multidims)
2095  {
2096  array_type = coerce_type;
2097  element_type = get_element_type(array_type);
2098  if (!OidIsValid(element_type))
2099  ereport(ERROR,
2100  (errcode(ERRCODE_UNDEFINED_OBJECT),
2101  errmsg("could not find element type for data type %s",
2102  format_type_be(array_type)),
2103  parser_errposition(pstate, a->location)));
2104  }
2105  else
2106  {
2107  element_type = coerce_type;
2108  array_type = get_array_type(element_type);
2109  if (!OidIsValid(array_type))
2110  ereport(ERROR,
2111  (errcode(ERRCODE_UNDEFINED_OBJECT),
2112  errmsg("could not find array type for data type %s",
2113  format_type_be(element_type)),
2114  parser_errposition(pstate, a->location)));
2115  }
2116  coerce_hard = false;
2117  }
2118 
2119  /*
2120  * Coerce elements to target type
2121  *
2122  * If the array has been explicitly cast, then the elements are in turn
2123  * explicitly coerced.
2124  *
2125  * If the array's type was merely derived from the common type of its
2126  * elements, then the elements are implicitly coerced to the common type.
2127  * This is consistent with other uses of select_common_type().
2128  */
2129  foreach(element, newelems)
2130  {
2131  Node *e = (Node *) lfirst(element);
2132  Node *newe;
2133 
2134  if (coerce_hard)
2135  {
2136  newe = coerce_to_target_type(pstate, e,
2137  exprType(e),
2138  coerce_type,
2139  typmod,
2142  -1);
2143  if (newe == NULL)
2144  ereport(ERROR,
2145  (errcode(ERRCODE_CANNOT_COERCE),
2146  errmsg("cannot cast type %s to %s",
2149  parser_errposition(pstate, exprLocation(e))));
2150  }
2151  else
2152  newe = coerce_to_common_type(pstate, e,
2153  coerce_type,
2154  "ARRAY");
2155  newcoercedelems = lappend(newcoercedelems, newe);
2156  }
2157 
2158  newa->array_typeid = array_type;
2159  /* array_collid will be set by parse_collate.c */
2160  newa->element_typeid = element_type;
2161  newa->elements = newcoercedelems;
2162  newa->location = a->location;
2163 
2164  return (Node *) newa;
2165 }
2166 
2167 static Node *
2168 transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
2169 {
2170  RowExpr *newr;
2171  char fname[16];
2172  int fnum;
2173 
2174  newr = makeNode(RowExpr);
2175 
2176  /* Transform the field expressions */
2177  newr->args = transformExpressionList(pstate, r->args,
2178  pstate->p_expr_kind, allowDefault);
2179 
2180  /* Disallow more columns than will fit in a tuple */
2182  ereport(ERROR,
2183  (errcode(ERRCODE_TOO_MANY_COLUMNS),
2184  errmsg("ROW expressions can have at most %d entries",
2186  parser_errposition(pstate, r->location)));
2187 
2188  /* Barring later casting, we consider the type RECORD */
2189  newr->row_typeid = RECORDOID;
2190  newr->row_format = COERCE_IMPLICIT_CAST;
2191 
2192  /* ROW() has anonymous columns, so invent some field names */
2193  newr->colnames = NIL;
2194  for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2195  {
2196  snprintf(fname, sizeof(fname), "f%d", fnum);
2197  newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2198  }
2199 
2200  newr->location = r->location;
2201 
2202  return (Node *) newr;
2203 }
2204 
2205 static Node *
2207 {
2209  Node *last_srf = pstate->p_last_srf;
2210  List *newargs = NIL;
2211  List *newcoercedargs = NIL;
2212  ListCell *args;
2213 
2214  foreach(args, c->args)
2215  {
2216  Node *e = (Node *) lfirst(args);
2217  Node *newe;
2218 
2219  newe = transformExprRecurse(pstate, e);
2220  newargs = lappend(newargs, newe);
2221  }
2222 
2223  newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2224  /* coalescecollid will be set by parse_collate.c */
2225 
2226  /* Convert arguments if necessary */
2227  foreach(args, newargs)
2228  {
2229  Node *e = (Node *) lfirst(args);
2230  Node *newe;
2231 
2232  newe = coerce_to_common_type(pstate, e,
2233  newc->coalescetype,
2234  "COALESCE");
2235  newcoercedargs = lappend(newcoercedargs, newe);
2236  }
2237 
2238  /* if any subexpression contained a SRF, complain */
2239  if (pstate->p_last_srf != last_srf)
2240  ereport(ERROR,
2241  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2242  /* translator: %s is name of a SQL construct, eg GROUP BY */
2243  errmsg("set-returning functions are not allowed in %s",
2244  "COALESCE"),
2245  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2246  parser_errposition(pstate,
2247  exprLocation(pstate->p_last_srf))));
2248 
2249  newc->args = newcoercedargs;
2250  newc->location = c->location;
2251  return (Node *) newc;
2252 }
2253 
2254 static Node *
2256 {
2257  MinMaxExpr *newm = makeNode(MinMaxExpr);
2258  List *newargs = NIL;
2259  List *newcoercedargs = NIL;
2260  const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2261  ListCell *args;
2262 
2263  newm->op = m->op;
2264  foreach(args, m->args)
2265  {
2266  Node *e = (Node *) lfirst(args);
2267  Node *newe;
2268 
2269  newe = transformExprRecurse(pstate, e);
2270  newargs = lappend(newargs, newe);
2271  }
2272 
2273  newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2274  /* minmaxcollid and inputcollid will be set by parse_collate.c */
2275 
2276  /* Convert arguments if necessary */
2277  foreach(args, newargs)
2278  {
2279  Node *e = (Node *) lfirst(args);
2280  Node *newe;
2281 
2282  newe = coerce_to_common_type(pstate, e,
2283  newm->minmaxtype,
2284  funcname);
2285  newcoercedargs = lappend(newcoercedargs, newe);
2286  }
2287 
2288  newm->args = newcoercedargs;
2289  newm->location = m->location;
2290  return (Node *) newm;
2291 }
2292 
2293 static Node *
2295 {
2296  /*
2297  * All we need to do is insert the correct result type and (where needed)
2298  * validate the typmod, so we just modify the node in-place.
2299  */
2300  switch (svf->op)
2301  {
2302  case SVFOP_CURRENT_DATE:
2303  svf->type = DATEOID;
2304  break;
2305  case SVFOP_CURRENT_TIME:
2306  svf->type = TIMETZOID;
2307  break;
2308  case SVFOP_CURRENT_TIME_N:
2309  svf->type = TIMETZOID;
2310  svf->typmod = anytime_typmod_check(true, svf->typmod);
2311  break;
2313  svf->type = TIMESTAMPTZOID;
2314  break;
2316  svf->type = TIMESTAMPTZOID;
2317  svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2318  break;
2319  case SVFOP_LOCALTIME:
2320  svf->type = TIMEOID;
2321  break;
2322  case SVFOP_LOCALTIME_N:
2323  svf->type = TIMEOID;
2324  svf->typmod = anytime_typmod_check(false, svf->typmod);
2325  break;
2326  case SVFOP_LOCALTIMESTAMP:
2327  svf->type = TIMESTAMPOID;
2328  break;
2330  svf->type = TIMESTAMPOID;
2331  svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2332  break;
2333  case SVFOP_CURRENT_ROLE:
2334  case SVFOP_CURRENT_USER:
2335  case SVFOP_USER:
2336  case SVFOP_SESSION_USER:
2337  case SVFOP_CURRENT_CATALOG:
2338  case SVFOP_CURRENT_SCHEMA:
2339  svf->type = NAMEOID;
2340  break;
2341  }
2342 
2343  return (Node *) svf;
2344 }
2345 
2346 static Node *
2348 {
2349  XmlExpr *newx;
2350  ListCell *lc;
2351  int i;
2352 
2353  newx = makeNode(XmlExpr);
2354  newx->op = x->op;
2355  if (x->name)
2356  newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
2357  else
2358  newx->name = NULL;
2359  newx->xmloption = x->xmloption;
2360  newx->type = XMLOID; /* this just marks the node as transformed */
2361  newx->typmod = -1;
2362  newx->location = x->location;
2363 
2364  /*
2365  * gram.y built the named args as a list of ResTarget. Transform each,
2366  * and break the names out as a separate list.
2367  */
2368  newx->named_args = NIL;
2369  newx->arg_names = NIL;
2370 
2371  foreach(lc, x->named_args)
2372  {
2373  ResTarget *r = lfirst_node(ResTarget, lc);
2374  Node *expr;
2375  char *argname;
2376 
2377  expr = transformExprRecurse(pstate, r->val);
2378 
2379  if (r->name)
2380  argname = map_sql_identifier_to_xml_name(r->name, false, false);
2381  else if (IsA(r->val, ColumnRef))
2383  true, false);
2384  else
2385  {
2386  ereport(ERROR,
2387  (errcode(ERRCODE_SYNTAX_ERROR),
2388  x->op == IS_XMLELEMENT
2389  ? errmsg("unnamed XML attribute value must be a column reference")
2390  : errmsg("unnamed XML element value must be a column reference"),
2391  parser_errposition(pstate, r->location)));
2392  argname = NULL; /* keep compiler quiet */
2393  }
2394 
2395  /* reject duplicate argnames in XMLELEMENT only */
2396  if (x->op == IS_XMLELEMENT)
2397  {
2398  ListCell *lc2;
2399 
2400  foreach(lc2, newx->arg_names)
2401  {
2402  if (strcmp(argname, strVal(lfirst(lc2))) == 0)
2403  ereport(ERROR,
2404  (errcode(ERRCODE_SYNTAX_ERROR),
2405  errmsg("XML attribute name \"%s\" appears more than once",
2406  argname),
2407  parser_errposition(pstate, r->location)));
2408  }
2409  }
2410 
2411  newx->named_args = lappend(newx->named_args, expr);
2412  newx->arg_names = lappend(newx->arg_names, makeString(argname));
2413  }
2414 
2415  /* The other arguments are of varying types depending on the function */
2416  newx->args = NIL;
2417  i = 0;
2418  foreach(lc, x->args)
2419  {
2420  Node *e = (Node *) lfirst(lc);
2421  Node *newe;
2422 
2423  newe = transformExprRecurse(pstate, e);
2424  switch (x->op)
2425  {
2426  case IS_XMLCONCAT:
2427  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2428  "XMLCONCAT");
2429  break;
2430  case IS_XMLELEMENT:
2431  /* no coercion necessary */
2432  break;
2433  case IS_XMLFOREST:
2434  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2435  "XMLFOREST");
2436  break;
2437  case IS_XMLPARSE:
2438  if (i == 0)
2439  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2440  "XMLPARSE");
2441  else
2442  newe = coerce_to_boolean(pstate, newe, "XMLPARSE");
2443  break;
2444  case IS_XMLPI:
2445  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2446  "XMLPI");
2447  break;
2448  case IS_XMLROOT:
2449  if (i == 0)
2450  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2451  "XMLROOT");
2452  else if (i == 1)
2453  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2454  "XMLROOT");
2455  else
2456  newe = coerce_to_specific_type(pstate, newe, INT4OID,
2457  "XMLROOT");
2458  break;
2459  case IS_XMLSERIALIZE:
2460  /* not handled here */
2461  Assert(false);
2462  break;
2463  case IS_DOCUMENT:
2464  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2465  "IS DOCUMENT");
2466  break;
2467  }
2468  newx->args = lappend(newx->args, newe);
2469  i++;
2470  }
2471 
2472  return (Node *) newx;
2473 }
2474 
2475 static Node *
2477 {
2478  Node *result;
2479  XmlExpr *xexpr;
2480  Oid targetType;
2481  int32 targetTypmod;
2482 
2483  xexpr = makeNode(XmlExpr);
2484  xexpr->op = IS_XMLSERIALIZE;
2485  xexpr->args = list_make1(coerce_to_specific_type(pstate,
2486  transformExprRecurse(pstate, xs->expr),
2487  XMLOID,
2488  "XMLSERIALIZE"));
2489 
2490  typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
2491 
2492  xexpr->xmloption = xs->xmloption;
2493  xexpr->indent = xs->indent;
2494  xexpr->location = xs->location;
2495  /* We actually only need these to be able to parse back the expression. */
2496  xexpr->type = targetType;
2497  xexpr->typmod = targetTypmod;
2498 
2499  /*
2500  * The actual target type is determined this way. SQL allows char and
2501  * varchar as target types. We allow anything that can be cast implicitly
2502  * from text. This way, user-defined text-like data types automatically
2503  * fit in.
2504  */
2505  result = coerce_to_target_type(pstate, (Node *) xexpr,
2506  TEXTOID, targetType, targetTypmod,
2509  -1);
2510  if (result == NULL)
2511  ereport(ERROR,
2512  (errcode(ERRCODE_CANNOT_COERCE),
2513  errmsg("cannot cast XMLSERIALIZE result to %s",
2514  format_type_be(targetType)),
2515  parser_errposition(pstate, xexpr->location)));
2516  return result;
2517 }
2518 
2519 static Node *
2521 {
2522  const char *clausename;
2523 
2524  switch (b->booltesttype)
2525  {
2526  case IS_TRUE:
2527  clausename = "IS TRUE";
2528  break;
2529  case IS_NOT_TRUE:
2530  clausename = "IS NOT TRUE";
2531  break;
2532  case IS_FALSE:
2533  clausename = "IS FALSE";
2534  break;
2535  case IS_NOT_FALSE:
2536  clausename = "IS NOT FALSE";
2537  break;
2538  case IS_UNKNOWN:
2539  clausename = "IS UNKNOWN";
2540  break;
2541  case IS_NOT_UNKNOWN:
2542  clausename = "IS NOT UNKNOWN";
2543  break;
2544  default:
2545  elog(ERROR, "unrecognized booltesttype: %d",
2546  (int) b->booltesttype);
2547  clausename = NULL; /* keep compiler quiet */
2548  }
2549 
2550  b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2551 
2552  b->arg = (Expr *) coerce_to_boolean(pstate,
2553  (Node *) b->arg,
2554  clausename);
2555 
2556  return (Node *) b;
2557 }
2558 
2559 static Node *
2561 {
2562  /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2563  Assert(pstate->p_target_nsitem != NULL);
2564  cexpr->cvarno = pstate->p_target_nsitem->p_rtindex;
2565 
2566  /*
2567  * Check to see if the cursor name matches a parameter of type REFCURSOR.
2568  * If so, replace the raw name reference with a parameter reference. (This
2569  * is a hack for the convenience of plpgsql.)
2570  */
2571  if (cexpr->cursor_name != NULL) /* in case already transformed */
2572  {
2573  ColumnRef *cref = makeNode(ColumnRef);
2574  Node *node = NULL;
2575 
2576  /* Build an unqualified ColumnRef with the given name */
2577  cref->fields = list_make1(makeString(cexpr->cursor_name));
2578  cref->location = -1;
2579 
2580  /* See if there is a translation available from a parser hook */
2581  if (pstate->p_pre_columnref_hook != NULL)
2582  node = pstate->p_pre_columnref_hook(pstate, cref);
2583  if (node == NULL && pstate->p_post_columnref_hook != NULL)
2584  node = pstate->p_post_columnref_hook(pstate, cref, NULL);
2585 
2586  /*
2587  * XXX Should we throw an error if we get a translation that isn't a
2588  * refcursor Param? For now it seems best to silently ignore false
2589  * matches.
2590  */
2591  if (node != NULL && IsA(node, Param))
2592  {
2593  Param *p = (Param *) node;
2594 
2595  if (p->paramkind == PARAM_EXTERN &&
2596  p->paramtype == REFCURSOROID)
2597  {
2598  /* Matches, so convert CURRENT OF to a param reference */
2599  cexpr->cursor_name = NULL;
2600  cexpr->cursor_param = p->paramid;
2601  }
2602  }
2603  }
2604 
2605  return (Node *) cexpr;
2606 }
2607 
2608 /*
2609  * Construct a whole-row reference to represent the notation "relation.*".
2610  */
2611 static Node *
2613  int sublevels_up, int location)
2614 {
2615  /*
2616  * Build the appropriate referencing node. Normally this can be a
2617  * whole-row Var, but if the nsitem is a JOIN USING alias then it contains
2618  * only a subset of the columns of the underlying join RTE, so that will
2619  * not work. Instead we immediately expand the reference into a RowExpr.
2620  * Since the JOIN USING's common columns are fully determined at this
2621  * point, there seems no harm in expanding it now rather than during
2622  * planning.
2623  *
2624  * Note that if the RTE is a function returning scalar, we create just a
2625  * plain reference to the function value, not a composite containing a
2626  * single column. This is pretty inconsistent at first sight, but it's
2627  * what we've done historically. One argument for it is that "rel" and
2628  * "rel.*" mean the same thing for composite relations, so why not for
2629  * scalar functions...
2630  */
2631  if (nsitem->p_names == nsitem->p_rte->eref)
2632  {
2633  Var *result;
2634 
2635  result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
2636  sublevels_up, true);
2637 
2638  /* location is not filled in by makeWholeRowVar */
2639  result->location = location;
2640 
2641  /* mark Var if it's nulled by any outer joins */
2642  markNullableIfNeeded(pstate, result);
2643 
2644  /* mark relation as requiring whole-row SELECT access */
2645  markVarForSelectPriv(pstate, result);
2646 
2647  return (Node *) result;
2648  }
2649  else
2650  {
2651  RowExpr *rowexpr;
2652  List *fields;
2653 
2654  /*
2655  * We want only as many columns as are listed in p_names->colnames,
2656  * and we should use those names not whatever possibly-aliased names
2657  * are in the RTE. We needn't worry about marking the RTE for SELECT
2658  * access, as the common columns are surely so marked already.
2659  */
2660  expandRTE(nsitem->p_rte, nsitem->p_rtindex,
2661  sublevels_up, location, false,
2662  NULL, &fields);
2663  rowexpr = makeNode(RowExpr);
2664  rowexpr->args = list_truncate(fields,
2665  list_length(nsitem->p_names->colnames));
2666  rowexpr->row_typeid = RECORDOID;
2667  rowexpr->row_format = COERCE_IMPLICIT_CAST;
2668  rowexpr->colnames = copyObject(nsitem->p_names->colnames);
2669  rowexpr->location = location;
2670 
2671  /* XXX we ought to mark the row as possibly nullable */
2672 
2673  return (Node *) rowexpr;
2674  }
2675 }
2676 
2677 /*
2678  * Handle an explicit CAST construct.
2679  *
2680  * Transform the argument, look up the type name, and apply any necessary
2681  * coercion function(s).
2682  */
2683 static Node *
2685 {
2686  Node *result;
2687  Node *arg = tc->arg;
2688  Node *expr;
2689  Oid inputType;
2690  Oid targetType;
2691  int32 targetTypmod;
2692  int location;
2693 
2694  /* Look up the type name first */
2695  typenameTypeIdAndMod(pstate, tc->typeName, &targetType, &targetTypmod);
2696 
2697  /*
2698  * If the subject of the typecast is an ARRAY[] construct and the target
2699  * type is an array type, we invoke transformArrayExpr() directly so that
2700  * we can pass down the type information. This avoids some cases where
2701  * transformArrayExpr() might not infer the correct type. Otherwise, just
2702  * transform the argument normally.
2703  */
2704  if (IsA(arg, A_ArrayExpr))
2705  {
2706  Oid targetBaseType;
2707  int32 targetBaseTypmod;
2708  Oid elementType;
2709 
2710  /*
2711  * If target is a domain over array, work with the base array type
2712  * here. Below, we'll cast the array type to the domain. In the
2713  * usual case that the target is not a domain, the remaining steps
2714  * will be a no-op.
2715  */
2716  targetBaseTypmod = targetTypmod;
2717  targetBaseType = getBaseTypeAndTypmod(targetType, &targetBaseTypmod);
2718  elementType = get_element_type(targetBaseType);
2719  if (OidIsValid(elementType))
2720  {
2721  expr = transformArrayExpr(pstate,
2722  (A_ArrayExpr *) arg,
2723  targetBaseType,
2724  elementType,
2725  targetBaseTypmod);
2726  }
2727  else
2728  expr = transformExprRecurse(pstate, arg);
2729  }
2730  else
2731  expr = transformExprRecurse(pstate, arg);
2732 
2733  inputType = exprType(expr);
2734  if (inputType == InvalidOid)
2735  return expr; /* do nothing if NULL input */
2736 
2737  /*
2738  * Location of the coercion is preferentially the location of the :: or
2739  * CAST symbol, but if there is none then use the location of the type
2740  * name (this can happen in TypeName 'string' syntax, for instance).
2741  */
2742  location = tc->location;
2743  if (location < 0)
2744  location = tc->typeName->location;
2745 
2746  result = coerce_to_target_type(pstate, expr, inputType,
2747  targetType, targetTypmod,
2750  location);
2751  if (result == NULL)
2752  ereport(ERROR,
2753  (errcode(ERRCODE_CANNOT_COERCE),
2754  errmsg("cannot cast type %s to %s",
2755  format_type_be(inputType),
2756  format_type_be(targetType)),
2757  parser_coercion_errposition(pstate, location, expr)));
2758 
2759  return result;
2760 }
2761 
2762 /*
2763  * Handle an explicit COLLATE clause.
2764  *
2765  * Transform the argument, and look up the collation name.
2766  */
2767 static Node *
2769 {
2770  CollateExpr *newc;
2771  Oid argtype;
2772 
2773  newc = makeNode(CollateExpr);
2774  newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2775 
2776  argtype = exprType((Node *) newc->arg);
2777 
2778  /*
2779  * The unknown type is not collatable, but coerce_type() takes care of it
2780  * separately, so we'll let it go here.
2781  */
2782  if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2783  ereport(ERROR,
2784  (errcode(ERRCODE_DATATYPE_MISMATCH),
2785  errmsg("collations are not supported by type %s",
2786  format_type_be(argtype)),
2787  parser_errposition(pstate, c->location)));
2788 
2789  newc->collOid = LookupCollation(pstate, c->collname, c->location);
2790  newc->location = c->location;
2791 
2792  return (Node *) newc;
2793 }
2794 
2795 /*
2796  * Transform a "row compare-op row" construct
2797  *
2798  * The inputs are lists of already-transformed expressions.
2799  * As with coerce_type, pstate may be NULL if no special unknown-Param
2800  * processing is wanted.
2801  *
2802  * The output may be a single OpExpr, an AND or OR combination of OpExprs,
2803  * or a RowCompareExpr. In all cases it is guaranteed to return boolean.
2804  * The AND, OR, and RowCompareExpr cases further imply things about the
2805  * behavior of the operators (ie, they behave as =, <>, or < <= > >=).
2806  */
2807 static Node *
2809  List *largs, List *rargs, int location)
2810 {
2811  RowCompareExpr *rcexpr;
2812  RowCompareType rctype;
2813  List *opexprs;
2814  List *opnos;
2815  List *opfamilies;
2816  ListCell *l,
2817  *r;
2818  List **opinfo_lists;
2819  Bitmapset *strats;
2820  int nopers;
2821  int i;
2822 
2823  nopers = list_length(largs);
2824  if (nopers != list_length(rargs))
2825  ereport(ERROR,
2826  (errcode(ERRCODE_SYNTAX_ERROR),
2827  errmsg("unequal number of entries in row expressions"),
2828  parser_errposition(pstate, location)));
2829 
2830  /*
2831  * We can't compare zero-length rows because there is no principled basis
2832  * for figuring out what the operator is.
2833  */
2834  if (nopers == 0)
2835  ereport(ERROR,
2836  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2837  errmsg("cannot compare rows of zero length"),
2838  parser_errposition(pstate, location)));
2839 
2840  /*
2841  * Identify all the pairwise operators, using make_op so that behavior is
2842  * the same as in the simple scalar case.
2843  */
2844  opexprs = NIL;
2845  forboth(l, largs, r, rargs)
2846  {
2847  Node *larg = (Node *) lfirst(l);
2848  Node *rarg = (Node *) lfirst(r);
2849  OpExpr *cmp;
2850 
2851  cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
2852  pstate->p_last_srf, location));
2853 
2854  /*
2855  * We don't use coerce_to_boolean here because we insist on the
2856  * operator yielding boolean directly, not via coercion. If it
2857  * doesn't yield bool it won't be in any index opfamilies...
2858  */
2859  if (cmp->opresulttype != BOOLOID)
2860  ereport(ERROR,
2861  (errcode(ERRCODE_DATATYPE_MISMATCH),
2862  errmsg("row comparison operator must yield type boolean, "
2863  "not type %s",
2864  format_type_be(cmp->opresulttype)),
2865  parser_errposition(pstate, location)));
2866  if (expression_returns_set((Node *) cmp))
2867  ereport(ERROR,
2868  (errcode(ERRCODE_DATATYPE_MISMATCH),
2869  errmsg("row comparison operator must not return a set"),
2870  parser_errposition(pstate, location)));
2871  opexprs = lappend(opexprs, cmp);
2872  }
2873 
2874  /*
2875  * If rows are length 1, just return the single operator. In this case we
2876  * don't insist on identifying btree semantics for the operator (but we
2877  * still require it to return boolean).
2878  */
2879  if (nopers == 1)
2880  return (Node *) linitial(opexprs);
2881 
2882  /*
2883  * Now we must determine which row comparison semantics (= <> < <= > >=)
2884  * apply to this set of operators. We look for btree opfamilies
2885  * containing the operators, and see which interpretations (strategy
2886  * numbers) exist for each operator.
2887  */
2888  opinfo_lists = (List **) palloc(nopers * sizeof(List *));
2889  strats = NULL;
2890  i = 0;
2891  foreach(l, opexprs)
2892  {
2893  Oid opno = ((OpExpr *) lfirst(l))->opno;
2894  Bitmapset *this_strats;
2895  ListCell *j;
2896 
2897  opinfo_lists[i] = get_op_btree_interpretation(opno);
2898 
2899  /*
2900  * convert strategy numbers into a Bitmapset to make the intersection
2901  * calculation easy.
2902  */
2903  this_strats = NULL;
2904  foreach(j, opinfo_lists[i])
2905  {
2906  OpBtreeInterpretation *opinfo = lfirst(j);
2907 
2908  this_strats = bms_add_member(this_strats, opinfo->strategy);
2909  }
2910  if (i == 0)
2911  strats = this_strats;
2912  else
2913  strats = bms_int_members(strats, this_strats);
2914  i++;
2915  }
2916 
2917  /*
2918  * If there are multiple common interpretations, we may use any one of
2919  * them ... this coding arbitrarily picks the lowest btree strategy
2920  * number.
2921  */
2922  i = bms_next_member(strats, -1);
2923  if (i < 0)
2924  {
2925  /* No common interpretation, so fail */
2926  ereport(ERROR,
2927  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2928  errmsg("could not determine interpretation of row comparison operator %s",
2929  strVal(llast(opname))),
2930  errhint("Row comparison operators must be associated with btree operator families."),
2931  parser_errposition(pstate, location)));
2932  }
2933  rctype = (RowCompareType) i;
2934 
2935  /*
2936  * For = and <> cases, we just combine the pairwise operators with AND or
2937  * OR respectively.
2938  */
2939  if (rctype == ROWCOMPARE_EQ)
2940  return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
2941  if (rctype == ROWCOMPARE_NE)
2942  return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
2943 
2944  /*
2945  * Otherwise we need to choose exactly which opfamily to associate with
2946  * each operator.
2947  */
2948  opfamilies = NIL;
2949  for (i = 0; i < nopers; i++)
2950  {
2951  Oid opfamily = InvalidOid;
2952  ListCell *j;
2953 
2954  foreach(j, opinfo_lists[i])
2955  {
2956  OpBtreeInterpretation *opinfo = lfirst(j);
2957 
2958  if (opinfo->strategy == rctype)
2959  {
2960  opfamily = opinfo->opfamily_id;
2961  break;
2962  }
2963  }
2964  if (OidIsValid(opfamily))
2965  opfamilies = lappend_oid(opfamilies, opfamily);
2966  else /* should not happen */
2967  ereport(ERROR,
2968  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2969  errmsg("could not determine interpretation of row comparison operator %s",
2970  strVal(llast(opname))),
2971  errdetail("There are multiple equally-plausible candidates."),
2972  parser_errposition(pstate, location)));
2973  }
2974 
2975  /*
2976  * Now deconstruct the OpExprs and create a RowCompareExpr.
2977  *
2978  * Note: can't just reuse the passed largs/rargs lists, because of
2979  * possibility that make_op inserted coercion operations.
2980  */
2981  opnos = NIL;
2982  largs = NIL;
2983  rargs = NIL;
2984  foreach(l, opexprs)
2985  {
2986  OpExpr *cmp = (OpExpr *) lfirst(l);
2987 
2988  opnos = lappend_oid(opnos, cmp->opno);
2989  largs = lappend(largs, linitial(cmp->args));
2990  rargs = lappend(rargs, lsecond(cmp->args));
2991  }
2992 
2993  rcexpr = makeNode(RowCompareExpr);
2994  rcexpr->rctype = rctype;
2995  rcexpr->opnos = opnos;
2996  rcexpr->opfamilies = opfamilies;
2997  rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
2998  rcexpr->largs = largs;
2999  rcexpr->rargs = rargs;
3000 
3001  return (Node *) rcexpr;
3002 }
3003 
3004 /*
3005  * Transform a "row IS DISTINCT FROM row" construct
3006  *
3007  * The input RowExprs are already transformed
3008  */
3009 static Node *
3011  RowExpr *lrow, RowExpr *rrow,
3012  int location)
3013 {
3014  Node *result = NULL;
3015  List *largs = lrow->args;
3016  List *rargs = rrow->args;
3017  ListCell *l,
3018  *r;
3019 
3020  if (list_length(largs) != list_length(rargs))
3021  ereport(ERROR,
3022  (errcode(ERRCODE_SYNTAX_ERROR),
3023  errmsg("unequal number of entries in row expressions"),
3024  parser_errposition(pstate, location)));
3025 
3026  forboth(l, largs, r, rargs)
3027  {
3028  Node *larg = (Node *) lfirst(l);
3029  Node *rarg = (Node *) lfirst(r);
3030  Node *cmp;
3031 
3032  cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
3033  if (result == NULL)
3034  result = cmp;
3035  else
3036  result = (Node *) makeBoolExpr(OR_EXPR,
3037  list_make2(result, cmp),
3038  location);
3039  }
3040 
3041  if (result == NULL)
3042  {
3043  /* zero-length rows? Generate constant FALSE */
3044  result = makeBoolConst(false, false);
3045  }
3046 
3047  return result;
3048 }
3049 
3050 /*
3051  * make the node for an IS DISTINCT FROM operator
3052  */
3053 static Expr *
3054 make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree,
3055  int location)
3056 {
3057  Expr *result;
3058 
3059  result = make_op(pstate, opname, ltree, rtree,
3060  pstate->p_last_srf, location);
3061  if (((OpExpr *) result)->opresulttype != BOOLOID)
3062  ereport(ERROR,
3063  (errcode(ERRCODE_DATATYPE_MISMATCH),
3064  errmsg("IS DISTINCT FROM requires = operator to yield boolean"),
3065  parser_errposition(pstate, location)));
3066  if (((OpExpr *) result)->opretset)
3067  ereport(ERROR,
3068  (errcode(ERRCODE_DATATYPE_MISMATCH),
3069  /* translator: %s is name of a SQL construct, eg NULLIF */
3070  errmsg("%s must not return a set", "IS DISTINCT FROM"),
3071  parser_errposition(pstate, location)));
3072 
3073  /*
3074  * We rely on DistinctExpr and OpExpr being same struct
3075  */
3076  NodeSetTag(result, T_DistinctExpr);
3077 
3078  return result;
3079 }
3080 
3081 /*
3082  * Produce a NullTest node from an IS [NOT] DISTINCT FROM NULL construct
3083  *
3084  * "arg" is the untransformed other argument
3085  */
3086 static Node *
3088 {
3089  NullTest *nt = makeNode(NullTest);
3090 
3091  nt->arg = (Expr *) transformExprRecurse(pstate, arg);
3092  /* the argument can be any type, so don't coerce it */
3093  if (distincta->kind == AEXPR_NOT_DISTINCT)
3094  nt->nulltesttype = IS_NULL;
3095  else
3096  nt->nulltesttype = IS_NOT_NULL;
3097  /* argisrow = false is correct whether or not arg is composite */
3098  nt->argisrow = false;
3099  nt->location = distincta->location;
3100  return (Node *) nt;
3101 }
3102 
3103 /*
3104  * Produce a string identifying an expression by kind.
3105  *
3106  * Note: when practical, use a simple SQL keyword for the result. If that
3107  * doesn't work well, check call sites to see whether custom error message
3108  * strings are required.
3109  */
3110 const char *
3112 {
3113  switch (exprKind)
3114  {
3115  case EXPR_KIND_NONE:
3116  return "invalid expression context";
3117  case EXPR_KIND_OTHER:
3118  return "extension expression";
3119  case EXPR_KIND_JOIN_ON:
3120  return "JOIN/ON";
3121  case EXPR_KIND_JOIN_USING:
3122  return "JOIN/USING";
3124  return "sub-SELECT in FROM";
3126  return "function in FROM";
3127  case EXPR_KIND_WHERE:
3128  return "WHERE";
3129  case EXPR_KIND_POLICY:
3130  return "POLICY";
3131  case EXPR_KIND_HAVING:
3132  return "HAVING";
3133  case EXPR_KIND_FILTER:
3134  return "FILTER";
3136  return "window PARTITION BY";
3138  return "window ORDER BY";
3140  return "window RANGE";
3142  return "window ROWS";
3144  return "window GROUPS";
3146  return "SELECT";
3148  return "INSERT";
3151  return "UPDATE";
3152  case EXPR_KIND_MERGE_WHEN:
3153  return "MERGE WHEN";
3154  case EXPR_KIND_GROUP_BY:
3155  return "GROUP BY";
3156  case EXPR_KIND_ORDER_BY:
3157  return "ORDER BY";
3158  case EXPR_KIND_DISTINCT_ON:
3159  return "DISTINCT ON";
3160  case EXPR_KIND_LIMIT:
3161  return "LIMIT";
3162  case EXPR_KIND_OFFSET:
3163  return "OFFSET";
3164  case EXPR_KIND_RETURNING:
3166  return "RETURNING";
3167  case EXPR_KIND_VALUES:
3169  return "VALUES";
3172  return "CHECK";
3175  return "DEFAULT";
3177  return "index expression";
3179  return "index predicate";
3181  return "statistics expression";
3183  return "USING";
3185  return "EXECUTE";
3187  return "WHEN";
3189  return "partition bound";
3191  return "PARTITION BY";
3193  return "CALL";
3194  case EXPR_KIND_COPY_WHERE:
3195  return "WHERE";
3197  return "GENERATED AS";
3198  case EXPR_KIND_CYCLE_MARK:
3199  return "CYCLE";
3200 
3201  /*
3202  * There is intentionally no default: case here, so that the
3203  * compiler will warn if we add a new ParseExprKind without
3204  * extending this switch. If we do see an unrecognized value at
3205  * runtime, we'll fall through to the "unrecognized" return.
3206  */
3207  }
3208  return "unrecognized expression kind";
3209 }
3210 
3211 /*
3212  * Make string Const node from JSON encoding name.
3213  *
3214  * UTF8 is default encoding.
3215  */
3216 static Const *
3218 {
3220  const char *enc;
3221  Name encname = palloc(sizeof(NameData));
3222 
3223  if (!format ||
3224  format->format_type == JS_FORMAT_DEFAULT ||
3225  format->encoding == JS_ENC_DEFAULT)
3227  else
3228  encoding = format->encoding;
3229 
3230  switch (encoding)
3231  {
3232  case JS_ENC_UTF16:
3233  enc = "UTF16";
3234  break;
3235  case JS_ENC_UTF32:
3236  enc = "UTF32";
3237  break;
3238  case JS_ENC_UTF8:
3239  enc = "UTF8";
3240  break;
3241  default:
3242  elog(ERROR, "invalid JSON encoding: %d", encoding);
3243  break;
3244  }
3245 
3246  namestrcpy(encname, enc);
3247 
3248  return makeConst(NAMEOID, -1, InvalidOid, NAMEDATALEN,
3249  NameGetDatum(encname), false, false);
3250 }
3251 
3252 /*
3253  * Make bytea => text conversion using specified JSON format encoding.
3254  */
3255 static Node *
3257 {
3259  FuncExpr *fexpr = makeFuncExpr(F_CONVERT_FROM, TEXTOID,
3260  list_make2(expr, encoding),
3263 
3264  fexpr->location = location;
3265 
3266  return (Node *) fexpr;
3267 }
3268 
3269 /*
3270  * Transform JSON value expression using specified input JSON format or
3271  * default format otherwise, coercing to the targettype if needed.
3272  *
3273  * Returned expression is either ve->raw_expr coerced to text (if needed) or
3274  * a JsonValueExpr with formatted_expr set to the coerced copy of raw_expr
3275  * if the specified format and the targettype requires it.
3276  */
3277 static Node *
3278 transformJsonValueExpr(ParseState *pstate, const char *constructName,
3279  JsonValueExpr *ve, JsonFormatType default_format,
3280  Oid targettype, bool isarg)
3281 {
3282  Node *expr = transformExprRecurse(pstate, (Node *) ve->raw_expr);
3283  Node *rawexpr;
3285  Oid exprtype;
3286  int location;
3287  char typcategory;
3288  bool typispreferred;
3289 
3290  if (exprType(expr) == UNKNOWNOID)
3291  expr = coerce_to_specific_type(pstate, expr, TEXTOID, constructName);
3292 
3293  rawexpr = expr;
3294  exprtype = exprType(expr);
3295  location = exprLocation(expr);
3296 
3297  get_type_category_preferred(exprtype, &typcategory, &typispreferred);
3298 
3299  if (ve->format->format_type != JS_FORMAT_DEFAULT)
3300  {
3301  if (ve->format->encoding != JS_ENC_DEFAULT && exprtype != BYTEAOID)
3302  ereport(ERROR,
3303  errcode(ERRCODE_DATATYPE_MISMATCH),
3304  errmsg("JSON ENCODING clause is only allowed for bytea input type"),
3305  parser_errposition(pstate, ve->format->location));
3306 
3307  if (exprtype == JSONOID || exprtype == JSONBOID)
3308  format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3309  else
3310  format = ve->format->format_type;
3311  }
3312  else if (isarg)
3313  {
3314  /*
3315  * Special treatment for PASSING arguments.
3316  *
3317  * Pass types supported by GetJsonPathVar() / JsonItemFromDatum()
3318  * directly without converting to json[b].
3319  */
3320  switch (exprtype)
3321  {
3322  case BOOLOID:
3323  case NUMERICOID:
3324  case INT2OID:
3325  case INT4OID:
3326  case INT8OID:
3327  case FLOAT4OID:
3328  case FLOAT8OID:
3329  case TEXTOID:
3330  case VARCHAROID:
3331  case DATEOID:
3332  case TIMEOID:
3333  case TIMETZOID:
3334  case TIMESTAMPOID:
3335  case TIMESTAMPTZOID:
3336  return expr;
3337 
3338  default:
3339  if (typcategory == TYPCATEGORY_STRING)
3340  return expr;
3341  /* else convert argument to json[b] type */
3342  break;
3343  }
3344 
3345  format = default_format;
3346  }
3347  else if (exprtype == JSONOID || exprtype == JSONBOID)
3348  format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3349  else
3350  format = default_format;
3351 
3352  if (format != JS_FORMAT_DEFAULT ||
3353  (OidIsValid(targettype) && exprtype != targettype))
3354  {
3355  Node *coerced;
3356  bool only_allow_cast = OidIsValid(targettype);
3357 
3358  /*
3359  * PASSING args are handled appropriately by GetJsonPathVar() /
3360  * JsonItemFromDatum().
3361  */
3362  if (!isarg &&
3363  !only_allow_cast &&
3364  exprtype != BYTEAOID && typcategory != TYPCATEGORY_STRING)
3365  ereport(ERROR,
3366  errcode(ERRCODE_DATATYPE_MISMATCH),
3368  errmsg("cannot use non-string types with implicit FORMAT JSON clause") :
3369  errmsg("cannot use non-string types with explicit FORMAT JSON clause"),
3370  parser_errposition(pstate, ve->format->location >= 0 ?
3371  ve->format->location : location));
3372 
3373  /* Convert encoded JSON text from bytea. */
3374  if (format == JS_FORMAT_JSON && exprtype == BYTEAOID)
3375  {
3376  expr = makeJsonByteaToTextConversion(expr, ve->format, location);
3377  exprtype = TEXTOID;
3378  }
3379 
3380  if (!OidIsValid(targettype))
3381  targettype = format == JS_FORMAT_JSONB ? JSONBOID : JSONOID;
3382 
3383  /* Try to coerce to the target type. */
3384  coerced = coerce_to_target_type(pstate, expr, exprtype,
3385  targettype, -1,
3388  location);
3389 
3390  if (!coerced)
3391  {
3392  /* If coercion failed, use to_json()/to_jsonb() functions. */
3393  FuncExpr *fexpr;
3394  Oid fnoid;
3395 
3396  /*
3397  * Though only allow a cast when the target type is specified by
3398  * the caller.
3399  */
3400  if (only_allow_cast)
3401  ereport(ERROR,
3402  (errcode(ERRCODE_CANNOT_COERCE),
3403  errmsg("cannot cast type %s to %s",
3404  format_type_be(exprtype),
3405  format_type_be(targettype)),
3406  parser_errposition(pstate, location)));
3407 
3408  fnoid = targettype == JSONOID ? F_TO_JSON : F_TO_JSONB;
3409  fexpr = makeFuncExpr(fnoid, targettype, list_make1(expr),
3411 
3412  fexpr->location = location;
3413 
3414  coerced = (Node *) fexpr;
3415  }
3416 
3417  if (coerced == expr)
3418  expr = rawexpr;
3419  else
3420  {
3421  ve = copyObject(ve);
3422  ve->raw_expr = (Expr *) rawexpr;
3423  ve->formatted_expr = (Expr *) coerced;
3424 
3425  expr = (Node *) ve;
3426  }
3427  }
3428 
3429  /* If returning a JsonValueExpr, formatted_expr must have been set. */
3430  Assert(!IsA(expr, JsonValueExpr) ||
3431  ((JsonValueExpr *) expr)->formatted_expr != NULL);
3432 
3433  return expr;
3434 }
3435 
3436 /*
3437  * Checks specified output format for its applicability to the target type.
3438  */
3439 static void
3441  Oid targettype, bool allow_format_for_non_strings)
3442 {
3443  if (!allow_format_for_non_strings &&
3444  format->format_type != JS_FORMAT_DEFAULT &&
3445  (targettype != BYTEAOID &&
3446  targettype != JSONOID &&
3447  targettype != JSONBOID))
3448  {
3449  char typcategory;
3450  bool typispreferred;
3451 
3452  get_type_category_preferred(targettype, &typcategory, &typispreferred);
3453 
3454  if (typcategory != TYPCATEGORY_STRING)
3455  ereport(ERROR,
3456  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3457  parser_errposition(pstate, format->location),
3458  errmsg("cannot use JSON format with non-string output types"));
3459  }
3460 
3461  if (format->format_type == JS_FORMAT_JSON)
3462  {
3463  JsonEncoding enc = format->encoding != JS_ENC_DEFAULT ?
3464  format->encoding : JS_ENC_UTF8;
3465 
3466  if (targettype != BYTEAOID &&
3467  format->encoding != JS_ENC_DEFAULT)
3468  ereport(ERROR,
3469  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3470  parser_errposition(pstate, format->location),
3471  errmsg("cannot set JSON encoding for non-bytea output types"));
3472 
3473  if (enc != JS_ENC_UTF8)
3474  ereport(ERROR,
3475  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3476  errmsg("unsupported JSON encoding"),
3477  errhint("Only UTF8 JSON encoding is supported."),
3478  parser_errposition(pstate, format->location));
3479  }
3480 }
3481 
3482 /*
3483  * Transform JSON output clause.
3484  *
3485  * Assigns target type oid and modifier.
3486  * Assigns default format or checks specified format for its applicability to
3487  * the target type.
3488  */
3489 static JsonReturning *
3491  bool allow_format)
3492 {
3493  JsonReturning *ret;
3494 
3495  /* if output clause is not specified, make default clause value */
3496  if (!output)
3497  {
3498  ret = makeNode(JsonReturning);
3499 
3501  ret->typid = InvalidOid;
3502  ret->typmod = -1;
3503 
3504  return ret;
3505  }
3506 
3507  ret = copyObject(output->returning);
3508 
3509  typenameTypeIdAndMod(pstate, output->typeName, &ret->typid, &ret->typmod);
3510 
3511  if (output->typeName->setof)
3512  ereport(ERROR,
3513  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3514  errmsg("returning SETOF types is not supported in SQL/JSON functions"));
3515 
3516  if (get_typtype(ret->typid) == TYPTYPE_PSEUDO)
3517  ereport(ERROR,
3518  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3519  errmsg("returning pseudo-types is not supported in SQL/JSON functions"));
3520 
3521  if (ret->format->format_type == JS_FORMAT_DEFAULT)
3522  /* assign JSONB format when returning jsonb, or JSON format otherwise */
3523  ret->format->format_type =
3524  ret->typid == JSONBOID ? JS_FORMAT_JSONB : JS_FORMAT_JSON;
3525  else
3526  checkJsonOutputFormat(pstate, ret->format, ret->typid, allow_format);
3527 
3528  return ret;
3529 }
3530 
3531 /*
3532  * Transform JSON output clause of JSON constructor functions.
3533  *
3534  * Derive RETURNING type, if not specified, from argument types.
3535  */
3536 static JsonReturning *
3538  List *args)
3539 {
3540  JsonReturning *returning = transformJsonOutput(pstate, output, true);
3541 
3542  if (!OidIsValid(returning->typid))
3543  {
3544  ListCell *lc;
3545  bool have_jsonb = false;
3546 
3547  foreach(lc, args)
3548  {
3549  Node *expr = lfirst(lc);
3550  Oid typid = exprType(expr);
3551 
3552  have_jsonb |= typid == JSONBOID;
3553 
3554  if (have_jsonb)
3555  break;
3556  }
3557 
3558  if (have_jsonb)
3559  {
3560  returning->typid = JSONBOID;
3561  returning->format->format_type = JS_FORMAT_JSONB;
3562  }
3563  else
3564  {
3565  /* XXX TEXT is default by the standard, but we return JSON */
3566  returning->typid = JSONOID;
3567  returning->format->format_type = JS_FORMAT_JSON;
3568  }
3569 
3570  returning->typmod = -1;
3571  }
3572 
3573  return returning;
3574 }
3575 
3576 /*
3577  * Coerce json[b]-valued function expression to the output type.
3578  */
3579 static Node *
3581  const JsonReturning *returning, bool report_error)
3582 {
3583  Node *res;
3584  int location;
3585  Oid exprtype = exprType(expr);
3586 
3587  /* if output type is not specified or equals to function type, return */
3588  if (!OidIsValid(returning->typid) || returning->typid == exprtype)
3589  return expr;
3590 
3591  location = exprLocation(expr);
3592 
3593  if (location < 0)
3594  location = returning->format->location;
3595 
3596  /* special case for RETURNING bytea FORMAT json */
3597  if (returning->format->format_type == JS_FORMAT_JSON &&
3598  returning->typid == BYTEAOID)
3599  {
3600  /* encode json text into bytea using pg_convert_to() */
3601  Node *texpr = coerce_to_specific_type(pstate, expr, TEXTOID,
3602  "JSON_FUNCTION");
3603  Const *enc = getJsonEncodingConst(returning->format);
3604  FuncExpr *fexpr = makeFuncExpr(F_CONVERT_TO, BYTEAOID,
3605  list_make2(texpr, enc),
3608 
3609  fexpr->location = location;
3610 
3611  return (Node *) fexpr;
3612  }
3613 
3614  /* try to coerce expression to the output type */
3615  res = coerce_to_target_type(pstate, expr, exprtype,
3616  returning->typid, returning->typmod,
3619  location);
3620 
3621  if (!res && report_error)
3622  ereport(ERROR,
3623  errcode(ERRCODE_CANNOT_COERCE),
3624  errmsg("cannot cast type %s to %s",
3625  format_type_be(exprtype),
3626  format_type_be(returning->typid)),
3627  parser_coercion_errposition(pstate, location, expr));
3628 
3629  return res;
3630 }
3631 
3632 /*
3633  * Make a JsonConstructorExpr node.
3634  */
3635 static Node *
3637  List *args, Expr *fexpr, JsonReturning *returning,
3638  bool unique, bool absent_on_null, int location)
3639 {
3641  Node *placeholder;
3642  Node *coercion;
3643 
3644  jsctor->args = args;
3645  jsctor->func = fexpr;
3646  jsctor->type = type;
3647  jsctor->returning = returning;
3648  jsctor->unique = unique;
3649  jsctor->absent_on_null = absent_on_null;
3650  jsctor->location = location;
3651 
3652  /*
3653  * Coerce to the RETURNING type and format, if needed. We abuse
3654  * CaseTestExpr here as placeholder to pass the result of either
3655  * evaluating 'fexpr' or whatever is produced by ExecEvalJsonConstructor()
3656  * that is of type JSON or JSONB to the coercion function.
3657  */
3658  if (fexpr)
3659  {
3661 
3662  cte->typeId = exprType((Node *) fexpr);
3663  cte->typeMod = exprTypmod((Node *) fexpr);
3664  cte->collation = exprCollation((Node *) fexpr);
3665 
3666  placeholder = (Node *) cte;
3667  }
3668  else
3669  {
3671 
3672  cte->typeId = returning->format->format_type == JS_FORMAT_JSONB ?
3673  JSONBOID : JSONOID;
3674  cte->typeMod = -1;
3675  cte->collation = InvalidOid;
3676 
3677  placeholder = (Node *) cte;
3678  }
3679 
3680  coercion = coerceJsonFuncExpr(pstate, placeholder, returning, true);
3681 
3682  if (coercion != placeholder)
3683  jsctor->coercion = (Expr *) coercion;
3684 
3685  return (Node *) jsctor;
3686 }
3687 
3688 /*
3689  * Transform JSON_OBJECT() constructor.
3690  *
3691  * JSON_OBJECT() is transformed into json[b]_build_object[_ext]() call
3692  * depending on the output JSON format. The first two arguments of
3693  * json[b]_build_object_ext() are absent_on_null and check_unique.
3694  *
3695  * Then function call result is coerced to the target type.
3696  */
3697 static Node *
3699 {
3700  JsonReturning *returning;
3701  List *args = NIL;
3702 
3703  /* transform key-value pairs, if any */
3704  if (ctor->exprs)
3705  {
3706  ListCell *lc;
3707 
3708  /* transform and append key-value arguments */
3709  foreach(lc, ctor->exprs)
3710  {
3712  Node *key = transformExprRecurse(pstate, (Node *) kv->key);
3713  Node *val = transformJsonValueExpr(pstate, "JSON_OBJECT()",
3714  kv->value,
3716  InvalidOid, false);
3717 
3718  args = lappend(args, key);
3719  args = lappend(args, val);
3720  }
3721  }
3722 
3723  returning = transformJsonConstructorOutput(pstate, ctor->output, args);
3724 
3725  return makeJsonConstructorExpr(pstate, JSCTOR_JSON_OBJECT, args, NULL,
3726  returning, ctor->unique,
3727  ctor->absent_on_null, ctor->location);
3728 }
3729 
3730 /*
3731  * Transform JSON_ARRAY(query [FORMAT] [RETURNING] [ON NULL]) into
3732  * (SELECT JSON_ARRAYAGG(a [FORMAT] [RETURNING] [ON NULL]) FROM (query) q(a))
3733  */
3734 static Node *
3737 {
3738  SubLink *sublink = makeNode(SubLink);
3741  Alias *alias = makeNode(Alias);
3742  ResTarget *target = makeNode(ResTarget);
3744  ColumnRef *colref = makeNode(ColumnRef);
3745  Query *query;
3746  ParseState *qpstate;
3747 
3748  /* Transform query only for counting target list entries. */
3749  qpstate = make_parsestate(pstate);
3750 
3751  query = transformStmt(qpstate, ctor->query);
3752 
3753  if (count_nonjunk_tlist_entries(query->targetList) != 1)
3754  ereport(ERROR,
3755  errcode(ERRCODE_SYNTAX_ERROR),
3756  errmsg("subquery must return only one column"),
3757  parser_errposition(pstate, ctor->location));
3758 
3759  free_parsestate(qpstate);
3760 
3761  colref->fields = list_make2(makeString(pstrdup("q")),
3762  makeString(pstrdup("a")));
3763  colref->location = ctor->location;
3764 
3765  /*
3766  * No formatting necessary, so set formatted_expr to be the same as
3767  * raw_expr.
3768  */
3769  agg->arg = makeJsonValueExpr((Expr *) colref, (Expr *) colref,
3770  ctor->format);
3771  agg->absent_on_null = ctor->absent_on_null;
3773  agg->constructor->agg_order = NIL;
3774  agg->constructor->output = ctor->output;
3775  agg->constructor->location = ctor->location;
3776 
3777  target->name = NULL;
3778  target->indirection = NIL;
3779  target->val = (Node *) agg;
3780  target->location = ctor->location;
3781 
3782  alias->aliasname = pstrdup("q");
3783  alias->colnames = list_make1(makeString(pstrdup("a")));
3784 
3785  range->lateral = false;
3786  range->subquery = ctor->query;
3787  range->alias = alias;
3788 
3789  select->targetList = list_make1(target);
3790  select->fromClause = list_make1(range);
3791 
3792  sublink->subLinkType = EXPR_SUBLINK;
3793  sublink->subLinkId = 0;
3794  sublink->testexpr = NULL;
3795  sublink->operName = NIL;
3796  sublink->subselect = (Node *) select;
3797  sublink->location = ctor->location;
3798 
3799  return transformExprRecurse(pstate, (Node *) sublink);
3800 }
3801 
3802 /*
3803  * Common code for JSON_OBJECTAGG and JSON_ARRAYAGG transformation.
3804  */
3805 static Node *
3807  JsonReturning *returning, List *args,
3808  Oid aggfnoid, Oid aggtype,
3809  JsonConstructorType ctor_type,
3810  bool unique, bool absent_on_null)
3811 {
3812  Node *node;
3813  Expr *aggfilter;
3814 
3815  aggfilter = agg_ctor->agg_filter ? (Expr *)
3816  transformWhereClause(pstate, agg_ctor->agg_filter,
3817  EXPR_KIND_FILTER, "FILTER") : NULL;
3818 
3819  if (agg_ctor->over)
3820  {
3821  /* window function */
3822  WindowFunc *wfunc = makeNode(WindowFunc);
3823 
3824  wfunc->winfnoid = aggfnoid;
3825  wfunc->wintype = aggtype;
3826  /* wincollid and inputcollid will be set by parse_collate.c */
3827  wfunc->args = args;
3828  wfunc->aggfilter = aggfilter;
3829  /* winref will be set by transformWindowFuncCall */
3830  wfunc->winstar = false;
3831  wfunc->winagg = true;
3832  wfunc->location = agg_ctor->location;
3833 
3834  /*
3835  * ordered aggs not allowed in windows yet
3836  */
3837  if (agg_ctor->agg_order != NIL)
3838  ereport(ERROR,
3839  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3840  errmsg("aggregate ORDER BY is not implemented for window functions"),
3841  parser_errposition(pstate, agg_ctor->location));
3842 
3843  /* parse_agg.c does additional window-func-specific processing */
3844  transformWindowFuncCall(pstate, wfunc, agg_ctor->over);
3845 
3846  node = (Node *) wfunc;
3847  }
3848  else
3849  {
3850  Aggref *aggref = makeNode(Aggref);
3851 
3852  aggref->aggfnoid = aggfnoid;
3853  aggref->aggtype = aggtype;
3854 
3855  /* aggcollid and inputcollid will be set by parse_collate.c */
3856  /* aggtranstype will be set by planner */
3857  /* aggargtypes will be set by transformAggregateCall */
3858  /* aggdirectargs and args will be set by transformAggregateCall */
3859  /* aggorder and aggdistinct will be set by transformAggregateCall */
3860  aggref->aggfilter = aggfilter;
3861  aggref->aggstar = false;
3862  aggref->aggvariadic = false;
3863  aggref->aggkind = AGGKIND_NORMAL;
3864  aggref->aggpresorted = false;
3865  /* agglevelsup will be set by transformAggregateCall */
3866  aggref->aggsplit = AGGSPLIT_SIMPLE; /* planner might change this */
3867  aggref->aggno = -1; /* planner will set aggno and aggtransno */
3868  aggref->aggtransno = -1;
3869  aggref->location = agg_ctor->location;
3870 
3871  transformAggregateCall(pstate, aggref, args, agg_ctor->agg_order, false);
3872 
3873  node = (Node *) aggref;
3874  }
3875 
3876  return makeJsonConstructorExpr(pstate, ctor_type, NIL, (Expr *) node,
3877  returning, unique, absent_on_null,
3878  agg_ctor->location);
3879 }
3880 
3881 /*
3882  * Transform JSON_OBJECTAGG() aggregate function.
3883  *
3884  * JSON_OBJECTAGG() is transformed into
3885  * json[b]_objectagg[_unique][_strict](key, value) call depending on
3886  * the output JSON format. Then the function call result is coerced to the
3887  * target output type.
3888  */
3889 static Node *
3891 {
3892  JsonReturning *returning;
3893  Node *key;
3894  Node *val;
3895  List *args;
3896  Oid aggfnoid;
3897  Oid aggtype;
3898 
3899  key = transformExprRecurse(pstate, (Node *) agg->arg->key);
3900  val = transformJsonValueExpr(pstate, "JSON_OBJECTAGG()",
3901  agg->arg->value,
3903  InvalidOid, false);
3904  args = list_make2(key, val);
3905 
3906  returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3907  args);
3908 
3909  if (returning->format->format_type == JS_FORMAT_JSONB)
3910  {
3911  if (agg->absent_on_null)
3912  if (agg->unique)
3913  aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE_STRICT;
3914  else
3915  aggfnoid = F_JSONB_OBJECT_AGG_STRICT;
3916  else if (agg->unique)
3917  aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE;
3918  else
3919  aggfnoid = F_JSONB_OBJECT_AGG;
3920 
3921  aggtype = JSONBOID;
3922  }
3923  else
3924  {
3925  if (agg->absent_on_null)
3926  if (agg->unique)
3927  aggfnoid = F_JSON_OBJECT_AGG_UNIQUE_STRICT;
3928  else
3929  aggfnoid = F_JSON_OBJECT_AGG_STRICT;
3930  else if (agg->unique)
3931  aggfnoid = F_JSON_OBJECT_AGG_UNIQUE;
3932  else
3933  aggfnoid = F_JSON_OBJECT_AGG;
3934 
3935  aggtype = JSONOID;
3936  }
3937 
3938  return transformJsonAggConstructor(pstate, agg->constructor, returning,
3939  args, aggfnoid, aggtype,
3941  agg->unique, agg->absent_on_null);
3942 }
3943 
3944 /*
3945  * Transform JSON_ARRAYAGG() aggregate function.
3946  *
3947  * JSON_ARRAYAGG() is transformed into json[b]_agg[_strict]() call depending
3948  * on the output JSON format and absent_on_null. Then the function call result
3949  * is coerced to the target output type.
3950  */
3951 static Node *
3953 {
3954  JsonReturning *returning;
3955  Node *arg;
3956  Oid aggfnoid;
3957  Oid aggtype;
3958 
3959  arg = transformJsonValueExpr(pstate, "JSON_ARRAYAGG()", agg->arg,
3960  JS_FORMAT_DEFAULT, InvalidOid, false);
3961 
3962  returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3963  list_make1(arg));
3964 
3965  if (returning->format->format_type == JS_FORMAT_JSONB)
3966  {
3967  aggfnoid = agg->absent_on_null ? F_JSONB_AGG_STRICT : F_JSONB_AGG;
3968  aggtype = JSONBOID;
3969  }
3970  else
3971  {
3972  aggfnoid = agg->absent_on_null ? F_JSON_AGG_STRICT : F_JSON_AGG;
3973  aggtype = JSONOID;
3974  }
3975 
3976  return transformJsonAggConstructor(pstate, agg->constructor, returning,
3977  list_make1(arg), aggfnoid, aggtype,
3979  false, agg->absent_on_null);
3980 }
3981 
3982 /*
3983  * Transform JSON_ARRAY() constructor.
3984  *
3985  * JSON_ARRAY() is transformed into json[b]_build_array[_ext]() call
3986  * depending on the output JSON format. The first argument of
3987  * json[b]_build_array_ext() is absent_on_null.
3988  *
3989  * Then function call result is coerced to the target type.
3990  */
3991 static Node *
3993 {
3994  JsonReturning *returning;
3995  List *args = NIL;
3996 
3997  /* transform element expressions, if any */
3998  if (ctor->exprs)
3999  {
4000  ListCell *lc;
4001 
4002  /* transform and append element arguments */
4003  foreach(lc, ctor->exprs)
4004  {
4005  JsonValueExpr *jsval = castNode(JsonValueExpr, lfirst(lc));
4006  Node *val = transformJsonValueExpr(pstate, "JSON_ARRAY()",
4007  jsval, JS_FORMAT_DEFAULT,
4008  InvalidOid, false);
4009 
4010  args = lappend(args, val);
4011  }
4012  }
4013 
4014  returning = transformJsonConstructorOutput(pstate, ctor->output, args);
4015 
4016  return makeJsonConstructorExpr(pstate, JSCTOR_JSON_ARRAY, args, NULL,
4017  returning, false, ctor->absent_on_null,
4018  ctor->location);
4019 }
4020 
4021 static Node *
4023  Oid *exprtype)
4024 {
4025  Node *raw_expr = transformExprRecurse(pstate, jsexpr);
4026  Node *expr = raw_expr;
4027 
4028  *exprtype = exprType(expr);
4029 
4030  /* prepare input document */
4031  if (*exprtype == BYTEAOID)
4032  {
4033  JsonValueExpr *jve;
4034 
4035  expr = raw_expr;
4036  expr = makeJsonByteaToTextConversion(expr, format, exprLocation(expr));
4037  *exprtype = TEXTOID;
4038 
4039  jve = makeJsonValueExpr((Expr *) raw_expr, (Expr *) expr, format);
4040  expr = (Node *) jve;
4041  }
4042  else
4043  {
4044  char typcategory;
4045  bool typispreferred;
4046 
4047  get_type_category_preferred(*exprtype, &typcategory, &typispreferred);
4048 
4049  if (*exprtype == UNKNOWNOID || typcategory == TYPCATEGORY_STRING)
4050  {
4051  expr = coerce_to_target_type(pstate, (Node *) expr, *exprtype,
4052  TEXTOID, -1,
4054  COERCE_IMPLICIT_CAST, -1);
4055  *exprtype = TEXTOID;
4056  }
4057 
4058  if (format->encoding != JS_ENC_DEFAULT)
4059  ereport(ERROR,
4060  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4061  parser_errposition(pstate, format->location),
4062  errmsg("cannot use JSON FORMAT ENCODING clause for non-bytea input types")));
4063  }
4064 
4065  return expr;
4066 }
4067 
4068 /*
4069  * Transform IS JSON predicate.
4070  */
4071 static Node *
4073 {
4074  Oid exprtype;
4075  Node *expr = transformJsonParseArg(pstate, pred->expr, pred->format,
4076  &exprtype);
4077 
4078  /* make resulting expression */
4079  if (exprtype != TEXTOID && exprtype != JSONOID && exprtype != JSONBOID)
4080  ereport(ERROR,
4081  (errcode(ERRCODE_DATATYPE_MISMATCH),
4082  errmsg("cannot use type %s in IS JSON predicate",
4083  format_type_be(exprtype))));
4084 
4085  /* This intentionally(?) drops the format clause. */
4086  return makeJsonIsPredicate(expr, NULL, pred->item_type,
4087  pred->unique_keys, pred->location);
4088 }
4089 
4090 /*
4091  * Transform the RETURNING clause of a JSON_*() expression if there is one and
4092  * create one if not.
4093  */
4094 static JsonReturning *
4096 {
4097  JsonReturning *returning;
4098 
4099  if (output)
4100  {
4101  returning = transformJsonOutput(pstate, output, false);
4102 
4103  Assert(OidIsValid(returning->typid));
4104 
4105  if (returning->typid != JSONOID && returning->typid != JSONBOID)
4106  ereport(ERROR,
4107  (errcode(ERRCODE_DATATYPE_MISMATCH),
4108  errmsg("cannot use RETURNING type %s in %s",
4109  format_type_be(returning->typid), fname),
4110  parser_errposition(pstate, output->typeName->location)));
4111  }
4112  else
4113  {
4114  /* Output type is JSON by default. */
4115  Oid targettype = JSONOID;
4117 
4118  returning = makeNode(JsonReturning);
4119  returning->format = makeJsonFormat(format, JS_ENC_DEFAULT, -1);
4120  returning->typid = targettype;
4121  returning->typmod = -1;
4122  }
4123 
4124  return returning;
4125 }
4126 
4127 /*
4128  * Transform a JSON() expression.
4129  *
4130  * JSON() is transformed into a JsonConstructorExpr of type JSCTOR_JSON_PARSE,
4131  * which validates the input expression value as JSON.
4132  */
4133 static Node *
4135 {
4136  JsonOutput *output = jsexpr->output;
4137  JsonReturning *returning;
4138  Node *arg;
4139 
4140  returning = transformJsonReturning(pstate, output, "JSON()");
4141 
4142  if (jsexpr->unique_keys)
4143  {
4144  /*
4145  * Coerce string argument to text and then to json[b] in the executor
4146  * node with key uniqueness check.
4147  */
4148  JsonValueExpr *jve = jsexpr->expr;
4149  Oid arg_type;
4150 
4151  arg = transformJsonParseArg(pstate, (Node *) jve->raw_expr, jve->format,
4152  &arg_type);
4153 
4154  if (arg_type != TEXTOID)
4155  ereport(ERROR,
4156  (errcode(ERRCODE_DATATYPE_MISMATCH),
4157  errmsg("cannot use non-string types with WITH UNIQUE KEYS clause"),
4158  parser_errposition(pstate, jsexpr->location)));
4159  }
4160  else
4161  {
4162  /*
4163  * Coerce argument to target type using CAST for compatibility with PG
4164  * function-like CASTs.
4165  */
4166  arg = transformJsonValueExpr(pstate, "JSON()", jsexpr->expr,
4167  JS_FORMAT_JSON, returning->typid, false);
4168  }
4169 
4171  returning, jsexpr->unique_keys, false,
4172  jsexpr->location);
4173 }
4174 
4175 /*
4176  * Transform a JSON_SCALAR() expression.
4177  *
4178  * JSON_SCALAR() is transformed into a JsonConstructorExpr of type
4179  * JSCTOR_JSON_SCALAR, which converts the input SQL scalar value into
4180  * a json[b] value.
4181  */
4182 static Node *
4184 {
4185  Node *arg = transformExprRecurse(pstate, (Node *) jsexpr->expr);
4186  JsonOutput *output = jsexpr->output;
4187  JsonReturning *returning;
4188 
4189  returning = transformJsonReturning(pstate, output, "JSON_SCALAR()");
4190 
4191  if (exprType(arg) == UNKNOWNOID)
4192  arg = coerce_to_specific_type(pstate, arg, TEXTOID, "JSON_SCALAR");
4193 
4195  returning, false, false, jsexpr->location);
4196 }
4197 
4198 /*
4199  * Transform a JSON_SERIALIZE() expression.
4200  *
4201  * JSON_SERIALIZE() is transformed into a JsonConstructorExpr of type
4202  * JSCTOR_JSON_SERIALIZE which converts the input JSON value into a character
4203  * or bytea string.
4204  */
4205 static Node *
4207 {
4208  JsonReturning *returning;
4209  Node *arg = transformJsonValueExpr(pstate, "JSON_SERIALIZE()",
4210  expr->expr,
4212  InvalidOid, false);
4213 
4214  if (expr->output)
4215  {
4216  returning = transformJsonOutput(pstate, expr->output, true);
4217 
4218  if (returning->typid != BYTEAOID)
4219  {
4220  char typcategory;
4221  bool typispreferred;
4222 
4223  get_type_category_preferred(returning->typid, &typcategory,
4224  &typispreferred);
4225  if (typcategory != TYPCATEGORY_STRING)
4226  ereport(ERROR,
4227  (errcode(ERRCODE_DATATYPE_MISMATCH),
4228  errmsg("cannot use RETURNING type %s in %s",
4229  format_type_be(returning->typid),
4230  "JSON_SERIALIZE()"),
4231  errhint("Try returning a string type or bytea.")));
4232  }
4233  }
4234  else
4235  {
4236  /* RETURNING TEXT FORMAT JSON is by default */
4237  returning = makeNode(JsonReturning);
4239  returning->typid = TEXTOID;
4240  returning->typmod = -1;
4241  }
4242 
4244  NULL, returning, false, false, expr->location);
4245 }
4246 
4247 /*
4248  * Transform JSON_VALUE, JSON_QUERY, JSON_EXISTS, JSON_TABLE functions into
4249  * a JsonExpr node.
4250  */
4251 static Node *
4253 {
4254  JsonExpr *jsexpr;
4255  Node *path_spec;
4256  const char *func_name = NULL;
4257  JsonFormatType default_format;
4258 
4259  switch (func->op)
4260  {
4261  case JSON_EXISTS_OP:
4262  func_name = "JSON_EXISTS";
4263  default_format = JS_FORMAT_DEFAULT;
4264  break;
4265  case JSON_QUERY_OP:
4266  func_name = "JSON_QUERY";
4267  default_format = JS_FORMAT_JSONB;
4268  break;
4269  case JSON_VALUE_OP:
4270  func_name = "JSON_VALUE";
4271  default_format = JS_FORMAT_DEFAULT;
4272  break;
4273  case JSON_TABLE_OP:
4274  func_name = "JSON_TABLE";
4275  default_format = JS_FORMAT_JSONB;
4276  break;
4277  default:
4278  elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4279  default_format = JS_FORMAT_DEFAULT; /* keep compiler quiet */
4280  break;
4281  }
4282 
4283  /*
4284  * Even though the syntax allows it, FORMAT JSON specification in
4285  * RETURNING is meaningless except for JSON_QUERY(). Flag if not
4286  * JSON_QUERY().
4287  */
4288  if (func->output && func->op != JSON_QUERY_OP)
4289  {
4291 
4292  if (format->format_type != JS_FORMAT_DEFAULT ||
4293  format->encoding != JS_ENC_DEFAULT)
4294  ereport(ERROR,
4295  errcode(ERRCODE_SYNTAX_ERROR),
4296  errmsg("cannot specify FORMAT JSON in RETURNING clause of %s()",
4297  func_name),
4298  parser_errposition(pstate, format->location));
4299  }
4300 
4301  /* OMIT QUOTES is meaningless when strings are wrapped. */
4302  if (func->op == JSON_QUERY_OP &&
4303  func->quotes == JS_QUOTES_OMIT &&
4304  (func->wrapper == JSW_CONDITIONAL ||
4305  func->wrapper == JSW_UNCONDITIONAL))
4306  ereport(ERROR,
4307  errcode(ERRCODE_SYNTAX_ERROR),
4308  errmsg("SQL/JSON QUOTES behavior must not be specified when WITH WRAPPER is used"),
4309  parser_errposition(pstate, func->location));
4310 
4311  jsexpr = makeNode(JsonExpr);
4312  jsexpr->location = func->location;
4313  jsexpr->op = func->op;
4314  jsexpr->column_name = func->column_name;
4315 
4316  /*
4317  * jsonpath machinery can only handle jsonb documents, so coerce the input
4318  * if not already of jsonb type.
4319  */
4320  jsexpr->formatted_expr = transformJsonValueExpr(pstate, func_name,
4321  func->context_item,
4322  default_format,
4323  JSONBOID,
4324  false);
4325  jsexpr->format = func->context_item->format;
4326 
4327  path_spec = transformExprRecurse(pstate, func->pathspec);
4328  path_spec = coerce_to_target_type(pstate, path_spec, exprType(path_spec),
4329  JSONPATHOID, -1,
4331  exprLocation(path_spec));
4332  if (path_spec == NULL)
4333  ereport(ERROR,
4334  (errcode(ERRCODE_DATATYPE_MISMATCH),
4335  errmsg("JSON path expression must be of type %s, not of type %s",
4336  "jsonpath", format_type_be(exprType(path_spec))),
4337  parser_errposition(pstate, exprLocation(path_spec))));
4338  jsexpr->path_spec = path_spec;
4339 
4340  /* Transform and coerce the PASSING arguments to jsonb. */
4341  transformJsonPassingArgs(pstate, func_name,
4343  func->passing,
4344  &jsexpr->passing_values,
4345  &jsexpr->passing_names);
4346 
4347  /* Transform the JsonOutput into JsonReturning. */
4348  jsexpr->returning = transformJsonOutput(pstate, func->output, false);
4349 
4350  switch (func->op)
4351  {
4352  case JSON_EXISTS_OP:
4353  /* JSON_EXISTS returns boolean by default. */
4354  if (!OidIsValid(jsexpr->returning->typid))
4355  {
4356  jsexpr->returning->typid = BOOLOID;
4357  jsexpr->returning->typmod = -1;
4358  }
4359 
4360  /* JSON_TABLE() COLUMNS can specify a non-boolean type. */
4361  if (jsexpr->returning->typid != BOOLOID)
4362  {
4363  Node *coercion_expr;
4364  CaseTestExpr *placeholder = makeNode(CaseTestExpr);
4365  int location = exprLocation((Node *) jsexpr);
4366 
4367  /*
4368  * We abuse CaseTestExpr here as placeholder to pass the
4369  * result of evaluating JSON_EXISTS to the coercion
4370  * expression.
4371  */
4372  placeholder->typeId = BOOLOID;
4373  placeholder->typeMod = -1;
4374  placeholder->collation = InvalidOid;
4375 
4376  coercion_expr =
4377  coerce_to_target_type(pstate, (Node *) placeholder, BOOLOID,
4378  jsexpr->returning->typid,
4379  jsexpr->returning->typmod,
4382  location);
4383 
4384  if (coercion_expr == NULL)
4385  ereport(ERROR,
4386  (errcode(ERRCODE_CANNOT_COERCE),
4387  errmsg("cannot cast type %s to %s",
4388  format_type_be(BOOLOID),
4389  format_type_be(jsexpr->returning->typid)),
4390  parser_coercion_errposition(pstate, location, (Node *) jsexpr)));
4391 
4392  if (coercion_expr != (Node *) placeholder)
4393  jsexpr->coercion_expr = coercion_expr;
4394  }
4395 
4396  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4398  jsexpr->returning);
4399  break;
4400 
4401  case JSON_QUERY_OP:
4402  /* JSON_QUERY returns jsonb by default. */
4403  if (!OidIsValid(jsexpr->returning->typid))
4404  {
4405  JsonReturning *ret = jsexpr->returning;
4406 
4407  ret->typid = JSONBOID;
4408  ret->typmod = -1;
4409  }
4410 
4411  /*
4412  * Keep quotes on scalar strings by default, omitting them only if
4413  * OMIT QUOTES is specified.
4414  */
4415  jsexpr->omit_quotes = (func->quotes == JS_QUOTES_OMIT);
4416  jsexpr->wrapper = func->wrapper;
4417 
4418  coerceJsonExprOutput(pstate, jsexpr);
4419 
4420  if (func->on_empty)
4421  jsexpr->on_empty = transformJsonBehavior(pstate,
4422  func->on_empty,
4424  jsexpr->returning);
4425  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4427  jsexpr->returning);
4428  break;
4429 
4430  case JSON_VALUE_OP:
4431  /* JSON_VALUE returns text by default. */
4432  if (!OidIsValid(jsexpr->returning->typid))
4433  {
4434  jsexpr->returning->typid = TEXTOID;
4435  jsexpr->returning->typmod = -1;
4436  }
4437 
4438  /*
4439  * Override whatever transformJsonOutput() set these to, which
4440  * assumes that output type to be jsonb.
4441  */
4444 
4445  /* Always omit quotes from scalar strings. */
4446  jsexpr->omit_quotes = true;
4447 
4448  coerceJsonExprOutput(pstate, jsexpr);
4449 
4450  if (func->on_empty)
4451  jsexpr->on_empty = transformJsonBehavior(pstate,
4452  func->on_empty,
4454  jsexpr->returning);
4455  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4457  jsexpr->returning);
4458  break;
4459 
4460  case JSON_TABLE_OP:
4461  if (!OidIsValid(jsexpr->returning->typid))
4462  {
4463  jsexpr->returning->typid = exprType(jsexpr->formatted_expr);
4464  jsexpr->returning->typmod = -1;
4465  }
4466  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4468  jsexpr->returning);
4469  break;
4470 
4471  default:
4472  elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4473  break;
4474  }
4475 
4476  return (Node *) jsexpr;
4477 }
4478 
4479 /*
4480  * Transform a SQL/JSON PASSING clause.
4481  */
4482 static void
4483 transformJsonPassingArgs(ParseState *pstate, const char *constructName,
4485  List **passing_values, List **passing_names)
4486 {
4487  ListCell *lc;
4488 
4489  *passing_values = NIL;
4490  *passing_names = NIL;
4491 
4492  foreach(lc, args)
4493  {
4495  Node *expr = transformJsonValueExpr(pstate, constructName,
4496  arg->val, format,
4497  InvalidOid, true);
4498 
4499  *passing_values = lappend(*passing_values, expr);
4500  *passing_names = lappend(*passing_names, makeString(arg->name));
4501  }
4502 }
4503 
4504 /*
4505  * Set up to coerce the result value of JSON_VALUE() / JSON_QUERY() to the
4506  * RETURNING type (default or user-specified), if needed.
4507  */
4508 static void
4510 {
4511  JsonReturning *returning = jsexpr->returning;
4512  Node *context_item = jsexpr->formatted_expr;
4513  int default_typmod;
4514  Oid default_typid;
4515  bool omit_quotes =
4516  jsexpr->op == JSON_QUERY_OP && jsexpr->omit_quotes;
4517  Node *coercion_expr = NULL;
4518 
4519  Assert(returning);
4520 
4521  /*
4522  * Check for cases where the coercion should be handled at runtime, that
4523  * is, without using a cast expression.
4524  */
4525  if (jsexpr->op == JSON_VALUE_OP)
4526  {
4527  /*
4528  * Use cast expressions for types with typmod and domain types.
4529  */
4530  if (returning->typmod == -1 &&
4531  get_typtype(returning->typid) != TYPTYPE_DOMAIN)
4532  {
4533  jsexpr->use_io_coercion = true;
4534  return;
4535  }
4536  }
4537  else if (jsexpr->op == JSON_QUERY_OP)
4538  {
4539  /*
4540  * Cast functions from jsonb to the following types (jsonb_bool() et
4541  * al) don't handle errors softly, so coerce either by calling
4542  * json_populate_type() or the type's input function so that any
4543  * errors are handled appropriately. The latter only if OMIT QUOTES is
4544  * true.
4545  */
4546  switch (returning->typid)
4547  {
4548  case BOOLOID:
4549  case NUMERICOID:
4550  case INT2OID:
4551  case INT4OID:
4552  case INT8OID:
4553  case FLOAT4OID:
4554  case FLOAT8OID:
4555  if (jsexpr->omit_quotes)
4556  jsexpr->use_io_coercion = true;
4557  else
4558  jsexpr->use_json_coercion = true;
4559  return;
4560  default:
4561  break;
4562  }
4563  }
4564 
4565  /* Look up a cast expression. */
4566 
4567  /*
4568  * For JSON_VALUE() and for JSON_QUERY() when OMIT QUOTES is true,
4569  * ExecEvalJsonExprPath() will convert a quote-stripped source value to
4570  * its text representation, so use TEXTOID as the source type.
4571  */
4572  if (omit_quotes || jsexpr->op == JSON_VALUE_OP)
4573  {
4574  default_typid = TEXTOID;
4575  default_typmod = -1;
4576  }
4577  else
4578  {
4579  default_typid = exprType(context_item);
4580  default_typmod = exprTypmod(context_item);
4581  }
4582 
4583  if (returning->typid != default_typid ||
4584  returning->typmod != default_typmod)
4585  {
4586  /*
4587  * We abuse CaseTestExpr here as placeholder to pass the result of
4588  * jsonpath evaluation as input to the coercion expression.
4589  */
4590  CaseTestExpr *placeholder = makeNode(CaseTestExpr);
4591 
4592  placeholder->typeId = default_typid;
4593  placeholder->typeMod = default_typmod;
4594 
4595  coercion_expr = coerceJsonFuncExpr(pstate, (Node *) placeholder,
4596  returning, false);
4597  if (coercion_expr == (Node *) placeholder)
4598  coercion_expr = NULL;
4599  }
4600 
4601  jsexpr->coercion_expr = coercion_expr;
4602 
4603  if (coercion_expr == NULL)
4604  {
4605  /*
4606  * Either no cast was found or coercion is unnecessary but still must
4607  * convert the string value to the output type.
4608  */
4609  if (omit_quotes || jsexpr->op == JSON_VALUE_OP)
4610  jsexpr->use_io_coercion = true;
4611  else
4612  jsexpr->use_json_coercion = true;
4613  }
4614 
4615  Assert(jsexpr->coercion_expr != NULL ||
4616  (jsexpr->use_io_coercion != jsexpr->use_json_coercion));
4617 }
4618 
4619 /*
4620  * Recursively checks if the given expression, or its sub-node in some cases,
4621  * is valid for using as an ON ERROR / ON EMPTY DEFAULT expression.
4622  */
4623 static bool
4625 {
4626  if (expr == NULL)
4627  return false;
4628 
4629  switch (nodeTag(expr))
4630  {
4631  /* Acceptable expression nodes */
4632  case T_Const:
4633  case T_FuncExpr:
4634  case T_OpExpr:
4635  return true;
4636 
4637  /* Acceptable iff arg of the following nodes is one of the above */
4638  case T_CoerceViaIO:
4639  case T_CoerceToDomain:
4640  case T_ArrayCoerceExpr:
4641  case T_ConvertRowtypeExpr:
4642  case T_RelabelType:
4643  case T_CollateExpr:
4645  context);
4646  default:
4647  break;
4648  }
4649 
4650  return false;
4651 }
4652 
4653 /*
4654  * Transform a JSON BEHAVIOR clause.
4655  */
4656 static JsonBehavior *
4658  JsonBehaviorType default_behavior,
4659  JsonReturning *returning)
4660 {
4661  JsonBehaviorType btype = default_behavior;
4662  Node *expr = NULL;
4663  bool coerce_at_runtime = false;
4664  int location = -1;
4665 
4666  if (behavior)
4667  {
4668  btype = behavior->btype;
4669  location = behavior->location;
4670  if (btype == JSON_BEHAVIOR_DEFAULT)
4671  {
4672  expr = transformExprRecurse(pstate, behavior->expr);
4673  if (!ValidJsonBehaviorDefaultExpr(expr, NULL))
4674  ereport(ERROR,
4675  (errcode(ERRCODE_DATATYPE_MISMATCH),
4676  errmsg("can only specify a constant, non-aggregate function, or operator expression for DEFAULT"),
4677  parser_errposition(pstate, exprLocation(expr))));
4678  if (contain_var_clause(expr))
4679  ereport(ERROR,
4680  (errcode(ERRCODE_DATATYPE_MISMATCH),
4681  errmsg("DEFAULT expression must not contain column references"),
4682  parser_errposition(pstate, exprLocation(expr))));
4683  if (expression_returns_set(expr))
4684  ereport(ERROR,
4685  (errcode(ERRCODE_DATATYPE_MISMATCH),
4686  errmsg("DEFAULT expression must not return a set"),
4687  parser_errposition(pstate, exprLocation(expr))));
4688  }
4689  }
4690 
4691  if (expr == NULL && btype != JSON_BEHAVIOR_ERROR)
4692  expr = GetJsonBehaviorConst(btype, location);
4693 
4694  if (expr)
4695  {
4696  Node *coerced_expr = expr;
4697  bool isnull = (IsA(expr, Const) && ((Const *) expr)->constisnull);
4698 
4699  /*
4700  * Coerce NULLs and "internal" (that is, not specified by the user)
4701  * jsonb-valued expressions at runtime using json_populate_type().
4702  *
4703  * For other (user-specified) non-NULL values, try to find a cast and
4704  * error out if one is not found.
4705  */
4706  if (isnull ||
4707  (exprType(expr) == JSONBOID &&
4708  btype == default_behavior))
4709  coerce_at_runtime = true;
4710  else
4711  coerced_expr =
4712  coerce_to_target_type(pstate, expr, exprType(expr),
4713  returning->typid, returning->typmod,
4715  exprLocation((Node *) behavior));
4716 
4717  if (coerced_expr == NULL)
4718  ereport(ERROR,
4719  errcode(ERRCODE_CANNOT_COERCE),
4720  errmsg("cannot cast behavior expression of type %s to %s",
4721  format_type_be(exprType(expr)),
4722  format_type_be(returning->typid)),
4723  parser_errposition(pstate, exprLocation(expr)));
4724  else
4725  expr = coerced_expr;
4726  }
4727 
4728  if (behavior)
4729  behavior->expr = expr;
4730  else
4731  behavior = makeJsonBehavior(btype, expr, location);
4732 
4733  behavior->coerce = coerce_at_runtime;
4734 
4735  return behavior;
4736 }
4737 
4738 /*
4739  * Returns a Const node holding the value for the given non-ERROR
4740  * JsonBehaviorType.
4741  */
4742 static Node *
4744 {
4745  Datum val = (Datum) 0;
4746  Oid typid = JSONBOID;
4747  int len = -1;
4748  bool isbyval = false;
4749  bool isnull = false;
4750  Const *con;
4751 
4752  switch (btype)
4753  {
4756  break;
4757 
4760  break;
4761 
4762  case JSON_BEHAVIOR_TRUE:
4763  val = BoolGetDatum(true);
4764  typid = BOOLOID;
4765  len = sizeof(bool);
4766  isbyval = true;
4767  break;
4768 
4769  case JSON_BEHAVIOR_FALSE:
4770  val = BoolGetDatum(false);
4771  typid = BOOLOID;
4772  len = sizeof(bool);
4773  isbyval = true;
4774  break;
4775 
4776  case JSON_BEHAVIOR_NULL:
4777  case JSON_BEHAVIOR_UNKNOWN:
4778  case JSON_BEHAVIOR_EMPTY:
4779  val = (Datum) 0;
4780  isnull = true;
4781  typid = INT4OID;
4782  len = sizeof(int32);
4783  isbyval = true;
4784  break;
4785 
4786  /* These two behavior types are handled by the caller. */
4787  case JSON_BEHAVIOR_DEFAULT:
4788  case JSON_BEHAVIOR_ERROR:
4789  Assert(false);
4790  break;
4791 
4792  default:
4793  elog(ERROR, "unrecognized SQL/JSON behavior %d", btype);
4794  break;
4795  }
4796 
4797  con = makeConst(typid, -1, InvalidOid, len, val, isnull, isbyval);
4798  con->location = location;
4799 
4800  return (Node *) con;
4801 }
#define InvalidAttrNumber
Definition: attnum.h:23
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:123
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1109
signed int int32
Definition: c.h:494
#define Assert(condition)
Definition: c.h:858
unsigned char bool
Definition: c.h:456
#define OidIsValid(objectId)
Definition: c.h:775
enc
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:71
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3153
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
int errdetail(const char *fmt,...)
Definition: elog.c:1205
int errhint(const char *fmt,...)
Definition: elog.c:1319
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define _(x)
Definition: elog.c:90
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
void err(int eval, const char *fmt,...)
Definition: err.c:43
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
Oid MyDatabaseId
Definition: globals.c:91
#define MaxTupleAttributeNumber
Definition: htup_details.h:34
#define funcname
Definition: indent_codes.h:69
FILE * output
long val
Definition: informix.c:670
int b
Definition: isn.c:70
int x
Definition: isn.c:71
int a
Definition: isn.c:69
int j
Definition: isn.c:74
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Datum jsonb_in(PG_FUNCTION_ARGS)
Definition: jsonb.c:73
List * list_truncate(List *list, int new_size)
Definition: list.c:631
List * lappend(List *list, void *datum)
Definition: list.c:339
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
List * lcons(void *datum, List *list)
Definition: list.c:495
List * list_delete_last(List *list)
Definition: list.c:957
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2759
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2655
List * get_op_btree_interpretation(Oid opno)
Definition: lsyscache.c:601
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3081
char get_typtype(Oid typid)
Definition: lsyscache.c:2629
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2538
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2787
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2710
#define type_is_array(typid)
Definition: lsyscache.h:209
Var * makeWholeRowVar(RangeTblEntry *rte, int varno, Index varlevelsup, bool allowScalar)
Definition: makefuncs.c:135
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Definition: makefuncs.c:521
JsonBehavior * makeJsonBehavior(JsonBehaviorType btype, Node *expr, int location)
Definition: makefuncs.c:880
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:240
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:301
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:424
JsonFormat * makeJsonFormat(JsonFormatType type, JsonEncoding encoding, int location)
Definition: makefuncs.c:847
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:371
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:359
JsonValueExpr * makeJsonValueExpr(Expr *raw_expr, Expr *formatted_expr, JsonFormat *format)
Definition: makefuncs.c:863
Node * makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType item_type, bool unique_keys, int location)
Definition: makefuncs.c:911
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void * palloc(Size size)
Definition: mcxt.c:1316
void namestrcpy(Name name, const char *str)
Definition: name.c:233
char * NameListToString(const List *names)
Definition: namespace.c:3579
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:816
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1386
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:758
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define copyObject(obj)
Definition: nodes.h:224
#define nodeTag(nodeptr)
Definition: nodes.h:133
@ CMD_SELECT
Definition: nodes.h:265
@ AGGSPLIT_SIMPLE
Definition: nodes.h:376
#define NodeSetTag(nodeptr, t)
Definition: nodes.h:156
#define makeNode(_type_)
Definition: nodes.h:155
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
Node * transformGroupingFunc(ParseState *pstate, GroupingFunc *p)
Definition: parse_agg.c:260
void transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, WindowDef *windef)
Definition: parse_agg.c:822
void transformAggregateCall(ParseState *pstate, Aggref *agg, List *args, List *aggorder, bool agg_distinct)
Definition: parse_agg.c:104
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:157
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:78
bool verify_common_type(Oid common_type, List *exprs)
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
int parser_coercion_errposition(ParseState *pstate, int coerce_location, Node *input_expr)
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
void assign_expr_collations(ParseState *pstate, Node *expr)
static Node * transformMergeSupportFunc(ParseState *pstate, MergeSupportFunc *f)
Definition: parse_expr.c:1378
static JsonReturning * transformJsonOutput(ParseState *pstate, const JsonOutput *output, bool allow_format)
Definition: parse_expr.c:3490
static Node * make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
Definition: parse_expr.c:3087
static Node * transformJsonValueExpr(ParseState *pstate, const char *constructName, JsonValueExpr *ve, JsonFormatType default_format, Oid targettype, bool isarg)
Definition: parse_expr.c:3278
static void unknown_attribute(ParseState *pstate, Node *relref, const char *attname, int location)
Definition: parse_expr.c:393
static Node * makeJsonByteaToTextConversion(Node *expr, JsonFormat *format, int location)
Definition: parse_expr.c:3256
static Node * transformJsonArrayConstructor(ParseState *pstate, JsonArrayConstructor *ctor)
Definition: parse_expr.c:3992
static Node * transformJsonScalarExpr(ParseState *pstate, JsonScalarExpr *expr)
Definition: parse_expr.c:4183
static Node * transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
Definition: parse_expr.c:2560
static Node * transformAExprOpAll(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1019
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:121
static Node * transformAExprIn(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1126
static JsonReturning * transformJsonConstructorOutput(ParseState *pstate, JsonOutput *output, List *args)
Definition: parse_expr.c:3537
static void checkJsonOutputFormat(ParseState *pstate, const JsonFormat *format, Oid targettype, bool allow_format_for_non_strings)
Definition: parse_expr.c:3440
static Node * transformAExprOpAny(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1005
static Node * transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
Definition: parse_expr.c:2476
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:139
static void coerceJsonExprOutput(ParseState *pstate, JsonExpr *jsexpr)
Definition: parse_expr.c:4509
static JsonReturning * transformJsonReturning(ParseState *pstate, JsonOutput *output, const char *fname)
Definition: parse_expr.c:4095
static Node * transformAExprNullIf(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1084
static Node * transformJsonAggConstructor(ParseState *pstate, JsonAggConstructor *agg_ctor, JsonReturning *returning, List *args, Oid aggfnoid, Oid aggtype, JsonConstructorType ctor_type, bool unique, bool absent_on_null)
Definition: parse_expr.c:3806
static bool exprIsNullConstant(Node *arg)
Definition: parse_expr.c:911
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:3054
static Node * transformJsonArrayQueryConstructor(ParseState *pstate, JsonArrayQueryConstructor *ctor)
Definition: parse_expr.c:3735
static Node * transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
Definition: parse_expr.c:2294
static Node * transformColumnRef(ParseState *pstate, ColumnRef *cref)
Definition: parse_expr.c:511
static Node * transformCollateClause(ParseState *pstate, CollateClause *c)
Definition: parse_expr.c:2768
static Node * transformBoolExpr(ParseState *pstate, BoolExpr *a)
Definition: parse_expr.c:1403
static bool ValidJsonBehaviorDefaultExpr(Node *expr, void *context)
Definition: parse_expr.c:4624
static Node * transformFuncCall(ParseState *pstate, FuncCall *fn)
Definition: parse_expr.c:1439
static Node * makeJsonConstructorExpr(ParseState *pstate, JsonConstructorType type, List *args, Expr *fexpr, JsonReturning *returning, bool unique, bool absent_on_null, int location)
Definition: parse_expr.c:3636
static Node * transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
Definition: parse_expr.c:2255
static Node * transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
Definition: parse_expr.c:2206
bool Transform_null_equals
Definition: parse_expr.c:46
static Node * transformSubLink(ParseState *pstate, SubLink *sublink)
Definition: parse_expr.c:1772
static void transformJsonPassingArgs(ParseState *pstate, const char *constructName, JsonFormatType format, List *args, List **passing_values, List **passing_names)
Definition: parse_expr.c:4483
static Node * transformJsonObjectConstructor(ParseState *pstate, JsonObjectConstructor *ctor)
Definition: parse_expr.c:3698
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2808
static Node * transformAExprOp(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:924
static Node * GetJsonBehaviorConst(JsonBehaviorType btype, int location)
Definition: parse_expr.c:4743
static JsonBehavior * transformJsonBehavior(ParseState *pstate, JsonBehavior *behavior, JsonBehaviorType default_behavior, JsonReturning *returning)
Definition: parse_expr.c:4657
static Node * transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg)
Definition: parse_expr.c:3952
static Node * transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
Definition: parse_expr.c:2015
static Node * transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
Definition: parse_expr.c:1484
static Node * coerceJsonFuncExpr(ParseState *pstate, Node *expr, const JsonReturning *returning, bool report_error)
Definition: parse_expr.c:3580
static Node * transformBooleanTest(ParseState *pstate, BooleanTest *b)
Definition: parse_expr.c:2520
static Node * make_row_distinct_op(ParseState *pstate, List *opname, RowExpr *lrow, RowExpr *rrow, int location)
Definition: parse_expr.c:3010
static Node * transformTypeCast(ParseState *pstate, TypeCast *tc)
Definition: parse_expr.c:2684
static Node * transformParamRef(ParseState *pstate, ParamRef *pref)
Definition: parse_expr.c:887
static Node * transformCaseExpr(ParseState *pstate, CaseExpr *c)
Definition: parse_expr.c:1632
static Node * transformJsonParseExpr(ParseState *pstate, JsonParseExpr *expr)
Definition: parse_expr.c:4134
static Node * transformIndirection(ParseState *pstate, A_Indirection *ind)
Definition: parse_expr.c:439
const char * ParseExprKindName(ParseExprKind exprKind)
Definition: parse_expr.c:3111
static Node * transformJsonParseArg(ParseState *pstate, Node *jsexpr, JsonFormat *format, Oid *exprtype)
Definition: parse_expr.c:4022
static Node * transformJsonSerializeExpr(ParseState *pstate, JsonSerializeExpr *expr)
Definition: parse_expr.c:4206
static Node * transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
Definition: parse_expr.c:2168
static Node * transformXmlExpr(ParseState *pstate, XmlExpr *x)
Definition: parse_expr.c:2347
static Node * transformAExprBetween(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1284
static Node * transformWholeRowRef(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location)
Definition: parse_expr.c:2612
static Const * getJsonEncodingConst(JsonFormat *format)
Definition: parse_expr.c:3217
static Node * transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred)
Definition: parse_expr.c:4072
static Node * transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *p)
Definition: parse_expr.c:4252
static Node * transformJsonObjectAgg(ParseState *pstate, JsonObjectAgg *agg)
Definition: parse_expr.c:3890
static Node * transformAExprDistinct(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1033
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:90
void free_parsestate(ParseState *pstate)
Definition: parse_node.c:72
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
Definition: parse_node.c:243
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:39
Const * make_const(ParseState *pstate, A_Const *aconst)
Definition: parse_node.c:347
ParseExprKind
Definition: parse_node.h:39
@ EXPR_KIND_EXECUTE_PARAMETER
Definition: parse_node.h:76
@ EXPR_KIND_DOMAIN_CHECK
Definition: parse_node.h:69
@ EXPR_KIND_COPY_WHERE
Definition: parse_node.h:82
@ EXPR_KIND_COLUMN_DEFAULT
Definition: parse_node.h:70
@ EXPR_KIND_DISTINCT_ON
Definition: parse_node.h:61
@ EXPR_KIND_MERGE_WHEN
Definition: parse_node.h:58
@ EXPR_KIND_STATS_EXPRESSION
Definition: parse_node.h:74
@ EXPR_KIND_INDEX_EXPRESSION
Definition: parse_node.h:72
@ EXPR_KIND_MERGE_RETURNING
Definition: parse_node.h:65
@ EXPR_KIND_PARTITION_BOUND
Definition: parse_node.h:79
@ EXPR_KIND_FUNCTION_DEFAULT
Definition: parse_node.h:71
@ EXPR_KIND_WINDOW_FRAME_RANGE
Definition: parse_node.h:51
@ EXPR_KIND_VALUES
Definition: parse_node.h:66
@ EXPR_KIND_FROM_SUBSELECT
Definition: parse_node.h:44
@ EXPR_KIND_POLICY
Definition: parse_node.h:78
@ EXPR_KIND_WINDOW_FRAME_GROUPS
Definition: parse_node.h:53
@ EXPR_KIND_PARTITION_EXPRESSION
Definition: parse_node.h:80
@ EXPR_KIND_JOIN_USING
Definition: parse_node.h:43
@ EXPR_KIND_INDEX_PREDICATE
Definition: parse_node.h:73
@ EXPR_KIND_ORDER_BY
Definition: parse_node.h:60
@ EXPR_KIND_OFFSET
Definition: parse_node.h:63
@ EXPR_KIND_JOIN_ON
Definition: parse_node.h:42
@ EXPR_KIND_HAVING
Definition: parse_node.h:47
@ EXPR_KIND_INSERT_TARGET
Definition: parse_node.h:55
@ EXPR_KIND_ALTER_COL_TRANSFORM
Definition: parse_node.h:75
@ EXPR_KIND_LIMIT
Definition: parse_node.h:62
@ EXPR_KIND_WHERE
Definition: parse_node.h:46
@ EXPR_KIND_UPDATE_TARGET
Definition: parse_node.h:57
@ EXPR_KIND_SELECT_TARGET
Definition: parse_node.h:54
@ EXPR_KIND_RETURNING
Definition: parse_node.h:64
@ EXPR_KIND_GENERATED_COLUMN
Definition: parse_node.h:83
@ EXPR_KIND_NONE
Definition: parse_node.h:40
@ EXPR_KIND_CALL_ARGUMENT
Definition: parse_node.h:81
@ EXPR_KIND_GROUP_BY
Definition: parse_node.h:59
@ EXPR_KIND_OTHER
Definition: parse_node.h:41
@ EXPR_KIND_FROM_FUNCTION
Definition: parse_node.h:45
@ EXPR_KIND_TRIGGER_WHEN
Definition: parse_node.h:77
@ EXPR_KIND_FILTER
Definition: parse_node.h:48
@ EXPR_KIND_UPDATE_SOURCE
Definition: parse_node.h:56
@ EXPR_KIND_CHECK_CONSTRAINT
Definition: parse_node.h:68
@ EXPR_KIND_WINDOW_PARTITION
Definition: parse_node.h:49
@ EXPR_KIND_CYCLE_MARK
Definition: parse_node.h:84
@ EXPR_KIND_WINDOW_FRAME_ROWS
Definition: parse_node.h:52
@ EXPR_KIND_WINDOW_ORDER
Definition: parse_node.h:50
@ EXPR_KIND_VALUES_SINGLE
Definition: parse_node.h:67
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:660
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:770
void markNullableIfNeeded(ParseState *pstate, Var *var)
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
void errorMissingColumn(ParseState *pstate, const char *relname, const char *colname, int location)
void markVarForSelectPriv(ParseState *pstate, Var *var)
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
Node * colNameToVar(ParseState *pstate, const char *colname, bool localonly, int location)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
Definition: parse_target.c:220
char * FigureColname(Node *node)
Oid LookupCollation(ParseState *pstate, List *collnames, int location)
Definition: parse_type.c:515
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310
#define ISCOMPLEX(typeid)
Definition: parse_type.h:59
@ JS_QUOTES_OMIT
Definition: parsenodes.h:1782
@ AEXPR_BETWEEN
Definition: parsenodes.h:323
@ AEXPR_NULLIF
Definition: parsenodes.h:318
@ AEXPR_NOT_DISTINCT
Definition: parsenodes.h:317
@ AEXPR_BETWEEN_SYM
Definition: parsenodes.h:325
@ AEXPR_NOT_BETWEEN_SYM
Definition: parsenodes.h:326
@ AEXPR_ILIKE
Definition: parsenodes.h:321
@ AEXPR_IN
Definition: parsenodes.h:319
@ AEXPR_NOT_BETWEEN
Definition: parsenodes.h:324
@ AEXPR_DISTINCT
Definition: parsenodes.h:316
@ AEXPR_SIMILAR
Definition: parsenodes.h:322
@ AEXPR_LIKE
Definition: parsenodes.h:320
@ AEXPR_OP
Definition: parsenodes.h:313
@ AEXPR_OP_ANY
Definition: parsenodes.h:314
@ AEXPR_OP_ALL
Definition: parsenodes.h:315
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition: analyze.c:221
Query * transformStmt(ParseState *pstate, Node *parseTree)
Definition: analyze.c:311
NameData attname
Definition: pg_attribute.h:41
void * arg
static char format
NameData relname
Definition: pg_class.h:38
#define NAMEDATALEN
const void size_t len
int32 encoding
Definition: pg_database.h:41
#define lfirst(lc)
Definition: pg_list.h:172
#define llast(l)
Definition: pg_list.h:198
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define lthird(l)
Definition: pg_list.h:188
#define list_make1(x1)
Definition: pg_list.h:212
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define lfourth(l)
Definition: pg_list.h:193
#define list_make2(x1, x2)
Definition: pg_list.h:214
#define snprintf
Definition: port.h:238
void check_stack_depth(void)
Definition: postgres.c:3531
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:373
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
char * c
e
Definition: preproc-init.c:82
@ IS_NOT_TRUE
Definition: primnodes.h:1948
@ IS_NOT_FALSE
Definition: primnodes.h:1948
@ IS_NOT_UNKNOWN
Definition: primnodes.h:1948
@ IS_TRUE
Definition: primnodes.h:1948
@ IS_UNKNOWN
Definition: primnodes.h:1948
@ IS_FALSE
Definition: primnodes.h:1948
@ ARRAY_SUBLINK
Definition: primnodes.h:973
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:972
@ EXPR_SUBLINK
Definition: primnodes.h:971
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:970
@ EXISTS_SUBLINK
Definition: primnodes.h:967
JsonFormatType
Definition: primnodes.h:1607
@ JS_FORMAT_JSONB
Definition: primnodes.h:1610
@ JS_FORMAT_DEFAULT
Definition: primnodes.h:1608
@ JS_FORMAT_JSON
Definition: primnodes.h:1609
@ IS_GREATEST
Definition: primnodes.h:1472
@ AND_EXPR
Definition: primnodes.h:901
@ OR_EXPR
Definition: primnodes.h:901
@ NOT_EXPR
Definition: primnodes.h:901
JsonEncoding
Definition: primnodes.h:1595
@ JS_ENC_DEFAULT
Definition: primnodes.h:1596
@ JS_ENC_UTF32
Definition: primnodes.h:1599
@ JS_ENC_UTF8
Definition: primnodes.h:1597
@ JS_ENC_UTF16
Definition: primnodes.h:1598
@ SVFOP_CURRENT_CATALOG
Definition: primnodes.h:1519
@ SVFOP_LOCALTIME_N
Definition: primnodes.h:1512
@ SVFOP_CURRENT_TIMESTAMP
Definition: primnodes.h:1509
@ SVFOP_LOCALTIME
Definition: primnodes.h:1511
@ SVFOP_CURRENT_TIMESTAMP_N
Definition: primnodes.h:1510
@ SVFOP_CURRENT_ROLE
Definition: primnodes.h:1515
@ SVFOP_USER
Definition: primnodes.h:1517
@ SVFOP_CURRENT_SCHEMA
Definition: primnodes.h:1520
@ SVFOP_LOCALTIMESTAMP_N
Definition: primnodes.h:1514
@ SVFOP_CURRENT_DATE
Definition: primnodes.h:1506
@ SVFOP_CURRENT_TIME_N
Definition: primnodes.h:1508
@ SVFOP_CURRENT_TIME
Definition: primnodes.h:1507
@ SVFOP_LOCALTIMESTAMP
Definition: primnodes.h:1513
@ SVFOP_CURRENT_USER
Definition: primnodes.h:1516
@ SVFOP_SESSION_USER
Definition: primnodes.h:1518
@ PARAM_MULTIEXPR
Definition: primnodes.h:370
@ PARAM_EXTERN
Definition: primnodes.h:367
@ PARAM_SUBLINK
Definition: primnodes.h:369
@ JSW_UNCONDITIONAL
Definition: primnodes.h:1719
@ JSW_CONDITIONAL
Definition: primnodes.h:1718
@ IS_DOCUMENT
Definition: primnodes.h:1557
@ IS_XMLFOREST
Definition: primnodes.h:1552
@ IS_XMLCONCAT
Definition: primnodes.h:1550
@ IS_XMLPI
Definition: primnodes.h:1554
@ IS_XMLPARSE
Definition: primnodes.h:1553
@ IS_XMLSERIALIZE
Definition: primnodes.h:1556
@ IS_XMLROOT
Definition: primnodes.h:1555
@ IS_XMLELEMENT
Definition: primnodes.h:1551
RowCompareType
Definition: primnodes.h:1423
@ ROWCOMPARE_NE
Definition: primnodes.h:1430
@ ROWCOMPARE_EQ
Definition: primnodes.h:1427
JsonBehaviorType
Definition: primnodes.h:1730
@ JSON_BEHAVIOR_ERROR
Definition: primnodes.h:1732
@ JSON_BEHAVIOR_TRUE
Definition: primnodes.h:1734
@ JSON_BEHAVIOR_DEFAULT
Definition: primnodes.h:1739
@ JSON_BEHAVIOR_EMPTY
Definition: primnodes.h:1733
@ JSON_BEHAVIOR_FALSE
Definition: primnodes.h:1735
@ JSON_BEHAVIOR_NULL
Definition: primnodes.h:1731
@ JSON_BEHAVIOR_EMPTY_OBJECT
Definition: primnodes.h:1738
@ JSON_BEHAVIOR_UNKNOWN
Definition: primnodes.h:1736
@ JSON_BEHAVIOR_EMPTY_ARRAY
Definition: primnodes.h:1737
@ JSON_QUERY_OP
Definition: primnodes.h:1769
@ JSON_TABLE_OP
Definition: primnodes.h:1771
@ JSON_EXISTS_OP
Definition: primnodes.h:1768
@ JSON_VALUE_OP
Definition: primnodes.h:1770
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:706
@ COERCE_EXPLICIT_CAST
Definition: primnodes.h:705
@ COERCE_EXPLICIT_CALL
Definition: primnodes.h:704
@ IS_NULL
Definition: primnodes.h:1924
@ IS_NOT_NULL
Definition: primnodes.h:1924
@ COERCION_EXPLICIT
Definition: primnodes.h:687
@ COERCION_IMPLICIT
Definition: primnodes.h:684
JsonConstructorType
Definition: primnodes.h:1655
@ JSCTOR_JSON_SERIALIZE
Definition: primnodes.h:1662
@ JSCTOR_JSON_ARRAYAGG
Definition: primnodes.h:1659
@ JSCTOR_JSON_PARSE
Definition: primnodes.h:1660
@ JSCTOR_JSON_OBJECT
Definition: primnodes.h:1656
@ JSCTOR_JSON_SCALAR
Definition: primnodes.h:1661
@ JSCTOR_JSON_ARRAY
Definition: primnodes.h:1657
@ JSCTOR_JSON_OBJECTAGG
Definition: primnodes.h:1658
tree context
Definition: radixtree.h:1829
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:376
bool isnull
Definition: parsenodes.h:363
ParseLoc location
Definition: parsenodes.h:364
ParseLoc location
Definition: parsenodes.h:338
A_Expr_Kind kind
Definition: parsenodes.h:334
Oid aggfnoid
Definition: primnodes.h:444
Expr * aggfilter
Definition: primnodes.h:477
ParseLoc location
Definition: primnodes.h:507
char * aliasname
Definition: primnodes.h:50
List * colnames
Definition: primnodes.h:51
ParseLoc location
Definition: primnodes.h:1354
List * elements
Definition: primnodes.h:1350
Expr * arg
Definition: primnodes.h:1283
ParseLoc location
Definition: primnodes.h:1286
Expr * defresult
Definition: primnodes.h:1285
List * args
Definition: primnodes.h:1284
Expr * result
Definition: primnodes.h:1296
Expr * expr
Definition: primnodes.h:1295
ParseLoc location
Definition: primnodes.h:1297
List * args
Definition: primnodes.h:1462
ParseLoc location
Definition: primnodes.h:1464
Expr * arg
Definition: primnodes.h:1249
ParseLoc location
Definition: primnodes.h:1251
ParseLoc location
Definition: parsenodes.h:295
List * fields
Definition: parsenodes.h:294
char * cursor_name
Definition: primnodes.h:2070
ParseLoc location
Definition: primnodes.h:740
struct WindowDef * over
Definition: parsenodes.h:1973
JsonOutput * output
Definition: parsenodes.h:1970
bool absent_on_null
Definition: parsenodes.h:1999
JsonValueExpr * arg
Definition: parsenodes.h:1998
JsonAggConstructor * constructor
Definition: parsenodes.h:1997
JsonOutput * output
Definition: parsenodes.h:1943
Node * expr
Definition: primnodes.h:1757
ParseLoc location
Definition: primnodes.h:1759
JsonBehaviorType btype
Definition: primnodes.h:1756
JsonReturning * returning
Definition: primnodes.h:1676
JsonConstructorType type
Definition: primnodes.h:1672
char * column_name
Definition: primnodes.h:1785
Node * formatted_expr
Definition: primnodes.h:1789
ParseLoc location
Definition: primnodes.h:1831
List * passing_values
Definition: primnodes.h:1802
JsonBehavior * on_empty
Definition: primnodes.h:1805
JsonFormat * format
Definition: primnodes.h:1792
List * passing_names
Definition: primnodes.h:1801
Node * path_spec
Definition: primnodes.h:1795
bool use_io_coercion
Definition: primnodes.h:1818
JsonReturning * returning
Definition: primnodes.h:1798
bool use_json_coercion
Definition: primnodes.h:1819
JsonWrapper wrapper
Definition: primnodes.h:1822
JsonExprOp op
Definition: primnodes.h:1783
JsonBehavior * on_error
Definition: primnodes.h:1806
bool omit_quotes
Definition: primnodes.h:1825
Node * coercion_expr
Definition: primnodes.h:1817
ParseLoc location
Definition: primnodes.h:1623
JsonEncoding encoding
Definition: primnodes.h:1622
JsonFormatType format_type
Definition: primnodes.h:1621
JsonOutput * output
Definition: parsenodes.h:1799
char * column_name
Definition: parsenodes.h:1794
JsonWrapper wrapper
Definition: parsenodes.h:1802
JsonQuotes quotes
Definition: parsenodes.h:1803
JsonExprOp op
Definition: parsenodes.h:1793
List * passing
Definition: parsenodes.h:1798
JsonBehavior * on_empty
Definition: parsenodes.h:1800
ParseLoc location
Definition: parsenodes.h:1804
Node * pathspec
Definition: parsenodes.h:1797
JsonBehavior * on_error
Definition: parsenodes.h:1801
JsonValueExpr * context_item
Definition: parsenodes.h:1796
JsonFormat * format
Definition: primnodes.h:1702
JsonValueType item_type
Definition: primnodes.h:1703
ParseLoc location
Definition: primnodes.h:1705
JsonValueExpr * value
Definition: parsenodes.h:1881
JsonAggConstructor * constructor
Definition: parsenodes.h:1984
JsonKeyValue * arg
Definition: parsenodes.h:1985
bool absent_on_null
Definition: parsenodes.h:1986
JsonOutput * output
Definition: parsenodes.h:1929
JsonReturning * returning
Definition: parsenodes.h:1760
JsonValueExpr * expr
Definition: parsenodes.h:1891
ParseLoc location
Definition: parsenodes.h:1894
JsonOutput * output
Definition: parsenodes.h:1892
JsonFormat * format
Definition: primnodes.h:1633
ParseLoc location
Definition: parsenodes.h:1906
JsonOutput * output
Definition: parsenodes.h:1905
JsonOutput * output
Definition: parsenodes.h:1917
JsonValueExpr * expr
Definition: parsenodes.h:1916
Expr * formatted_expr
Definition: primnodes.h:1650
JsonFormat * format
Definition: primnodes.h:1651
Expr * raw_expr
Definition: primnodes.h:1649
Definition: pg_list.h:54
ParseLoc location
Definition: primnodes.h:606
List * args
Definition: primnodes.h:1488
ParseLoc location
Definition: primnodes.h:1490
MinMaxOp op
Definition: primnodes.h:1486
Expr * arg
Definition: primnodes.h:761
Definition: nodes.h:129
NullTestType nulltesttype
Definition: primnodes.h:1931
ParseLoc location
Definition: primnodes.h:1934
Expr * arg
Definition: primnodes.h:1930
List * args
Definition: primnodes.h:806
ParseLoc location
Definition: parsenodes.h:305
int number
Definition: parsenodes.h:304
ParseLoc location
Definition: primnodes.h:384
int paramid
Definition: primnodes.h:377
Oid paramtype
Definition: primnodes.h:378
ParamKind paramkind
Definition: primnodes.h:376
RangeTblEntry * p_rte
Definition: parse_node.h:287
ParseState * parentParseState
Definition: parse_node.h:192
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:208
ParseExprKind p_expr_kind
Definition: parse_node.h:211
List * p_multiassign_exprs
Definition: parse_node.h:213
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:237
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:235
bool p_hasSubLinks
Definition: parse_node.h:226
Node * p_last_srf
Definition: parse_node.h:229
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:236
CmdType commandType
Definition: parsenodes.h:121
List * targetList
Definition: parsenodes.h:191
Node * val
Definition: parsenodes.h:519
ParseLoc location
Definition: parsenodes.h:520
List * indirection
Definition: parsenodes.h:518
char * name
Definition: parsenodes.h:517
RowCompareType rctype
Definition: primnodes.h:1438
List * args
Definition: primnodes.h:1381
ParseLoc location
Definition: primnodes.h:1405
SQLValueFunctionOp op
Definition: primnodes.h:1526
Definition: value.h:64
Expr * expr
Definition: primnodes.h:2162
AttrNumber resno
Definition: primnodes.h:2164
TypeName * typeName
Definition: parsenodes.h:374
ParseLoc location
Definition: parsenodes.h:375
Node * arg
Definition: parsenodes.h:373
ParseLoc location
Definition: parsenodes.h:275
Definition: primnodes.h:248
ParseLoc location
Definition: primnodes.h:293
List * args
Definition: primnodes.h:575
Expr * aggfilter
Definition: primnodes.h:577
ParseLoc location
Definition: primnodes.h:585
Oid winfnoid
Definition: primnodes.h:567
List * args
Definition: primnodes.h:1578
ParseLoc location
Definition: primnodes.h:1587
bool indent
Definition: primnodes.h:1582
List * named_args
Definition: primnodes.h:1574
XmlExprOp op
Definition: primnodes.h:1570
ParseLoc location
Definition: parsenodes.h:849
TypeName * typeName
Definition: parsenodes.h:847
Node * expr
Definition: parsenodes.h:846
XmlOptionType xmloption
Definition: parsenodes.h:845
Definition: ltree.h:43
Definition: c.h:741
static void * fn(void *arg)
Definition: thread-alloc.c:119
int count_nonjunk_tlist_entries(List *tlist)
Definition: tlist.c:186
String * makeString(char *str)
Definition: value.c:63
#define strVal(v)
Definition: value.h:82
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:441
bool contain_var_clause(Node *node)
Definition: var.c:403
const char * type
#define select(n, r, w, e, timeout)
Definition: win32_port.h:495
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2315