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 3340 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().

3344 {
3345  int cgroup;
3346  const char *copname;
3347 
3348  Assert(opgroup > 0);
3349 
3350  /*
3351  * Complain if left child, which should be same or higher precedence
3352  * according to current rules, used to be lower precedence.
3353  *
3354  * Exception to precedence rules: if left child is IN or NOT IN or a
3355  * postfix operator, the grouping is syntactically forced regardless of
3356  * precedence.
3357  */
3358  cgroup = operator_precedence_group(lchild, &copname);
3359  if (cgroup > 0)
3360  {
3361  if (oldprecedence_l[cgroup] < oldprecedence_r[opgroup] &&
3362  cgroup != PREC_GROUP_IN &&
3363  cgroup != PREC_GROUP_NOT_IN &&
3364  cgroup != PREC_GROUP_POSTFIX_OP &&
3365  cgroup != PREC_GROUP_POSTFIX_IS)
3366  ereport(WARNING,
3367  (errmsg("operator precedence change: %s is now lower precedence than %s",
3368  opname, copname),
3369  parser_errposition(pstate, location)));
3370  }
3371 
3372  /*
3373  * Complain if right child, which should be higher precedence according to
3374  * current rules, used to be same or lower precedence.
3375  *
3376  * Exception to precedence rules: if right child is a prefix operator, the
3377  * grouping is syntactically forced regardless of precedence.
3378  */
3379  cgroup = operator_precedence_group(rchild, &copname);
3380  if (cgroup > 0)
3381  {
3382  if (oldprecedence_r[cgroup] <= oldprecedence_l[opgroup] &&
3383  cgroup != PREC_GROUP_PREFIX_OP)
3384  ereport(WARNING,
3385  (errmsg("operator precedence change: %s is now lower precedence than %s",
3386  opname, copname),
3387  parser_errposition(pstate, location)));
3388  }
3389 }
#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:3097
#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:680
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:563
Definition: nodes.h:287
Value val
Definition: parsenodes.h:287
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 3038 of file parse_expr.c.

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

Referenced by make_row_distinct_op(), and transformAExprDistinct().

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

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

◆ make_row_comparison_op()

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

Definition at line 2792 of file parse_expr.c.

References AND_EXPR, OpExpr::args, bms_add_member(), bms_first_member(), bms_int_members(), BOOLOID, 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().

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

2997 {
2998  Node *result = NULL;
2999  List *largs = lrow->args;
3000  List *rargs = rrow->args;
3001  ListCell *l,
3002  *r;
3003 
3004  if (list_length(largs) != list_length(rargs))
3005  ereport(ERROR,
3006  (errcode(ERRCODE_SYNTAX_ERROR),
3007  errmsg("unequal number of entries in row expressions"),
3008  parser_errposition(pstate, location)));
3009 
3010  forboth(l, largs, r, rargs)
3011  {
3012  Node *larg = (Node *) lfirst(l);
3013  Node *rarg = (Node *) lfirst(r);
3014  Node *cmp;
3015 
3016  cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
3017  if (result == NULL)
3018  result = cmp;
3019  else
3020  result = (Node *) makeBoolExpr(OR_EXPR,
3021  list_make2(result, cmp),
3022  location);
3023  }
3024 
3025  if (result == NULL)
3026  {
3027  /* zero-length rows? Generate constant FALSE */
3028  result = makeBoolConst(false, false);
3029  }
3030 
3031  return result;
3032 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
List * args
Definition: primnodes.h:990
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Definition: nodes.h:512
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:3038
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 3097 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().

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

References EXPR_KIND_ALTER_COL_TRANSFORM, EXPR_KIND_CALL, 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_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().

3400 {
3401  switch (exprKind)
3402  {
3403  case EXPR_KIND_NONE:
3404  return "invalid expression context";
3405  case EXPR_KIND_OTHER:
3406  return "extension expression";
3407  case EXPR_KIND_JOIN_ON:
3408  return "JOIN/ON";
3409  case EXPR_KIND_JOIN_USING:
3410  return "JOIN/USING";
3412  return "sub-SELECT in FROM";
3414  return "function in FROM";
3415  case EXPR_KIND_WHERE:
3416  return "WHERE";
3417  case EXPR_KIND_POLICY:
3418  return "POLICY";
3419  case EXPR_KIND_HAVING:
3420  return "HAVING";
3421  case EXPR_KIND_FILTER:
3422  return "FILTER";
3424  return "window PARTITION BY";
3426  return "window ORDER BY";
3428  return "window RANGE";
3430  return "window ROWS";
3432  return "SELECT";
3434  return "INSERT";
3437  return "UPDATE";
3438  case EXPR_KIND_GROUP_BY:
3439  return "GROUP BY";
3440  case EXPR_KIND_ORDER_BY:
3441  return "ORDER BY";
3442  case EXPR_KIND_DISTINCT_ON:
3443  return "DISTINCT ON";
3444  case EXPR_KIND_LIMIT:
3445  return "LIMIT";
3446  case EXPR_KIND_OFFSET:
3447  return "OFFSET";
3448  case EXPR_KIND_RETURNING:
3449  return "RETURNING";
3450  case EXPR_KIND_VALUES:
3452  return "VALUES";
3455  return "CHECK";
3458  return "DEFAULT";
3460  return "index expression";
3462  return "index predicate";
3464  return "USING";
3466  return "EXECUTE";
3468  return "WHEN";
3470  return "PARTITION BY";
3471  case EXPR_KIND_CALL:
3472  return "CALL";
3473 
3474  /*
3475  * There is intentionally no default: case here, so that the
3476  * compiler will warn if we add a new ParseExprKind without
3477  * extending this switch. If we do see an unrecognized value at
3478  * runtime, we'll fall through to the "unrecognized" return.
3479  */
3480  }
3481  return "unrecognized expression kind";
3482 }

◆ 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:274
#define castNode(_type_, nodeptr)
Definition: nodes.h:581
static int operator_precedence_group(Node *node, const char **nodename)
Definition: parse_expr.c:3097
Definition: nodes.h:512
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:277
int location
Definition: parsenodes.h:278
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:276
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:3340
#define Assert(condition)
Definition: c.h:680
static int list_length(const List *l)
Definition: pg_list.h:89
#define elog
Definition: elog.h:219
#define copyObject(obj)
Definition: nodes.h:625
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:563
A_Expr_Kind kind
Definition: parsenodes.h:274
Definition: nodes.h:512
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:3038
#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:277
int location
Definition: parsenodes.h:278
Node * lexpr
Definition: parsenodes.h:276
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:2994
static void emit_precedence_warnings(ParseState *pstate, int opgroup, const char *opname, Node *lchild, Node *rchild, int location)
Definition: parse_expr.c:3340
static Node * make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
Definition: parse_expr.c:3071
List * name
Definition: parsenodes.h:275

◆ 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, RECORDOID, 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:960
#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:563
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:2545
#define PREC_GROUP_IN
Definition: parse_expr.c:58
Definition: nodes.h:512
#define strVal(v)
Definition: value.h:54
Oid array_typeid
Definition: primnodes.h:956
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:586
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2792
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:277
int location
Definition: parsenodes.h:278
#define RECORDOID
Definition: pg_type.h:680
List * elements
Definition: primnodes.h:959
Node * lexpr
Definition: parsenodes.h:276
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:961
Node * p_last_srf
Definition: parse_node.h:205
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:3340
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:560
#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:275
Oid element_typeid
Definition: primnodes.h:958
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
#define copyObject(obj)
Definition: nodes.h:625
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, BOOLOID, 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:561
Definition: nodes.h:512
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid opresulttype
Definition: primnodes.h:498
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
Node * rexpr
Definition: parsenodes.h:277
int location
Definition: parsenodes.h:278
#define ereport(elevel, rest)
Definition: elog.h:122
Node * lexpr
Definition: parsenodes.h:276
Node * p_last_srf
Definition: parse_node.h:205
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
#define BOOLOID
Definition: pg_type.h:288
List * name
Definition: parsenodes.h:275
int errmsg(const char *fmt,...)
Definition: elog.c:797
List * args
Definition: primnodes.h:502
bool opretset
Definition: primnodes.h:499

◆ 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:512
#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:277
int location
Definition: parsenodes.h:278
Node * lexpr
Definition: parsenodes.h:276
int location
Definition: primnodes.h:203
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:3340
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
List * name
Definition: parsenodes.h:275
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:563
#define castNode(_type_, nodeptr)
Definition: nodes.h:581
static int operator_precedence_group(Node *node, const char **nodename)
Definition: parse_expr.c:3097
Definition: nodes.h:512
#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:2792
#define linitial(l)
Definition: pg_list.h:111
Expr * arg
Definition: primnodes.h:1187
Node * rexpr
Definition: parsenodes.h:277
int location
Definition: parsenodes.h:278
Node * lexpr
Definition: parsenodes.h:276
bool Transform_null_equals
Definition: parse_expr.c:45
Node * p_last_srf
Definition: parse_node.h:205
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:3340
NullTestType nulltesttype
Definition: primnodes.h:1188
#define makeNode(_type_)
Definition: nodes.h:560
int location
Definition: primnodes.h:1190
static int list_length(const List *l)
Definition: pg_list.h:89
List * name
Definition: parsenodes.h:275

◆ 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:512
#define strVal(v)
Definition: value.h:54
Node * rexpr
Definition: parsenodes.h:277
int location
Definition: parsenodes.h:278
Node * lexpr
Definition: parsenodes.h:276
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:3340
List * name
Definition: parsenodes.h:275
#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:512
#define strVal(v)
Definition: value.h:54
Node * rexpr
Definition: parsenodes.h:277
int location
Definition: parsenodes.h:278
Node * lexpr
Definition: parsenodes.h:276
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:3340
List * name
Definition: parsenodes.h:275
#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 2014 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().

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

2523 {
2524  const char *clausename;
2525 
2528  (Node *) b->arg, NULL,
2529  b->location);
2530 
2531  switch (b->booltesttype)
2532  {
2533  case IS_TRUE:
2534  clausename = "IS TRUE";
2535  break;
2536  case IS_NOT_TRUE:
2537  clausename = "IS NOT TRUE";
2538  break;
2539  case IS_FALSE:
2540  clausename = "IS FALSE";
2541  break;
2542  case IS_NOT_FALSE:
2543  clausename = "IS NOT FALSE";
2544  break;
2545  case IS_UNKNOWN:
2546  clausename = "IS UNKNOWN";
2547  break;
2548  case IS_NOT_UNKNOWN:
2549  clausename = "IS NOT UNKNOWN";
2550  break;
2551  default:
2552  elog(ERROR, "unrecognized booltesttype: %d",
2553  (int) b->booltesttype);
2554  clausename = NULL; /* keep compiler quiet */
2555  }
2556 
2557  b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2558 
2559  b->arg = (Expr *) coerce_to_boolean(pstate,
2560  (Node *) b->arg,
2561  clausename);
2562 
2563  return (Node *) b;
2564 }
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:512
#define ERROR
Definition: elog.h:43
Expr * arg
Definition: primnodes.h:1210
BoolTestType booltesttype
Definition: primnodes.h:1211
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:3340
#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:512
int location
Definition: primnodes.h:564
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:368
#define ERROR
Definition: elog.h:43
BoolExprType boolop
Definition: primnodes.h:562
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
List * args
Definition: primnodes.h:563
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, TEXTOID, transformExprRecurse(), Value::type, CaseTestExpr::typeId, CaseTestExpr::typeMod, UNKNOWNOID, 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
#define TEXTOID
Definition: pg_type.h:324
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:512
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid casetype
Definition: primnodes.h:910
unsigned int Oid
Definition: postgres_ext.h:31
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:49
#define OidIsValid(objectId)
Definition: c.h:586
int32 typeMod
Definition: primnodes.h:941
int location
Definition: primnodes.h:926
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:913
Node * p_last_srf
Definition: parse_node.h:205
List * lcons(void *datum, List *list)
Definition: list.c:259
#define makeNode(_type_)
Definition: nodes.h:560
#define Assert(condition)
Definition: c.h:680
#define lfirst(lc)
Definition: pg_list.h:106
int location
Definition: parsenodes.h:288
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:915
#define UNKNOWNOID
Definition: pg_type.h:431
Definition: nodes.h:287
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:912
Expr * result
Definition: primnodes.h:925
Expr * defresult
Definition: primnodes.h:914
Expr * expr
Definition: primnodes.h:924
Value val
Definition: parsenodes.h:287
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 2204 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().

2205 {
2207  Node *last_srf = pstate->p_last_srf;
2208  List *newargs = NIL;
2209  List *newcoercedargs = NIL;
2210  ListCell *args;
2211 
2212  foreach(args, c->args)
2213  {
2214  Node *e = (Node *) lfirst(args);
2215  Node *newe;
2216 
2217  newe = transformExprRecurse(pstate, e);
2218  newargs = lappend(newargs, newe);
2219  }
2220 
2221  newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2222  /* coalescecollid will be set by parse_collate.c */
2223 
2224  /* Convert arguments if necessary */
2225  foreach(args, newargs)
2226  {
2227  Node *e = (Node *) lfirst(args);
2228  Node *newe;
2229 
2230  newe = coerce_to_common_type(pstate, e,
2231  newc->coalescetype,
2232  "COALESCE");
2233  newcoercedargs = lappend(newcoercedargs, newe);
2234  }
2235 
2236  /* if any subexpression contained a SRF, complain */
2237  if (pstate->p_last_srf != last_srf)
2238  ereport(ERROR,
2239  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2240  /* translator: %s is name of a SQL construct, eg GROUP BY */
2241  errmsg("set-returning functions are not allowed in %s",
2242  "COALESCE"),
2243  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2244  parser_errposition(pstate,
2245  exprLocation(pstate->p_last_srf))));
2246 
2247  newc->args = newcoercedargs;
2248  newc->location = c->location;
2249  return (Node *) newc;
2250 }
#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:512
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
List * args
Definition: primnodes.h:1054
#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:205
#define makeNode(_type_)
Definition: nodes.h:560
#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:1052
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 2752 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(), type_is_collatable(), and UNKNOWNOID.

Referenced by transformExprRecurse().

2753 {
2754  CollateExpr *newc;
2755  Oid argtype;
2756 
2757  newc = makeNode(CollateExpr);
2758  newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2759 
2760  argtype = exprType((Node *) newc->arg);
2761 
2762  /*
2763  * The unknown type is not collatable, but coerce_type() takes care of it
2764  * separately, so we'll let it go here.
2765  */
2766  if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2767  ereport(ERROR,
2768  (errcode(ERRCODE_DATATYPE_MISMATCH),
2769  errmsg("collations are not supported by type %s",
2770  format_type_be(argtype)),
2771  parser_errposition(pstate, c->location)));
2772 
2773  newc->collOid = LookupCollation(pstate, c->collname, c->location);
2774  newc->location = c->location;
2775 
2776  return (Node *) newc;
2777 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
Definition: nodes.h:512
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
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:560
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:880
#define UNKNOWNOID
Definition: pg_type.h:431
List * collname
Definition: parsenodes.h:309
int errmsg(const char *fmt,...)
Definition: elog.c:797
int location
Definition: primnodes.h:882
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:2839

◆ 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:563
Definition: nodes.h:512
#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:212
int location
Definition: parsenodes.h:235
#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:3063
Node * p_last_srf
Definition: parse_node.h:205
Oid MyDatabaseId
Definition: globals.c:77
static Node * transformWholeRowRef(ParseState *pstate, RangeTblEntry *rte, int location)
Definition: parse_expr.c:2624
#define Assert(condition)
Definition: c.h:680
#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:211
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:421
List * fields
Definition: parsenodes.h:234
Node * colNameToVar(ParseState *pstate, const char *colname, bool localonly, int location)

◆ transformCurrentOfExpr()

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

Definition at line 2567 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, REFCURSOROID, and RTERangeTablePosn().

Referenced by transformExprRecurse().

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

◆ 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(), ExecuteCallStmt(), ExpandIndirectionStar(), findTargetlistEntrySQL99(), interpret_function_parameter_list(), resolve_unique_index_expr(), transformAlterTableStmt(), transformArraySubscripts(), 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:512
ParseExprKind
Definition: parse_node.h:33
ParseExprKind p_expr_kind
Definition: parse_node.h:187
#define Assert(condition)
Definition: c.h:680

◆ 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:2172
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:2567
static Node * transformAExprOpAny(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:949
A_Expr_Kind kind
Definition: parsenodes.h:274
#define PREC_GROUP_POSTFIX_IS
Definition: parse_expr.c:51
static Node * transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
Definition: parse_expr.c:2253
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:512
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:2522
static Node * transformXmlExpr(ParseState *pstate, XmlExpr *x)
Definition: parse_expr.c:2345
static Node * transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
Definition: parse_expr.c:2204
#define ERROR
Definition: elog.h:43
static Node * transformBoolExpr(ParseState *pstate, BoolExpr *a)
Definition: parse_expr.c:1408
Expr * arg
Definition: primnodes.h:1187
static Node * transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
Definition: parse_expr.c:2014
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:3154
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:2424
#define ereport(elevel, rest)
Definition: elog.h:122
Node * lexpr
Definition: parsenodes.h:276
static Node * transformAExprOp(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:848
Definition: nodes.h:146
static Node * transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
Definition: parse_expr.c:1489
static Node * transformCollateClause(ParseState *pstate, CollateClause *c)
Definition: parse_expr.c:2752
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:3340
#define InvalidOid
Definition: postgres_ext.h:36
Expr * arg
Definition: primnodes.h:478
int location
Definition: parsenodes.h:288
Definition: value.h:42
int location
Definition: primnodes.h:1190
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:517
static Node * transformTypeCast(ParseState *pstate, TypeCast *tc)
Definition: parse_expr.c:2659
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:2479
static Node * transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
Definition: parse_expr.c:2292
bool argisrow
Definition: primnodes.h:1189
#define elog
Definition: elog.h:219
Value val
Definition: parsenodes.h:287
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:512
int location
Definition: parsenodes.h:358
List * agg_order
Definition: parsenodes.h:351
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:205
#define Assert(condition)
Definition: c.h:680
#define lfirst(lc)
Definition: pg_list.h:106
List * args
Definition: parsenodes.h:350
void * arg
List * funcname
Definition: parsenodes.h:349
Node * node
Definition: parsenodes.h:468
bool agg_within_group
Definition: parsenodes.h:353
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:563
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:512
#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:205
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:680
#define lfirst(lc)
Definition: pg_list.h:106
List * indirection
Definition: parsenodes.h:405
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 2253 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().

2254 {
2255  MinMaxExpr *newm = makeNode(MinMaxExpr);
2256  List *newargs = NIL;
2257  List *newcoercedargs = NIL;
2258  const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2259  ListCell *args;
2260 
2261  newm->op = m->op;
2262  foreach(args, m->args)
2263  {
2264  Node *e = (Node *) lfirst(args);
2265  Node *newe;
2266 
2267  newe = transformExprRecurse(pstate, e);
2268  newargs = lappend(newargs, newe);
2269  }
2270 
2271  newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2272  /* minmaxcollid and inputcollid will be set by parse_collate.c */
2273 
2274  /* Convert arguments if necessary */
2275  foreach(args, newargs)
2276  {
2277  Node *e = (Node *) lfirst(args);
2278  Node *newe;
2279 
2280  newe = coerce_to_common_type(pstate, e,
2281  newm->minmaxtype,
2282  funcname);
2283  newcoercedargs = lappend(newcoercedargs, newe);
2284  }
2285 
2286  newm->args = newcoercedargs;
2287  newm->location = m->location;
2288  return (Node *) newm;
2289 }
Oid minmaxtype
Definition: primnodes.h:1070
#define NIL
Definition: pg_list.h:69
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
List * args
Definition: primnodes.h:1074
Definition: nodes.h:512
int location
Definition: primnodes.h:1075
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:560
#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:1073
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:2172
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:164
List * args
Definition: primnodes.h:990
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
#define castNode(_type_, nodeptr)
Definition: nodes.h:581
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define llast(l)
Definition: pg_list.h:131
ParamKind paramkind
Definition: primnodes.h:244
Definition: nodes.h:512
int errcode(int sqlerrcode)
Definition: elog.c:575
List * p_multiassign_exprs
Definition: parse_node.h:189
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
List * targetList
Definition: parsenodes.h:138
bool resjunk
Definition: primnodes.h:1382
#define ERROR
Definition: elog.h:43
Oid paramcollid
Definition: primnodes.h:248
int location
Definition: primnodes.h:249
int location
Definition: primnodes.h:1007
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:187
int32 paramtypmod
Definition: primnodes.h:247
#define makeNode(_type_)
Definition: nodes.h:560
#define Assert(condition)
Definition: c.h:680
Expr * expr
Definition: primnodes.h:1375
int paramid
Definition: primnodes.h:245
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:246

◆ 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:512
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:213
#define ereport(elevel, rest)
Definition: elog.h:122
int number
Definition: parsenodes.h:244
int location
Definition: parsenodes.h:245
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 2172 of file parse_expr.c.

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

Referenced by transformExprRecurse(), and transformMultiAssignRef().

2173 {
2174  RowExpr *newr;
2175  char fname[16];
2176  int fnum;
2177  ListCell *lc;
2178 
2179  newr = makeNode(RowExpr);
2180 
2181  /* Transform the field expressions */
2182  newr->args = transformExpressionList(pstate, r->args,
2183  pstate->p_expr_kind, allowDefault);
2184 
2185  /* Barring later casting, we consider the type RECORD */
2186  newr->row_typeid = RECORDOID;
2188 
2189  /* ROW() has anonymous columns, so invent some field names */
2190  newr->colnames = NIL;
2191  fnum = 1;
2192  foreach(lc, newr->args)
2193  {
2194  snprintf(fname, sizeof(fname), "f%d", fnum++);
2195  newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2196  }
2197 
2198  newr->location = r->location;
2199 
2200  return (Node *) newr;
2201 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
List * args
Definition: primnodes.h:990
char * pstrdup(const char *in)
Definition: mcxt.c:1063
Definition: nodes.h:512
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
List * colnames
Definition: primnodes.h:1006
int location
Definition: primnodes.h:1007
#define RECORDOID
Definition: pg_type.h:680
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:187
#define makeNode(_type_)
Definition: nodes.h:560
Oid row_typeid
Definition: primnodes.h:991
CoercionForm row_format
Definition: primnodes.h:1005

◆ transformSQLValueFunction()

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

Definition at line 2292 of file parse_expr.c.

References anytime_typmod_check(), anytimestamp_typmod_check(), DATEOID, NAMEOID, 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, TIMEOID, TIMESTAMPOID, TIMESTAMPTZOID, TIMETZOID, SQLValueFunction::type, and SQLValueFunction::typmod.

Referenced by transformExprRecurse().

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

◆ 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, 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_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:
1812  case EXPR_KIND_GROUP_BY:
1813  case EXPR_KIND_ORDER_BY:
1814  case EXPR_KIND_DISTINCT_ON:
1815  case EXPR_KIND_LIMIT:
1816  case EXPR_KIND_OFFSET:
1817  case EXPR_KIND_RETURNING:
1818  case EXPR_KIND_VALUES:
1820  case EXPR_KIND_CALL:
1821  /* okay */
1822  break;
1825  err = _("cannot use subquery in check constraint");
1826  break;
1829  err = _("cannot use subquery in DEFAULT expression");
1830  break;
1832  err = _("cannot use subquery in index expression");
1833  break;
1835  err = _("cannot use subquery in index predicate");
1836  break;
1838  err = _("cannot use subquery in transform expression");
1839  break;
1841  err = _("cannot use subquery in EXECUTE parameter");
1842  break;
1844  err = _("cannot use subquery in trigger WHEN condition");
1845  break;
1847  err = _("cannot use subquery in partition key expression");
1848  break;
1849 
1850  /*
1851  * There is intentionally no default: case here, so that the
1852  * compiler will warn if we add a new ParseExprKind without
1853  * extending this switch. If we do see an unrecognized value at
1854  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
1855  * which is sane anyway.
1856  */
1857  }
1858  if (err)
1859  ereport(ERROR,
1860  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1861  errmsg_internal("%s", err),
1862  parser_errposition(pstate, sublink->location)));
1863 
1864  pstate->p_hasSubLinks = true;
1865 
1866  /*
1867  * OK, let's transform the sub-SELECT.
1868  */
1869  qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
1870 
1871  /*
1872  * Check that we got a SELECT. Anything else should be impossible given
1873  * restrictions of the grammar, but check anyway.
1874  */
1875  if (!IsA(qtree, Query) ||
1876  qtree->commandType != CMD_SELECT)
1877  elog(ERROR, "unexpected non-SELECT command in SubLink");
1878 
1879  sublink->subselect = (Node *) qtree;
1880 
1881  if (sublink->subLinkType == EXISTS_SUBLINK)
1882  {
1883  /*
1884  * EXISTS needs no test expression or combining operator. These fields
1885  * should be null already, but make sure.
1886  */
1887  sublink->testexpr = NULL;
1888  sublink->operName = NIL;
1889  }
1890  else if (sublink->subLinkType == EXPR_SUBLINK ||
1891  sublink->subLinkType == ARRAY_SUBLINK)
1892  {
1893  /*
1894  * Make sure the subselect delivers a single column (ignoring resjunk
1895  * targets).
1896  */
1897  if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
1898  ereport(ERROR,
1899  (errcode(ERRCODE_SYNTAX_ERROR),
1900  errmsg("subquery must return only one column"),
1901  parser_errposition(pstate, sublink->location)));
1902 
1903  /*
1904  * EXPR and ARRAY need no test expression or combining operator. These
1905  * fields should be null already, but make sure.
1906  */
1907  sublink->testexpr = NULL;
1908  sublink->operName = NIL;
1909  }
1910  else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1911  {
1912  /* Same as EXPR case, except no restriction on number of columns */
1913  sublink->testexpr = NULL;
1914  sublink->operName = NIL;
1915  }
1916  else
1917  {
1918  /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
1919  Node *lefthand;
1920  List *left_list;
1921  List *right_list;
1922  ListCell *l;
1923 
1925  {
1926  if (sublink->operName == NIL)
1928  sublink->testexpr, NULL,
1929  sublink->location);
1930  else
1932  strVal(llast(sublink->operName)),
1933  sublink->testexpr, NULL,
1934  sublink->location);
1935  }
1936 
1937  /*
1938  * If the source was "x IN (select)", convert to "x = ANY (select)".
1939  */
1940  if (sublink->operName == NIL)
1941  sublink->operName = list_make1(makeString("="));
1942 
1943  /*
1944  * Transform lefthand expression, and convert to a list
1945  */
1946  lefthand = transformExprRecurse(pstate, sublink->testexpr);
1947  if (lefthand && IsA(lefthand, RowExpr))
1948  left_list = ((RowExpr *) lefthand)->args;
1949  else
1950  left_list = list_make1(lefthand);
1951 
1952  /*
1953  * Build a list of PARAM_SUBLINK nodes representing the output columns
1954  * of the subquery.
1955  */
1956  right_list = NIL;
1957  foreach(l, qtree->targetList)
1958  {
1959  TargetEntry *tent = (TargetEntry *) lfirst(l);
1960  Param *param;
1961 
1962  if (tent->resjunk)
1963  continue;
1964 
1965  param = makeNode(Param);
1966  param->paramkind = PARAM_SUBLINK;
1967  param->paramid = tent->resno;
1968  param->paramtype = exprType((Node *) tent->expr);
1969  param->paramtypmod = exprTypmod((Node *) tent->expr);
1970  param->paramcollid = exprCollation((Node *) tent->expr);
1971  param->location = -1;
1972 
1973  right_list = lappend(right_list, param);
1974  }
1975 
1976  /*
1977  * We could rely on make_row_comparison_op to complain if the list
1978  * lengths differ, but we prefer to generate a more specific error
1979  * message.
1980  */
1981  if (list_length(left_list) < list_length(right_list))
1982  ereport(ERROR,
1983  (errcode(ERRCODE_SYNTAX_ERROR),
1984  errmsg("subquery has too many columns"),
1985  parser_errposition(pstate, sublink->location)));
1986  if (list_length(left_list) > list_length(right_list))
1987  ereport(ERROR,
1988  (errcode(ERRCODE_SYNTAX_ERROR),
1989  errmsg("subquery has too few columns"),
1990  parser_errposition(pstate, sublink->location)));
1991 
1992  /*
1993  * Identify the combining operator(s) and generate a suitable
1994  * row-comparison expression.
1995  */
1996  sublink->testexpr = make_row_comparison_op(pstate,
1997  sublink->operName,
1998  left_list,
1999  right_list,
2000  sublink->location);
2001  }
2002 
2003  return result;
2004 }
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:202
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276