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-2021, 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_type.h"
19 #include "commands/dbcommands.h"
20 #include "miscadmin.h"
21 #include "nodes/makefuncs.h"
22 #include "nodes/nodeFuncs.h"
23 #include "optimizer/optimizer.h"
24 #include "parser/analyze.h"
25 #include "parser/parse_agg.h"
26 #include "parser/parse_clause.h"
27 #include "parser/parse_coerce.h"
28 #include "parser/parse_collate.h"
29 #include "parser/parse_expr.h"
30 #include "parser/parse_func.h"
31 #include "parser/parse_oper.h"
32 #include "parser/parse_relation.h"
33 #include "parser/parse_target.h"
34 #include "parser/parse_type.h"
35 #include "utils/builtins.h"
36 #include "utils/date.h"
37 #include "utils/lsyscache.h"
38 #include "utils/timestamp.h"
39 #include "utils/xml.h"
40 
41 /* GUC parameters */
42 bool Transform_null_equals = false;
43 
44 
45 static Node *transformExprRecurse(ParseState *pstate, Node *expr);
46 static Node *transformParamRef(ParseState *pstate, ParamRef *pref);
47 static Node *transformAExprOp(ParseState *pstate, A_Expr *a);
48 static Node *transformAExprOpAny(ParseState *pstate, A_Expr *a);
49 static Node *transformAExprOpAll(ParseState *pstate, A_Expr *a);
50 static Node *transformAExprDistinct(ParseState *pstate, A_Expr *a);
51 static Node *transformAExprNullIf(ParseState *pstate, A_Expr *a);
52 static Node *transformAExprIn(ParseState *pstate, A_Expr *a);
53 static Node *transformAExprBetween(ParseState *pstate, A_Expr *a);
54 static Node *transformBoolExpr(ParseState *pstate, BoolExpr *a);
55 static Node *transformFuncCall(ParseState *pstate, FuncCall *fn);
56 static Node *transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref);
57 static Node *transformCaseExpr(ParseState *pstate, CaseExpr *c);
58 static Node *transformSubLink(ParseState *pstate, SubLink *sublink);
59 static Node *transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
60  Oid array_type, Oid element_type, int32 typmod);
61 static Node *transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault);
63 static Node *transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m);
65  SQLValueFunction *svf);
66 static Node *transformXmlExpr(ParseState *pstate, XmlExpr *x);
68 static Node *transformBooleanTest(ParseState *pstate, BooleanTest *b);
69 static Node *transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr);
70 static Node *transformColumnRef(ParseState *pstate, ColumnRef *cref);
71 static Node *transformWholeRowRef(ParseState *pstate,
72  ParseNamespaceItem *nsitem,
73  int sublevels_up, int location);
75 static Node *transformTypeCast(ParseState *pstate, TypeCast *tc);
77 static Node *make_row_comparison_op(ParseState *pstate, List *opname,
78  List *largs, List *rargs, int location);
79 static Node *make_row_distinct_op(ParseState *pstate, List *opname,
80  RowExpr *lrow, RowExpr *rrow, int location);
81 static Expr *make_distinct_op(ParseState *pstate, List *opname,
82  Node *ltree, Node *rtree, int location);
84  A_Expr *distincta, Node *arg);
85 
86 
87 /*
88  * transformExpr -
89  * Analyze and transform expressions. Type checking and type casting is
90  * done here. This processing converts the raw grammar output into
91  * expression trees with fully determined semantics.
92  */
93 Node *
94 transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
95 {
96  Node *result;
97  ParseExprKind sv_expr_kind;
98 
99  /* Save and restore identity of expression type we're parsing */
100  Assert(exprKind != EXPR_KIND_NONE);
101  sv_expr_kind = pstate->p_expr_kind;
102  pstate->p_expr_kind = exprKind;
103 
104  result = transformExprRecurse(pstate, expr);
105 
106  pstate->p_expr_kind = sv_expr_kind;
107 
108  return result;
109 }
110 
111 static Node *
113 {
114  Node *result;
115 
116  if (expr == NULL)
117  return NULL;
118 
119  /* Guard against stack overflow due to overly complex expressions */
121 
122  switch (nodeTag(expr))
123  {
124  case T_ColumnRef:
125  result = transformColumnRef(pstate, (ColumnRef *) expr);
126  break;
127 
128  case T_ParamRef:
129  result = transformParamRef(pstate, (ParamRef *) expr);
130  break;
131 
132  case T_A_Const:
133  result = (Node *) make_const(pstate, (A_Const *) expr);
134  break;
135 
136  case T_A_Indirection:
137  result = transformIndirection(pstate, (A_Indirection *) expr);
138  break;
139 
140  case T_A_ArrayExpr:
141  result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
142  InvalidOid, InvalidOid, -1);
143  break;
144 
145  case T_TypeCast:
146  result = transformTypeCast(pstate, (TypeCast *) expr);
147  break;
148 
149  case T_CollateClause:
150  result = transformCollateClause(pstate, (CollateClause *) expr);
151  break;
152 
153  case T_A_Expr:
154  {
155  A_Expr *a = (A_Expr *) expr;
156 
157  switch (a->kind)
158  {
159  case AEXPR_OP:
160  result = transformAExprOp(pstate, a);
161  break;
162  case AEXPR_OP_ANY:
163  result = transformAExprOpAny(pstate, a);
164  break;
165  case AEXPR_OP_ALL:
166  result = transformAExprOpAll(pstate, a);
167  break;
168  case AEXPR_DISTINCT:
169  case AEXPR_NOT_DISTINCT:
170  result = transformAExprDistinct(pstate, a);
171  break;
172  case AEXPR_NULLIF:
173  result = transformAExprNullIf(pstate, a);
174  break;
175  case AEXPR_IN:
176  result = transformAExprIn(pstate, a);
177  break;
178  case AEXPR_LIKE:
179  case AEXPR_ILIKE:
180  case AEXPR_SIMILAR:
181  /* we can transform these just like AEXPR_OP */
182  result = transformAExprOp(pstate, a);
183  break;
184  case AEXPR_BETWEEN:
185  case AEXPR_NOT_BETWEEN:
186  case AEXPR_BETWEEN_SYM:
188  result = transformAExprBetween(pstate, a);
189  break;
190  default:
191  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
192  result = NULL; /* keep compiler quiet */
193  break;
194  }
195  break;
196  }
197 
198  case T_BoolExpr:
199  result = transformBoolExpr(pstate, (BoolExpr *) expr);
200  break;
201 
202  case T_FuncCall:
203  result = transformFuncCall(pstate, (FuncCall *) expr);
204  break;
205 
206  case T_MultiAssignRef:
207  result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
208  break;
209 
210  case T_GroupingFunc:
211  result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
212  break;
213 
214  case T_NamedArgExpr:
215  {
216  NamedArgExpr *na = (NamedArgExpr *) expr;
217 
218  na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
219  result = expr;
220  break;
221  }
222 
223  case T_SubLink:
224  result = transformSubLink(pstate, (SubLink *) expr);
225  break;
226 
227  case T_CaseExpr:
228  result = transformCaseExpr(pstate, (CaseExpr *) expr);
229  break;
230 
231  case T_RowExpr:
232  result = transformRowExpr(pstate, (RowExpr *) expr, false);
233  break;
234 
235  case T_CoalesceExpr:
236  result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
237  break;
238 
239  case T_MinMaxExpr:
240  result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
241  break;
242 
243  case T_SQLValueFunction:
244  result = transformSQLValueFunction(pstate,
245  (SQLValueFunction *) expr);
246  break;
247 
248  case T_XmlExpr:
249  result = transformXmlExpr(pstate, (XmlExpr *) expr);
250  break;
251 
252  case T_XmlSerialize:
253  result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
254  break;
255 
256  case T_NullTest:
257  {
258  NullTest *n = (NullTest *) expr;
259 
260  n->arg = (Expr *) transformExprRecurse(pstate, (Node *) n->arg);
261  /* the argument can be any type, so don't coerce it */
262  n->argisrow = type_is_rowtype(exprType((Node *) n->arg));
263  result = expr;
264  break;
265  }
266 
267  case T_BooleanTest:
268  result = transformBooleanTest(pstate, (BooleanTest *) expr);
269  break;
270 
271  case T_CurrentOfExpr:
272  result = transformCurrentOfExpr(pstate, (CurrentOfExpr *) expr);
273  break;
274 
275  /*
276  * In all places where DEFAULT is legal, the caller should have
277  * processed it rather than passing it to transformExpr().
278  */
279  case T_SetToDefault:
280  ereport(ERROR,
281  (errcode(ERRCODE_SYNTAX_ERROR),
282  errmsg("DEFAULT is not allowed in this context"),
283  parser_errposition(pstate,
284  ((SetToDefault *) expr)->location)));
285  break;
286 
287  /*
288  * CaseTestExpr doesn't require any processing; it is only
289  * injected into parse trees in a fully-formed state.
290  *
291  * Ordinarily we should not see a Var here, but it is convenient
292  * for transformJoinUsingClause() to create untransformed operator
293  * trees containing already-transformed Vars. The best
294  * alternative would be to deconstruct and reconstruct column
295  * references, which seems expensively pointless. So allow it.
296  */
297  case T_CaseTestExpr:
298  case T_Var:
299  {
300  result = (Node *) expr;
301  break;
302  }
303 
304  default:
305  /* should not reach here */
306  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
307  result = NULL; /* keep compiler quiet */
308  break;
309  }
310 
311  return result;
312 }
313 
314 /*
315  * helper routine for delivering "column does not exist" error message
316  *
317  * (Usually we don't have to work this hard, but the general case of field
318  * selection from an arbitrary node needs it.)
319  */
320 static void
321 unknown_attribute(ParseState *pstate, Node *relref, const char *attname,
322  int location)
323 {
324  RangeTblEntry *rte;
325 
326  if (IsA(relref, Var) &&
327  ((Var *) relref)->varattno == InvalidAttrNumber)
328  {
329  /* Reference the RTE by alias not by actual table name */
330  rte = GetRTEByRangeTablePosn(pstate,
331  ((Var *) relref)->varno,
332  ((Var *) relref)->varlevelsup);
333  ereport(ERROR,
334  (errcode(ERRCODE_UNDEFINED_COLUMN),
335  errmsg("column %s.%s does not exist",
336  rte->eref->aliasname, attname),
337  parser_errposition(pstate, location)));
338  }
339  else
340  {
341  /* Have to do it by reference to the type of the expression */
342  Oid relTypeId = exprType(relref);
343 
344  if (ISCOMPLEX(relTypeId))
345  ereport(ERROR,
346  (errcode(ERRCODE_UNDEFINED_COLUMN),
347  errmsg("column \"%s\" not found in data type %s",
348  attname, format_type_be(relTypeId)),
349  parser_errposition(pstate, location)));
350  else if (relTypeId == RECORDOID)
351  ereport(ERROR,
352  (errcode(ERRCODE_UNDEFINED_COLUMN),
353  errmsg("could not identify column \"%s\" in record data type",
354  attname),
355  parser_errposition(pstate, location)));
356  else
357  ereport(ERROR,
358  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
359  errmsg("column notation .%s applied to type %s, "
360  "which is not a composite type",
361  attname, format_type_be(relTypeId)),
362  parser_errposition(pstate, location)));
363  }
364 }
365 
366 static Node *
368 {
369  Node *last_srf = pstate->p_last_srf;
370  Node *result = transformExprRecurse(pstate, ind->arg);
371  List *subscripts = NIL;
372  int location = exprLocation(result);
373  ListCell *i;
374 
375  /*
376  * We have to split any field-selection operations apart from
377  * subscripting. Adjacent A_Indices nodes have to be treated as a single
378  * multidimensional subscript operation.
379  */
380  foreach(i, ind->indirection)
381  {
382  Node *n = lfirst(i);
383 
384  if (IsA(n, A_Indices))
385  subscripts = lappend(subscripts, n);
386  else if (IsA(n, A_Star))
387  {
388  ereport(ERROR,
389  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
390  errmsg("row expansion via \"*\" is not supported here"),
391  parser_errposition(pstate, location)));
392  }
393  else
394  {
395  Node *newresult;
396 
397  Assert(IsA(n, String));
398 
399  /* process subscripts before this field selection */
400  if (subscripts)
401  result = (Node *) transformContainerSubscripts(pstate,
402  result,
403  exprType(result),
404  exprTypmod(result),
405  subscripts,
406  false);
407  subscripts = NIL;
408 
409  newresult = ParseFuncOrColumn(pstate,
410  list_make1(n),
411  list_make1(result),
412  last_srf,
413  NULL,
414  false,
415  location);
416  if (newresult == NULL)
417  unknown_attribute(pstate, result, strVal(n), location);
418  result = newresult;
419  }
420  }
421  /* process trailing subscripts, if any */
422  if (subscripts)
423  result = (Node *) transformContainerSubscripts(pstate,
424  result,
425  exprType(result),
426  exprTypmod(result),
427  subscripts,
428  false);
429 
430  return result;
431 }
432 
433 /*
434  * Transform a ColumnRef.
435  *
436  * If you find yourself changing this code, see also ExpandColumnRefStar.
437  */
438 static Node *
440 {
441  Node *node = NULL;
442  char *nspname = NULL;
443  char *relname = NULL;
444  char *colname = NULL;
445  ParseNamespaceItem *nsitem;
446  int levels_up;
447  enum
448  {
449  CRERR_NO_COLUMN,
450  CRERR_NO_RTE,
451  CRERR_WRONG_DB,
452  CRERR_TOO_MANY
453  } crerr = CRERR_NO_COLUMN;
454  const char *err;
455 
456  /*
457  * Check to see if the column reference is in an invalid place within the
458  * query. We allow column references in most places, except in default
459  * expressions and partition bound expressions.
460  */
461  err = NULL;
462  switch (pstate->p_expr_kind)
463  {
464  case EXPR_KIND_NONE:
465  Assert(false); /* can't happen */
466  break;
467  case EXPR_KIND_OTHER:
468  case EXPR_KIND_JOIN_ON:
472  case EXPR_KIND_WHERE:
473  case EXPR_KIND_POLICY:
474  case EXPR_KIND_HAVING:
475  case EXPR_KIND_FILTER:
485  case EXPR_KIND_GROUP_BY:
486  case EXPR_KIND_ORDER_BY:
488  case EXPR_KIND_LIMIT:
489  case EXPR_KIND_OFFSET:
490  case EXPR_KIND_RETURNING:
491  case EXPR_KIND_VALUES:
507  /* okay */
508  break;
509 
511  err = _("cannot use column reference in DEFAULT expression");
512  break;
514  err = _("cannot use column reference in partition bound expression");
515  break;
516 
517  /*
518  * There is intentionally no default: case here, so that the
519  * compiler will warn if we add a new ParseExprKind without
520  * extending this switch. If we do see an unrecognized value at
521  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
522  * which is sane anyway.
523  */
524  }
525  if (err)
526  ereport(ERROR,
527  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
528  errmsg_internal("%s", err),
529  parser_errposition(pstate, cref->location)));
530 
531  /*
532  * Give the PreParseColumnRefHook, if any, first shot. If it returns
533  * non-null then that's all, folks.
534  */
535  if (pstate->p_pre_columnref_hook != NULL)
536  {
537  node = pstate->p_pre_columnref_hook(pstate, cref);
538  if (node != NULL)
539  return node;
540  }
541 
542  /*----------
543  * The allowed syntaxes are:
544  *
545  * A First try to resolve as unqualified column name;
546  * if no luck, try to resolve as unqualified table name (A.*).
547  * A.B A is an unqualified table name; B is either a
548  * column or function name (trying column name first).
549  * A.B.C schema A, table B, col or func name C.
550  * A.B.C.D catalog A, schema B, table C, col or func D.
551  * A.* A is an unqualified table name; means whole-row value.
552  * A.B.* whole-row value of table B in schema A.
553  * A.B.C.* whole-row value of table C in schema B in catalog A.
554  *
555  * We do not need to cope with bare "*"; that will only be accepted by
556  * the grammar at the top level of a SELECT list, and transformTargetList
557  * will take care of it before it ever gets here. Also, "A.*" etc will
558  * be expanded by transformTargetList if they appear at SELECT top level,
559  * so here we are only going to see them as function or operator inputs.
560  *
561  * Currently, if a catalog name is given then it must equal the current
562  * database name; we check it here and then discard it.
563  *----------
564  */
565  switch (list_length(cref->fields))
566  {
567  case 1:
568  {
569  Node *field1 = (Node *) linitial(cref->fields);
570 
571  Assert(IsA(field1, String));
572  colname = strVal(field1);
573 
574  /* Try to identify as an unqualified column */
575  node = colNameToVar(pstate, colname, false, cref->location);
576 
577  if (node == NULL)
578  {
579  /*
580  * Not known as a column of any range-table entry.
581  *
582  * Try to find the name as a relation. Note that only
583  * relations already entered into the rangetable will be
584  * recognized.
585  *
586  * This is a hack for backwards compatibility with
587  * PostQUEL-inspired syntax. The preferred form now is
588  * "rel.*".
589  */
590  nsitem = refnameNamespaceItem(pstate, NULL, colname,
591  cref->location,
592  &levels_up);
593  if (nsitem)
594  node = transformWholeRowRef(pstate, nsitem, levels_up,
595  cref->location);
596  }
597  break;
598  }
599  case 2:
600  {
601  Node *field1 = (Node *) linitial(cref->fields);
602  Node *field2 = (Node *) lsecond(cref->fields);
603 
604  Assert(IsA(field1, String));
605  relname = strVal(field1);
606 
607  /* Locate the referenced nsitem */
608  nsitem = refnameNamespaceItem(pstate, nspname, relname,
609  cref->location,
610  &levels_up);
611  if (nsitem == NULL)
612  {
613  crerr = CRERR_NO_RTE;
614  break;
615  }
616 
617  /* Whole-row reference? */
618  if (IsA(field2, A_Star))
619  {
620  node = transformWholeRowRef(pstate, nsitem, levels_up,
621  cref->location);
622  break;
623  }
624 
625  Assert(IsA(field2, String));
626  colname = strVal(field2);
627 
628  /* Try to identify as a column of the nsitem */
629  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
630  cref->location);
631  if (node == NULL)
632  {
633  /* Try it as a function call on the whole row */
634  node = transformWholeRowRef(pstate, nsitem, levels_up,
635  cref->location);
636  node = ParseFuncOrColumn(pstate,
637  list_make1(makeString(colname)),
638  list_make1(node),
639  pstate->p_last_srf,
640  NULL,
641  false,
642  cref->location);
643  }
644  break;
645  }
646  case 3:
647  {
648  Node *field1 = (Node *) linitial(cref->fields);
649  Node *field2 = (Node *) lsecond(cref->fields);
650  Node *field3 = (Node *) lthird(cref->fields);
651 
652  Assert(IsA(field1, String));
653  nspname = strVal(field1);
654  Assert(IsA(field2, String));
655  relname = strVal(field2);
656 
657  /* Locate the referenced nsitem */
658  nsitem = refnameNamespaceItem(pstate, nspname, relname,
659  cref->location,
660  &levels_up);
661  if (nsitem == NULL)
662  {
663  crerr = CRERR_NO_RTE;
664  break;
665  }
666 
667  /* Whole-row reference? */
668  if (IsA(field3, A_Star))
669  {
670  node = transformWholeRowRef(pstate, nsitem, levels_up,
671  cref->location);
672  break;
673  }
674 
675  Assert(IsA(field3, String));
676  colname = strVal(field3);
677 
678  /* Try to identify as a column of the nsitem */
679  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
680  cref->location);
681  if (node == NULL)
682  {
683  /* Try it as a function call on the whole row */
684  node = transformWholeRowRef(pstate, nsitem, levels_up,
685  cref->location);
686  node = ParseFuncOrColumn(pstate,
687  list_make1(makeString(colname)),
688  list_make1(node),
689  pstate->p_last_srf,
690  NULL,
691  false,
692  cref->location);
693  }
694  break;
695  }
696  case 4:
697  {
698  Node *field1 = (Node *) linitial(cref->fields);
699  Node *field2 = (Node *) lsecond(cref->fields);
700  Node *field3 = (Node *) lthird(cref->fields);
701  Node *field4 = (Node *) lfourth(cref->fields);
702  char *catname;
703 
704  Assert(IsA(field1, String));
705  catname = strVal(field1);
706  Assert(IsA(field2, String));
707  nspname = strVal(field2);
708  Assert(IsA(field3, String));
709  relname = strVal(field3);
710 
711  /*
712  * We check the catalog name and then ignore it.
713  */
714  if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
715  {
716  crerr = CRERR_WRONG_DB;
717  break;
718  }
719 
720  /* Locate the referenced nsitem */
721  nsitem = refnameNamespaceItem(pstate, nspname, relname,
722  cref->location,
723  &levels_up);
724  if (nsitem == NULL)
725  {
726  crerr = CRERR_NO_RTE;
727  break;
728  }
729 
730  /* Whole-row reference? */
731  if (IsA(field4, A_Star))
732  {
733  node = transformWholeRowRef(pstate, nsitem, levels_up,
734  cref->location);
735  break;
736  }
737 
738  Assert(IsA(field4, String));
739  colname = strVal(field4);
740 
741  /* Try to identify as a column of the nsitem */
742  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
743  cref->location);
744  if (node == NULL)
745  {
746  /* Try it as a function call on the whole row */
747  node = transformWholeRowRef(pstate, nsitem, levels_up,
748  cref->location);
749  node = ParseFuncOrColumn(pstate,
750  list_make1(makeString(colname)),
751  list_make1(node),
752  pstate->p_last_srf,
753  NULL,
754  false,
755  cref->location);
756  }
757  break;
758  }
759  default:
760  crerr = CRERR_TOO_MANY; /* too many dotted names */
761  break;
762  }
763 
764  /*
765  * Now give the PostParseColumnRefHook, if any, a chance. We pass the
766  * translation-so-far so that it can throw an error if it wishes in the
767  * case that it has a conflicting interpretation of the ColumnRef. (If it
768  * just translates anyway, we'll throw an error, because we can't undo
769  * whatever effects the preceding steps may have had on the pstate.) If it
770  * returns NULL, use the standard translation, or throw a suitable error
771  * if there is none.
772  */
773  if (pstate->p_post_columnref_hook != NULL)
774  {
775  Node *hookresult;
776 
777  hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
778  if (node == NULL)
779  node = hookresult;
780  else if (hookresult != NULL)
781  ereport(ERROR,
782  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
783  errmsg("column reference \"%s\" is ambiguous",
784  NameListToString(cref->fields)),
785  parser_errposition(pstate, cref->location)));
786  }
787 
788  /*
789  * Throw error if no translation found.
790  */
791  if (node == NULL)
792  {
793  switch (crerr)
794  {
795  case CRERR_NO_COLUMN:
796  errorMissingColumn(pstate, relname, colname, cref->location);
797  break;
798  case CRERR_NO_RTE:
799  errorMissingRTE(pstate, makeRangeVar(nspname, relname,
800  cref->location));
801  break;
802  case CRERR_WRONG_DB:
803  ereport(ERROR,
804  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
805  errmsg("cross-database references are not implemented: %s",
806  NameListToString(cref->fields)),
807  parser_errposition(pstate, cref->location)));
808  break;
809  case CRERR_TOO_MANY:
810  ereport(ERROR,
811  (errcode(ERRCODE_SYNTAX_ERROR),
812  errmsg("improper qualified name (too many dotted names): %s",
813  NameListToString(cref->fields)),
814  parser_errposition(pstate, cref->location)));
815  break;
816  }
817  }
818 
819  return node;
820 }
821 
822 static Node *
824 {
825  Node *result;
826 
827  /*
828  * The core parser knows nothing about Params. If a hook is supplied,
829  * call it. If not, or if the hook returns NULL, throw a generic error.
830  */
831  if (pstate->p_paramref_hook != NULL)
832  result = pstate->p_paramref_hook(pstate, pref);
833  else
834  result = NULL;
835 
836  if (result == NULL)
837  ereport(ERROR,
838  (errcode(ERRCODE_UNDEFINED_PARAMETER),
839  errmsg("there is no parameter $%d", pref->number),
840  parser_errposition(pstate, pref->location)));
841 
842  return result;
843 }
844 
845 /* Test whether an a_expr is a plain NULL constant or not */
846 static bool
848 {
849  if (arg && IsA(arg, A_Const))
850  {
851  A_Const *con = (A_Const *) arg;
852 
853  if (con->isnull)
854  return true;
855  }
856  return false;
857 }
858 
859 static Node *
861 {
862  Node *lexpr = a->lexpr;
863  Node *rexpr = a->rexpr;
864  Node *result;
865 
866  /*
867  * Special-case "foo = NULL" and "NULL = foo" for compatibility with
868  * standards-broken products (like Microsoft's). Turn these into IS NULL
869  * exprs. (If either side is a CaseTestExpr, then the expression was
870  * generated internally from a CASE-WHEN expression, and
871  * transform_null_equals does not apply.)
872  */
873  if (Transform_null_equals &&
874  list_length(a->name) == 1 &&
875  strcmp(strVal(linitial(a->name)), "=") == 0 &&
876  (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
877  (!IsA(lexpr, CaseTestExpr) && !IsA(rexpr, CaseTestExpr)))
878  {
879  NullTest *n = makeNode(NullTest);
880 
881  n->nulltesttype = IS_NULL;
882  n->location = a->location;
883 
884  if (exprIsNullConstant(lexpr))
885  n->arg = (Expr *) rexpr;
886  else
887  n->arg = (Expr *) lexpr;
888 
889  result = transformExprRecurse(pstate, (Node *) n);
890  }
891  else if (lexpr && IsA(lexpr, RowExpr) &&
892  rexpr && IsA(rexpr, SubLink) &&
893  ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
894  {
895  /*
896  * Convert "row op subselect" into a ROWCOMPARE sublink. Formerly the
897  * grammar did this, but now that a row construct is allowed anywhere
898  * in expressions, it's easier to do it here.
899  */
900  SubLink *s = (SubLink *) rexpr;
901 
903  s->testexpr = lexpr;
904  s->operName = a->name;
905  s->location = a->location;
906  result = transformExprRecurse(pstate, (Node *) s);
907  }
908  else if (lexpr && IsA(lexpr, RowExpr) &&
909  rexpr && IsA(rexpr, RowExpr))
910  {
911  /* ROW() op ROW() is handled specially */
912  lexpr = transformExprRecurse(pstate, lexpr);
913  rexpr = transformExprRecurse(pstate, rexpr);
914 
915  result = make_row_comparison_op(pstate,
916  a->name,
917  castNode(RowExpr, lexpr)->args,
918  castNode(RowExpr, rexpr)->args,
919  a->location);
920  }
921  else
922  {
923  /* Ordinary scalar operator */
924  Node *last_srf = pstate->p_last_srf;
925 
926  lexpr = transformExprRecurse(pstate, lexpr);
927  rexpr = transformExprRecurse(pstate, rexpr);
928 
929  result = (Node *) make_op(pstate,
930  a->name,
931  lexpr,
932  rexpr,
933  last_srf,
934  a->location);
935  }
936 
937  return result;
938 }
939 
940 static Node *
942 {
943  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
944  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
945 
946  return (Node *) make_scalar_array_op(pstate,
947  a->name,
948  true,
949  lexpr,
950  rexpr,
951  a->location);
952 }
953 
954 static Node *
956 {
957  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
958  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
959 
960  return (Node *) make_scalar_array_op(pstate,
961  a->name,
962  false,
963  lexpr,
964  rexpr,
965  a->location);
966 }
967 
968 static Node *
970 {
971  Node *lexpr = a->lexpr;
972  Node *rexpr = a->rexpr;
973  Node *result;
974 
975  /*
976  * If either input is an undecorated NULL literal, transform to a NullTest
977  * on the other input. That's simpler to process than a full DistinctExpr,
978  * and it avoids needing to require that the datatype have an = operator.
979  */
980  if (exprIsNullConstant(rexpr))
981  return make_nulltest_from_distinct(pstate, a, lexpr);
982  if (exprIsNullConstant(lexpr))
983  return make_nulltest_from_distinct(pstate, a, rexpr);
984 
985  lexpr = transformExprRecurse(pstate, lexpr);
986  rexpr = transformExprRecurse(pstate, rexpr);
987 
988  if (lexpr && IsA(lexpr, RowExpr) &&
989  rexpr && IsA(rexpr, RowExpr))
990  {
991  /* ROW() op ROW() is handled specially */
992  result = make_row_distinct_op(pstate, a->name,
993  (RowExpr *) lexpr,
994  (RowExpr *) rexpr,
995  a->location);
996  }
997  else
998  {
999  /* Ordinary scalar operator */
1000  result = (Node *) make_distinct_op(pstate,
1001  a->name,
1002  lexpr,
1003  rexpr,
1004  a->location);
1005  }
1006 
1007  /*
1008  * If it's NOT DISTINCT, we first build a DistinctExpr and then stick a
1009  * NOT on top.
1010  */
1011  if (a->kind == AEXPR_NOT_DISTINCT)
1012  result = (Node *) makeBoolExpr(NOT_EXPR,
1013  list_make1(result),
1014  a->location);
1015 
1016  return result;
1017 }
1018 
1019 static Node *
1021 {
1022  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1023  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1024  OpExpr *result;
1025 
1026  result = (OpExpr *) make_op(pstate,
1027  a->name,
1028  lexpr,
1029  rexpr,
1030  pstate->p_last_srf,
1031  a->location);
1032 
1033  /*
1034  * The comparison operator itself should yield boolean ...
1035  */
1036  if (result->opresulttype != BOOLOID)
1037  ereport(ERROR,
1038  (errcode(ERRCODE_DATATYPE_MISMATCH),
1039  errmsg("NULLIF requires = operator to yield boolean"),
1040  parser_errposition(pstate, a->location)));
1041  if (result->opretset)
1042  ereport(ERROR,
1043  (errcode(ERRCODE_DATATYPE_MISMATCH),
1044  /* translator: %s is name of a SQL construct, eg NULLIF */
1045  errmsg("%s must not return a set", "NULLIF"),
1046  parser_errposition(pstate, a->location)));
1047 
1048  /*
1049  * ... but the NullIfExpr will yield the first operand's type.
1050  */
1051  result->opresulttype = exprType((Node *) linitial(result->args));
1052 
1053  /*
1054  * We rely on NullIfExpr and OpExpr being the same struct
1055  */
1056  NodeSetTag(result, T_NullIfExpr);
1057 
1058  return (Node *) result;
1059 }
1060 
1061 static Node *
1063 {
1064  Node *result = NULL;
1065  Node *lexpr;
1066  List *rexprs;
1067  List *rvars;
1068  List *rnonvars;
1069  bool useOr;
1070  ListCell *l;
1071 
1072  /*
1073  * If the operator is <>, combine with AND not OR.
1074  */
1075  if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1076  useOr = false;
1077  else
1078  useOr = true;
1079 
1080  /*
1081  * We try to generate a ScalarArrayOpExpr from IN/NOT IN, but this is only
1082  * possible if there is a suitable array type available. If not, we fall
1083  * back to a boolean condition tree with multiple copies of the lefthand
1084  * expression. Also, any IN-list items that contain Vars are handled as
1085  * separate boolean conditions, because that gives the planner more scope
1086  * for optimization on such clauses.
1087  *
1088  * First step: transform all the inputs, and detect whether any contain
1089  * Vars.
1090  */
1091  lexpr = transformExprRecurse(pstate, a->lexpr);
1092  rexprs = rvars = rnonvars = NIL;
1093  foreach(l, (List *) a->rexpr)
1094  {
1095  Node *rexpr = transformExprRecurse(pstate, lfirst(l));
1096 
1097  rexprs = lappend(rexprs, rexpr);
1098  if (contain_vars_of_level(rexpr, 0))
1099  rvars = lappend(rvars, rexpr);
1100  else
1101  rnonvars = lappend(rnonvars, rexpr);
1102  }
1103 
1104  /*
1105  * ScalarArrayOpExpr is only going to be useful if there's more than one
1106  * non-Var righthand item.
1107  */
1108  if (list_length(rnonvars) > 1)
1109  {
1110  List *allexprs;
1111  Oid scalar_type;
1112  Oid array_type;
1113 
1114  /*
1115  * Try to select a common type for the array elements. Note that
1116  * since the LHS' type is first in the list, it will be preferred when
1117  * there is doubt (eg, when all the RHS items are unknown literals).
1118  *
1119  * Note: use list_concat here not lcons, to avoid damaging rnonvars.
1120  */
1121  allexprs = list_concat(list_make1(lexpr), rnonvars);
1122  scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1123 
1124  /*
1125  * Do we have an array type to use? Aside from the case where there
1126  * isn't one, we don't risk using ScalarArrayOpExpr when the common
1127  * type is RECORD, because the RowExpr comparison logic below can cope
1128  * with some cases of non-identical row types.
1129  */
1130  if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
1131  array_type = get_array_type(scalar_type);
1132  else
1133  array_type = InvalidOid;
1134  if (array_type != InvalidOid)
1135  {
1136  /*
1137  * OK: coerce all the right-hand non-Var inputs to the common type
1138  * and build an ArrayExpr for them.
1139  */
1140  List *aexprs;
1141  ArrayExpr *newa;
1142 
1143  aexprs = NIL;
1144  foreach(l, rnonvars)
1145  {
1146  Node *rexpr = (Node *) lfirst(l);
1147 
1148  rexpr = coerce_to_common_type(pstate, rexpr,
1149  scalar_type,
1150  "IN");
1151  aexprs = lappend(aexprs, rexpr);
1152  }
1153  newa = makeNode(ArrayExpr);
1154  newa->array_typeid = array_type;
1155  /* array_collid will be set by parse_collate.c */
1156  newa->element_typeid = scalar_type;
1157  newa->elements = aexprs;
1158  newa->multidims = false;
1159  newa->location = -1;
1160 
1161  result = (Node *) make_scalar_array_op(pstate,
1162  a->name,
1163  useOr,
1164  lexpr,
1165  (Node *) newa,
1166  a->location);
1167 
1168  /* Consider only the Vars (if any) in the loop below */
1169  rexprs = rvars;
1170  }
1171  }
1172 
1173  /*
1174  * Must do it the hard way, ie, with a boolean expression tree.
1175  */
1176  foreach(l, rexprs)
1177  {
1178  Node *rexpr = (Node *) lfirst(l);
1179  Node *cmp;
1180 
1181  if (IsA(lexpr, RowExpr) &&
1182  IsA(rexpr, RowExpr))
1183  {
1184  /* ROW() op ROW() is handled specially */
1185  cmp = make_row_comparison_op(pstate,
1186  a->name,
1187  copyObject(((RowExpr *) lexpr)->args),
1188  ((RowExpr *) rexpr)->args,
1189  a->location);
1190  }
1191  else
1192  {
1193  /* Ordinary scalar operator */
1194  cmp = (Node *) make_op(pstate,
1195  a->name,
1196  copyObject(lexpr),
1197  rexpr,
1198  pstate->p_last_srf,
1199  a->location);
1200  }
1201 
1202  cmp = coerce_to_boolean(pstate, cmp, "IN");
1203  if (result == NULL)
1204  result = cmp;
1205  else
1206  result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
1207  list_make2(result, cmp),
1208  a->location);
1209  }
1210 
1211  return result;
1212 }
1213 
1214 static Node *
1216 {
1217  Node *aexpr;
1218  Node *bexpr;
1219  Node *cexpr;
1220  Node *result;
1221  Node *sub1;
1222  Node *sub2;
1223  List *args;
1224 
1225  /* Deconstruct A_Expr into three subexprs */
1226  aexpr = a->lexpr;
1227  args = castNode(List, a->rexpr);
1228  Assert(list_length(args) == 2);
1229  bexpr = (Node *) linitial(args);
1230  cexpr = (Node *) lsecond(args);
1231 
1232  /*
1233  * Build the equivalent comparison expression. Make copies of
1234  * multiply-referenced subexpressions for safety. (XXX this is really
1235  * wrong since it results in multiple runtime evaluations of what may be
1236  * volatile expressions ...)
1237  *
1238  * Ideally we would not use hard-wired operators here but instead use
1239  * opclasses. However, mixed data types and other issues make this
1240  * difficult:
1241  * http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
1242  */
1243  switch (a->kind)
1244  {
1245  case AEXPR_BETWEEN:
1246  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1247  aexpr, bexpr,
1248  a->location),
1249  makeSimpleA_Expr(AEXPR_OP, "<=",
1250  copyObject(aexpr), cexpr,
1251  a->location));
1252  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1253  break;
1254  case AEXPR_NOT_BETWEEN:
1255  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1256  aexpr, bexpr,
1257  a->location),
1259  copyObject(aexpr), cexpr,
1260  a->location));
1261  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1262  break;
1263  case AEXPR_BETWEEN_SYM:
1264  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1265  aexpr, bexpr,
1266  a->location),
1267  makeSimpleA_Expr(AEXPR_OP, "<=",
1268  copyObject(aexpr), cexpr,
1269  a->location));
1270  sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1271  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1272  copyObject(aexpr), copyObject(cexpr),
1273  a->location),
1274  makeSimpleA_Expr(AEXPR_OP, "<=",
1275  copyObject(aexpr), copyObject(bexpr),
1276  a->location));
1277  sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1278  args = list_make2(sub1, sub2);
1279  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1280  break;
1281  case AEXPR_NOT_BETWEEN_SYM:
1282  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1283  aexpr, bexpr,
1284  a->location),
1286  copyObject(aexpr), cexpr,
1287  a->location));
1288  sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1289  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1290  copyObject(aexpr), copyObject(cexpr),
1291  a->location),
1293  copyObject(aexpr), copyObject(bexpr),
1294  a->location));
1295  sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1296  args = list_make2(sub1, sub2);
1297  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1298  break;
1299  default:
1300  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
1301  result = NULL; /* keep compiler quiet */
1302  break;
1303  }
1304 
1305  return transformExprRecurse(pstate, result);
1306 }
1307 
1308 static Node *
1310 {
1311  List *args = NIL;
1312  const char *opname;
1313  ListCell *lc;
1314 
1315  switch (a->boolop)
1316  {
1317  case AND_EXPR:
1318  opname = "AND";
1319  break;
1320  case OR_EXPR:
1321  opname = "OR";
1322  break;
1323  case NOT_EXPR:
1324  opname = "NOT";
1325  break;
1326  default:
1327  elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
1328  opname = NULL; /* keep compiler quiet */
1329  break;
1330  }
1331 
1332  foreach(lc, a->args)
1333  {
1334  Node *arg = (Node *) lfirst(lc);
1335 
1336  arg = transformExprRecurse(pstate, arg);
1337  arg = coerce_to_boolean(pstate, arg, opname);
1338  args = lappend(args, arg);
1339  }
1340 
1341  return (Node *) makeBoolExpr(a->boolop, args, a->location);
1342 }
1343 
1344 static Node *
1346 {
1347  Node *last_srf = pstate->p_last_srf;
1348  List *targs;
1349  ListCell *args;
1350 
1351  /* Transform the list of arguments ... */
1352  targs = NIL;
1353  foreach(args, fn->args)
1354  {
1355  targs = lappend(targs, transformExprRecurse(pstate,
1356  (Node *) lfirst(args)));
1357  }
1358 
1359  /*
1360  * When WITHIN GROUP is used, we treat its ORDER BY expressions as
1361  * additional arguments to the function, for purposes of function lookup
1362  * and argument type coercion. So, transform each such expression and add
1363  * them to the targs list. We don't explicitly mark where each argument
1364  * came from, but ParseFuncOrColumn can tell what's what by reference to
1365  * list_length(fn->agg_order).
1366  */
1367  if (fn->agg_within_group)
1368  {
1369  Assert(fn->agg_order != NIL);
1370  foreach(args, fn->agg_order)
1371  {
1372  SortBy *arg = (SortBy *) lfirst(args);
1373 
1374  targs = lappend(targs, transformExpr(pstate, arg->node,
1376  }
1377  }
1378 
1379  /* ... and hand off to ParseFuncOrColumn */
1380  return ParseFuncOrColumn(pstate,
1381  fn->funcname,
1382  targs,
1383  last_srf,
1384  fn,
1385  false,
1386  fn->location);
1387 }
1388 
1389 static Node *
1391 {
1392  SubLink *sublink;
1393  RowExpr *rexpr;
1394  Query *qtree;
1395  TargetEntry *tle;
1396 
1397  /* We should only see this in first-stage processing of UPDATE tlists */
1399 
1400  /* We only need to transform the source if this is the first column */
1401  if (maref->colno == 1)
1402  {
1403  /*
1404  * For now, we only allow EXPR SubLinks and RowExprs as the source of
1405  * an UPDATE multiassignment. This is sufficient to cover interesting
1406  * cases; at worst, someone would have to write (SELECT * FROM expr)
1407  * to expand a composite-returning expression of another form.
1408  */
1409  if (IsA(maref->source, SubLink) &&
1410  ((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
1411  {
1412  /* Relabel it as a MULTIEXPR_SUBLINK */
1413  sublink = (SubLink *) maref->source;
1414  sublink->subLinkType = MULTIEXPR_SUBLINK;
1415  /* And transform it */
1416  sublink = (SubLink *) transformExprRecurse(pstate,
1417  (Node *) sublink);
1418 
1419  qtree = castNode(Query, sublink->subselect);
1420 
1421  /* Check subquery returns required number of columns */
1422  if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
1423  ereport(ERROR,
1424  (errcode(ERRCODE_SYNTAX_ERROR),
1425  errmsg("number of columns does not match number of values"),
1426  parser_errposition(pstate, sublink->location)));
1427 
1428  /*
1429  * Build a resjunk tlist item containing the MULTIEXPR SubLink,
1430  * and add it to pstate->p_multiassign_exprs, whence it will later
1431  * get appended to the completed targetlist. We needn't worry
1432  * about selecting a resno for it; transformUpdateStmt will do
1433  * that.
1434  */
1435  tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
1437  tle);
1438 
1439  /*
1440  * Assign a unique-within-this-targetlist ID to the MULTIEXPR
1441  * SubLink. We can just use its position in the
1442  * p_multiassign_exprs list.
1443  */
1444  sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
1445  }
1446  else if (IsA(maref->source, RowExpr))
1447  {
1448  /* Transform the RowExpr, allowing SetToDefault items */
1449  rexpr = (RowExpr *) transformRowExpr(pstate,
1450  (RowExpr *) maref->source,
1451  true);
1452 
1453  /* Check it returns required number of columns */
1454  if (list_length(rexpr->args) != maref->ncolumns)
1455  ereport(ERROR,
1456  (errcode(ERRCODE_SYNTAX_ERROR),
1457  errmsg("number of columns does not match number of values"),
1458  parser_errposition(pstate, rexpr->location)));
1459 
1460  /*
1461  * Temporarily append it to p_multiassign_exprs, so we can get it
1462  * back when we come back here for additional columns.
1463  */
1464  tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
1466  tle);
1467  }
1468  else
1469  ereport(ERROR,
1470  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1471  errmsg("source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression"),
1472  parser_errposition(pstate, exprLocation(maref->source))));
1473  }
1474  else
1475  {
1476  /*
1477  * Second or later column in a multiassignment. Re-fetch the
1478  * transformed SubLink or RowExpr, which we assume is still the last
1479  * entry in p_multiassign_exprs.
1480  */
1481  Assert(pstate->p_multiassign_exprs != NIL);
1482  tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
1483  }
1484 
1485  /*
1486  * Emit the appropriate output expression for the current column
1487  */
1488  if (IsA(tle->expr, SubLink))
1489  {
1490  Param *param;
1491 
1492  sublink = (SubLink *) tle->expr;
1493  Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
1494  qtree = castNode(Query, sublink->subselect);
1495 
1496  /* Build a Param representing the current subquery output column */
1497  tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
1498  Assert(!tle->resjunk);
1499 
1500  param = makeNode(Param);
1501  param->paramkind = PARAM_MULTIEXPR;
1502  param->paramid = (sublink->subLinkId << 16) | maref->colno;
1503  param->paramtype = exprType((Node *) tle->expr);
1504  param->paramtypmod = exprTypmod((Node *) tle->expr);
1505  param->paramcollid = exprCollation((Node *) tle->expr);
1506  param->location = exprLocation((Node *) tle->expr);
1507 
1508  return (Node *) param;
1509  }
1510 
1511  if (IsA(tle->expr, RowExpr))
1512  {
1513  Node *result;
1514 
1515  rexpr = (RowExpr *) tle->expr;
1516 
1517  /* Just extract and return the next element of the RowExpr */
1518  result = (Node *) list_nth(rexpr->args, maref->colno - 1);
1519 
1520  /*
1521  * If we're at the last column, delete the RowExpr from
1522  * p_multiassign_exprs; we don't need it anymore, and don't want it in
1523  * the finished UPDATE tlist. We assume this is still the last entry
1524  * in p_multiassign_exprs.
1525  */
1526  if (maref->colno == maref->ncolumns)
1527  pstate->p_multiassign_exprs =
1529 
1530  return result;
1531  }
1532 
1533  elog(ERROR, "unexpected expr type in multiassign list");
1534  return NULL; /* keep compiler quiet */
1535 }
1536 
1537 static Node *
1539 {
1540  CaseExpr *newc = makeNode(CaseExpr);
1541  Node *last_srf = pstate->p_last_srf;
1542  Node *arg;
1543  CaseTestExpr *placeholder;
1544  List *newargs;
1545  List *resultexprs;
1546  ListCell *l;
1547  Node *defresult;
1548  Oid ptype;
1549 
1550  /* transform the test expression, if any */
1551  arg = transformExprRecurse(pstate, (Node *) c->arg);
1552 
1553  /* generate placeholder for test expression */
1554  if (arg)
1555  {
1556  /*
1557  * If test expression is an untyped literal, force it to text. We have
1558  * to do something now because we won't be able to do this coercion on
1559  * the placeholder. This is not as flexible as what was done in 7.4
1560  * and before, but it's good enough to handle the sort of silly coding
1561  * commonly seen.
1562  */
1563  if (exprType(arg) == UNKNOWNOID)
1564  arg = coerce_to_common_type(pstate, arg, TEXTOID, "CASE");
1565 
1566  /*
1567  * Run collation assignment on the test expression so that we know
1568  * what collation to mark the placeholder with. In principle we could
1569  * leave it to parse_collate.c to do that later, but propagating the
1570  * result to the CaseTestExpr would be unnecessarily complicated.
1571  */
1572  assign_expr_collations(pstate, arg);
1573 
1574  placeholder = makeNode(CaseTestExpr);
1575  placeholder->typeId = exprType(arg);
1576  placeholder->typeMod = exprTypmod(arg);
1577  placeholder->collation = exprCollation(arg);
1578  }
1579  else
1580  placeholder = NULL;
1581 
1582  newc->arg = (Expr *) arg;
1583 
1584  /* transform the list of arguments */
1585  newargs = NIL;
1586  resultexprs = NIL;
1587  foreach(l, c->args)
1588  {
1589  CaseWhen *w = lfirst_node(CaseWhen, l);
1590  CaseWhen *neww = makeNode(CaseWhen);
1591  Node *warg;
1592 
1593  warg = (Node *) w->expr;
1594  if (placeholder)
1595  {
1596  /* shorthand form was specified, so expand... */
1597  warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1598  (Node *) placeholder,
1599  warg,
1600  w->location);
1601  }
1602  neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1603 
1604  neww->expr = (Expr *) coerce_to_boolean(pstate,
1605  (Node *) neww->expr,
1606  "CASE/WHEN");
1607 
1608  warg = (Node *) w->result;
1609  neww->result = (Expr *) transformExprRecurse(pstate, warg);
1610  neww->location = w->location;
1611 
1612  newargs = lappend(newargs, neww);
1613  resultexprs = lappend(resultexprs, neww->result);
1614  }
1615 
1616  newc->args = newargs;
1617 
1618  /* transform the default clause */
1619  defresult = (Node *) c->defresult;
1620  if (defresult == NULL)
1621  {
1622  A_Const *n = makeNode(A_Const);
1623 
1624  n->isnull = true;
1625  n->location = -1;
1626  defresult = (Node *) n;
1627  }
1628  newc->defresult = (Expr *) transformExprRecurse(pstate, defresult);
1629 
1630  /*
1631  * Note: default result is considered the most significant type in
1632  * determining preferred type. This is how the code worked before, but it
1633  * seems a little bogus to me --- tgl
1634  */
1635  resultexprs = lcons(newc->defresult, resultexprs);
1636 
1637  ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
1638  Assert(OidIsValid(ptype));
1639  newc->casetype = ptype;
1640  /* casecollid will be set by parse_collate.c */
1641 
1642  /* Convert default result clause, if necessary */
1643  newc->defresult = (Expr *)
1644  coerce_to_common_type(pstate,
1645  (Node *) newc->defresult,
1646  ptype,
1647  "CASE/ELSE");
1648 
1649  /* Convert when-clause results, if necessary */
1650  foreach(l, newc->args)
1651  {
1652  CaseWhen *w = (CaseWhen *) lfirst(l);
1653 
1654  w->result = (Expr *)
1655  coerce_to_common_type(pstate,
1656  (Node *) w->result,
1657  ptype,
1658  "CASE/WHEN");
1659  }
1660 
1661  /* if any subexpression contained a SRF, complain */
1662  if (pstate->p_last_srf != last_srf)
1663  ereport(ERROR,
1664  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1665  /* translator: %s is name of a SQL construct, eg GROUP BY */
1666  errmsg("set-returning functions are not allowed in %s",
1667  "CASE"),
1668  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
1669  parser_errposition(pstate,
1670  exprLocation(pstate->p_last_srf))));
1671 
1672  newc->location = c->location;
1673 
1674  return (Node *) newc;
1675 }
1676 
1677 static Node *
1679 {
1680  Node *result = (Node *) sublink;
1681  Query *qtree;
1682  const char *err;
1683 
1684  /*
1685  * Check to see if the sublink is in an invalid place within the query. We
1686  * allow sublinks everywhere in SELECT/INSERT/UPDATE/DELETE, but generally
1687  * not in utility statements.
1688  */
1689  err = NULL;
1690  switch (pstate->p_expr_kind)
1691  {
1692  case EXPR_KIND_NONE:
1693  Assert(false); /* can't happen */
1694  break;
1695  case EXPR_KIND_OTHER:
1696  /* Accept sublink here; caller must throw error if wanted */
1697  break;
1698  case EXPR_KIND_JOIN_ON:
1699  case EXPR_KIND_JOIN_USING:
1702  case EXPR_KIND_WHERE:
1703  case EXPR_KIND_POLICY:
1704  case EXPR_KIND_HAVING:
1705  case EXPR_KIND_FILTER:
1715  case EXPR_KIND_GROUP_BY:
1716  case EXPR_KIND_ORDER_BY:
1717  case EXPR_KIND_DISTINCT_ON:
1718  case EXPR_KIND_LIMIT:
1719  case EXPR_KIND_OFFSET:
1720  case EXPR_KIND_RETURNING:
1721  case EXPR_KIND_VALUES:
1723  case EXPR_KIND_CYCLE_MARK:
1724  /* okay */
1725  break;
1728  err = _("cannot use subquery in check constraint");
1729  break;
1732  err = _("cannot use subquery in DEFAULT expression");
1733  break;
1735  err = _("cannot use subquery in index expression");
1736  break;
1738  err = _("cannot use subquery in index predicate");
1739  break;
1741  err = _("cannot use subquery in statistics expression");
1742  break;
1744  err = _("cannot use subquery in transform expression");
1745  break;
1747  err = _("cannot use subquery in EXECUTE parameter");
1748  break;
1750  err = _("cannot use subquery in trigger WHEN condition");
1751  break;
1753  err = _("cannot use subquery in partition bound");
1754  break;
1756  err = _("cannot use subquery in partition key expression");
1757  break;
1759  err = _("cannot use subquery in CALL argument");
1760  break;
1761  case EXPR_KIND_COPY_WHERE:
1762  err = _("cannot use subquery in COPY FROM WHERE condition");
1763  break;
1765  err = _("cannot use subquery in column generation expression");
1766  break;
1767 
1768  /*
1769  * There is intentionally no default: case here, so that the
1770  * compiler will warn if we add a new ParseExprKind without
1771  * extending this switch. If we do see an unrecognized value at
1772  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
1773  * which is sane anyway.
1774  */
1775  }
1776  if (err)
1777  ereport(ERROR,
1778  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1779  errmsg_internal("%s", err),
1780  parser_errposition(pstate, sublink->location)));
1781 
1782  pstate->p_hasSubLinks = true;
1783 
1784  /*
1785  * OK, let's transform the sub-SELECT.
1786  */
1787  qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
1788 
1789  /*
1790  * Check that we got a SELECT. Anything else should be impossible given
1791  * restrictions of the grammar, but check anyway.
1792  */
1793  if (!IsA(qtree, Query) ||
1794  qtree->commandType != CMD_SELECT)
1795  elog(ERROR, "unexpected non-SELECT command in SubLink");
1796 
1797  sublink->subselect = (Node *) qtree;
1798 
1799  if (sublink->subLinkType == EXISTS_SUBLINK)
1800  {
1801  /*
1802  * EXISTS needs no test expression or combining operator. These fields
1803  * should be null already, but make sure.
1804  */
1805  sublink->testexpr = NULL;
1806  sublink->operName = NIL;
1807  }
1808  else if (sublink->subLinkType == EXPR_SUBLINK ||
1809  sublink->subLinkType == ARRAY_SUBLINK)
1810  {
1811  /*
1812  * Make sure the subselect delivers a single column (ignoring resjunk
1813  * targets).
1814  */
1815  if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
1816  ereport(ERROR,
1817  (errcode(ERRCODE_SYNTAX_ERROR),
1818  errmsg("subquery must return only one column"),
1819  parser_errposition(pstate, sublink->location)));
1820 
1821  /*
1822  * EXPR and ARRAY need no test expression or combining operator. These
1823  * fields should be null already, but make sure.
1824  */
1825  sublink->testexpr = NULL;
1826  sublink->operName = NIL;
1827  }
1828  else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1829  {
1830  /* Same as EXPR case, except no restriction on number of columns */
1831  sublink->testexpr = NULL;
1832  sublink->operName = NIL;
1833  }
1834  else
1835  {
1836  /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
1837  Node *lefthand;
1838  List *left_list;
1839  List *right_list;
1840  ListCell *l;
1841 
1842  /*
1843  * If the source was "x IN (select)", convert to "x = ANY (select)".
1844  */
1845  if (sublink->operName == NIL)
1846  sublink->operName = list_make1(makeString("="));
1847 
1848  /*
1849  * Transform lefthand expression, and convert to a list
1850  */
1851  lefthand = transformExprRecurse(pstate, sublink->testexpr);
1852  if (lefthand && IsA(lefthand, RowExpr))
1853  left_list = ((RowExpr *) lefthand)->args;
1854  else
1855  left_list = list_make1(lefthand);
1856 
1857  /*
1858  * Build a list of PARAM_SUBLINK nodes representing the output columns
1859  * of the subquery.
1860  */
1861  right_list = NIL;
1862  foreach(l, qtree->targetList)
1863  {
1864  TargetEntry *tent = (TargetEntry *) lfirst(l);
1865  Param *param;
1866 
1867  if (tent->resjunk)
1868  continue;
1869 
1870  param = makeNode(Param);
1871  param->paramkind = PARAM_SUBLINK;
1872  param->paramid = tent->resno;
1873  param->paramtype = exprType((Node *) tent->expr);
1874  param->paramtypmod = exprTypmod((Node *) tent->expr);
1875  param->paramcollid = exprCollation((Node *) tent->expr);
1876  param->location = -1;
1877 
1878  right_list = lappend(right_list, param);
1879  }
1880 
1881  /*
1882  * We could rely on make_row_comparison_op to complain if the list
1883  * lengths differ, but we prefer to generate a more specific error
1884  * message.
1885  */
1886  if (list_length(left_list) < list_length(right_list))
1887  ereport(ERROR,
1888  (errcode(ERRCODE_SYNTAX_ERROR),
1889  errmsg("subquery has too many columns"),
1890  parser_errposition(pstate, sublink->location)));
1891  if (list_length(left_list) > list_length(right_list))
1892  ereport(ERROR,
1893  (errcode(ERRCODE_SYNTAX_ERROR),
1894  errmsg("subquery has too few columns"),
1895  parser_errposition(pstate, sublink->location)));
1896 
1897  /*
1898  * Identify the combining operator(s) and generate a suitable
1899  * row-comparison expression.
1900  */
1901  sublink->testexpr = make_row_comparison_op(pstate,
1902  sublink->operName,
1903  left_list,
1904  right_list,
1905  sublink->location);
1906  }
1907 
1908  return result;
1909 }
1910 
1911 /*
1912  * transformArrayExpr
1913  *
1914  * If the caller specifies the target type, the resulting array will
1915  * be of exactly that type. Otherwise we try to infer a common type
1916  * for the elements using select_common_type().
1917  */
1918 static Node *
1920  Oid array_type, Oid element_type, int32 typmod)
1921 {
1922  ArrayExpr *newa = makeNode(ArrayExpr);
1923  List *newelems = NIL;
1924  List *newcoercedelems = NIL;
1925  ListCell *element;
1926  Oid coerce_type;
1927  bool coerce_hard;
1928 
1929  /*
1930  * Transform the element expressions
1931  *
1932  * Assume that the array is one-dimensional unless we find an array-type
1933  * element expression.
1934  */
1935  newa->multidims = false;
1936  foreach(element, a->elements)
1937  {
1938  Node *e = (Node *) lfirst(element);
1939  Node *newe;
1940 
1941  /*
1942  * If an element is itself an A_ArrayExpr, recurse directly so that we
1943  * can pass down any target type we were given.
1944  */
1945  if (IsA(e, A_ArrayExpr))
1946  {
1947  newe = transformArrayExpr(pstate,
1948  (A_ArrayExpr *) e,
1949  array_type,
1950  element_type,
1951  typmod);
1952  /* we certainly have an array here */
1953  Assert(array_type == InvalidOid || array_type == exprType(newe));
1954  newa->multidims = true;
1955  }
1956  else
1957  {
1958  newe = transformExprRecurse(pstate, e);
1959 
1960  /*
1961  * Check for sub-array expressions, if we haven't already found
1962  * one.
1963  */
1964  if (!newa->multidims && type_is_array(exprType(newe)))
1965  newa->multidims = true;
1966  }
1967 
1968  newelems = lappend(newelems, newe);
1969  }
1970 
1971  /*
1972  * Select a target type for the elements.
1973  *
1974  * If we haven't been given a target array type, we must try to deduce a
1975  * common type based on the types of the individual elements present.
1976  */
1977  if (OidIsValid(array_type))
1978  {
1979  /* Caller must ensure array_type matches element_type */
1980  Assert(OidIsValid(element_type));
1981  coerce_type = (newa->multidims ? array_type : element_type);
1982  coerce_hard = true;
1983  }
1984  else
1985  {
1986  /* Can't handle an empty array without a target type */
1987  if (newelems == NIL)
1988  ereport(ERROR,
1989  (errcode(ERRCODE_INDETERMINATE_DATATYPE),
1990  errmsg("cannot determine type of empty array"),
1991  errhint("Explicitly cast to the desired type, "
1992  "for example ARRAY[]::integer[]."),
1993  parser_errposition(pstate, a->location)));
1994 
1995  /* Select a common type for the elements */
1996  coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
1997 
1998  if (newa->multidims)
1999  {
2000  array_type = coerce_type;
2001  element_type = get_element_type(array_type);
2002  if (!OidIsValid(element_type))
2003  ereport(ERROR,
2004  (errcode(ERRCODE_UNDEFINED_OBJECT),
2005  errmsg("could not find element type for data type %s",
2006  format_type_be(array_type)),
2007  parser_errposition(pstate, a->location)));
2008  }
2009  else
2010  {
2011  element_type = coerce_type;
2012  array_type = get_array_type(element_type);
2013  if (!OidIsValid(array_type))
2014  ereport(ERROR,
2015  (errcode(ERRCODE_UNDEFINED_OBJECT),
2016  errmsg("could not find array type for data type %s",
2017  format_type_be(element_type)),
2018  parser_errposition(pstate, a->location)));
2019  }
2020  coerce_hard = false;
2021  }
2022 
2023  /*
2024  * Coerce elements to target type
2025  *
2026  * If the array has been explicitly cast, then the elements are in turn
2027  * explicitly coerced.
2028  *
2029  * If the array's type was merely derived from the common type of its
2030  * elements, then the elements are implicitly coerced to the common type.
2031  * This is consistent with other uses of select_common_type().
2032  */
2033  foreach(element, newelems)
2034  {
2035  Node *e = (Node *) lfirst(element);
2036  Node *newe;
2037 
2038  if (coerce_hard)
2039  {
2040  newe = coerce_to_target_type(pstate, e,
2041  exprType(e),
2042  coerce_type,
2043  typmod,
2046  -1);
2047  if (newe == NULL)
2048  ereport(ERROR,
2049  (errcode(ERRCODE_CANNOT_COERCE),
2050  errmsg("cannot cast type %s to %s",
2052  format_type_be(coerce_type)),
2053  parser_errposition(pstate, exprLocation(e))));
2054  }
2055  else
2056  newe = coerce_to_common_type(pstate, e,
2057  coerce_type,
2058  "ARRAY");
2059  newcoercedelems = lappend(newcoercedelems, newe);
2060  }
2061 
2062  newa->array_typeid = array_type;
2063  /* array_collid will be set by parse_collate.c */
2064  newa->element_typeid = element_type;
2065  newa->elements = newcoercedelems;
2066  newa->location = a->location;
2067 
2068  return (Node *) newa;
2069 }
2070 
2071 static Node *
2072 transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
2073 {
2074  RowExpr *newr;
2075  char fname[16];
2076  int fnum;
2077 
2078  newr = makeNode(RowExpr);
2079 
2080  /* Transform the field expressions */
2081  newr->args = transformExpressionList(pstate, r->args,
2082  pstate->p_expr_kind, allowDefault);
2083 
2084  /* Barring later casting, we consider the type RECORD */
2085  newr->row_typeid = RECORDOID;
2087 
2088  /* ROW() has anonymous columns, so invent some field names */
2089  newr->colnames = NIL;
2090  for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2091  {
2092  snprintf(fname, sizeof(fname), "f%d", fnum);
2093  newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2094  }
2095 
2096  newr->location = r->location;
2097 
2098  return (Node *) newr;
2099 }
2100 
2101 static Node *
2103 {
2105  Node *last_srf = pstate->p_last_srf;
2106  List *newargs = NIL;
2107  List *newcoercedargs = NIL;
2108  ListCell *args;
2109 
2110  foreach(args, c->args)
2111  {
2112  Node *e = (Node *) lfirst(args);
2113  Node *newe;
2114 
2115  newe = transformExprRecurse(pstate, e);
2116  newargs = lappend(newargs, newe);
2117  }
2118 
2119  newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2120  /* coalescecollid will be set by parse_collate.c */
2121 
2122  /* Convert arguments if necessary */
2123  foreach(args, newargs)
2124  {
2125  Node *e = (Node *) lfirst(args);
2126  Node *newe;
2127 
2128  newe = coerce_to_common_type(pstate, e,
2129  newc->coalescetype,
2130  "COALESCE");
2131  newcoercedargs = lappend(newcoercedargs, newe);
2132  }
2133 
2134  /* if any subexpression contained a SRF, complain */
2135  if (pstate->p_last_srf != last_srf)
2136  ereport(ERROR,
2137  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2138  /* translator: %s is name of a SQL construct, eg GROUP BY */
2139  errmsg("set-returning functions are not allowed in %s",
2140  "COALESCE"),
2141  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2142  parser_errposition(pstate,
2143  exprLocation(pstate->p_last_srf))));
2144 
2145  newc->args = newcoercedargs;
2146  newc->location = c->location;
2147  return (Node *) newc;
2148 }
2149 
2150 static Node *
2152 {
2153  MinMaxExpr *newm = makeNode(MinMaxExpr);
2154  List *newargs = NIL;
2155  List *newcoercedargs = NIL;
2156  const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2157  ListCell *args;
2158 
2159  newm->op = m->op;
2160  foreach(args, m->args)
2161  {
2162  Node *e = (Node *) lfirst(args);
2163  Node *newe;
2164 
2165  newe = transformExprRecurse(pstate, e);
2166  newargs = lappend(newargs, newe);
2167  }
2168 
2169  newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2170  /* minmaxcollid and inputcollid will be set by parse_collate.c */
2171 
2172  /* Convert arguments if necessary */
2173  foreach(args, newargs)
2174  {
2175  Node *e = (Node *) lfirst(args);
2176  Node *newe;
2177 
2178  newe = coerce_to_common_type(pstate, e,
2179  newm->minmaxtype,
2180  funcname);
2181  newcoercedargs = lappend(newcoercedargs, newe);
2182  }
2183 
2184  newm->args = newcoercedargs;
2185  newm->location = m->location;
2186  return (Node *) newm;
2187 }
2188 
2189 static Node *
2191 {
2192  /*
2193  * All we need to do is insert the correct result type and (where needed)
2194  * validate the typmod, so we just modify the node in-place.
2195  */
2196  switch (svf->op)
2197  {
2198  case SVFOP_CURRENT_DATE:
2199  svf->type = DATEOID;
2200  break;
2201  case SVFOP_CURRENT_TIME:
2202  svf->type = TIMETZOID;
2203  break;
2204  case SVFOP_CURRENT_TIME_N:
2205  svf->type = TIMETZOID;
2206  svf->typmod = anytime_typmod_check(true, svf->typmod);
2207  break;
2209  svf->type = TIMESTAMPTZOID;
2210  break;
2212  svf->type = TIMESTAMPTZOID;
2213  svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2214  break;
2215  case SVFOP_LOCALTIME:
2216  svf->type = TIMEOID;
2217  break;
2218  case SVFOP_LOCALTIME_N:
2219  svf->type = TIMEOID;
2220  svf->typmod = anytime_typmod_check(false, svf->typmod);
2221  break;
2222  case SVFOP_LOCALTIMESTAMP:
2223  svf->type = TIMESTAMPOID;
2224  break;
2226  svf->type = TIMESTAMPOID;
2227  svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2228  break;
2229  case SVFOP_CURRENT_ROLE:
2230  case SVFOP_CURRENT_USER:
2231  case SVFOP_USER:
2232  case SVFOP_SESSION_USER:
2233  case SVFOP_CURRENT_CATALOG:
2234  case SVFOP_CURRENT_SCHEMA:
2235  svf->type = NAMEOID;
2236  break;
2237  }
2238 
2239  return (Node *) svf;
2240 }
2241 
2242 static Node *
2244 {
2245  XmlExpr *newx;
2246  ListCell *lc;
2247  int i;
2248 
2249  newx = makeNode(XmlExpr);
2250  newx->op = x->op;
2251  if (x->name)
2252  newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
2253  else
2254  newx->name = NULL;
2255  newx->xmloption = x->xmloption;
2256  newx->type = XMLOID; /* this just marks the node as transformed */
2257  newx->typmod = -1;
2258  newx->location = x->location;
2259 
2260  /*
2261  * gram.y built the named args as a list of ResTarget. Transform each,
2262  * and break the names out as a separate list.
2263  */
2264  newx->named_args = NIL;
2265  newx->arg_names = NIL;
2266 
2267  foreach(lc, x->named_args)
2268  {
2269  ResTarget *r = lfirst_node(ResTarget, lc);
2270  Node *expr;
2271  char *argname;
2272 
2273  expr = transformExprRecurse(pstate, r->val);
2274 
2275  if (r->name)
2276  argname = map_sql_identifier_to_xml_name(r->name, false, false);
2277  else if (IsA(r->val, ColumnRef))
2279  true, false);
2280  else
2281  {
2282  ereport(ERROR,
2283  (errcode(ERRCODE_SYNTAX_ERROR),
2284  x->op == IS_XMLELEMENT
2285  ? errmsg("unnamed XML attribute value must be a column reference")
2286  : errmsg("unnamed XML element value must be a column reference"),
2287  parser_errposition(pstate, r->location)));
2288  argname = NULL; /* keep compiler quiet */
2289  }
2290 
2291  /* reject duplicate argnames in XMLELEMENT only */
2292  if (x->op == IS_XMLELEMENT)
2293  {
2294  ListCell *lc2;
2295 
2296  foreach(lc2, newx->arg_names)
2297  {
2298  if (strcmp(argname, strVal(lfirst(lc2))) == 0)
2299  ereport(ERROR,
2300  (errcode(ERRCODE_SYNTAX_ERROR),
2301  errmsg("XML attribute name \"%s\" appears more than once",
2302  argname),
2303  parser_errposition(pstate, r->location)));
2304  }
2305  }
2306 
2307  newx->named_args = lappend(newx->named_args, expr);
2308  newx->arg_names = lappend(newx->arg_names, makeString(argname));
2309  }
2310 
2311  /* The other arguments are of varying types depending on the function */
2312  newx->args = NIL;
2313  i = 0;
2314  foreach(lc, x->args)
2315  {
2316  Node *e = (Node *) lfirst(lc);
2317  Node *newe;
2318 
2319  newe = transformExprRecurse(pstate, e);
2320  switch (x->op)
2321  {
2322  case IS_XMLCONCAT:
2323  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2324  "XMLCONCAT");
2325  break;
2326  case IS_XMLELEMENT:
2327  /* no coercion necessary */
2328  break;
2329  case IS_XMLFOREST:
2330  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2331  "XMLFOREST");
2332  break;
2333  case IS_XMLPARSE:
2334  if (i == 0)
2335  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2336  "XMLPARSE");
2337  else
2338  newe = coerce_to_boolean(pstate, newe, "XMLPARSE");
2339  break;
2340  case IS_XMLPI:
2341  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2342  "XMLPI");
2343  break;
2344  case IS_XMLROOT:
2345  if (i == 0)
2346  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2347  "XMLROOT");
2348  else if (i == 1)
2349  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2350  "XMLROOT");
2351  else
2352  newe = coerce_to_specific_type(pstate, newe, INT4OID,
2353  "XMLROOT");
2354  break;
2355  case IS_XMLSERIALIZE:
2356  /* not handled here */
2357  Assert(false);
2358  break;
2359  case IS_DOCUMENT:
2360  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2361  "IS DOCUMENT");
2362  break;
2363  }
2364  newx->args = lappend(newx->args, newe);
2365  i++;
2366  }
2367 
2368  return (Node *) newx;
2369 }
2370 
2371 static Node *
2373 {
2374  Node *result;
2375  XmlExpr *xexpr;
2376  Oid targetType;
2377  int32 targetTypmod;
2378 
2379  xexpr = makeNode(XmlExpr);
2380  xexpr->op = IS_XMLSERIALIZE;
2381  xexpr->args = list_make1(coerce_to_specific_type(pstate,
2382  transformExprRecurse(pstate, xs->expr),
2383  XMLOID,
2384  "XMLSERIALIZE"));
2385 
2386  typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
2387 
2388  xexpr->xmloption = xs->xmloption;
2389  xexpr->location = xs->location;
2390  /* We actually only need these to be able to parse back the expression. */
2391  xexpr->type = targetType;
2392  xexpr->typmod = targetTypmod;
2393 
2394  /*
2395  * The actual target type is determined this way. SQL allows char and
2396  * varchar as target types. We allow anything that can be cast implicitly
2397  * from text. This way, user-defined text-like data types automatically
2398  * fit in.
2399  */
2400  result = coerce_to_target_type(pstate, (Node *) xexpr,
2401  TEXTOID, targetType, targetTypmod,
2404  -1);
2405  if (result == NULL)
2406  ereport(ERROR,
2407  (errcode(ERRCODE_CANNOT_COERCE),
2408  errmsg("cannot cast XMLSERIALIZE result to %s",
2409  format_type_be(targetType)),
2410  parser_errposition(pstate, xexpr->location)));
2411  return result;
2412 }
2413 
2414 static Node *
2416 {
2417  const char *clausename;
2418 
2419  switch (b->booltesttype)
2420  {
2421  case IS_TRUE:
2422  clausename = "IS TRUE";
2423  break;
2424  case IS_NOT_TRUE:
2425  clausename = "IS NOT TRUE";
2426  break;
2427  case IS_FALSE:
2428  clausename = "IS FALSE";
2429  break;
2430  case IS_NOT_FALSE:
2431  clausename = "IS NOT FALSE";
2432  break;
2433  case IS_UNKNOWN:
2434  clausename = "IS UNKNOWN";
2435  break;
2436  case IS_NOT_UNKNOWN:
2437  clausename = "IS NOT UNKNOWN";
2438  break;
2439  default:
2440  elog(ERROR, "unrecognized booltesttype: %d",
2441  (int) b->booltesttype);
2442  clausename = NULL; /* keep compiler quiet */
2443  }
2444 
2445  b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2446 
2447  b->arg = (Expr *) coerce_to_boolean(pstate,
2448  (Node *) b->arg,
2449  clausename);
2450 
2451  return (Node *) b;
2452 }
2453 
2454 static Node *
2456 {
2457  /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2458  Assert(pstate->p_target_nsitem != NULL);
2459  cexpr->cvarno = pstate->p_target_nsitem->p_rtindex;
2460 
2461  /*
2462  * Check to see if the cursor name matches a parameter of type REFCURSOR.
2463  * If so, replace the raw name reference with a parameter reference. (This
2464  * is a hack for the convenience of plpgsql.)
2465  */
2466  if (cexpr->cursor_name != NULL) /* in case already transformed */
2467  {
2468  ColumnRef *cref = makeNode(ColumnRef);
2469  Node *node = NULL;
2470 
2471  /* Build an unqualified ColumnRef with the given name */
2472  cref->fields = list_make1(makeString(cexpr->cursor_name));
2473  cref->location = -1;
2474 
2475  /* See if there is a translation available from a parser hook */
2476  if (pstate->p_pre_columnref_hook != NULL)
2477  node = pstate->p_pre_columnref_hook(pstate, cref);
2478  if (node == NULL && pstate->p_post_columnref_hook != NULL)
2479  node = pstate->p_post_columnref_hook(pstate, cref, NULL);
2480 
2481  /*
2482  * XXX Should we throw an error if we get a translation that isn't a
2483  * refcursor Param? For now it seems best to silently ignore false
2484  * matches.
2485  */
2486  if (node != NULL && IsA(node, Param))
2487  {
2488  Param *p = (Param *) node;
2489 
2490  if (p->paramkind == PARAM_EXTERN &&
2491  p->paramtype == REFCURSOROID)
2492  {
2493  /* Matches, so convert CURRENT OF to a param reference */
2494  cexpr->cursor_name = NULL;
2495  cexpr->cursor_param = p->paramid;
2496  }
2497  }
2498  }
2499 
2500  return (Node *) cexpr;
2501 }
2502 
2503 /*
2504  * Construct a whole-row reference to represent the notation "relation.*".
2505  */
2506 static Node *
2508  int sublevels_up, int location)
2509 {
2510  /*
2511  * Build the appropriate referencing node. Normally this can be a
2512  * whole-row Var, but if the nsitem is a JOIN USING alias then it contains
2513  * only a subset of the columns of the underlying join RTE, so that will
2514  * not work. Instead we immediately expand the reference into a RowExpr.
2515  * Since the JOIN USING's common columns are fully determined at this
2516  * point, there seems no harm in expanding it now rather than during
2517  * planning.
2518  *
2519  * Note that if the RTE is a function returning scalar, we create just a
2520  * plain reference to the function value, not a composite containing a
2521  * single column. This is pretty inconsistent at first sight, but it's
2522  * what we've done historically. One argument for it is that "rel" and
2523  * "rel.*" mean the same thing for composite relations, so why not for
2524  * scalar functions...
2525  */
2526  if (nsitem->p_names == nsitem->p_rte->eref)
2527  {
2528  Var *result;
2529 
2530  result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
2531  sublevels_up, true);
2532 
2533  /* location is not filled in by makeWholeRowVar */
2534  result->location = location;
2535 
2536  /* mark relation as requiring whole-row SELECT access */
2537  markVarForSelectPriv(pstate, result);
2538 
2539  return (Node *) result;
2540  }
2541  else
2542  {
2543  RowExpr *rowexpr;
2544  List *fields;
2545 
2546  /*
2547  * We want only as many columns as are listed in p_names->colnames,
2548  * and we should use those names not whatever possibly-aliased names
2549  * are in the RTE. We needn't worry about marking the RTE for SELECT
2550  * access, as the common columns are surely so marked already.
2551  */
2552  expandRTE(nsitem->p_rte, nsitem->p_rtindex,
2553  sublevels_up, location, false,
2554  NULL, &fields);
2555  rowexpr = makeNode(RowExpr);
2556  rowexpr->args = list_truncate(fields,
2557  list_length(nsitem->p_names->colnames));
2558  rowexpr->row_typeid = RECORDOID;
2559  rowexpr->row_format = COERCE_IMPLICIT_CAST;
2560  rowexpr->colnames = copyObject(nsitem->p_names->colnames);
2561  rowexpr->location = location;
2562 
2563  return (Node *) rowexpr;
2564  }
2565 }
2566 
2567 /*
2568  * Handle an explicit CAST construct.
2569  *
2570  * Transform the argument, look up the type name, and apply any necessary
2571  * coercion function(s).
2572  */
2573 static Node *
2575 {
2576  Node *result;
2577  Node *arg = tc->arg;
2578  Node *expr;
2579  Oid inputType;
2580  Oid targetType;
2581  int32 targetTypmod;
2582  int location;
2583 
2584  /* Look up the type name first */
2585  typenameTypeIdAndMod(pstate, tc->typeName, &targetType, &targetTypmod);
2586 
2587  /*
2588  * If the subject of the typecast is an ARRAY[] construct and the target
2589  * type is an array type, we invoke transformArrayExpr() directly so that
2590  * we can pass down the type information. This avoids some cases where
2591  * transformArrayExpr() might not infer the correct type. Otherwise, just
2592  * transform the argument normally.
2593  */
2594  if (IsA(arg, A_ArrayExpr))
2595  {
2596  Oid targetBaseType;
2597  int32 targetBaseTypmod;
2598  Oid elementType;
2599 
2600  /*
2601  * If target is a domain over array, work with the base array type
2602  * here. Below, we'll cast the array type to the domain. In the
2603  * usual case that the target is not a domain, the remaining steps
2604  * will be a no-op.
2605  */
2606  targetBaseTypmod = targetTypmod;
2607  targetBaseType = getBaseTypeAndTypmod(targetType, &targetBaseTypmod);
2608  elementType = get_element_type(targetBaseType);
2609  if (OidIsValid(elementType))
2610  {
2611  expr = transformArrayExpr(pstate,
2612  (A_ArrayExpr *) arg,
2613  targetBaseType,
2614  elementType,
2615  targetBaseTypmod);
2616  }
2617  else
2618  expr = transformExprRecurse(pstate, arg);
2619  }
2620  else
2621  expr = transformExprRecurse(pstate, arg);
2622 
2623  inputType = exprType(expr);
2624  if (inputType == InvalidOid)
2625  return expr; /* do nothing if NULL input */
2626 
2627  /*
2628  * Location of the coercion is preferentially the location of the :: or
2629  * CAST symbol, but if there is none then use the location of the type
2630  * name (this can happen in TypeName 'string' syntax, for instance).
2631  */
2632  location = tc->location;
2633  if (location < 0)
2634  location = tc->typeName->location;
2635 
2636  result = coerce_to_target_type(pstate, expr, inputType,
2637  targetType, targetTypmod,
2640  location);
2641  if (result == NULL)
2642  ereport(ERROR,
2643  (errcode(ERRCODE_CANNOT_COERCE),
2644  errmsg("cannot cast type %s to %s",
2645  format_type_be(inputType),
2646  format_type_be(targetType)),
2647  parser_coercion_errposition(pstate, location, expr)));
2648 
2649  return result;
2650 }
2651 
2652 /*
2653  * Handle an explicit COLLATE clause.
2654  *
2655  * Transform the argument, and look up the collation name.
2656  */
2657 static Node *
2659 {
2660  CollateExpr *newc;
2661  Oid argtype;
2662 
2663  newc = makeNode(CollateExpr);
2664  newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2665 
2666  argtype = exprType((Node *) newc->arg);
2667 
2668  /*
2669  * The unknown type is not collatable, but coerce_type() takes care of it
2670  * separately, so we'll let it go here.
2671  */
2672  if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2673  ereport(ERROR,
2674  (errcode(ERRCODE_DATATYPE_MISMATCH),
2675  errmsg("collations are not supported by type %s",
2676  format_type_be(argtype)),
2677  parser_errposition(pstate, c->location)));
2678 
2679  newc->collOid = LookupCollation(pstate, c->collname, c->location);
2680  newc->location = c->location;
2681 
2682  return (Node *) newc;
2683 }
2684 
2685 /*
2686  * Transform a "row compare-op row" construct
2687  *
2688  * The inputs are lists of already-transformed expressions.
2689  * As with coerce_type, pstate may be NULL if no special unknown-Param
2690  * processing is wanted.
2691  *
2692  * The output may be a single OpExpr, an AND or OR combination of OpExprs,
2693  * or a RowCompareExpr. In all cases it is guaranteed to return boolean.
2694  * The AND, OR, and RowCompareExpr cases further imply things about the
2695  * behavior of the operators (ie, they behave as =, <>, or < <= > >=).
2696  */
2697 static Node *
2699  List *largs, List *rargs, int location)
2700 {
2701  RowCompareExpr *rcexpr;
2702  RowCompareType rctype;
2703  List *opexprs;
2704  List *opnos;
2705  List *opfamilies;
2706  ListCell *l,
2707  *r;
2708  List **opinfo_lists;
2709  Bitmapset *strats;
2710  int nopers;
2711  int i;
2712 
2713  nopers = list_length(largs);
2714  if (nopers != list_length(rargs))
2715  ereport(ERROR,
2716  (errcode(ERRCODE_SYNTAX_ERROR),
2717  errmsg("unequal number of entries in row expressions"),
2718  parser_errposition(pstate, location)));
2719 
2720  /*
2721  * We can't compare zero-length rows because there is no principled basis
2722  * for figuring out what the operator is.
2723  */
2724  if (nopers == 0)
2725  ereport(ERROR,
2726  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2727  errmsg("cannot compare rows of zero length"),
2728  parser_errposition(pstate, location)));
2729 
2730  /*
2731  * Identify all the pairwise operators, using make_op so that behavior is
2732  * the same as in the simple scalar case.
2733  */
2734  opexprs = NIL;
2735  forboth(l, largs, r, rargs)
2736  {
2737  Node *larg = (Node *) lfirst(l);
2738  Node *rarg = (Node *) lfirst(r);
2739  OpExpr *cmp;
2740 
2741  cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
2742  pstate->p_last_srf, location));
2743 
2744  /*
2745  * We don't use coerce_to_boolean here because we insist on the
2746  * operator yielding boolean directly, not via coercion. If it
2747  * doesn't yield bool it won't be in any index opfamilies...
2748  */
2749  if (cmp->opresulttype != BOOLOID)
2750  ereport(ERROR,
2751  (errcode(ERRCODE_DATATYPE_MISMATCH),
2752  errmsg("row comparison operator must yield type boolean, "
2753  "not type %s",
2755  parser_errposition(pstate, location)));
2756  if (expression_returns_set((Node *) cmp))
2757  ereport(ERROR,
2758  (errcode(ERRCODE_DATATYPE_MISMATCH),
2759  errmsg("row comparison operator must not return a set"),
2760  parser_errposition(pstate, location)));
2761  opexprs = lappend(opexprs, cmp);
2762  }
2763 
2764  /*
2765  * If rows are length 1, just return the single operator. In this case we
2766  * don't insist on identifying btree semantics for the operator (but we
2767  * still require it to return boolean).
2768  */
2769  if (nopers == 1)
2770  return (Node *) linitial(opexprs);
2771 
2772  /*
2773  * Now we must determine which row comparison semantics (= <> < <= > >=)
2774  * apply to this set of operators. We look for btree opfamilies
2775  * containing the operators, and see which interpretations (strategy
2776  * numbers) exist for each operator.
2777  */
2778  opinfo_lists = (List **) palloc(nopers * sizeof(List *));
2779  strats = NULL;
2780  i = 0;
2781  foreach(l, opexprs)
2782  {
2783  Oid opno = ((OpExpr *) lfirst(l))->opno;
2784  Bitmapset *this_strats;
2785  ListCell *j;
2786 
2787  opinfo_lists[i] = get_op_btree_interpretation(opno);
2788 
2789  /*
2790  * convert strategy numbers into a Bitmapset to make the intersection
2791  * calculation easy.
2792  */
2793  this_strats = NULL;
2794  foreach(j, opinfo_lists[i])
2795  {
2796  OpBtreeInterpretation *opinfo = lfirst(j);
2797 
2798  this_strats = bms_add_member(this_strats, opinfo->strategy);
2799  }
2800  if (i == 0)
2801  strats = this_strats;
2802  else
2803  strats = bms_int_members(strats, this_strats);
2804  i++;
2805  }
2806 
2807  /*
2808  * If there are multiple common interpretations, we may use any one of
2809  * them ... this coding arbitrarily picks the lowest btree strategy
2810  * number.
2811  */
2812  i = bms_first_member(strats);
2813  if (i < 0)
2814  {
2815  /* No common interpretation, so fail */
2816  ereport(ERROR,
2817  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2818  errmsg("could not determine interpretation of row comparison operator %s",
2819  strVal(llast(opname))),
2820  errhint("Row comparison operators must be associated with btree operator families."),
2821  parser_errposition(pstate, location)));
2822  }
2823  rctype = (RowCompareType) i;
2824 
2825  /*
2826  * For = and <> cases, we just combine the pairwise operators with AND or
2827  * OR respectively.
2828  */
2829  if (rctype == ROWCOMPARE_EQ)
2830  return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
2831  if (rctype == ROWCOMPARE_NE)
2832  return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
2833 
2834  /*
2835  * Otherwise we need to choose exactly which opfamily to associate with
2836  * each operator.
2837  */
2838  opfamilies = NIL;
2839  for (i = 0; i < nopers; i++)
2840  {
2841  Oid opfamily = InvalidOid;
2842  ListCell *j;
2843 
2844  foreach(j, opinfo_lists[i])
2845  {
2846  OpBtreeInterpretation *opinfo = lfirst(j);
2847 
2848  if (opinfo->strategy == rctype)
2849  {
2850  opfamily = opinfo->opfamily_id;
2851  break;
2852  }
2853  }
2854  if (OidIsValid(opfamily))
2855  opfamilies = lappend_oid(opfamilies, opfamily);
2856  else /* should not happen */
2857  ereport(ERROR,
2858  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2859  errmsg("could not determine interpretation of row comparison operator %s",
2860  strVal(llast(opname))),
2861  errdetail("There are multiple equally-plausible candidates."),
2862  parser_errposition(pstate, location)));
2863  }
2864 
2865  /*
2866  * Now deconstruct the OpExprs and create a RowCompareExpr.
2867  *
2868  * Note: can't just reuse the passed largs/rargs lists, because of
2869  * possibility that make_op inserted coercion operations.
2870  */
2871  opnos = NIL;
2872  largs = NIL;
2873  rargs = NIL;
2874  foreach(l, opexprs)
2875  {
2876  OpExpr *cmp = (OpExpr *) lfirst(l);
2877 
2878  opnos = lappend_oid(opnos, cmp->opno);
2879  largs = lappend(largs, linitial(cmp->args));
2880  rargs = lappend(rargs, lsecond(cmp->args));
2881  }
2882 
2883  rcexpr = makeNode(RowCompareExpr);
2884  rcexpr->rctype = rctype;
2885  rcexpr->opnos = opnos;
2886  rcexpr->opfamilies = opfamilies;
2887  rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
2888  rcexpr->largs = largs;
2889  rcexpr->rargs = rargs;
2890 
2891  return (Node *) rcexpr;
2892 }
2893 
2894 /*
2895  * Transform a "row IS DISTINCT FROM row" construct
2896  *
2897  * The input RowExprs are already transformed
2898  */
2899 static Node *
2901  RowExpr *lrow, RowExpr *rrow,
2902  int location)
2903 {
2904  Node *result = NULL;
2905  List *largs = lrow->args;
2906  List *rargs = rrow->args;
2907  ListCell *l,
2908  *r;
2909 
2910  if (list_length(largs) != list_length(rargs))
2911  ereport(ERROR,
2912  (errcode(ERRCODE_SYNTAX_ERROR),
2913  errmsg("unequal number of entries in row expressions"),
2914  parser_errposition(pstate, location)));
2915 
2916  forboth(l, largs, r, rargs)
2917  {
2918  Node *larg = (Node *) lfirst(l);
2919  Node *rarg = (Node *) lfirst(r);
2920  Node *cmp;
2921 
2922  cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
2923  if (result == NULL)
2924  result = cmp;
2925  else
2926  result = (Node *) makeBoolExpr(OR_EXPR,
2927  list_make2(result, cmp),
2928  location);
2929  }
2930 
2931  if (result == NULL)
2932  {
2933  /* zero-length rows? Generate constant FALSE */
2934  result = makeBoolConst(false, false);
2935  }
2936 
2937  return result;
2938 }
2939 
2940 /*
2941  * make the node for an IS DISTINCT FROM operator
2942  */
2943 static Expr *
2944 make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree,
2945  int location)
2946 {
2947  Expr *result;
2948 
2949  result = make_op(pstate, opname, ltree, rtree,
2950  pstate->p_last_srf, location);
2951  if (((OpExpr *) result)->opresulttype != BOOLOID)
2952  ereport(ERROR,
2953  (errcode(ERRCODE_DATATYPE_MISMATCH),
2954  errmsg("IS DISTINCT FROM requires = operator to yield boolean"),
2955  parser_errposition(pstate, location)));
2956  if (((OpExpr *) result)->opretset)
2957  ereport(ERROR,
2958  (errcode(ERRCODE_DATATYPE_MISMATCH),
2959  /* translator: %s is name of a SQL construct, eg NULLIF */
2960  errmsg("%s must not return a set", "IS DISTINCT FROM"),
2961  parser_errposition(pstate, location)));
2962 
2963  /*
2964  * We rely on DistinctExpr and OpExpr being same struct
2965  */
2966  NodeSetTag(result, T_DistinctExpr);
2967 
2968  return result;
2969 }
2970 
2971 /*
2972  * Produce a NullTest node from an IS [NOT] DISTINCT FROM NULL construct
2973  *
2974  * "arg" is the untransformed other argument
2975  */
2976 static Node *
2978 {
2979  NullTest *nt = makeNode(NullTest);
2980 
2981  nt->arg = (Expr *) transformExprRecurse(pstate, arg);
2982  /* the argument can be any type, so don't coerce it */
2983  if (distincta->kind == AEXPR_NOT_DISTINCT)
2984  nt->nulltesttype = IS_NULL;
2985  else
2986  nt->nulltesttype = IS_NOT_NULL;
2987  /* argisrow = false is correct whether or not arg is composite */
2988  nt->argisrow = false;
2989  nt->location = distincta->location;
2990  return (Node *) nt;
2991 }
2992 
2993 /*
2994  * Produce a string identifying an expression by kind.
2995  *
2996  * Note: when practical, use a simple SQL keyword for the result. If that
2997  * doesn't work well, check call sites to see whether custom error message
2998  * strings are required.
2999  */
3000 const char *
3002 {
3003  switch (exprKind)
3004  {
3005  case EXPR_KIND_NONE:
3006  return "invalid expression context";
3007  case EXPR_KIND_OTHER:
3008  return "extension expression";
3009  case EXPR_KIND_JOIN_ON:
3010  return "JOIN/ON";
3011  case EXPR_KIND_JOIN_USING:
3012  return "JOIN/USING";
3014  return "sub-SELECT in FROM";
3016  return "function in FROM";
3017  case EXPR_KIND_WHERE:
3018  return "WHERE";
3019  case EXPR_KIND_POLICY:
3020  return "POLICY";
3021  case EXPR_KIND_HAVING:
3022  return "HAVING";
3023  case EXPR_KIND_FILTER:
3024  return "FILTER";
3026  return "window PARTITION BY";
3028  return "window ORDER BY";
3030  return "window RANGE";
3032  return "window ROWS";
3034  return "window GROUPS";
3036  return "SELECT";
3038  return "INSERT";
3041  return "UPDATE";
3042  case EXPR_KIND_GROUP_BY:
3043  return "GROUP BY";
3044  case EXPR_KIND_ORDER_BY:
3045  return "ORDER BY";
3046  case EXPR_KIND_DISTINCT_ON:
3047  return "DISTINCT ON";
3048  case EXPR_KIND_LIMIT:
3049  return "LIMIT";
3050  case EXPR_KIND_OFFSET:
3051  return "OFFSET";
3052  case EXPR_KIND_RETURNING:
3053  return "RETURNING";
3054  case EXPR_KIND_VALUES:
3056  return "VALUES";
3059  return "CHECK";
3062  return "DEFAULT";
3064  return "index expression";
3066  return "index predicate";
3068  return "statistics expression";
3070  return "USING";
3072  return "EXECUTE";
3074  return "WHEN";
3076  return "partition bound";
3078  return "PARTITION BY";
3080  return "CALL";
3081  case EXPR_KIND_COPY_WHERE:
3082  return "WHERE";
3084  return "GENERATED AS";
3085  case EXPR_KIND_CYCLE_MARK:
3086  return "CYCLE";
3087 
3088  /*
3089  * There is intentionally no default: case here, so that the
3090  * compiler will warn if we add a new ParseExprKind without
3091  * extending this switch. If we do see an unrecognized value at
3092  * runtime, we'll fall through to the "unrecognized" return.
3093  */
3094  }
3095  return "unrecognized expression kind";
3096 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:672
static Node * transformAExprDistinct(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:969
Oid minmaxtype
Definition: primnodes.h:1148
bool multidims
Definition: primnodes.h:1038
#define NIL
Definition: pg_list.h:65
static Node * transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
Definition: parse_expr.c:2072
static bool exprIsNullConstant(Node *arg)
Definition: parse_expr.c:847
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
List * args
Definition: primnodes.h:1152
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2485
bool p_hasSubLinks
Definition: parse_node.h:211
List * args
Definition: primnodes.h:1068
static void unknown_attribute(ParseState *pstate, Node *relref, const char *attname, int location)
Definition: parse_expr.c:321
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:996
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
static Node * transformIndirection(ParseState *pstate, A_Indirection *ind)
Definition: parse_expr.c:367
Node * val
Definition: parsenodes.h:466
int errhint(const char *fmt,...)
Definition: elog.c:1156
static Node * transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
Definition: parse_expr.c:2455
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:783
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
static Node * transformAExprOpAny(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:941
char * name
Definition: primnodes.h:1228
A_Expr_Kind kind
Definition: parsenodes.h:285
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1250
Const * make_const(ParseState *pstate, A_Const *aconst)
Definition: parse_node.c:352
void markVarForSelectPriv(ParseState *pstate, Var *var)
List * colnames
Definition: primnodes.h:43
static Node * transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
Definition: parse_expr.c:2151
char * name
Definition: parsenodes.h:464
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:69
#define castNode(_type_, nodeptr)
Definition: nodes.h:605
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2706
#define NodeSetTag(nodeptr, t)
Definition: nodes.h:585
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2734
int location
Definition: primnodes.h:1235
char * pstrdup(const char *in)
Definition: mcxt.c:1299
static Node * transformAExprNullIf(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1020
RowCompareType rctype
Definition: primnodes.h:1116
List * opfamilies
Definition: primnodes.h:1118
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:709
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:94
#define llast(l)
Definition: pg_list.h:194
Oid type
Definition: primnodes.h:1233
static Node * transformSubLink(ParseState *pstate, SubLink *sublink)
Definition: parse_expr.c:1678
List * list_truncate(List *list, int new_size)
Definition: list.c:600
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:157
ParamKind paramkind
Definition: primnodes.h:267
Definition: nodes.h:536
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
#define strVal(v)
Definition: value.h:65
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
int errcode(int sqlerrcode)
Definition: elog.c:698
void errorMissingColumn(ParseState *pstate, const char *relname, const char *colname, int location)
Definition: value.h:51
List * p_multiassign_exprs
Definition: parse_node.h:198
Oid array_typeid
Definition: primnodes.h:1034
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:2944
String * makeString(char *str)
Definition: value.c:51
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:193
TypeName * typeName
Definition: parsenodes.h:789
int location
Definition: parsenodes.h:383
Oid casetype
Definition: primnodes.h:976
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
List * arg_names
Definition: primnodes.h:1230
Definition: primnodes.h:186
static Node * transformBooleanTest(ParseState *pstate, BooleanTest *b)
Definition: parse_expr.c:2415
static Node * transformXmlExpr(ParseState *pstate, XmlExpr *x)
Definition: parse_expr.c:2243
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define OidIsValid(objectId)
Definition: c.h:710
ParseExprKind
Definition: parse_node.h:38
List * agg_order
Definition: parsenodes.h:375
#define lsecond(l)
Definition: pg_list.h:179
static Node * transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
Definition: parse_expr.c:2102
int32 typeMod
Definition: primnodes.h:1019
int location
Definition: primnodes.h:627
int location
Definition: primnodes.h:992
SQLValueFunctionOp op
Definition: primnodes.h:1189
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2698
const char * ParseExprKindName(ParseExprKind exprKind)
Definition: parse_expr.c:3001
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:221
signed int int32
Definition: c.h:429
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:369
List * targetList
Definition: parsenodes.h:150
int location
Definition: parsenodes.h:322
int location
Definition: parsenodes.h:248
int location
Definition: parsenodes.h:467
#define list_make1(x1)
Definition: pg_list.h:206
void assign_expr_collations(ParseState *pstate, Node *expr)
RangeTblEntry * p_rte
Definition: parse_node.h:268
bool isnull
Definition: parsenodes.h:310
Oid opresulttype
Definition: primnodes.h:544
bool resjunk
Definition: primnodes.h:1462
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
NameData attname
Definition: pg_attribute.h:41
List * colnames
Definition: primnodes.h:1084
Oid paramcollid
Definition: primnodes.h:271
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:90
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
static Node * transformBoolExpr(ParseState *pstate, BoolExpr *a)
Definition: parse_expr.c:1309
int location
Definition: primnodes.h:272
char * FigureColname(Node *node)
List * args
Definition: primnodes.h:1132
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
BoolExprType boolop
Definition: primnodes.h:625
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:357
Expr * arg
Definition: primnodes.h:1265
static Node * transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
Definition: parse_expr.c:1919
#define lfirst_node(type, lc)
Definition: pg_list.h:172
static Node * transformAExprOpAll(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:955
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2113
int location
Definition: primnodes.h:201
static Node * transformFuncCall(ParseState *pstate, FuncCall *fn)
Definition: parse_expr.c:1345
int location
Definition: primnodes.h:1085
char * c
List * elements
Definition: parsenodes.h:439
Var * makeWholeRowVar(RangeTblEntry *rte, int varno, Index varlevelsup, bool allowScalar)
Definition: makefuncs.c:133
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:222
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2036
int location
Definition: primnodes.h:1153
void check_stack_depth(void)
Definition: postgres.c:3469
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
Definition: parse_node.c:248
Node * rexpr
Definition: parsenodes.h:288
int errdetail(const char *fmt,...)
Definition: elog.c:1042
Expr * arg
Definition: primnodes.h:1288
static Node * transformWholeRowRef(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location)
Definition: parse_expr.c:2507
AttrNumber resno
Definition: primnodes.h:1456
static Node * transformParamRef(ParseState *pstate, ParamRef *pref)
Definition: parse_expr.c:823
int location
Definition: parsenodes.h:289
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
List * list_delete_last(List *list)
Definition: list.c:892
char * cursor_name
Definition: primnodes.h:1364
List * elements
Definition: primnodes.h:1037
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2602
Node * lexpr
Definition: parsenodes.h:287
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
static Node * transformAExprOp(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:860
Definition: ltree.h:42
XmlOptionType xmloption
Definition: parsenodes.h:787
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:336
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:376
Definition: nodes.h:156
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
XmlExprOp op
Definition: primnodes.h:1227
char * NameListToString(List *names)
Definition: namespace.c:3147
static Node * transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
Definition: parse_expr.c:1390
static Node * transformCollateClause(ParseState *pstate, CollateClause *c)
Definition: parse_expr.c:2658
List * args
Definition: primnodes.h:979
bool Transform_null_equals
Definition: parse_expr.c:42
List * get_op_btree_interpretation(Oid opno)
Definition: lsyscache.c:599
int location
Definition: primnodes.h:1039
BoolTestType booltesttype
Definition: primnodes.h:1289
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
Definition: parse_target.c:220
Node * p_last_srf
Definition: parse_node.h:214
Oid MyDatabaseId
Definition: globals.c:88
Node * transformGroupingFunc(ParseState *pstate, GroupingFunc *p)
Definition: parse_agg.c:247
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
TypeName * typeName
Definition: parsenodes.h:321
static Node * make_row_distinct_op(ParseState *pstate, List *opname, RowExpr *lrow, RowExpr *rrow, int location)
Definition: parse_expr.c:2900
int number
Definition: parsenodes.h:257
ParseExprKind p_expr_kind
Definition: parse_node.h:196
NullTestType nulltesttype
Definition: primnodes.h:1266
int32 typmod
Definition: primnodes.h:1234
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310
Oid LookupCollation(ParseState *pstate, List *collnames, int location)
Definition: parse_type.c:517
#define InvalidOid
Definition: postgres_ext.h:36
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition: analyze.c:186
List * named_args
Definition: primnodes.h:1229
#define ereport(elevel,...)
Definition: elog.h:157
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
RowCompareType
Definition: primnodes.h:1102
static void * fn(void *arg)
CmdType commandType
Definition: parsenodes.h:120
List * args
Definition: primnodes.h:1231
int32 paramtypmod
Definition: primnodes.h:270
int location
Definition: parsenodes.h:258
List * lcons(void *datum, List *list)
Definition: list.c:468
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
#define makeNode(_type_)
Definition: nodes.h:584
Expr * arg
Definition: primnodes.h:524
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
int location
Definition: parsenodes.h:311
List * indirection
Definition: parsenodes.h:430
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:104
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:431
Expr * expr
Definition: primnodes.h:1455
XmlOptionType xmloption
Definition: primnodes.h:1232
int paramid
Definition: primnodes.h:268
#define ISCOMPLEX(typeid)
Definition: parse_type.h:58
#define lfourth(l)
Definition: pg_list.h:189
int location
Definition: parsenodes.h:228
int location
Definition: primnodes.h:1268
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
List * args
Definition: parsenodes.h:374
Oid row_typeid
Definition: primnodes.h:1069
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
Expr * arg
Definition: primnodes.h:946
int location
Definition: primnodes.h:981
#define type_is_array(typid)
Definition: lsyscache.h:202
static Node * make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
Definition: parse_expr.c:2977
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
List * args
Definition: primnodes.h:626
List * name
Definition: parsenodes.h:286
static Node * transformCaseExpr(ParseState *pstate, CaseExpr *c)
Definition: parse_expr.c:1538
#define InvalidAttrNumber
Definition: attnum.h:23
#define nodeTag(nodeptr)
Definition: nodes.h:541
Oid element_typeid
Definition: primnodes.h:1036
static Node * transformTypeCast(ParseState *pstate, TypeCast *tc)
Definition: parse_expr.c:2574
List * collname
Definition: parsenodes.h:332
e
Definition: preproc-init.c:82
static Node * transformColumnRef(ParseState *pstate, ColumnRef *cref)
Definition: parse_expr.c:439
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
static Node * transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
Definition: parse_expr.c:2372
int parser_coercion_errposition(ParseState *pstate, int coerce_location, Node *input_expr)
#define elog(elevel,...)
Definition: elog.h:232
int i
Oid coalescetype
Definition: primnodes.h:1130
int count_nonjunk_tlist_entries(List *tlist)
Definition: tlist.c:175
static Node * transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
Definition: parse_expr.c:2190
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
Node * expr
Definition: parsenodes.h:788
void * arg
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
#define lthird(l)
Definition: pg_list.h:184
bool argisrow
Definition: primnodes.h:1267
MinMaxOp op
Definition: primnodes.h:1151
Expr * arg
Definition: primnodes.h:978
Oid opno
Definition: primnodes.h:542
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:220
List * funcname
Definition: parsenodes.h:373
Alias * eref
Definition: parsenodes.h:1153
Expr * result
Definition: primnodes.h:991
Node * node
Definition: parsenodes.h:493
bool agg_within_group
Definition: parsenodes.h:378
#define copyObject(obj)
Definition: nodes.h:652
List * args
Definition: primnodes.h:548
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:902
List * inputcollids
Definition: primnodes.h:1119
Expr * defresult
Definition: primnodes.h:980
Expr * expr
Definition: primnodes.h:990
int location
Definition: primnodes.h:948
CoercionForm row_format
Definition: primnodes.h:1083
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3028
Definition: pg_list.h:50
#define snprintf
Definition: port.h:216
#define _(x)
Definition: elog.c:89
Oid paramtype
Definition: primnodes.h:269
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:422
static Node * transformAExprBetween(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1215
List * fields
Definition: parsenodes.h:247
Node * colNameToVar(ParseState *pstate, const char *colname, bool localonly, int location)
bool opretset
Definition: primnodes.h:545
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747
Node * arg
Definition: parsenodes.h:320
static Node * transformAExprIn(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1062
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)