PostgreSQL Source Code  git master
parse_clause.h File Reference
Include dependency graph for parse_clause.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void transformFromClause (ParseState *pstate, List *frmList)
 
int setTargetTable (ParseState *pstate, RangeVar *relation, bool inh, bool alsoSource, AclMode requiredPerms)
 
NodetransformWhereClause (ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
 
NodetransformLimitClause (ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
 
ListtransformGroupClause (ParseState *pstate, List *grouplist, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
 
ListtransformSortClause (ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
 
ListtransformWindowDefinitions (ParseState *pstate, List *windowdefs, List **targetlist)
 
ListtransformDistinctClause (ParseState *pstate, List **targetlist, List *sortClause, bool is_agg)
 
ListtransformDistinctOnClause (ParseState *pstate, List *distinctlist, List **targetlist, List *sortClause)
 
void transformOnConflictArbiter (ParseState *pstate, OnConflictClause *onConflictClause, List **arbiterExpr, Node **arbiterWhere, Oid *constraint)
 
ListaddTargetToSortList (ParseState *pstate, TargetEntry *tle, List *sortlist, List *targetlist, SortBy *sortby)
 
Index assignSortGroupRef (TargetEntry *tle, List *tlist)
 
bool targetIsInSortList (TargetEntry *tle, Oid sortop, List *sortList)
 

Function Documentation

◆ addTargetToSortList()

List* addTargetToSortList ( ParseState pstate,
TargetEntry tle,
List sortlist,
List targetlist,
SortBy sortby 
)

Definition at line 3259 of file parse_clause.c.

References Assert, assignSortGroupRef(), cancel_parser_errposition_callback(), COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, compatible_oper_opid(), elog, SortGroupClause::eqop, ereport, errcode(), errhint(), errmsg(), ERROR, TargetEntry::expr, exprLocation(), exprType(), get_equality_op_for_ordering_op(), get_sort_group_operators(), SortGroupClause::hashable, InvalidOid, lappend(), llast, SortBy::location, makeNode, NIL, SortBy::node, SortGroupClause::nulls_first, OidIsValid, op_hashjoinable(), setup_parser_errposition_callback(), SORTBY_ASC, SORTBY_DEFAULT, SORTBY_DESC, SortBy::sortby_dir, SortBy::sortby_nulls, SORTBY_NULLS_DEFAULT, SORTBY_NULLS_FIRST, SORTBY_NULLS_LAST, SORTBY_USING, SortGroupClause::sortop, strVal, targetIsInSortList(), SortGroupClause::tleSortGroupRef, and SortBy::useOp.

Referenced by transformAggregateCall(), and transformSortClause().

3261 {
3262  Oid restype = exprType((Node *) tle->expr);
3263  Oid sortop;
3264  Oid eqop;
3265  bool hashable;
3266  bool reverse;
3267  int location;
3268  ParseCallbackState pcbstate;
3269 
3270  /* if tlist item is an UNKNOWN literal, change it to TEXT */
3271  if (restype == UNKNOWNOID)
3272  {
3273  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3274  restype, TEXTOID, -1,
3277  -1);
3278  restype = TEXTOID;
3279  }
3280 
3281  /*
3282  * Rather than clutter the API of get_sort_group_operators and the other
3283  * functions we're about to use, make use of error context callback to
3284  * mark any error reports with a parse position. We point to the operator
3285  * location if present, else to the expression being sorted. (NB: use the
3286  * original untransformed expression here; the TLE entry might well point
3287  * at a duplicate expression in the regular SELECT list.)
3288  */
3289  location = sortby->location;
3290  if (location < 0)
3291  location = exprLocation(sortby->node);
3292  setup_parser_errposition_callback(&pcbstate, pstate, location);
3293 
3294  /* determine the sortop, eqop, and directionality */
3295  switch (sortby->sortby_dir)
3296  {
3297  case SORTBY_DEFAULT:
3298  case SORTBY_ASC:
3299  get_sort_group_operators(restype,
3300  true, true, false,
3301  &sortop, &eqop, NULL,
3302  &hashable);
3303  reverse = false;
3304  break;
3305  case SORTBY_DESC:
3306  get_sort_group_operators(restype,
3307  false, true, true,
3308  NULL, &eqop, &sortop,
3309  &hashable);
3310  reverse = true;
3311  break;
3312  case SORTBY_USING:
3313  Assert(sortby->useOp != NIL);
3314  sortop = compatible_oper_opid(sortby->useOp,
3315  restype,
3316  restype,
3317  false);
3318 
3319  /*
3320  * Verify it's a valid ordering operator, fetch the corresponding
3321  * equality operator, and determine whether to consider it like
3322  * ASC or DESC.
3323  */
3324  eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3325  if (!OidIsValid(eqop))
3326  ereport(ERROR,
3327  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3328  errmsg("operator %s is not a valid ordering operator",
3329  strVal(llast(sortby->useOp))),
3330  errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3331 
3332  /*
3333  * Also see if the equality operator is hashable.
3334  */
3335  hashable = op_hashjoinable(eqop, restype);
3336  break;
3337  default:
3338  elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3339  sortop = InvalidOid; /* keep compiler quiet */
3340  eqop = InvalidOid;
3341  hashable = false;
3342  reverse = false;
3343  break;
3344  }
3345 
3347 
3348  /* avoid making duplicate sortlist entries */
3349  if (!targetIsInSortList(tle, sortop, sortlist))
3350  {
3352 
3353  sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3354 
3355  sortcl->eqop = eqop;
3356  sortcl->sortop = sortop;
3357  sortcl->hashable = hashable;
3358 
3359  switch (sortby->sortby_nulls)
3360  {
3361  case SORTBY_NULLS_DEFAULT:
3362  /* NULLS FIRST is default for DESC; other way for ASC */
3363  sortcl->nulls_first = reverse;
3364  break;
3365  case SORTBY_NULLS_FIRST:
3366  sortcl->nulls_first = true;
3367  break;
3368  case SORTBY_NULLS_LAST:
3369  sortcl->nulls_first = false;
3370  break;
3371  default:
3372  elog(ERROR, "unrecognized sortby_nulls: %d",
3373  sortby->sortby_nulls);
3374  break;
3375  }
3376 
3377  sortlist = lappend(sortlist, sortcl);
3378  }
3379 
3380  return sortlist;
3381 }
#define NIL
Definition: pg_list.h:65
Index assignSortGroupRef(TargetEntry *tle, List *tlist)
int errhint(const char *fmt,...)
Definition: elog.c:1069
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
SortByDir sortby_dir
Definition: parsenodes.h:470
List * useOp
Definition: parsenodes.h:472
Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse)
Definition: lsyscache.c:264
#define llast(l)
Definition: pg_list.h:215
Index tleSortGroupRef
Definition: parsenodes.h:1255
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:525
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:160
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1244
#define ERROR
Definition: elog.h:43
SortByNulls sortby_nulls
Definition: parsenodes.h:471
#define ereport(elevel, rest)
Definition: elog.h:141
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:144
List * lappend(List *list, void *datum)
Definition: list.c:322
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition: parse_oper.c:187
Expr * expr
Definition: primnodes.h:1407
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int errmsg(const char *fmt,...)
Definition: elog.c:822
Oid compatible_oper_opid(List *op, Oid arg1, Oid arg2, bool noError)
Definition: parse_oper.c:494
#define elog(elevel,...)
Definition: elog.h:228
Node * node
Definition: parsenodes.h:469
int location
Definition: parsenodes.h:473

◆ assignSortGroupRef()

Index assignSortGroupRef ( TargetEntry tle,
List tlist 
)

Definition at line 3457 of file parse_clause.c.

References lfirst, and TargetEntry::ressortgroupref.

Referenced by addTargetToGroupList(), addTargetToSortList(), build_minmax_path(), create_unique_plan(), and transformDistinctOnClause().

3458 {
3459  Index maxRef;
3460  ListCell *l;
3461 
3462  if (tle->ressortgroupref) /* already has one? */
3463  return tle->ressortgroupref;
3464 
3465  /* easiest way to pick an unused refnumber: max used + 1 */
3466  maxRef = 0;
3467  foreach(l, tlist)
3468  {
3469  Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3470 
3471  if (ref > maxRef)
3472  maxRef = ref;
3473  }
3474  tle->ressortgroupref = maxRef + 1;
3475  return tle->ressortgroupref;
3476 }
unsigned int Index
Definition: c.h:476
#define lfirst(lc)
Definition: pg_list.h:190
Index ressortgroupref
Definition: primnodes.h:1410

◆ setTargetTable()

int setTargetTable ( ParseState pstate,
RangeVar relation,
bool  inh,
bool  alsoSource,
AclMode  requiredPerms 
)

Definition at line 178 of file parse_clause.c.

References addNSItemToQuery(), addRangeTableEntryForRelation(), RangeVar::alias, ereport, errcode(), errmsg(), ERROR, NoLock, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, ParseState::p_target_nsitem, ParseState::p_target_relation, parserOpenTable(), RangeVar::relname, RangeTblEntry::requiredPerms, RowExclusiveLock, scanNameSpaceForENR(), RangeVar::schemaname, and table_close().

Referenced by transformDeleteStmt(), transformInsertStmt(), and transformUpdateStmt().

180 {
181  ParseNamespaceItem *nsitem;
182 
183  /*
184  * ENRs hide tables of the same name, so we need to check for them first.
185  * In contrast, CTEs don't hide tables (for this purpose).
186  */
187  if (relation->schemaname == NULL &&
188  scanNameSpaceForENR(pstate, relation->relname))
189  ereport(ERROR,
190  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
191  errmsg("relation \"%s\" cannot be the target of a modifying statement",
192  relation->relname)));
193 
194  /* Close old target; this could only happen for multi-action rules */
195  if (pstate->p_target_relation != NULL)
197 
198  /*
199  * Open target rel and grab suitable lock (which we will hold till end of
200  * transaction).
201  *
202  * free_parsestate() will eventually do the corresponding table_close(),
203  * but *not* release the lock.
204  */
205  pstate->p_target_relation = parserOpenTable(pstate, relation,
207 
208  /*
209  * Now build an RTE and a ParseNamespaceItem.
210  */
211  nsitem = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
213  relation->alias, inh, false);
214 
215  /* remember the RTE/nsitem as being the query target */
216  pstate->p_target_nsitem = nsitem;
217 
218  /*
219  * Override addRangeTableEntry's default ACL_SELECT permissions check, and
220  * instead mark target table as requiring exactly the specified
221  * permissions.
222  *
223  * If we find an explicit reference to the rel later during parse
224  * analysis, we will add the ACL_SELECT bit back again; see
225  * markVarForSelectPriv and its callers.
226  */
227  nsitem->p_rte->requiredPerms = requiredPerms;
228 
229  /*
230  * If UPDATE/DELETE, add table to joinlist and namespace.
231  */
232  if (alsoSource)
233  addNSItemToQuery(pstate, nsitem, true, true, true);
234 
235  return nsitem->p_rtindex;
236 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
int errcode(int sqlerrcode)
Definition: elog.c:608
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:191
AclMode requiredPerms
Definition: parsenodes.h:1117
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
char * schemaname
Definition: primnodes.h:67
char * relname
Definition: primnodes.h:68
RangeTblEntry * p_rte
Definition: parse_node.h:257
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
bool scanNameSpaceForENR(ParseState *pstate, const char *refname)
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:822
Relation p_target_relation
Definition: parse_node.h:190
Alias * alias
Definition: primnodes.h:72

◆ targetIsInSortList()

bool targetIsInSortList ( TargetEntry tle,
Oid  sortop,
List sortList 
)

Definition at line 3498 of file parse_clause.c.

References get_commutator(), InvalidOid, lfirst, TargetEntry::ressortgroupref, SortGroupClause::sortop, and SortGroupClause::tleSortGroupRef.

Referenced by addTargetToGroupList(), addTargetToSortList(), check_output_expressions(), examine_simple_variable(), targetIsInAllPartitionLists(), transformDistinctOnClause(), and transformGroupClauseExpr().

3499 {
3500  Index ref = tle->ressortgroupref;
3501  ListCell *l;
3502 
3503  /* no need to scan list if tle has no marker */
3504  if (ref == 0)
3505  return false;
3506 
3507  foreach(l, sortList)
3508  {
3509  SortGroupClause *scl = (SortGroupClause *) lfirst(l);
3510 
3511  if (scl->tleSortGroupRef == ref &&
3512  (sortop == InvalidOid ||
3513  sortop == scl->sortop ||
3514  sortop == get_commutator(scl->sortop)))
3515  return true;
3516  }
3517  return false;
3518 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1311
Index tleSortGroupRef
Definition: parsenodes.h:1255
unsigned int Index
Definition: c.h:476
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:190
Index ressortgroupref
Definition: primnodes.h:1410

◆ transformDistinctClause()

List* transformDistinctClause ( ParseState pstate,
List **  targetlist,
List sortClause,
bool  is_agg 
)

Definition at line 2839 of file parse_clause.c.

References addTargetToGroupList(), copyObject, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprLocation(), get_sortgroupclause_tle(), lappend(), lfirst, NIL, parser_errposition(), and TargetEntry::resjunk.

Referenced by transformAggregateCall(), and transformSelectStmt().

2841 {
2842  List *result = NIL;
2843  ListCell *slitem;
2844  ListCell *tlitem;
2845 
2846  /*
2847  * The distinctClause should consist of all ORDER BY items followed by all
2848  * other non-resjunk targetlist items. There must not be any resjunk
2849  * ORDER BY items --- that would imply that we are sorting by a value that
2850  * isn't necessarily unique within a DISTINCT group, so the results
2851  * wouldn't be well-defined. This construction ensures we follow the rule
2852  * that sortClause and distinctClause match; in fact the sortClause will
2853  * always be a prefix of distinctClause.
2854  *
2855  * Note a corner case: the same TLE could be in the ORDER BY list multiple
2856  * times with different sortops. We have to include it in the
2857  * distinctClause the same way to preserve the prefix property. The net
2858  * effect will be that the TLE value will be made unique according to both
2859  * sortops.
2860  */
2861  foreach(slitem, sortClause)
2862  {
2863  SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
2864  TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
2865 
2866  if (tle->resjunk)
2867  ereport(ERROR,
2868  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2869  is_agg ?
2870  errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
2871  errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
2872  parser_errposition(pstate,
2873  exprLocation((Node *) tle->expr))));
2874  result = lappend(result, copyObject(scl));
2875  }
2876 
2877  /*
2878  * Now add any remaining non-resjunk tlist items, using default sort/group
2879  * semantics for their data types.
2880  */
2881  foreach(tlitem, *targetlist)
2882  {
2883  TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
2884 
2885  if (tle->resjunk)
2886  continue; /* ignore junk */
2887  result = addTargetToGroupList(pstate, tle,
2888  result, *targetlist,
2889  exprLocation((Node *) tle->expr));
2890  }
2891 
2892  /*
2893  * Complain if we found nothing to make DISTINCT. Returning an empty list
2894  * would cause the parsed Query to look like it didn't have DISTINCT, with
2895  * results that would probably surprise the user. Note: this case is
2896  * presently impossible for aggregates because of grammar restrictions,
2897  * but we check anyway.
2898  */
2899  if (result == NIL)
2900  ereport(ERROR,
2901  (errcode(ERRCODE_SYNTAX_ERROR),
2902  is_agg ?
2903  errmsg("an aggregate with DISTINCT must have at least one argument") :
2904  errmsg("SELECT DISTINCT must have at least one column")));
2905 
2906  return result;
2907 }
#define NIL
Definition: pg_list.h:65
static List * addTargetToGroupList(ParseState *pstate, TargetEntry *tle, List *grouplist, List *targetlist, int location)
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:389
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
bool resjunk
Definition: primnodes.h:1414
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:322
#define lfirst(lc)
Definition: pg_list.h:190
Expr * expr
Definition: primnodes.h:1407
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define copyObject(obj)
Definition: nodes.h:641
Definition: pg_list.h:50

◆ transformDistinctOnClause()

List* transformDistinctOnClause ( ParseState pstate,
List distinctlist,
List **  targetlist,
List sortClause 
)

Definition at line 2923 of file parse_clause.c.

References addTargetToGroupList(), Assert, assignSortGroupRef(), copyObject, ereport, errcode(), errmsg(), ERROR, EXPR_KIND_DISTINCT_ON, exprLocation(), findTargetlistEntrySQL92(), forboth, get_matching_location(), get_sortgroupref_tle(), InvalidOid, lappend(), lappend_int(), lfirst, lfirst_int, list_member_int(), NIL, parser_errposition(), targetIsInSortList(), and SortGroupClause::tleSortGroupRef.

Referenced by transformSelectStmt().

2925 {
2926  List *result = NIL;
2927  List *sortgrouprefs = NIL;
2928  bool skipped_sortitem;
2929  ListCell *lc;
2930  ListCell *lc2;
2931 
2932  /*
2933  * Add all the DISTINCT ON expressions to the tlist (if not already
2934  * present, they are added as resjunk items). Assign sortgroupref numbers
2935  * to them, and make a list of these numbers. (NB: we rely below on the
2936  * sortgrouprefs list being one-for-one with the original distinctlist.
2937  * Also notice that we could have duplicate DISTINCT ON expressions and
2938  * hence duplicate entries in sortgrouprefs.)
2939  */
2940  foreach(lc, distinctlist)
2941  {
2942  Node *dexpr = (Node *) lfirst(lc);
2943  int sortgroupref;
2944  TargetEntry *tle;
2945 
2946  tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
2948  sortgroupref = assignSortGroupRef(tle, *targetlist);
2949  sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
2950  }
2951 
2952  /*
2953  * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
2954  * semantics from ORDER BY items that match DISTINCT ON items, and also
2955  * adopt their column sort order. We insist that the distinctClause and
2956  * sortClause match, so throw error if we find the need to add any more
2957  * distinctClause items after we've skipped an ORDER BY item that wasn't
2958  * in DISTINCT ON.
2959  */
2960  skipped_sortitem = false;
2961  foreach(lc, sortClause)
2962  {
2963  SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
2964 
2965  if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
2966  {
2967  if (skipped_sortitem)
2968  ereport(ERROR,
2969  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2970  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2971  parser_errposition(pstate,
2973  sortgrouprefs,
2974  distinctlist))));
2975  else
2976  result = lappend(result, copyObject(scl));
2977  }
2978  else
2979  skipped_sortitem = true;
2980  }
2981 
2982  /*
2983  * Now add any remaining DISTINCT ON items, using default sort/group
2984  * semantics for their data types. (Note: this is pretty questionable; if
2985  * the ORDER BY list doesn't include all the DISTINCT ON items and more
2986  * besides, you certainly aren't using DISTINCT ON in the intended way,
2987  * and you probably aren't going to get consistent results. It might be
2988  * better to throw an error or warning here. But historically we've
2989  * allowed it, so keep doing so.)
2990  */
2991  forboth(lc, distinctlist, lc2, sortgrouprefs)
2992  {
2993  Node *dexpr = (Node *) lfirst(lc);
2994  int sortgroupref = lfirst_int(lc2);
2995  TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
2996 
2997  if (targetIsInSortList(tle, InvalidOid, result))
2998  continue; /* already in list (with some semantics) */
2999  if (skipped_sortitem)
3000  ereport(ERROR,
3001  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3002  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
3003  parser_errposition(pstate, exprLocation(dexpr))));
3004  result = addTargetToGroupList(pstate, tle,
3005  result, *targetlist,
3006  exprLocation(dexpr));
3007  }
3008 
3009  /*
3010  * An empty result list is impossible here because of grammar
3011  * restrictions.
3012  */
3013  Assert(result != NIL);
3014 
3015  return result;
3016 }
#define NIL
Definition: pg_list.h:65
static List * addTargetToGroupList(ParseState *pstate, TargetEntry *tle, List *grouplist, List *targetlist, int location)
Index assignSortGroupRef(TargetEntry *tle, List *tlist)
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
Index tleSortGroupRef
Definition: parsenodes.h:1255
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
static int get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:191
bool list_member_int(const List *list, int datum)
Definition: list.c:655
#define ereport(elevel, rest)
Definition: elog.h:141
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:367
List * lappend_int(List *list, int datum)
Definition: list.c:340
List * lappend(List *list, void *datum)
Definition: list.c:322
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
static TargetEntry * findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define copyObject(obj)
Definition: nodes.h:641
Definition: pg_list.h:50

◆ transformFromClause()

void transformFromClause ( ParseState pstate,
List frmList 
)

Definition at line 114 of file parse_clause.c.

References checkNameSpaceConflicts(), lappend(), lfirst, list_concat(), ParseState::p_joinlist, ParseState::p_namespace, setNamespaceLateralState(), and transformFromClauseItem().

Referenced by transformDeleteStmt(), transformSelectStmt(), and transformUpdateStmt().

115 {
116  ListCell *fl;
117 
118  /*
119  * The grammar will have produced a list of RangeVars, RangeSubselects,
120  * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
121  * entries to the rtable), check for duplicate refnames, and then add it
122  * to the joinlist and namespace.
123  *
124  * Note we must process the items left-to-right for proper handling of
125  * LATERAL references.
126  */
127  foreach(fl, frmList)
128  {
129  Node *n = lfirst(fl);
130  ParseNamespaceItem *nsitem;
131  List *namespace;
132 
133  n = transformFromClauseItem(pstate, n,
134  &nsitem,
135  &namespace);
136 
137  checkNameSpaceConflicts(pstate, pstate->p_namespace, namespace);
138 
139  /* Mark the new namespace items as visible only to LATERAL */
140  setNamespaceLateralState(namespace, true, true);
141 
142  pstate->p_joinlist = lappend(pstate->p_joinlist, n);
143  pstate->p_namespace = list_concat(pstate->p_namespace, namespace);
144  }
145 
146  /*
147  * We're done parsing the FROM list, so make all namespace items
148  * unconditionally visible. Note that this will also reset lateral_only
149  * for any namespace items that were already present when we were called;
150  * but those should have been that way already.
151  */
152  setNamespaceLateralState(pstate->p_namespace, false, true);
153 }
Definition: nodes.h:525
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
static Node * transformFromClauseItem(ParseState *pstate, Node *n, ParseNamespaceItem **top_nsitem, List **namespace)
List * p_namespace
Definition: parse_node.h:184
static void setNamespaceLateralState(List *namespace, bool lateral_only, bool lateral_ok)
void checkNameSpaceConflicts(ParseState *pstate, List *namespace1, List *namespace2)
List * lappend(List *list, void *datum)
Definition: list.c:322
#define lfirst(lc)
Definition: pg_list.h:190
List * p_joinlist
Definition: parse_node.h:182
Definition: pg_list.h:50

◆ transformGroupClause()

List* transformGroupClause ( ParseState pstate,
List grouplist,
List **  groupingSets,
List **  targetlist,
List sortClause,
ParseExprKind  exprKind,
bool  useSQL99 
)

Definition at line 2486 of file parse_clause.c.

References Assert, bms_add_member(), exprLocation(), flatten_grouping_sets(), GROUPING_SET_CUBE, GROUPING_SET_EMPTY, GROUPING_SET_ROLLUP, GROUPING_SET_SETS, GROUPING_SET_SIMPLE, IsA, GroupingSet::kind, lappend(), lfirst, list_make1, list_make1_int, makeGroupingSet(), NIL, transformGroupClauseExpr(), and transformGroupingSet().

Referenced by transformSelectStmt(), and transformWindowDefinitions().

2489 {
2490  List *result = NIL;
2491  List *flat_grouplist;
2492  List *gsets = NIL;
2493  ListCell *gl;
2494  bool hasGroupingSets = false;
2495  Bitmapset *seen_local = NULL;
2496 
2497  /*
2498  * Recursively flatten implicit RowExprs. (Technically this is only needed
2499  * for GROUP BY, per the syntax rules for grouping sets, but we do it
2500  * anyway.)
2501  */
2502  flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
2503  true,
2504  &hasGroupingSets);
2505 
2506  /*
2507  * If the list is now empty, but hasGroupingSets is true, it's because we
2508  * elided redundant empty grouping sets. Restore a single empty grouping
2509  * set to leave a canonical form: GROUP BY ()
2510  */
2511 
2512  if (flat_grouplist == NIL && hasGroupingSets)
2513  {
2515  NIL,
2516  exprLocation((Node *) grouplist)));
2517  }
2518 
2519  foreach(gl, flat_grouplist)
2520  {
2521  Node *gexpr = (Node *) lfirst(gl);
2522 
2523  if (IsA(gexpr, GroupingSet))
2524  {
2525  GroupingSet *gset = (GroupingSet *) gexpr;
2526 
2527  switch (gset->kind)
2528  {
2529  case GROUPING_SET_EMPTY:
2530  gsets = lappend(gsets, gset);
2531  break;
2532  case GROUPING_SET_SIMPLE:
2533  /* can't happen */
2534  Assert(false);
2535  break;
2536  case GROUPING_SET_SETS:
2537  case GROUPING_SET_CUBE:
2538  case GROUPING_SET_ROLLUP:
2539  gsets = lappend(gsets,
2540  transformGroupingSet(&result,
2541  pstate, gset,
2542  targetlist, sortClause,
2543  exprKind, useSQL99, true));
2544  break;
2545  }
2546  }
2547  else
2548  {
2549  Index ref = transformGroupClauseExpr(&result, seen_local,
2550  pstate, gexpr,
2551  targetlist, sortClause,
2552  exprKind, useSQL99, true);
2553 
2554  if (ref > 0)
2555  {
2556  seen_local = bms_add_member(seen_local, ref);
2557  if (hasGroupingSets)
2558  gsets = lappend(gsets,
2560  list_make1_int(ref),
2561  exprLocation(gexpr)));
2562  }
2563  }
2564  }
2565 
2566  /* parser should prevent this */
2567  Assert(gsets == NIL || groupingSets != NULL);
2568 
2569  if (groupingSets)
2570  *groupingSets = gsets;
2571 
2572  return result;
2573 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
Definition: nodes.h:525
static Node * transformGroupingSet(List **flatresult, ParseState *pstate, GroupingSet *gset, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
GroupingSetKind kind
Definition: parsenodes.h:1324
#define list_make1(x1)
Definition: pg_list.h:227
static Node * flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
#define list_make1_int(x1)
Definition: pg_list.h:238
List * lappend(List *list, void *datum)
Definition: list.c:322
unsigned int Index
Definition: c.h:476
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
GroupingSet * makeGroupingSet(GroupingSetKind kind, List *content, int location)
Definition: makefuncs.c:788
Definition: pg_list.h:50
static Index transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local, ParseState *pstate, Node *gexpr, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)

◆ transformLimitClause()

Node* transformLimitClause ( ParseState pstate,
Node clause,
ParseExprKind  exprKind,
const char *  constructName 
)

Definition at line 1747 of file parse_clause.c.

References checkExprIsVarFree(), coerce_to_specific_type(), and transformExpr().

Referenced by transformSelectStmt(), transformSetOperationStmt(), and transformValuesClause().

1749 {
1750  Node *qual;
1751 
1752  if (clause == NULL)
1753  return NULL;
1754 
1755  qual = transformExpr(pstate, clause, exprKind);
1756 
1757  qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1758 
1759  /* LIMIT can't refer to any variables of the current query */
1760  checkExprIsVarFree(pstate, qual, constructName);
1761 
1762  return qual;
1763 }
static void checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:145
Definition: nodes.h:525
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)

◆ transformOnConflictArbiter()

void transformOnConflictArbiter ( ParseState pstate,
OnConflictClause onConflictClause,
List **  arbiterExpr,
Node **  arbiterWhere,
Oid constraint 
)

Definition at line 3151 of file parse_clause.c.

References ACL_SELECT, OnConflictClause::action, addNSItemToQuery(), bms_add_members(), InferClause::conname, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_INDEX_PREDICATE, exprLocation(), get_relation_constraint_attnos(), InferClause::indexElems, OnConflictClause::infer, InvalidOid, IsCatalogRelation(), NIL, ONCONFLICT_UPDATE, ParseState::p_namespace, ParseNamespaceItem::p_rte, ParseState::p_target_nsitem, ParseState::p_target_relation, parser_errposition(), RelationGetRelationName, RelationGetRelid, RelationIsUsedAsCatalogTable, RangeTblEntry::requiredPerms, resolve_unique_index_expr(), RangeTblEntry::selectedCols, transformExpr(), and InferClause::whereClause.

Referenced by transformOnConflictClause().

3155 {
3156  InferClause *infer = onConflictClause->infer;
3157 
3158  *arbiterExpr = NIL;
3159  *arbiterWhere = NULL;
3160  *constraint = InvalidOid;
3161 
3162  if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
3163  ereport(ERROR,
3164  (errcode(ERRCODE_SYNTAX_ERROR),
3165  errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
3166  errhint("For example, ON CONFLICT (column_name)."),
3167  parser_errposition(pstate,
3168  exprLocation((Node *) onConflictClause))));
3169 
3170  /*
3171  * To simplify certain aspects of its design, speculative insertion into
3172  * system catalogs is disallowed
3173  */
3174  if (IsCatalogRelation(pstate->p_target_relation))
3175  ereport(ERROR,
3176  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3177  errmsg("ON CONFLICT is not supported with system catalog tables"),
3178  parser_errposition(pstate,
3179  exprLocation((Node *) onConflictClause))));
3180 
3181  /* Same applies to table used by logical decoding as catalog table */
3183  ereport(ERROR,
3184  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3185  errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
3187  parser_errposition(pstate,
3188  exprLocation((Node *) onConflictClause))));
3189 
3190  /* ON CONFLICT DO NOTHING does not require an inference clause */
3191  if (infer)
3192  {
3193  List *save_namespace;
3194 
3195  /*
3196  * While we process the arbiter expressions, accept only non-qualified
3197  * references to the target table. Hide any other relations.
3198  */
3199  save_namespace = pstate->p_namespace;
3200  pstate->p_namespace = NIL;
3201  addNSItemToQuery(pstate, pstate->p_target_nsitem,
3202  false, false, true);
3203 
3204  if (infer->indexElems)
3205  *arbiterExpr = resolve_unique_index_expr(pstate, infer,
3206  pstate->p_target_relation);
3207 
3208  /*
3209  * Handling inference WHERE clause (for partial unique index
3210  * inference)
3211  */
3212  if (infer->whereClause)
3213  *arbiterWhere = transformExpr(pstate, infer->whereClause,
3215 
3216  pstate->p_namespace = save_namespace;
3217 
3218  /*
3219  * If the arbiter is specified by constraint name, get the constraint
3220  * OID and mark the constrained columns as requiring SELECT privilege,
3221  * in the same way as would have happened if the arbiter had been
3222  * specified by explicit reference to the constraint's index columns.
3223  */
3224  if (infer->conname)
3225  {
3226  Oid relid = RelationGetRelid(pstate->p_target_relation);
3227  RangeTblEntry *rte = pstate->p_target_nsitem->p_rte;
3228  Bitmapset *conattnos;
3229 
3230  conattnos = get_relation_constraint_attnos(relid, infer->conname,
3231  false, constraint);
3232 
3233  /* Make sure the rel as a whole is marked for SELECT access */
3234  rte->requiredPerms |= ACL_SELECT;
3235  /* Mark the constrained columns as requiring SELECT access */
3236  rte->selectedCols = bms_add_members(rte->selectedCols, conattnos);
3237  }
3238  }
3239 
3240  /*
3241  * It's convenient to form a list of expressions based on the
3242  * representation used by CREATE INDEX, since the same restrictions are
3243  * appropriate (e.g. on subqueries). However, from here on, a dedicated
3244  * primnode representation is used for inference elements, and so
3245  * assign_query_collations() can be trusted to do the right thing with the
3246  * post parse analysis query tree inference clause representation.
3247  */
3248 }
#define NIL
Definition: pg_list.h:65
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:99
int errhint(const char *fmt,...)
Definition: elog.c:1069
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:145
List * indexElems
Definition: parsenodes.h:1410
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:191
AclMode requiredPerms
Definition: parsenodes.h:1117
unsigned int Oid
Definition: postgres_ext.h:31
RangeTblEntry * p_rte
Definition: parse_node.h:257
Bitmapset * selectedCols
Definition: parsenodes.h:1119
#define ERROR
Definition: elog.h:43
List * p_namespace
Definition: parse_node.h:184
char * conname
Definition: parsenodes.h:1412
Bitmapset * get_relation_constraint_attnos(Oid relid, const char *conname, bool missing_ok, Oid *constraintOid)
#define RelationGetRelationName(relation)
Definition: rel.h:462
#define ereport(elevel, rest)
Definition: elog.h:141
#define ACL_SELECT
Definition: parsenodes.h:75
InferClause * infer
Definition: parsenodes.h:1426
#define RelationIsUsedAsCatalogTable(relation)
Definition: rel.h:320
#define InvalidOid
Definition: postgres_ext.h:36
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Node * whereClause
Definition: parsenodes.h:1411
int errmsg(const char *fmt,...)
Definition: elog.c:822
Relation p_target_relation
Definition: parse_node.h:190
OnConflictAction action
Definition: parsenodes.h:1425
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:428
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
static List * resolve_unique_index_expr(ParseState *pstate, InferClause *infer, Relation heapRel)

◆ transformSortClause()

List* transformSortClause ( ParseState pstate,
List orderlist,
List **  targetlist,
ParseExprKind  exprKind,
bool  useSQL99 
)

Definition at line 2586 of file parse_clause.c.

References addTargetToSortList(), findTargetlistEntrySQL92(), findTargetlistEntrySQL99(), lfirst, NIL, and SortBy::node.

Referenced by transformAggregateCall(), transformSelectStmt(), transformSetOperationStmt(), transformValuesClause(), and transformWindowDefinitions().

2591 {
2592  List *sortlist = NIL;
2593  ListCell *olitem;
2594 
2595  foreach(olitem, orderlist)
2596  {
2597  SortBy *sortby = (SortBy *) lfirst(olitem);
2598  TargetEntry *tle;
2599 
2600  if (useSQL99)
2601  tle = findTargetlistEntrySQL99(pstate, sortby->node,
2602  targetlist, exprKind);
2603  else
2604  tle = findTargetlistEntrySQL92(pstate, sortby->node,
2605  targetlist, exprKind);
2606 
2607  sortlist = addTargetToSortList(pstate, tle,
2608  sortlist, *targetlist, sortby);
2609  }
2610 
2611  return sortlist;
2612 }
#define NIL
Definition: pg_list.h:65
List * addTargetToSortList(ParseState *pstate, TargetEntry *tle, List *sortlist, List *targetlist, SortBy *sortby)
static TargetEntry * findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
#define lfirst(lc)
Definition: pg_list.h:190
static TargetEntry * findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
Node * node
Definition: parsenodes.h:469
Definition: pg_list.h:50

◆ transformWhereClause()

Node* transformWhereClause ( ParseState pstate,
Node clause,
ParseExprKind  exprKind,
const char *  constructName 
)

Definition at line 1720 of file parse_clause.c.

References coerce_to_boolean(), and transformExpr().

Referenced by AlterPolicy(), CreatePolicy(), CreateTrigger(), ParseFuncOrColumn(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformDeleteStmt(), transformIndexStmt(), transformJoinOnClause(), transformOnConflictClause(), transformRuleStmt(), transformSelectStmt(), and transformUpdateStmt().

1722 {
1723  Node *qual;
1724 
1725  if (clause == NULL)
1726  return NULL;
1727 
1728  qual = transformExpr(pstate, clause, exprKind);
1729 
1730  qual = coerce_to_boolean(pstate, qual, constructName);
1731 
1732  return qual;
1733 }
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:145
Definition: nodes.h:525
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

◆ transformWindowDefinitions()

List* transformWindowDefinitions ( ParseState pstate,
List windowdefs,
List **  targetlist 
)

Definition at line 2619 of file parse_clause.c.

References BTLessStrategyNumber, castNode, WindowClause::copiedOrder, copyObject, elog, WindowClause::endInRangeFunc, WindowDef::endOffset, WindowClause::endOffset, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, exprCollation(), findWindowClause(), FRAMEOPTION_DEFAULTS, FRAMEOPTION_END_OFFSET, FRAMEOPTION_GROUPS, FRAMEOPTION_RANGE, FRAMEOPTION_START_OFFSET, WindowDef::frameOptions, WindowClause::frameOptions, get_ordering_op_properties(), get_sortgroupclause_expr(), WindowClause::inRangeAsc, WindowClause::inRangeColl, WindowClause::inRangeNullsFirst, InvalidOid, lappend(), lfirst, linitial, list_length(), WindowDef::location, makeNode, WindowDef::name, WindowClause::name, NIL, SortGroupClause::nulls_first, WindowDef::orderClause, WindowClause::orderClause, parser_errposition(), WindowDef::partitionClause, WindowClause::partitionClause, WindowDef::refname, WindowClause::refname, SortGroupClause::sortop, WindowClause::startInRangeFunc, WindowDef::startOffset, WindowClause::startOffset, transformFrameOffset(), transformGroupClause(), transformSortClause(), and WindowClause::winref.

Referenced by transformSelectStmt().

2622 {
2623  List *result = NIL;
2624  Index winref = 0;
2625  ListCell *lc;
2626 
2627  foreach(lc, windowdefs)
2628  {
2629  WindowDef *windef = (WindowDef *) lfirst(lc);
2630  WindowClause *refwc = NULL;
2631  List *partitionClause;
2632  List *orderClause;
2633  Oid rangeopfamily = InvalidOid;
2634  Oid rangeopcintype = InvalidOid;
2635  WindowClause *wc;
2636 
2637  winref++;
2638 
2639  /*
2640  * Check for duplicate window names.
2641  */
2642  if (windef->name &&
2643  findWindowClause(result, windef->name) != NULL)
2644  ereport(ERROR,
2645  (errcode(ERRCODE_WINDOWING_ERROR),
2646  errmsg("window \"%s\" is already defined", windef->name),
2647  parser_errposition(pstate, windef->location)));
2648 
2649  /*
2650  * If it references a previous window, look that up.
2651  */
2652  if (windef->refname)
2653  {
2654  refwc = findWindowClause(result, windef->refname);
2655  if (refwc == NULL)
2656  ereport(ERROR,
2657  (errcode(ERRCODE_UNDEFINED_OBJECT),
2658  errmsg("window \"%s\" does not exist",
2659  windef->refname),
2660  parser_errposition(pstate, windef->location)));
2661  }
2662 
2663  /*
2664  * Transform PARTITION and ORDER specs, if any. These are treated
2665  * almost exactly like top-level GROUP BY and ORDER BY clauses,
2666  * including the special handling of nondefault operator semantics.
2667  */
2668  orderClause = transformSortClause(pstate,
2669  windef->orderClause,
2670  targetlist,
2672  true /* force SQL99 rules */ );
2673  partitionClause = transformGroupClause(pstate,
2674  windef->partitionClause,
2675  NULL,
2676  targetlist,
2677  orderClause,
2679  true /* force SQL99 rules */ );
2680 
2681  /*
2682  * And prepare the new WindowClause.
2683  */
2684  wc = makeNode(WindowClause);
2685  wc->name = windef->name;
2686  wc->refname = windef->refname;
2687 
2688  /*
2689  * Per spec, a windowdef that references a previous one copies the
2690  * previous partition clause (and mustn't specify its own). It can
2691  * specify its own ordering clause, but only if the previous one had
2692  * none. It always specifies its own frame clause, and the previous
2693  * one must not have a frame clause. Yeah, it's bizarre that each of
2694  * these cases works differently, but SQL:2008 says so; see 7.11
2695  * <window clause> syntax rule 10 and general rule 1. The frame
2696  * clause rule is especially bizarre because it makes "OVER foo"
2697  * different from "OVER (foo)", and requires the latter to throw an
2698  * error if foo has a nondefault frame clause. Well, ours not to
2699  * reason why, but we do go out of our way to throw a useful error
2700  * message for such cases.
2701  */
2702  if (refwc)
2703  {
2704  if (partitionClause)
2705  ereport(ERROR,
2706  (errcode(ERRCODE_WINDOWING_ERROR),
2707  errmsg("cannot override PARTITION BY clause of window \"%s\"",
2708  windef->refname),
2709  parser_errposition(pstate, windef->location)));
2711  }
2712  else
2713  wc->partitionClause = partitionClause;
2714  if (refwc)
2715  {
2716  if (orderClause && refwc->orderClause)
2717  ereport(ERROR,
2718  (errcode(ERRCODE_WINDOWING_ERROR),
2719  errmsg("cannot override ORDER BY clause of window \"%s\"",
2720  windef->refname),
2721  parser_errposition(pstate, windef->location)));
2722  if (orderClause)
2723  {
2724  wc->orderClause = orderClause;
2725  wc->copiedOrder = false;
2726  }
2727  else
2728  {
2729  wc->orderClause = copyObject(refwc->orderClause);
2730  wc->copiedOrder = true;
2731  }
2732  }
2733  else
2734  {
2735  wc->orderClause = orderClause;
2736  wc->copiedOrder = false;
2737  }
2738  if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
2739  {
2740  /*
2741  * Use this message if this is a WINDOW clause, or if it's an OVER
2742  * clause that includes ORDER BY or framing clauses. (We already
2743  * rejected PARTITION BY above, so no need to check that.)
2744  */
2745  if (windef->name ||
2746  orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
2747  ereport(ERROR,
2748  (errcode(ERRCODE_WINDOWING_ERROR),
2749  errmsg("cannot copy window \"%s\" because it has a frame clause",
2750  windef->refname),
2751  parser_errposition(pstate, windef->location)));
2752  /* Else this clause is just OVER (foo), so say this: */
2753  ereport(ERROR,
2754  (errcode(ERRCODE_WINDOWING_ERROR),
2755  errmsg("cannot copy window \"%s\" because it has a frame clause",
2756  windef->refname),
2757  errhint("Omit the parentheses in this OVER clause."),
2758  parser_errposition(pstate, windef->location)));
2759  }
2760  wc->frameOptions = windef->frameOptions;
2761 
2762  /*
2763  * RANGE offset PRECEDING/FOLLOWING requires exactly one ORDER BY
2764  * column; check that and get its sort opfamily info.
2765  */
2766  if ((wc->frameOptions & FRAMEOPTION_RANGE) &&
2769  {
2770  SortGroupClause *sortcl;
2771  Node *sortkey;
2772  int16 rangestrategy;
2773 
2774  if (list_length(wc->orderClause) != 1)
2775  ereport(ERROR,
2776  (errcode(ERRCODE_WINDOWING_ERROR),
2777  errmsg("RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column"),
2778  parser_errposition(pstate, windef->location)));
2779  sortcl = castNode(SortGroupClause, linitial(wc->orderClause));
2780  sortkey = get_sortgroupclause_expr(sortcl, *targetlist);
2781  /* Find the sort operator in pg_amop */
2782  if (!get_ordering_op_properties(sortcl->sortop,
2783  &rangeopfamily,
2784  &rangeopcintype,
2785  &rangestrategy))
2786  elog(ERROR, "operator %u is not a valid ordering operator",
2787  sortcl->sortop);
2788  /* Record properties of sort ordering */
2789  wc->inRangeColl = exprCollation(sortkey);
2790  wc->inRangeAsc = (rangestrategy == BTLessStrategyNumber);
2791  wc->inRangeNullsFirst = sortcl->nulls_first;
2792  }
2793 
2794  /* Per spec, GROUPS mode requires an ORDER BY clause */
2795  if (wc->frameOptions & FRAMEOPTION_GROUPS)
2796  {
2797  if (wc->orderClause == NIL)
2798  ereport(ERROR,
2799  (errcode(ERRCODE_WINDOWING_ERROR),
2800  errmsg("GROUPS mode requires an ORDER BY clause"),
2801  parser_errposition(pstate, windef->location)));
2802  }
2803 
2804  /* Process frame offset expressions */
2805  wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
2806  rangeopfamily, rangeopcintype,
2807  &wc->startInRangeFunc,
2808  windef->startOffset);
2809  wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
2810  rangeopfamily, rangeopcintype,
2811  &wc->endInRangeFunc,
2812  windef->endOffset);
2813  wc->winref = winref;
2814 
2815  result = lappend(result, wc);
2816  }
2817 
2818  return result;
2819 }
signed short int16
Definition: c.h:346
List * partitionClause
Definition: parsenodes.h:489
char * refname
Definition: parsenodes.h:1350
#define NIL
Definition: pg_list.h:65
bool copiedOrder
Definition: parsenodes.h:1362
int errhint(const char *fmt,...)
Definition: elog.c:1069
#define FRAMEOPTION_DEFAULTS
Definition: parsenodes.h:531
bool inRangeNullsFirst
Definition: parsenodes.h:1360
int frameOptions
Definition: parsenodes.h:491
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
char * name
Definition: parsenodes.h:487
Oid endInRangeFunc
Definition: parsenodes.h:1357
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:401
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define FRAMEOPTION_START_OFFSET
Definition: parsenodes.h:523
Node * startOffset
Definition: parsenodes.h:492
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
List * partitionClause
Definition: parsenodes.h:1351
int location
Definition: parsenodes.h:494
Node * endOffset
Definition: parsenodes.h:493
List * transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:322
Node * startOffset
Definition: parsenodes.h:1354
List * orderClause
Definition: parsenodes.h:490
unsigned int Index
Definition: c.h:476
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:573
#define FRAMEOPTION_RANGE
Definition: parsenodes.h:505
#define lfirst(lc)
Definition: pg_list.h:190
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
Definition: lsyscache.c:204
Node * endOffset
Definition: parsenodes.h:1355
static int list_length(const List *l)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
#define FRAMEOPTION_END_OFFSET
Definition: parsenodes.h:525
List * orderClause
Definition: parsenodes.h:1352
int errmsg(const char *fmt,...)
Definition: elog.c:822
static Node * transformFrameOffset(ParseState *pstate, int frameOptions, Oid rangeopfamily, Oid rangeopcintype, Oid *inRangeFunc, Node *clause)
#define elog(elevel,...)
Definition: elog.h:228
#define FRAMEOPTION_GROUPS
Definition: parsenodes.h:507
char * refname
Definition: parsenodes.h:488
#define copyObject(obj)
Definition: nodes.h:641
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:50
Oid startInRangeFunc
Definition: parsenodes.h:1356
static WindowClause * findWindowClause(List *wclist, const char *name)