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