PostgreSQL Source Code  git master
parse_expr.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "parser/analyze.h"
#include "parser/parse_agg.h"
#include "parser/parse_clause.h"
#include "parser/parse_coerce.h"
#include "parser/parse_collate.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
#include "parser/parse_oper.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/lsyscache.h"
#include "utils/timestamp.h"
#include "utils/xml.h"
Include dependency graph for parse_expr.c:

Go to the source code of this file.

Macros

#define PREC_GROUP_POSTFIX_IS   1 /* postfix IS tests (NullTest, etc) */
 
#define PREC_GROUP_INFIX_IS   2 /* infix IS (IS DISTINCT FROM, etc) */
 
#define PREC_GROUP_LESS   3 /* < > */
 
#define PREC_GROUP_EQUAL   4 /* = */
 
#define PREC_GROUP_LESS_EQUAL   5 /* <= >= <> */
 
#define PREC_GROUP_LIKE   6 /* LIKE ILIKE SIMILAR */
 
#define PREC_GROUP_BETWEEN   7 /* BETWEEN */
 
#define PREC_GROUP_IN   8 /* IN */
 
#define PREC_GROUP_NOT_LIKE   9 /* NOT LIKE/ILIKE/SIMILAR */
 
#define PREC_GROUP_NOT_BETWEEN   10 /* NOT BETWEEN */
 
#define PREC_GROUP_NOT_IN   11 /* NOT IN */
 
#define PREC_GROUP_POSTFIX_OP   12 /* generic postfix operators */
 
#define PREC_GROUP_INFIX_OP   13 /* generic infix operators */
 
#define PREC_GROUP_PREFIX_OP   14 /* generic prefix operators */
 

Functions

static NodetransformExprRecurse (ParseState *pstate, Node *expr)
 
static NodetransformParamRef (ParseState *pstate, ParamRef *pref)
 
static NodetransformAExprOp (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprOpAny (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprOpAll (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprDistinct (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprNullIf (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprOf (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprIn (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprBetween (ParseState *pstate, A_Expr *a)
 
static NodetransformBoolExpr (ParseState *pstate, BoolExpr *a)
 
static NodetransformFuncCall (ParseState *pstate, FuncCall *fn)
 
static NodetransformMultiAssignRef (ParseState *pstate, MultiAssignRef *maref)
 
static NodetransformCaseExpr (ParseState *pstate, CaseExpr *c)
 
static NodetransformSubLink (ParseState *pstate, SubLink *sublink)
 
static NodetransformArrayExpr (ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
 
static NodetransformRowExpr (ParseState *pstate, RowExpr *r, bool allowDefault)
 
static NodetransformCoalesceExpr (ParseState *pstate, CoalesceExpr *c)
 
static NodetransformMinMaxExpr (ParseState *pstate, MinMaxExpr *m)
 
static NodetransformSQLValueFunction (ParseState *pstate, SQLValueFunction *svf)
 
static NodetransformXmlExpr (ParseState *pstate, XmlExpr *x)
 
static NodetransformXmlSerialize (ParseState *pstate, XmlSerialize *xs)
 
static NodetransformBooleanTest (ParseState *pstate, BooleanTest *b)
 
static NodetransformCurrentOfExpr (ParseState *pstate, CurrentOfExpr *cexpr)
 
static NodetransformColumnRef (ParseState *pstate, ColumnRef *cref)
 
static NodetransformWholeRowRef (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location)
 
static NodetransformIndirection (ParseState *pstate, A_Indirection *ind)
 
static NodetransformTypeCast (ParseState *pstate, TypeCast *tc)
 
static NodetransformCollateClause (ParseState *pstate, CollateClause *c)
 
static Nodemake_row_comparison_op (ParseState *pstate, List *opname, List *largs, List *rargs, int location)
 
static Nodemake_row_distinct_op (ParseState *pstate, List *opname, RowExpr *lrow, RowExpr *rrow, int location)
 
static Exprmake_distinct_op (ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
 
static Nodemake_nulltest_from_distinct (ParseState *pstate, A_Expr *distincta, Node *arg)
 
static int operator_precedence_group (Node *node, const char **nodename)
 
static void emit_precedence_warnings (ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
 
NodetransformExpr (ParseState *pstate, Node *expr, ParseExprKind exprKind)
 
static void unknown_attribute (ParseState *pstate, Node *relref, const char *attname, int location)
 
static bool exprIsNullConstant (Node *arg)
 
const char * ParseExprKindName (ParseExprKind exprKind)
 

Variables

bool operator_precedence_warning = false
 
bool Transform_null_equals = false
 
static const int oldprecedence_l []
 
static const int oldprecedence_r []
 

Macro Definition Documentation

◆ PREC_GROUP_BETWEEN

#define PREC_GROUP_BETWEEN   7 /* BETWEEN */

Definition at line 55 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_EQUAL

#define PREC_GROUP_EQUAL   4 /* = */

Definition at line 52 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_IN

#define PREC_GROUP_IN   8 /* IN */

◆ PREC_GROUP_INFIX_IS

#define PREC_GROUP_INFIX_IS   2 /* infix IS (IS DISTINCT FROM, etc) */

Definition at line 50 of file parse_expr.c.

Referenced by operator_precedence_group(), and transformAExprDistinct().

◆ PREC_GROUP_INFIX_OP

#define PREC_GROUP_INFIX_OP   13 /* generic infix operators */

Definition at line 61 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_LESS

#define PREC_GROUP_LESS   3 /* < > */

Definition at line 51 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_LESS_EQUAL

#define PREC_GROUP_LESS_EQUAL   5 /* <= >= <> */

Definition at line 53 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_LIKE

#define PREC_GROUP_LIKE   6 /* LIKE ILIKE SIMILAR */

Definition at line 54 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_NOT_BETWEEN

#define PREC_GROUP_NOT_BETWEEN   10 /* NOT BETWEEN */

Definition at line 58 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_NOT_IN

#define PREC_GROUP_NOT_IN   11 /* NOT IN */

◆ PREC_GROUP_NOT_LIKE

#define PREC_GROUP_NOT_LIKE   9 /* NOT LIKE/ILIKE/SIMILAR */

Definition at line 57 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_POSTFIX_IS

#define PREC_GROUP_POSTFIX_IS   1 /* postfix IS tests (NullTest, etc) */

◆ PREC_GROUP_POSTFIX_OP

#define PREC_GROUP_POSTFIX_OP   12 /* generic postfix operators */

◆ PREC_GROUP_PREFIX_OP

#define PREC_GROUP_PREFIX_OP   14 /* generic prefix operators */

Definition at line 62 of file parse_expr.c.

Referenced by emit_precedence_warnings(), and operator_precedence_group().

Function Documentation

◆ emit_precedence_warnings()

static void emit_precedence_warnings ( ParseState pstate,
int  opgroup,
const char *  opname,
Node lchild,
Node rchild,
int  location 
)
static

Definition at line 3421 of file parse_expr.c.

References Assert, ereport, errmsg(), oldprecedence_l, oldprecedence_r, operator_precedence_group(), parser_errposition(), PREC_GROUP_IN, PREC_GROUP_NOT_IN, PREC_GROUP_POSTFIX_IS, PREC_GROUP_POSTFIX_OP, PREC_GROUP_PREFIX_OP, and WARNING.

Referenced by transformAExprBetween(), transformAExprDistinct(), transformAExprIn(), transformAExprOf(), transformAExprOp(), transformAExprOpAll(), transformAExprOpAny(), transformBooleanTest(), transformExprRecurse(), transformSubLink(), and transformXmlExpr().

3425 {
3426  int cgroup;
3427  const char *copname;
3428 
3429  Assert(opgroup > 0);
3430 
3431  /*
3432  * Complain if left child, which should be same or higher precedence
3433  * according to current rules, used to be lower precedence.
3434  *
3435  * Exception to precedence rules: if left child is IN or NOT IN or a
3436  * postfix operator, the grouping is syntactically forced regardless of
3437  * precedence.
3438  */
3439  cgroup = operator_precedence_group(lchild, &copname);
3440  if (cgroup > 0)
3441  {
3442  if (oldprecedence_l[cgroup] < oldprecedence_r[opgroup] &&
3443  cgroup != PREC_GROUP_IN &&
3444  cgroup != PREC_GROUP_NOT_IN &&
3445  cgroup != PREC_GROUP_POSTFIX_OP &&
3446  cgroup != PREC_GROUP_POSTFIX_IS)
3447  ereport(WARNING,
3448  (errmsg("operator precedence change: %s is now lower precedence than %s",
3449  opname, copname),
3450  parser_errposition(pstate, location)));
3451  }
3452 
3453  /*
3454  * Complain if right child, which should be higher precedence according to
3455  * current rules, used to be same or lower precedence.
3456  *
3457  * Exception to precedence rules: if right child is a prefix operator, the
3458  * grouping is syntactically forced regardless of precedence.
3459  */
3460  cgroup = operator_precedence_group(rchild, &copname);
3461  if (cgroup > 0)
3462  {
3463  if (oldprecedence_r[cgroup] <= oldprecedence_l[opgroup] &&
3464  cgroup != PREC_GROUP_PREFIX_OP)
3465  ereport(WARNING,
3466  (errmsg("operator precedence change: %s is now lower precedence than %s",
3467  opname, copname),
3468  parser_errposition(pstate, location)));
3469  }
3470 }
#define PREC_GROUP_PREFIX_OP
Definition: parse_expr.c:62
#define PREC_GROUP_POSTFIX_IS
Definition: parse_expr.c:49
static int operator_precedence_group(Node *node, const char **nodename)
Definition: parse_expr.c:3178
#define PREC_GROUP_IN
Definition: parse_expr.c:56
#define PREC_GROUP_NOT_IN
Definition: parse_expr.c:59
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
static const int oldprecedence_l[]
Definition: parse_expr.c:83
int errmsg(const char *fmt,...)
Definition: elog.c:824
static const int oldprecedence_r[]
Definition: parse_expr.c:86
#define PREC_GROUP_POSTFIX_OP
Definition: parse_expr.c:60

◆ exprIsNullConstant()

static bool exprIsNullConstant ( Node arg)
static

Definition at line 914 of file parse_expr.c.

References IsA, T_Null, Value::type, and A_Const::val.

Referenced by transformAExprDistinct(), and transformAExprOp().

915 {
916  if (arg && IsA(arg, A_Const))
917  {
918  A_Const *con = (A_Const *) arg;
919 
920  if (con->val.type == T_Null)
921  return true;
922  }
923  return false;
924 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Definition: nodes.h:295
Value val
Definition: parsenodes.h:289
NodeTag type
Definition: value.h:44

◆ make_distinct_op()

static Expr * make_distinct_op ( ParseState pstate,
List opname,
Node ltree,
Node rtree,
int  location 
)
static

Definition at line 3119 of file parse_expr.c.

References ereport, errcode(), errmsg(), ERROR, make_op(), NodeSetTag, ParseState::p_last_srf, parser_errposition(), and T_DistinctExpr.

Referenced by make_row_distinct_op(), and transformAExprDistinct().

3121 {
3122  Expr *result;
3123 
3124  result = make_op(pstate, opname, ltree, rtree,
3125  pstate->p_last_srf, location);
3126  if (((OpExpr *) result)->opresulttype != BOOLOID)
3127  ereport(ERROR,
3128  (errcode(ERRCODE_DATATYPE_MISMATCH),
3129  errmsg("IS DISTINCT FROM requires = operator to yield boolean"),
3130  parser_errposition(pstate, location)));
3131  if (((OpExpr *) result)->opretset)
3132  ereport(ERROR,
3133  (errcode(ERRCODE_DATATYPE_MISMATCH),
3134  /* translator: %s is name of a SQL construct, eg NULLIF */
3135  errmsg("%s must not return a set", "IS DISTINCT FROM"),
3136  parser_errposition(pstate, location)));
3137 
3138  /*
3139  * We rely on DistinctExpr and OpExpr being same struct
3140  */
3141  NodeSetTag(result, T_DistinctExpr);
3142 
3143  return result;
3144 }
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:747
#define NodeSetTag(nodeptr, t)
Definition: nodes.h:578
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
Node * p_last_srf
Definition: parse_node.h:212
#define ereport(elevel,...)
Definition: elog.h:144
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ make_nulltest_from_distinct()

static Node * make_nulltest_from_distinct ( ParseState pstate,
A_Expr distincta,
Node arg 
)
static

Definition at line 3152 of file parse_expr.c.

References AEXPR_NOT_DISTINCT, NullTest::arg, NullTest::argisrow, IS_NOT_NULL, IS_NULL, A_Expr::kind, A_Expr::location, NullTest::location, makeNode, NullTest::nulltesttype, and transformExprRecurse().

Referenced by transformAExprDistinct().

3153 {
3154  NullTest *nt = makeNode(NullTest);
3155 
3156  nt->arg = (Expr *) transformExprRecurse(pstate, arg);
3157  /* the argument can be any type, so don't coerce it */
3158  if (distincta->kind == AEXPR_NOT_DISTINCT)
3159  nt->nulltesttype = IS_NULL;
3160  else
3161  nt->nulltesttype = IS_NOT_NULL;
3162  /* argisrow = false is correct whether or not arg is composite */
3163  nt->argisrow = false;
3164  nt->location = distincta->location;
3165  return (Node *) nt;
3166 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
A_Expr_Kind kind
Definition: parsenodes.h:276
Definition: nodes.h:529
Expr * arg
Definition: primnodes.h:1219
int location
Definition: parsenodes.h:280
NullTestType nulltesttype
Definition: primnodes.h:1220
#define makeNode(_type_)
Definition: nodes.h:577
int location
Definition: primnodes.h:1222
bool argisrow
Definition: primnodes.h:1221

◆ make_row_comparison_op()

static Node * make_row_comparison_op ( ParseState pstate,
List opname,
List largs,
List rargs,
int  location 
)
static

Definition at line 2873 of file parse_expr.c.

References AND_EXPR, OpExpr::args, bms_add_member(), bms_first_member(), bms_int_members(), castNode, cmp(), ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, expression_returns_set(), forboth, format_type_be(), get_op_btree_interpretation(), i, RowCompareExpr::inputcollids, InvalidOid, lappend(), lappend_oid(), RowCompareExpr::largs, lfirst, linitial, list_length(), llast, lsecond, make_op(), makeBoolExpr(), makeNode, NIL, OidIsValid, RowCompareExpr::opfamilies, OpBtreeInterpretation::opfamily_id, OpExpr::opno, RowCompareExpr::opnos, OpExpr::opresulttype, OR_EXPR, ParseState::p_last_srf, palloc(), parser_errposition(), RowCompareExpr::rargs, RowCompareExpr::rctype, ROWCOMPARE_EQ, ROWCOMPARE_NE, OpBtreeInterpretation::strategy, and strVal.

Referenced by transformAExprIn(), transformAExprOp(), and transformSubLink().

2875 {
2876  RowCompareExpr *rcexpr;
2877  RowCompareType rctype;
2878  List *opexprs;
2879  List *opnos;
2880  List *opfamilies;
2881  ListCell *l,
2882  *r;
2883  List **opinfo_lists;
2884  Bitmapset *strats;
2885  int nopers;
2886  int i;
2887 
2888  nopers = list_length(largs);
2889  if (nopers != list_length(rargs))
2890  ereport(ERROR,
2891  (errcode(ERRCODE_SYNTAX_ERROR),
2892  errmsg("unequal number of entries in row expressions"),
2893  parser_errposition(pstate, location)));
2894 
2895  /*
2896  * We can't compare zero-length rows because there is no principled basis
2897  * for figuring out what the operator is.
2898  */
2899  if (nopers == 0)
2900  ereport(ERROR,
2901  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2902  errmsg("cannot compare rows of zero length"),
2903  parser_errposition(pstate, location)));
2904 
2905  /*
2906  * Identify all the pairwise operators, using make_op so that behavior is
2907  * the same as in the simple scalar case.
2908  */
2909  opexprs = NIL;
2910  forboth(l, largs, r, rargs)
2911  {
2912  Node *larg = (Node *) lfirst(l);
2913  Node *rarg = (Node *) lfirst(r);
2914  OpExpr *cmp;
2915 
2916  cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
2917  pstate->p_last_srf, location));
2918 
2919  /*
2920  * We don't use coerce_to_boolean here because we insist on the
2921  * operator yielding boolean directly, not via coercion. If it
2922  * doesn't yield bool it won't be in any index opfamilies...
2923  */
2924  if (cmp->opresulttype != BOOLOID)
2925  ereport(ERROR,
2926  (errcode(ERRCODE_DATATYPE_MISMATCH),
2927  errmsg("row comparison operator must yield type boolean, "
2928  "not type %s",
2930  parser_errposition(pstate, location)));
2931  if (expression_returns_set((Node *) cmp))
2932  ereport(ERROR,
2933  (errcode(ERRCODE_DATATYPE_MISMATCH),
2934  errmsg("row comparison operator must not return a set"),
2935  parser_errposition(pstate, location)));
2936  opexprs = lappend(opexprs, cmp);
2937  }
2938 
2939  /*
2940  * If rows are length 1, just return the single operator. In this case we
2941  * don't insist on identifying btree semantics for the operator (but we
2942  * still require it to return boolean).
2943  */
2944  if (nopers == 1)
2945  return (Node *) linitial(opexprs);
2946 
2947  /*
2948  * Now we must determine which row comparison semantics (= <> < <= > >=)
2949  * apply to this set of operators. We look for btree opfamilies
2950  * containing the operators, and see which interpretations (strategy
2951  * numbers) exist for each operator.
2952  */
2953  opinfo_lists = (List **) palloc(nopers * sizeof(List *));
2954  strats = NULL;
2955  i = 0;
2956  foreach(l, opexprs)
2957  {
2958  Oid opno = ((OpExpr *) lfirst(l))->opno;
2959  Bitmapset *this_strats;
2960  ListCell *j;
2961 
2962  opinfo_lists[i] = get_op_btree_interpretation(opno);
2963 
2964  /*
2965  * convert strategy numbers into a Bitmapset to make the intersection
2966  * calculation easy.
2967  */
2968  this_strats = NULL;
2969  foreach(j, opinfo_lists[i])
2970  {
2971  OpBtreeInterpretation *opinfo = lfirst(j);
2972 
2973  this_strats = bms_add_member(this_strats, opinfo->strategy);
2974  }
2975  if (i == 0)
2976  strats = this_strats;
2977  else
2978  strats = bms_int_members(strats, this_strats);
2979  i++;
2980  }
2981 
2982  /*
2983  * If there are multiple common interpretations, we may use any one of
2984  * them ... this coding arbitrarily picks the lowest btree strategy
2985  * number.
2986  */
2987  i = bms_first_member(strats);
2988  if (i < 0)
2989  {
2990  /* No common interpretation, so fail */
2991  ereport(ERROR,
2992  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2993  errmsg("could not determine interpretation of row comparison operator %s",
2994  strVal(llast(opname))),
2995  errhint("Row comparison operators must be associated with btree operator families."),
2996  parser_errposition(pstate, location)));
2997  }
2998  rctype = (RowCompareType) i;
2999 
3000  /*
3001  * For = and <> cases, we just combine the pairwise operators with AND or
3002  * OR respectively.
3003  */
3004  if (rctype == ROWCOMPARE_EQ)
3005  return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
3006  if (rctype == ROWCOMPARE_NE)
3007  return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
3008 
3009  /*
3010  * Otherwise we need to choose exactly which opfamily to associate with
3011  * each operator.
3012  */
3013  opfamilies = NIL;
3014  for (i = 0; i < nopers; i++)
3015  {
3016  Oid opfamily = InvalidOid;
3017  ListCell *j;
3018 
3019  foreach(j, opinfo_lists[i])
3020  {
3021  OpBtreeInterpretation *opinfo = lfirst(j);
3022 
3023  if (opinfo->strategy == rctype)
3024  {
3025  opfamily = opinfo->opfamily_id;
3026  break;
3027  }
3028  }
3029  if (OidIsValid(opfamily))
3030  opfamilies = lappend_oid(opfamilies, opfamily);
3031  else /* should not happen */
3032  ereport(ERROR,
3033  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3034  errmsg("could not determine interpretation of row comparison operator %s",
3035  strVal(llast(opname))),
3036  errdetail("There are multiple equally-plausible candidates."),
3037  parser_errposition(pstate, location)));
3038  }
3039 
3040  /*
3041  * Now deconstruct the OpExprs and create a RowCompareExpr.
3042  *
3043  * Note: can't just reuse the passed largs/rargs lists, because of
3044  * possibility that make_op inserted coercion operations.
3045  */
3046  opnos = NIL;
3047  largs = NIL;
3048  rargs = NIL;
3049  foreach(l, opexprs)
3050  {
3051  OpExpr *cmp = (OpExpr *) lfirst(l);
3052 
3053  opnos = lappend_oid(opnos, cmp->opno);
3054  largs = lappend(largs, linitial(cmp->args));
3055  rargs = lappend(rargs, lsecond(cmp->args));
3056  }
3057 
3058  rcexpr = makeNode(RowCompareExpr);
3059  rcexpr->rctype = rctype;
3060  rcexpr->opnos = opnos;
3061  rcexpr->opfamilies = opfamilies;
3062  rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
3063  rcexpr->largs = largs;
3064  rcexpr->rargs = rargs;
3065 
3066  return (Node *) rcexpr;
3067 }
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:747
#define NIL
Definition: pg_list.h:65
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:996
int errhint(const char *fmt,...)
Definition: elog.c:1071
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
RowCompareType rctype
Definition: primnodes.h:1070
List * opfamilies
Definition: primnodes.h:1072
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:669
#define llast(l)
Definition: pg_list.h:215
Definition: nodes.h:529
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:610
char * format_type_be(Oid type_oid)
Definition: format_type.c:327
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
#define OidIsValid(objectId)
Definition: c.h:644
#define lsecond(l)
Definition: pg_list.h:200
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:367
Oid opresulttype
Definition: primnodes.h:518
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:957
List * lappend(List *list, void *datum)
Definition: list.c:321
List * get_op_btree_interpretation(Oid opno)
Definition: lsyscache.c:599
Node * p_last_srf
Definition: parse_node.h:212
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
RowCompareType
Definition: primnodes.h:1056
#define makeNode(_type_)
Definition: nodes.h:577
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
Oid opno
Definition: primnodes.h:516
List * args
Definition: primnodes.h:522
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:902
List * inputcollids
Definition: primnodes.h:1073
Definition: pg_list.h:50
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ make_row_distinct_op()

static Node * make_row_distinct_op ( ParseState pstate,
List opname,
RowExpr lrow,
RowExpr rrow,
int  location 
)
static

Definition at line 3075 of file parse_expr.c.

References RowExpr::args, cmp(), ereport, errcode(), errmsg(), ERROR, forboth, lfirst, list_length(), list_make2, make_distinct_op(), makeBoolConst(), makeBoolExpr(), OR_EXPR, and parser_errposition().

Referenced by transformAExprDistinct().

3078 {
3079  Node *result = NULL;
3080  List *largs = lrow->args;
3081  List *rargs = rrow->args;
3082  ListCell *l,
3083  *r;
3084 
3085  if (list_length(largs) != list_length(rargs))
3086  ereport(ERROR,
3087  (errcode(ERRCODE_SYNTAX_ERROR),
3088  errmsg("unequal number of entries in row expressions"),
3089  parser_errposition(pstate, location)));
3090 
3091  forboth(l, largs, r, rargs)
3092  {
3093  Node *larg = (Node *) lfirst(l);
3094  Node *rarg = (Node *) lfirst(r);
3095  Node *cmp;
3096 
3097  cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
3098  if (result == NULL)
3099  result = cmp;
3100  else
3101  result = (Node *) makeBoolExpr(OR_EXPR,
3102  list_make2(result, cmp),
3103  location);
3104  }
3105 
3106  if (result == NULL)
3107  {
3108  /* zero-length rows? Generate constant FALSE */
3109  result = makeBoolConst(false, false);
3110  }
3111 
3112  return result;
3113 }
#define list_make2(x1, x2)
Definition: pg_list.h:229
List * args
Definition: primnodes.h:1022
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
Definition: nodes.h:529
int errcode(int sqlerrcode)
Definition: elog.c:610
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:3119
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:367
#define ERROR
Definition: elog.h:43
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:355
#define ereport(elevel,...)
Definition: elog.h:144
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:824
Definition: pg_list.h:50
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ operator_precedence_group()

static int operator_precedence_group ( Node node,
const char **  nodename 
)
static

Definition at line 3178 of file parse_expr.c.

References AEXPR_BETWEEN, AEXPR_BETWEEN_SYM, AEXPR_DISTINCT, AEXPR_ILIKE, AEXPR_IN, AEXPR_LIKE, AEXPR_NOT_BETWEEN, AEXPR_NOT_BETWEEN_SYM, AEXPR_NOT_DISTINCT, AEXPR_OF, AEXPR_OP, AEXPR_OP_ALL, AEXPR_OP_ANY, AEXPR_SIMILAR, ALL_SUBLINK, ANY_SUBLINK, BoolExpr::args, Assert, BoolExpr::boolop, IS_DOCUMENT, IsA, A_Expr::kind, A_Expr::lexpr, linitial, list_length(), llast, BoolExpr::location, SubLink::location, XmlExpr::location, A_Expr::name, NIL, NOT_EXPR, XmlExpr::op, SubLink::operName, PREC_GROUP_BETWEEN, PREC_GROUP_EQUAL, PREC_GROUP_IN, PREC_GROUP_INFIX_IS, PREC_GROUP_INFIX_OP, PREC_GROUP_LESS, PREC_GROUP_LESS_EQUAL, PREC_GROUP_LIKE, PREC_GROUP_NOT_BETWEEN, PREC_GROUP_NOT_IN, PREC_GROUP_NOT_LIKE, PREC_GROUP_POSTFIX_IS, PREC_GROUP_POSTFIX_OP, PREC_GROUP_PREFIX_OP, A_Expr::rexpr, strVal, and SubLink::subLinkType.

Referenced by emit_precedence_warnings(), transformAExprBetween(), and transformAExprOp().

3179 {
3180  int group = 0;
3181 
3182  *nodename = NULL;
3183  if (node == NULL)
3184  return 0;
3185 
3186  if (IsA(node, A_Expr))
3187  {
3188  A_Expr *aexpr = (A_Expr *) node;
3189 
3190  if (aexpr->kind == AEXPR_OP &&
3191  aexpr->lexpr != NULL &&
3192  aexpr->rexpr != NULL)
3193  {
3194  /* binary operator */
3195  if (list_length(aexpr->name) == 1)
3196  {
3197  *nodename = strVal(linitial(aexpr->name));
3198  /* Ignore if op was always higher priority than IS-tests */
3199  if (strcmp(*nodename, "+") == 0 ||
3200  strcmp(*nodename, "-") == 0 ||
3201  strcmp(*nodename, "*") == 0 ||
3202  strcmp(*nodename, "/") == 0 ||
3203  strcmp(*nodename, "%") == 0 ||
3204  strcmp(*nodename, "^") == 0)
3205  group = 0;
3206  else if (strcmp(*nodename, "<") == 0 ||
3207  strcmp(*nodename, ">") == 0)
3208  group = PREC_GROUP_LESS;
3209  else if (strcmp(*nodename, "=") == 0)
3210  group = PREC_GROUP_EQUAL;
3211  else if (strcmp(*nodename, "<=") == 0 ||
3212  strcmp(*nodename, ">=") == 0 ||
3213  strcmp(*nodename, "<>") == 0)
3214  group = PREC_GROUP_LESS_EQUAL;
3215  else
3216  group = PREC_GROUP_INFIX_OP;
3217  }
3218  else
3219  {
3220  /* schema-qualified operator syntax */
3221  *nodename = "OPERATOR()";
3222  group = PREC_GROUP_INFIX_OP;
3223  }
3224  }
3225  else if (aexpr->kind == AEXPR_OP &&
3226  aexpr->lexpr == NULL &&
3227  aexpr->rexpr != NULL)
3228  {
3229  /* prefix operator */
3230  if (list_length(aexpr->name) == 1)
3231  {
3232  *nodename = strVal(linitial(aexpr->name));
3233  /* Ignore if op was always higher priority than IS-tests */
3234  if (strcmp(*nodename, "+") == 0 ||
3235  strcmp(*nodename, "-") == 0)
3236  group = 0;
3237  else
3238  group = PREC_GROUP_PREFIX_OP;
3239  }
3240  else
3241  {
3242  /* schema-qualified operator syntax */
3243  *nodename = "OPERATOR()";
3244  group = PREC_GROUP_PREFIX_OP;
3245  }
3246  }
3247  else if (aexpr->kind == AEXPR_OP &&
3248  aexpr->lexpr != NULL &&
3249  aexpr->rexpr == NULL)
3250  {
3251  /* postfix operator */
3252  if (list_length(aexpr->name) == 1)
3253  {
3254  *nodename = strVal(linitial(aexpr->name));
3255  group = PREC_GROUP_POSTFIX_OP;
3256  }
3257  else
3258  {
3259  /* schema-qualified operator syntax */
3260  *nodename = "OPERATOR()";
3261  group = PREC_GROUP_POSTFIX_OP;
3262  }
3263  }
3264  else if (aexpr->kind == AEXPR_OP_ANY ||
3265  aexpr->kind == AEXPR_OP_ALL)
3266  {
3267  *nodename = strVal(llast(aexpr->name));
3268  group = PREC_GROUP_POSTFIX_OP;
3269  }
3270  else if (aexpr->kind == AEXPR_DISTINCT ||
3271  aexpr->kind == AEXPR_NOT_DISTINCT)
3272  {
3273  *nodename = "IS";
3274  group = PREC_GROUP_INFIX_IS;
3275  }
3276  else if (aexpr->kind == AEXPR_OF)
3277  {
3278  *nodename = "IS";
3279  group = PREC_GROUP_POSTFIX_IS;
3280  }
3281  else if (aexpr->kind == AEXPR_IN)
3282  {
3283  *nodename = "IN";
3284  if (strcmp(strVal(linitial(aexpr->name)), "=") == 0)
3285  group = PREC_GROUP_IN;
3286  else
3287  group = PREC_GROUP_NOT_IN;
3288  }
3289  else if (aexpr->kind == AEXPR_LIKE)
3290  {
3291  *nodename = "LIKE";
3292  if (strcmp(strVal(linitial(aexpr->name)), "~~") == 0)
3293  group = PREC_GROUP_LIKE;
3294  else
3295  group = PREC_GROUP_NOT_LIKE;
3296  }
3297  else if (aexpr->kind == AEXPR_ILIKE)
3298  {
3299  *nodename = "ILIKE";
3300  if (strcmp(strVal(linitial(aexpr->name)), "~~*") == 0)
3301  group = PREC_GROUP_LIKE;
3302  else
3303  group = PREC_GROUP_NOT_LIKE;
3304  }
3305  else if (aexpr->kind == AEXPR_SIMILAR)
3306  {
3307  *nodename = "SIMILAR";
3308  if (strcmp(strVal(linitial(aexpr->name)), "~") == 0)
3309  group = PREC_GROUP_LIKE;
3310  else
3311  group = PREC_GROUP_NOT_LIKE;
3312  }
3313  else if (aexpr->kind == AEXPR_BETWEEN ||
3314  aexpr->kind == AEXPR_BETWEEN_SYM)
3315  {
3316  Assert(list_length(aexpr->name) == 1);
3317  *nodename = strVal(linitial(aexpr->name));
3318  group = PREC_GROUP_BETWEEN;
3319  }
3320  else if (aexpr->kind == AEXPR_NOT_BETWEEN ||
3321  aexpr->kind == AEXPR_NOT_BETWEEN_SYM)
3322  {
3323  Assert(list_length(aexpr->name) == 1);
3324  *nodename = strVal(linitial(aexpr->name));
3325  group = PREC_GROUP_NOT_BETWEEN;
3326  }
3327  }
3328  else if (IsA(node, NullTest) ||
3329  IsA(node, BooleanTest))
3330  {
3331  *nodename = "IS";
3332  group = PREC_GROUP_POSTFIX_IS;
3333  }
3334  else if (IsA(node, XmlExpr))
3335  {
3336  XmlExpr *x = (XmlExpr *) node;
3337 
3338  if (x->op == IS_DOCUMENT)
3339  {
3340  *nodename = "IS";
3341  group = PREC_GROUP_POSTFIX_IS;
3342  }
3343  }
3344  else if (IsA(node, SubLink))
3345  {
3346  SubLink *s = (SubLink *) node;
3347 
3348  if (s->subLinkType == ANY_SUBLINK ||
3349  s->subLinkType == ALL_SUBLINK)
3350  {
3351  if (s->operName == NIL)
3352  {
3353  *nodename = "IN";
3354  group = PREC_GROUP_IN;
3355  }
3356  else
3357  {
3358  *nodename = strVal(llast(s->operName));
3359  group = PREC_GROUP_POSTFIX_OP;
3360  }
3361  }
3362  }
3363  else if (IsA(node, BoolExpr))
3364  {
3365  /*
3366  * Must dig into NOTs to see if it's IS NOT DOCUMENT or NOT IN. This
3367  * opens us to possibly misrecognizing, eg, NOT (x IS DOCUMENT) as a
3368  * problematic construct. We can tell the difference by checking
3369  * whether the parse locations of the two nodes are identical.
3370  *
3371  * Note that when we are comparing the child node to its own children,
3372  * we will not know that it was a NOT. Fortunately, that doesn't
3373  * matter for these cases.
3374  */
3375  BoolExpr *b = (BoolExpr *) node;
3376 
3377  if (b->boolop == NOT_EXPR)
3378  {
3379  Node *child = (Node *) linitial(b->args);
3380 
3381  if (IsA(child, XmlExpr))
3382  {
3383  XmlExpr *x = (XmlExpr *) child;
3384 
3385  if (x->op == IS_DOCUMENT &&
3386  x->location == b->location)
3387  {
3388  *nodename = "IS";
3389  group = PREC_GROUP_POSTFIX_IS;
3390  }
3391  }
3392  else if (IsA(child, SubLink))
3393  {
3394  SubLink *s = (SubLink *) child;
3395 
3396  if (s->subLinkType == ANY_SUBLINK && s->operName == NIL &&
3397  s->location == b->location)
3398  {
3399  *nodename = "IN";
3400  group = PREC_GROUP_NOT_IN;
3401  }
3402  }
3403  }
3404  }
3405  return group;
3406 }
#define NIL
Definition: pg_list.h:65
#define PREC_GROUP_PREFIX_OP
Definition: parse_expr.c:62
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
#define PREC_GROUP_INFIX_OP
Definition: parse_expr.c:61
A_Expr_Kind kind
Definition: parsenodes.h:276
#define PREC_GROUP_POSTFIX_IS
Definition: parse_expr.c:49
int location
Definition: primnodes.h:1189
#define PREC_GROUP_IN
Definition: parse_expr.c:56
#define llast(l)
Definition: pg_list.h:215
Definition: nodes.h:529
#define strVal(v)
Definition: value.h:54
#define PREC_GROUP_INFIX_IS
Definition: parse_expr.c:50
int location
Definition: primnodes.h:584
#define linitial(l)
Definition: pg_list.h:195
#define PREC_GROUP_BETWEEN
Definition: parse_expr.c:55
BoolExprType boolop
Definition: primnodes.h:582
#define PREC_GROUP_NOT_IN
Definition: parse_expr.c:59
Node * rexpr
Definition: parsenodes.h:279
#define PREC_GROUP_NOT_LIKE
Definition: parse_expr.c:57
Node * lexpr
Definition: parsenodes.h:278
#define PREC_GROUP_LIKE
Definition: parse_expr.c:54
XmlExprOp op
Definition: primnodes.h:1181
#define PREC_GROUP_EQUAL
Definition: parse_expr.c:52
#define Assert(condition)
Definition: c.h:738
static int list_length(const List *l)
Definition: pg_list.h:169
List * args
Definition: primnodes.h:583
List * name
Definition: parsenodes.h:277
#define PREC_GROUP_LESS
Definition: parse_expr.c:51
#define PREC_GROUP_LESS_EQUAL
Definition: parse_expr.c:53
#define PREC_GROUP_POSTFIX_OP
Definition: parse_expr.c:60
#define PREC_GROUP_NOT_BETWEEN
Definition: parse_expr.c:58

◆ ParseExprKindName()

const char* ParseExprKindName ( ParseExprKind  exprKind)

Definition at line 3480 of file parse_expr.c.

References EXPR_KIND_ALTER_COL_TRANSFORM, EXPR_KIND_CALL_ARGUMENT, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_COLUMN_DEFAULT, EXPR_KIND_COPY_WHERE, EXPR_KIND_DISTINCT_ON, EXPR_KIND_DOMAIN_CHECK, EXPR_KIND_EXECUTE_PARAMETER, EXPR_KIND_FILTER, EXPR_KIND_FROM_FUNCTION, EXPR_KIND_FROM_SUBSELECT, EXPR_KIND_FUNCTION_DEFAULT, EXPR_KIND_GENERATED_COLUMN, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_INDEX_EXPRESSION, EXPR_KIND_INDEX_PREDICATE, EXPR_KIND_INSERT_TARGET, EXPR_KIND_JOIN_ON, EXPR_KIND_JOIN_USING, EXPR_KIND_LIMIT, EXPR_KIND_NONE, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_OTHER, EXPR_KIND_PARTITION_BOUND, EXPR_KIND_PARTITION_EXPRESSION, EXPR_KIND_POLICY, EXPR_KIND_RETURNING, EXPR_KIND_SELECT_TARGET, EXPR_KIND_TRIGGER_WHEN, EXPR_KIND_UPDATE_SOURCE, EXPR_KIND_UPDATE_TARGET, EXPR_KIND_VALUES, EXPR_KIND_VALUES_SINGLE, EXPR_KIND_WHERE, EXPR_KIND_WINDOW_FRAME_GROUPS, EXPR_KIND_WINDOW_FRAME_RANGE, EXPR_KIND_WINDOW_FRAME_ROWS, EXPR_KIND_WINDOW_ORDER, and EXPR_KIND_WINDOW_PARTITION.

Referenced by check_agglevels_and_constraints(), check_srf_call_placement(), checkTargetlistEntrySQL92(), findTargetlistEntrySQL92(), and transformWindowFuncCall().

3481 {
3482  switch (exprKind)
3483  {
3484  case EXPR_KIND_NONE:
3485  return "invalid expression context";
3486  case EXPR_KIND_OTHER:
3487  return "extension expression";
3488  case EXPR_KIND_JOIN_ON:
3489  return "JOIN/ON";
3490  case EXPR_KIND_JOIN_USING:
3491  return "JOIN/USING";
3493  return "sub-SELECT in FROM";
3495  return "function in FROM";
3496  case EXPR_KIND_WHERE:
3497  return "WHERE";
3498  case EXPR_KIND_POLICY:
3499  return "POLICY";
3500  case EXPR_KIND_HAVING:
3501  return "HAVING";
3502  case EXPR_KIND_FILTER:
3503  return "FILTER";
3505  return "window PARTITION BY";
3507  return "window ORDER BY";
3509  return "window RANGE";
3511  return "window ROWS";
3513  return "window GROUPS";
3515  return "SELECT";
3517  return "INSERT";
3520  return "UPDATE";
3521  case EXPR_KIND_GROUP_BY:
3522  return "GROUP BY";
3523  case EXPR_KIND_ORDER_BY:
3524  return "ORDER BY";
3525  case EXPR_KIND_DISTINCT_ON:
3526  return "DISTINCT ON";
3527  case EXPR_KIND_LIMIT:
3528  return "LIMIT";
3529  case EXPR_KIND_OFFSET:
3530  return "OFFSET";
3531  case EXPR_KIND_RETURNING:
3532  return "RETURNING";
3533  case EXPR_KIND_VALUES:
3535  return "VALUES";
3538  return "CHECK";
3541  return "DEFAULT";
3543  return "index expression";
3545  return "index predicate";
3547  return "USING";
3549  return "EXECUTE";
3551  return "WHEN";
3553  return "partition bound";
3555  return "PARTITION BY";
3557  return "CALL";
3558  case EXPR_KIND_COPY_WHERE:
3559  return "WHERE";
3561  return "GENERATED AS";
3562 
3563  /*
3564  * There is intentionally no default: case here, so that the
3565  * compiler will warn if we add a new ParseExprKind without
3566  * extending this switch. If we do see an unrecognized value at
3567  * runtime, we'll fall through to the "unrecognized" return.
3568  */
3569  }
3570  return "unrecognized expression kind";
3571 }

◆ transformAExprBetween()

static Node * transformAExprBetween ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1377 of file parse_expr.c.

References AEXPR_BETWEEN, AEXPR_BETWEEN_SYM, AEXPR_NOT_BETWEEN, AEXPR_NOT_BETWEEN_SYM, AEXPR_OP, AEXPR_PAREN, AND_EXPR, generate_unaccent_rules::args, Assert, castNode, copyObject, elog, emit_precedence_warnings(), ERROR, A_Expr::kind, A_Expr::lexpr, linitial, list_length(), list_make2, A_Expr::location, lsecond, makeA_Expr(), makeBoolExpr(), makeSimpleA_Expr(), NIL, operator_precedence_group(), operator_precedence_warning, OR_EXPR, A_Expr::rexpr, and transformExprRecurse().

Referenced by transformExprRecurse().

1378 {
1379  Node *aexpr;
1380  Node *bexpr;
1381  Node *cexpr;
1382  Node *result;
1383  Node *sub1;
1384  Node *sub2;
1385  List *args;
1386 
1387  /* Deconstruct A_Expr into three subexprs */
1388  aexpr = a->lexpr;
1389  args = castNode(List, a->rexpr);
1390  Assert(list_length(args) == 2);
1391  bexpr = (Node *) linitial(args);
1392  cexpr = (Node *) lsecond(args);
1393 
1395  {
1396  int opgroup;
1397  const char *opname;
1398 
1399  opgroup = operator_precedence_group((Node *) a, &opname);
1400  emit_precedence_warnings(pstate, opgroup, opname,
1401  aexpr, cexpr,
1402  a->location);
1403  /* We can ignore bexpr thanks to syntactic restrictions */
1404  /* Wrap subexpressions to prevent extra warnings */
1405  aexpr = (Node *) makeA_Expr(AEXPR_PAREN, NIL, aexpr, NULL, -1);
1406  bexpr = (Node *) makeA_Expr(AEXPR_PAREN, NIL, bexpr, NULL, -1);
1407  cexpr = (Node *) makeA_Expr(AEXPR_PAREN, NIL, cexpr, NULL, -1);
1408  }
1409 
1410  /*
1411  * Build the equivalent comparison expression. Make copies of
1412  * multiply-referenced subexpressions for safety. (XXX this is really
1413  * wrong since it results in multiple runtime evaluations of what may be
1414  * volatile expressions ...)
1415  *
1416  * Ideally we would not use hard-wired operators here but instead use
1417  * opclasses. However, mixed data types and other issues make this
1418  * difficult:
1419  * http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
1420  */
1421  switch (a->kind)
1422  {
1423  case AEXPR_BETWEEN:
1424  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1425  aexpr, bexpr,
1426  a->location),
1427  makeSimpleA_Expr(AEXPR_OP, "<=",
1428  copyObject(aexpr), cexpr,
1429  a->location));
1430  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1431  break;
1432  case AEXPR_NOT_BETWEEN:
1433  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1434  aexpr, bexpr,
1435  a->location),
1437  copyObject(aexpr), cexpr,
1438  a->location));
1439  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1440  break;
1441  case AEXPR_BETWEEN_SYM:
1442  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1443  aexpr, bexpr,
1444  a->location),
1445  makeSimpleA_Expr(AEXPR_OP, "<=",
1446  copyObject(aexpr), cexpr,
1447  a->location));
1448  sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1449  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1450  copyObject(aexpr), copyObject(cexpr),
1451  a->location),
1452  makeSimpleA_Expr(AEXPR_OP, "<=",
1453  copyObject(aexpr), copyObject(bexpr),
1454  a->location));
1455  sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1456  args = list_make2(sub1, sub2);
1457  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1458  break;
1459  case AEXPR_NOT_BETWEEN_SYM:
1460  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1461  aexpr, bexpr,
1462  a->location),
1464  copyObject(aexpr), cexpr,
1465  a->location));
1466  sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1467  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1468  copyObject(aexpr), copyObject(cexpr),
1469  a->location),
1471  copyObject(aexpr), copyObject(bexpr),
1472  a->location));
1473  sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1474  args = list_make2(sub1, sub2);
1475  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1476  break;
1477  default:
1478  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
1479  result = NULL; /* keep compiler quiet */
1480  break;
1481  }
1482 
1483  return transformExprRecurse(pstate, result);
1484 }
#define list_make2(x1, x2)
Definition: pg_list.h:229
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
A_Expr_Kind kind
Definition: parsenodes.h:276
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
static int operator_precedence_group(Node *node, const char **nodename)
Definition: parse_expr.c:3178
Definition: nodes.h:529
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
#define lsecond(l)
Definition: pg_list.h:200
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:367
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
A_Expr * makeA_Expr(A_Expr_Kind kind, List *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:30
Node * lexpr
Definition: parsenodes.h:278
bool operator_precedence_warning
Definition: parse_expr.c:42
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3421
#define Assert(condition)
Definition: c.h:738
static int list_length(const List *l)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:214
#define copyObject(obj)
Definition: nodes.h:645
Definition: pg_list.h:50

◆ transformAExprDistinct()

static Node * transformAExprDistinct ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1074 of file parse_expr.c.

References AEXPR_NOT_DISTINCT, emit_precedence_warnings(), exprIsNullConstant(), IsA, A_Expr::kind, A_Expr::lexpr, list_make1, A_Expr::location, make_distinct_op(), make_nulltest_from_distinct(), make_row_distinct_op(), makeBoolExpr(), A_Expr::name, NOT_EXPR, operator_precedence_warning, PREC_GROUP_INFIX_IS, A_Expr::rexpr, and transformExprRecurse().

Referenced by transformExprRecurse().

1075 {
1076  Node *lexpr = a->lexpr;
1077  Node *rexpr = a->rexpr;
1078  Node *result;
1079 
1082  lexpr, rexpr,
1083  a->location);
1084 
1085  /*
1086  * If either input is an undecorated NULL literal, transform to a NullTest
1087  * on the other input. That's simpler to process than a full DistinctExpr,
1088  * and it avoids needing to require that the datatype have an = operator.
1089  */
1090  if (exprIsNullConstant(rexpr))
1091  return make_nulltest_from_distinct(pstate, a, lexpr);
1092  if (exprIsNullConstant(lexpr))
1093  return make_nulltest_from_distinct(pstate, a, rexpr);
1094 
1095  lexpr = transformExprRecurse(pstate, lexpr);
1096  rexpr = transformExprRecurse(pstate, rexpr);
1097 
1098  if (lexpr && IsA(lexpr, RowExpr) &&
1099  rexpr && IsA(rexpr, RowExpr))
1100  {
1101  /* ROW() op ROW() is handled specially */
1102  result = make_row_distinct_op(pstate, a->name,
1103  (RowExpr *) lexpr,
1104  (RowExpr *) rexpr,
1105  a->location);
1106  }
1107  else
1108  {
1109  /* Ordinary scalar operator */
1110  result = (Node *) make_distinct_op(pstate,
1111  a->name,
1112  lexpr,
1113  rexpr,
1114  a->location);
1115  }
1116 
1117  /*
1118  * If it's NOT DISTINCT, we first build a DistinctExpr and then stick a
1119  * NOT on top.
1120  */
1121  if (a->kind == AEXPR_NOT_DISTINCT)
1122  result = (Node *) makeBoolExpr(NOT_EXPR,
1123  list_make1(result),
1124  a->location);
1125 
1126  return result;
1127 }
static bool exprIsNullConstant(Node *arg)
Definition: parse_expr.c:914
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
A_Expr_Kind kind
Definition: parsenodes.h:276
Definition: nodes.h:529
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:3119
#define PREC_GROUP_INFIX_IS
Definition: parse_expr.c:50
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:367
#define list_make1(x1)
Definition: pg_list.h:227
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
Node * lexpr
Definition: parsenodes.h:278
bool operator_precedence_warning
Definition: parse_expr.c:42
static Node * make_row_distinct_op(ParseState *pstate, List *opname, RowExpr *lrow, RowExpr *rrow, int location)
Definition: parse_expr.c:3075
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3421
static Node * make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
Definition: parse_expr.c:3152
List * name
Definition: parsenodes.h:277

◆ transformAExprIn()

static Node * transformAExprIn ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1217 of file parse_expr.c.

References AND_EXPR, generate_unaccent_rules::args, ArrayExpr::array_typeid, cmp(), coerce_to_boolean(), coerce_to_common_type(), contain_vars_of_level(), copyObject, ArrayExpr::element_typeid, ArrayExpr::elements, emit_precedence_warnings(), get_array_type(), InvalidOid, IsA, lappend(), A_Expr::lexpr, lfirst, linitial, list_concat(), list_length(), list_make1, list_make2, A_Expr::location, ArrayExpr::location, make_op(), make_row_comparison_op(), make_scalar_array_op(), makeBoolExpr(), makeNode, ArrayExpr::multidims, A_Expr::name, NIL, OidIsValid, operator_precedence_warning, OR_EXPR, ParseState::p_last_srf, PREC_GROUP_IN, PREC_GROUP_NOT_IN, A_Expr::rexpr, select_common_type(), strVal, and transformExprRecurse().

Referenced by transformExprRecurse().

1218 {
1219  Node *result = NULL;
1220  Node *lexpr;
1221  List *rexprs;
1222  List *rvars;
1223  List *rnonvars;
1224  bool useOr;
1225  ListCell *l;
1226 
1227  /*
1228  * If the operator is <>, combine with AND not OR.
1229  */
1230  if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1231  useOr = false;
1232  else
1233  useOr = true;
1234 
1236  emit_precedence_warnings(pstate,
1237  useOr ? PREC_GROUP_IN : PREC_GROUP_NOT_IN,
1238  "IN",
1239  a->lexpr, NULL,
1240  a->location);
1241 
1242  /*
1243  * We try to generate a ScalarArrayOpExpr from IN/NOT IN, but this is only
1244  * possible if there is a suitable array type available. If not, we fall
1245  * back to a boolean condition tree with multiple copies of the lefthand
1246  * expression. Also, any IN-list items that contain Vars are handled as
1247  * separate boolean conditions, because that gives the planner more scope
1248  * for optimization on such clauses.
1249  *
1250  * First step: transform all the inputs, and detect whether any contain
1251  * Vars.
1252  */
1253  lexpr = transformExprRecurse(pstate, a->lexpr);
1254  rexprs = rvars = rnonvars = NIL;
1255  foreach(l, (List *) a->rexpr)
1256  {
1257  Node *rexpr = transformExprRecurse(pstate, lfirst(l));
1258 
1259  rexprs = lappend(rexprs, rexpr);
1260  if (contain_vars_of_level(rexpr, 0))
1261  rvars = lappend(rvars, rexpr);
1262  else
1263  rnonvars = lappend(rnonvars, rexpr);
1264  }
1265 
1266  /*
1267  * ScalarArrayOpExpr is only going to be useful if there's more than one
1268  * non-Var righthand item.
1269  */
1270  if (list_length(rnonvars) > 1)
1271  {
1272  List *allexprs;
1273  Oid scalar_type;
1274  Oid array_type;
1275 
1276  /*
1277  * Try to select a common type for the array elements. Note that
1278  * since the LHS' type is first in the list, it will be preferred when
1279  * there is doubt (eg, when all the RHS items are unknown literals).
1280  *
1281  * Note: use list_concat here not lcons, to avoid damaging rnonvars.
1282  */
1283  allexprs = list_concat(list_make1(lexpr), rnonvars);
1284  scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1285 
1286  /*
1287  * Do we have an array type to use? Aside from the case where there
1288  * isn't one, we don't risk using ScalarArrayOpExpr when the common
1289  * type is RECORD, because the RowExpr comparison logic below can cope
1290  * with some cases of non-identical row types.
1291  */
1292  if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
1293  array_type = get_array_type(scalar_type);
1294  else
1295  array_type = InvalidOid;
1296  if (array_type != InvalidOid)
1297  {
1298  /*
1299  * OK: coerce all the right-hand non-Var inputs to the common type
1300  * and build an ArrayExpr for them.
1301  */
1302  List *aexprs;
1303  ArrayExpr *newa;
1304 
1305  aexprs = NIL;
1306  foreach(l, rnonvars)
1307  {
1308  Node *rexpr = (Node *) lfirst(l);
1309 
1310  rexpr = coerce_to_common_type(pstate, rexpr,
1311  scalar_type,
1312  "IN");
1313  aexprs = lappend(aexprs, rexpr);
1314  }
1315  newa = makeNode(ArrayExpr);
1316  newa->array_typeid = array_type;
1317  /* array_collid will be set by parse_collate.c */
1318  newa->element_typeid = scalar_type;
1319  newa->elements = aexprs;
1320  newa->multidims = false;
1321  newa->location = -1;
1322 
1323  result = (Node *) make_scalar_array_op(pstate,
1324  a->name,
1325  useOr,
1326  lexpr,
1327  (Node *) newa,
1328  a->location);
1329 
1330  /* Consider only the Vars (if any) in the loop below */
1331  rexprs = rvars;
1332  }
1333  }
1334 
1335  /*
1336  * Must do it the hard way, ie, with a boolean expression tree.
1337  */
1338  foreach(l, rexprs)
1339  {
1340  Node *rexpr = (Node *) lfirst(l);
1341  Node *cmp;
1342 
1343  if (IsA(lexpr, RowExpr) &&
1344  IsA(rexpr, RowExpr))
1345  {
1346  /* ROW() op ROW() is handled specially */
1347  cmp = make_row_comparison_op(pstate,
1348  a->name,
1349  copyObject(((RowExpr *) lexpr)->args),
1350  ((RowExpr *) rexpr)->args,
1351  a->location);
1352  }
1353  else
1354  {
1355  /* Ordinary scalar operator */
1356  cmp = (Node *) make_op(pstate,
1357  a->name,
1358  copyObject(lexpr),
1359  rexpr,
1360  pstate->p_last_srf,
1361  a->location);
1362  }
1363 
1364  cmp = coerce_to_boolean(pstate, cmp, "IN");
1365  if (result == NULL)
1366  result = cmp;
1367  else
1368  result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
1369  list_make2(result, cmp),
1370  a->location);
1371  }
1372 
1373  return result;
1374 }
#define list_make2(x1, x2)
Definition: pg_list.h:229
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:747
bool multidims
Definition: primnodes.h:992
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:867
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2615
#define PREC_GROUP_IN
Definition: parse_expr.c:56
Definition: nodes.h:529
#define strVal(v)
Definition: value.h:54
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
Oid array_typeid
Definition: primnodes.h:988
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2873
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:367
#define list_make1(x1)
Definition: pg_list.h:227
#define linitial(l)
Definition: pg_list.h:195
#define PREC_GROUP_NOT_IN
Definition: parse_expr.c:59
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
List * elements
Definition: primnodes.h:991
Node * lexpr
Definition: parsenodes.h:278
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:321
int location
Definition: primnodes.h:993
Node * p_last_srf
Definition: parse_node.h:212
bool operator_precedence_warning
Definition: parse_expr.c:42
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3421
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:577
#define lfirst(lc)
Definition: pg_list.h:190
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
static int list_length(const List *l)
Definition: pg_list.h:169
List * name
Definition: parsenodes.h:277
Oid element_typeid
Definition: primnodes.h:990
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
#define copyObject(obj)
Definition: nodes.h:645
Definition: pg_list.h:50
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformAExprNullIf()

static Node * transformAExprNullIf ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1130 of file parse_expr.c.

References OpExpr::args, ereport, errcode(), errmsg(), ERROR, exprType(), A_Expr::lexpr, linitial, A_Expr::location, make_op(), A_Expr::name, NodeSetTag, OpExpr::opresulttype, OpExpr::opretset, ParseState::p_last_srf, parser_errposition(), A_Expr::rexpr, T_NullIfExpr, and transformExprRecurse().

Referenced by transformExprRecurse().

1131 {
1132  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1133  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1134  OpExpr *result;
1135 
1136  result = (OpExpr *) make_op(pstate,
1137  a->name,
1138  lexpr,
1139  rexpr,
1140  pstate->p_last_srf,
1141  a->location);
1142 
1143  /*
1144  * The comparison operator itself should yield boolean ...
1145  */
1146  if (result->opresulttype != BOOLOID)
1147  ereport(ERROR,
1148  (errcode(ERRCODE_DATATYPE_MISMATCH),
1149  errmsg("NULLIF requires = operator to yield boolean"),
1150  parser_errposition(pstate, a->location)));
1151  if (result->opretset)
1152  ereport(ERROR,
1153  (errcode(ERRCODE_DATATYPE_MISMATCH),
1154  /* translator: %s is name of a SQL construct, eg NULLIF */
1155  errmsg("%s must not return a set", "NULLIF"),
1156  parser_errposition(pstate, a->location)));
1157 
1158  /*
1159  * ... but the NullIfExpr will yield the first operand's type.
1160  */
1161  result->opresulttype = exprType((Node *) linitial(result->args));
1162 
1163  /*
1164  * We rely on NullIfExpr and OpExpr being the same struct
1165  */
1166  NodeSetTag(result, T_NullIfExpr);
1167 
1168  return (Node *) result;
1169 }
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:747
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
#define NodeSetTag(nodeptr, t)
Definition: nodes.h:578
Definition: nodes.h:529
int errcode(int sqlerrcode)
Definition: elog.c:610
Oid opresulttype
Definition: primnodes.h:518
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
Node * lexpr
Definition: parsenodes.h:278
Node * p_last_srf
Definition: parse_node.h:212
#define ereport(elevel,...)
Definition: elog.h:144
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
List * name
Definition: parsenodes.h:277
int errmsg(const char *fmt,...)
Definition: elog.c:824
List * args
Definition: primnodes.h:522
bool opretset
Definition: primnodes.h:519

◆ transformAExprOf()

static Node * transformAExprOf ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1176 of file parse_expr.c.

References emit_precedence_warnings(), exprLocation(), exprType(), A_Expr::lexpr, lfirst, linitial, Const::location, A_Expr::location, makeBoolConst(), A_Expr::name, operator_precedence_warning, PREC_GROUP_POSTFIX_IS, A_Expr::rexpr, strVal, transformExprRecurse(), and typenameTypeId().

Referenced by transformExprRecurse().

1177 {
1178  Node *lexpr = a->lexpr;
1179  Const *result;
1180  ListCell *telem;
1181  Oid ltype,
1182  rtype;
1183  bool matched = false;
1184 
1187  lexpr, NULL,
1188  a->location);
1189 
1190  lexpr = transformExprRecurse(pstate, lexpr);
1191 
1192  ltype = exprType(lexpr);
1193  foreach(telem, (List *) a->rexpr)
1194  {
1195  rtype = typenameTypeId(pstate, lfirst(telem));
1196  matched = (rtype == ltype);
1197  if (matched)
1198  break;
1199  }
1200 
1201  /*
1202  * We have two forms: equals or not equals. Flip the sense of the result
1203  * for not equals.
1204  */
1205  if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1206  matched = (!matched);
1207 
1208  result = (Const *) makeBoolConst(matched, false);
1209 
1210  /* Make the result have the original input's parse location */
1211  result->location = exprLocation((Node *) a);
1212 
1213  return (Node *) result;
1214 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
#define PREC_GROUP_POSTFIX_IS
Definition: parse_expr.c:49
Definition: nodes.h:529
#define strVal(v)
Definition: value.h:54
unsigned int Oid
Definition: postgres_ext.h:31
#define linitial(l)
Definition: pg_list.h:195
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:355
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
Node * lexpr
Definition: parsenodes.h:278
int location
Definition: primnodes.h:221
bool operator_precedence_warning
Definition: parse_expr.c:42
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3421
#define lfirst(lc)
Definition: pg_list.h:190
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
List * name
Definition: parsenodes.h:277
Definition: pg_list.h:50
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition: parse_type.c:291

◆ transformAExprOp()

static Node * transformAExprOp ( ParseState pstate,
A_Expr a 
)
static

Definition at line 927 of file parse_expr.c.

References AEXPR_PAREN, NullTest::arg, generate_unaccent_rules::args, castNode, emit_precedence_warnings(), EXPR_SUBLINK, exprIsNullConstant(), IS_NULL, IsA, A_Expr::lexpr, linitial, list_length(), A_Expr::location, SubLink::location, NullTest::location, make_op(), make_row_comparison_op(), makeNode, A_Expr::name, NullTest::nulltesttype, operator_precedence_group(), operator_precedence_warning, SubLink::operName, ParseState::p_last_srf, A_Expr::rexpr, ROWCOMPARE_SUBLINK, strVal, SubLink::subLinkType, SubLink::testexpr, Transform_null_equals, and transformExprRecurse().

Referenced by transformExprRecurse().

928 {
929  Node *lexpr = a->lexpr;
930  Node *rexpr = a->rexpr;
931  Node *result;
932 
934  {
935  int opgroup;
936  const char *opname;
937 
938  opgroup = operator_precedence_group((Node *) a, &opname);
939  if (opgroup > 0)
940  emit_precedence_warnings(pstate, opgroup, opname,
941  lexpr, rexpr,
942  a->location);
943 
944  /* Look through AEXPR_PAREN nodes so they don't affect tests below */
945  while (lexpr && IsA(lexpr, A_Expr) &&
946  ((A_Expr *) lexpr)->kind == AEXPR_PAREN)
947  lexpr = ((A_Expr *) lexpr)->lexpr;
948  while (rexpr && IsA(rexpr, A_Expr) &&
949  ((A_Expr *) rexpr)->kind == AEXPR_PAREN)
950  rexpr = ((A_Expr *) rexpr)->lexpr;
951  }
952 
953  /*
954  * Special-case "foo = NULL" and "NULL = foo" for compatibility with
955  * standards-broken products (like Microsoft's). Turn these into IS NULL
956  * exprs. (If either side is a CaseTestExpr, then the expression was
957  * generated internally from a CASE-WHEN expression, and
958  * transform_null_equals does not apply.)
959  */
960  if (Transform_null_equals &&
961  list_length(a->name) == 1 &&
962  strcmp(strVal(linitial(a->name)), "=") == 0 &&
963  (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
964  (!IsA(lexpr, CaseTestExpr) && !IsA(rexpr, CaseTestExpr)))
965  {
966  NullTest *n = makeNode(NullTest);
967 
968  n->nulltesttype = IS_NULL;
969  n->location = a->location;
970 
971  if (exprIsNullConstant(lexpr))
972  n->arg = (Expr *) rexpr;
973  else
974  n->arg = (Expr *) lexpr;
975 
976  result = transformExprRecurse(pstate, (Node *) n);
977  }
978  else if (lexpr && IsA(lexpr, RowExpr) &&
979  rexpr && IsA(rexpr, SubLink) &&
980  ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
981  {
982  /*
983  * Convert "row op subselect" into a ROWCOMPARE sublink. Formerly the
984  * grammar did this, but now that a row construct is allowed anywhere
985  * in expressions, it's easier to do it here.
986  */
987  SubLink *s = (SubLink *) rexpr;
988 
990  s->testexpr = lexpr;
991  s->operName = a->name;
992  s->location = a->location;
993  result = transformExprRecurse(pstate, (Node *) s);
994  }
995  else if (lexpr && IsA(lexpr, RowExpr) &&
996  rexpr && IsA(rexpr, RowExpr))
997  {
998  /* ROW() op ROW() is handled specially */
999  lexpr = transformExprRecurse(pstate, lexpr);
1000  rexpr = transformExprRecurse(pstate, rexpr);
1001 
1002  result = make_row_comparison_op(pstate,
1003  a->name,
1004  castNode(RowExpr, lexpr)->args,
1005  castNode(RowExpr, rexpr)->args,
1006  a->location);
1007  }
1008  else
1009  {
1010  /* Ordinary scalar operator */
1011  Node *last_srf = pstate->p_last_srf;
1012 
1013  lexpr = transformExprRecurse(pstate, lexpr);
1014  rexpr = transformExprRecurse(pstate, rexpr);
1015 
1016  result = (Node *) make_op(pstate,
1017  a->name,
1018  lexpr,
1019  rexpr,
1020  last_srf,
1021  a->location);
1022  }
1023 
1024  return result;
1025 }
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:747
static bool exprIsNullConstant(Node *arg)
Definition: parse_expr.c:914
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
static int operator_precedence_group(Node *node, const char **nodename)
Definition: parse_expr.c:3178
Definition: nodes.h:529
#define strVal(v)
Definition: value.h:54
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2873
#define linitial(l)
Definition: pg_list.h:195
Expr * arg
Definition: primnodes.h:1219
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
Node * lexpr
Definition: parsenodes.h:278
bool Transform_null_equals
Definition: parse_expr.c:43
Node * p_last_srf
Definition: parse_node.h:212
bool operator_precedence_warning
Definition: parse_expr.c:42
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3421
NullTestType nulltesttype
Definition: primnodes.h:1220
#define makeNode(_type_)
Definition: nodes.h:577
int location
Definition: primnodes.h:1222
static int list_length(const List *l)
Definition: pg_list.h:169
List * name
Definition: parsenodes.h:277

◆ transformAExprOpAll()

static Node * transformAExprOpAll ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1051 of file parse_expr.c.

References emit_precedence_warnings(), A_Expr::lexpr, llast, A_Expr::location, make_scalar_array_op(), A_Expr::name, operator_precedence_warning, PREC_GROUP_POSTFIX_OP, A_Expr::rexpr, strVal, and transformExprRecurse().

Referenced by transformExprRecurse().

1052 {
1053  Node *lexpr = a->lexpr;
1054  Node *rexpr = a->rexpr;
1055 
1058  strVal(llast(a->name)),
1059  lexpr, NULL,
1060  a->location);
1061 
1062  lexpr = transformExprRecurse(pstate, lexpr);
1063  rexpr = transformExprRecurse(pstate, rexpr);
1064 
1065  return (Node *) make_scalar_array_op(pstate,
1066  a->name,
1067  false,
1068  lexpr,
1069  rexpr,
1070  a->location);
1071 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:867
#define llast(l)
Definition: pg_list.h:215
Definition: nodes.h:529
#define strVal(v)
Definition: value.h:54
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
Node * lexpr
Definition: parsenodes.h:278
bool operator_precedence_warning
Definition: parse_expr.c:42
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3421
List * name
Definition: parsenodes.h:277
#define PREC_GROUP_POSTFIX_OP
Definition: parse_expr.c:60

◆ transformAExprOpAny()

static Node * transformAExprOpAny ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1028 of file parse_expr.c.

References emit_precedence_warnings(), A_Expr::lexpr, llast, A_Expr::location, make_scalar_array_op(), A_Expr::name, operator_precedence_warning, PREC_GROUP_POSTFIX_OP, A_Expr::rexpr, strVal, and transformExprRecurse().

Referenced by transformExprRecurse().

1029 {
1030  Node *lexpr = a->lexpr;
1031  Node *rexpr = a->rexpr;
1032 
1035  strVal(llast(a->name)),
1036  lexpr, NULL,
1037  a->location);
1038 
1039  lexpr = transformExprRecurse(pstate, lexpr);
1040  rexpr = transformExprRecurse(pstate, rexpr);
1041 
1042  return (Node *) make_scalar_array_op(pstate,
1043  a->name,
1044  true,
1045  lexpr,
1046  rexpr,
1047  a->location);
1048 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:867
#define llast(l)
Definition: pg_list.h:215
Definition: nodes.h:529
#define strVal(v)
Definition: value.h:54
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
Node * lexpr
Definition: parsenodes.h:278
bool operator_precedence_warning
Definition: parse_expr.c:42
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3421
List * name
Definition: parsenodes.h:277
#define PREC_GROUP_POSTFIX_OP
Definition: parse_expr.c:60

◆ transformArrayExpr()

static Node * transformArrayExpr ( ParseState pstate,
A_ArrayExpr a,
Oid  array_type,
Oid  element_type,
int32  typmod 
)
static

Definition at line 2105 of file parse_expr.c.

References AEXPR_PAREN, ArrayExpr::array_typeid, Assert, COERCE_EXPLICIT_CAST, coerce_to_common_type(), coerce_to_target_type(), coerce_type(), COERCION_EXPLICIT, element(), ArrayExpr::element_typeid, A_ArrayExpr::elements, ArrayExpr::elements, ereport, errcode(), errhint(), errmsg(), ERROR, exprLocation(), exprType(), format_type_be(), get_array_type(), get_element_type(), InvalidOid, IsA, lappend(), lfirst, A_ArrayExpr::location, ArrayExpr::location, makeNode, ArrayExpr::multidims, NIL, OidIsValid, parser_errposition(), select_common_type(), transformExprRecurse(), and type_is_array.

Referenced by transformExprRecurse(), and transformTypeCast().

2107 {
2108  ArrayExpr *newa = makeNode(ArrayExpr);
2109  List *newelems = NIL;
2110  List *newcoercedelems = NIL;
2111  ListCell *element;
2112  Oid coerce_type;
2113  bool coerce_hard;
2114 
2115  /*
2116  * Transform the element expressions
2117  *
2118  * Assume that the array is one-dimensional unless we find an array-type
2119  * element expression.
2120  */
2121  newa->multidims = false;
2122  foreach(element, a->elements)
2123  {
2124  Node *e = (Node *) lfirst(element);
2125  Node *newe;
2126 
2127  /* Look through AEXPR_PAREN nodes so they don't affect test below */
2128  while (e && IsA(e, A_Expr) &&
2129  ((A_Expr *) e)->kind == AEXPR_PAREN)
2130  e = ((A_Expr *) e)->lexpr;
2131 
2132  /*
2133  * If an element is itself an A_ArrayExpr, recurse directly so that we
2134  * can pass down any target type we were given.
2135  */
2136  if (IsA(e, A_ArrayExpr))
2137  {
2138  newe = transformArrayExpr(pstate,
2139  (A_ArrayExpr *) e,
2140  array_type,
2141  element_type,
2142  typmod);
2143  /* we certainly have an array here */
2144  Assert(array_type == InvalidOid || array_type == exprType(newe));
2145  newa->multidims = true;
2146  }
2147  else
2148  {
2149  newe = transformExprRecurse(pstate, e);
2150 
2151  /*
2152  * Check for sub-array expressions, if we haven't already found
2153  * one.
2154  */
2155  if (!newa->multidims && type_is_array(exprType(newe)))
2156  newa->multidims = true;
2157  }
2158 
2159  newelems = lappend(newelems, newe);
2160  }
2161 
2162  /*
2163  * Select a target type for the elements.
2164  *
2165  * If we haven't been given a target array type, we must try to deduce a
2166  * common type based on the types of the individual elements present.
2167  */
2168  if (OidIsValid(array_type))
2169  {
2170  /* Caller must ensure array_type matches element_type */
2171  Assert(OidIsValid(element_type));
2172  coerce_type = (newa->multidims ? array_type : element_type);
2173  coerce_hard = true;
2174  }
2175  else
2176  {
2177  /* Can't handle an empty array without a target type */
2178  if (newelems == NIL)
2179  ereport(ERROR,
2180  (errcode(ERRCODE_INDETERMINATE_DATATYPE),
2181  errmsg("cannot determine type of empty array"),
2182  errhint("Explicitly cast to the desired type, "
2183  "for example ARRAY[]::integer[]."),
2184  parser_errposition(pstate, a->location)));
2185 
2186  /* Select a common type for the elements */
2187  coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
2188 
2189  if (newa->multidims)
2190  {
2191  array_type = coerce_type;
2192  element_type = get_element_type(array_type);
2193  if (!OidIsValid(element_type))
2194  ereport(ERROR,
2195  (errcode(ERRCODE_UNDEFINED_OBJECT),
2196  errmsg("could not find element type for data type %s",
2197  format_type_be(array_type)),
2198  parser_errposition(pstate, a->location)));
2199  }
2200  else
2201  {
2202  element_type = coerce_type;
2203  array_type = get_array_type(element_type);
2204  if (!OidIsValid(array_type))
2205  ereport(ERROR,
2206  (errcode(ERRCODE_UNDEFINED_OBJECT),
2207  errmsg("could not find array type for data type %s",
2208  format_type_be(element_type)),
2209  parser_errposition(pstate, a->location)));
2210  }
2211  coerce_hard = false;
2212  }
2213 
2214  /*
2215  * Coerce elements to target type
2216  *
2217  * If the array has been explicitly cast, then the elements are in turn
2218  * explicitly coerced.
2219  *
2220  * If the array's type was merely derived from the common type of its
2221  * elements, then the elements are implicitly coerced to the common type.
2222  * This is consistent with other uses of select_common_type().
2223  */
2224  foreach(element, newelems)
2225  {
2226  Node *e = (Node *) lfirst(element);
2227  Node *newe;
2228 
2229  if (coerce_hard)
2230  {
2231  newe = coerce_to_target_type(pstate, e,
2232  exprType(e),
2233  coerce_type,
2234  typmod,
2237  -1);
2238  if (newe == NULL)
2239  ereport(ERROR,
2240  (errcode(ERRCODE_CANNOT_COERCE),
2241  errmsg("cannot cast type %s to %s",
2243  format_type_be(coerce_type)),
2244  parser_errposition(pstate, exprLocation(e))));
2245  }
2246  else
2247  newe = coerce_to_common_type(pstate, e,
2248  coerce_type,
2249  "ARRAY");
2250  newcoercedelems = lappend(newcoercedelems, newe);
2251  }
2252 
2253  newa->array_typeid = array_type;
2254  /* array_collid will be set by parse_collate.c */
2255  newa->element_typeid = element_type;
2256  newa->elements = newcoercedelems;
2257  newa->location = a->location;
2258 
2259  return (Node *) newa;
2260 }
bool multidims
Definition: primnodes.h:992
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
int errhint(const char *fmt,...)
Definition: elog.c:1071
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2587
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2615
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:155
Definition: nodes.h:529
int errcode(int sqlerrcode)
Definition: elog.c:610
Oid array_typeid
Definition: primnodes.h:988
char * format_type_be(Oid type_oid)
Definition: format_type.c:327
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
#define ERROR
Definition: elog.h:43
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
static Node * transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
Definition: parse_expr.c:2105
List * elements
Definition: parsenodes.h:416
List * elements
Definition: primnodes.h:991
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:321
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:380
int location
Definition: primnodes.h:993
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define makeNode(_type_)
Definition: nodes.h:577
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
#define type_is_array(typid)
Definition: lsyscache.h:190
Oid element_typeid
Definition: primnodes.h:990
e
Definition: preproc-init.c:82
int errmsg(const char *fmt,...)
Definition: elog.c:824
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Definition: pg_list.h:50

◆ transformBooleanTest()

static Node * transformBooleanTest ( ParseState pstate,
BooleanTest b 
)
static

Definition at line 2611 of file parse_expr.c.

References BooleanTest::arg, BooleanTest::booltesttype, coerce_to_boolean(), elog, emit_precedence_warnings(), ERROR, IS_FALSE, IS_NOT_FALSE, IS_NOT_TRUE, IS_NOT_UNKNOWN, IS_TRUE, IS_UNKNOWN, BooleanTest::location, operator_precedence_warning, PREC_GROUP_POSTFIX_IS, and transformExprRecurse().

Referenced by transformExprRecurse().

2612 {
2613  const char *clausename;
2614 
2617  (Node *) b->arg, NULL,
2618  b->location);
2619 
2620  switch (b->booltesttype)
2621  {
2622  case IS_TRUE:
2623  clausename = "IS TRUE";
2624  break;
2625  case IS_NOT_TRUE:
2626  clausename = "IS NOT TRUE";
2627  break;
2628  case IS_FALSE:
2629  clausename = "IS FALSE";
2630  break;
2631  case IS_NOT_FALSE:
2632  clausename = "IS NOT FALSE";
2633  break;
2634  case IS_UNKNOWN:
2635  clausename = "IS UNKNOWN";
2636  break;
2637  case IS_NOT_UNKNOWN:
2638  clausename = "IS NOT UNKNOWN";
2639  break;
2640  default:
2641  elog(ERROR, "unrecognized booltesttype: %d",
2642  (int) b->booltesttype);
2643  clausename = NULL; /* keep compiler quiet */
2644  }
2645 
2646  b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2647 
2648  b->arg = (Expr *) coerce_to_boolean(pstate,
2649  (Node *) b->arg,
2650  clausename);
2651 
2652  return (Node *) b;
2653 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
#define PREC_GROUP_POSTFIX_IS
Definition: parse_expr.c:49
Definition: nodes.h:529
#define ERROR
Definition: elog.h:43
Expr * arg
Definition: primnodes.h:1242
BoolTestType booltesttype
Definition: primnodes.h:1243
bool operator_precedence_warning
Definition: parse_expr.c:42
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3421
#define elog(elevel,...)
Definition: elog.h:214
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformBoolExpr()

static Node * transformBoolExpr ( ParseState pstate,
BoolExpr a 
)
static

Definition at line 1487 of file parse_expr.c.

References AND_EXPR, generate_unaccent_rules::args, BoolExpr::args, BoolExpr::boolop, coerce_to_boolean(), elog, ERROR, lappend(), lfirst, BoolExpr::location, makeBoolExpr(), NIL, NOT_EXPR, OR_EXPR, and transformExprRecurse().

Referenced by transformExprRecurse().

1488 {
1489  List *args = NIL;
1490  const char *opname;
1491  ListCell *lc;
1492 
1493  switch (a->boolop)
1494  {
1495  case AND_EXPR:
1496  opname = "AND";
1497  break;
1498  case OR_EXPR:
1499  opname = "OR";
1500  break;
1501  case NOT_EXPR:
1502  opname = "NOT";
1503  break;
1504  default:
1505  elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
1506  opname = NULL; /* keep compiler quiet */
1507  break;
1508  }
1509 
1510  foreach(lc, a->args)
1511  {
1512  Node *arg = (Node *) lfirst(lc);
1513 
1514  arg = transformExprRecurse(pstate, arg);
1515  arg = coerce_to_boolean(pstate, arg, opname);
1516  args = lappend(args, arg);
1517  }
1518 
1519  return (Node *) makeBoolExpr(a->boolop, args, a->location);
1520 }
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
Definition: nodes.h:529
int location
Definition: primnodes.h:584
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:367
#define ERROR
Definition: elog.h:43
BoolExprType boolop
Definition: primnodes.h:582
List * lappend(List *list, void *datum)
Definition: list.c:321
#define lfirst(lc)
Definition: pg_list.h:190
List * args
Definition: primnodes.h:583
#define elog(elevel,...)
Definition: elog.h:214
void * arg
Definition: pg_list.h:50
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformCaseExpr()

static Node * transformCaseExpr ( ParseState pstate,
CaseExpr c 
)
static

Definition at line 1715 of file parse_expr.c.

References AEXPR_OP, arg, CaseExpr::arg, CaseExpr::args, Assert, assign_expr_collations(), CaseExpr::casetype, coerce_to_boolean(), coerce_to_common_type(), CaseTestExpr::collation, CaseExpr::defresult, ereport, errcode(), errhint(), errmsg(), ERROR, CaseWhen::expr, exprCollation(), exprLocation(), exprType(), exprTypmod(), lappend(), lcons(), lfirst, lfirst_node, A_Const::location, CaseExpr::location, CaseWhen::location, makeNode, makeSimpleA_Expr(), NIL, OidIsValid, ParseState::p_last_srf, parser_errposition(), CaseWhen::result, select_common_type(), T_Null, transformExprRecurse(), Value::type, CaseTestExpr::typeId, CaseTestExpr::typeMod, and A_Const::val.

Referenced by transformExprRecurse().

1716 {
1717  CaseExpr *newc = makeNode(CaseExpr);
1718  Node *last_srf = pstate->p_last_srf;
1719  Node *arg;
1720  CaseTestExpr *placeholder;
1721  List *newargs;
1722  List *resultexprs;
1723  ListCell *l;
1724  Node *defresult;
1725  Oid ptype;
1726 
1727  /* transform the test expression, if any */
1728  arg = transformExprRecurse(pstate, (Node *) c->arg);
1729 
1730  /* generate placeholder for test expression */
1731  if (arg)
1732  {
1733  /*
1734  * If test expression is an untyped literal, force it to text. We have
1735  * to do something now because we won't be able to do this coercion on
1736  * the placeholder. This is not as flexible as what was done in 7.4
1737  * and before, but it's good enough to handle the sort of silly coding
1738  * commonly seen.
1739  */
1740  if (exprType(arg) == UNKNOWNOID)
1741  arg = coerce_to_common_type(pstate, arg, TEXTOID, "CASE");
1742 
1743  /*
1744  * Run collation assignment on the test expression so that we know
1745  * what collation to mark the placeholder with. In principle we could
1746  * leave it to parse_collate.c to do that later, but propagating the
1747  * result to the CaseTestExpr would be unnecessarily complicated.
1748  */
1749  assign_expr_collations(pstate, arg);
1750 
1751  placeholder = makeNode(CaseTestExpr);
1752  placeholder->typeId = exprType(arg);
1753  placeholder->typeMod = exprTypmod(arg);
1754  placeholder->collation = exprCollation(arg);
1755  }
1756  else
1757  placeholder = NULL;
1758 
1759  newc->arg = (Expr *) arg;
1760 
1761  /* transform the list of arguments */
1762  newargs = NIL;
1763  resultexprs = NIL;
1764  foreach(l, c->args)
1765  {
1766  CaseWhen *w = lfirst_node(CaseWhen, l);
1767  CaseWhen *neww = makeNode(CaseWhen);
1768  Node *warg;
1769 
1770  warg = (Node *) w->expr;
1771  if (placeholder)
1772  {
1773  /* shorthand form was specified, so expand... */
1774  warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1775  (Node *) placeholder,
1776  warg,
1777  w->location);
1778  }
1779  neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1780 
1781  neww->expr = (Expr *) coerce_to_boolean(pstate,
1782  (Node *) neww->expr,
1783  "CASE/WHEN");
1784 
1785  warg = (Node *) w->result;
1786  neww->result = (Expr *) transformExprRecurse(pstate, warg);
1787  neww->location = w->location;
1788 
1789  newargs = lappend(newargs, neww);
1790  resultexprs = lappend(resultexprs, neww->result);
1791  }
1792 
1793  newc->args = newargs;
1794 
1795  /* transform the default clause */
1796  defresult = (Node *) c->defresult;
1797  if (defresult == NULL)
1798  {
1799  A_Const *n = makeNode(A_Const);
1800 
1801  n->val.type = T_Null;
1802  n->location = -1;
1803  defresult = (Node *) n;
1804  }
1805  newc->defresult = (Expr *) transformExprRecurse(pstate, defresult);
1806 
1807  /*
1808  * Note: default result is considered the most significant type in
1809  * determining preferred type. This is how the code worked before, but it
1810  * seems a little bogus to me --- tgl
1811  */
1812  resultexprs = lcons(newc->defresult, resultexprs);
1813 
1814  ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
1815  Assert(OidIsValid(ptype));
1816  newc->casetype = ptype;
1817  /* casecollid will be set by parse_collate.c */
1818 
1819  /* Convert default result clause, if necessary */
1820  newc->defresult = (Expr *)
1821  coerce_to_common_type(pstate,
1822  (Node *) newc->defresult,
1823  ptype,
1824  "CASE/ELSE");
1825 
1826  /* Convert when-clause results, if necessary */
1827  foreach(l, newc->args)
1828  {
1829  CaseWhen *w = (CaseWhen *) lfirst(l);
1830 
1831  w->result = (Expr *)
1832  coerce_to_common_type(pstate,
1833  (Node *) w->result,
1834  ptype,
1835  "CASE/WHEN");
1836  }
1837 
1838  /* if any subexpression contained a SRF, complain */
1839  if (pstate->p_last_srf != last_srf)
1840  ereport(ERROR,
1841  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1842  /* translator: %s is name of a SQL construct, eg GROUP BY */
1843  errmsg("set-returning functions are not allowed in %s",
1844  "CASE"),
1845  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
1846  parser_errposition(pstate,
1847  exprLocation(pstate->p_last_srf))));
1848 
1849  newc->location = c->location;
1850 
1851  return (Node *) newc;
1852 }
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
int errhint(const char *fmt,...)
Definition: elog.c:1071
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
Definition: nodes.h:529
int errcode(int sqlerrcode)
Definition: elog.c:610
Oid casetype
Definition: primnodes.h:930
unsigned int Oid
Definition: postgres_ext.h:31
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
#define OidIsValid(objectId)
Definition: c.h:644
int32 typeMod
Definition: primnodes.h:973
int location
Definition: primnodes.h:946
void assign_expr_collations(ParseState *pstate, Node *expr)
#define ERROR
Definition: elog.h:43
#define lfirst_node(type, lc)
Definition: pg_list.h:193
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:321
List * args
Definition: primnodes.h:933
Node * p_last_srf
Definition: parse_node.h:212
#define ereport(elevel,...)
Definition: elog.h:144
List * lcons(void *datum, List *list)
Definition: list.c:453
#define makeNode(_type_)
Definition: nodes.h:577
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
int location
Definition: parsenodes.h:290
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
int location
Definition: primnodes.h:935
Definition: nodes.h:295
int errmsg(const char *fmt,...)
Definition: elog.c:824
void * arg
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Expr * arg
Definition: primnodes.h:932
Expr * result
Definition: primnodes.h:945
Expr * defresult
Definition: primnodes.h:934
Expr * expr
Definition: primnodes.h:944
Value val
Definition: parsenodes.h:289
Definition: pg_list.h:50
NodeTag type
Definition: value.h:44
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformCoalesceExpr()

static Node * transformCoalesceExpr ( ParseState pstate,
CoalesceExpr c 
)
static

Definition at line 2293 of file parse_expr.c.

References generate_unaccent_rules::args, CoalesceExpr::args, CoalesceExpr::coalescetype, coerce_to_common_type(), ereport, errcode(), errhint(), errmsg(), ERROR, exprLocation(), lappend(), lfirst, CoalesceExpr::location, makeNode, NIL, ParseState::p_last_srf, parser_errposition(), select_common_type(), and transformExprRecurse().

Referenced by transformExprRecurse().

2294 {
2296  Node *last_srf = pstate->p_last_srf;
2297  List *newargs = NIL;
2298  List *newcoercedargs = NIL;
2299  ListCell *args;
2300 
2301  foreach(args, c->args)
2302  {
2303  Node *e = (Node *) lfirst(args);
2304  Node *newe;
2305 
2306  newe = transformExprRecurse(pstate, e);
2307  newargs = lappend(newargs, newe);
2308  }
2309 
2310  newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2311  /* coalescecollid will be set by parse_collate.c */
2312 
2313  /* Convert arguments if necessary */
2314  foreach(args, newargs)
2315  {
2316  Node *e = (Node *) lfirst(args);
2317  Node *newe;
2318 
2319  newe = coerce_to_common_type(pstate, e,
2320  newc->coalescetype,
2321  "COALESCE");
2322  newcoercedargs = lappend(newcoercedargs, newe);
2323  }
2324 
2325  /* if any subexpression contained a SRF, complain */
2326  if (pstate->p_last_srf != last_srf)
2327  ereport(ERROR,
2328  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2329  /* translator: %s is name of a SQL construct, eg GROUP BY */
2330  errmsg("set-returning functions are not allowed in %s",
2331  "COALESCE"),
2332  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2333  parser_errposition(pstate,
2334  exprLocation(pstate->p_last_srf))));
2335 
2336  newc->args = newcoercedargs;
2337  newc->location = c->location;
2338  return (Node *) newc;
2339 }
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
int errhint(const char *fmt,...)
Definition: elog.c:1071
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
Definition: nodes.h:529
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
List * args
Definition: primnodes.h:1086
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:321
Node * p_last_srf
Definition: parse_node.h:212
#define ereport(elevel,...)
Definition: elog.h:144
#define makeNode(_type_)
Definition: nodes.h:577
#define lfirst(lc)
Definition: pg_list.h:190
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
e
Definition: preproc-init.c:82
int errmsg(const char *fmt,...)
Definition: elog.c:824
Oid coalescetype
Definition: primnodes.h:1084
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Definition: pg_list.h:50

◆ transformCollateClause()

static Node * transformCollateClause ( ParseState pstate,
CollateClause c 
)
static

Definition at line 2833 of file parse_expr.c.

References CollateClause::arg, CollateExpr::arg, CollateClause::collname, CollateExpr::collOid, ereport, errcode(), errmsg(), ERROR, exprType(), format_type_be(), CollateClause::location, CollateExpr::location, LookupCollation(), makeNode, parser_errposition(), transformExprRecurse(), and type_is_collatable().

Referenced by transformExprRecurse().

2834 {
2835  CollateExpr *newc;
2836  Oid argtype;
2837 
2838  newc = makeNode(CollateExpr);
2839  newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2840 
2841  argtype = exprType((Node *) newc->arg);
2842 
2843  /*
2844  * The unknown type is not collatable, but coerce_type() takes care of it
2845  * separately, so we'll let it go here.
2846  */
2847  if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2848  ereport(ERROR,
2849  (errcode(ERRCODE_DATATYPE_MISMATCH),
2850  errmsg("collations are not supported by type %s",
2851  format_type_be(argtype)),
2852  parser_errposition(pstate, c->location)));
2853 
2854  newc->collOid = LookupCollation(pstate, c->collname, c->location);
2855  newc->location = c->location;
2856 
2857  return (Node *) newc;
2858 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
Definition: nodes.h:529
int errcode(int sqlerrcode)
Definition: elog.c:610
char * format_type_be(Oid type_oid)
Definition: format_type.c:327
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
Oid LookupCollation(ParseState *pstate, List *collnames, int location)
Definition: parse_type.c:513
#define ereport(elevel,...)
Definition: elog.h:144
#define makeNode(_type_)
Definition: nodes.h:577
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Expr * arg
Definition: primnodes.h:900
List * collname
Definition: parsenodes.h:311
int errmsg(const char *fmt,...)
Definition: elog.c:824
int location
Definition: primnodes.h:902
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:2909

◆ transformColumnRef()

static Node * transformColumnRef ( ParseState pstate,
ColumnRef cref 
)
static

Definition at line 508 of file parse_expr.c.

References _, Assert, colNameToVar(), ereport, errcode(), errmsg(), errmsg_internal(), ERROR, errorMissingColumn(), errorMissingRTE(), EXPR_KIND_ALTER_COL_TRANSFORM, EXPR_KIND_CALL_ARGUMENT, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_COLUMN_DEFAULT, EXPR_KIND_COPY_WHERE, EXPR_KIND_DISTINCT_ON, EXPR_KIND_DOMAIN_CHECK, EXPR_KIND_EXECUTE_PARAMETER, EXPR_KIND_FILTER, EXPR_KIND_FROM_FUNCTION, EXPR_KIND_FROM_SUBSELECT, EXPR_KIND_FUNCTION_DEFAULT, EXPR_KIND_GENERATED_COLUMN, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_INDEX_EXPRESSION, EXPR_KIND_INDEX_PREDICATE, EXPR_KIND_INSERT_TARGET, EXPR_KIND_JOIN_ON, EXPR_KIND_JOIN_USING, EXPR_KIND_LIMIT, EXPR_KIND_NONE, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_OTHER, EXPR_KIND_PARTITION_BOUND, EXPR_KIND_PARTITION_EXPRESSION, EXPR_KIND_POLICY, EXPR_KIND_RETURNING, EXPR_KIND_SELECT_TARGET, EXPR_KIND_TRIGGER_WHEN, EXPR_KIND_UPDATE_SOURCE, EXPR_KIND_UPDATE_TARGET, EXPR_KIND_VALUES, EXPR_KIND_VALUES_SINGLE, EXPR_KIND_WHERE, EXPR_KIND_WINDOW_FRAME_GROUPS, EXPR_KIND_WINDOW_FRAME_RANGE, EXPR_KIND_WINDOW_FRAME_ROWS, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, ColumnRef::fields, get_database_name(), IsA, lfourth, linitial, list_length(), list_make1, ColumnRef::location, lsecond, lthird, makeRangeVar(), makeString(), MyDatabaseId, NameListToString(), ParseState::p_expr_kind, ParseState::p_last_srf, ParseState::p_post_columnref_hook, ParseState::p_pre_columnref_hook, ParseFuncOrColumn(), parser_errposition(), refnameNamespaceItem(), relname, scanNSItemForColumn(), strVal, and transformWholeRowRef().

Referenced by transformExprRecurse().

509 {
510  Node *node = NULL;
511  char *nspname = NULL;
512  char *relname = NULL;
513  char *colname = NULL;
514  ParseNamespaceItem *nsitem;
515  int levels_up;
516  enum
517  {
518  CRERR_NO_COLUMN,
519  CRERR_NO_RTE,
520  CRERR_WRONG_DB,
521  CRERR_TOO_MANY
522  } crerr = CRERR_NO_COLUMN;
523  const char *err;
524 
525  /*
526  * Check to see if the column reference is in an invalid place within the
527  * query. We allow column references in most places, except in default
528  * expressions and partition bound expressions.
529  */
530  err = NULL;
531  switch (pstate->p_expr_kind)
532  {
533  case EXPR_KIND_NONE:
534  Assert(false); /* can't happen */
535  break;
536  case EXPR_KIND_OTHER:
537  case EXPR_KIND_JOIN_ON:
541  case EXPR_KIND_WHERE:
542  case EXPR_KIND_POLICY:
543  case EXPR_KIND_HAVING:
544  case EXPR_KIND_FILTER:
554  case EXPR_KIND_GROUP_BY:
555  case EXPR_KIND_ORDER_BY:
557  case EXPR_KIND_LIMIT:
558  case EXPR_KIND_OFFSET:
559  case EXPR_KIND_RETURNING:
560  case EXPR_KIND_VALUES:
574  /* okay */
575  break;
576 
578  err = _("cannot use column reference in DEFAULT expression");
579  break;
581  err = _("cannot use column reference in partition bound expression");
582  break;
583 
584  /*
585  * There is intentionally no default: case here, so that the
586  * compiler will warn if we add a new ParseExprKind without
587  * extending this switch. If we do see an unrecognized value at
588  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
589  * which is sane anyway.
590  */
591  }
592  if (err)
593  ereport(ERROR,
594  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
595  errmsg_internal("%s", err),
596  parser_errposition(pstate, cref->location)));
597 
598  /*
599  * Give the PreParseColumnRefHook, if any, first shot. If it returns
600  * non-null then that's all, folks.
601  */
602  if (pstate->p_pre_columnref_hook != NULL)
603  {
604  node = pstate->p_pre_columnref_hook(pstate, cref);
605  if (node != NULL)
606  return node;
607  }
608 
609  /*----------
610  * The allowed syntaxes are:
611  *
612  * A First try to resolve as unqualified column name;
613  * if no luck, try to resolve as unqualified table name (A.*).
614  * A.B A is an unqualified table name; B is either a
615  * column or function name (trying column name first).
616  * A.B.C schema A, table B, col or func name C.
617  * A.B.C.D catalog A, schema B, table C, col or func D.
618  * A.* A is an unqualified table name; means whole-row value.
619  * A.B.* whole-row value of table B in schema A.
620  * A.B.C.* whole-row value of table C in schema B in catalog A.
621  *
622  * We do not need to cope with bare "*"; that will only be accepted by
623  * the grammar at the top level of a SELECT list, and transformTargetList
624  * will take care of it before it ever gets here. Also, "A.*" etc will
625  * be expanded by transformTargetList if they appear at SELECT top level,
626  * so here we are only going to see them as function or operator inputs.
627  *
628  * Currently, if a catalog name is given then it must equal the current
629  * database name; we check it here and then discard it.
630  *----------
631  */
632  switch (list_length(cref->fields))
633  {
634  case 1:
635  {
636  Node *field1 = (Node *) linitial(cref->fields);
637 
638  Assert(IsA(field1, String));
639  colname = strVal(field1);
640 
641  /* Try to identify as an unqualified column */
642  node = colNameToVar(pstate, colname, false, cref->location);
643 
644  if (node == NULL)
645  {
646  /*
647  * Not known as a column of any range-table entry.
648  *
649  * Try to find the name as a relation. Note that only
650  * relations already entered into the rangetable will be
651  * recognized.
652  *
653  * This is a hack for backwards compatibility with
654  * PostQUEL-inspired syntax. The preferred form now is
655  * "rel.*".
656  */
657  nsitem = refnameNamespaceItem(pstate, NULL, colname,
658  cref->location,
659  &levels_up);
660  if (nsitem)
661  node = transformWholeRowRef(pstate, nsitem, levels_up,
662  cref->location);
663  }
664  break;
665  }
666  case 2:
667  {
668  Node *field1 = (Node *) linitial(cref->fields);
669  Node *field2 = (Node *) lsecond(cref->fields);
670 
671  Assert(IsA(field1, String));
672  relname = strVal(field1);
673 
674  /* Locate the referenced nsitem */
675  nsitem = refnameNamespaceItem(pstate, nspname, relname,
676  cref->location,
677  &levels_up);
678  if (nsitem == NULL)
679  {
680  crerr = CRERR_NO_RTE;
681  break;
682  }
683 
684  /* Whole-row reference? */
685  if (IsA(field2, A_Star))
686  {
687  node = transformWholeRowRef(pstate, nsitem, levels_up,
688  cref->location);
689  break;
690  }
691 
692  Assert(IsA(field2, String));
693  colname = strVal(field2);
694 
695  /* Try to identify as a column of the nsitem */
696  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
697  cref->location);
698  if (node == NULL)
699  {
700  /* Try it as a function call on the whole row */
701  node = transformWholeRowRef(pstate, nsitem, levels_up,
702  cref->location);
703  node = ParseFuncOrColumn(pstate,
704  list_make1(makeString(colname)),
705  list_make1(node),
706  pstate->p_last_srf,
707  NULL,
708  false,
709  cref->location);
710  }
711  break;
712  }
713  case 3:
714  {
715  Node *field1 = (Node *) linitial(cref->fields);
716  Node *field2 = (Node *) lsecond(cref->fields);
717  Node *field3 = (Node *) lthird(cref->fields);
718 
719  Assert(IsA(field1, String));
720  nspname = strVal(field1);
721  Assert(IsA(field2, String));
722  relname = strVal(field2);
723 
724  /* Locate the referenced nsitem */
725  nsitem = refnameNamespaceItem(pstate, nspname, relname,
726  cref->location,
727  &levels_up);
728  if (nsitem == NULL)
729  {
730  crerr = CRERR_NO_RTE;
731  break;
732  }
733 
734  /* Whole-row reference? */
735  if (IsA(field3, A_Star))
736  {
737  node = transformWholeRowRef(pstate, nsitem, levels_up,
738  cref->location);
739  break;
740  }
741 
742  Assert(IsA(field3, String));
743  colname = strVal(field3);
744 
745  /* Try to identify as a column of the nsitem */
746  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
747  cref->location);
748  if (node == NULL)
749  {
750  /* Try it as a function call on the whole row */
751  node = transformWholeRowRef(pstate, nsitem, levels_up,
752  cref->location);
753  node = ParseFuncOrColumn(pstate,
754  list_make1(makeString(colname)),
755  list_make1(node),
756  pstate->p_last_srf,
757  NULL,
758  false,
759  cref->location);
760  }
761  break;
762  }
763  case 4:
764  {
765  Node *field1 = (Node *) linitial(cref->fields);
766  Node *field2 = (Node *) lsecond(cref->fields);
767  Node *field3 = (Node *) lthird(cref->fields);
768  Node *field4 = (Node *) lfourth(cref->fields);
769  char *catname;
770 
771  Assert(IsA(field1, String));
772  catname = strVal(field1);
773  Assert(IsA(field2, String));
774  nspname = strVal(field2);
775  Assert(IsA(field3, String));
776  relname = strVal(field3);
777 
778  /*
779  * We check the catalog name and then ignore it.
780  */
781  if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
782  {
783  crerr = CRERR_WRONG_DB;
784  break;
785  }
786 
787  /* Locate the referenced nsitem */
788  nsitem = refnameNamespaceItem(pstate, nspname, relname,
789  cref->location,
790  &levels_up);
791  if (nsitem == NULL)
792  {
793  crerr = CRERR_NO_RTE;
794  break;
795  }
796 
797  /* Whole-row reference? */
798  if (IsA(field4, A_Star))
799  {
800  node = transformWholeRowRef(pstate, nsitem, levels_up,
801  cref->location);
802  break;
803  }
804 
805  Assert(IsA(field4, String));
806  colname = strVal(field4);
807 
808  /* Try to identify as a column of the nsitem */
809  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
810  cref->location);
811  if (node == NULL)
812  {
813  /* Try it as a function call on the whole row */
814  node = transformWholeRowRef(pstate, nsitem, levels_up,
815  cref->location);
816  node = ParseFuncOrColumn(pstate,
817  list_make1(makeString(colname)),
818  list_make1(node),
819  pstate->p_last_srf,
820  NULL,
821  false,
822  cref->location);
823  }
824  break;
825  }
826  default:
827  crerr = CRERR_TOO_MANY; /* too many dotted names */
828  break;
829  }
830 
831  /*
832  * Now give the PostParseColumnRefHook, if any, a chance. We pass the
833  * translation-so-far so that it can throw an error if it wishes in the
834  * case that it has a conflicting interpretation of the ColumnRef. (If it
835  * just translates anyway, we'll throw an error, because we can't undo
836  * whatever effects the preceding steps may have had on the pstate.) If it
837  * returns NULL, use the standard translation, or throw a suitable error
838  * if there is none.
839  */
840  if (pstate->p_post_columnref_hook != NULL)
841  {
842  Node *hookresult;
843 
844  hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
845  if (node == NULL)
846  node = hookresult;
847  else if (hookresult != NULL)
848  ereport(ERROR,
849  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
850  errmsg("column reference \"%s\" is ambiguous",
851  NameListToString(cref->fields)),
852  parser_errposition(pstate, cref->location)));
853  }
854 
855  /*
856  * Throw error if no translation found.
857  */
858  if (node == NULL)
859  {
860  switch (crerr)
861  {
862  case CRERR_NO_COLUMN:
863  errorMissingColumn(pstate, relname, colname, cref->location);
864  break;
865  case CRERR_NO_RTE:
866  errorMissingRTE(pstate, makeRangeVar(nspname, relname,
867  cref->location));
868  break;
869  case CRERR_WRONG_DB:
870  ereport(ERROR,
871  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
872  errmsg("cross-database references are not implemented: %s",
873  NameListToString(cref->fields)),
874  parser_errposition(pstate, cref->location)));
875  break;
876  case CRERR_TOO_MANY:
877  ereport(ERROR,
878  (errcode(ERRCODE_SYNTAX_ERROR),
879  errmsg("improper qualified name (too many dotted names): %s",
880  NameListToString(cref->fields)),
881  parser_errposition(pstate, cref->location)));
882  break;
883  }
884  }
885 
886  return node;
887 }
Value * makeString(char *str)
Definition: value.c:53
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Definition: nodes.h:529
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:610
void errorMissingColumn(ParseState *pstate, const char *relname, const char *colname, int location)
NameData relname
Definition: pg_class.h:38
#define lsecond(l)
Definition: pg_list.h:200
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:219
int location
Definition: parsenodes.h:237
#define list_make1(x1)
Definition: pg_list.h:227
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:88
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
static Node * transformWholeRowRef(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location)
Definition: parse_expr.c:2708
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
char * NameListToString(List *names)
Definition: namespace.c:3102
Node * p_last_srf
Definition: parse_node.h:212
Oid MyDatabaseId
Definition: globals.c:85
ParseExprKind p_expr_kind
Definition: parse_node.h:194
#define ereport(elevel,...)
Definition: elog.h:144
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
int errmsg_internal(const char *fmt,...)
Definition: elog.c:911
#define Assert(condition)
Definition: c.h:738
#define lfourth(l)
Definition: pg_list.h:210
static int list_length(const List *l)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define lthird(l)
Definition: pg_list.h:205
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:218
#define _(x)
Definition: elog.c:88
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:420
List * fields
Definition: parsenodes.h:236
Node * colNameToVar(ParseState *pstate, const char *colname, bool localonly, int location)

◆ transformCurrentOfExpr()

static Node * transformCurrentOfExpr ( ParseState pstate,
CurrentOfExpr cexpr 
)
static

Definition at line 2656 of file parse_expr.c.

References Assert, CurrentOfExpr::cursor_name, CurrentOfExpr::cursor_param, CurrentOfExpr::cvarno, ColumnRef::fields, IsA, list_make1, ColumnRef::location, makeNode, makeString(), ParseState::p_post_columnref_hook, ParseState::p_pre_columnref_hook, ParseNamespaceItem::p_rtindex, ParseState::p_target_nsitem, PARAM_EXTERN, Param::paramid, Param::paramkind, and Param::paramtype.

Referenced by transformExprRecurse().

2657 {
2658  /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2659  Assert(pstate->p_target_nsitem != NULL);
2660  cexpr->cvarno = pstate->p_target_nsitem->p_rtindex;
2661 
2662  /*
2663  * Check to see if the cursor name matches a parameter of type REFCURSOR.
2664  * If so, replace the raw name reference with a parameter reference. (This
2665  * is a hack for the convenience of plpgsql.)
2666  */
2667  if (cexpr->cursor_name != NULL) /* in case already transformed */
2668  {
2669  ColumnRef *cref = makeNode(ColumnRef);
2670  Node *node = NULL;
2671 
2672  /* Build an unqualified ColumnRef with the given name */
2673  cref->fields = list_make1(makeString(cexpr->cursor_name));
2674  cref->location = -1;
2675 
2676  /* See if there is a translation available from a parser hook */
2677  if (pstate->p_pre_columnref_hook != NULL)
2678  node = pstate->p_pre_columnref_hook(pstate, cref);
2679  if (node == NULL && pstate->p_post_columnref_hook != NULL)
2680  node = pstate->p_post_columnref_hook(pstate, cref, NULL);
2681 
2682  /*
2683  * XXX Should we throw an error if we get a translation that isn't a
2684  * refcursor Param? For now it seems best to silently ignore false
2685  * matches.
2686  */
2687  if (node != NULL && IsA(node, Param))
2688  {
2689  Param *p = (Param *) node;
2690 
2691  if (p->paramkind == PARAM_EXTERN &&
2692  p->paramtype == REFCURSOROID)
2693  {
2694  /* Matches, so convert CURRENT OF to a param reference */
2695  cexpr->cursor_name = NULL;
2696  cexpr->cursor_param = p->paramid;
2697  }
2698  }
2699  }
2700 
2701  return (Node *) cexpr;
2702 }
Value * makeString(char *str)
Definition: value.c:53
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
ParamKind paramkind
Definition: primnodes.h:262
Definition: nodes.h:529
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:191
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:219
int location
Definition: parsenodes.h:237
#define list_make1(x1)
Definition: pg_list.h:227
char * cursor_name
Definition: primnodes.h:1317
#define makeNode(_type_)
Definition: nodes.h:577
#define Assert(condition)
Definition: c.h:738
int paramid
Definition: primnodes.h:263
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:218
Oid paramtype
Definition: primnodes.h:264
List * fields
Definition: parsenodes.h:236

◆ transformExpr()

Node* transformExpr ( ParseState pstate,
Node expr,
ParseExprKind  exprKind 
)

Definition at line 145 of file parse_expr.c.

References Assert, EXPR_KIND_NONE, ParseState::p_expr_kind, and transformExprRecurse().

Referenced by cookConstraint(), cookDefault(), DoCopy(), domainAddConstraint(), EvaluateParams(), ExpandIndirectionStar(), findTargetlistEntrySQL99(), interpret_function_parameter_list(), resolve_unique_index_expr(), transformAlterTableStmt(), transformCallStmt(), transformContainerSubscripts(), transformExpressionList(), transformFrameOffset(), transformFuncCall(), transformGroupingFunc(), transformIndexStmt(), transformJoinUsingClause(), transformLimitClause(), transformOnConflictArbiter(), transformPartitionBoundValue(), transformPartitionSpec(), transformRangeFunction(), transformRangeTableFunc(), transformRangeTableSample(), transformTargetEntry(), and transformWhereClause().

146 {
147  Node *result;
148  ParseExprKind sv_expr_kind;
149 
150  /* Save and restore identity of expression type we're parsing */
151  Assert(exprKind != EXPR_KIND_NONE);
152  sv_expr_kind = pstate->p_expr_kind;
153  pstate->p_expr_kind = exprKind;
154 
155  result = transformExprRecurse(pstate, expr);
156 
157  pstate->p_expr_kind = sv_expr_kind;
158 
159  return result;
160 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
Definition: nodes.h:529
ParseExprKind
Definition: parse_node.h:38
ParseExprKind p_expr_kind
Definition: parse_node.h:194
#define Assert(condition)
Definition: c.h:738

◆ transformExprRecurse()

static Node * transformExprRecurse ( ParseState pstate,
Node expr 
)
static

Definition at line 163 of file parse_expr.c.

References AEXPR_BETWEEN, AEXPR_BETWEEN_SYM, AEXPR_DISTINCT, AEXPR_ILIKE, AEXPR_IN, AEXPR_LIKE, AEXPR_NOT_BETWEEN, AEXPR_NOT_BETWEEN_SYM, AEXPR_NOT_DISTINCT, AEXPR_NULLIF, AEXPR_OF, AEXPR_OP, AEXPR_OP_ALL, AEXPR_OP_ANY, AEXPR_PAREN, AEXPR_SIMILAR, NamedArgExpr::arg, NullTest::arg, NullTest::argisrow, check_stack_depth(), elog, emit_precedence_warnings(), ereport, errcode(), errmsg(), ERROR, exprType(), InvalidOid, A_Expr::kind, A_Expr::lexpr, A_Const::location, NullTest::location, make_const(), nodeTag, operator_precedence_warning, parser_errposition(), PREC_GROUP_POSTFIX_IS, T_A_ArrayExpr, T_A_Const, T_A_Expr, T_A_Indirection, T_BooleanTest, T_BoolExpr, T_CaseExpr, T_CaseTestExpr, T_CoalesceExpr, T_CollateClause, T_ColumnRef, T_CurrentOfExpr, T_FuncCall, T_GroupingFunc, T_MinMaxExpr, T_MultiAssignRef, T_NamedArgExpr, T_NullTest, T_ParamRef, T_RowExpr, T_SetToDefault, T_SQLValueFunction, T_SubLink, T_TypeCast, T_Var, T_XmlExpr, T_XmlSerialize, transformAExprBetween(), transformAExprDistinct(), transformAExprIn(), transformAExprNullIf(), transformAExprOf(), transformAExprOp(), transformAExprOpAll(), transformAExprOpAny(), transformArrayExpr(), transformBooleanTest(), transformBoolExpr(), transformCaseExpr(), transformCoalesceExpr(), transformCollateClause(), transformColumnRef(), transformCurrentOfExpr(), transformFuncCall(), transformGroupingFunc(), transformIndirection(), transformMinMaxExpr(), transformMultiAssignRef(), transformParamRef(), transformRowExpr(), transformSQLValueFunction(), transformSubLink(), transformTypeCast(), transformXmlExpr(), transformXmlSerialize(), type_is_rowtype(), A_Const::val, and val.

Referenced by make_nulltest_from_distinct(), transformAExprBetween(), transformAExprDistinct(), transformAExprIn(), transformAExprNullIf(), transformAExprOf(), transformAExprOp(), transformAExprOpAll(), transformAExprOpAny(), transformArrayExpr(), transformBooleanTest(), transformBoolExpr(), transformCaseExpr(), transformCoalesceExpr(), transformCollateClause(), transformExpr(), transformFuncCall(), transformIndirection(), transformMinMaxExpr(), transformMultiAssignRef(), transformSubLink(), transformTypeCast(), transformXmlExpr(), and transformXmlSerialize().

164 {
165  Node *result;
166 
167  if (expr == NULL)
168  return NULL;
169 
170  /* Guard against stack overflow due to overly complex expressions */
172 
173  switch (nodeTag(expr))
174  {
175  case T_ColumnRef:
176  result = transformColumnRef(pstate, (ColumnRef *) expr);
177  break;
178 
179  case T_ParamRef:
180  result = transformParamRef(pstate, (ParamRef *) expr);
181  break;
182 
183  case T_A_Const:
184  {
185  A_Const *con = (A_Const *) expr;
186  Value *val = &con->val;
187 
188  result = (Node *) make_const(pstate, val, con->location);
189  break;
190  }
191 
192  case T_A_Indirection:
193  result = transformIndirection(pstate, (A_Indirection *) expr);
194  break;
195 
196  case T_A_ArrayExpr:
197  result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
198  InvalidOid, InvalidOid, -1);
199  break;
200 
201  case T_TypeCast:
202  result = transformTypeCast(pstate, (TypeCast *) expr);
203  break;
204 
205  case T_CollateClause:
206  result = transformCollateClause(pstate, (CollateClause *) expr);
207  break;
208 
209  case T_A_Expr:
210  {
211  A_Expr *a = (A_Expr *) expr;
212 
213  switch (a->kind)
214  {
215  case AEXPR_OP:
216  result = transformAExprOp(pstate, a);
217  break;
218  case AEXPR_OP_ANY:
219  result = transformAExprOpAny(pstate, a);
220  break;
221  case AEXPR_OP_ALL:
222  result = transformAExprOpAll(pstate, a);
223  break;
224  case AEXPR_DISTINCT:
225  case AEXPR_NOT_DISTINCT:
226  result = transformAExprDistinct(pstate, a);
227  break;
228  case AEXPR_NULLIF:
229  result = transformAExprNullIf(pstate, a);
230  break;
231  case AEXPR_OF:
232  result = transformAExprOf(pstate, a);
233  break;
234  case AEXPR_IN:
235  result = transformAExprIn(pstate, a);
236  break;
237  case AEXPR_LIKE:
238  case AEXPR_ILIKE:
239  case AEXPR_SIMILAR:
240  /* we can transform these just like AEXPR_OP */
241  result = transformAExprOp(pstate, a);
242  break;
243  case AEXPR_BETWEEN:
244  case AEXPR_NOT_BETWEEN:
245  case AEXPR_BETWEEN_SYM:
247  result = transformAExprBetween(pstate, a);
248  break;
249  case AEXPR_PAREN:
250  result = transformExprRecurse(pstate, a->lexpr);
251  break;
252  default:
253  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
254  result = NULL; /* keep compiler quiet */
255  break;
256  }
257  break;
258  }
259 
260  case T_BoolExpr:
261  result = transformBoolExpr(pstate, (BoolExpr *) expr);
262  break;
263 
264  case T_FuncCall:
265  result = transformFuncCall(pstate, (FuncCall *) expr);
266  break;
267 
268  case T_MultiAssignRef:
269  result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
270  break;
271 
272  case T_GroupingFunc:
273  result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
274  break;
275 
276  case T_NamedArgExpr:
277  {
278  NamedArgExpr *na = (NamedArgExpr *) expr;
279 
280  na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
281  result = expr;
282  break;
283  }
284 
285  case T_SubLink:
286  result = transformSubLink(pstate, (SubLink *) expr);
287  break;
288 
289  case T_CaseExpr:
290  result = transformCaseExpr(pstate, (CaseExpr *) expr);
291  break;
292 
293  case T_RowExpr:
294  result = transformRowExpr(pstate, (RowExpr *) expr, false);
295  break;
296 
297  case T_CoalesceExpr:
298  result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
299  break;
300 
301  case T_MinMaxExpr:
302  result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
303  break;
304 
305  case T_SQLValueFunction:
306  result = transformSQLValueFunction(pstate,
307  (SQLValueFunction *) expr);
308  break;
309 
310  case T_XmlExpr:
311  result = transformXmlExpr(pstate, (XmlExpr *) expr);
312  break;
313 
314  case T_XmlSerialize:
315  result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
316  break;
317 
318  case T_NullTest:
319  {
320  NullTest *n = (NullTest *) expr;
321 
324  (Node *) n->arg, NULL,
325  n->location);
326 
327  n->arg = (Expr *) transformExprRecurse(pstate, (Node *) n->arg);
328  /* the argument can be any type, so don't coerce it */
329  n->argisrow = type_is_rowtype(exprType((Node *) n->arg));
330  result = expr;
331  break;
332  }
333 
334  case T_BooleanTest:
335  result = transformBooleanTest(pstate, (BooleanTest *) expr);
336  break;
337 
338  case T_CurrentOfExpr:
339  result = transformCurrentOfExpr(pstate, (CurrentOfExpr *) expr);
340  break;
341 
342  /*
343  * In all places where DEFAULT is legal, the caller should have
344  * processed it rather than passing it to transformExpr().
345  */
346  case T_SetToDefault:
347  ereport(ERROR,
348  (errcode(ERRCODE_SYNTAX_ERROR),
349  errmsg("DEFAULT is not allowed in this context"),
350  parser_errposition(pstate,
351  ((SetToDefault *) expr)->location)));
352  break;
353 
354  /*
355  * CaseTestExpr doesn't require any processing; it is only
356  * injected into parse trees in a fully-formed state.
357  *
358  * Ordinarily we should not see a Var here, but it is convenient
359  * for transformJoinUsingClause() to create untransformed operator
360  * trees containing already-transformed Vars. The best
361  * alternative would be to deconstruct and reconstruct column
362  * references, which seems expensively pointless. So allow it.
363  */
364  case T_CaseTestExpr:
365  case T_Var:
366  {
367  result = (Node *) expr;
368  break;
369  }
370 
371  default:
372  /* should not reach here */
373  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
374  result = NULL; /* keep compiler quiet */
375  break;
376  }
377 
378  return result;
379 }
static Node * transformAExprDistinct(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1074
static Node * transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
Definition: parse_expr.c:2263
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
static Node * transformIndirection(ParseState *pstate, A_Indirection *ind)
Definition: parse_expr.c:434
static Node * transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
Definition: parse_expr.c:2656
static Node * transformAExprOpAny(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1028
A_Expr_Kind kind
Definition: parsenodes.h:276
#define PREC_GROUP_POSTFIX_IS
Definition: parse_expr.c:49
static Node * transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
Definition: parse_expr.c:2342
static Node * transformAExprNullIf(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1130
static Node * transformSubLink(ParseState *pstate, SubLink *sublink)
Definition: parse_expr.c:1855
Definition: nodes.h:529
int errcode(int sqlerrcode)
Definition: elog.c:610
Const * make_const(ParseState *pstate, Value *value, int location)
Definition: parse_node.c:455
static Node * transformBooleanTest(ParseState *pstate, BooleanTest *b)
Definition: parse_expr.c:2611
static Node * transformXmlExpr(ParseState *pstate, XmlExpr *x)
Definition: parse_expr.c:2434
static Node * transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
Definition: parse_expr.c:2293
#define ERROR
Definition: elog.h:43
static Node * transformBoolExpr(ParseState *pstate, BoolExpr *a)
Definition: parse_expr.c:1487
Expr * arg
Definition: primnodes.h:1219
static Node * transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
Definition: parse_expr.c:2105
static Node * transformAExprOpAll(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1051
static Node * transformFuncCall(ParseState *pstate, FuncCall *fn)
Definition: parse_expr.c:1523
void check_stack_depth(void)
Definition: postgres.c:3312
static Node * transformParamRef(ParseState *pstate, ParamRef *pref)
Definition: parse_expr.c:890
static Node * transformAExprOf(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1176
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2494
Node * lexpr
Definition: parsenodes.h:278
static Node * transformAExprOp(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:927
Definition: nodes.h:153
static Node * transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
Definition: parse_expr.c:1568
static Node * transformCollateClause(ParseState *pstate, CollateClause *c)
Definition: parse_expr.c:2833
Node * transformGroupingFunc(ParseState *pstate, GroupingFunc *p)
Definition: parse_agg.c:247
bool operator_precedence_warning
Definition: parse_expr.c:42
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3421
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
Expr * arg
Definition: primnodes.h:498
int location
Definition: parsenodes.h:290
Definition: value.h:42
int location
Definition: primnodes.h:1222
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
static Node * transformCaseExpr(ParseState *pstate, CaseExpr *c)
Definition: parse_expr.c:1715
#define nodeTag(nodeptr)
Definition: nodes.h:534
static Node * transformTypeCast(ParseState *pstate, TypeCast *tc)
Definition: parse_expr.c:2740
static Node * transformColumnRef(ParseState *pstate, ColumnRef *cref)
Definition: parse_expr.c:508
int errmsg(const char *fmt,...)
Definition: elog.c:824
static Node * transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
Definition: parse_expr.c:2568
#define elog(elevel,...)
Definition: elog.h:214
static Node * transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
Definition: parse_expr.c:2381
bool argisrow
Definition: primnodes.h:1221
Value val
Definition: parsenodes.h:289
long val
Definition: informix.c:664
static Node * transformAExprBetween(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1377
static Node * transformAExprIn(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1217

◆ transformFuncCall()

static Node * transformFuncCall ( ParseState pstate,
FuncCall fn 
)
static

Definition at line 1523 of file parse_expr.c.

References FuncCall::agg_order, FuncCall::agg_within_group, generate_unaccent_rules::args, FuncCall::args, Assert, EXPR_KIND_ORDER_BY, FuncCall::funcname, lappend(), lfirst, FuncCall::location, NIL, SortBy::node, ParseState::p_last_srf, ParseFuncOrColumn(), transformExpr(), and transformExprRecurse().

Referenced by transformExprRecurse().

1524 {
1525  Node *last_srf = pstate->p_last_srf;
1526  List *targs;
1527  ListCell *args;
1528 
1529  /* Transform the list of arguments ... */
1530  targs = NIL;
1531  foreach(args, fn->args)
1532  {
1533  targs = lappend(targs, transformExprRecurse(pstate,
1534  (Node *) lfirst(args)));
1535  }
1536 
1537  /*
1538  * When WITHIN GROUP is used, we treat its ORDER BY expressions as
1539  * additional arguments to the function, for purposes of function lookup
1540  * and argument type coercion. So, transform each such expression and add
1541  * them to the targs list. We don't explicitly mark where each argument
1542  * came from, but ParseFuncOrColumn can tell what's what by reference to
1543  * list_length(fn->agg_order).
1544  */
1545  if (fn->agg_within_group)
1546  {
1547  Assert(fn->agg_order != NIL);
1548  foreach(args, fn->agg_order)
1549  {
1550  SortBy *arg = (SortBy *) lfirst(args);
1551 
1552  targs = lappend(targs, transformExpr(pstate, arg->node,
1554  }
1555  }
1556 
1557  /* ... and hand off to ParseFuncOrColumn */
1558  return ParseFuncOrColumn(pstate,
1559  fn->funcname,
1560  targs,
1561  last_srf,
1562  fn,
1563  false,
1564  fn->location);
1565 }
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:145
Definition: nodes.h:529
int location
Definition: parsenodes.h:360
List * agg_order
Definition: parsenodes.h:353
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:88
List * lappend(List *list, void *datum)
Definition: list.c:321
Node * p_last_srf
Definition: parse_node.h:212
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
List * args
Definition: parsenodes.h:352
void * arg
List * funcname
Definition: parsenodes.h:351
Node * node
Definition: parsenodes.h:470
bool agg_within_group
Definition: parsenodes.h:355
Definition: pg_list.h:50

◆ transformIndirection()

static Node * transformIndirection ( ParseState pstate,
A_Indirection ind 
)
static

Definition at line 434 of file parse_expr.c.

References A_Indirection::arg, Assert, ereport, errcode(), errmsg(), ERROR, exprLocation(), exprType(), exprTypmod(), i, A_Indirection::indirection, InvalidOid, IsA, lappend(), lfirst, list_make1, NIL, ParseState::p_last_srf, ParseFuncOrColumn(), parser_errposition(), strVal, transformContainerSubscripts(), transformExprRecurse(), and unknown_attribute().

Referenced by transformExprRecurse().

435 {
436  Node *last_srf = pstate->p_last_srf;
437  Node *result = transformExprRecurse(pstate, ind->arg);
438  List *subscripts = NIL;
439  int location = exprLocation(result);
440  ListCell *i;
441 
442  /*
443  * We have to split any field-selection operations apart from
444  * subscripting. Adjacent A_Indices nodes have to be treated as a single
445  * multidimensional subscript operation.
446  */
447  foreach(i, ind->indirection)
448  {
449  Node *n = lfirst(i);
450 
451  if (IsA(n, A_Indices))
452  subscripts = lappend(subscripts, n);
453  else if (IsA(n, A_Star))
454  {
455  ereport(ERROR,
456  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
457  errmsg("row expansion via \"*\" is not supported here"),
458  parser_errposition(pstate, location)));
459  }
460  else
461  {
462  Node *newresult;
463 
464  Assert(IsA(n, String));
465 
466  /* process subscripts before this field selection */
467  if (subscripts)
468  result = (Node *) transformContainerSubscripts(pstate,
469  result,
470  exprType(result),
471  InvalidOid,
472  exprTypmod(result),
473  subscripts,
474  NULL);
475  subscripts = NIL;
476 
477  newresult = ParseFuncOrColumn(pstate,
478  list_make1(n),
479  list_make1(result),
480  last_srf,
481  NULL,
482  false,
483  location);
484  if (newresult == NULL)
485  unknown_attribute(pstate, result, strVal(n), location);
486  result = newresult;
487  }
488  }
489  /* process trailing subscripts, if any */
490  if (subscripts)
491  result = (Node *) transformContainerSubscripts(pstate,
492  result,
493  exprType(result),
494  InvalidOid,
495  exprTypmod(result),
496  subscripts,
497  NULL);
498 
499  return result;
500 }
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
static void unknown_attribute(ParseState *pstate, Node *relref, const char *attname, int location)
Definition: parse_expr.c:388
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
Definition: nodes.h:529
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:610
#define list_make1(x1)
Definition: pg_list.h:227
#define ERROR
Definition: elog.h:43
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:88
List * lappend(List *list, void *datum)
Definition: list.c:321
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, Oid elementType, int32 containerTypMod, List *indirection, Node *assignFrom)
Definition: parse_node.c:276
Node * p_last_srf
Definition: parse_node.h:212
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
List * indirection
Definition: parsenodes.h:407
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
Definition: pg_list.h:50

◆ transformMinMaxExpr()

static Node * transformMinMaxExpr ( ParseState pstate,
MinMaxExpr m 
)
static

Definition at line 2342 of file parse_expr.c.

References generate_unaccent_rules::args, MinMaxExpr::args, coerce_to_common_type(), IS_GREATEST, lappend(), lfirst, MinMaxExpr::location, makeNode, MinMaxExpr::minmaxtype, NIL, MinMaxExpr::op, select_common_type(), and transformExprRecurse().

Referenced by transformExprRecurse().

2343 {
2344  MinMaxExpr *newm = makeNode(MinMaxExpr);
2345  List *newargs = NIL;
2346  List *newcoercedargs = NIL;
2347  const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2348  ListCell *args;
2349 
2350  newm->op = m->op;
2351  foreach(args, m->args)
2352  {
2353  Node *e = (Node *) lfirst(args);
2354  Node *newe;
2355 
2356  newe = transformExprRecurse(pstate, e);
2357  newargs = lappend(newargs, newe);
2358  }
2359 
2360  newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2361  /* minmaxcollid and inputcollid will be set by parse_collate.c */
2362 
2363  /* Convert arguments if necessary */
2364  foreach(args, newargs)
2365  {
2366  Node *e = (Node *) lfirst(args);
2367  Node *newe;
2368 
2369  newe = coerce_to_common_type(pstate, e,
2370  newm->minmaxtype,
2371  funcname);
2372  newcoercedargs = lappend(newcoercedargs, newe);
2373  }
2374 
2375  newm->args = newcoercedargs;
2376  newm->location = m->location;
2377  return (Node *) newm;
2378 }
Oid minmaxtype
Definition: primnodes.h:1102
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
List * args
Definition: primnodes.h:1106
Definition: nodes.h:529
int location
Definition: primnodes.h:1107
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:321
#define makeNode(_type_)
Definition: nodes.h:577
#define lfirst(lc)
Definition: pg_list.h:190
e
Definition: preproc-init.c:82
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
MinMaxOp op
Definition: primnodes.h:1105
Definition: pg_list.h:50

◆ transformMultiAssignRef()

static Node * transformMultiAssignRef ( ParseState pstate,
MultiAssignRef maref 
)
static

Definition at line 1568 of file parse_expr.c.

References RowExpr::args, Assert, castNode, MultiAssignRef::colno, count_nonjunk_tlist_entries(), elog, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, EXPR_KIND_UPDATE_SOURCE, EXPR_SUBLINK, exprCollation(), exprLocation(), exprType(), exprTypmod(), IsA, lappend(), list_delete_ptr(), list_length(), list_nth(), llast, Param::location, RowExpr::location, makeNode, makeTargetEntry(), MULTIEXPR_SUBLINK, MultiAssignRef::ncolumns, NIL, ParseState::p_expr_kind, ParseState::p_multiassign_exprs, PARAM_MULTIEXPR, Param::paramcollid, Param::paramid, Param::paramkind, Param::paramtype, Param::paramtypmod, parser_errposition(), TargetEntry::resjunk, MultiAssignRef::source, SubLink::subLinkId, SubLink::subLinkType, SubLink::subselect, Query::targetList, transformExprRecurse(), and transformRowExpr().

Referenced by transformExprRecurse().

1569 {
1570  SubLink *sublink;
1571  RowExpr *rexpr;
1572  Query *qtree;
1573  TargetEntry *tle;
1574 
1575  /* We should only see this in first-stage processing of UPDATE tlists */
1577 
1578  /* We only need to transform the source if this is the first column */
1579  if (maref->colno == 1)
1580  {
1581  /*
1582  * For now, we only allow EXPR SubLinks and RowExprs as the source of
1583  * an UPDATE multiassignment. This is sufficient to cover interesting
1584  * cases; at worst, someone would have to write (SELECT * FROM expr)
1585  * to expand a composite-returning expression of another form.
1586  */
1587  if (IsA(maref->source, SubLink) &&
1588  ((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
1589  {
1590  /* Relabel it as a MULTIEXPR_SUBLINK */
1591  sublink = (SubLink *) maref->source;
1592  sublink->subLinkType = MULTIEXPR_SUBLINK;
1593  /* And transform it */
1594  sublink = (SubLink *) transformExprRecurse(pstate,
1595  (Node *) sublink);
1596 
1597  qtree = castNode(Query, sublink->subselect);
1598 
1599  /* Check subquery returns required number of columns */
1600  if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
1601  ereport(ERROR,
1602  (errcode(ERRCODE_SYNTAX_ERROR),
1603  errmsg("number of columns does not match number of values"),
1604  parser_errposition(pstate, sublink->location)));
1605 
1606  /*
1607  * Build a resjunk tlist item containing the MULTIEXPR SubLink,
1608  * and add it to pstate->p_multiassign_exprs, whence it will later
1609  * get appended to the completed targetlist. We needn't worry
1610  * about selecting a resno for it; transformUpdateStmt will do
1611  * that.
1612  */
1613  tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
1615  tle);
1616 
1617  /*
1618  * Assign a unique-within-this-targetlist ID to the MULTIEXPR
1619  * SubLink. We can just use its position in the
1620  * p_multiassign_exprs list.
1621  */
1622  sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
1623  }
1624  else if (IsA(maref->source, RowExpr))
1625  {
1626  /* Transform the RowExpr, allowing SetToDefault items */
1627  rexpr = (RowExpr *) transformRowExpr(pstate,
1628  (RowExpr *) maref->source,
1629  true);
1630 
1631  /* Check it returns required number of columns */
1632  if (list_length(rexpr->args) != maref->ncolumns)
1633  ereport(ERROR,
1634  (errcode(ERRCODE_SYNTAX_ERROR),
1635  errmsg("number of columns does not match number of values"),
1636  parser_errposition(pstate, rexpr->location)));
1637 
1638  /*
1639  * Temporarily append it to p_multiassign_exprs, so we can get it
1640  * back when we come back here for additional columns.
1641  */
1642  tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
1644  tle);
1645  }
1646  else
1647  ereport(ERROR,
1648  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1649  errmsg("source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression"),
1650  parser_errposition(pstate, exprLocation(maref->source))));
1651  }
1652  else
1653  {
1654  /*
1655  * Second or later column in a multiassignment. Re-fetch the
1656  * transformed SubLink or RowExpr, which we assume is still the last
1657  * entry in p_multiassign_exprs.
1658  */
1659  Assert(pstate->p_multiassign_exprs != NIL);
1660  tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
1661  }
1662 
1663  /*
1664  * Emit the appropriate output expression for the current column
1665  */
1666  if (IsA(tle->expr, SubLink))
1667  {
1668  Param *param;
1669 
1670  sublink = (SubLink *) tle->expr;
1671  Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
1672  qtree = castNode(Query, sublink->subselect);
1673 
1674  /* Build a Param representing the current subquery output column */
1675  tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
1676  Assert(!tle->resjunk);
1677 
1678  param = makeNode(Param);
1679  param->paramkind = PARAM_MULTIEXPR;
1680  param->paramid = (sublink->subLinkId << 16) | maref->colno;
1681  param->paramtype = exprType((Node *) tle->expr);
1682  param->paramtypmod = exprTypmod((Node *) tle->expr);
1683  param->paramcollid = exprCollation((Node *) tle->expr);
1684  param->location = exprLocation((Node *) tle->expr);
1685 
1686  return (Node *) param;
1687  }
1688 
1689  if (IsA(tle->expr, RowExpr))
1690  {
1691  Node *result;
1692 
1693  rexpr = (RowExpr *) tle->expr;
1694 
1695  /* Just extract and return the next element of the RowExpr */
1696  result = (Node *) list_nth(rexpr->args, maref->colno - 1);
1697 
1698  /*
1699  * If we're at the last column, delete the RowExpr from
1700  * p_multiassign_exprs; we don't need it anymore, and don't want it in
1701  * the finished UPDATE tlist.
1702  */
1703  if (maref->colno == maref->ncolumns)
1704  pstate->p_multiassign_exprs =
1705  list_delete_ptr(pstate->p_multiassign_exprs, tle);
1706 
1707  return result;
1708  }
1709 
1710  elog(ERROR, "unexpected expr type in multiassign list");
1711  return NULL; /* keep compiler quiet */
1712 }
#define NIL
Definition: pg_list.h:65
static Node * transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
Definition: parse_expr.c:2263
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:163
List * args
Definition: primnodes.h:1022
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
#define llast(l)
Definition: pg_list.h:215
ParamKind paramkind
Definition: primnodes.h:262
Definition: nodes.h:529
int errcode(int sqlerrcode)
Definition: elog.c:610
List * p_multiassign_exprs
Definition: parse_node.h:196
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:796
List * targetList
Definition: parsenodes.h:140
bool resjunk
Definition: primnodes.h:1414
#define ERROR
Definition: elog.h:43
Oid paramcollid
Definition: primnodes.h:266
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
int location
Definition: primnodes.h:267
int location
Definition: primnodes.h:1039
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:236
List * lappend(List *list, void *datum)
Definition: list.c:321
ParseExprKind p_expr_kind
Definition: parse_node.h:194
#define ereport(elevel,...)
Definition: elog.h:144
int32 paramtypmod
Definition: primnodes.h:265
#define makeNode(_type_)
Definition: nodes.h:577
#define Assert(condition)
Definition: c.h:738
Expr * expr
Definition: primnodes.h:1407
int paramid
Definition: primnodes.h:263
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
int count_nonjunk_tlist_entries(List *tlist)
Definition: tlist.c:208
Oid paramtype
Definition: primnodes.h:264

◆ transformParamRef()

static Node * transformParamRef ( ParseState pstate,
ParamRef pref 
)
static

Definition at line 890 of file parse_expr.c.

References ereport, errcode(), errmsg(), ERROR, ParamRef::location, ParamRef::number, ParseState::p_paramref_hook, and parser_errposition().

Referenced by transformExprRecurse().

891 {
892  Node *result;
893 
894  /*
895  * The core parser knows nothing about Params. If a hook is supplied,
896  * call it. If not, or if the hook returns NULL, throw a generic error.
897  */
898  if (pstate->p_paramref_hook != NULL)
899  result = pstate->p_paramref_hook(pstate, pref);
900  else
901  result = NULL;
902 
903  if (result == NULL)
904  ereport(ERROR,
905  (errcode(ERRCODE_UNDEFINED_PARAMETER),
906  errmsg("there is no parameter $%d", pref->number),
907  parser_errposition(pstate, pref->location)));
908 
909  return result;
910 }
Definition: nodes.h:529
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:220
int number
Definition: parsenodes.h:246
#define ereport(elevel,...)
Definition: elog.h:144
int location
Definition: parsenodes.h:247
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ transformRowExpr()

static Node * transformRowExpr ( ParseState pstate,
RowExpr r,
bool  allowDefault 
)
static

Definition at line 2263 of file parse_expr.c.

References RowExpr::args, COERCE_IMPLICIT_CAST, RowExpr::colnames, lappend(), list_length(), RowExpr::location, makeNode, makeString(), NIL, ParseState::p_expr_kind, pstrdup(), RowExpr::row_format, RowExpr::row_typeid, snprintf, and transformExpressionList().

Referenced by transformExprRecurse(), and transformMultiAssignRef().

2264 {
2265  RowExpr *newr;
2266  char fname[16];
2267  int fnum;
2268 
2269  newr = makeNode(RowExpr);
2270 
2271  /* Transform the field expressions */
2272  newr->args = transformExpressionList(pstate, r->args,
2273  pstate->p_expr_kind, allowDefault);
2274 
2275  /* Barring later casting, we consider the type RECORD */
2276  newr->row_typeid = RECORDOID;
2278 
2279  /* ROW() has anonymous columns, so invent some field names */
2280  newr->colnames = NIL;
2281  for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2282  {
2283  snprintf(fname, sizeof(fname), "f%d", fnum);
2284  newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2285  }
2286 
2287  newr->location = r->location;
2288 
2289  return (Node *) newr;
2290 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
List * args
Definition: primnodes.h:1022
char * pstrdup(const char *in)
Definition: mcxt.c:1186
Definition: nodes.h:529
List * colnames
Definition: primnodes.h:1038
int location
Definition: primnodes.h:1039
List * lappend(List *list, void *datum)
Definition: list.c:321
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
Definition: parse_target.c:230
ParseExprKind p_expr_kind
Definition: parse_node.h:194
#define makeNode(_type_)
Definition: nodes.h:577
Oid row_typeid
Definition: primnodes.h:1023
static int list_length(const List *l)
Definition: pg_list.h:169
CoercionForm row_format
Definition: primnodes.h:1037
#define snprintf
Definition: port.h:193

◆ transformSQLValueFunction()

static Node * transformSQLValueFunction ( ParseState pstate,
SQLValueFunction svf 
)
static

Definition at line 2381 of file parse_expr.c.

References anytime_typmod_check(), anytimestamp_typmod_check(), SQLValueFunction::op, SVFOP_CURRENT_CATALOG, SVFOP_CURRENT_DATE, SVFOP_CURRENT_ROLE, SVFOP_CURRENT_SCHEMA, SVFOP_CURRENT_TIME, SVFOP_CURRENT_TIME_N, SVFOP_CURRENT_TIMESTAMP, SVFOP_CURRENT_TIMESTAMP_N, SVFOP_CURRENT_USER, SVFOP_LOCALTIME, SVFOP_LOCALTIME_N, SVFOP_LOCALTIMESTAMP, SVFOP_LOCALTIMESTAMP_N, SVFOP_SESSION_USER, SVFOP_USER, SQLValueFunction::type, and SQLValueFunction::typmod.

Referenced by transformExprRecurse().

2382 {
2383  /*
2384  * All we need to do is insert the correct result type and (where needed)
2385  * validate the typmod, so we just modify the node in-place.
2386  */
2387  switch (svf->op)
2388  {
2389  case SVFOP_CURRENT_DATE:
2390  svf->type = DATEOID;
2391  break;
2392  case SVFOP_CURRENT_TIME:
2393  svf->type = TIMETZOID;
2394  break;
2395  case SVFOP_CURRENT_TIME_N:
2396  svf->type = TIMETZOID;
2397  svf->typmod = anytime_typmod_check(true, svf->typmod);
2398  break;
2400  svf->type = TIMESTAMPTZOID;
2401  break;
2403  svf->type = TIMESTAMPTZOID;
2404  svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2405  break;
2406  case SVFOP_LOCALTIME:
2407  svf->type = TIMEOID;
2408  break;
2409  case SVFOP_LOCALTIME_N:
2410  svf->type = TIMEOID;
2411  svf->typmod = anytime_typmod_check(false, svf->typmod);
2412  break;
2413  case SVFOP_LOCALTIMESTAMP:
2414  svf->type = TIMESTAMPOID;
2415  break;
2417  svf->type = TIMESTAMPOID;
2418  svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2419  break;
2420  case SVFOP_CURRENT_ROLE:
2421  case SVFOP_CURRENT_USER:
2422  case SVFOP_USER:
2423  case SVFOP_SESSION_USER:
2424  case SVFOP_CURRENT_CATALOG:
2425  case SVFOP_CURRENT_SCHEMA:
2426  svf->type = NAMEOID;
2427  break;
2428  }
2429 
2430  return (Node *) svf;
2431 }
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:67
Definition: nodes.h:529
SQLValueFunctionOp op
Definition: primnodes.h:1143
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:101

◆ transformSubLink()

static Node * transformSubLink ( ParseState pstate,
SubLink sublink 
)
static

Definition at line 1855 of file parse_expr.c.

References _, ARRAY_SUBLINK, Assert, CMD_SELECT, Query::commandType, count_nonjunk_tlist_entries(), elog, emit_precedence_warnings(), ereport, errcode(), errmsg(), errmsg_internal(), ERROR, EXISTS_SUBLINK, TargetEntry::expr, EXPR_KIND_ALTER_COL_TRANSFORM, EXPR_KIND_CALL_ARGUMENT, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_COLUMN_DEFAULT, EXPR_KIND_COPY_WHERE, EXPR_KIND_DISTINCT_ON, EXPR_KIND_DOMAIN_CHECK, EXPR_KIND_EXECUTE_PARAMETER, EXPR_KIND_FILTER, EXPR_KIND_FROM_FUNCTION, EXPR_KIND_FROM_SUBSELECT, EXPR_KIND_FUNCTION_DEFAULT, EXPR_KIND_GENERATED_COLUMN, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_INDEX_EXPRESSION, EXPR_KIND_INDEX_PREDICATE, EXPR_KIND_INSERT_TARGET, EXPR_KIND_JOIN_ON, EXPR_KIND_JOIN_USING, EXPR_KIND_LIMIT, EXPR_KIND_NONE, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_OTHER, EXPR_KIND_PARTITION_BOUND, EXPR_KIND_PARTITION_EXPRESSION, EXPR_KIND_POLICY, EXPR_KIND_RETURNING, EXPR_KIND_SELECT_TARGET, EXPR_KIND_TRIGGER_WHEN, EXPR_KIND_UPDATE_SOURCE, EXPR_KIND_UPDATE_TARGET, EXPR_KIND_VALUES, EXPR_KIND_VALUES_SINGLE, EXPR_KIND_WHERE, EXPR_KIND_WINDOW_FRAME_GROUPS, EXPR_KIND_WINDOW_FRAME_RANGE, EXPR_KIND_WINDOW_FRAME_ROWS, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, EXPR_SUBLINK, exprCollation(), exprType(), exprTypmod(), IsA, lappend(), lfirst, list_length(), list_make1, llast, Param::location, SubLink::location, make_row_comparison_op(), makeNode, makeString(), MULTIEXPR_SUBLINK, NIL, operator_precedence_warning, SubLink::operName, ParseState::p_expr_kind, ParseState::p_hasSubLinks, PARAM_SUBLINK, Param::paramcollid, Param::paramid, Param::paramkind, Param::paramtype, Param::paramtypmod, parse_sub_analyze(), parser_errposition(), PREC_GROUP_IN, PREC_GROUP_POSTFIX_OP, TargetEntry::resjunk, TargetEntry::resno, strVal, SubLink::subLinkType, SubLink::subselect, Query::targetList, SubLink::testexpr, and transformExprRecurse().

Referenced by transformExprRecurse().

1856 {
1857  Node *result = (Node *) sublink;
1858  Query *qtree;
1859  const char *err;
1860 
1861  /*
1862  * Check to see if the sublink is in an invalid place within the query. We
1863  * allow sublinks everywhere in SELECT/INSERT/UPDATE/DELETE, but generally
1864  * not in utility statements.
1865  */
1866  err = NULL;
1867  switch (pstate->p_expr_kind)
1868  {
1869  case EXPR_KIND_NONE:
1870  Assert(false); /* can't happen */
1871  break;
1872  case EXPR_KIND_OTHER:
1873  /* Accept sublink here; caller must throw error if wanted */
1874  break;
1875  case EXPR_KIND_JOIN_ON:
1876  case EXPR_KIND_JOIN_USING:
1879  case EXPR_KIND_WHERE:
1880  case EXPR_KIND_POLICY:
1881  case EXPR_KIND_HAVING:
1882  case EXPR_KIND_FILTER:
1892  case EXPR_KIND_GROUP_BY:
1893  case EXPR_KIND_ORDER_BY:
1894  case EXPR_KIND_DISTINCT_ON:
1895  case EXPR_KIND_LIMIT:
1896  case EXPR_KIND_OFFSET:
1897  case EXPR_KIND_RETURNING:
1898  case EXPR_KIND_VALUES:
1900  /* okay */
1901  break;
1904  err = _("cannot use subquery in check constraint");
1905  break;
1908  err = _("cannot use subquery in DEFAULT expression");
1909  break;
1911  err = _("cannot use subquery in index expression");
1912  break;
1914  err = _("cannot use subquery in index predicate");
1915  break;
1917  err = _("cannot use subquery in transform expression");
1918  break;
1920  err = _("cannot use subquery in EXECUTE parameter");
1921  break;
1923  err = _("cannot use subquery in trigger WHEN condition");
1924  break;
1926  err = _("cannot use subquery in partition bound");
1927  break;
1929  err = _("cannot use subquery in partition key expression");
1930  break;
1932  err = _("cannot use subquery in CALL argument");
1933  break;
1934  case EXPR_KIND_COPY_WHERE:
1935  err = _("cannot use subquery in COPY FROM WHERE condition");
1936  break;
1938  err = _("cannot use subquery in column generation expression");
1939  break;
1940 
1941  /*
1942  * There is intentionally no default: case here, so that the
1943  * compiler will warn if we add a new ParseExprKind without
1944