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