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

Go to the source code of this file.

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 NodetransformAExprIn (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprBetween (ParseState *pstate, A_Expr *a)
 
static NodetransformBoolExpr (ParseState *pstate, BoolExpr *a)
 
static NodetransformFuncCall (ParseState *pstate, FuncCall *fn)
 
static NodetransformMultiAssignRef (ParseState *pstate, MultiAssignRef *maref)
 
static NodetransformCaseExpr (ParseState *pstate, CaseExpr *c)
 
static NodetransformSubLink (ParseState *pstate, SubLink *sublink)
 
static NodetransformArrayExpr (ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
 
static NodetransformRowExpr (ParseState *pstate, RowExpr *r, bool allowDefault)
 
static NodetransformCoalesceExpr (ParseState *pstate, CoalesceExpr *c)
 
static NodetransformMinMaxExpr (ParseState *pstate, MinMaxExpr *m)
 
static NodetransformSQLValueFunction (ParseState *pstate, SQLValueFunction *svf)
 
static NodetransformXmlExpr (ParseState *pstate, XmlExpr *x)
 
static NodetransformXmlSerialize (ParseState *pstate, XmlSerialize *xs)
 
static NodetransformBooleanTest (ParseState *pstate, BooleanTest *b)
 
static NodetransformCurrentOfExpr (ParseState *pstate, CurrentOfExpr *cexpr)
 
static NodetransformColumnRef (ParseState *pstate, ColumnRef *cref)
 
static NodetransformWholeRowRef (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location)
 
static NodetransformIndirection (ParseState *pstate, A_Indirection *ind)
 
static NodetransformTypeCast (ParseState *pstate, TypeCast *tc)
 
static NodetransformCollateClause (ParseState *pstate, CollateClause *c)
 
static Nodemake_row_comparison_op (ParseState *pstate, List *opname, List *largs, List *rargs, int location)
 
static Nodemake_row_distinct_op (ParseState *pstate, List *opname, RowExpr *lrow, RowExpr *rrow, int location)
 
static Exprmake_distinct_op (ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
 
static Nodemake_nulltest_from_distinct (ParseState *pstate, A_Expr *distincta, Node *arg)
 
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 Transform_null_equals = false
 

Function Documentation

◆ exprIsNullConstant()

static bool exprIsNullConstant ( Node arg)
static

Definition at line 851 of file parse_expr.c.

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

Referenced by transformAExprDistinct(), and transformAExprOp().

852 {
853  if (arg && IsA(arg, A_Const))
854  {
855  A_Const *con = (A_Const *) arg;
856 
857  if (con->val.type == T_Null)
858  return true;
859  }
860  return false;
861 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
Definition: nodes.h:296
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 2910 of file parse_expr.c.

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

Referenced by make_row_distinct_op(), and transformAExprDistinct().

2912 {
2913  Expr *result;
2914 
2915  result = make_op(pstate, opname, ltree, rtree,
2916  pstate->p_last_srf, location);
2917  if (((OpExpr *) result)->opresulttype != BOOLOID)
2918  ereport(ERROR,
2919  (errcode(ERRCODE_DATATYPE_MISMATCH),
2920  errmsg("IS DISTINCT FROM requires = operator to yield boolean"),
2921  parser_errposition(pstate, location)));
2922  if (((OpExpr *) result)->opretset)
2923  ereport(ERROR,
2924  (errcode(ERRCODE_DATATYPE_MISMATCH),
2925  /* translator: %s is name of a SQL construct, eg NULLIF */
2926  errmsg("%s must not return a set", "IS DISTINCT FROM"),
2927  parser_errposition(pstate, location)));
2928 
2929  /*
2930  * We rely on DistinctExpr and OpExpr being same struct
2931  */
2932  NodeSetTag(result, T_DistinctExpr);
2933 
2934  return result;
2935 }
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:672
#define NodeSetTag(nodeptr, t)
Definition: nodes.h:582
int errcode(int sqlerrcode)
Definition: elog.c:694
#define ERROR
Definition: elog.h:45
Node * p_last_srf
Definition: parse_node.h:213
#define ereport(elevel,...)
Definition: elog.h:155
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:905

◆ make_nulltest_from_distinct()

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

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

2944 {
2945  NullTest *nt = makeNode(NullTest);
2946 
2947  nt->arg = (Expr *) transformExprRecurse(pstate, arg);
2948  /* the argument can be any type, so don't coerce it */
2949  if (distincta->kind == AEXPR_NOT_DISTINCT)
2950  nt->nulltesttype = IS_NULL;
2951  else
2952  nt->nulltesttype = IS_NOT_NULL;
2953  /* argisrow = false is correct whether or not arg is composite */
2954  nt->argisrow = false;
2955  nt->location = distincta->location;
2956  return (Node *) nt;
2957 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
A_Expr_Kind kind
Definition: parsenodes.h:274
Definition: nodes.h:533
Expr * arg
Definition: primnodes.h:1243
int location
Definition: parsenodes.h:278
NullTestType nulltesttype
Definition: primnodes.h:1244
#define makeNode(_type_)
Definition: nodes.h:581
int location
Definition: primnodes.h:1246
bool argisrow
Definition: primnodes.h:1245

◆ make_row_comparison_op()

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

Definition at line 2664 of file parse_expr.c.

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

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

2666 {
2667  RowCompareExpr *rcexpr;
2668  RowCompareType rctype;
2669  List *opexprs;
2670  List *opnos;
2671  List *opfamilies;
2672  ListCell *l,
2673  *r;
2674  List **opinfo_lists;
2675  Bitmapset *strats;
2676  int nopers;
2677  int i;
2678 
2679  nopers = list_length(largs);
2680  if (nopers != list_length(rargs))
2681  ereport(ERROR,
2682  (errcode(ERRCODE_SYNTAX_ERROR),
2683  errmsg("unequal number of entries in row expressions"),
2684  parser_errposition(pstate, location)));
2685 
2686  /*
2687  * We can't compare zero-length rows because there is no principled basis
2688  * for figuring out what the operator is.
2689  */
2690  if (nopers == 0)
2691  ereport(ERROR,
2692  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2693  errmsg("cannot compare rows of zero length"),
2694  parser_errposition(pstate, location)));
2695 
2696  /*
2697  * Identify all the pairwise operators, using make_op so that behavior is
2698  * the same as in the simple scalar case.
2699  */
2700  opexprs = NIL;
2701  forboth(l, largs, r, rargs)
2702  {
2703  Node *larg = (Node *) lfirst(l);
2704  Node *rarg = (Node *) lfirst(r);
2705  OpExpr *cmp;
2706 
2707  cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
2708  pstate->p_last_srf, location));
2709 
2710  /*
2711  * We don't use coerce_to_boolean here because we insist on the
2712  * operator yielding boolean directly, not via coercion. If it
2713  * doesn't yield bool it won't be in any index opfamilies...
2714  */
2715  if (cmp->opresulttype != BOOLOID)
2716  ereport(ERROR,
2717  (errcode(ERRCODE_DATATYPE_MISMATCH),
2718  errmsg("row comparison operator must yield type boolean, "
2719  "not type %s",
2721  parser_errposition(pstate, location)));
2722  if (expression_returns_set((Node *) cmp))
2723  ereport(ERROR,
2724  (errcode(ERRCODE_DATATYPE_MISMATCH),
2725  errmsg("row comparison operator must not return a set"),
2726  parser_errposition(pstate, location)));
2727  opexprs = lappend(opexprs, cmp);
2728  }
2729 
2730  /*
2731  * If rows are length 1, just return the single operator. In this case we
2732  * don't insist on identifying btree semantics for the operator (but we
2733  * still require it to return boolean).
2734  */
2735  if (nopers == 1)
2736  return (Node *) linitial(opexprs);
2737 
2738  /*
2739  * Now we must determine which row comparison semantics (= <> < <= > >=)
2740  * apply to this set of operators. We look for btree opfamilies
2741  * containing the operators, and see which interpretations (strategy
2742  * numbers) exist for each operator.
2743  */
2744  opinfo_lists = (List **) palloc(nopers * sizeof(List *));
2745  strats = NULL;
2746  i = 0;
2747  foreach(l, opexprs)
2748  {
2749  Oid opno = ((OpExpr *) lfirst(l))->opno;
2750  Bitmapset *this_strats;
2751  ListCell *j;
2752 
2753  opinfo_lists[i] = get_op_btree_interpretation(opno);
2754 
2755  /*
2756  * convert strategy numbers into a Bitmapset to make the intersection
2757  * calculation easy.
2758  */
2759  this_strats = NULL;
2760  foreach(j, opinfo_lists[i])
2761  {
2762  OpBtreeInterpretation *opinfo = lfirst(j);
2763 
2764  this_strats = bms_add_member(this_strats, opinfo->strategy);
2765  }
2766  if (i == 0)
2767  strats = this_strats;
2768  else
2769  strats = bms_int_members(strats, this_strats);
2770  i++;
2771  }
2772 
2773  /*
2774  * If there are multiple common interpretations, we may use any one of
2775  * them ... this coding arbitrarily picks the lowest btree strategy
2776  * number.
2777  */
2778  i = bms_first_member(strats);
2779  if (i < 0)
2780  {
2781  /* No common interpretation, so fail */
2782  ereport(ERROR,
2783  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2784  errmsg("could not determine interpretation of row comparison operator %s",
2785  strVal(llast(opname))),
2786  errhint("Row comparison operators must be associated with btree operator families."),
2787  parser_errposition(pstate, location)));
2788  }
2789  rctype = (RowCompareType) i;
2790 
2791  /*
2792  * For = and <> cases, we just combine the pairwise operators with AND or
2793  * OR respectively.
2794  */
2795  if (rctype == ROWCOMPARE_EQ)
2796  return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
2797  if (rctype == ROWCOMPARE_NE)
2798  return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
2799 
2800  /*
2801  * Otherwise we need to choose exactly which opfamily to associate with
2802  * each operator.
2803  */
2804  opfamilies = NIL;
2805  for (i = 0; i < nopers; i++)
2806  {
2807  Oid opfamily = InvalidOid;
2808  ListCell *j;
2809 
2810  foreach(j, opinfo_lists[i])
2811  {
2812  OpBtreeInterpretation *opinfo = lfirst(j);
2813 
2814  if (opinfo->strategy == rctype)
2815  {
2816  opfamily = opinfo->opfamily_id;
2817  break;
2818  }
2819  }
2820  if (OidIsValid(opfamily))
2821  opfamilies = lappend_oid(opfamilies, opfamily);
2822  else /* should not happen */
2823  ereport(ERROR,
2824  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2825  errmsg("could not determine interpretation of row comparison operator %s",
2826  strVal(llast(opname))),
2827  errdetail("There are multiple equally-plausible candidates."),
2828  parser_errposition(pstate, location)));
2829  }
2830 
2831  /*
2832  * Now deconstruct the OpExprs and create a RowCompareExpr.
2833  *
2834  * Note: can't just reuse the passed largs/rargs lists, because of
2835  * possibility that make_op inserted coercion operations.
2836  */
2837  opnos = NIL;
2838  largs = NIL;
2839  rargs = NIL;
2840  foreach(l, opexprs)
2841  {
2842  OpExpr *cmp = (OpExpr *) lfirst(l);
2843 
2844  opnos = lappend_oid(opnos, cmp->opno);
2845  largs = lappend(largs, linitial(cmp->args));
2846  rargs = lappend(rargs, lsecond(cmp->args));
2847  }
2848 
2849  rcexpr = makeNode(RowCompareExpr);
2850  rcexpr->rctype = rctype;
2851  rcexpr->opnos = opnos;
2852  rcexpr->opfamilies = opfamilies;
2853  rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
2854  rcexpr->largs = largs;
2855  rcexpr->rargs = rargs;
2856 
2857  return (Node *) rcexpr;
2858 }
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:672
#define NIL
Definition: pg_list.h:65
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:996
int errhint(const char *fmt,...)
Definition: elog.c:1152
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
#define castNode(_type_, nodeptr)
Definition: nodes.h:602
RowCompareType rctype
Definition: primnodes.h:1094
List * opfamilies
Definition: primnodes.h:1096
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:709
#define llast(l)
Definition: pg_list.h:194
Definition: nodes.h:533
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:694
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define OidIsValid(objectId)
Definition: c.h:710
#define lsecond(l)
Definition: pg_list.h:179
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:369
Oid opresulttype
Definition: primnodes.h:539
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:45
int errdetail(const char *fmt,...)
Definition: elog.c:1038
List * lappend(List *list, void *datum)
Definition: list.c:336
List * get_op_btree_interpretation(Oid opno)
Definition: lsyscache.c:599
Node * p_last_srf
Definition: parse_node.h:213
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
RowCompareType
Definition: primnodes.h:1080
#define makeNode(_type_)
Definition: nodes.h:581
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:905
int i
Oid opno
Definition: primnodes.h:537
List * args
Definition: primnodes.h:543
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:902
List * inputcollids
Definition: primnodes.h:1097
Definition: pg_list.h:50
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

◆ make_row_distinct_op()

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

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

2869 {
2870  Node *result = NULL;
2871  List *largs = lrow->args;
2872  List *rargs = rrow->args;
2873  ListCell *l,
2874  *r;
2875 
2876  if (list_length(largs) != list_length(rargs))
2877  ereport(ERROR,
2878  (errcode(ERRCODE_SYNTAX_ERROR),
2879  errmsg("unequal number of entries in row expressions"),
2880  parser_errposition(pstate, location)));
2881 
2882  forboth(l, largs, r, rargs)
2883  {
2884  Node *larg = (Node *) lfirst(l);
2885  Node *rarg = (Node *) lfirst(r);
2886  Node *cmp;
2887 
2888  cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
2889  if (result == NULL)
2890  result = cmp;
2891  else
2892  result = (Node *) makeBoolExpr(OR_EXPR,
2893  list_make2(result, cmp),
2894  location);
2895  }
2896 
2897  if (result == NULL)
2898  {
2899  /* zero-length rows? Generate constant FALSE */
2900  result = makeBoolConst(false, false);
2901  }
2902 
2903  return result;
2904 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
List * args
Definition: primnodes.h:1046
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:2910
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:369
#define ERROR
Definition: elog.h:45
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:357
#define ereport(elevel,...)
Definition: elog.h:155
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:905
Definition: pg_list.h:50
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

◆ ParseExprKindName()

const char* ParseExprKindName ( ParseExprKind  exprKind)

Definition at line 2967 of file parse_expr.c.

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

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

2968 {
2969  switch (exprKind)
2970  {
2971  case EXPR_KIND_NONE:
2972  return "invalid expression context";
2973  case EXPR_KIND_OTHER:
2974  return "extension expression";
2975  case EXPR_KIND_JOIN_ON:
2976  return "JOIN/ON";
2977  case EXPR_KIND_JOIN_USING:
2978  return "JOIN/USING";
2980  return "sub-SELECT in FROM";
2982  return "function in FROM";
2983  case EXPR_KIND_WHERE:
2984  return "WHERE";
2985  case EXPR_KIND_POLICY:
2986  return "POLICY";
2987  case EXPR_KIND_HAVING:
2988  return "HAVING";
2989  case EXPR_KIND_FILTER:
2990  return "FILTER";
2992  return "window PARTITION BY";
2994  return "window ORDER BY";
2996  return "window RANGE";
2998  return "window ROWS";
3000  return "window GROUPS";
3002  return "SELECT";
3004  return "INSERT";
3007  return "UPDATE";
3008  case EXPR_KIND_GROUP_BY:
3009  return "GROUP BY";
3010  case EXPR_KIND_ORDER_BY:
3011  return "ORDER BY";
3012  case EXPR_KIND_DISTINCT_ON:
3013  return "DISTINCT ON";
3014  case EXPR_KIND_LIMIT:
3015  return "LIMIT";
3016  case EXPR_KIND_OFFSET:
3017  return "OFFSET";
3018  case EXPR_KIND_RETURNING:
3019  return "RETURNING";
3020  case EXPR_KIND_VALUES:
3022  return "VALUES";
3025  return "CHECK";
3028  return "DEFAULT";
3030  return "index expression";
3032  return "index predicate";
3034  return "USING";
3036  return "EXECUTE";
3038  return "WHEN";
3040  return "partition bound";
3042  return "PARTITION BY";
3044  return "CALL";
3045  case EXPR_KIND_COPY_WHERE:
3046  return "WHERE";
3048  return "GENERATED AS";
3049  case EXPR_KIND_CYCLE_MARK:
3050  return "CYCLE";
3051 
3052  /*
3053  * There is intentionally no default: case here, so that the
3054  * compiler will warn if we add a new ParseExprKind without
3055  * extending this switch. If we do see an unrecognized value at
3056  * runtime, we'll fall through to the "unrecognized" return.
3057  */
3058  }
3059  return "unrecognized expression kind";
3060 }

◆ transformAExprBetween()

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

Definition at line 1219 of file parse_expr.c.

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

Referenced by transformExprRecurse().

1220 {
1221  Node *aexpr;
1222  Node *bexpr;
1223  Node *cexpr;
1224  Node *result;
1225  Node *sub1;
1226  Node *sub2;
1227  List *args;
1228 
1229  /* Deconstruct A_Expr into three subexprs */
1230  aexpr = a->lexpr;
1231  args = castNode(List, a->rexpr);
1232  Assert(list_length(args) == 2);
1233  bexpr = (Node *) linitial(args);
1234  cexpr = (Node *) lsecond(args);
1235 
1236  /*
1237  * Build the equivalent comparison expression. Make copies of
1238  * multiply-referenced subexpressions for safety. (XXX this is really
1239  * wrong since it results in multiple runtime evaluations of what may be
1240  * volatile expressions ...)
1241  *
1242  * Ideally we would not use hard-wired operators here but instead use
1243  * opclasses. However, mixed data types and other issues make this
1244  * difficult:
1245  * http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
1246  */
1247  switch (a->kind)
1248  {
1249  case AEXPR_BETWEEN:
1250  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1251  aexpr, bexpr,
1252  a->location),
1253  makeSimpleA_Expr(AEXPR_OP, "<=",
1254  copyObject(aexpr), cexpr,
1255  a->location));
1256  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1257  break;
1258  case AEXPR_NOT_BETWEEN:
1259  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1260  aexpr, bexpr,
1261  a->location),
1263  copyObject(aexpr), cexpr,
1264  a->location));
1265  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1266  break;
1267  case AEXPR_BETWEEN_SYM:
1268  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1269  aexpr, bexpr,
1270  a->location),
1271  makeSimpleA_Expr(AEXPR_OP, "<=",
1272  copyObject(aexpr), cexpr,
1273  a->location));
1274  sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1275  args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1276  copyObject(aexpr), copyObject(cexpr),
1277  a->location),
1278  makeSimpleA_Expr(AEXPR_OP, "<=",
1279  copyObject(aexpr), copyObject(bexpr),
1280  a->location));
1281  sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1282  args = list_make2(sub1, sub2);
1283  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1284  break;
1285  case AEXPR_NOT_BETWEEN_SYM:
1286  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1287  aexpr, bexpr,
1288  a->location),
1290  copyObject(aexpr), cexpr,
1291  a->location));
1292  sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1293  args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1294  copyObject(aexpr), copyObject(cexpr),
1295  a->location),
1297  copyObject(aexpr), copyObject(bexpr),
1298  a->location));
1299  sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1300  args = list_make2(sub1, sub2);
1301  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1302  break;
1303  default:
1304  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
1305  result = NULL; /* keep compiler quiet */
1306  break;
1307  }
1308 
1309  return transformExprRecurse(pstate, result);
1310 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
A_Expr_Kind kind
Definition: parsenodes.h:274
#define castNode(_type_, nodeptr)
Definition: nodes.h:602
Definition: nodes.h:533
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
#define lsecond(l)
Definition: pg_list.h:179
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:369
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:45
Node * rexpr
Definition: parsenodes.h:277
int location
Definition: parsenodes.h:278
Node * lexpr
Definition: parsenodes.h:276
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
#define elog(elevel,...)
Definition: elog.h:227
#define copyObject(obj)
Definition: nodes.h:649
Definition: pg_list.h:50

◆ transformAExprDistinct()

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

Definition at line 973 of file parse_expr.c.

References AEXPR_NOT_DISTINCT, 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, A_Expr::rexpr, and transformExprRecurse().

Referenced by transformExprRecurse().

974 {
975  Node *lexpr = a->lexpr;
976  Node *rexpr = a->rexpr;
977  Node *result;
978 
979  /*
980  * If either input is an undecorated NULL literal, transform to a NullTest
981  * on the other input. That's simpler to process than a full DistinctExpr,
982  * and it avoids needing to require that the datatype have an = operator.
983  */
984  if (exprIsNullConstant(rexpr))
985  return make_nulltest_from_distinct(pstate, a, lexpr);
986  if (exprIsNullConstant(lexpr))
987  return make_nulltest_from_distinct(pstate, a, rexpr);
988 
989  lexpr = transformExprRecurse(pstate, lexpr);
990  rexpr = transformExprRecurse(pstate, rexpr);
991 
992  if (lexpr && IsA(lexpr, RowExpr) &&
993  rexpr && IsA(rexpr, RowExpr))
994  {
995  /* ROW() op ROW() is handled specially */
996  result = make_row_distinct_op(pstate, a->name,
997  (RowExpr *) lexpr,
998  (RowExpr *) rexpr,
999  a->location);
1000  }
1001  else
1002  {
1003  /* Ordinary scalar operator */
1004  result = (Node *) make_distinct_op(pstate,
1005  a->name,
1006  lexpr,
1007  rexpr,
1008  a->location);
1009  }
1010 
1011  /*
1012  * If it's NOT DISTINCT, we first build a DistinctExpr and then stick a
1013  * NOT on top.
1014  */
1015  if (a->kind == AEXPR_NOT_DISTINCT)
1016  result = (Node *) makeBoolExpr(NOT_EXPR,
1017  list_make1(result),
1018  a->location);
1019 
1020  return result;
1021 }
static bool exprIsNullConstant(Node *arg)
Definition: parse_expr.c:851
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
A_Expr_Kind kind
Definition: parsenodes.h:274
Definition: nodes.h:533
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:2910
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:369
#define list_make1(x1)
Definition: pg_list.h:206
Node * rexpr
Definition: parsenodes.h:277
int location
Definition: parsenodes.h:278
Node * lexpr
Definition: parsenodes.h:276
static Node * make_row_distinct_op(ParseState *pstate, List *opname, RowExpr *lrow, RowExpr *rrow, int location)
Definition: parse_expr.c:2866
static Node * make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
Definition: parse_expr.c:2943
List * name
Definition: parsenodes.h:275

◆ transformAExprIn()

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

Definition at line 1066 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, 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, OR_EXPR, ParseState::p_last_srf, A_Expr::rexpr, select_common_type(), strVal, and transformExprRecurse().

Referenced by transformExprRecurse().

1067 {
1068  Node *result = NULL;
1069  Node *lexpr;
1070  List *rexprs;
1071  List *rvars;
1072  List *rnonvars;
1073  bool useOr;
1074  ListCell *l;
1075 
1076  /*
1077  * If the operator is <>, combine with AND not OR.
1078  */
1079  if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1080  useOr = false;
1081  else
1082  useOr = true;
1083 
1084  /*
1085  * We try to generate a ScalarArrayOpExpr from IN/NOT IN, but this is only
1086  * possible if there is a suitable array type available. If not, we fall
1087  * back to a boolean condition tree with multiple copies of the lefthand
1088  * expression. Also, any IN-list items that contain Vars are handled as
1089  * separate boolean conditions, because that gives the planner more scope
1090  * for optimization on such clauses.
1091  *
1092  * First step: transform all the inputs, and detect whether any contain
1093  * Vars.
1094  */
1095  lexpr = transformExprRecurse(pstate, a->lexpr);
1096  rexprs = rvars = rnonvars = NIL;
1097  foreach(l, (List *) a->rexpr)
1098  {
1099  Node *rexpr = transformExprRecurse(pstate, lfirst(l));
1100 
1101  rexprs = lappend(rexprs, rexpr);
1102  if (contain_vars_of_level(rexpr, 0))
1103  rvars = lappend(rvars, rexpr);
1104  else
1105  rnonvars = lappend(rnonvars, rexpr);
1106  }
1107 
1108  /*
1109  * ScalarArrayOpExpr is only going to be useful if there's more than one
1110  * non-Var righthand item.
1111  */
1112  if (list_length(rnonvars) > 1)
1113  {
1114  List *allexprs;
1115  Oid scalar_type;
1116  Oid array_type;
1117 
1118  /*
1119  * Try to select a common type for the array elements. Note that
1120  * since the LHS' type is first in the list, it will be preferred when
1121  * there is doubt (eg, when all the RHS items are unknown literals).
1122  *
1123  * Note: use list_concat here not lcons, to avoid damaging rnonvars.
1124  */
1125  allexprs = list_concat(list_make1(lexpr), rnonvars);
1126  scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1127 
1128  /*
1129  * Do we have an array type to use? Aside from the case where there
1130  * isn't one, we don't risk using ScalarArrayOpExpr when the common
1131  * type is RECORD, because the RowExpr comparison logic below can cope
1132  * with some cases of non-identical row types.
1133  */
1134  if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
1135  array_type = get_array_type(scalar_type);
1136  else
1137  array_type = InvalidOid;
1138  if (array_type != InvalidOid)
1139  {
1140  /*
1141  * OK: coerce all the right-hand non-Var inputs to the common type
1142  * and build an ArrayExpr for them.
1143  */
1144  List *aexprs;
1145  ArrayExpr *newa;
1146 
1147  aexprs = NIL;
1148  foreach(l, rnonvars)
1149  {
1150  Node *rexpr = (Node *) lfirst(l);
1151 
1152  rexpr = coerce_to_common_type(pstate, rexpr,
1153  scalar_type,
1154  "IN");
1155  aexprs = lappend(aexprs, rexpr);
1156  }
1157  newa = makeNode(ArrayExpr);
1158  newa->array_typeid = array_type;
1159  /* array_collid will be set by parse_collate.c */
1160  newa->element_typeid = scalar_type;
1161  newa->elements = aexprs;
1162  newa->multidims = false;
1163  newa->location = -1;
1164 
1165  result = (Node *) make_scalar_array_op(pstate,
1166  a->name,
1167  useOr,
1168  lexpr,
1169  (Node *) newa,
1170  a->location);
1171 
1172  /* Consider only the Vars (if any) in the loop below */
1173  rexprs = rvars;
1174  }
1175  }
1176 
1177  /*
1178  * Must do it the hard way, ie, with a boolean expression tree.
1179  */
1180  foreach(l, rexprs)
1181  {
1182  Node *rexpr = (Node *) lfirst(l);
1183  Node *cmp;
1184 
1185  if (IsA(lexpr, RowExpr) &&
1186  IsA(rexpr, RowExpr))
1187  {
1188  /* ROW() op ROW() is handled specially */
1189  cmp = make_row_comparison_op(pstate,
1190  a->name,
1191  copyObject(((RowExpr *) lexpr)->args),
1192  ((RowExpr *) rexpr)->args,
1193  a->location);
1194  }
1195  else
1196  {
1197  /* Ordinary scalar operator */
1198  cmp = (Node *) make_op(pstate,
1199  a->name,
1200  copyObject(lexpr),
1201  rexpr,
1202  pstate->p_last_srf,
1203  a->location);
1204  }
1205 
1206  cmp = coerce_to_boolean(pstate, cmp, "IN");
1207  if (result == NULL)
1208  result = cmp;
1209  else
1210  result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
1211  list_make2(result, cmp),
1212  a->location);
1213  }
1214 
1215  return result;
1216 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:672
bool multidims
Definition: primnodes.h:1016
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:783
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2734
Definition: nodes.h:533
#define strVal(v)
Definition: value.h:54
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
Oid array_typeid
Definition: primnodes.h:1012
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2664
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:369
#define list_make1(x1)
Definition: pg_list.h:206
#define linitial(l)
Definition: pg_list.h:174
Node * rexpr
Definition: parsenodes.h:277
int location
Definition: parsenodes.h:278
List * elements
Definition: primnodes.h:1015
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:336
int location
Definition: primnodes.h:1017
Node * p_last_srf
Definition: parse_node.h:213
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:581
#define lfirst(lc)
Definition: pg_list.h:169
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:396
static int list_length(const List *l)
Definition: pg_list.h:149
List * name
Definition: parsenodes.h:275
Oid element_typeid
Definition: primnodes.h:1014
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
#define copyObject(obj)
Definition: nodes.h:649
Definition: pg_list.h:50
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformAExprNullIf()

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

Definition at line 1024 of file parse_expr.c.

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

Referenced by transformExprRecurse().

1025 {
1026  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1027  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1028  OpExpr *result;
1029 
1030  result = (OpExpr *) make_op(pstate,
1031  a->name,
1032  lexpr,
1033  rexpr,
1034  pstate->p_last_srf,
1035  a->location);
1036 
1037  /*
1038  * The comparison operator itself should yield boolean ...
1039  */
1040  if (result->opresulttype != BOOLOID)
1041  ereport(ERROR,
1042  (errcode(ERRCODE_DATATYPE_MISMATCH),
1043  errmsg("NULLIF requires = operator to yield boolean"),
1044  parser_errposition(pstate, a->location)));
1045  if (result->opretset)
1046  ereport(ERROR,
1047  (errcode(ERRCODE_DATATYPE_MISMATCH),
1048  /* translator: %s is name of a SQL construct, eg NULLIF */
1049  errmsg("%s must not return a set", "NULLIF"),
1050  parser_errposition(pstate, a->location)));
1051 
1052  /*
1053  * ... but the NullIfExpr will yield the first operand's type.
1054  */
1055  result->opresulttype = exprType((Node *) linitial(result->args));
1056 
1057  /*
1058  * We rely on NullIfExpr and OpExpr being the same struct
1059  */
1060  NodeSetTag(result, T_NullIfExpr);
1061 
1062  return (Node *) result;
1063 }
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:672
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
#define NodeSetTag(nodeptr, t)
Definition: nodes.h:582
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
Oid opresulttype
Definition: primnodes.h:539
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:45
Node * rexpr
Definition: parsenodes.h:277
int location
Definition: parsenodes.h:278
Node * lexpr
Definition: parsenodes.h:276
Node * p_last_srf
Definition: parse_node.h:213
#define ereport(elevel,...)
Definition: elog.h:155
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
List * name
Definition: parsenodes.h:275
int errmsg(const char *fmt,...)
Definition: elog.c:905
List * args
Definition: primnodes.h:543
bool opretset
Definition: primnodes.h:540

◆ transformAExprOp()

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

Definition at line 864 of file parse_expr.c.

References NullTest::arg, generate_unaccent_rules::args, castNode, 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, SubLink::operName, ParseState::p_last_srf, A_Expr::rexpr, ROWCOMPARE_SUBLINK, strVal, SubLink::subLinkType, SubLink::testexpr, Transform_null_equals, and transformExprRecurse().

Referenced by transformExprRecurse().

865 {
866  Node *lexpr = a->lexpr;
867  Node *rexpr = a->rexpr;
868  Node *result;
869 
870  /*
871  * Special-case "foo = NULL" and "NULL = foo" for compatibility with
872  * standards-broken products (like Microsoft's). Turn these into IS NULL
873  * exprs. (If either side is a CaseTestExpr, then the expression was
874  * generated internally from a CASE-WHEN expression, and
875  * transform_null_equals does not apply.)
876  */
877  if (Transform_null_equals &&
878  list_length(a->name) == 1 &&
879  strcmp(strVal(linitial(a->name)), "=") == 0 &&
880  (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
881  (!IsA(lexpr, CaseTestExpr) && !IsA(rexpr, CaseTestExpr)))
882  {
883  NullTest *n = makeNode(NullTest);
884 
885  n->nulltesttype = IS_NULL;
886  n->location = a->location;
887 
888  if (exprIsNullConstant(lexpr))
889  n->arg = (Expr *) rexpr;
890  else
891  n->arg = (Expr *) lexpr;
892 
893  result = transformExprRecurse(pstate, (Node *) n);
894  }
895  else if (lexpr && IsA(lexpr, RowExpr) &&
896  rexpr && IsA(rexpr, SubLink) &&
897  ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
898  {
899  /*
900  * Convert "row op subselect" into a ROWCOMPARE sublink. Formerly the
901  * grammar did this, but now that a row construct is allowed anywhere
902  * in expressions, it's easier to do it here.
903  */
904  SubLink *s = (SubLink *) rexpr;
905 
907  s->testexpr = lexpr;
908  s->operName = a->name;
909  s->location = a->location;
910  result = transformExprRecurse(pstate, (Node *) s);
911  }
912  else if (lexpr && IsA(lexpr, RowExpr) &&
913  rexpr && IsA(rexpr, RowExpr))
914  {
915  /* ROW() op ROW() is handled specially */
916  lexpr = transformExprRecurse(pstate, lexpr);
917  rexpr = transformExprRecurse(pstate, rexpr);
918 
919  result = make_row_comparison_op(pstate,
920  a->name,
921  castNode(RowExpr, lexpr)->args,
922  castNode(RowExpr, rexpr)->args,
923  a->location);
924  }
925  else
926  {
927  /* Ordinary scalar operator */
928  Node *last_srf = pstate->p_last_srf;
929 
930  lexpr = transformExprRecurse(pstate, lexpr);
931  rexpr = transformExprRecurse(pstate, rexpr);
932 
933  result = (Node *) make_op(pstate,
934  a->name,
935  lexpr,
936  rexpr,
937  last_srf,
938  a->location);
939  }
940 
941  return result;
942 }
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:672
static bool exprIsNullConstant(Node *arg)
Definition: parse_expr.c:851
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
#define castNode(_type_, nodeptr)
Definition: nodes.h:602
Definition: nodes.h:533
#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:2664
#define linitial(l)
Definition: pg_list.h:174
Expr * arg
Definition: primnodes.h:1243
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:42
Node * p_last_srf
Definition: parse_node.h:213
NullTestType nulltesttype
Definition: primnodes.h:1244
#define makeNode(_type_)
Definition: nodes.h:581
int location
Definition: primnodes.h:1246
static int list_length(const List *l)
Definition: pg_list.h:149
List * name
Definition: parsenodes.h:275

◆ transformAExprOpAll()

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

Definition at line 959 of file parse_expr.c.

References A_Expr::lexpr, A_Expr::location, make_scalar_array_op(), A_Expr::name, A_Expr::rexpr, and transformExprRecurse().

Referenced by transformExprRecurse().

960 {
961  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
962  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
963 
964  return (Node *) make_scalar_array_op(pstate,
965  a->name,
966  false,
967  lexpr,
968  rexpr,
969  a->location);
970 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:783
Definition: nodes.h:533
Node * rexpr
Definition: parsenodes.h:277
int location
Definition: parsenodes.h:278
Node * lexpr
Definition: parsenodes.h:276
List * name
Definition: parsenodes.h:275

◆ transformAExprOpAny()

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

Definition at line 945 of file parse_expr.c.

References A_Expr::lexpr, A_Expr::location, make_scalar_array_op(), A_Expr::name, A_Expr::rexpr, and transformExprRecurse().

Referenced by transformExprRecurse().

946 {
947  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
948  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
949 
950  return (Node *) make_scalar_array_op(pstate,
951  a->name,
952  true,
953  lexpr,
954  rexpr,
955  a->location);
956 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:783
Definition: nodes.h:533
Node * rexpr
Definition: parsenodes.h:277
int location
Definition: parsenodes.h:278
Node * lexpr
Definition: parsenodes.h:276
List * name
Definition: parsenodes.h:275

◆ transformArrayExpr()

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

Definition at line 1920 of file parse_expr.c.

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

1922 {
1923  ArrayExpr *newa = makeNode(ArrayExpr);
1924  List *newelems = NIL;
1925  List *newcoercedelems = NIL;
1926  ListCell *element;
1927  Oid coerce_type;
1928  bool coerce_hard;
1929 
1930  /*
1931  * Transform the element expressions
1932  *
1933  * Assume that the array is one-dimensional unless we find an array-type
1934  * element expression.
1935  */
1936  newa->multidims = false;
1937  foreach(element, a->elements)
1938  {
1939  Node *e = (Node *) lfirst(element);
1940  Node *newe;
1941 
1942  /*
1943  * If an element is itself an A_ArrayExpr, recurse directly so that we
1944  * can pass down any target type we were given.
1945  */
1946  if (IsA(e, A_ArrayExpr))
1947  {
1948  newe = transformArrayExpr(pstate,
1949  (A_ArrayExpr *) e,
1950  array_type,
1951  element_type,
1952  typmod);
1953  /* we certainly have an array here */
1954  Assert(array_type == InvalidOid || array_type == exprType(newe));
1955  newa->multidims = true;
1956  }
1957  else
1958  {
1959  newe = transformExprRecurse(pstate, e);
1960 
1961  /*
1962  * Check for sub-array expressions, if we haven't already found
1963  * one.
1964  */
1965  if (!newa->multidims && type_is_array(exprType(newe)))
1966  newa->multidims = true;
1967  }
1968 
1969  newelems = lappend(newelems, newe);
1970  }
1971 
1972  /*
1973  * Select a target type for the elements.
1974  *
1975  * If we haven't been given a target array type, we must try to deduce a
1976  * common type based on the types of the individual elements present.
1977  */
1978  if (OidIsValid(array_type))
1979  {
1980  /* Caller must ensure array_type matches element_type */
1981  Assert(OidIsValid(element_type));
1982  coerce_type = (newa->multidims ? array_type : element_type);
1983  coerce_hard = true;
1984  }
1985  else
1986  {
1987  /* Can't handle an empty array without a target type */
1988  if (newelems == NIL)
1989  ereport(ERROR,
1990  (errcode(ERRCODE_INDETERMINATE_DATATYPE),
1991  errmsg("cannot determine type of empty array"),
1992  errhint("Explicitly cast to the desired type, "
1993  "for example ARRAY[]::integer[]."),
1994  parser_errposition(pstate, a->location)));
1995 
1996  /* Select a common type for the elements */
1997  coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
1998 
1999  if (newa->multidims)
2000  {
2001  array_type = coerce_type;
2002  element_type = get_element_type(array_type);
2003  if (!OidIsValid(element_type))
2004  ereport(ERROR,
2005  (errcode(ERRCODE_UNDEFINED_OBJECT),
2006  errmsg("could not find element type for data type %s",
2007  format_type_be(array_type)),
2008  parser_errposition(pstate, a->location)));
2009  }
2010  else
2011  {
2012  element_type = coerce_type;
2013  array_type = get_array_type(element_type);
2014  if (!OidIsValid(array_type))
2015  ereport(ERROR,
2016  (errcode(ERRCODE_UNDEFINED_OBJECT),
2017  errmsg("could not find array type for data type %s",
2018  format_type_be(element_type)),
2019  parser_errposition(pstate, a->location)));
2020  }
2021  coerce_hard = false;
2022  }
2023 
2024  /*
2025  * Coerce elements to target type
2026  *
2027  * If the array has been explicitly cast, then the elements are in turn
2028  * explicitly coerced.
2029  *
2030  * If the array's type was merely derived from the common type of its
2031  * elements, then the elements are implicitly coerced to the common type.
2032  * This is consistent with other uses of select_common_type().
2033  */
2034  foreach(element, newelems)
2035  {
2036  Node *e = (Node *) lfirst(element);
2037  Node *newe;
2038 
2039  if (coerce_hard)
2040  {
2041  newe = coerce_to_target_type(pstate, e,
2042  exprType(e),
2043  coerce_type,
2044  typmod,
2047  -1);
2048  if (newe == NULL)
2049  ereport(ERROR,
2050  (errcode(ERRCODE_CANNOT_COERCE),
2051  errmsg("cannot cast type %s to %s",
2053  format_type_be(coerce_type)),
2054  parser_errposition(pstate, exprLocation(e))));
2055  }
2056  else
2057  newe = coerce_to_common_type(pstate, e,
2058  coerce_type,
2059  "ARRAY");
2060  newcoercedelems = lappend(newcoercedelems, newe);
2061  }
2062 
2063  newa->array_typeid = array_type;
2064  /* array_collid will be set by parse_collate.c */
2065  newa->element_typeid = element_type;
2066  newa->elements = newcoercedelems;
2067  newa->location = a->location;
2068 
2069  return (Node *) newa;
2070 }
bool multidims
Definition: primnodes.h:1016
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
int errhint(const char *fmt,...)
Definition: elog.c:1152
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1231
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2706
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2734
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:533
int errcode(int sqlerrcode)
Definition: elog.c:694
Oid array_typeid
Definition: primnodes.h:1012
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:45
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:1920
List * elements
Definition: parsenodes.h:416
List * elements
Definition: primnodes.h:1015
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:336
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:376
int location
Definition: primnodes.h:1017
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
#define makeNode(_type_)
Definition: nodes.h:581
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
#define type_is_array(typid)
Definition: lsyscache.h:202
Oid element_typeid
Definition: primnodes.h:1014
e
Definition: preproc-init.c:82
int errmsg(const char *fmt,...)
Definition: elog.c:905
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Definition: pg_list.h:50

◆ transformBooleanTest()

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

Definition at line 2416 of file parse_expr.c.

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

Referenced by transformExprRecurse().

2417 {
2418  const char *clausename;
2419 
2420  switch (b->booltesttype)
2421  {
2422  case IS_TRUE:
2423  clausename = "IS TRUE";
2424  break;
2425  case IS_NOT_TRUE:
2426  clausename = "IS NOT TRUE";
2427  break;
2428  case IS_FALSE:
2429  clausename = "IS FALSE";
2430  break;
2431  case IS_NOT_FALSE:
2432  clausename = "IS NOT FALSE";
2433  break;
2434  case IS_UNKNOWN:
2435  clausename = "IS UNKNOWN";
2436  break;
2437  case IS_NOT_UNKNOWN:
2438  clausename = "IS NOT UNKNOWN";
2439  break;
2440  default:
2441  elog(ERROR, "unrecognized booltesttype: %d",
2442  (int) b->booltesttype);
2443  clausename = NULL; /* keep compiler quiet */
2444  }
2445 
2446  b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2447 
2448  b->arg = (Expr *) coerce_to_boolean(pstate,
2449  (Node *) b->arg,
2450  clausename);
2451 
2452  return (Node *) b;
2453 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
Definition: nodes.h:533
#define ERROR
Definition: elog.h:45
Expr * arg
Definition: primnodes.h:1266
BoolTestType booltesttype
Definition: primnodes.h:1267
#define elog(elevel,...)
Definition: elog.h:227
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformBoolExpr()

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

Definition at line 1313 of file parse_expr.c.

References AND_EXPR, arg, 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().

1314 {
1315  List *args = NIL;
1316  const char *opname;
1317  ListCell *lc;
1318 
1319  switch (a->boolop)
1320  {
1321  case AND_EXPR:
1322  opname = "AND";
1323  break;
1324  case OR_EXPR:
1325  opname = "OR";
1326  break;
1327  case NOT_EXPR:
1328  opname = "NOT";
1329  break;
1330  default:
1331  elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
1332  opname = NULL; /* keep compiler quiet */
1333  break;
1334  }
1335 
1336  foreach(lc, a->args)
1337  {
1338  Node *arg = (Node *) lfirst(lc);
1339 
1340  arg = transformExprRecurse(pstate, arg);
1341  arg = coerce_to_boolean(pstate, arg, opname);
1342  args = lappend(args, arg);
1343  }
1344 
1345  return (Node *) makeBoolExpr(a->boolop, args, a->location);
1346 }
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
Definition: nodes.h:533
int location
Definition: primnodes.h:605
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:369
#define ERROR
Definition: elog.h:45
BoolExprType boolop
Definition: primnodes.h:603
List * lappend(List *list, void *datum)
Definition: list.c:336
#define lfirst(lc)
Definition: pg_list.h:169
List * args
Definition: primnodes.h:604
#define elog(elevel,...)
Definition: elog.h:227
void * arg
Definition: pg_list.h:50
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformCaseExpr()

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

Definition at line 1542 of file parse_expr.c.

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

Referenced by transformExprRecurse().

1543 {
1544  CaseExpr *newc = makeNode(CaseExpr);
1545  Node *last_srf = pstate->p_last_srf;
1546  Node *arg;
1547  CaseTestExpr *placeholder;
1548  List *newargs;
1549  List *resultexprs;
1550  ListCell *l;
1551  Node *defresult;
1552  Oid ptype;
1553 
1554  /* transform the test expression, if any */
1555  arg = transformExprRecurse(pstate, (Node *) c->arg);
1556 
1557  /* generate placeholder for test expression */
1558  if (arg)
1559  {
1560  /*
1561  * If test expression is an untyped literal, force it to text. We have
1562  * to do something now because we won't be able to do this coercion on
1563  * the placeholder. This is not as flexible as what was done in 7.4
1564  * and before, but it's good enough to handle the sort of silly coding
1565  * commonly seen.
1566  */
1567  if (exprType(arg) == UNKNOWNOID)
1568  arg = coerce_to_common_type(pstate, arg, TEXTOID, "CASE");
1569 
1570  /*
1571  * Run collation assignment on the test expression so that we know
1572  * what collation to mark the placeholder with. In principle we could
1573  * leave it to parse_collate.c to do that later, but propagating the
1574  * result to the CaseTestExpr would be unnecessarily complicated.
1575  */
1576  assign_expr_collations(pstate, arg);
1577 
1578  placeholder = makeNode(CaseTestExpr);
1579  placeholder->typeId = exprType(arg);
1580  placeholder->typeMod = exprTypmod(arg);
1581  placeholder->collation = exprCollation(arg);
1582  }
1583  else
1584  placeholder = NULL;
1585 
1586  newc->arg = (Expr *) arg;
1587 
1588  /* transform the list of arguments */
1589  newargs = NIL;
1590  resultexprs = NIL;
1591  foreach(l, c->args)
1592  {
1593  CaseWhen *w = lfirst_node(CaseWhen, l);
1594  CaseWhen *neww = makeNode(CaseWhen);
1595  Node *warg;
1596 
1597  warg = (Node *) w->expr;
1598  if (placeholder)
1599  {
1600  /* shorthand form was specified, so expand... */
1601  warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1602  (Node *) placeholder,
1603  warg,
1604  w->location);
1605  }
1606  neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1607 
1608  neww->expr = (Expr *) coerce_to_boolean(pstate,
1609  (Node *) neww->expr,
1610  "CASE/WHEN");
1611 
1612  warg = (Node *) w->result;
1613  neww->result = (Expr *) transformExprRecurse(pstate, warg);
1614  neww->location = w->location;
1615 
1616  newargs = lappend(newargs, neww);
1617  resultexprs = lappend(resultexprs, neww->result);
1618  }
1619 
1620  newc->args = newargs;
1621 
1622  /* transform the default clause */
1623  defresult = (Node *) c->defresult;
1624  if (defresult == NULL)
1625  {
1626  A_Const *n = makeNode(A_Const);
1627 
1628  n->val.type = T_Null;
1629  n->location = -1;
1630  defresult = (Node *) n;
1631  }
1632  newc->defresult = (Expr *) transformExprRecurse(pstate, defresult);
1633 
1634  /*
1635  * Note: default result is considered the most significant type in
1636  * determining preferred type. This is how the code worked before, but it
1637  * seems a little bogus to me --- tgl
1638  */
1639  resultexprs = lcons(newc->defresult, resultexprs);
1640 
1641  ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
1642  Assert(OidIsValid(ptype));
1643  newc->casetype = ptype;
1644  /* casecollid will be set by parse_collate.c */
1645 
1646  /* Convert default result clause, if necessary */
1647  newc->defresult = (Expr *)
1648  coerce_to_common_type(pstate,
1649  (Node *) newc->defresult,
1650  ptype,
1651  "CASE/ELSE");
1652 
1653  /* Convert when-clause results, if necessary */
1654  foreach(l, newc->args)
1655  {
1656  CaseWhen *w = (CaseWhen *) lfirst(l);
1657 
1658  w->result = (Expr *)
1659  coerce_to_common_type(pstate,
1660  (Node *) w->result,
1661  ptype,
1662  "CASE/WHEN");
1663  }
1664 
1665  /* if any subexpression contained a SRF, complain */
1666  if (pstate->p_last_srf != last_srf)
1667  ereport(ERROR,
1668  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1669  /* translator: %s is name of a SQL construct, eg GROUP BY */
1670  errmsg("set-returning functions are not allowed in %s",
1671  "CASE"),
1672  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
1673  parser_errposition(pstate,
1674  exprLocation(pstate->p_last_srf))));
1675 
1676  newc->location = c->location;
1677 
1678  return (Node *) newc;
1679 }
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
int errhint(const char *fmt,...)
Definition: elog.c:1152
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1231
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
Oid casetype
Definition: primnodes.h:954
unsigned int Oid
Definition: postgres_ext.h:31
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
#define OidIsValid(objectId)
Definition: c.h:710
int32 typeMod
Definition: primnodes.h:997
int location
Definition: primnodes.h:970
void assign_expr_collations(ParseState *pstate, Node *expr)
#define ERROR
Definition: elog.h:45
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:336
List * args
Definition: primnodes.h:957
Node * p_last_srf
Definition: parse_node.h:213
#define ereport(elevel,...)
Definition: elog.h:155
List * lcons(void *datum, List *list)
Definition: list.c:468
#define makeNode(_type_)
Definition: nodes.h:581
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
int location
Definition: parsenodes.h:288
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
int location
Definition: primnodes.h:959
Definition: nodes.h:296
int errmsg(const char *fmt,...)
Definition: elog.c:905
void * arg
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Expr * arg
Definition: primnodes.h:956
Expr * result
Definition: primnodes.h:969
Expr * defresult
Definition: primnodes.h:958
Expr * expr
Definition: primnodes.h:968
Value val
Definition: parsenodes.h:287
Definition: pg_list.h:50
NodeTag type
Definition: value.h:44
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformCoalesceExpr()

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

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

2104 {
2106  Node *last_srf = pstate->p_last_srf;
2107  List *newargs = NIL;
2108  List *newcoercedargs = NIL;
2109  ListCell *args;
2110 
2111  foreach(args, c->args)
2112  {
2113  Node *e = (Node *) lfirst(args);
2114  Node *newe;
2115 
2116  newe = transformExprRecurse(pstate, e);
2117  newargs = lappend(newargs, newe);
2118  }
2119 
2120  newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2121  /* coalescecollid will be set by parse_collate.c */
2122 
2123  /* Convert arguments if necessary */
2124  foreach(args, newargs)
2125  {
2126  Node *e = (Node *) lfirst(args);
2127  Node *newe;
2128 
2129  newe = coerce_to_common_type(pstate, e,
2130  newc->coalescetype,
2131  "COALESCE");
2132  newcoercedargs = lappend(newcoercedargs, newe);
2133  }
2134 
2135  /* if any subexpression contained a SRF, complain */
2136  if (pstate->p_last_srf != last_srf)
2137  ereport(ERROR,
2138  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2139  /* translator: %s is name of a SQL construct, eg GROUP BY */
2140  errmsg("set-returning functions are not allowed in %s",
2141  "COALESCE"),
2142  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2143  parser_errposition(pstate,
2144  exprLocation(pstate->p_last_srf))));
2145 
2146  newc->args = newcoercedargs;
2147  newc->location = c->location;
2148  return (Node *) newc;
2149 }
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
int errhint(const char *fmt,...)
Definition: elog.c:1152
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1231
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
#define ERROR
Definition: elog.h:45
List * args
Definition: primnodes.h:1110
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:336
Node * p_last_srf
Definition: parse_node.h:213
#define ereport(elevel,...)
Definition: elog.h:155
#define makeNode(_type_)
Definition: nodes.h:581
#define lfirst(lc)
Definition: pg_list.h:169
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:905
Oid coalescetype
Definition: primnodes.h:1108
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Definition: pg_list.h:50

◆ transformCollateClause()

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

Definition at line 2624 of file parse_expr.c.

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

Referenced by transformExprRecurse().

2625 {
2626  CollateExpr *newc;
2627  Oid argtype;
2628 
2629  newc = makeNode(CollateExpr);
2630  newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2631 
2632  argtype = exprType((Node *) newc->arg);
2633 
2634  /*
2635  * The unknown type is not collatable, but coerce_type() takes care of it
2636  * separately, so we'll let it go here.
2637  */
2638  if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2639  ereport(ERROR,
2640  (errcode(ERRCODE_DATATYPE_MISMATCH),
2641  errmsg("collations are not supported by type %s",
2642  format_type_be(argtype)),
2643  parser_errposition(pstate, c->location)));
2644 
2645  newc->collOid = LookupCollation(pstate, c->collname, c->location);
2646  newc->location = c->location;
2647 
2648  return (Node *) newc;
2649 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:45
Oid LookupCollation(ParseState *pstate, List *collnames, int location)
Definition: parse_type.c:513
#define ereport(elevel,...)
Definition: elog.h:155
#define makeNode(_type_)
Definition: nodes.h:581
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Expr * arg
Definition: primnodes.h:924
List * collname
Definition: parsenodes.h:309
int errmsg(const char *fmt,...)
Definition: elog.c:905
int location
Definition: primnodes.h:926
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3028

◆ transformColumnRef()

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

Definition at line 444 of file parse_expr.c.

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

Referenced by transformExprRecurse().

445 {
446  Node *node = NULL;
447  char *nspname = NULL;
448  char *relname = NULL;
449  char *colname = NULL;
450  ParseNamespaceItem *nsitem;
451  int levels_up;
452  enum
453  {
454  CRERR_NO_COLUMN,
455  CRERR_NO_RTE,
456  CRERR_WRONG_DB,
457  CRERR_TOO_MANY
458  } crerr = CRERR_NO_COLUMN;
459  const char *err;
460 
461  /*
462  * Check to see if the column reference is in an invalid place within the
463  * query. We allow column references in most places, except in default
464  * expressions and partition bound expressions.
465  */
466  err = NULL;
467  switch (pstate->p_expr_kind)
468  {
469  case EXPR_KIND_NONE:
470  Assert(false); /* can't happen */
471  break;
472  case EXPR_KIND_OTHER:
473  case EXPR_KIND_JOIN_ON:
477  case EXPR_KIND_WHERE:
478  case EXPR_KIND_POLICY:
479  case EXPR_KIND_HAVING:
480  case EXPR_KIND_FILTER:
490  case EXPR_KIND_GROUP_BY:
491  case EXPR_KIND_ORDER_BY:
493  case EXPR_KIND_LIMIT:
494  case EXPR_KIND_OFFSET:
495  case EXPR_KIND_RETURNING:
496  case EXPR_KIND_VALUES:
511  /* okay */
512  break;
513 
515  err = _("cannot use column reference in DEFAULT expression");
516  break;
518  err = _("cannot use column reference in partition bound expression");
519  break;
520 
521  /*
522  * There is intentionally no default: case here, so that the
523  * compiler will warn if we add a new ParseExprKind without
524  * extending this switch. If we do see an unrecognized value at
525  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
526  * which is sane anyway.
527  */
528  }
529  if (err)
530  ereport(ERROR,
531  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
532  errmsg_internal("%s", err),
533  parser_errposition(pstate, cref->location)));
534 
535  /*
536  * Give the PreParseColumnRefHook, if any, first shot. If it returns
537  * non-null then that's all, folks.
538  */
539  if (pstate->p_pre_columnref_hook != NULL)
540  {
541  node = pstate->p_pre_columnref_hook(pstate, cref);
542  if (node != NULL)
543  return node;
544  }
545 
546  /*----------
547  * The allowed syntaxes are:
548  *
549  * A First try to resolve as unqualified column name;
550  * if no luck, try to resolve as unqualified table name (A.*).
551  * A.B A is an unqualified table name; B is either a
552  * column or function name (trying column name first).
553  * A.B.C schema A, table B, col or func name C.
554  * A.B.C.D catalog A, schema B, table C, col or func D.
555  * A.* A is an unqualified table name; means whole-row value.
556  * A.B.* whole-row value of table B in schema A.
557  * A.B.C.* whole-row value of table C in schema B in catalog A.
558  *
559  * We do not need to cope with bare "*"; that will only be accepted by
560  * the grammar at the top level of a SELECT list, and transformTargetList
561  * will take care of it before it ever gets here. Also, "A.*" etc will
562  * be expanded by transformTargetList if they appear at SELECT top level,
563  * so here we are only going to see them as function or operator inputs.
564  *
565  * Currently, if a catalog name is given then it must equal the current
566  * database name; we check it here and then discard it.
567  *----------
568  */
569  switch (list_length(cref->fields))
570  {
571  case 1:
572  {
573  Node *field1 = (Node *) linitial(cref->fields);
574 
575  Assert(IsA(field1, String));
576  colname = strVal(field1);
577 
578  /* Try to identify as an unqualified column */
579  node = colNameToVar(pstate, colname, false, cref->location);
580 
581  if (node == NULL)
582  {
583  /*
584  * Not known as a column of any range-table entry.
585  *
586  * Try to find the name as a relation. Note that only
587  * relations already entered into the rangetable will be
588  * recognized.
589  *
590  * This is a hack for backwards compatibility with
591  * PostQUEL-inspired syntax. The preferred form now is
592  * "rel.*".
593  */
594  nsitem = refnameNamespaceItem(pstate, NULL, colname,
595  cref->location,
596  &levels_up);
597  if (nsitem)
598  node = transformWholeRowRef(pstate, nsitem, levels_up,
599  cref->location);
600  }
601  break;
602  }
603  case 2:
604  {
605  Node *field1 = (Node *) linitial(cref->fields);
606  Node *field2 = (Node *) lsecond(cref->fields);
607 
608  Assert(IsA(field1, String));
609  relname = strVal(field1);
610 
611  /* Locate the referenced nsitem */
612  nsitem = refnameNamespaceItem(pstate, nspname, relname,
613  cref->location,
614  &levels_up);
615  if (nsitem == NULL)
616  {
617  crerr = CRERR_NO_RTE;
618  break;
619  }
620 
621  /* Whole-row reference? */
622  if (IsA(field2, A_Star))
623  {
624  node = transformWholeRowRef(pstate, nsitem, levels_up,
625  cref->location);
626  break;
627  }
628 
629  Assert(IsA(field2, String));
630  colname = strVal(field2);
631 
632  /* Try to identify as a column of the nsitem */
633  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
634  cref->location);
635  if (node == NULL)
636  {
637  /* Try it as a function call on the whole row */
638  node = transformWholeRowRef(pstate, nsitem, levels_up,
639  cref->location);
640  node = ParseFuncOrColumn(pstate,
641  list_make1(makeString(colname)),
642  list_make1(node),
643  pstate->p_last_srf,
644  NULL,
645  false,
646  cref->location);
647  }
648  break;
649  }
650  case 3:
651  {
652  Node *field1 = (Node *) linitial(cref->fields);
653  Node *field2 = (Node *) lsecond(cref->fields);
654  Node *field3 = (Node *) lthird(cref->fields);
655 
656  Assert(IsA(field1, String));
657  nspname = strVal(field1);
658  Assert(IsA(field2, String));
659  relname = strVal(field2);
660 
661  /* Locate the referenced nsitem */
662  nsitem = refnameNamespaceItem(pstate, nspname, relname,
663  cref->location,
664  &levels_up);
665  if (nsitem == NULL)
666  {
667  crerr = CRERR_NO_RTE;
668  break;
669  }
670 
671  /* Whole-row reference? */
672  if (IsA(field3, A_Star))
673  {
674  node = transformWholeRowRef(pstate, nsitem, levels_up,
675  cref->location);
676  break;
677  }
678 
679  Assert(IsA(field3, String));
680  colname = strVal(field3);
681 
682  /* Try to identify as a column of the nsitem */
683  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
684  cref->location);
685  if (node == NULL)
686  {
687  /* Try it as a function call on the whole row */
688  node = transformWholeRowRef(pstate, nsitem, levels_up,
689  cref->location);
690  node = ParseFuncOrColumn(pstate,
691  list_make1(makeString(colname)),
692  list_make1(node),
693  pstate->p_last_srf,
694  NULL,
695  false,
696  cref->location);
697  }
698  break;
699  }
700  case 4:
701  {
702  Node *field1 = (Node *) linitial(cref->fields);
703  Node *field2 = (Node *) lsecond(cref->fields);
704  Node *field3 = (Node *) lthird(cref->fields);
705  Node *field4 = (Node *) lfourth(cref->fields);
706  char *catname;
707 
708  Assert(IsA(field1, String));
709  catname = strVal(field1);
710  Assert(IsA(field2, String));
711  nspname = strVal(field2);
712  Assert(IsA(field3, String));
713  relname = strVal(field3);
714 
715  /*
716  * We check the catalog name and then ignore it.
717  */
718  if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
719  {
720  crerr = CRERR_WRONG_DB;
721  break;
722  }
723 
724  /* Locate the referenced nsitem */
725  nsitem = refnameNamespaceItem(pstate, nspname, relname,
726  cref->location,
727  &levels_up);
728  if (nsitem == NULL)
729  {
730  crerr = CRERR_NO_RTE;
731  break;
732  }
733 
734  /* Whole-row reference? */
735  if (IsA(field4, A_Star))
736  {
737  node = transformWholeRowRef(pstate, nsitem, levels_up,
738  cref->location);
739  break;
740  }
741 
742  Assert(IsA(field4, String));
743  colname = strVal(field4);
744 
745  /* Try to identify as a column of the nsitem */
746  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
747  cref->location);
748  if (node == NULL)
749  {
750  /* Try it as a function call on the whole row */
751  node = transformWholeRowRef(pstate, nsitem, levels_up,
752  cref->location);
753  node = ParseFuncOrColumn(pstate,
754  list_make1(makeString(colname)),
755  list_make1(node),
756  pstate->p_last_srf,
757  NULL,
758  false,
759  cref->location);
760  }
761  break;
762  }
763  default:
764  crerr = CRERR_TOO_MANY; /* too many dotted names */
765  break;
766  }
767 
768  /*
769  * Now give the PostParseColumnRefHook, if any, a chance. We pass the
770  * translation-so-far so that it can throw an error if it wishes in the
771  * case that it has a conflicting interpretation of the ColumnRef. (If it
772  * just translates anyway, we'll throw an error, because we can't undo
773  * whatever effects the preceding steps may have had on the pstate.) If it
774  * returns NULL, use the standard translation, or throw a suitable error
775  * if there is none.
776  */
777  if (pstate->p_post_columnref_hook != NULL)
778  {
779  Node *hookresult;
780 
781  hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
782  if (node == NULL)
783  node = hookresult;
784  else if (hookresult != NULL)
785  ereport(ERROR,
786  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
787  errmsg("column reference \"%s\" is ambiguous",
788  NameListToString(cref->fields)),
789  parser_errposition(pstate, cref->location)));
790  }
791 
792  /*
793  * Throw error if no translation found.
794  */
795  if (node == NULL)
796  {
797  switch (crerr)
798  {
799  case CRERR_NO_COLUMN:
800  errorMissingColumn(pstate, relname, colname, cref->location);
801  break;
802  case CRERR_NO_RTE:
803  errorMissingRTE(pstate, makeRangeVar(nspname, relname,
804  cref->location));
805  break;
806  case CRERR_WRONG_DB:
807  ereport(ERROR,
808  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
809  errmsg("cross-database references are not implemented: %s",
810  NameListToString(cref->fields)),
811  parser_errposition(pstate, cref->location)));
812  break;
813  case CRERR_TOO_MANY:
814  ereport(ERROR,
815  (errcode(ERRCODE_SYNTAX_ERROR),
816  errmsg("improper qualified name (too many dotted names): %s",
817  NameListToString(cref->fields)),
818  parser_errposition(pstate, cref->location)));
819  break;
820  }
821  }
822 
823  return node;
824 }
Value * makeString(char *str)
Definition: value.c:53
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
Definition: nodes.h:533
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:694
void errorMissingColumn(ParseState *pstate, const char *relname, const char *colname, int location)
NameData relname
Definition: pg_class.h:38
#define lsecond(l)
Definition: pg_list.h:179
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:220
int location
Definition: parsenodes.h:237
#define list_make1(x1)
Definition: pg_list.h:206
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:45
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:88
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
static Node * transformWholeRowRef(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location)
Definition: parse_expr.c:2508
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
char * NameListToString(List *names)
Definition: namespace.c:3101
Node * p_last_srf
Definition: parse_node.h:213
Oid MyDatabaseId
Definition: globals.c:86
ParseExprKind p_expr_kind
Definition: parse_node.h:195
#define ereport(elevel,...)
Definition: elog.h:155
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
int errmsg_internal(const char *fmt,...)
Definition: elog.c:992
#define Assert(condition)
Definition: c.h:804
#define lfourth(l)
Definition: pg_list.h:189
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define lthird(l)
Definition: pg_list.h:184
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:219
#define _(x)
Definition: elog.c:89
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:422
List * fields
Definition: parsenodes.h:236
Node * colNameToVar(ParseState *pstate, const char *colname, bool localonly, int location)

◆ transformCurrentOfExpr()

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

Definition at line 2456 of file parse_expr.c.

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

Referenced by transformExprRecurse().

2457 {
2458  /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2459  Assert(pstate->p_target_nsitem != NULL);
2460  cexpr->cvarno = pstate->p_target_nsitem->p_rtindex;
2461 
2462  /*
2463  * Check to see if the cursor name matches a parameter of type REFCURSOR.
2464  * If so, replace the raw name reference with a parameter reference. (This
2465  * is a hack for the convenience of plpgsql.)
2466  */
2467  if (cexpr->cursor_name != NULL) /* in case already transformed */
2468  {
2469  ColumnRef *cref = makeNode(ColumnRef);
2470  Node *node = NULL;
2471 
2472  /* Build an unqualified ColumnRef with the given name */
2473  cref->fields = list_make1(makeString(cexpr->cursor_name));
2474  cref->location = -1;
2475 
2476  /* See if there is a translation available from a parser hook */
2477  if (pstate->p_pre_columnref_hook != NULL)
2478  node = pstate->p_pre_columnref_hook(pstate, cref);
2479  if (node == NULL && pstate->p_post_columnref_hook != NULL)
2480  node = pstate->p_post_columnref_hook(pstate, cref, NULL);
2481 
2482  /*
2483  * XXX Should we throw an error if we get a translation that isn't a
2484  * refcursor Param? For now it seems best to silently ignore false
2485  * matches.
2486  */
2487  if (node != NULL && IsA(node, Param))
2488  {
2489  Param *p = (Param *) node;
2490 
2491  if (p->paramkind == PARAM_EXTERN &&
2492  p->paramtype == REFCURSOROID)
2493  {
2494  /* Matches, so convert CURRENT OF to a param reference */
2495  cexpr->cursor_name = NULL;
2496  cexpr->cursor_param = p->paramid;
2497  }
2498  }
2499  }
2500 
2501  return (Node *) cexpr;
2502 }
Value * makeString(char *str)
Definition: value.c:53
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
ParamKind paramkind
Definition: primnodes.h:262
Definition: nodes.h:533
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:192
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:220
int location
Definition: parsenodes.h:237
#define list_make1(x1)
Definition: pg_list.h:206
char * cursor_name
Definition: primnodes.h:1341
#define makeNode(_type_)
Definition: nodes.h:581
#define Assert(condition)
Definition: c.h:804
int paramid
Definition: primnodes.h:263
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:219
Oid paramtype
Definition: primnodes.h:264
List * fields
Definition: parsenodes.h:236

◆ transformExpr()

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

Definition at line 94 of file parse_expr.c.

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

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

95 {
96  Node *result;
97  ParseExprKind sv_expr_kind;
98 
99  /* Save and restore identity of expression type we're parsing */
100  Assert(exprKind != EXPR_KIND_NONE);
101  sv_expr_kind = pstate->p_expr_kind;
102  pstate->p_expr_kind = exprKind;
103 
104  result = transformExprRecurse(pstate, expr);
105 
106  pstate->p_expr_kind = sv_expr_kind;
107 
108  return result;
109 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
Definition: nodes.h:533
ParseExprKind
Definition: parse_node.h:38
ParseExprKind p_expr_kind
Definition: parse_node.h:195
#define Assert(condition)
Definition: c.h:804

◆ transformExprRecurse()

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

Definition at line 112 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_OP, AEXPR_OP_ALL, AEXPR_OP_ANY, AEXPR_SIMILAR, NamedArgExpr::arg, NullTest::arg, NullTest::argisrow, check_stack_depth(), elog, ereport, errcode(), errmsg(), ERROR, exprType(), InvalidOid, A_Expr::kind, A_Const::location, make_const(), nodeTag, parser_errposition(), 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(), 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(), transformAExprOp(), transformAExprOpAll(), transformAExprOpAny(), transformArrayExpr(), transformBooleanTest(), transformBoolExpr(), transformCaseExpr(), transformCoalesceExpr(), transformCollateClause(), transformExpr(), transformFuncCall(), transformIndirection(), transformMinMaxExpr(), transformMultiAssignRef(), transformSubLink(), transformTypeCast(), transformXmlExpr(), and transformXmlSerialize().

113 {
114  Node *result;
115 
116  if (expr == NULL)
117  return NULL;
118 
119  /* Guard against stack overflow due to overly complex expressions */
121 
122  switch (nodeTag(expr))
123  {
124  case T_ColumnRef:
125  result = transformColumnRef(pstate, (ColumnRef *) expr);
126  break;
127 
128  case T_ParamRef:
129  result = transformParamRef(pstate, (ParamRef *) expr);
130  break;
131 
132  case T_A_Const:
133  {
134  A_Const *con = (A_Const *) expr;
135  Value *val = &con->val;
136 
137  result = (Node *) make_const(pstate, val, con->location);
138  break;
139  }
140 
141  case T_A_Indirection:
142  result = transformIndirection(pstate, (A_Indirection *) expr);
143  break;
144 
145  case T_A_ArrayExpr:
146  result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
147  InvalidOid, InvalidOid, -1);
148  break;
149 
150  case T_TypeCast:
151  result = transformTypeCast(pstate, (TypeCast *) expr);
152  break;
153 
154  case T_CollateClause:
155  result = transformCollateClause(pstate, (CollateClause *) expr);
156  break;
157 
158  case T_A_Expr:
159  {
160  A_Expr *a = (A_Expr *) expr;
161 
162  switch (a->kind)
163  {
164  case AEXPR_OP:
165  result = transformAExprOp(pstate, a);
166  break;
167  case AEXPR_OP_ANY:
168  result = transformAExprOpAny(pstate, a);
169  break;
170  case AEXPR_OP_ALL:
171  result = transformAExprOpAll(pstate, a);
172  break;
173  case AEXPR_DISTINCT:
174  case AEXPR_NOT_DISTINCT:
175  result = transformAExprDistinct(pstate, a);
176  break;
177  case AEXPR_NULLIF:
178  result = transformAExprNullIf(pstate, a);
179  break;
180  case AEXPR_IN:
181  result = transformAExprIn(pstate, a);
182  break;
183  case AEXPR_LIKE:
184  case AEXPR_ILIKE:
185  case AEXPR_SIMILAR:
186  /* we can transform these just like AEXPR_OP */
187  result = transformAExprOp(pstate, a);
188  break;
189  case AEXPR_BETWEEN:
190  case AEXPR_NOT_BETWEEN:
191  case AEXPR_BETWEEN_SYM:
193  result = transformAExprBetween(pstate, a);
194  break;
195  default:
196  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
197  result = NULL; /* keep compiler quiet */
198  break;
199  }
200  break;
201  }
202 
203  case T_BoolExpr:
204  result = transformBoolExpr(pstate, (BoolExpr *) expr);
205  break;
206 
207  case T_FuncCall:
208  result = transformFuncCall(pstate, (FuncCall *) expr);
209  break;
210 
211  case T_MultiAssignRef:
212  result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
213  break;
214 
215  case T_GroupingFunc:
216  result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
217  break;
218 
219  case T_NamedArgExpr:
220  {
221  NamedArgExpr *na = (NamedArgExpr *) expr;
222 
223  na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
224  result = expr;
225  break;
226  }
227 
228  case T_SubLink:
229  result = transformSubLink(pstate, (SubLink *) expr);
230  break;
231 
232  case T_CaseExpr:
233  result = transformCaseExpr(pstate, (CaseExpr *) expr);
234  break;
235 
236  case T_RowExpr:
237  result = transformRowExpr(pstate, (RowExpr *) expr, false);
238  break;
239 
240  case T_CoalesceExpr:
241  result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
242  break;
243 
244  case T_MinMaxExpr:
245  result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
246  break;
247 
248  case T_SQLValueFunction:
249  result = transformSQLValueFunction(pstate,
250  (SQLValueFunction *) expr);
251  break;
252 
253  case T_XmlExpr:
254  result = transformXmlExpr(pstate, (XmlExpr *) expr);
255  break;
256 
257  case T_XmlSerialize:
258  result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
259  break;
260 
261  case T_NullTest:
262  {
263  NullTest *n = (NullTest *) expr;
264 
265  n->arg = (Expr *) transformExprRecurse(pstate, (Node *) n->arg);
266  /* the argument can be any type, so don't coerce it */
267  n->argisrow = type_is_rowtype(exprType((Node *) n->arg));
268  result = expr;
269  break;
270  }
271 
272  case T_BooleanTest:
273  result = transformBooleanTest(pstate, (BooleanTest *) expr);
274  break;
275 
276  case T_CurrentOfExpr:
277  result = transformCurrentOfExpr(pstate, (CurrentOfExpr *) expr);
278  break;
279 
280  /*
281  * In all places where DEFAULT is legal, the caller should have
282  * processed it rather than passing it to transformExpr().
283  */
284  case T_SetToDefault:
285  ereport(ERROR,
286  (errcode(ERRCODE_SYNTAX_ERROR),
287  errmsg("DEFAULT is not allowed in this context"),
288  parser_errposition(pstate,
289  ((SetToDefault *) expr)->location)));
290  break;
291 
292  /*
293  * CaseTestExpr doesn't require any processing; it is only
294  * injected into parse trees in a fully-formed state.
295  *
296  * Ordinarily we should not see a Var here, but it is convenient
297  * for transformJoinUsingClause() to create untransformed operator
298  * trees containing already-transformed Vars. The best
299  * alternative would be to deconstruct and reconstruct column
300  * references, which seems expensively pointless. So allow it.
301  */
302  case T_CaseTestExpr:
303  case T_Var:
304  {
305  result = (Node *) expr;
306  break;
307  }
308 
309  default:
310  /* should not reach here */
311  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
312  result = NULL; /* keep compiler quiet */
313  break;
314  }
315 
316  return result;
317 }
static Node * transformAExprDistinct(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:973
static Node * transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
Definition: parse_expr.c:2073
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
static Node * transformIndirection(ParseState *pstate, A_Indirection *ind)
Definition: parse_expr.c:372
static Node * transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
Definition: parse_expr.c:2456
static Node * transformAExprOpAny(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:945
A_Expr_Kind kind
Definition: parsenodes.h:274
static Node * transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
Definition: parse_expr.c:2152
static Node * transformAExprNullIf(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1024
static Node * transformSubLink(ParseState *pstate, SubLink *sublink)
Definition: parse_expr.c:1682
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
Const * make_const(ParseState *pstate, Value *value, int location)
Definition: parse_node.c:352
static Node * transformBooleanTest(ParseState *pstate, BooleanTest *b)
Definition: parse_expr.c:2416
static Node * transformXmlExpr(ParseState *pstate, XmlExpr *x)
Definition: parse_expr.c:2244
static Node * transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
Definition: parse_expr.c:2103
#define ERROR
Definition: elog.h:45
static Node * transformBoolExpr(ParseState *pstate, BoolExpr *a)
Definition: parse_expr.c:1313
Expr * arg
Definition: primnodes.h:1243
static Node * transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
Definition: parse_expr.c:1920
static Node * transformAExprOpAll(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:959
static Node * transformFuncCall(ParseState *pstate, FuncCall *fn)
Definition: parse_expr.c:1349
void check_stack_depth(void)
Definition: postgres.c:3310
static Node * transformParamRef(ParseState *pstate, ParamRef *pref)
Definition: parse_expr.c:827
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2602
static Node * transformAExprOp(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:864
Definition: nodes.h:155
static Node * transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
Definition: parse_expr.c:1394
static Node * transformCollateClause(ParseState *pstate, CollateClause *c)
Definition: parse_expr.c:2624
Node * transformGroupingFunc(ParseState *pstate, GroupingFunc *p)
Definition: parse_agg.c:247
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
Expr * arg
Definition: primnodes.h:519
int location
Definition: parsenodes.h:288
Definition: value.h:42
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
static Node * transformCaseExpr(ParseState *pstate, CaseExpr *c)
Definition: parse_expr.c:1542
#define nodeTag(nodeptr)
Definition: nodes.h:538
static Node * transformTypeCast(ParseState *pstate, TypeCast *tc)
Definition: parse_expr.c:2540
static Node * transformColumnRef(ParseState *pstate, ColumnRef *cref)
Definition: parse_expr.c:444
int errmsg(const char *fmt,...)
Definition: elog.c:905
static Node * transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
Definition: parse_expr.c:2373
#define elog(elevel,...)
Definition: elog.h:227
static Node * transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
Definition: parse_expr.c:2191
bool argisrow
Definition: primnodes.h:1245
Value val
Definition: parsenodes.h:287
long val
Definition: informix.c:664
static Node * transformAExprBetween(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1219
static Node * transformAExprIn(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1066

◆ transformFuncCall()

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

Definition at line 1349 of file parse_expr.c.

References FuncCall::agg_order, FuncCall::agg_within_group, arg, 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().

1350 {
1351  Node *last_srf = pstate->p_last_srf;
1352  List *targs;
1353  ListCell *args;
1354 
1355  /* Transform the list of arguments ... */
1356  targs = NIL;
1357  foreach(args, fn->args)
1358  {
1359  targs = lappend(targs, transformExprRecurse(pstate,
1360  (Node *) lfirst(args)));
1361  }
1362 
1363  /*
1364  * When WITHIN GROUP is used, we treat its ORDER BY expressions as
1365  * additional arguments to the function, for purposes of function lookup
1366  * and argument type coercion. So, transform each such expression and add
1367  * them to the targs list. We don't explicitly mark where each argument
1368  * came from, but ParseFuncOrColumn can tell what's what by reference to
1369  * list_length(fn->agg_order).
1370  */
1371  if (fn->agg_within_group)
1372  {
1373  Assert(fn->agg_order != NIL);
1374  foreach(args, fn->agg_order)
1375  {
1376  SortBy *arg = (SortBy *) lfirst(args);
1377 
1378  targs = lappend(targs, transformExpr(pstate, arg->node,
1380  }
1381  }
1382 
1383  /* ... and hand off to ParseFuncOrColumn */
1384  return ParseFuncOrColumn(pstate,
1385  fn->funcname,
1386  targs,
1387  last_srf,
1388  fn,
1389  false,
1390  fn->location);
1391 }
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:94
Definition: nodes.h:533
int location
Definition: parsenodes.h:360
List * agg_order
Definition: parsenodes.h:352
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:88
List * lappend(List *list, void *datum)
Definition: list.c:336
Node * p_last_srf
Definition: parse_node.h:213
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * args
Definition: parsenodes.h:351
void * arg
List * funcname
Definition: parsenodes.h:350
Node * node
Definition: parsenodes.h:470
bool agg_within_group
Definition: parsenodes.h:355
Definition: pg_list.h:50

◆ transformIndirection()

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

Definition at line 372 of file parse_expr.c.

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

Referenced by transformExprRecurse().

373 {
374  Node *last_srf = pstate->p_last_srf;
375  Node *result = transformExprRecurse(pstate, ind->arg);
376  List *subscripts = NIL;
377  int location = exprLocation(result);
378  ListCell *i;
379 
380  /*
381  * We have to split any field-selection operations apart from
382  * subscripting. Adjacent A_Indices nodes have to be treated as a single
383  * multidimensional subscript operation.
384  */
385  foreach(i, ind->indirection)
386  {
387  Node *n = lfirst(i);
388 
389  if (IsA(n, A_Indices))
390  subscripts = lappend(subscripts, n);
391  else if (IsA(n, A_Star))
392  {
393  ereport(ERROR,
394  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
395  errmsg("row expansion via \"*\" is not supported here"),
396  parser_errposition(pstate, location)));
397  }
398  else
399  {
400  Node *newresult;
401 
402  Assert(IsA(n, String));
403 
404  /* process subscripts before this field selection */
405  if (subscripts)
406  result = (Node *) transformContainerSubscripts(pstate,
407  result,
408  exprType(result),
409  exprTypmod(result),
410  subscripts,
411  false);
412  subscripts = NIL;
413 
414  newresult = ParseFuncOrColumn(pstate,
415  list_make1(n),
416  list_make1(result),
417  last_srf,
418  NULL,
419  false,
420  location);
421  if (newresult == NULL)
422  unknown_attribute(pstate, result, strVal(n), location);
423  result = newresult;
424  }
425  }
426  /* process trailing subscripts, if any */
427  if (subscripts)
428  result = (Node *) transformContainerSubscripts(pstate,
429  result,
430  exprType(result),
431  exprTypmod(result),
432  subscripts,
433  false);
434 
435  return result;
436 }
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
static void unknown_attribute(ParseState *pstate, Node *relref, const char *attname, int location)
Definition: parse_expr.c:326
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1231
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
Definition: nodes.h:533
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:694
#define list_make1(x1)
Definition: pg_list.h:206
#define ERROR
Definition: elog.h:45
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:88
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
Definition: parse_node.c:248
List * lappend(List *list, void *datum)
Definition: list.c:336
Node * p_last_srf
Definition: parse_node.h:213
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * indirection
Definition: parsenodes.h:407
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:905
int i
Definition: pg_list.h:50

◆ transformMinMaxExpr()

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

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

2153 {
2154  MinMaxExpr *newm = makeNode(MinMaxExpr);
2155  List *newargs = NIL;
2156  List *newcoercedargs = NIL;
2157  const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2158  ListCell *args;
2159 
2160  newm->op = m->op;
2161  foreach(args, m->args)
2162  {
2163  Node *e = (Node *) lfirst(args);
2164  Node *newe;
2165 
2166  newe = transformExprRecurse(pstate, e);
2167  newargs = lappend(newargs, newe);
2168  }
2169 
2170  newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2171  /* minmaxcollid and inputcollid will be set by parse_collate.c */
2172 
2173  /* Convert arguments if necessary */
2174  foreach(args, newargs)
2175  {
2176  Node *e = (Node *) lfirst(args);
2177  Node *newe;
2178 
2179  newe = coerce_to_common_type(pstate, e,
2180  newm->minmaxtype,
2181  funcname);
2182  newcoercedargs = lappend(newcoercedargs, newe);
2183  }
2184 
2185  newm->args = newcoercedargs;
2186  newm->location = m->location;
2187  return (Node *) newm;
2188 }
Oid minmaxtype
Definition: primnodes.h:1126
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
List * args
Definition: primnodes.h:1130
Definition: nodes.h:533
int location
Definition: primnodes.h:1131
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:336
#define makeNode(_type_)
Definition: nodes.h:581
#define lfirst(lc)
Definition: pg_list.h:169
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:1129
Definition: pg_list.h:50

◆ transformMultiAssignRef()

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

Definition at line 1394 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_last(), 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().

1395 {
1396  SubLink *sublink;
1397  RowExpr *rexpr;
1398  Query *qtree;
1399  TargetEntry *tle;
1400 
1401  /* We should only see this in first-stage processing of UPDATE tlists */
1403 
1404  /* We only need to transform the source if this is the first column */
1405  if (maref->colno == 1)
1406  {
1407  /*
1408  * For now, we only allow EXPR SubLinks and RowExprs as the source of
1409  * an UPDATE multiassignment. This is sufficient to cover interesting
1410  * cases; at worst, someone would have to write (SELECT * FROM expr)
1411  * to expand a composite-returning expression of another form.
1412  */
1413  if (IsA(maref->source, SubLink) &&
1414  ((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
1415  {
1416  /* Relabel it as a MULTIEXPR_SUBLINK */
1417  sublink = (SubLink *) maref->source;
1418  sublink->subLinkType = MULTIEXPR_SUBLINK;
1419  /* And transform it */
1420  sublink = (SubLink *) transformExprRecurse(pstate,
1421  (Node *) sublink);
1422 
1423  qtree = castNode(Query, sublink->subselect);
1424 
1425  /* Check subquery returns required number of columns */
1426  if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
1427  ereport(ERROR,
1428  (errcode(ERRCODE_SYNTAX_ERROR),
1429  errmsg("number of columns does not match number of values"),
1430  parser_errposition(pstate, sublink->location)));
1431 
1432  /*
1433  * Build a resjunk tlist item containing the MULTIEXPR SubLink,
1434  * and add it to pstate->p_multiassign_exprs, whence it will later
1435  * get appended to the completed targetlist. We needn't worry
1436  * about selecting a resno for it; transformUpdateStmt will do
1437  * that.
1438  */
1439  tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
1441  tle);
1442 
1443  /*
1444  * Assign a unique-within-this-targetlist ID to the MULTIEXPR
1445  * SubLink. We can just use its position in the
1446  * p_multiassign_exprs list.
1447  */
1448  sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
1449  }
1450  else if (IsA(maref->source, RowExpr))
1451  {
1452  /* Transform the RowExpr, allowing SetToDefault items */
1453  rexpr = (RowExpr *) transformRowExpr(pstate,
1454  (RowExpr *) maref->source,
1455  true);
1456 
1457  /* Check it returns required number of columns */
1458  if (list_length(rexpr->args) != maref->ncolumns)
1459  ereport(ERROR,
1460  (errcode(ERRCODE_SYNTAX_ERROR),
1461  errmsg("number of columns does not match number of values"),
1462  parser_errposition(pstate, rexpr->location)));
1463 
1464  /*
1465  * Temporarily append it to p_multiassign_exprs, so we can get it
1466  * back when we come back here for additional columns.
1467  */
1468  tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
1470  tle);
1471  }
1472  else
1473  ereport(ERROR,
1474  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1475  errmsg("source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression"),
1476  parser_errposition(pstate, exprLocation(maref->source))));
1477  }
1478  else
1479  {
1480  /*
1481  * Second or later column in a multiassignment. Re-fetch the
1482  * transformed SubLink or RowExpr, which we assume is still the last
1483  * entry in p_multiassign_exprs.
1484  */
1485  Assert(pstate->p_multiassign_exprs != NIL);
1486  tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
1487  }
1488 
1489  /*
1490  * Emit the appropriate output expression for the current column
1491  */
1492  if (IsA(tle->expr, SubLink))
1493  {
1494  Param *param;
1495 
1496  sublink = (SubLink *) tle->expr;
1497  Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
1498  qtree = castNode(Query, sublink->subselect);
1499 
1500  /* Build a Param representing the current subquery output column */
1501  tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
1502  Assert(!tle->resjunk);
1503 
1504  param = makeNode(Param);
1505  param->paramkind = PARAM_MULTIEXPR;
1506  param->paramid = (sublink->subLinkId << 16) | maref->colno;
1507  param->paramtype = exprType((Node *) tle->expr);
1508  param->paramtypmod = exprTypmod((Node *) tle->expr);
1509  param->paramcollid = exprCollation((Node *) tle->expr);
1510  param->location = exprLocation((Node *) tle->expr);
1511 
1512  return (Node *) param;
1513  }
1514 
1515  if (IsA(tle->expr, RowExpr))
1516  {
1517  Node *result;
1518 
1519  rexpr = (RowExpr *) tle->expr;
1520 
1521  /* Just extract and return the next element of the RowExpr */
1522  result = (Node *) list_nth(rexpr->args, maref->colno - 1);
1523 
1524  /*
1525  * If we're at the last column, delete the RowExpr from
1526  * p_multiassign_exprs; we don't need it anymore, and don't want it in
1527  * the finished UPDATE tlist. We assume this is still the last entry
1528  * in p_multiassign_exprs.
1529  */
1530  if (maref->colno == maref->ncolumns)
1531  pstate->p_multiassign_exprs =
1533 
1534  return result;
1535  }
1536 
1537  elog(ERROR, "unexpected expr type in multiassign list");
1538  return NULL; /* keep compiler quiet */
1539 }
#define NIL
Definition: pg_list.h:65
static Node * transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
Definition: parse_expr.c:2073
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
List * args
Definition: primnodes.h:1046
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1231
#define castNode(_type_, nodeptr)
Definition: nodes.h:602
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
#define llast(l)
Definition: pg_list.h:194
ParamKind paramkind
Definition: primnodes.h:262
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
List * p_multiassign_exprs
Definition: parse_node.h:197
List * targetList
Definition: parsenodes.h:140
bool resjunk
Definition: primnodes.h:1438
#define ERROR
Definition: elog.h:45
Oid paramcollid
Definition: primnodes.h:266
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
int location
Definition: primnodes.h:267
int location
Definition: primnodes.h:1063
List * list_delete_last(List *list)
Definition: list.c:892
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
List * lappend(List *list, void *datum)
Definition: list.c:336
ParseExprKind p_expr_kind
Definition: parse_node.h:195
#define ereport(elevel,...)
Definition: elog.h:155
int32 paramtypmod
Definition: primnodes.h:265
#define makeNode(_type_)
Definition: nodes.h:581
#define Assert(condition)
Definition: c.h:804
Expr * expr
Definition: primnodes.h:1431
int paramid
Definition: primnodes.h:263
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define elog(elevel,...)
Definition: elog.h:227
int count_nonjunk_tlist_entries(List *tlist)
Definition: tlist.c:203
Oid paramtype
Definition: primnodes.h:264

◆ transformParamRef()

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

Definition at line 827 of file parse_expr.c.

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

Referenced by transformExprRecurse().

828 {
829  Node *result;
830 
831  /*
832  * The core parser knows nothing about Params. If a hook is supplied,
833  * call it. If not, or if the hook returns NULL, throw a generic error.
834  */
835  if (pstate->p_paramref_hook != NULL)
836  result = pstate->p_paramref_hook(pstate, pref);
837  else
838  result = NULL;
839 
840  if (result == NULL)
841  ereport(ERROR,
842  (errcode(ERRCODE_UNDEFINED_PARAMETER),
843  errmsg("there is no parameter $%d", pref->number),
844  parser_errposition(pstate, pref->location)));
845 
846  return result;
847 }
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
#define ERROR
Definition: elog.h:45
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:221
int number
Definition: parsenodes.h:246
#define ereport(elevel,...)
Definition: elog.h:155
int location
Definition: parsenodes.h:247
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:905

◆ transformRowExpr()

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

Definition at line 2073 of file parse_expr.c.

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

Referenced by transformExprRecurse(), and transformMultiAssignRef().

2074 {
2075  RowExpr *newr;
2076  char fname[16];
2077  int fnum;
2078 
2079  newr = makeNode(RowExpr);
2080 
2081  /* Transform the field expressions */
2082  newr->args = transformExpressionList(pstate, r->args,
2083  pstate->p_expr_kind, allowDefault);
2084 
2085  /* Barring later casting, we consider the type RECORD */
2086  newr->row_typeid = RECORDOID;
2088 
2089  /* ROW() has anonymous columns, so invent some field names */
2090  newr->colnames = NIL;
2091  for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2092  {
2093  snprintf(fname, sizeof(fname), "f%d", fnum);
2094  newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2095  }
2096 
2097  newr->location = r->location;
2098 
2099  return (Node *) newr;
2100 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
List * args
Definition: primnodes.h:1046
char * pstrdup(const char *in)
Definition: mcxt.c:1187
Definition: nodes.h:533
List * colnames
Definition: primnodes.h:1062
int location
Definition: primnodes.h:1063
List * lappend(List *list, void *datum)
Definition: list.c:336
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
Definition: parse_target.c:220
ParseExprKind p_expr_kind
Definition: parse_node.h:195
#define makeNode(_type_)
Definition: nodes.h:581
Oid row_typeid
Definition: primnodes.h:1047
static int list_length(const List *l)
Definition: pg_list.h:149
CoercionForm row_format
Definition: primnodes.h:1061
#define snprintf
Definition: port.h:216

◆ transformSQLValueFunction()

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

Definition at line 2191 of file parse_expr.c.

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

Referenced by transformExprRecurse().

2192 {
2193  /*
2194  * All we need to do is insert the correct result type and (where needed)
2195  * validate the typmod, so we just modify the node in-place.
2196  */
2197  switch (svf->op)
2198  {
2199  case SVFOP_CURRENT_DATE:
2200  svf->type = DATEOID;
2201  break;
2202  case SVFOP_CURRENT_TIME:
2203  svf->type = TIMETZOID;
2204  break;
2205  case SVFOP_CURRENT_TIME_N:
2206  svf->type = TIMETZOID;
2207  svf->typmod = anytime_typmod_check(true, svf->typmod);
2208  break;
2210  svf->type = TIMESTAMPTZOID;
2211  break;
2213  svf->type = TIMESTAMPTZOID;
2214  svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2215  break;
2216  case SVFOP_LOCALTIME:
2217  svf->type = TIMEOID;
2218  break;
2219  case SVFOP_LOCALTIME_N:
2220  svf->type = TIMEOID;
2221  svf->typmod = anytime_typmod_check(false, svf->typmod);
2222  break;
2223  case SVFOP_LOCALTIMESTAMP:
2224  svf->type = TIMESTAMPOID;
2225  break;
2227  svf->type = TIMESTAMPOID;
2228  svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2229  break;
2230  case SVFOP_CURRENT_ROLE:
2231  case SVFOP_CURRENT_USER:
2232  case SVFOP_USER:
2233  case SVFOP_SESSION_USER:
2234  case SVFOP_CURRENT_CATALOG:
2235  case SVFOP_CURRENT_SCHEMA:
2236  svf->type = NAMEOID;
2237  break;
2238  }
2239 
2240  return (Node *) svf;
2241 }
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:68
Definition: nodes.h:533
SQLValueFunctionOp op
Definition: primnodes.h:1167
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:102

◆ transformSubLink()

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

Definition at line 1682 of file parse_expr.c.

References _, ARRAY_SUBLINK, Assert, CMD_SELECT, Query::commandType, count_nonjunk_tlist_entries(), elog, ereport, errcode(), errmsg(), errmsg_internal(), ERROR, EXISTS_SUBLINK, TargetEntry::expr, EXPR_KIND_ALTER_COL_TRANSFORM, EXPR_KIND_CALL_ARGUMENT, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_COLUMN_DEFAULT, EXPR_KIND_COPY_WHERE, EXPR_KIND_CYCLE_MARK, EXPR_KIND_DISTINCT_ON, EXPR_KIND_DOMAIN_CHECK, EXPR_KIND_EXECUTE_PARAMETER, EXPR_KIND_FILTER, EXPR_KIND_FROM_FUNCTION, EXPR_KIND_FROM_SUBSELECT, EXPR_KIND_FUNCTION_DEFAULT, EXPR_KIND_GENERATED_COLUMN, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_INDEX_EXPRESSION, EXPR_KIND_INDEX_PREDICATE, EXPR_KIND_INSERT_TARGET, EXPR_KIND_JOIN_ON, EXPR_KIND_JOIN_USING, EXPR_KIND_LIMIT, EXPR_KIND_NONE, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_OTHER, EXPR_KIND_PARTITION_BOUND, EXPR_KIND_PARTITION_EXPRESSION, EXPR_KIND_POLICY, EXPR_KIND_RETURNING, EXPR_KIND_SELECT_TARGET, EXPR_KIND_TRIGGER_WHEN, EXPR_KIND_UPDATE_SOURCE, EXPR_KIND_UPDATE_TARGET, EXPR_KIND_VALUES, EXPR_KIND_VALUES_SINGLE, EXPR_KIND_WHERE, EXPR_KIND_WINDOW_FRAME_GROUPS, EXPR_KIND_WINDOW_FRAME_RANGE, EXPR_KIND_WINDOW_FRAME_ROWS, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, EXPR_SUBLINK, exprCollation(), exprType(), exprTypmod(), IsA, lappend(), lfirst, list_length(), list_make1, Param::location, SubLink::location, make_row_comparison_op(), makeNode, makeString(), MULTIEXPR_SUBLINK, NIL, 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(), TargetEntry::resjunk, TargetEntry::resno, SubLink::subLinkType, SubLink::subselect, Query::targetList, SubLink::testexpr, and transformExprRecurse().

Referenced by transformExprRecurse().

1683 {
1684  Node *result = (Node *) sublink;
1685  Query *qtree;
1686  const char *err;
1687 
1688  /*
1689  * Check to see if the sublink is in an invalid place within the query. We
1690  * allow sublinks everywhere in SELECT/INSERT/UPDATE/DELETE, but generally
1691  * not in utility statements.
1692  */
1693  err = NULL;
1694  switch (pstate->p_expr_kind)
1695  {
1696  case EXPR_KIND_NONE:
1697  Assert(false); /* can't happen */
1698  break;
1699  case EXPR_KIND_OTHER:
1700  /* Accept sublink here; caller must throw error if wanted */
1701  break;
1702  case EXPR_KIND_JOIN_ON:
1703  case EXPR_KIND_JOIN_USING:
1706  case EXPR_KIND_WHERE:
1707  case EXPR_KIND_POLICY:
1708  case EXPR_KIND_HAVING:
1709  case EXPR_KIND_FILTER:
1719  case EXPR_KIND_GROUP_BY:
1720  case EXPR_KIND_ORDER_BY:
1721  case EXPR_KIND_DISTINCT_ON:
1722  case EXPR_KIND_LIMIT:
1723  case EXPR_KIND_OFFSET:
1724  case EXPR_KIND_RETURNING:
1725  case EXPR_KIND_VALUES:
1727  case EXPR_KIND_CYCLE_MARK:
1728  /* okay */
1729  break;
1732  err = _("cannot use subquery in check constraint");
1733  break;
1736  err = _("cannot use subquery in DEFAULT expression");
1737  break;
1739  err = _("cannot use subquery in index expression");
1740  break;
1742  err = _("cannot use subquery in index predicate");
1743  break;
1745  err = _("cannot use subquery in transform expression");
1746  break;
1748  err = _("cannot use subquery in EXECUTE parameter");
1749  break;
1751  err = _("cannot use subquery in trigger WHEN condition");
1752  break;
1754  err = _("cannot use subquery in partition bound");
1755  break;
1757  err = _("cannot use subquery in partition key expression");
1758  break;
1760  err = _("cannot use subquery in CALL argument");
1761  break;
1762  case EXPR_KIND_COPY_WHERE:
1763  err = _("cannot use subquery in COPY FROM WHERE condition");
1764  break;
1766  err = _("cannot use subquery in column generation expression");
1767  break;
1768 
1769  /*
1770  * There is intentionally no default: case here, so that the
1771  * compiler will warn if we add a new ParseExprKind without
1772  * extending this switch. If we do see an unrecognized value at
1773  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
1774  * which is sane anyway.
1775  */
1776  }
1777  if (err)
1778  ereport(ERROR,
1779  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1780  errmsg_internal("%s", err),
1781  parser_errposition(pstate, sublink->location)));
1782 
1783  pstate->p_hasSubLinks = true;
1784 
1785  /*
1786  * OK, let's transform the sub-SELECT.
1787  */
1788  qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
1789 
1790  /*
1791  * Check that we got a SELECT. Anything else should be impossible given
1792  * restrictions of the grammar, but check anyway.
1793  */
1794  if (!IsA(qtree, Query) ||
1795  qtree->commandType != CMD_SELECT)
1796  elog(ERROR, "unexpected non-SELECT command in SubLink");
1797 
1798  sublink->subselect = (Node *) qtree;
1799 
1800  if (sublink->subLinkType == EXISTS_SUBLINK)
1801  {
1802  /*
1803  * EXISTS needs no test expression or combining operator. These fields
1804  * should be null already, but make sure.
1805  */
1806  sublink->testexpr = NULL;
1807  sublink->operName = NIL;
1808  }
1809  else if (sublink->subLinkType == EXPR_SUBLINK ||
1810  sublink->subLinkType == ARRAY_SUBLINK)
1811  {
1812  /*
1813  * Make sure the subselect delivers a single column (ignoring resjunk
1814  * targets).
1815  */
1816  if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
1817  ereport(ERROR,
1818  (errcode(ERRCODE_SYNTAX_ERROR),
1819  errmsg("subquery must return only one column"),
1820  parser_errposition(pstate, sublink->location)));
1821 
1822  /*
1823  * EXPR and ARRAY need no test expression or combining operator. These
1824  * fields should be null already, but make sure.
1825  */
1826  sublink->testexpr = NULL;
1827  sublink->operName = NIL;
1828  }
1829  else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1830  {
1831  /* Same as EXPR case, except no restriction on number of columns */
1832  sublink->testexpr = NULL;
1833  sublink->operName = NIL;
1834  }
1835  else
1836  {
1837  /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
1838  Node *lefthand;
1839  List *left_list;
1840  List *right_list;
1841  ListCell *l;
1842 
1843  /*
1844  * If the source was "x IN (select)", convert to "x = ANY (select)".
1845  */
1846  if (sublink->operName == NIL)
1847  sublink->operName = list_make1(makeString("="));
1848 
1849  /*
1850  * Transform lefthand expression, and convert to a list
1851  */
1852  lefthand = transformExprRecurse(pstate, sublink->testexpr);
1853  if (lefthand && IsA(lefthand, RowExpr))
1854  left_list = ((RowExpr *) lefthand)->args;
1855  else
1856  left_list = list_make1(lefthand);
1857 
1858  /*
1859  * Build a list of PARAM_SUBLINK nodes representing the output columns
1860  * of the subquery.
1861  */
1862  right_list = NIL;
1863  foreach(l, qtree->targetList)
1864  {
1865  TargetEntry *tent = (TargetEntry *) lfirst(l);
1866  Param *param;
1867 
1868  if (tent->resjunk)
1869  continue;
1870 
1871  param = makeNode(Param);
1872  param->paramkind = PARAM_SUBLINK;
1873  param->paramid = tent->resno;
1874  param->paramtype = exprType((Node *) tent->expr);
1875  param->paramtypmod = exprTypmod((Node *) tent->expr);
1876  param->paramcollid = exprCollation((Node *) tent->expr);
1877  param->location = -1;
1878 
1879  right_list = lappend(right_list, param);
1880  }
1881 
1882  /*
1883  * We could rely on make_row_comparison_op to complain if the list
1884  * lengths differ, but we prefer to generate a more specific error
1885  * message.
1886  */
1887  if (list_length(left_list) < list_length(right_list))
1888  ereport(ERROR,
1889  (errcode(ERRCODE_SYNTAX_ERROR),
1890  errmsg("subquery has too many columns"),
1891  parser_errposition(pstate, sublink->location)));
1892  if (list_length(left_list) > list_length(right_list))
1893  ereport(ERROR,
1894  (errcode(ERRCODE_SYNTAX_ERROR),
1895  errmsg("subquery has too few columns"),
1896  parser_errposition(pstate, sublink->location)));
1897 
1898  /*
1899  * Identify the combining operator(s) and generate a suitable
1900  * row-comparison expression.
1901  */
1902  sublink->testexpr = make_row_comparison_op(pstate,
1903  sublink->operName,
1904  left_list,
1905  right_list,
1906  sublink->location);
1907  }
1908 
1909  return result;
1910 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
bool p_hasSubLinks
Definition: parse_node.h:210
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
ParamKind paramkind
Definition: primnodes.h:262
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2664
List * targetList
Definition: parsenodes.h:140
#define list_make1(x1)
Definition: pg_list.h:206
bool resjunk
Definition: primnodes.h:1438
#define ERROR
Definition: elog.h:45
Oid paramcollid
Definition: primnodes.h:266
int location
Definition: primnodes.h:267
AttrNumber resno
Definition: primnodes.h:1432
List * lappend(List *list, void *datum)
Definition: list.c:336
ParseExprKind p_expr_kind
Definition: parse_node.h:195
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition: analyze.c:168
#define ereport(elevel,...)
Definition: elog.h:155
CmdType commandType
Definition: parsenodes.h:112
int32 paramtypmod
Definition: primnodes.h:265
int errmsg_internal(const char *fmt,...)
Definition: elog.c:992
#define makeNode(_type_)
Definition: nodes.h:581
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1431
int paramid
Definition: primnodes.h:263
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define elog(elevel,...)
Definition: elog.h:227
int count_nonjunk_tlist_entries(List *tlist)
Definition: tlist.c:203
Definition: pg_list.h:50
#define _(x)
Definition: elog.c:89
Oid paramtype
Definition: primnodes.h:264

◆ transformTypeCast()

static Node * transformTypeCast ( ParseState pstate,
TypeCast tc 
)
static

Definition at line 2540 of file parse_expr.c.

References arg, TypeCast::arg, COERCE_EXPLICIT_CAST, coerce_to_target_type(), COERCION_EXPLICIT, ereport, errcode(), errmsg(), ERROR, exprType(), format_type_be(), get_element_type(), getBaseTypeAndTypmod(), InvalidOid, IsA, TypeName::location, TypeCast::location, OidIsValid, parser_coercion_errposition(), transformArrayExpr(), transformExprRecurse(), TypeCast::typeName, and typenameTypeIdAndMod().

Referenced by transformExprRecurse().

2541 {
2542  Node *result;
2543  Node *arg = tc->arg;
2544  Node *expr;
2545  Oid inputType;
2546  Oid targetType;
2547  int32 targetTypmod;
2548  int location;
2549 
2550  /* Look up the type name first */
2551  typenameTypeIdAndMod(pstate, tc->typeName, &targetType, &targetTypmod);
2552 
2553  /*
2554  * If the subject of the typecast is an ARRAY[] construct and the target
2555  * type is an array type, we invoke transformArrayExpr() directly so that
2556  * we can pass down the type information. This avoids some cases where
2557  * transformArrayExpr() might not infer the correct type. Otherwise, just
2558  * transform the argument normally.
2559  */
2560  if (IsA(arg, A_ArrayExpr))
2561  {
2562  Oid targetBaseType;
2563  int32 targetBaseTypmod;
2564  Oid elementType;
2565 
2566  /*
2567  * If target is a domain over array, work with the base array type
2568  * here. Below, we'll cast the array type to the domain. In the
2569  * usual case that the target is not a domain, the remaining steps
2570  * will be a no-op.
2571  */
2572  targetBaseTypmod = targetTypmod;
2573  targetBaseType = getBaseTypeAndTypmod(targetType, &targetBaseTypmod);
2574  elementType = get_element_type(targetBaseType);
2575  if (OidIsValid(elementType))
2576  {
2577  expr = transformArrayExpr(pstate,
2578  (A_ArrayExpr *) arg,
2579  targetBaseType,
2580  elementType,
2581  targetBaseTypmod);
2582  }
2583  else
2584  expr = transformExprRecurse(pstate, arg);
2585  }
2586  else
2587  expr = transformExprRecurse(pstate, arg);
2588 
2589  inputType = exprType(expr);
2590  if (inputType == InvalidOid)
2591  return expr; /* do nothing if NULL input */
2592 
2593  /*
2594  * Location of the coercion is preferentially the location of the :: or
2595  * CAST symbol, but if there is none then use the location of the type
2596  * name (this can happen in TypeName 'string' syntax, for instance).
2597  */
2598  location = tc->location;
2599  if (location < 0)
2600  location = tc->typeName->location;
2601 
2602  result = coerce_to_target_type(pstate, expr, inputType,
2603  targetType, targetTypmod,
2606  location);
2607  if (result == NULL)
2608  ereport(ERROR,
2609  (errcode(ERRCODE_CANNOT_COERCE),
2610  errmsg("cannot cast type %s to %s",
2611  format_type_be(inputType),
2612  format_type_be(targetType)),
2613  parser_coercion_errposition(pstate, location, expr)));
2614 
2615  return result;
2616 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2485
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2706
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
signed int int32
Definition: c.h:429
int location
Definition: parsenodes.h:299
#define ERROR
Definition: elog.h:45
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:1920
TypeName * typeName
Definition: parsenodes.h:298
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
int location
Definition: parsenodes.h:217
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int errmsg(const char *fmt,...)
Definition: elog.c:905
int parser_coercion_errposition(ParseState *pstate, int coerce_location, Node *input_expr)
void * arg
Node * arg
Definition: parsenodes.h:297

◆ transformWholeRowRef()

static Node * transformWholeRowRef ( ParseState pstate,
ParseNamespaceItem nsitem,
int  sublevels_up,
int  location 
)
static

Definition at line 2508 of file parse_expr.c.

References Var::location, makeWholeRowVar(), markVarForSelectPriv(), ParseNamespaceItem::p_rte, and ParseNamespaceItem::p_rtindex.

Referenced by transformColumnRef().

2510 {
2511  Var *result;
2512 
2513  /*
2514  * Build the appropriate referencing node. Note that if the RTE is a
2515  * function returning scalar, we create just a plain reference to the
2516  * function value, not a composite containing a single column. This is
2517  * pretty inconsistent at first sight, but it's what we've done
2518  * historically. One argument for it is that "rel" and "rel.*" mean the
2519  * same thing for composite relations, so why not for scalar functions...
2520  */
2521  result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
2522  sublevels_up, true);
2523 
2524  /* location is not filled in by makeWholeRowVar */
2525  result->location = location;
2526 
2527  /* mark relation as requiring whole-row SELECT access */
2528  markVarForSelectPriv(pstate, result);
2529 
2530  return (Node *) result;
2531 }
void markVarForSelectPriv(ParseState *pstate, Var *var)
Definition: nodes.h:533
Var * makeWholeRowVar(RangeTblEntry *rte, Index varno, Index varlevelsup, bool allowScalar)
Definition: makefuncs.c:133
Definition: primnodes.h:181
RangeTblEntry * p_rte
Definition: parse_node.h:258
int location
Definition: primnodes.h:196

◆ transformXmlExpr()

static Node * transformXmlExpr ( ParseState pstate,
XmlExpr x 
)
static

Definition at line 2244 of file parse_expr.c.

References XmlExpr::arg_names, XmlExpr::args, Assert, coerce_to_boolean(), coerce_to_specific_type(), ereport, errcode(), errmsg(), ERROR, FigureColname(), i, IS_DOCUMENT, IS_XMLCONCAT, IS_XMLELEMENT, IS_XMLFOREST, IS_XMLPARSE, IS_XMLPI, IS_XMLROOT, IS_XMLSERIALIZE, IsA, lappend(), lfirst, lfirst_node, ResTarget::location, XmlExpr::location, makeNode, makeString(), map_sql_identifier_to_xml_name(), ResTarget::name, XmlExpr::name, XmlExpr::named_args, NIL, XmlExpr::op, parser_errposition(), strVal, transformExprRecurse(), XmlExpr::type, XmlExpr::typmod, ResTarget::val, and XmlExpr::xmloption.

Referenced by transformExprRecurse().

2245 {
2246  XmlExpr *newx;
2247  ListCell *lc;
2248  int i;
2249 
2250  newx = makeNode(XmlExpr);
2251  newx->op = x->op;
2252  if (x->name)
2253  newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
2254  else
2255  newx->name = NULL;
2256  newx->xmloption = x->xmloption;
2257  newx->type = XMLOID; /* this just marks the node as transformed */
2258  newx->typmod = -1;
2259  newx->location = x->location;
2260 
2261  /*
2262  * gram.y built the named args as a list of ResTarget. Transform each,
2263  * and break the names out as a separate list.
2264  */
2265  newx->named_args = NIL;
2266  newx->arg_names = NIL;
2267 
2268  foreach(lc, x->named_args)
2269  {
2270  ResTarget *r = lfirst_node(ResTarget, lc);
2271  Node *expr;
2272  char *argname;
2273 
2274  expr = transformExprRecurse(pstate, r->val);
2275 
2276  if (r->name)
2277  argname = map_sql_identifier_to_xml_name(r->name, false, false);
2278  else if (IsA(r->val, ColumnRef))
2280  true, false);
2281  else
2282  {
2283  ereport(ERROR,
2284  (errcode(ERRCODE_SYNTAX_ERROR),
2285  x->op == IS_XMLELEMENT
2286  ? errmsg("unnamed XML attribute value must be a column reference")
2287  : errmsg("unnamed XML element value must be a column reference"),
2288  parser_errposition(pstate, r->location)));
2289  argname = NULL; /* keep compiler quiet */
2290  }
2291 
2292  /* reject duplicate argnames in XMLELEMENT only */
2293  if (x->op == IS_XMLELEMENT)
2294  {
2295  ListCell *lc2;
2296 
2297  foreach(lc2, newx->arg_names)
2298  {
2299  if (strcmp(argname, strVal(lfirst(lc2))) == 0)
2300  ereport(ERROR,
2301  (errcode(ERRCODE_SYNTAX_ERROR),
2302  errmsg("XML attribute name \"%s\" appears more than once",
2303  argname),
2304  parser_errposition(pstate, r->location)));
2305  }
2306  }
2307 
2308  newx->named_args = lappend(newx->named_args, expr);
2309  newx->arg_names = lappend(newx->arg_names, makeString(argname));
2310  }
2311 
2312  /* The other arguments are of varying types depending on the function */
2313  newx->args = NIL;
2314  i = 0;
2315  foreach(lc, x->args)
2316  {
2317  Node *e = (Node *) lfirst(lc);
2318  Node *newe;
2319 
2320  newe = transformExprRecurse(pstate, e);
2321  switch (x->op)
2322  {
2323  case IS_XMLCONCAT:
2324  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2325  "XMLCONCAT");
2326  break;
2327  case IS_XMLELEMENT:
2328  /* no coercion necessary */
2329  break;
2330  case IS_XMLFOREST:
2331  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2332  "XMLFOREST");
2333  break;
2334  case IS_XMLPARSE:
2335  if (i == 0)
2336  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2337  "XMLPARSE");
2338  else
2339  newe = coerce_to_boolean(pstate, newe, "XMLPARSE");
2340  break;
2341  case IS_XMLPI:
2342  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2343  "XMLPI");
2344  break;
2345  case IS_XMLROOT:
2346  if (i == 0)
2347  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2348  "XMLROOT");
2349  else if (i == 1)
2350  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2351  "XMLROOT");
2352  else
2353  newe = coerce_to_specific_type(pstate, newe, INT4OID,
2354  "XMLROOT");
2355  break;
2356  case IS_XMLSERIALIZE:
2357  /* not handled here */
2358  Assert(false);
2359  break;
2360  case IS_DOCUMENT:
2361  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2362  "IS DOCUMENT");
2363  break;
2364  }
2365  newx->args = lappend(newx->args, newe);
2366  i++;
2367  }
2368 
2369  return (Node *) newx;
2370 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
Node * val
Definition: parsenodes.h:443
char * name
Definition: primnodes.h:1206
char * name
Definition: parsenodes.h:441
int location
Definition: primnodes.h:1213
Oid type
Definition: primnodes.h:1211
Definition: nodes.h:533
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:694
List * arg_names
Definition: primnodes.h:1208
int location
Definition: parsenodes.h:444
#define ERROR
Definition: elog.h:45
char * FigureColname(Node *node)
#define lfirst_node(type, lc)
Definition: pg_list.h:172
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2036
List * lappend(List *list, void *datum)
Definition: list.c:336
XmlExprOp op
Definition: primnodes.h:1205
int32 typmod
Definition: primnodes.h:1212
List * named_args
Definition: primnodes.h:1207
#define ereport(elevel,...)
Definition: elog.h:155
List * args
Definition: primnodes.h:1209
#define makeNode(_type_)
Definition: nodes.h:581
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
XmlOptionType xmloption
Definition: primnodes.h:1210
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:905
int i
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformXmlSerialize()

static Node * transformXmlSerialize ( ParseState pstate,
XmlSerialize xs 
)
static

Definition at line 2373 of file parse_expr.c.

References XmlExpr::args, COERCE_IMPLICIT_CAST, coerce_to_specific_type(), coerce_to_target_type(), COERCION_IMPLICIT, ereport, errcode(), errmsg(), ERROR, XmlSerialize::expr, format_type_be(), IS_XMLSERIALIZE, list_make1, XmlSerialize::location, XmlExpr::location, makeNode, XmlExpr::op, parser_errposition(), transformExprRecurse(), XmlExpr::type, XmlSerialize::typeName, typenameTypeIdAndMod(), XmlExpr::typmod, XmlSerialize::xmloption, and XmlExpr::xmloption.

Referenced by transformExprRecurse().

2374 {
2375  Node *result;
2376  XmlExpr *xexpr;
2377  Oid targetType;
2378  int32 targetTypmod;
2379 
2380  xexpr = makeNode(XmlExpr);
2381  xexpr->op = IS_XMLSERIALIZE;
2382  xexpr->args = list_make1(coerce_to_specific_type(pstate,
2383  transformExprRecurse(pstate, xs->expr),
2384  XMLOID,
2385  "XMLSERIALIZE"));
2386 
2387  typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
2388 
2389  xexpr->xmloption = xs->xmloption;
2390  xexpr->location = xs->location;
2391  /* We actually only need these to be able to parse back the expression. */
2392  xexpr->type = targetType;
2393  xexpr->typmod = targetTypmod;
2394 
2395  /*
2396  * The actual target type is determined this way. SQL allows char and
2397  * varchar as target types. We allow anything that can be cast implicitly
2398  * from text. This way, user-defined text-like data types automatically
2399  * fit in.
2400  */
2401  result = coerce_to_target_type(pstate, (Node *) xexpr,
2402  TEXTOID, targetType, targetTypmod,
2405  -1);
2406  if (result == NULL)
2407  ereport(ERROR,
2408  (errcode(ERRCODE_CANNOT_COERCE),
2409  errmsg("cannot cast XMLSERIALIZE result to %s",
2410  format_type_be(targetType)),
2411  parser_errposition(pstate, xexpr->location)));
2412  return result;
2413 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:112
int location
Definition: primnodes.h:1213
Oid type
Definition: primnodes.h:1211
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:694
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
TypeName * typeName
Definition: parsenodes.h:764
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:429
#define list_make1(x1)
Definition: pg_list.h:206
#define ERROR
Definition: elog.h:45
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
XmlOptionType xmloption
Definition: parsenodes.h:762
XmlExprOp op
Definition: primnodes.h:1205
int32 typmod
Definition: primnodes.h:1212
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310
#define ereport(elevel,...)
Definition: elog.h:155
List * args
Definition: primnodes.h:1209
#define makeNode(_type_)
Definition: nodes.h:581
XmlOptionType xmloption
Definition: primnodes.h:1210
int parser_errposition(ParseState *pstate, int location)