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