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/tlist.h"
#include "optimizer/var.h"
#include "parser/analyze.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 "parser/parse_agg.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, RangeTblEntry *rte, 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 57 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_EQUAL

#define PREC_GROUP_EQUAL   4 /* = */

Definition at line 54 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 52 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 63 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_LESS

#define PREC_GROUP_LESS   3 /* < > */

Definition at line 53 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_LESS_EQUAL

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

Definition at line 55 of file parse_expr.c.

Referenced by operator_precedence_group().

◆ PREC_GROUP_LIKE

#define PREC_GROUP_LIKE   6 /* LIKE ILIKE SIMILAR */

Definition at line 56 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 60 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 59 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 64 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 3343 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().

3347 {
3348  int cgroup;
3349  const char *copname;
3350 
3351  Assert(opgroup > 0);
3352 
3353  /*
3354  * Complain if left child, which should be same or higher precedence
3355  * according to current rules, used to be lower precedence.
3356  *
3357  * Exception to precedence rules: if left child is IN or NOT IN or a
3358  * postfix operator, the grouping is syntactically forced regardless of
3359  * precedence.
3360  */
3361  cgroup = operator_precedence_group(lchild, &copname);
3362  if (cgroup > 0)
3363  {
3364  if (oldprecedence_l[cgroup] < oldprecedence_r[opgroup] &&
3365  cgroup != PREC_GROUP_IN &&
3366  cgroup != PREC_GROUP_NOT_IN &&
3367  cgroup != PREC_GROUP_POSTFIX_OP &&
3368  cgroup != PREC_GROUP_POSTFIX_IS)
3369  ereport(WARNING,
3370  (errmsg("operator precedence change: %s is now lower precedence than %s",
3371  opname, copname),
3372  parser_errposition(pstate, location)));
3373  }
3374 
3375  /*
3376  * Complain if right child, which should be higher precedence according to
3377  * current rules, used to be same or lower precedence.
3378  *
3379  * Exception to precedence rules: if right child is a prefix operator, the
3380  * grouping is syntactically forced regardless of precedence.
3381  */
3382  cgroup = operator_precedence_group(rchild, &copname);
3383  if (cgroup > 0)
3384  {
3385  if (oldprecedence_r[cgroup] <= oldprecedence_l[opgroup] &&
3386  cgroup != PREC_GROUP_PREFIX_OP)
3387  ereport(WARNING,
3388  (errmsg("operator precedence change: %s is now lower precedence than %s",
3389  opname, copname),
3390  parser_errposition(pstate, location)));
3391  }
3392 }
#define PREC_GROUP_PREFIX_OP
Definition: parse_expr.c:64
#define PREC_GROUP_POSTFIX_IS
Definition: parse_expr.c:51
static int operator_precedence_group(Node *node, const char **nodename)
Definition: parse_expr.c:3100
#define PREC_GROUP_IN
Definition: parse_expr.c:58
#define PREC_GROUP_NOT_IN
Definition: parse_expr.c:61
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
#define Assert(condition)
Definition: c.h:699
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
static const int oldprecedence_l[]
Definition: parse_expr.c:85
int errmsg(const char *fmt,...)
Definition: elog.c:797
static const int oldprecedence_r[]
Definition: parse_expr.c:88
#define PREC_GROUP_POSTFIX_OP
Definition: parse_expr.c:62

◆ exprIsNullConstant()

static bool exprIsNullConstant ( Node arg)
static

Definition at line 835 of file parse_expr.c.

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

Referenced by transformAExprDistinct(), and transformAExprOp().

836 {
837  if (arg && IsA(arg, A_Const))
838  {
839  A_Const *con = (A_Const *) arg;
840 
841  if (con->val.type == T_Null)
842  return true;
843  }
844  return false;
845 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Definition: nodes.h:291
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 3041 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().

3043 {
3044  Expr *result;
3045 
3046  result = make_op(pstate, opname, ltree, rtree,
3047  pstate->p_last_srf, location);
3048  if (((OpExpr *) result)->opresulttype != BOOLOID)
3049  ereport(ERROR,
3050  (errcode(ERRCODE_DATATYPE_MISMATCH),
3051  errmsg("IS DISTINCT FROM requires = operator to yield boolean"),
3052  parser_errposition(pstate, location)));
3053  if (((OpExpr *) result)->opretset)
3054  ereport(ERROR,
3055  (errcode(ERRCODE_DATATYPE_MISMATCH),
3056  /* translator: %s is name of a SQL construct, eg NULLIF */
3057  errmsg("%s must not return a set", "IS DISTINCT FROM"),
3058  parser_errposition(pstate, location)));
3059 
3060  /*
3061  * We rely on DistinctExpr and OpExpr being same struct
3062  */
3063  NodeSetTag(result, T_DistinctExpr);
3064 
3065  return result;
3066 }
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:566
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Node * p_last_srf
Definition: parse_node.h:206
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ make_nulltest_from_distinct()

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

Definition at line 3074 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().

3075 {
3076  NullTest *nt = makeNode(NullTest);
3077 
3078  nt->arg = (Expr *) transformExprRecurse(pstate, arg);
3079  /* the argument can be any type, so don't coerce it */
3080  if (distincta->kind == AEXPR_NOT_DISTINCT)
3081  nt->nulltesttype = IS_NULL;
3082  else
3083  nt->nulltesttype = IS_NOT_NULL;
3084  /* argisrow = false is correct whether or not arg is composite */
3085  nt->argisrow = false;
3086  nt->location = distincta->location;
3087  return (Node *) nt;
3088 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
A_Expr_Kind kind
Definition: parsenodes.h:276
Definition: nodes.h:517
Expr * arg
Definition: primnodes.h:1188
int location
Definition: parsenodes.h:280
NullTestType nulltesttype
Definition: primnodes.h:1189
#define makeNode(_type_)
Definition: nodes.h:565
int location
Definition: primnodes.h:1191
bool argisrow
Definition: primnodes.h:1190

◆ make_row_comparison_op()

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

Definition at line 2795 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().

2797 {
2798  RowCompareExpr *rcexpr;
2799  RowCompareType rctype;
2800  List *opexprs;
2801  List *opnos;
2802  List *opfamilies;
2803  ListCell *l,
2804  *r;
2805  List **opinfo_lists;
2806  Bitmapset *strats;
2807  int nopers;
2808  int i;
2809 
2810  nopers = list_length(largs);
2811  if (nopers != list_length(rargs))
2812  ereport(ERROR,
2813  (errcode(ERRCODE_SYNTAX_ERROR),
2814  errmsg("unequal number of entries in row expressions"),
2815  parser_errposition(pstate, location)));
2816 
2817  /*
2818  * We can't compare zero-length rows because there is no principled basis
2819  * for figuring out what the operator is.
2820  */
2821  if (nopers == 0)
2822  ereport(ERROR,
2823  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2824  errmsg("cannot compare rows of zero length"),
2825  parser_errposition(pstate, location)));
2826 
2827  /*
2828  * Identify all the pairwise operators, using make_op so that behavior is
2829  * the same as in the simple scalar case.
2830  */
2831  opexprs = NIL;
2832  forboth(l, largs, r, rargs)
2833  {
2834  Node *larg = (Node *) lfirst(l);
2835  Node *rarg = (Node *) lfirst(r);
2836  OpExpr *cmp;
2837 
2838  cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
2839  pstate->p_last_srf, location));
2840 
2841  /*
2842  * We don't use coerce_to_boolean here because we insist on the
2843  * operator yielding boolean directly, not via coercion. If it
2844  * doesn't yield bool it won't be in any index opfamilies...
2845  */
2846  if (cmp->opresulttype != BOOLOID)
2847  ereport(ERROR,
2848  (errcode(ERRCODE_DATATYPE_MISMATCH),
2849  errmsg("row comparison operator must yield type boolean, "
2850  "not type %s",
2852  parser_errposition(pstate, location)));
2853  if (expression_returns_set((Node *) cmp))
2854  ereport(ERROR,
2855  (errcode(ERRCODE_DATATYPE_MISMATCH),
2856  errmsg("row comparison operator must not return a set"),
2857  parser_errposition(pstate, location)));
2858  opexprs = lappend(opexprs, cmp);
2859  }
2860 
2861  /*
2862  * If rows are length 1, just return the single operator. In this case we
2863  * don't insist on identifying btree semantics for the operator (but we
2864  * still require it to return boolean).
2865  */
2866  if (nopers == 1)
2867  return (Node *) linitial(opexprs);
2868 
2869  /*
2870  * Now we must determine which row comparison semantics (= <> < <= > >=)
2871  * apply to this set of operators. We look for btree opfamilies
2872  * containing the operators, and see which interpretations (strategy
2873  * numbers) exist for each operator.
2874  */
2875  opinfo_lists = (List **) palloc(nopers * sizeof(List *));
2876  strats = NULL;
2877  i = 0;
2878  foreach(l, opexprs)
2879  {
2880  Oid opno = ((OpExpr *) lfirst(l))->opno;
2881  Bitmapset *this_strats;
2882  ListCell *j;
2883 
2884  opinfo_lists[i] = get_op_btree_interpretation(opno);
2885 
2886  /*
2887  * convert strategy numbers into a Bitmapset to make the intersection
2888  * calculation easy.
2889  */
2890  this_strats = NULL;
2891  foreach(j, opinfo_lists[i])
2892  {
2893  OpBtreeInterpretation *opinfo = lfirst(j);
2894 
2895  this_strats = bms_add_member(this_strats, opinfo->strategy);
2896  }
2897  if (i == 0)
2898  strats = this_strats;
2899  else
2900  strats = bms_int_members(strats, this_strats);
2901  i++;
2902  }
2903 
2904  /*
2905  * If there are multiple common interpretations, we may use any one of
2906  * them ... this coding arbitrarily picks the lowest btree strategy
2907  * number.
2908  */
2909  i = bms_first_member(strats);
2910  if (i < 0)
2911  {
2912  /* No common interpretation, so fail */
2913  ereport(ERROR,
2914  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2915  errmsg("could not determine interpretation of row comparison operator %s",
2916  strVal(llast(opname))),
2917  errhint("Row comparison operators must be associated with btree operator families."),
2918  parser_errposition(pstate, location)));
2919  }
2920  rctype = (RowCompareType) i;
2921 
2922  /*
2923  * For = and <> cases, we just combine the pairwise operators with AND or
2924  * OR respectively.
2925  */
2926  if (rctype == ROWCOMPARE_EQ)
2927  return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
2928  if (rctype == ROWCOMPARE_NE)
2929  return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
2930 
2931  /*
2932  * Otherwise we need to choose exactly which opfamily to associate with
2933  * each operator.
2934  */
2935  opfamilies = NIL;
2936  for (i = 0; i < nopers; i++)
2937  {
2938  Oid opfamily = InvalidOid;
2939  ListCell *j;
2940 
2941  foreach(j, opinfo_lists[i])
2942  {
2943  OpBtreeInterpretation *opinfo = lfirst(j);
2944 
2945  if (opinfo->strategy == rctype)
2946  {
2947  opfamily = opinfo->opfamily_id;
2948  break;
2949  }
2950  }
2951  if (OidIsValid(opfamily))
2952  opfamilies = lappend_oid(opfamilies, opfamily);
2953  else /* should not happen */
2954  ereport(ERROR,
2955  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2956  errmsg("could not determine interpretation of row comparison operator %s",
2957  strVal(llast(opname))),
2958  errdetail("There are multiple equally-plausible candidates."),
2959  parser_errposition(pstate, location)));
2960  }
2961 
2962  /*
2963  * Now deconstruct the OpExprs and create a RowCompareExpr.
2964  *
2965  * Note: can't just reuse the passed largs/rargs lists, because of
2966  * possibility that make_op inserted coercion operations.
2967  */
2968  opnos = NIL;
2969  largs = NIL;
2970  rargs = NIL;
2971  foreach(l, opexprs)
2972  {
2973  OpExpr *cmp = (OpExpr *) lfirst(l);
2974 
2975  opnos = lappend_oid(opnos, cmp->opno);
2976  largs = lappend(largs, linitial(cmp->args));
2977  rargs = lappend(rargs, lsecond(cmp->args));
2978  }
2979 
2980  rcexpr = makeNode(RowCompareExpr);
2981  rcexpr->rctype = rctype;
2982  rcexpr->opnos = opnos;
2983  rcexpr->opfamilies = opfamilies;
2984  rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
2985  rcexpr->largs = largs;
2986  rcexpr->rargs = rargs;
2987 
2988  return (Node *) rcexpr;
2989 }
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:69
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:1023
int errhint(const char *fmt,...)
Definition: elog.c:987
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
RowCompareType rctype
Definition: primnodes.h:1039
List * opfamilies
Definition: primnodes.h:1041
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
#define llast(l)
Definition: pg_list.h:131
Definition: nodes.h:517
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
#define OidIsValid(objectId)
Definition: c.h:605
#define lsecond(l)
Definition: pg_list.h:116
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:368
Oid opresulttype
Definition: primnodes.h:499
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
List * get_op_btree_interpretation(Oid opno)
Definition: lsyscache.c:598
Node * p_last_srf
Definition: parse_node.h:206
#define InvalidOid
Definition: postgres_ext.h:36
RowCompareType
Definition: primnodes.h:1025
#define makeNode(_type_)
Definition: nodes.h:565
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:764
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Oid opno
Definition: primnodes.h:497
List * args
Definition: primnodes.h:503
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:929
List * inputcollids
Definition: primnodes.h:1042
Definition: pg_list.h:45
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 2997 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().

3000 {
3001  Node *result = NULL;
3002  List *largs = lrow->args;
3003  List *rargs = rrow->args;
3004  ListCell *l,
3005  *r;
3006 
3007  if (list_length(largs) != list_length(rargs))
3008  ereport(ERROR,
3009  (errcode(ERRCODE_SYNTAX_ERROR),
3010  errmsg("unequal number of entries in row expressions"),
3011  parser_errposition(pstate, location)));
3012 
3013  forboth(l, largs, r, rargs)
3014  {
3015  Node *larg = (Node *) lfirst(l);
3016  Node *rarg = (Node *) lfirst(r);
3017  Node *cmp;
3018 
3019  cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
3020  if (result == NULL)
3021  result = cmp;
3022  else
3023  result = (Node *) makeBoolExpr(OR_EXPR,
3024  list_make2(result, cmp),
3025  location);
3026  }
3027 
3028  if (result == NULL)
3029  {
3030  /* zero-length rows? Generate constant FALSE */
3031  result = makeBoolConst(false, false);
3032  }
3033 
3034  return result;
3035 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
List * args
Definition: primnodes.h:991
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Definition: nodes.h:517
int errcode(int sqlerrcode)
Definition: elog.c:575
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:3041
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:368
#define ERROR
Definition: elog.h:43
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:356
#define ereport(elevel, rest)
Definition: elog.h:122
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: pg_list.h:45
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 3100 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().

3101 {
3102  int group = 0;
3103 
3104  *nodename = NULL;
3105  if (node == NULL)
3106  return 0;
3107 
3108  if (IsA(node, A_Expr))
3109  {
3110  A_Expr *aexpr = (A_Expr *) node;
3111 
3112  if (aexpr->kind == AEXPR_OP &&
3113  aexpr->lexpr != NULL &&
3114  aexpr->rexpr != NULL)
3115  {
3116  /* binary operator */
3117  if (list_length(aexpr->name) == 1)
3118  {
3119  *nodename = strVal(linitial(aexpr->name));
3120  /* Ignore if op was always higher priority than IS-tests */
3121  if (strcmp(*nodename, "+") == 0 ||
3122  strcmp(*nodename, "-") == 0 ||
3123  strcmp(*nodename, "*") == 0 ||
3124  strcmp(*nodename, "/") == 0 ||
3125  strcmp(*nodename, "%") == 0 ||
3126  strcmp(*nodename, "^") == 0)
3127  group = 0;
3128  else if (strcmp(*nodename, "<") == 0 ||
3129  strcmp(*nodename, ">") == 0)
3130  group = PREC_GROUP_LESS;
3131  else if (strcmp(*nodename, "=") == 0)
3132  group = PREC_GROUP_EQUAL;
3133  else if (strcmp(*nodename, "<=") == 0 ||
3134  strcmp(*nodename, ">=") == 0 ||
3135  strcmp(*nodename, "<>") == 0)
3136  group = PREC_GROUP_LESS_EQUAL;
3137  else
3138  group = PREC_GROUP_INFIX_OP;
3139  }
3140  else
3141  {
3142  /* schema-qualified operator syntax */
3143  *nodename = "OPERATOR()";
3144  group = PREC_GROUP_INFIX_OP;
3145  }
3146  }
3147  else if (aexpr->kind == AEXPR_OP &&
3148  aexpr->lexpr == NULL &&
3149  aexpr->rexpr != NULL)
3150  {
3151  /* prefix operator */
3152  if (list_length(aexpr->name) == 1)
3153  {
3154  *nodename = strVal(linitial(aexpr->name));
3155  /* Ignore if op was always higher priority than IS-tests */
3156  if (strcmp(*nodename, "+") == 0 ||
3157  strcmp(*nodename, "-"))
3158  group = 0;
3159  else
3160  group = PREC_GROUP_PREFIX_OP;
3161  }
3162  else
3163  {
3164  /* schema-qualified operator syntax */
3165  *nodename = "OPERATOR()";
3166  group = PREC_GROUP_PREFIX_OP;
3167  }
3168  }
3169  else if (aexpr->kind == AEXPR_OP &&
3170  aexpr->lexpr != NULL &&
3171  aexpr->rexpr == NULL)
3172  {
3173  /* postfix operator */
3174  if (list_length(aexpr->name) == 1)
3175  {
3176  *nodename = strVal(linitial(aexpr->name));
3177  group = PREC_GROUP_POSTFIX_OP;
3178  }
3179  else
3180  {
3181  /* schema-qualified operator syntax */
3182  *nodename = "OPERATOR()";
3183  group = PREC_GROUP_POSTFIX_OP;
3184  }
3185  }
3186  else if (aexpr->kind == AEXPR_OP_ANY ||
3187  aexpr->kind == AEXPR_OP_ALL)
3188  {
3189  *nodename = strVal(llast(aexpr->name));
3190  group = PREC_GROUP_POSTFIX_OP;
3191  }
3192  else if (aexpr->kind == AEXPR_DISTINCT ||
3193  aexpr->kind == AEXPR_NOT_DISTINCT)
3194  {
3195  *nodename = "IS";
3196  group = PREC_GROUP_INFIX_IS;
3197  }
3198  else if (aexpr->kind == AEXPR_OF)
3199  {
3200  *nodename = "IS";
3201  group = PREC_GROUP_POSTFIX_IS;
3202  }
3203  else if (aexpr->kind == AEXPR_IN)
3204  {
3205  *nodename = "IN";
3206  if (strcmp(strVal(linitial(aexpr->name)), "=") == 0)
3207  group = PREC_GROUP_IN;
3208  else
3209  group = PREC_GROUP_NOT_IN;
3210  }
3211  else if (aexpr->kind == AEXPR_LIKE)
3212  {
3213  *nodename = "LIKE";
3214  if (strcmp(strVal(linitial(aexpr->name)), "~~") == 0)
3215  group = PREC_GROUP_LIKE;
3216  else
3217  group = PREC_GROUP_NOT_LIKE;
3218  }
3219  else if (aexpr->kind == AEXPR_ILIKE)
3220  {
3221  *nodename = "ILIKE";
3222  if (strcmp(strVal(linitial(aexpr->name)), "~~*") == 0)
3223  group = PREC_GROUP_LIKE;
3224  else
3225  group = PREC_GROUP_NOT_LIKE;
3226  }
3227  else if (aexpr->kind == AEXPR_SIMILAR)
3228  {
3229  *nodename = "SIMILAR";
3230  if (strcmp(strVal(linitial(aexpr->name)), "~") == 0)
3231  group = PREC_GROUP_LIKE;
3232  else
3233  group = PREC_GROUP_NOT_LIKE;
3234  }
3235  else if (aexpr->kind == AEXPR_BETWEEN ||
3236  aexpr->kind == AEXPR_BETWEEN_SYM)
3237  {
3238  Assert(list_length(aexpr->name) == 1);
3239  *nodename = strVal(linitial(aexpr->name));
3240  group = PREC_GROUP_BETWEEN;
3241  }
3242  else if (aexpr->kind == AEXPR_NOT_BETWEEN ||
3243  aexpr->kind == AEXPR_NOT_BETWEEN_SYM)
3244  {
3245  Assert(list_length(aexpr->name) == 1);
3246  *nodename = strVal(linitial(aexpr->name));
3247  group = PREC_GROUP_NOT_BETWEEN;
3248  }
3249  }
3250  else if (IsA(node, NullTest) ||
3251  IsA(node, BooleanTest))
3252  {
3253  *nodename = "IS";
3254  group = PREC_GROUP_POSTFIX_IS;
3255  }
3256  else if (IsA(node, XmlExpr))
3257  {
3258  XmlExpr *x = (XmlExpr *) node;
3259 
3260  if (x->op == IS_DOCUMENT)
3261  {
3262  *nodename = "IS";
3263  group = PREC_GROUP_POSTFIX_IS;
3264  }
3265  }
3266  else if (IsA(node, SubLink))
3267  {
3268  SubLink *s = (SubLink *) node;
3269 
3270  if (s->subLinkType == ANY_SUBLINK ||
3271  s->subLinkType == ALL_SUBLINK)
3272  {
3273  if (s->operName == NIL)
3274  {
3275  *nodename = "IN";
3276  group = PREC_GROUP_IN;
3277  }
3278  else
3279  {
3280  *nodename = strVal(llast(s->operName));
3281  group = PREC_GROUP_POSTFIX_OP;
3282  }
3283  }
3284  }
3285  else if (IsA(node, BoolExpr))
3286  {
3287  /*
3288  * Must dig into NOTs to see if it's IS NOT DOCUMENT or NOT IN. This
3289  * opens us to possibly misrecognizing, eg, NOT (x IS DOCUMENT) as a
3290  * problematic construct. We can tell the difference by checking
3291  * whether the parse locations of the two nodes are identical.
3292  *
3293  * Note that when we are comparing the child node to its own children,
3294  * we will not know that it was a NOT. Fortunately, that doesn't
3295  * matter for these cases.
3296  */
3297  BoolExpr *b = (BoolExpr *) node;
3298 
3299  if (b->boolop == NOT_EXPR)
3300  {
3301  Node *child = (Node *) linitial(b->args);
3302 
3303  if (IsA(child, XmlExpr))
3304  {
3305  XmlExpr *x = (XmlExpr *) child;
3306 
3307  if (x->op == IS_DOCUMENT &&
3308  x->location == b->location)
3309  {
3310  *nodename = "IS";
3311  group = PREC_GROUP_POSTFIX_IS;
3312  }
3313  }
3314  else if (IsA(child, SubLink))
3315  {
3316  SubLink *s = (SubLink *) child;
3317 
3318  if (s->subLinkType == ANY_SUBLINK && s->operName == NIL &&
3319  s->location == b->location)
3320  {
3321  *nodename = "IN";
3322  group = PREC_GROUP_NOT_IN;
3323  }
3324  }
3325  }
3326  }
3327  return group;
3328 }
#define NIL
Definition: pg_list.h:69
#define PREC_GROUP_PREFIX_OP
Definition: parse_expr.c:64
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
#define PREC_GROUP_INFIX_OP
Definition: parse_expr.c:63
A_Expr_Kind kind
Definition: parsenodes.h:276
#define PREC_GROUP_POSTFIX_IS
Definition: parse_expr.c:51
int location
Definition: primnodes.h:1158
#define PREC_GROUP_IN
Definition: parse_expr.c:58
#define llast(l)
Definition: pg_list.h:131
Definition: nodes.h:517
#define strVal(v)
Definition: value.h:54
#define PREC_GROUP_INFIX_IS
Definition: parse_expr.c:52
int location
Definition: primnodes.h:565
#define linitial(l)
Definition: pg_list.h:111
#define PREC_GROUP_BETWEEN
Definition: parse_expr.c:57
BoolExprType boolop
Definition: primnodes.h:563
#define PREC_GROUP_NOT_IN
Definition: parse_expr.c:61
Node * rexpr
Definition: parsenodes.h:279
#define PREC_GROUP_NOT_LIKE
Definition: parse_expr.c:59
Node * lexpr
Definition: parsenodes.h:278
#define PREC_GROUP_LIKE
Definition: parse_expr.c:56
XmlExprOp op
Definition: primnodes.h:1150
#define PREC_GROUP_EQUAL
Definition: parse_expr.c:54
#define Assert(condition)
Definition: c.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
List * args
Definition: primnodes.h:564
List * name
Definition: parsenodes.h:277
#define PREC_GROUP_LESS
Definition: parse_expr.c:53
#define PREC_GROUP_LESS_EQUAL
Definition: parse_expr.c:55
#define PREC_GROUP_POSTFIX_OP
Definition: parse_expr.c:62
#define PREC_GROUP_NOT_BETWEEN
Definition: parse_expr.c:60

◆ ParseExprKindName()

const char* ParseExprKindName ( ParseExprKind  exprKind)

Definition at line 3402 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_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_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_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().

3403 {
3404  switch (exprKind)
3405  {
3406  case EXPR_KIND_NONE:
3407  return "invalid expression context";
3408  case EXPR_KIND_OTHER:
3409  return "extension expression";
3410  case EXPR_KIND_JOIN_ON:
3411  return "JOIN/ON";
3412  case EXPR_KIND_JOIN_USING:
3413  return "JOIN/USING";
3415  return "sub-SELECT in FROM";
3417  return "function in FROM";
3418  case EXPR_KIND_WHERE:
3419  return "WHERE";
3420  case EXPR_KIND_POLICY:
3421  return "POLICY";
3422  case EXPR_KIND_HAVING:
3423  return "HAVING";
3424  case EXPR_KIND_FILTER:
3425  return "FILTER";
3427  return "window PARTITION BY";
3429  return "window ORDER BY";
3431  return "window RANGE";
3433  return "window ROWS";
3435  return "window GROUPS";
3437  return "SELECT";
3439  return "INSERT";
3442  return "UPDATE";
3443  case EXPR_KIND_GROUP_BY:
3444  return "GROUP BY";
3445  case EXPR_KIND_ORDER_BY:
3446  return "ORDER BY";
3447  case EXPR_KIND_DISTINCT_ON:
3448  return "DISTINCT ON";
3449  case EXPR_KIND_LIMIT:
3450  return "LIMIT";
3451  case EXPR_KIND_OFFSET:
3452  return "OFFSET";
3453  case EXPR_KIND_RETURNING:
3454  return "RETURNING";
3455  case EXPR_KIND_VALUES:
3457  return "VALUES";
3460  return "CHECK";
3463  return "DEFAULT";
3465  return "index expression";
3467  return "index predicate";
3469  return "USING";
3471  return "EXECUTE";
3473  return "WHEN";
3475  return "PARTITION BY";
3477  return "CALL";
3478 
3479  /*
3480  * There is intentionally no default: case here, so that the
3481  * compiler will warn if we add a new ParseExprKind without
3482  * extending this switch. If we do see an unrecognized value at
3483  * runtime, we'll fall through to the "unrecognized" return.
3484  */
3485  }
3486  return "unrecognized expression kind";
3487 }

◆ transformAExprBetween()

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

Definition at line 1298 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().

1299 {
1300  Node *aexpr;
1301  Node *bexpr;
1302  Node *cexpr;
1303  Node *result;
1304  Node *sub1;
1305  Node *sub2;
1306  List *args;
1307 
1308  /* Deconstruct A_Expr into three subexprs */
1309  aexpr = a->lexpr;
1310  args = castNode(List, a->rexpr);
1311  Assert(list_length(args) == 2);
1312  bexpr = (Node *) linitial(args);
1313  cexpr = (Node *) lsecond(args);
1314 
1316  {
1317  int opgroup;
1318  const char *opname;
1319 
1320  opgroup = operator_precedence_group((Node *) a, &opname);
1321  emit_precedence_warnings(pstate, opgroup, opname,
1322  aexpr, cexpr,
1323  a->location);
1324  /* We can ignore bexpr thanks to syntactic restrictions */
1325  /* Wrap subexpressions to prevent extra warnings */
1326  aexpr = (Node *) makeA_Expr(AEXPR_PAREN, NIL, aexpr, NULL, -1);
1327  bexpr = (Node *) makeA_Expr(AEXPR_PAREN, NIL, bexpr, NULL, -1);
1328  cexpr = (Node *) makeA_Expr(AEXPR_PAREN, NIL, cexpr, NULL, -1);
1329  }
1330 
1331  /*
1332  * Build the equivalent comparison expression. Make copies of
1333  * multiply-referenced subexpressions for safety. (XXX this is really
1334  * wrong since it results in multiple runtime evaluations of what may be
1335  * volatile expressions ...)
1336  *
1337  * Ideally we would not use hard-wired operators here but instead use
1338  * opclasses. However, mixed data types and other issues make this
1339  * difficult:
1340  * http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
1341  */
1342  switch (a->kind)
1343  {
1344  case AEXPR_BETWEEN:
1345  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1346  aexpr, bexpr,
1347  a->location),
1348  makeSimpleA_Expr(AEXPR_OP, "<=",
1349  copyObject(aexpr), cexpr,
1350  a->location));
1351  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1352  break;
1353  case AEXPR_NOT_BETWEEN:
1354  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1355  aexpr, bexpr,
1356  a->location),
1358  copyObject(aexpr), cexpr,
1359  a->location));
1360  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1361  break;
1362  case AEXPR_BETWEEN_SYM:
1363  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1364  aexpr, bexpr,
1365  a->location),
1366  makeSimpleA_Expr(AEXPR_OP, "<=",
1367  copyObject(aexpr), cexpr,
1368  a->location));
1369  sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1370  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1371  copyObject(aexpr), copyObject(cexpr),
1372  a->location),
1373  makeSimpleA_Expr(AEXPR_OP, "<=",
1374  copyObject(aexpr), copyObject(bexpr),
1375  a->location));
1376  sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1377  args = list_make2(sub1, sub2);
1378  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1379  break;
1380  case AEXPR_NOT_BETWEEN_SYM:
1381  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1382  aexpr, bexpr,
1383  a->location),
1385  copyObject(aexpr), cexpr,
1386  a->location));
1387  sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1388  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1389  copyObject(aexpr), copyObject(cexpr),
1390  a->location),
1392  copyObject(aexpr), copyObject(bexpr),
1393  a->location));
1394  sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1395  args = list_make2(sub1, sub2);
1396  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1397  break;
1398  default:
1399  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
1400  result = NULL; /* keep compiler quiet */
1401  break;
1402  }
1403 
1404  return transformExprRecurse(pstate, result);
1405 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
A_Expr_Kind kind
Definition: parsenodes.h:276
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
static int operator_precedence_group(Node *node, const char **nodename)
Definition: parse_expr.c:3100
Definition: nodes.h:517
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:49
#define lsecond(l)
Definition: pg_list.h:116
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:368
#define linitial(l)
Definition: pg_list.h:111
#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:31
Node * lexpr
Definition: parsenodes.h:278
bool operator_precedence_warning
Definition: parse_expr.c:44
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3343
#define Assert(condition)
Definition: c.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
#define elog
Definition: elog.h:219
#define copyObject(obj)
Definition: nodes.h:630
Definition: pg_list.h:45

◆ transformAExprDistinct()

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

Definition at line 995 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().

996 {
997  Node *lexpr = a->lexpr;
998  Node *rexpr = a->rexpr;
999  Node *result;
1000 
1003  lexpr, rexpr,
1004  a->location);
1005 
1006  /*
1007  * If either input is an undecorated NULL literal, transform to a NullTest
1008  * on the other input. That's simpler to process than a full DistinctExpr,
1009  * and it avoids needing to require that the datatype have an = operator.
1010  */
1011  if (exprIsNullConstant(rexpr))
1012  return make_nulltest_from_distinct(pstate, a, lexpr);
1013  if (exprIsNullConstant(lexpr))
1014  return make_nulltest_from_distinct(pstate, a, rexpr);
1015 
1016  lexpr = transformExprRecurse(pstate, lexpr);
1017  rexpr = transformExprRecurse(pstate, rexpr);
1018 
1019  if (lexpr && IsA(lexpr, RowExpr) &&
1020  rexpr && IsA(rexpr, RowExpr))
1021  {
1022  /* ROW() op ROW() is handled specially */
1023  result = make_row_distinct_op(pstate, a->name,
1024  (RowExpr *) lexpr,
1025  (RowExpr *) rexpr,
1026  a->location);
1027  }
1028  else
1029  {
1030  /* Ordinary scalar operator */
1031  result = (Node *) make_distinct_op(pstate,
1032  a->name,
1033  lexpr,
1034  rexpr,
1035  a->location);
1036  }
1037 
1038  /*
1039  * If it's NOT DISTINCT, we first build a DistinctExpr and then stick a
1040  * NOT on top.
1041  */
1042  if (a->kind == AEXPR_NOT_DISTINCT)
1043  result = (Node *) makeBoolExpr(NOT_EXPR,
1044  list_make1(result),
1045  a->location);
1046 
1047  return result;
1048 }
static bool exprIsNullConstant(Node *arg)
Definition: parse_expr.c:835
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
A_Expr_Kind kind
Definition: parsenodes.h:276
Definition: nodes.h:517
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:3041
#define PREC_GROUP_INFIX_IS
Definition: parse_expr.c:52
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:368
#define list_make1(x1)
Definition: pg_list.h:139
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:44
static Node * make_row_distinct_op(ParseState *pstate, List *opname, RowExpr *lrow, RowExpr *rrow, int location)
Definition: parse_expr.c:2997
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3343
static Node * make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
Definition: parse_expr.c:3074
List * name
Definition: parsenodes.h:277

◆ transformAExprIn()

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

Definition at line 1138 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().

1139 {
1140  Node *result = NULL;
1141  Node *lexpr;
1142  List *rexprs;
1143  List *rvars;
1144  List *rnonvars;
1145  bool useOr;
1146  ListCell *l;
1147 
1148  /*
1149  * If the operator is <>, combine with AND not OR.
1150  */
1151  if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1152  useOr = false;
1153  else
1154  useOr = true;
1155 
1157  emit_precedence_warnings(pstate,
1158  useOr ? PREC_GROUP_IN : PREC_GROUP_NOT_IN,
1159  "IN",
1160  a->lexpr, NULL,
1161  a->location);
1162 
1163  /*
1164  * We try to generate a ScalarArrayOpExpr from IN/NOT IN, but this is only
1165  * possible if there is a suitable array type available. If not, we fall
1166  * back to a boolean condition tree with multiple copies of the lefthand
1167  * expression. Also, any IN-list items that contain Vars are handled as
1168  * separate boolean conditions, because that gives the planner more scope
1169  * for optimization on such clauses.
1170  *
1171  * First step: transform all the inputs, and detect whether any contain
1172  * Vars.
1173  */
1174  lexpr = transformExprRecurse(pstate, a->lexpr);
1175  rexprs = rvars = rnonvars = NIL;
1176  foreach(l, (List *) a->rexpr)
1177  {
1178  Node *rexpr = transformExprRecurse(pstate, lfirst(l));
1179 
1180  rexprs = lappend(rexprs, rexpr);
1181  if (contain_vars_of_level(rexpr, 0))
1182  rvars = lappend(rvars, rexpr);
1183  else
1184  rnonvars = lappend(rnonvars, rexpr);
1185  }
1186 
1187  /*
1188  * ScalarArrayOpExpr is only going to be useful if there's more than one
1189  * non-Var righthand item.
1190  */
1191  if (list_length(rnonvars) > 1)
1192  {
1193  List *allexprs;
1194  Oid scalar_type;
1195  Oid array_type;
1196 
1197  /*
1198  * Try to select a common type for the array elements. Note that
1199  * since the LHS' type is first in the list, it will be preferred when
1200  * there is doubt (eg, when all the RHS items are unknown literals).
1201  *
1202  * Note: use list_concat here not lcons, to avoid damaging rnonvars.
1203  */
1204  allexprs = list_concat(list_make1(lexpr), rnonvars);
1205  scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1206 
1207  /*
1208  * Do we have an array type to use? Aside from the case where there
1209  * isn't one, we don't risk using ScalarArrayOpExpr when the common
1210  * type is RECORD, because the RowExpr comparison logic below can cope
1211  * with some cases of non-identical row types.
1212  */
1213  if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
1214  array_type = get_array_type(scalar_type);
1215  else
1216  array_type = InvalidOid;
1217  if (array_type != InvalidOid)
1218  {
1219  /*
1220  * OK: coerce all the right-hand non-Var inputs to the common type
1221  * and build an ArrayExpr for them.
1222  */
1223  List *aexprs;
1224  ArrayExpr *newa;
1225 
1226  aexprs = NIL;
1227  foreach(l, rnonvars)
1228  {
1229  Node *rexpr = (Node *) lfirst(l);
1230 
1231  rexpr = coerce_to_common_type(pstate, rexpr,
1232  scalar_type,
1233  "IN");
1234  aexprs = lappend(aexprs, rexpr);
1235  }
1236  newa = makeNode(ArrayExpr);
1237  newa->array_typeid = array_type;
1238  /* array_collid will be set by parse_collate.c */
1239  newa->element_typeid = scalar_type;
1240  newa->elements = aexprs;
1241  newa->multidims = false;
1242  newa->location = -1;
1243 
1244  result = (Node *) make_scalar_array_op(pstate,
1245  a->name,
1246  useOr,
1247  lexpr,
1248  (Node *) newa,
1249  a->location);
1250 
1251  /* Consider only the Vars (if any) in the loop below */
1252  rexprs = rvars;
1253  }
1254  }
1255 
1256  /*
1257  * Must do it the hard way, ie, with a boolean expression tree.
1258  */
1259  foreach(l, rexprs)
1260  {
1261  Node *rexpr = (Node *) lfirst(l);
1262  Node *cmp;
1263 
1264  if (IsA(lexpr, RowExpr) &&
1265  IsA(rexpr, RowExpr))
1266  {
1267  /* ROW() op ROW() is handled specially */
1268  cmp = make_row_comparison_op(pstate,
1269  a->name,
1270  copyObject(((RowExpr *) lexpr)->args),
1271  ((RowExpr *) rexpr)->args,
1272  a->location);
1273  }
1274  else
1275  {
1276  /* Ordinary scalar operator */
1277  cmp = (Node *) make_op(pstate,
1278  a->name,
1279  copyObject(lexpr),
1280  rexpr,
1281  pstate->p_last_srf,
1282  a->location);
1283  }
1284 
1285  cmp = coerce_to_boolean(pstate, cmp, "IN");
1286  if (result == NULL)
1287  result = cmp;
1288  else
1289  result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
1290  list_make2(result, cmp),
1291  a->location);
1292  }
1293 
1294  return result;
1295 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
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:961
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
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:2530
#define PREC_GROUP_IN
Definition: parse_expr.c:58
Definition: nodes.h:517
#define strVal(v)
Definition: value.h:54
Oid array_typeid
Definition: primnodes.h:957
List * list_concat(List *list1, List *list2)
Definition: list.c:321
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2795
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:368
#define list_make1(x1)
Definition: pg_list.h:139
#define linitial(l)
Definition: pg_list.h:111
#define PREC_GROUP_NOT_IN
Definition: parse_expr.c:61
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
List * elements
Definition: primnodes.h:960
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:128
int location
Definition: primnodes.h:962
Node * p_last_srf
Definition: parse_node.h:206
bool operator_precedence_warning
Definition: parse_expr.c:44
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3343
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:565
#define lfirst(lc)
Definition: pg_list.h:106
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
static int list_length(const List *l)
Definition: pg_list.h:89
List * name
Definition: parsenodes.h:277
Oid element_typeid
Definition: primnodes.h:959
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
#define copyObject(obj)
Definition: nodes.h:630
Definition: pg_list.h:45
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 1051 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().

1052 {
1053  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1054  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1055  OpExpr *result;
1056 
1057  result = (OpExpr *) make_op(pstate,
1058  a->name,
1059  lexpr,
1060  rexpr,
1061  pstate->p_last_srf,
1062  a->location);
1063 
1064  /*
1065  * The comparison operator itself should yield boolean ...
1066  */
1067  if (result->opresulttype != BOOLOID)
1068  ereport(ERROR,
1069  (errcode(ERRCODE_DATATYPE_MISMATCH),
1070  errmsg("NULLIF requires = operator to yield boolean"),
1071  parser_errposition(pstate, a->location)));
1072  if (result->opretset)
1073  ereport(ERROR,
1074  (errcode(ERRCODE_DATATYPE_MISMATCH),
1075  /* translator: %s is name of a SQL construct, eg NULLIF */
1076  errmsg("%s must not return a set", "NULLIF"),
1077  parser_errposition(pstate, a->location)));
1078 
1079  /*
1080  * ... but the NullIfExpr will yield the first operand's type.
1081  */
1082  result->opresulttype = exprType((Node *) linitial(result->args));
1083 
1084  /*
1085  * We rely on NullIfExpr and OpExpr being the same struct
1086  */
1087  NodeSetTag(result, T_NullIfExpr);
1088 
1089  return (Node *) result;
1090 }
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:164
#define NodeSetTag(nodeptr, t)
Definition: nodes.h:566
Definition: nodes.h:517
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid opresulttype
Definition: primnodes.h:499
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
#define ereport(elevel, rest)
Definition: elog.h:122
Node * lexpr
Definition: parsenodes.h:278
Node * p_last_srf
Definition: parse_node.h:206
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
List * name
Definition: parsenodes.h:277
int errmsg(const char *fmt,...)
Definition: elog.c:797
List * args
Definition: primnodes.h:503
bool opretset
Definition: primnodes.h:500

◆ transformAExprOf()

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

Definition at line 1097 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().

1098 {
1099  Node *lexpr = a->lexpr;
1100  Const *result;
1101  ListCell *telem;
1102  Oid ltype,
1103  rtype;
1104  bool matched = false;
1105 
1108  lexpr, NULL,
1109  a->location);
1110 
1111  lexpr = transformExprRecurse(pstate, lexpr);
1112 
1113  ltype = exprType(lexpr);
1114  foreach(telem, (List *) a->rexpr)
1115  {
1116  rtype = typenameTypeId(pstate, lfirst(telem));
1117  matched = (rtype == ltype);
1118  if (matched)
1119  break;
1120  }
1121 
1122  /*
1123  * We have two forms: equals or not equals. Flip the sense of the result
1124  * for not equals.
1125  */
1126  if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1127  matched = (!matched);
1128 
1129  result = (Const *) makeBoolConst(matched, false);
1130 
1131  /* Make the result have the original input's parse location */
1132  result->location = exprLocation((Node *) a);
1133 
1134  return (Node *) result;
1135 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
#define PREC_GROUP_POSTFIX_IS
Definition: parse_expr.c:51
Definition: nodes.h:517
#define strVal(v)
Definition: value.h:54
unsigned int Oid
Definition: postgres_ext.h:31
#define linitial(l)
Definition: pg_list.h:111
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:356
Node * rexpr
Definition: parsenodes.h:279
int location
Definition: parsenodes.h:280
Node * lexpr
Definition: parsenodes.h:278
int location
Definition: primnodes.h:204
bool operator_precedence_warning
Definition: parse_expr.c:44
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3343
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
List * name
Definition: parsenodes.h:277
Definition: pg_list.h:45
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition: parse_type.c:274

◆ transformAExprOp()

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

Definition at line 848 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().

849 {
850  Node *lexpr = a->lexpr;
851  Node *rexpr = a->rexpr;
852  Node *result;
853 
855  {
856  int opgroup;
857  const char *opname;
858 
859  opgroup = operator_precedence_group((Node *) a, &opname);
860  if (opgroup > 0)
861  emit_precedence_warnings(pstate, opgroup, opname,
862  lexpr, rexpr,
863  a->location);
864 
865  /* Look through AEXPR_PAREN nodes so they don't affect tests below */
866  while (lexpr && IsA(lexpr, A_Expr) &&
867  ((A_Expr *) lexpr)->kind == AEXPR_PAREN)
868  lexpr = ((A_Expr *) lexpr)->lexpr;
869  while (rexpr && IsA(rexpr, A_Expr) &&
870  ((A_Expr *) rexpr)->kind == AEXPR_PAREN)
871  rexpr = ((A_Expr *) rexpr)->lexpr;
872  }
873 
874  /*
875  * Special-case "foo = NULL" and "NULL = foo" for compatibility with
876  * standards-broken products (like Microsoft's). Turn these into IS NULL
877  * exprs. (If either side is a CaseTestExpr, then the expression was
878  * generated internally from a CASE-WHEN expression, and
879  * transform_null_equals does not apply.)
880  */
881  if (Transform_null_equals &&
882  list_length(a->name) == 1 &&
883  strcmp(strVal(linitial(a->name)), "=") == 0 &&
884  (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
885  (!IsA(lexpr, CaseTestExpr) &&!IsA(rexpr, CaseTestExpr)))
886  {
887  NullTest *n = makeNode(NullTest);
888 
889  n->nulltesttype = IS_NULL;
890  n->location = a->location;
891 
892  if (exprIsNullConstant(lexpr))
893  n->arg = (Expr *) rexpr;
894  else
895  n->arg = (Expr *) lexpr;
896 
897  result = transformExprRecurse(pstate, (Node *) n);
898  }
899  else if (lexpr && IsA(lexpr, RowExpr) &&
900  rexpr && IsA(rexpr, SubLink) &&
901  ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
902  {
903  /*
904  * Convert "row op subselect" into a ROWCOMPARE sublink. Formerly the
905  * grammar did this, but now that a row construct is allowed anywhere
906  * in expressions, it's easier to do it here.
907  */
908  SubLink *s = (SubLink *) rexpr;
909 
911  s->testexpr = lexpr;
912  s->operName = a->name;
913  s->location = a->location;
914  result = transformExprRecurse(pstate, (Node *) s);
915  }
916  else if (lexpr && IsA(lexpr, RowExpr) &&
917  rexpr && IsA(rexpr, RowExpr))
918  {
919  /* ROW() op ROW() is handled specially */
920  lexpr = transformExprRecurse(pstate, lexpr);
921  rexpr = transformExprRecurse(pstate, rexpr);
922 
923  result = make_row_comparison_op(pstate,
924  a->name,
925  castNode(RowExpr, lexpr)->args,
926  castNode(RowExpr, rexpr)->args,
927  a->location);
928  }
929  else
930  {
931  /* Ordinary scalar operator */
932  Node *last_srf = pstate->p_last_srf;
933 
934  lexpr = transformExprRecurse(pstate, lexpr);
935  rexpr = transformExprRecurse(pstate, rexpr);
936 
937  result = (Node *) make_op(pstate,
938  a->name,
939  lexpr,
940  rexpr,
941  last_srf,
942  a->location);
943  }
944 
945  return result;
946 }
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:835
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
static int operator_precedence_group(Node *node, const char **nodename)
Definition: parse_expr.c:3100
Definition: nodes.h:517
#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:2795
#define linitial(l)
Definition: pg_list.h:111
Expr * arg
Definition: primnodes.h:1188
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:45
Node * p_last_srf
Definition: parse_node.h:206
bool operator_precedence_warning
Definition: parse_expr.c:44
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3343
NullTestType nulltesttype
Definition: primnodes.h:1189
#define makeNode(_type_)
Definition: nodes.h:565
int location
Definition: primnodes.h:1191
static int list_length(const List *l)
Definition: pg_list.h:89
List * name
Definition: parsenodes.h:277

◆ transformAExprOpAll()

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

Definition at line 972 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().

973 {
974  Node *lexpr = a->lexpr;
975  Node *rexpr = a->rexpr;
976 
979  strVal(llast(a->name)),
980  lexpr, NULL,
981  a->location);
982 
983  lexpr = transformExprRecurse(pstate, lexpr);
984  rexpr = transformExprRecurse(pstate, rexpr);
985 
986  return (Node *) make_scalar_array_op(pstate,
987  a->name,
988  false,
989  lexpr,
990  rexpr,
991  a->location);
992 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
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:131
Definition: nodes.h:517
#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:44
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3343
List * name
Definition: parsenodes.h:277
#define PREC_GROUP_POSTFIX_OP
Definition: parse_expr.c:62

◆ transformAExprOpAny()

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

Definition at line 949 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().

950 {
951  Node *lexpr = a->lexpr;
952  Node *rexpr = a->rexpr;
953 
956  strVal(llast(a->name)),
957  lexpr, NULL,
958  a->location);
959 
960  lexpr = transformExprRecurse(pstate, lexpr);
961  rexpr = transformExprRecurse(pstate, rexpr);
962 
963  return (Node *) make_scalar_array_op(pstate,
964  a->name,
965  true,
966  lexpr,
967  rexpr,
968  a->location);
969 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
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:131
Definition: nodes.h:517
#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:44
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3343
List * name
Definition: parsenodes.h:277
#define PREC_GROUP_POSTFIX_OP
Definition: parse_expr.c:62

◆ transformArrayExpr()

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

Definition at line 2017 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().

2019 {
2020  ArrayExpr *newa = makeNode(ArrayExpr);
2021  List *newelems = NIL;
2022  List *newcoercedelems = NIL;
2023  ListCell *element;
2024  Oid coerce_type;
2025  bool coerce_hard;
2026 
2027  /*
2028  * Transform the element expressions
2029  *
2030  * Assume that the array is one-dimensional unless we find an array-type
2031  * element expression.
2032  */
2033  newa->multidims = false;
2034  foreach(element, a->elements)
2035  {
2036  Node *e = (Node *) lfirst(element);
2037  Node *newe;
2038 
2039  /* Look through AEXPR_PAREN nodes so they don't affect test below */
2040  while (e && IsA(e, A_Expr) &&
2041  ((A_Expr *) e)->kind == AEXPR_PAREN)
2042  e = ((A_Expr *) e)->lexpr;
2043 
2044  /*
2045  * If an element is itself an A_ArrayExpr, recurse directly so that we
2046  * can pass down any target type we were given.
2047  */
2048  if (IsA(e, A_ArrayExpr))
2049  {
2050  newe = transformArrayExpr(pstate,
2051  (A_ArrayExpr *) e,
2052  array_type,
2053  element_type,
2054  typmod);
2055  /* we certainly have an array here */
2056  Assert(array_type == InvalidOid || array_type == exprType(newe));
2057  newa->multidims = true;
2058  }
2059  else
2060  {
2061  newe = transformExprRecurse(pstate, e);
2062 
2063  /*
2064  * Check for sub-array expressions, if we haven't already found
2065  * one.
2066  */
2067  if (!newa->multidims && type_is_array(exprType(newe)))
2068  newa->multidims = true;
2069  }
2070 
2071  newelems = lappend(newelems, newe);
2072  }
2073 
2074  /*
2075  * Select a target type for the elements.
2076  *
2077  * If we haven't been given a target array type, we must try to deduce a
2078  * common type based on the types of the individual elements present.
2079  */
2080  if (OidIsValid(array_type))
2081  {
2082  /* Caller must ensure array_type matches element_type */
2083  Assert(OidIsValid(element_type));
2084  coerce_type = (newa->multidims ? array_type : element_type);
2085  coerce_hard = true;
2086  }
2087  else
2088  {
2089  /* Can't handle an empty array without a target type */
2090  if (newelems == NIL)
2091  ereport(ERROR,
2092  (errcode(ERRCODE_INDETERMINATE_DATATYPE),
2093  errmsg("cannot determine type of empty array"),
2094  errhint("Explicitly cast to the desired type, "
2095  "for example ARRAY[]::integer[]."),
2096  parser_errposition(pstate, a->location)));
2097 
2098  /* Select a common type for the elements */
2099  coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
2100 
2101  if (newa->multidims)
2102  {
2103  array_type = coerce_type;
2104  element_type = get_element_type(array_type);
2105  if (!OidIsValid(element_type))
2106  ereport(ERROR,
2107  (errcode(ERRCODE_UNDEFINED_OBJECT),
2108  errmsg("could not find element type for data type %s",
2109  format_type_be(array_type)),
2110  parser_errposition(pstate, a->location)));
2111  }
2112  else
2113  {
2114  element_type = coerce_type;
2115  array_type = get_array_type(element_type);
2116  if (!OidIsValid(array_type))
2117  ereport(ERROR,
2118  (errcode(ERRCODE_UNDEFINED_OBJECT),
2119  errmsg("could not find array type for data type %s",
2120  format_type_be(element_type)),
2121  parser_errposition(pstate, a->location)));
2122  }
2123  coerce_hard = false;
2124  }
2125 
2126  /*
2127  * Coerce elements to target type
2128  *
2129  * If the array has been explicitly cast, then the elements are in turn
2130  * explicitly coerced.
2131  *
2132  * If the array's type was merely derived from the common type of its
2133  * elements, then the elements are implicitly coerced to the common type.
2134  * This is consistent with other uses of select_common_type().
2135  */
2136  foreach(element, newelems)
2137  {
2138  Node *e = (Node *) lfirst(element);
2139  Node *newe;
2140 
2141  if (coerce_hard)
2142  {
2143  newe = coerce_to_target_type(pstate, e,
2144  exprType(e),
2145  coerce_type,
2146  typmod,
2149  -1);
2150  if (newe == NULL)
2151  ereport(ERROR,
2152  (errcode(ERRCODE_CANNOT_COERCE),
2153  errmsg("cannot cast type %s to %s",
2155  format_type_be(coerce_type)),
2156  parser_errposition(pstate, exprLocation(e))));
2157  }
2158  else
2159  newe = coerce_to_common_type(pstate, e,
2160  coerce_type,
2161  "ARRAY");
2162  newcoercedelems = lappend(newcoercedelems, newe);
2163  }
2164 
2165  newa->array_typeid = array_type;
2166  /* array_collid will be set by parse_collate.c */
2167  newa->element_typeid = element_type;
2168  newa->elements = newcoercedelems;
2169  newa->location = a->location;
2170 
2171  return (Node *) newa;
2172 }
bool multidims
Definition: primnodes.h:961
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2502
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2530
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:156
Definition: nodes.h:517
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid array_typeid
Definition: primnodes.h:957
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
#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:78
static Node * transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
Definition: parse_expr.c:2017
List * elements
Definition: parsenodes.h:416
List * elements
Definition: primnodes.h:960
#define ereport(elevel, rest)
Definition: elog.h:122
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:128
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:380
int location
Definition: primnodes.h:962
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
#define type_is_array(typid)
Definition: lsyscache.h:180
Oid element_typeid
Definition: primnodes.h:959
e
Definition: preproc-init.c:82
int errmsg(const char *fmt,...)
Definition: elog.c:797
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Definition: pg_list.h:45

◆ transformBooleanTest()

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

Definition at line 2525 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().

2526 {
2527  const char *clausename;
2528 
2531  (Node *) b->arg, NULL,
2532  b->location);
2533 
2534  switch (b->booltesttype)
2535  {
2536  case IS_TRUE:
2537  clausename = "IS TRUE";
2538  break;
2539  case IS_NOT_TRUE:
2540  clausename = "IS NOT TRUE";
2541  break;
2542  case IS_FALSE:
2543  clausename = "IS FALSE";
2544  break;
2545  case IS_NOT_FALSE:
2546  clausename = "IS NOT FALSE";
2547  break;
2548  case IS_UNKNOWN:
2549  clausename = "IS UNKNOWN";
2550  break;
2551  case IS_NOT_UNKNOWN:
2552  clausename = "IS NOT UNKNOWN";
2553  break;
2554  default:
2555  elog(ERROR, "unrecognized booltesttype: %d",
2556  (int) b->booltesttype);
2557  clausename = NULL; /* keep compiler quiet */
2558  }
2559 
2560  b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2561 
2562  b->arg = (Expr *) coerce_to_boolean(pstate,
2563  (Node *) b->arg,
2564  clausename);
2565 
2566  return (Node *) b;
2567 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
#define PREC_GROUP_POSTFIX_IS
Definition: parse_expr.c:51
Definition: nodes.h:517
#define ERROR
Definition: elog.h:43
Expr * arg
Definition: primnodes.h:1211
BoolTestType booltesttype
Definition: primnodes.h:1212
bool operator_precedence_warning
Definition: parse_expr.c:44
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3343
#define elog
Definition: elog.h:219
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformBoolExpr()

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

Definition at line 1408 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().

1409 {
1410  List *args = NIL;
1411  const char *opname;
1412  ListCell *lc;
1413 
1414  switch (a->boolop)
1415  {
1416  case AND_EXPR:
1417  opname = "AND";
1418  break;
1419  case OR_EXPR:
1420  opname = "OR";
1421  break;
1422  case NOT_EXPR:
1423  opname = "NOT";
1424  break;
1425  default:
1426  elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
1427  opname = NULL; /* keep compiler quiet */
1428  break;
1429  }
1430 
1431  foreach(lc, a->args)
1432  {
1433  Node *arg = (Node *) lfirst(lc);
1434 
1435  arg = transformExprRecurse(pstate, arg);
1436  arg = coerce_to_boolean(pstate, arg, opname);
1437  args = lappend(args, arg);
1438  }
1439 
1440  return (Node *) makeBoolExpr(a->boolop, args, a->location);
1441 }
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
Definition: nodes.h:517
int location
Definition: primnodes.h:565
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:368
#define ERROR
Definition: elog.h:43
BoolExprType boolop
Definition: primnodes.h:563
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
List * args
Definition: primnodes.h:564
void * arg
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformCaseExpr()

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

Definition at line 1636 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().

1637 {
1638  CaseExpr *newc = makeNode(CaseExpr);
1639  Node *last_srf = pstate->p_last_srf;
1640  Node *arg;
1641  CaseTestExpr *placeholder;
1642  List *newargs;
1643  List *resultexprs;
1644  ListCell *l;
1645  Node *defresult;
1646  Oid ptype;
1647 
1648  /* transform the test expression, if any */
1649  arg = transformExprRecurse(pstate, (Node *) c->arg);
1650 
1651  /* generate placeholder for test expression */
1652  if (arg)
1653  {
1654  /*
1655  * If test expression is an untyped literal, force it to text. We have
1656  * to do something now because we won't be able to do this coercion on
1657  * the placeholder. This is not as flexible as what was done in 7.4
1658  * and before, but it's good enough to handle the sort of silly coding
1659  * commonly seen.
1660  */
1661  if (exprType(arg) == UNKNOWNOID)
1662  arg = coerce_to_common_type(pstate, arg, TEXTOID, "CASE");
1663 
1664  /*
1665  * Run collation assignment on the test expression so that we know
1666  * what collation to mark the placeholder with. In principle we could
1667  * leave it to parse_collate.c to do that later, but propagating the
1668  * result to the CaseTestExpr would be unnecessarily complicated.
1669  */
1670  assign_expr_collations(pstate, arg);
1671 
1672  placeholder = makeNode(CaseTestExpr);
1673  placeholder->typeId = exprType(arg);
1674  placeholder->typeMod = exprTypmod(arg);
1675  placeholder->collation = exprCollation(arg);
1676  }
1677  else
1678  placeholder = NULL;
1679 
1680  newc->arg = (Expr *) arg;
1681 
1682  /* transform the list of arguments */
1683  newargs = NIL;
1684  resultexprs = NIL;
1685  foreach(l, c->args)
1686  {
1687  CaseWhen *w = lfirst_node(CaseWhen, l);
1688  CaseWhen *neww = makeNode(CaseWhen);
1689  Node *warg;
1690 
1691  warg = (Node *) w->expr;
1692  if (placeholder)
1693  {
1694  /* shorthand form was specified, so expand... */
1695  warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1696  (Node *) placeholder,
1697  warg,
1698  w->location);
1699  }
1700  neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1701 
1702  neww->expr = (Expr *) coerce_to_boolean(pstate,
1703  (Node *) neww->expr,
1704  "CASE/WHEN");
1705 
1706  warg = (Node *) w->result;
1707  neww->result = (Expr *) transformExprRecurse(pstate, warg);
1708  neww->location = w->location;
1709 
1710  newargs = lappend(newargs, neww);
1711  resultexprs = lappend(resultexprs, neww->result);
1712  }
1713 
1714  newc->args = newargs;
1715 
1716  /* transform the default clause */
1717  defresult = (Node *) c->defresult;
1718  if (defresult == NULL)
1719  {
1720  A_Const *n = makeNode(A_Const);
1721 
1722  n->val.type = T_Null;
1723  n->location = -1;
1724  defresult = (Node *) n;
1725  }
1726  newc->defresult = (Expr *) transformExprRecurse(pstate, defresult);
1727 
1728  /*
1729  * Note: default result is considered the most significant type in
1730  * determining preferred type. This is how the code worked before, but it
1731  * seems a little bogus to me --- tgl
1732  */
1733  resultexprs = lcons(newc->defresult, resultexprs);
1734 
1735  ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
1736  Assert(OidIsValid(ptype));
1737  newc->casetype = ptype;
1738  /* casecollid will be set by parse_collate.c */
1739 
1740  /* Convert default result clause, if necessary */
1741  newc->defresult = (Expr *)
1742  coerce_to_common_type(pstate,
1743  (Node *) newc->defresult,
1744  ptype,
1745  "CASE/ELSE");
1746 
1747  /* Convert when-clause results, if necessary */
1748  foreach(l, newc->args)
1749  {
1750  CaseWhen *w = (CaseWhen *) lfirst(l);
1751 
1752  w->result = (Expr *)
1753  coerce_to_common_type(pstate,
1754  (Node *) w->result,
1755  ptype,
1756  "CASE/WHEN");
1757  }
1758 
1759  /* if any subexpression contained a SRF, complain */
1760  if (pstate->p_last_srf != last_srf)
1761  ereport(ERROR,
1762  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1763  /* translator: %s is name of a SQL construct, eg GROUP BY */
1764  errmsg("set-returning functions are not allowed in %s",
1765  "CASE"),
1766  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
1767  parser_errposition(pstate,
1768  exprLocation(pstate->p_last_srf))));
1769 
1770  newc->location = c->location;
1771 
1772  return (Node *) newc;
1773 }
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:517
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid casetype
Definition: primnodes.h:911
unsigned int Oid
Definition: postgres_ext.h:31
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:49
#define OidIsValid(objectId)
Definition: c.h:605
int32 typeMod
Definition: primnodes.h:942
int location
Definition: primnodes.h:927
void assign_expr_collations(ParseState *pstate, Node *expr)
#define ERROR
Definition: elog.h:43
#define lfirst_node(type, lc)
Definition: pg_list.h:109
#define ereport(elevel, rest)
Definition: elog.h:122
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:128
List * args
Definition: primnodes.h:914
Node * p_last_srf
Definition: parse_node.h:206
List * lcons(void *datum, List *list)
Definition: list.c:259
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
int location
Definition: parsenodes.h:290
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
int location
Definition: primnodes.h:916
Definition: nodes.h:291
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Expr * arg
Definition: primnodes.h:913
Expr * result
Definition: primnodes.h:926
Expr * defresult
Definition: primnodes.h:915
Expr * expr
Definition: primnodes.h:925
Value val
Definition: parsenodes.h:289
Definition: pg_list.h:45
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 2207 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().

2208 {
2210  Node *last_srf = pstate->p_last_srf;
2211  List *newargs = NIL;
2212  List *newcoercedargs = NIL;
2213  ListCell *args;
2214 
2215  foreach(args, c->args)
2216  {
2217  Node *e = (Node *) lfirst(args);
2218  Node *newe;
2219 
2220  newe = transformExprRecurse(pstate, e);
2221  newargs = lappend(newargs, newe);
2222  }
2223 
2224  newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2225  /* coalescecollid will be set by parse_collate.c */
2226 
2227  /* Convert arguments if necessary */
2228  foreach(args, newargs)
2229  {
2230  Node *e = (Node *) lfirst(args);
2231  Node *newe;
2232 
2233  newe = coerce_to_common_type(pstate, e,
2234  newc->coalescetype,
2235  "COALESCE");
2236  newcoercedargs = lappend(newcoercedargs, newe);
2237  }
2238 
2239  /* if any subexpression contained a SRF, complain */
2240  if (pstate->p_last_srf != last_srf)
2241  ereport(ERROR,
2242  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2243  /* translator: %s is name of a SQL construct, eg GROUP BY */
2244  errmsg("set-returning functions are not allowed in %s",
2245  "COALESCE"),
2246  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2247  parser_errposition(pstate,
2248  exprLocation(pstate->p_last_srf))));
2249 
2250  newc->args = newcoercedargs;
2251  newc->location = c->location;
2252  return (Node *) newc;
2253 }
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Definition: nodes.h:517
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
List * args
Definition: primnodes.h:1055
#define ereport(elevel, rest)
Definition: elog.h:122
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * p_last_srf
Definition: parse_node.h:206
#define makeNode(_type_)
Definition: nodes.h:565
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
e
Definition: preproc-init.c:82
int errmsg(const char *fmt,...)
Definition: elog.c:797
Oid coalescetype
Definition: primnodes.h:1053
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Definition: pg_list.h:45

◆ transformCollateClause()

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

Definition at line 2755 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().

2756 {
2757  CollateExpr *newc;
2758  Oid argtype;
2759 
2760  newc = makeNode(CollateExpr);
2761  newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2762 
2763  argtype = exprType((Node *) newc->arg);
2764 
2765  /*
2766  * The unknown type is not collatable, but coerce_type() takes care of it
2767  * separately, so we'll let it go here.
2768  */
2769  if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2770  ereport(ERROR,
2771  (errcode(ERRCODE_DATATYPE_MISMATCH),
2772  errmsg("collations are not supported by type %s",
2773  format_type_be(argtype)),
2774  parser_errposition(pstate, c->location)));
2775 
2776  newc->collOid = LookupCollation(pstate, c->collname, c->location);
2777  newc->location = c->location;
2778 
2779  return (Node *) newc;
2780 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
Definition: nodes.h:517
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Oid LookupCollation(ParseState *pstate, List *collnames, int location)
Definition: parse_type.c:496
#define makeNode(_type_)
Definition: nodes.h:565
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Expr * arg
Definition: primnodes.h:881
List * collname
Definition: parsenodes.h:311
int errmsg(const char *fmt,...)
Definition: elog.c:797
int location
Definition: primnodes.h:883
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:2824

◆ transformColumnRef()

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

Definition at line 509 of file parse_expr.c.

References Assert, colNameToVar(), ereport, errcode(), errmsg(), ERROR, errorMissingColumn(), errorMissingRTE(), ColumnRef::fields, get_database_name(), IsA, lfourth, linitial, list_length(), list_make1, ColumnRef::location, lsecond, lthird, makeRangeVar(), makeString(), MyDatabaseId, NameListToString(), ParseState::p_last_srf, ParseState::p_post_columnref_hook, ParseState::p_pre_columnref_hook, ParseFuncOrColumn(), parser_errposition(), refnameRangeTblEntry(), scanRTEForColumn(), strVal, and transformWholeRowRef().

Referenced by transformExprRecurse().

510 {
511  Node *node = NULL;
512  char *nspname = NULL;
513  char *relname = NULL;
514  char *colname = NULL;
515  RangeTblEntry *rte;
516  int levels_up;
517  enum
518  {
519  CRERR_NO_COLUMN,
520  CRERR_NO_RTE,
521  CRERR_WRONG_DB,
522  CRERR_TOO_MANY
523  } crerr = CRERR_NO_COLUMN;
524 
525  /*
526  * Give the PreParseColumnRefHook, if any, first shot. If it returns
527  * non-null then that's all, folks.
528  */
529  if (pstate->p_pre_columnref_hook != NULL)
530  {
531  node = pstate->p_pre_columnref_hook(pstate, cref);
532  if (node != NULL)
533  return node;
534  }
535 
536  /*----------
537  * The allowed syntaxes are:
538  *
539  * A First try to resolve as unqualified column name;
540  * if no luck, try to resolve as unqualified table name (A.*).
541  * A.B A is an unqualified table name; B is either a
542  * column or function name (trying column name first).
543  * A.B.C schema A, table B, col or func name C.
544  * A.B.C.D catalog A, schema B, table C, col or func D.
545  * A.* A is an unqualified table name; means whole-row value.
546  * A.B.* whole-row value of table B in schema A.
547  * A.B.C.* whole-row value of table C in schema B in catalog A.
548  *
549  * We do not need to cope with bare "*"; that will only be accepted by
550  * the grammar at the top level of a SELECT list, and transformTargetList
551  * will take care of it before it ever gets here. Also, "A.*" etc will
552  * be expanded by transformTargetList if they appear at SELECT top level,
553  * so here we are only going to see them as function or operator inputs.
554  *
555  * Currently, if a catalog name is given then it must equal the current
556  * database name; we check it here and then discard it.
557  *----------
558  */
559  switch (list_length(cref->fields))
560  {
561  case 1:
562  {
563  Node *field1 = (Node *) linitial(cref->fields);
564 
565  Assert(IsA(field1, String));
566  colname = strVal(field1);
567 
568  /* Try to identify as an unqualified column */
569  node = colNameToVar(pstate, colname, false, cref->location);
570 
571  if (node == NULL)
572  {
573  /*
574  * Not known as a column of any range-table entry.
575  *
576  * Try to find the name as a relation. Note that only
577  * relations already entered into the rangetable will be
578  * recognized.
579  *
580  * This is a hack for backwards compatibility with
581  * PostQUEL-inspired syntax. The preferred form now is
582  * "rel.*".
583  */
584  rte = refnameRangeTblEntry(pstate, NULL, colname,
585  cref->location,
586  &levels_up);
587  if (rte)
588  node = transformWholeRowRef(pstate, rte,
589  cref->location);
590  }
591  break;
592  }
593  case 2:
594  {
595  Node *field1 = (Node *) linitial(cref->fields);
596  Node *field2 = (Node *) lsecond(cref->fields);
597 
598  Assert(IsA(field1, String));
599  relname = strVal(field1);
600 
601  /* Locate the referenced RTE */
602  rte = refnameRangeTblEntry(pstate, nspname, relname,
603  cref->location,
604  &levels_up);
605  if (rte == NULL)
606  {
607  crerr = CRERR_NO_RTE;
608  break;
609  }
610 
611  /* Whole-row reference? */
612  if (IsA(field2, A_Star))
613  {
614  node = transformWholeRowRef(pstate, rte, cref->location);
615  break;
616  }
617 
618  Assert(IsA(field2, String));
619  colname = strVal(field2);
620 
621  /* Try to identify as a column of the RTE */
622  node = scanRTEForColumn(pstate, rte, colname, cref->location,
623  0, NULL);
624  if (node == NULL)
625  {
626  /* Try it as a function call on the whole row */
627  node = transformWholeRowRef(pstate, rte, cref->location);
628  node = ParseFuncOrColumn(pstate,
629  list_make1(makeString(colname)),
630  list_make1(node),
631  pstate->p_last_srf,
632  NULL,
633  false,
634  cref->location);
635  }
636  break;
637  }
638  case 3:
639  {
640  Node *field1 = (Node *) linitial(cref->fields);
641  Node *field2 = (Node *) lsecond(cref->fields);
642  Node *field3 = (Node *) lthird(cref->fields);
643 
644  Assert(IsA(field1, String));
645  nspname = strVal(field1);
646  Assert(IsA(field2, String));
647  relname = strVal(field2);
648 
649  /* Locate the referenced RTE */
650  rte = refnameRangeTblEntry(pstate, nspname, relname,
651  cref->location,
652  &levels_up);
653  if (rte == NULL)
654  {
655  crerr = CRERR_NO_RTE;
656  break;
657  }
658 
659  /* Whole-row reference? */
660  if (IsA(field3, A_Star))
661  {
662  node = transformWholeRowRef(pstate, rte, cref->location);
663  break;
664  }
665 
666  Assert(IsA(field3, String));
667  colname = strVal(field3);
668 
669  /* Try to identify as a column of the RTE */
670  node = scanRTEForColumn(pstate, rte, colname, cref->location,
671  0, NULL);
672  if (node == NULL)
673  {
674  /* Try it as a function call on the whole row */
675  node = transformWholeRowRef(pstate, rte, cref->location);
676  node = ParseFuncOrColumn(pstate,
677  list_make1(makeString(colname)),
678  list_make1(node),
679  pstate->p_last_srf,
680  NULL,
681  false,
682  cref->location);
683  }
684  break;
685  }
686  case 4:
687  {
688  Node *field1 = (Node *) linitial(cref->fields);
689  Node *field2 = (Node *) lsecond(cref->fields);
690  Node *field3 = (Node *) lthird(cref->fields);
691  Node *field4 = (Node *) lfourth(cref->fields);
692  char *catname;
693 
694  Assert(IsA(field1, String));
695  catname = strVal(field1);
696  Assert(IsA(field2, String));
697  nspname = strVal(field2);
698  Assert(IsA(field3, String));
699  relname = strVal(field3);
700 
701  /*
702  * We check the catalog name and then ignore it.
703  */
704  if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
705  {
706  crerr = CRERR_WRONG_DB;
707  break;
708  }
709 
710  /* Locate the referenced RTE */
711  rte = refnameRangeTblEntry(pstate, nspname, relname,
712  cref->location,
713  &levels_up);
714  if (rte == NULL)
715  {
716  crerr = CRERR_NO_RTE;
717  break;
718  }
719 
720  /* Whole-row reference? */
721  if (IsA(field4, A_Star))
722  {
723  node = transformWholeRowRef(pstate, rte, cref->location);
724  break;
725  }
726 
727  Assert(IsA(field4, String));
728  colname = strVal(field4);
729 
730  /* Try to identify as a column of the RTE */
731  node = scanRTEForColumn(pstate, rte, colname, cref->location,
732  0, NULL);
733  if (node == NULL)
734  {
735  /* Try it as a function call on the whole row */
736  node = transformWholeRowRef(pstate, rte, cref->location);
737  node = ParseFuncOrColumn(pstate,
738  list_make1(makeString(colname)),
739  list_make1(node),
740  pstate->p_last_srf,
741  NULL,
742  false,
743  cref->location);
744  }
745  break;
746  }
747  default:
748  crerr = CRERR_TOO_MANY; /* too many dotted names */
749  break;
750  }
751 
752  /*
753  * Now give the PostParseColumnRefHook, if any, a chance. We pass the
754  * translation-so-far so that it can throw an error if it wishes in the
755  * case that it has a conflicting interpretation of the ColumnRef. (If it
756  * just translates anyway, we'll throw an error, because we can't undo
757  * whatever effects the preceding steps may have had on the pstate.) If it
758  * returns NULL, use the standard translation, or throw a suitable error
759  * if there is none.
760  */
761  if (pstate->p_post_columnref_hook != NULL)
762  {
763  Node *hookresult;
764 
765  hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
766  if (node == NULL)
767  node = hookresult;
768  else if (hookresult != NULL)
769  ereport(ERROR,
770  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
771  errmsg("column reference \"%s\" is ambiguous",
772  NameListToString(cref->fields)),
773  parser_errposition(pstate, cref->location)));
774  }
775 
776  /*
777  * Throw error if no translation found.
778  */
779  if (node == NULL)
780  {
781  switch (crerr)
782  {
783  case CRERR_NO_COLUMN:
784  errorMissingColumn(pstate, relname, colname, cref->location);
785  break;
786  case CRERR_NO_RTE:
787  errorMissingRTE(pstate, makeRangeVar(nspname, relname,
788  cref->location));
789  break;
790  case CRERR_WRONG_DB:
791  ereport(ERROR,
792  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
793  errmsg("cross-database references are not implemented: %s",
794  NameListToString(cref->fields)),
795  parser_errposition(pstate, cref->location)));
796  break;
797  case CRERR_TOO_MANY:
798  ereport(ERROR,
799  (errcode(ERRCODE_SYNTAX_ERROR),
800  errmsg("improper qualified name (too many dotted names): %s",
801  NameListToString(cref->fields)),
802  parser_errposition(pstate, cref->location)));
803  break;
804  }
805  }
806 
807  return node;
808 }
Value * makeString(char *str)
Definition: value.c:53
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Definition: nodes.h:517
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
void errorMissingColumn(ParseState *pstate, const char *relname, const char *colname, int location)
RangeTblEntry * refnameRangeTblEntry(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
#define lsecond(l)
Definition: pg_list.h:116
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:213
int location
Definition: parsenodes.h:237
#define list_make1(x1)
Definition: pg_list.h:139
#define linitial(l)
Definition: pg_list.h:111
Node * scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
#define ERROR
Definition: elog.h:43
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:73
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
#define ereport(elevel, rest)
Definition: elog.h:122
char * NameListToString(List *names)
Definition: namespace.c:3082
Node * p_last_srf
Definition: parse_node.h:206
Oid MyDatabaseId
Definition: globals.c:84
static Node * transformWholeRowRef(ParseState *pstate, RangeTblEntry *rte, int location)
Definition: parse_expr.c:2627
#define Assert(condition)
Definition: c.h:699
#define lfourth(l)
Definition: pg_list.h:126
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define lthird(l)
Definition: pg_list.h:121
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:212
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:421
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 2570 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, ParseState::p_target_rangetblentry, PARAM_EXTERN, Param::paramid, Param::paramkind, Param::paramtype, and RTERangeTablePosn().

Referenced by transformExprRecurse().

2571 {
2572  int sublevels_up;
2573 
2574  /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2575  Assert(pstate->p_target_rangetblentry != NULL);
2576  cexpr->cvarno = RTERangeTablePosn(pstate,
2577  pstate->p_target_rangetblentry,
2578  &sublevels_up);
2579  Assert(sublevels_up == 0);
2580 
2581  /*
2582  * Check to see if the cursor name matches a parameter of type REFCURSOR.
2583  * If so, replace the raw name reference with a parameter reference. (This
2584  * is a hack for the convenience of plpgsql.)
2585  */
2586  if (cexpr->cursor_name != NULL) /* in case already transformed */
2587  {
2588  ColumnRef *cref = makeNode(ColumnRef);
2589  Node *node = NULL;
2590 
2591  /* Build an unqualified ColumnRef with the given name */
2592  cref->fields = list_make1(makeString(cexpr->cursor_name));
2593  cref->location = -1;
2594 
2595  /* See if there is a translation available from a parser hook */
2596  if (pstate->p_pre_columnref_hook != NULL)
2597  node = pstate->p_pre_columnref_hook(pstate, cref);
2598  if (node == NULL && pstate->p_post_columnref_hook != NULL)
2599  node = pstate->p_post_columnref_hook(pstate, cref, NULL);
2600 
2601  /*
2602  * XXX Should we throw an error if we get a translation that isn't a
2603  * refcursor Param? For now it seems best to silently ignore false
2604  * matches.
2605  */
2606  if (node != NULL && IsA(node, Param))
2607  {
2608  Param *p = (Param *) node;
2609 
2610  if (p->paramkind == PARAM_EXTERN &&
2611  p->paramtype == REFCURSOROID)
2612  {
2613  /* Matches, so convert CURRENT OF to a param reference */
2614  cexpr->cursor_name = NULL;
2615  cexpr->cursor_param = p->paramid;
2616  }
2617  }
2618  }
2619 
2620  return (Node *) cexpr;
2621 }
Value * makeString(char *str)
Definition: value.c:53
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
ParamKind paramkind
Definition: primnodes.h:245
Definition: nodes.h:517
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:213
int location
Definition: parsenodes.h:237
#define list_make1(x1)
Definition: pg_list.h:139
char * cursor_name
Definition: primnodes.h:1286
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:185
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
int paramid
Definition: primnodes.h:246
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:212
Oid paramtype
Definition: primnodes.h:247
List * fields
Definition: parsenodes.h:236

◆ transformExpr()

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

Definition at line 146 of file parse_expr.c.

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

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

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

◆ transformExprRecurse()

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

Definition at line 164 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().

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

◆ transformFuncCall()

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

Definition at line 1444 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().

1445 {
1446  Node *last_srf = pstate->p_last_srf;
1447  List *targs;
1448  ListCell *args;
1449 
1450  /* Transform the list of arguments ... */
1451  targs = NIL;
1452  foreach(args, fn->args)
1453  {
1454  targs = lappend(targs, transformExprRecurse(pstate,
1455  (Node *) lfirst(args)));
1456  }
1457 
1458  /*
1459  * When WITHIN GROUP is used, we treat its ORDER BY expressions as
1460  * additional arguments to the function, for purposes of function lookup
1461  * and argument type coercion. So, transform each such expression and add
1462  * them to the targs list. We don't explicitly mark where each argument
1463  * came from, but ParseFuncOrColumn can tell what's what by reference to
1464  * list_length(fn->agg_order).
1465  */
1466  if (fn->agg_within_group)
1467  {
1468  Assert(fn->agg_order != NIL);
1469  foreach(args, fn->agg_order)
1470  {
1471  SortBy *arg = (SortBy *) lfirst(args);
1472 
1473  targs = lappend(targs, transformExpr(pstate, arg->node,
1475  }
1476  }
1477 
1478  /* ... and hand off to ParseFuncOrColumn */
1479  return ParseFuncOrColumn(pstate,
1480  fn->funcname,
1481  targs,
1482  last_srf,
1483  fn,
1484  false,
1485  fn->location);
1486 }
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
Definition: nodes.h:517
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:73
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * p_last_srf
Definition: parse_node.h:206
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
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:45

◆ transformIndirection()

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

Definition at line 435 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, transformArraySubscripts(), transformExprRecurse(), and unknown_attribute().

Referenced by transformExprRecurse().

436 {
437  Node *last_srf = pstate->p_last_srf;
438  Node *result = transformExprRecurse(pstate, ind->arg);
439  List *subscripts = NIL;
440  int location = exprLocation(result);
441  ListCell *i;
442 
443  /*
444  * We have to split any field-selection operations apart from
445  * subscripting. Adjacent A_Indices nodes have to be treated as a single
446  * multidimensional subscript operation.
447  */
448  foreach(i, ind->indirection)
449  {
450  Node *n = lfirst(i);
451 
452  if (IsA(n, A_Indices))
453  subscripts = lappend(subscripts, n);
454  else if (IsA(n, A_Star))
455  {
456  ereport(ERROR,
457  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
458  errmsg("row expansion via \"*\" is not supported here"),
459  parser_errposition(pstate, location)));
460  }
461  else
462  {
463  Node *newresult;
464 
465  Assert(IsA(n, String));
466 
467  /* process subscripts before this field selection */
468  if (subscripts)
469  result = (Node *) transformArraySubscripts(pstate,
470  result,
471  exprType(result),
472  InvalidOid,
473  exprTypmod(result),
474  subscripts,
475  NULL);
476  subscripts = NIL;
477 
478  newresult = ParseFuncOrColumn(pstate,
479  list_make1(n),
480  list_make1(result),
481  last_srf,
482  NULL,
483  false,
484  location);
485  if (newresult == NULL)
486  unknown_attribute(pstate, result, strVal(n), location);
487  result = newresult;
488  }
489  }
490  /* process trailing subscripts, if any */
491  if (subscripts)
492  result = (Node *) transformArraySubscripts(pstate,
493  result,
494  exprType(result),
495  InvalidOid,
496  exprTypmod(result),
497  subscripts,
498  NULL);
499 
500  return result;
501 }
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
static void unknown_attribute(ParseState *pstate, Node *relref, const char *attname, int location)
Definition: parse_expr.c:389
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:517
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define list_make1(x1)
Definition: pg_list.h:139
#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:73
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * p_last_srf
Definition: parse_node.h:206
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List * indirection
Definition: parsenodes.h:407
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: pg_list.h:45
ArrayRef * transformArraySubscripts(ParseState *pstate, Node *arrayBase, Oid arrayType, Oid elementType, int32 arrayTypMod, List *indirection, Node *assignFrom)
Definition: parse_node.c:294

◆ transformMinMaxExpr()

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

Definition at line 2256 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().

2257 {
2258  MinMaxExpr *newm = makeNode(MinMaxExpr);
2259  List *newargs = NIL;
2260  List *newcoercedargs = NIL;
2261  const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2262  ListCell *args;
2263 
2264  newm->op = m->op;
2265  foreach(args, m->args)
2266  {
2267  Node *e = (Node *) lfirst(args);
2268  Node *newe;
2269 
2270  newe = transformExprRecurse(pstate, e);
2271  newargs = lappend(newargs, newe);
2272  }
2273 
2274  newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2275  /* minmaxcollid and inputcollid will be set by parse_collate.c */
2276 
2277  /* Convert arguments if necessary */
2278  foreach(args, newargs)
2279  {
2280  Node *e = (Node *) lfirst(args);
2281  Node *newe;
2282 
2283  newe = coerce_to_common_type(pstate, e,
2284  newm->minmaxtype,
2285  funcname);
2286  newcoercedargs = lappend(newcoercedargs, newe);
2287  }
2288 
2289  newm->args = newcoercedargs;
2290  newm->location = m->location;
2291  return (Node *) newm;
2292 }
Oid minmaxtype
Definition: primnodes.h:1071
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
List * args
Definition: primnodes.h:1075
Definition: nodes.h:517
int location
Definition: primnodes.h:1076
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:128
#define makeNode(_type_)
Definition: nodes.h:565
#define lfirst(lc)
Definition: pg_list.h:106
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:1074
Definition: pg_list.h:45

◆ transformMultiAssignRef()

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

Definition at line 1489 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().

1490 {
1491  SubLink *sublink;
1492  RowExpr *rexpr;
1493  Query *qtree;
1494  TargetEntry *tle;
1495 
1496  /* We should only see this in first-stage processing of UPDATE tlists */
1498 
1499  /* We only need to transform the source if this is the first column */
1500  if (maref->colno == 1)
1501  {
1502  /*
1503  * For now, we only allow EXPR SubLinks and RowExprs as the source of
1504  * an UPDATE multiassignment. This is sufficient to cover interesting
1505  * cases; at worst, someone would have to write (SELECT * FROM expr)
1506  * to expand a composite-returning expression of another form.
1507  */
1508  if (IsA(maref->source, SubLink) &&
1509  ((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
1510  {
1511  /* Relabel it as a MULTIEXPR_SUBLINK */
1512  sublink = (SubLink *) maref->source;
1513  sublink->subLinkType = MULTIEXPR_SUBLINK;
1514  /* And transform it */
1515  sublink = (SubLink *) transformExprRecurse(pstate,
1516  (Node *) sublink);
1517 
1518  qtree = castNode(Query, sublink->subselect);
1519 
1520  /* Check subquery returns required number of columns */
1521  if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
1522  ereport(ERROR,
1523  (errcode(ERRCODE_SYNTAX_ERROR),
1524  errmsg("number of columns does not match number of values"),
1525  parser_errposition(pstate, sublink->location)));
1526 
1527  /*
1528  * Build a resjunk tlist item containing the MULTIEXPR SubLink,
1529  * and add it to pstate->p_multiassign_exprs, whence it will later
1530  * get appended to the completed targetlist. We needn't worry
1531  * about selecting a resno for it; transformUpdateStmt will do
1532  * that.
1533  */
1534  tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
1536  tle);
1537 
1538  /*
1539  * Assign a unique-within-this-targetlist ID to the MULTIEXPR
1540  * SubLink. We can just use its position in the
1541  * p_multiassign_exprs list.
1542  */
1543  sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
1544  }
1545  else if (IsA(maref->source, RowExpr))
1546  {
1547  /* Transform the RowExpr, allowing SetToDefault items */
1548  rexpr = (RowExpr *) transformRowExpr(pstate,
1549  (RowExpr *) maref->source,
1550  true);
1551 
1552  /* Check it returns required number of columns */
1553  if (list_length(rexpr->args) != maref->ncolumns)
1554  ereport(ERROR,
1555  (errcode(ERRCODE_SYNTAX_ERROR),
1556  errmsg("number of columns does not match number of values"),
1557  parser_errposition(pstate, rexpr->location)));
1558 
1559  /*
1560  * Temporarily append it to p_multiassign_exprs, so we can get it
1561  * back when we come back here for additional columns.
1562  */
1563  tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
1565  tle);
1566  }
1567  else
1568  ereport(ERROR,
1569  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1570  errmsg("source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression"),
1571  parser_errposition(pstate, exprLocation(maref->source))));
1572  }
1573  else
1574  {
1575  /*
1576  * Second or later column in a multiassignment. Re-fetch the
1577  * transformed SubLink or RowExpr, which we assume is still the last
1578  * entry in p_multiassign_exprs.
1579  */
1580  Assert(pstate->p_multiassign_exprs != NIL);
1581  tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
1582  }
1583 
1584  /*
1585  * Emit the appropriate output expression for the current column
1586  */
1587  if (IsA(tle->expr, SubLink))
1588  {
1589  Param *param;
1590 
1591  sublink = (SubLink *) tle->expr;
1592  Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
1593  qtree = castNode(Query, sublink->subselect);
1594 
1595  /* Build a Param representing the current subquery output column */
1596  tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
1597  Assert(!tle->resjunk);
1598 
1599  param = makeNode(Param);
1600  param->paramkind = PARAM_MULTIEXPR;
1601  param->paramid = (sublink->subLinkId << 16) | maref->colno;
1602  param->paramtype = exprType((Node *) tle->expr);
1603  param->paramtypmod = exprTypmod((Node *) tle->expr);
1604  param->paramcollid = exprCollation((Node *) tle->expr);
1605  param->location = exprLocation((Node *) tle->expr);
1606 
1607  return (Node *) param;
1608  }
1609 
1610  if (IsA(tle->expr, RowExpr))
1611  {
1612  Node *result;
1613 
1614  rexpr = (RowExpr *) tle->expr;
1615 
1616  /* Just extract and return the next element of the RowExpr */
1617  result = (Node *) list_nth(rexpr->args, maref->colno - 1);
1618 
1619  /*
1620  * If we're at the last column, delete the RowExpr from
1621  * p_multiassign_exprs; we don't need it anymore, and don't want it in
1622  * the finished UPDATE tlist.
1623  */
1624  if (maref->colno == maref->ncolumns)
1625  pstate->p_multiassign_exprs =
1626  list_delete_ptr(pstate->p_multiassign_exprs, tle);
1627 
1628  return result;
1629  }
1630 
1631  elog(ERROR, "unexpected expr type in multiassign list");
1632  return NULL; /* keep compiler quiet */
1633 }
#define NIL
Definition: pg_list.h:69
static Node * transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
Definition: parse_expr.c:2175
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
List * args
Definition: primnodes.h:991
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define llast(l)
Definition: pg_list.h:131
ParamKind paramkind
Definition: primnodes.h:245
Definition: nodes.h:517
int errcode(int sqlerrcode)
Definition: elog.c:575
List * p_multiassign_exprs
Definition: parse_node.h:190
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
List * targetList
Definition: parsenodes.h:140
bool resjunk
Definition: primnodes.h:1383
#define ERROR
Definition: elog.h:43
Oid paramcollid
Definition: primnodes.h:249
int location
Definition: primnodes.h:250
int location
Definition: primnodes.h:1008
void * list_nth(const List *list, int n)
Definition: list.c:410
#define ereport(elevel, rest)
Definition: elog.h:122
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:237
List * lappend(List *list, void *datum)
Definition: list.c:128
ParseExprKind p_expr_kind
Definition: parse_node.h:188
int32 paramtypmod
Definition: primnodes.h:248
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
Expr * expr
Definition: primnodes.h:1376
int paramid
Definition: primnodes.h:246
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
int errmsg(const char *fmt,...)
Definition: elog.c:797
int count_nonjunk_tlist_entries(List *tlist)
Definition: tlist.c:189
#define elog
Definition: elog.h:219
Oid paramtype
Definition: primnodes.h:247

◆ transformParamRef()

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

Definition at line 811 of file parse_expr.c.

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

Referenced by transformExprRecurse().

812 {
813  Node *result;
814 
815  /*
816  * The core parser knows nothing about Params. If a hook is supplied,
817  * call it. If not, or if the hook returns NULL, throw a generic error.
818  */
819  if (pstate->p_paramref_hook != NULL)
820  result = pstate->p_paramref_hook(pstate, pref);
821  else
822  result = NULL;
823 
824  if (result == NULL)
825  ereport(ERROR,
826  (errcode(ERRCODE_UNDEFINED_PARAMETER),
827  errmsg("there is no parameter $%d", pref->number),
828  parser_errposition(pstate, pref->location)));
829 
830  return result;
831 }
Definition: nodes.h:517
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:214
#define ereport(elevel, rest)
Definition: elog.h:122
int number
Definition: parsenodes.h:246
int location
Definition: parsenodes.h:247
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ transformRowExpr()

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

Definition at line 2175 of file parse_expr.c.

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

Referenced by transformExprRecurse(), and transformMultiAssignRef().

2176 {
2177  RowExpr *newr;
2178  char fname[16];
2179  int fnum;
2180  ListCell *lc;
2181 
2182  newr = makeNode(RowExpr);
2183 
2184  /* Transform the field expressions */
2185  newr->args = transformExpressionList(pstate, r->args,
2186  pstate->p_expr_kind, allowDefault);
2187 
2188  /* Barring later casting, we consider the type RECORD */
2189  newr->row_typeid = RECORDOID;
2191 
2192  /* ROW() has anonymous columns, so invent some field names */
2193  newr->colnames = NIL;
2194  fnum = 1;
2195  foreach(lc, newr->args)
2196  {
2197  snprintf(fname, sizeof(fname), "f%d", fnum++);
2198  newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2199  }
2200 
2201  newr->location = r->location;
2202 
2203  return (Node *) newr;
2204 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
List * args
Definition: primnodes.h:991
char * pstrdup(const char *in)
Definition: mcxt.c:1161
Definition: nodes.h:517
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
List * colnames
Definition: primnodes.h:1007
int location
Definition: primnodes.h:1008
List * lappend(List *list, void *datum)
Definition: list.c:128
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
Definition: parse_target.c:228
ParseExprKind p_expr_kind
Definition: parse_node.h:188
#define makeNode(_type_)
Definition: nodes.h:565
Oid row_typeid
Definition: primnodes.h:992
CoercionForm row_format
Definition: primnodes.h:1006

◆ transformSQLValueFunction()

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

Definition at line 2295 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().

2296 {
2297  /*
2298  * All we need to do is insert the correct result type and (where needed)
2299  * validate the typmod, so we just modify the node in-place.
2300  */
2301  switch (svf->op)
2302  {
2303  case SVFOP_CURRENT_DATE:
2304  svf->type = DATEOID;
2305  break;
2306  case SVFOP_CURRENT_TIME:
2307  svf->type = TIMETZOID;
2308  break;
2309  case SVFOP_CURRENT_TIME_N:
2310  svf->type = TIMETZOID;
2311  svf->typmod = anytime_typmod_check(true, svf->typmod);
2312  break;
2314  svf->type = TIMESTAMPTZOID;
2315  break;
2317  svf->type = TIMESTAMPTZOID;
2318  svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2319  break;
2320  case SVFOP_LOCALTIME:
2321  svf->type = TIMEOID;
2322  break;
2323  case SVFOP_LOCALTIME_N:
2324  svf->type = TIMEOID;
2325  svf->typmod = anytime_typmod_check(false, svf->typmod);
2326  break;
2327  case SVFOP_LOCALTIMESTAMP:
2328  svf->type = TIMESTAMPOID;
2329  break;
2331  svf->type = TIMESTAMPOID;
2332  svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2333  break;
2334  case SVFOP_CURRENT_ROLE:
2335  case SVFOP_CURRENT_USER:
2336  case SVFOP_USER:
2337  case SVFOP_SESSION_USER:
2338  case SVFOP_CURRENT_CATALOG:
2339  case SVFOP_CURRENT_SCHEMA:
2340  svf->type = NAMEOID;
2341  break;
2342  }
2343 
2344  return (Node *) svf;
2345 }
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:72
Definition: nodes.h:517
SQLValueFunctionOp op
Definition: primnodes.h:1112
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:102

◆ transformSubLink()

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

Definition at line 1776 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_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_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_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().

1777 {
1778  Node *result = (Node *) sublink;
1779  Query *qtree;
1780  const char *err;
1781 
1782  /*
1783  * Check to see if the sublink is in an invalid place within the query. We
1784  * allow sublinks everywhere in SELECT/INSERT/UPDATE/DELETE, but generally
1785  * not in utility statements.
1786  */
1787  err = NULL;
1788  switch (pstate->p_expr_kind)
1789  {
1790  case EXPR_KIND_NONE:
1791  Assert(false); /* can't happen */
1792  break;
1793  case EXPR_KIND_OTHER:
1794  /* Accept sublink here; caller must throw error if wanted */
1795  break;
1796  case EXPR_KIND_JOIN_ON:
1797  case EXPR_KIND_JOIN_USING:
1800  case EXPR_KIND_WHERE:
1801  case EXPR_KIND_POLICY:
1802  case EXPR_KIND_HAVING:
1803  case EXPR_KIND_FILTER:
1813  case EXPR_KIND_GROUP_BY:
1814  case EXPR_KIND_ORDER_BY:
1815  case EXPR_KIND_DISTINCT_ON:
1816  case EXPR_KIND_LIMIT:
1817  case EXPR_KIND_OFFSET:
1818  case EXPR_KIND_RETURNING:
1819  case EXPR_KIND_VALUES:
1821  /* okay */
1822  break;
1825  err = _("cannot use subquery in check constraint");
1826  break;
1829  err = _("cannot use subquery in DEFAULT expression");
1830  break;
1832  err = _("cannot use subquery in index expression");
1833  break;
1835  err = _("cannot use subquery in index predicate");
1836  break;
1838  err = _("cannot use subquery in transform expression");
1839  break;
1841  err = _("cannot use subquery in EXECUTE parameter");
1842  break;
1844  err = _("cannot use subquery in trigger WHEN condition");
1845  break;
1847  err = _("cannot use subquery in partition key expression");
1848  break;
1850  err = _("cannot use subquery in CALL argument");
1851  break;
1852 
1853  /*
1854  * There is intentionally no default: case here, so that the
1855  * compiler will warn if we add a new ParseExprKind without
1856  * extending this switch. If we do see an unrecognized value at
1857  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
1858  * which is sane anyway.
1859  */
1860  }
1861  if (err)
1862  ereport(ERROR,
1863  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1864  errmsg_internal("%s", err),
1865  parser_errposition(pstate, sublink->location)));
1866 
1867  pstate->p_hasSubLinks = true;
1868 
1869  /*
1870  * OK, let's transform the sub-SELECT.
1871  */
1872  qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
1873 
1874  /*
1875  * Check that we got a SELECT. Anything else should be impossible given
1876  * restrictions of the grammar, but check anyway.
1877  */
1878  if (!IsA(qtree, Query) ||
1879  qtree->commandType != CMD_SELECT)
1880  elog(ERROR, "unexpected non-SELECT command in SubLink");
1881 
1882  sublink->subselect = (Node *) qtree;
1883 
1884  if (sublink->subLinkType == EXISTS_SUBLINK)
1885  {
1886  /*
1887  * EXISTS needs no test expression or combining operator. These fields
1888  * should be null already, but make sure.
1889  */
1890  sublink->testexpr = NULL;
1891  sublink->operName = NIL;
1892  }
1893  else if (sublink->subLinkType == EXPR_SUBLINK ||
1894  sublink->subLinkType == ARRAY_SUBLINK)
1895  {
1896  /*
1897  * Make sure the subselect delivers a single column (ignoring resjunk
1898  * targets).
1899  */
1900  if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
1901  ereport(ERROR,
1902  (errcode(ERRCODE_SYNTAX_ERROR),
1903  errmsg("subquery must return only one column"),
1904  parser_errposition(pstate, sublink->location)));
1905 
1906  /*
1907  * EXPR and ARRAY need no test expression or combining operator. These
1908  * fields should be null already, but make sure.
1909  */
1910  sublink->testexpr = NULL;
1911  sublink->operName = NIL;
1912  }
1913  else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1914  {
1915  /* Same as EXPR case, except no restriction on number of columns */
1916  sublink->testexpr = NULL;
1917  sublink->operName = NIL;
1918  }
1919  else
1920  {
1921  /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
1922  Node *lefthand;
1923  List *left_list;
1924  List *right_list;
1925  ListCell *l;
1926 
1928  {
1929  if (sublink->operName == NIL)
1931  sublink->testexpr, NULL,
1932  sublink->location);
1933  else
1935  strVal(llast(sublink->operName)),
1936  sublink->testexpr, NULL,
1937  sublink->location);
1938  }
1939 
1940  /*
1941  * If the source was "x IN (select)", convert to "x = ANY (select)".
1942  */
1943  if (sublink->operName == NIL)
1944  sublink->operName = list_make1(makeString("="));
1945 
1946  /*
1947  * Transform lefthand expression, and convert to a list
1948  */
1949  lefthand = transformExprRecurse(pstate, sublink->testexpr);
1950  if (lefthand && IsA(lefthand, RowExpr))
1951  left_list = ((RowExpr *) lefthand)->args;
1952  else
1953  left_list = list_make1(lefthand);
1954 
1955  /*
1956  * Build a list of PARAM_SUBLINK nodes representing the output columns
1957  * of the subquery.
1958  */
1959  right_list = NIL;
1960  foreach(l, qtree->targetList)
1961  {
1962  TargetEntry *tent = (TargetEntry *) lfirst(l);
1963  Param *param;
1964 
1965  if (tent->resjunk)
1966  continue;
1967 
1968  param = makeNode(Param);
1969  param->paramkind = PARAM_SUBLINK;
1970  param->paramid = tent->resno;
1971  param->paramtype = exprType((Node *) tent->expr);
1972  param->paramtypmod = exprTypmod((Node *) tent->expr);
1973  param->paramcollid = exprCollation((Node *) tent->expr);
1974  param->location = -1;
1975 
1976  right_list = lappend(right_list, param);
1977  }
1978 
1979  /*
1980  * We could rely on make_row_comparison_op to complain if the list
1981  * lengths differ, but we prefer to generate a more specific error
1982  * message.
1983  */
1984  if (list_length(left_list) < list_length(right_list))
1985  ereport(ERROR,
1986  (errcode(ERRCODE_SYNTAX_ERROR),
1987  errmsg("subquery has too many columns"),
1988  parser_errposition(pstate, sublink->location)));
1989  if (list_length(left_list) > list_length(right_list))
1990  ereport(ERROR,
1991  (errcode(ERRCODE_SYNTAX_ERROR),
1992  errmsg("subquery has too few columns"),
1993  parser_errposition(pstate, sublink->location)));
1994 
1995  /*
1996  * Identify the combining operator(s) and generate a suitable
1997  * row-comparison expression.
1998  */
1999  sublink->testexpr = make_row_comparison_op(pstate,
2000  sublink->operName,
2001  left_list,
2002  right_list,
2003  sublink->location);
2004  }
2005 
2006  return result;
2007 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
bool p_hasSubLinks
Definition: parse_node.h:203
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define PREC_GROUP_IN
Definition: parse_expr.c:58
#define llast(l)
Definition: pg_list.h:131
ParamKind paramkind
Definition: primnodes.h:245
Definition: nodes.h:517
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2795
List * targetList
Definition: parsenodes.h:140
#define list_make1(x1)
Definition: pg_list.h:139