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, LimitOption limitOption)
 
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 3262 of file parse_clause.c.

3264 {
3265  Oid restype = exprType((Node *) tle->expr);
3266  Oid sortop;
3267  Oid eqop;
3268  bool hashable;
3269  bool reverse;
3270  int location;
3271  ParseCallbackState pcbstate;
3272 
3273  /* if tlist item is an UNKNOWN literal, change it to TEXT */
3274  if (restype == UNKNOWNOID)
3275  {
3276  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3277  restype, TEXTOID, -1,
3280  -1);
3281  restype = TEXTOID;
3282  }
3283 
3284  /*
3285  * Rather than clutter the API of get_sort_group_operators and the other
3286  * functions we're about to use, make use of error context callback to
3287  * mark any error reports with a parse position. We point to the operator
3288  * location if present, else to the expression being sorted. (NB: use the
3289  * original untransformed expression here; the TLE entry might well point
3290  * at a duplicate expression in the regular SELECT list.)
3291  */
3292  location = sortby->location;
3293  if (location < 0)
3294  location = exprLocation(sortby->node);
3295  setup_parser_errposition_callback(&pcbstate, pstate, location);
3296 
3297  /* determine the sortop, eqop, and directionality */
3298  switch (sortby->sortby_dir)
3299  {
3300  case SORTBY_DEFAULT:
3301  case SORTBY_ASC:
3302  get_sort_group_operators(restype,
3303  true, true, false,
3304  &sortop, &eqop, NULL,
3305  &hashable);
3306  reverse = false;
3307  break;
3308  case SORTBY_DESC:
3309  get_sort_group_operators(restype,
3310  false, true, true,
3311  NULL, &eqop, &sortop,
3312  &hashable);
3313  reverse = true;
3314  break;
3315  case SORTBY_USING:
3316  Assert(sortby->useOp != NIL);
3317  sortop = compatible_oper_opid(sortby->useOp,
3318  restype,
3319  restype,
3320  false);
3321 
3322  /*
3323  * Verify it's a valid ordering operator, fetch the corresponding
3324  * equality operator, and determine whether to consider it like
3325  * ASC or DESC.
3326  */
3327  eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3328  if (!OidIsValid(eqop))
3329  ereport(ERROR,
3330  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3331  errmsg("operator %s is not a valid ordering operator",
3332  strVal(llast(sortby->useOp))),
3333  errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3334 
3335  /*
3336  * Also see if the equality operator is hashable.
3337  */
3338  hashable = op_hashjoinable(eqop, restype);
3339  break;
3340  default:
3341  elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3342  sortop = InvalidOid; /* keep compiler quiet */
3343  eqop = InvalidOid;
3344  hashable = false;
3345  reverse = false;
3346  break;
3347  }
3348 
3350 
3351  /* avoid making duplicate sortlist entries */
3352  if (!targetIsInSortList(tle, sortop, sortlist))
3353  {
3355 
3356  sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3357 
3358  sortcl->eqop = eqop;
3359  sortcl->sortop = sortop;
3360  sortcl->hashable = hashable;
3361 
3362  switch (sortby->sortby_nulls)
3363  {
3364  case SORTBY_NULLS_DEFAULT:
3365  /* NULLS FIRST is default for DESC; other way for ASC */
3366  sortcl->nulls_first = reverse;
3367  break;
3368  case SORTBY_NULLS_FIRST:
3369  sortcl->nulls_first = true;
3370  break;
3371  case SORTBY_NULLS_LAST:
3372  sortcl->nulls_first = false;
3373  break;
3374  default:
3375  elog(ERROR, "unrecognized sortby_nulls: %d",
3376  sortby->sortby_nulls);
3377  break;
3378  }
3379 
3380  sortlist = lappend(sortlist, sortcl);
3381  }
3382 
3383  return sortlist;
3384 }
#define OidIsValid(objectId)
Definition: c.h:759
int errhint(const char *fmt,...)
Definition: elog.c:1316
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse)
Definition: lsyscache.c:266
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1419
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1243
#define makeNode(_type_)
Definition: nodes.h:165
Index assignSortGroupRef(TargetEntry *tle, List *tlist)
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:157
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:160
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:144
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:192
Oid compatible_oper_opid(List *op, Oid arg1, Oid arg2, bool noError)
Definition: parse_oper.c:499
@ SORTBY_NULLS_DEFAULT
Definition: parsenodes.h:61
@ SORTBY_NULLS_LAST
Definition: parsenodes.h:63
@ SORTBY_NULLS_FIRST
Definition: parsenodes.h:62
@ SORTBY_USING
Definition: parsenodes.h:56
@ SORTBY_DESC
Definition: parsenodes.h:55
@ SORTBY_ASC
Definition: parsenodes.h:54
@ SORTBY_DEFAULT
Definition: parsenodes.h:53
#define llast(l)
Definition: pg_list.h:198
#define NIL
Definition: pg_list.h:68
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:628
@ COERCION_IMPLICIT
Definition: primnodes.h:606
Definition: nodes.h:118
SortByNulls sortby_nulls
Definition: parsenodes.h:539
Node * node
Definition: parsenodes.h:537
List * useOp
Definition: parsenodes.h:540
SortByDir sortby_dir
Definition: parsenodes.h:538
int location
Definition: parsenodes.h:541
Index tleSortGroupRef
Definition: parsenodes.h:1375
Expr * expr
Definition: primnodes.h:1681
#define strVal(v)
Definition: value.h:82

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

◆ assignSortGroupRef()

Index assignSortGroupRef ( TargetEntry tle,
List tlist 
)

Definition at line 3460 of file parse_clause.c.

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

References lfirst, and TargetEntry::ressortgroupref.

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

◆ setTargetTable()

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

Definition at line 179 of file parse_clause.c.

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

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

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

◆ targetIsInSortList()

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

Definition at line 3501 of file parse_clause.c.

3502 {
3503  Index ref = tle->ressortgroupref;
3504  ListCell *l;
3505 
3506  /* no need to scan list if tle has no marker */
3507  if (ref == 0)
3508  return false;
3509 
3510  foreach(l, sortList)
3511  {
3512  SortGroupClause *scl = (SortGroupClause *) lfirst(l);
3513 
3514  if (scl->tleSortGroupRef == ref &&
3515  (sortop == InvalidOid ||
3516  sortop == scl->sortop ||
3517  sortop == get_commutator(scl->sortop)))
3518  return true;
3519  }
3520  return false;
3521 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1491

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

◆ transformDistinctClause()

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

Definition at line 2854 of file parse_clause.c.

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

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

Referenced by transformAggregateCall(), transformPLAssignStmt(), and transformSelectStmt().

◆ transformDistinctOnClause()

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

Definition at line 2938 of file parse_clause.c.

2940 {
2941  List *result = NIL;
2942  List *sortgrouprefs = NIL;
2943  bool skipped_sortitem;
2944  ListCell *lc;
2945  ListCell *lc2;
2946 
2947  /*
2948  * Add all the DISTINCT ON expressions to the tlist (if not already
2949  * present, they are added as resjunk items). Assign sortgroupref numbers
2950  * to them, and make a list of these numbers. (NB: we rely below on the
2951  * sortgrouprefs list being one-for-one with the original distinctlist.
2952  * Also notice that we could have duplicate DISTINCT ON expressions and
2953  * hence duplicate entries in sortgrouprefs.)
2954  */
2955  foreach(lc, distinctlist)
2956  {
2957  Node *dexpr = (Node *) lfirst(lc);
2958  int sortgroupref;
2959  TargetEntry *tle;
2960 
2961  tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
2963  sortgroupref = assignSortGroupRef(tle, *targetlist);
2964  sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
2965  }
2966 
2967  /*
2968  * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
2969  * semantics from ORDER BY items that match DISTINCT ON items, and also
2970  * adopt their column sort order. We insist that the distinctClause and
2971  * sortClause match, so throw error if we find the need to add any more
2972  * distinctClause items after we've skipped an ORDER BY item that wasn't
2973  * in DISTINCT ON.
2974  */
2975  skipped_sortitem = false;
2976  foreach(lc, sortClause)
2977  {
2978  SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
2979 
2980  if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
2981  {
2982  if (skipped_sortitem)
2983  ereport(ERROR,
2984  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2985  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2986  parser_errposition(pstate,
2988  sortgrouprefs,
2989  distinctlist))));
2990  else
2991  result = lappend(result, copyObject(scl));
2992  }
2993  else
2994  skipped_sortitem = true;
2995  }
2996 
2997  /*
2998  * Now add any remaining DISTINCT ON items, using default sort/group
2999  * semantics for their data types. (Note: this is pretty questionable; if
3000  * the ORDER BY list doesn't include all the DISTINCT ON items and more
3001  * besides, you certainly aren't using DISTINCT ON in the intended way,
3002  * and you probably aren't going to get consistent results. It might be
3003  * better to throw an error or warning here. But historically we've
3004  * allowed it, so keep doing so.)
3005  */
3006  forboth(lc, distinctlist, lc2, sortgrouprefs)
3007  {
3008  Node *dexpr = (Node *) lfirst(lc);
3009  int sortgroupref = lfirst_int(lc2);
3010  TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
3011 
3012  if (targetIsInSortList(tle, InvalidOid, result))
3013  continue; /* already in list (with some semantics) */
3014  if (skipped_sortitem)
3015  ereport(ERROR,
3016  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3017  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
3018  parser_errposition(pstate, exprLocation(dexpr))));
3019  result = addTargetToGroupList(pstate, tle,
3020  result, *targetlist,
3021  exprLocation(dexpr));
3022  }
3023 
3024  /*
3025  * An empty result list is impossible here because of grammar
3026  * restrictions.
3027  */
3028  Assert(result != NIL);
3029 
3030  return result;
3031 }
List * lappend_int(List *list, int datum)
Definition: list.c:356
bool list_member_int(const List *list, int datum)
Definition: list.c:701
static int get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
static TargetEntry * findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
@ EXPR_KIND_DISTINCT_ON
Definition: parse_node.h:61
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:467
#define lfirst_int(lc)
Definition: pg_list.h:173
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:345

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 transformPLAssignStmt(), and transformSelectStmt().

◆ transformFromClause()

void transformFromClause ( ParseState pstate,
List frmList 
)

Definition at line 113 of file parse_clause.c.

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

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

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

◆ transformGroupClause()

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

Definition at line 2500 of file parse_clause.c.

2503 {
2504  List *result = NIL;
2505  List *flat_grouplist;
2506  List *gsets = NIL;
2507  ListCell *gl;
2508  bool hasGroupingSets = false;
2509  Bitmapset *seen_local = NULL;
2510 
2511  /*
2512  * Recursively flatten implicit RowExprs. (Technically this is only needed
2513  * for GROUP BY, per the syntax rules for grouping sets, but we do it
2514  * anyway.)
2515  */
2516  flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
2517  true,
2518  &hasGroupingSets);
2519 
2520  /*
2521  * If the list is now empty, but hasGroupingSets is true, it's because we
2522  * elided redundant empty grouping sets. Restore a single empty grouping
2523  * set to leave a canonical form: GROUP BY ()
2524  */
2525 
2526  if (flat_grouplist == NIL && hasGroupingSets)
2527  {
2529  NIL,
2530  exprLocation((Node *) grouplist)));
2531  }
2532 
2533  foreach(gl, flat_grouplist)
2534  {
2535  Node *gexpr = (Node *) lfirst(gl);
2536 
2537  if (IsA(gexpr, GroupingSet))
2538  {
2539  GroupingSet *gset = (GroupingSet *) gexpr;
2540 
2541  switch (gset->kind)
2542  {
2543  case GROUPING_SET_EMPTY:
2544  gsets = lappend(gsets, gset);
2545  break;
2546  case GROUPING_SET_SIMPLE:
2547  /* can't happen */
2548  Assert(false);
2549  break;
2550  case GROUPING_SET_SETS:
2551  case GROUPING_SET_CUBE:
2552  case GROUPING_SET_ROLLUP:
2553  gsets = lappend(gsets,
2554  transformGroupingSet(&result,
2555  pstate, gset,
2556  targetlist, sortClause,
2557  exprKind, useSQL99, true));
2558  break;
2559  }
2560  }
2561  else
2562  {
2563  Index ref = transformGroupClauseExpr(&result, seen_local,
2564  pstate, gexpr,
2565  targetlist, sortClause,
2566  exprKind, useSQL99, true);
2567 
2568  if (ref > 0)
2569  {
2570  seen_local = bms_add_member(seen_local, ref);
2571  if (hasGroupingSets)
2572  gsets = lappend(gsets,
2574  list_make1_int(ref),
2575  exprLocation(gexpr)));
2576  }
2577  }
2578  }
2579 
2580  /* parser should prevent this */
2581  Assert(gsets == NIL || groupingSets != NULL);
2582 
2583  if (groupingSets)
2584  *groupingSets = gsets;
2585 
2586  return result;
2587 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:739
GroupingSet * makeGroupingSet(GroupingSetKind kind, List *content, int location)
Definition: makefuncs.c:797
#define IsA(nodeptr, _type_)
Definition: nodes.h:168
static Node * transformGroupingSet(List **flatresult, ParseState *pstate, GroupingSet *gset, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
static Node * flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
static Index transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local, ParseState *pstate, Node *gexpr, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
@ GROUPING_SET_CUBE
Definition: parsenodes.h:1438
@ GROUPING_SET_SIMPLE
Definition: parsenodes.h:1436
@ GROUPING_SET_ROLLUP
Definition: parsenodes.h:1437
@ GROUPING_SET_SETS
Definition: parsenodes.h:1439
@ GROUPING_SET_EMPTY
Definition: parsenodes.h:1435
#define list_make1(x1)
Definition: pg_list.h:212
#define list_make1_int(x1)
Definition: pg_list.h:227
GroupingSetKind kind
Definition: parsenodes.h:1445

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 transformPLAssignStmt(), transformSelectStmt(), and transformWindowDefinitions().

◆ transformLimitClause()

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

Definition at line 1749 of file parse_clause.c.

1752 {
1753  Node *qual;
1754 
1755  if (clause == NULL)
1756  return NULL;
1757 
1758  qual = transformExpr(pstate, clause, exprKind);
1759 
1760  qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1761 
1762  /* LIMIT can't refer to any variables of the current query */
1763  checkExprIsVarFree(pstate, qual, constructName);
1764 
1765  /*
1766  * Don't allow NULLs in FETCH FIRST .. WITH TIES. This test is ugly and
1767  * extremely simplistic, in that you can pass a NULL anyway by hiding it
1768  * inside an expression -- but this protects ruleutils against emitting an
1769  * unadorned NULL that's not accepted back by the grammar.
1770  */
1771  if (exprKind == EXPR_KIND_LIMIT && limitOption == LIMIT_OPTION_WITH_TIES &&
1772  IsA(clause, A_Const) && castNode(A_Const, clause)->isnull)
1773  ereport(ERROR,
1774  (errcode(ERRCODE_INVALID_ROW_COUNT_IN_LIMIT_CLAUSE),
1775  errmsg("row count cannot be null in FETCH FIRST ... WITH TIES clause")));
1776 
1777  return qual;
1778 }
@ LIMIT_OPTION_WITH_TIES
Definition: nodes.h:429
#define castNode(_type_, nodeptr)
Definition: nodes.h:186
static void checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:92
@ EXPR_KIND_LIMIT
Definition: parse_node.h:62

References castNode, checkExprIsVarFree(), coerce_to_specific_type(), ereport, errcode(), errmsg(), ERROR, EXPR_KIND_LIMIT, IsA, LIMIT_OPTION_WITH_TIES, and transformExpr().

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

◆ transformOnConflictArbiter()

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

Definition at line 3166 of file parse_clause.c.

3170 {
3171  InferClause *infer = onConflictClause->infer;
3172 
3173  *arbiterExpr = NIL;
3174  *arbiterWhere = NULL;
3175  *constraint = InvalidOid;
3176 
3177  if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
3178  ereport(ERROR,
3179  (errcode(ERRCODE_SYNTAX_ERROR),
3180  errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
3181  errhint("For example, ON CONFLICT (column_name)."),
3182  parser_errposition(pstate,
3183  exprLocation((Node *) onConflictClause))));
3184 
3185  /*
3186  * To simplify certain aspects of its design, speculative insertion into
3187  * system catalogs is disallowed
3188  */
3189  if (IsCatalogRelation(pstate->p_target_relation))
3190  ereport(ERROR,
3191  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3192  errmsg("ON CONFLICT is not supported with system catalog tables"),
3193  parser_errposition(pstate,
3194  exprLocation((Node *) onConflictClause))));
3195 
3196  /* Same applies to table used by logical decoding as catalog table */
3198  ereport(ERROR,
3199  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3200  errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
3202  parser_errposition(pstate,
3203  exprLocation((Node *) onConflictClause))));
3204 
3205  /* ON CONFLICT DO NOTHING does not require an inference clause */
3206  if (infer)
3207  {
3208  if (infer->indexElems)
3209  *arbiterExpr = resolve_unique_index_expr(pstate, infer,
3210  pstate->p_target_relation);
3211 
3212  /*
3213  * Handling inference WHERE clause (for partial unique index
3214  * inference)
3215  */
3216  if (infer->whereClause)
3217  *arbiterWhere = transformExpr(pstate, infer->whereClause,
3219 
3220  /*
3221  * If the arbiter is specified by constraint name, get the constraint
3222  * OID and mark the constrained columns as requiring SELECT privilege,
3223  * in the same way as would have happened if the arbiter had been
3224  * specified by explicit reference to the constraint's index columns.
3225  */
3226  if (infer->conname)
3227  {
3228  Oid relid = RelationGetRelid(pstate->p_target_relation);
3229  RTEPermissionInfo *perminfo = pstate->p_target_nsitem->p_perminfo;
3230  Bitmapset *conattnos;
3231 
3232  conattnos = get_relation_constraint_attnos(relid, infer->conname,
3233  false, constraint);
3234 
3235  /* Make sure the rel as a whole is marked for SELECT access */
3236  perminfo->requiredPerms |= ACL_SELECT;
3237  /* Mark the constrained columns as requiring SELECT access */
3238  perminfo->selectedCols = bms_add_members(perminfo->selectedCols,
3239  conattnos);
3240  }
3241  }
3242 
3243  /*
3244  * It's convenient to form a list of expressions based on the
3245  * representation used by CREATE INDEX, since the same restrictions are
3246  * appropriate (e.g. on subqueries). However, from here on, a dedicated
3247  * primnode representation is used for inference elements, and so
3248  * assign_query_collations() can be trusted to do the right thing with the
3249  * post parse analysis query tree inference clause representation.
3250  */
3251 }
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:796
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:105
@ ONCONFLICT_UPDATE
Definition: nodes.h:417
static List * resolve_unique_index_expr(ParseState *pstate, InferClause *infer, Relation heapRel)
@ EXPR_KIND_INDEX_PREDICATE
Definition: parse_node.h:72
#define ACL_SELECT
Definition: parsenodes.h:84
Bitmapset * get_relation_constraint_attnos(Oid relid, const char *conname, bool missing_ok, Oid *constraintOid)
#define RelationGetRelid(relation)
Definition: rel.h:501
#define RelationIsUsedAsCatalogTable(relation)
Definition: rel.h:382
#define RelationGetRelationName(relation)
Definition: rel.h:535
char * conname
Definition: parsenodes.h:1544
List * indexElems
Definition: parsenodes.h:1542
Node * whereClause
Definition: parsenodes.h:1543
InferClause * infer
Definition: parsenodes.h:1558
OnConflictAction action
Definition: parsenodes.h:1557
Bitmapset * selectedCols
Definition: parsenodes.h:1232

References ACL_SELECT, OnConflictClause::action, 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, ParseNamespaceItem::p_perminfo, ParseState::p_target_nsitem, ParseState::p_target_relation, parser_errposition(), RelationGetRelationName, RelationGetRelid, RelationIsUsedAsCatalogTable, RTEPermissionInfo::requiredPerms, resolve_unique_index_expr(), RTEPermissionInfo::selectedCols, transformExpr(), and InferClause::whereClause.

Referenced by transformOnConflictClause().

◆ transformSortClause()

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

Definition at line 2600 of file parse_clause.c.

2605 {
2606  List *sortlist = NIL;
2607  ListCell *olitem;
2608 
2609  foreach(olitem, orderlist)
2610  {
2611  SortBy *sortby = (SortBy *) lfirst(olitem);
2612  TargetEntry *tle;
2613 
2614  if (useSQL99)
2615  tle = findTargetlistEntrySQL99(pstate, sortby->node,
2616  targetlist, exprKind);
2617  else
2618  tle = findTargetlistEntrySQL92(pstate, sortby->node,
2619  targetlist, exprKind);
2620 
2621  sortlist = addTargetToSortList(pstate, tle,
2622  sortlist, *targetlist, sortby);
2623  }
2624 
2625  return sortlist;
2626 }
List * addTargetToSortList(ParseState *pstate, TargetEntry *tle, List *sortlist, List *targetlist, SortBy *sortby)
static TargetEntry * findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)

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

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

◆ transformWhereClause()

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

Definition at line 1722 of file parse_clause.c.

1724 {
1725  Node *qual;
1726 
1727  if (clause == NULL)
1728  return NULL;
1729 
1730  qual = transformExpr(pstate, clause, exprKind);
1731 
1732  qual = coerce_to_boolean(pstate, qual, constructName);
1733 
1734  return qual;
1735 }
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

References coerce_to_boolean(), and transformExpr().

Referenced by AlterPolicy(), CreatePolicy(), CreateTriggerFiringOn(), ParseFuncOrColumn(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformDeleteStmt(), transformIndexStmt(), transformJoinOnClause(), transformMergeStmt(), transformOnConflictClause(), transformPLAssignStmt(), TransformPubWhereClauses(), transformRuleStmt(), transformSelectStmt(), and transformUpdateStmt().

◆ transformWindowDefinitions()

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

Definition at line 2633 of file parse_clause.c.

2636 {
2637  List *result = NIL;
2638  Index winref = 0;
2639  ListCell *lc;
2640 
2641  foreach(lc, windowdefs)
2642  {
2643  WindowDef *windef = (WindowDef *) lfirst(lc);
2644  WindowClause *refwc = NULL;
2645  List *partitionClause;
2646  List *orderClause;
2647  Oid rangeopfamily = InvalidOid;
2648  Oid rangeopcintype = InvalidOid;
2649  WindowClause *wc;
2650 
2651  winref++;
2652 
2653  /*
2654  * Check for duplicate window names.
2655  */
2656  if (windef->name &&
2657  findWindowClause(result, windef->name) != NULL)
2658  ereport(ERROR,
2659  (errcode(ERRCODE_WINDOWING_ERROR),
2660  errmsg("window \"%s\" is already defined", windef->name),
2661  parser_errposition(pstate, windef->location)));
2662 
2663  /*
2664  * If it references a previous window, look that up.
2665  */
2666  if (windef->refname)
2667  {
2668  refwc = findWindowClause(result, windef->refname);
2669  if (refwc == NULL)
2670  ereport(ERROR,
2671  (errcode(ERRCODE_UNDEFINED_OBJECT),
2672  errmsg("window \"%s\" does not exist",
2673  windef->refname),
2674  parser_errposition(pstate, windef->location)));
2675  }
2676 
2677  /*
2678  * Transform PARTITION and ORDER specs, if any. These are treated
2679  * almost exactly like top-level GROUP BY and ORDER BY clauses,
2680  * including the special handling of nondefault operator semantics.
2681  */
2682  orderClause = transformSortClause(pstate,
2683  windef->orderClause,
2684  targetlist,
2686  true /* force SQL99 rules */ );
2687  partitionClause = transformGroupClause(pstate,
2688  windef->partitionClause,
2689  NULL,
2690  targetlist,
2691  orderClause,
2693  true /* force SQL99 rules */ );
2694 
2695  /*
2696  * And prepare the new WindowClause.
2697  */
2698  wc = makeNode(WindowClause);
2699  wc->name = windef->name;
2700  wc->refname = windef->refname;
2701 
2702  /*
2703  * Per spec, a windowdef that references a previous one copies the
2704  * previous partition clause (and mustn't specify its own). It can
2705  * specify its own ordering clause, but only if the previous one had
2706  * none. It always specifies its own frame clause, and the previous
2707  * one must not have a frame clause. Yeah, it's bizarre that each of
2708  * these cases works differently, but SQL:2008 says so; see 7.11
2709  * <window clause> syntax rule 10 and general rule 1. The frame
2710  * clause rule is especially bizarre because it makes "OVER foo"
2711  * different from "OVER (foo)", and requires the latter to throw an
2712  * error if foo has a nondefault frame clause. Well, ours not to
2713  * reason why, but we do go out of our way to throw a useful error
2714  * message for such cases.
2715  */
2716  if (refwc)
2717  {
2718  if (partitionClause)
2719  ereport(ERROR,
2720  (errcode(ERRCODE_WINDOWING_ERROR),
2721  errmsg("cannot override PARTITION BY clause of window \"%s\"",
2722  windef->refname),
2723  parser_errposition(pstate, windef->location)));
2725  }
2726  else
2727  wc->partitionClause = partitionClause;
2728  if (refwc)
2729  {
2730  if (orderClause && refwc->orderClause)
2731  ereport(ERROR,
2732  (errcode(ERRCODE_WINDOWING_ERROR),
2733  errmsg("cannot override ORDER BY clause of window \"%s\"",
2734  windef->refname),
2735  parser_errposition(pstate, windef->location)));
2736  if (orderClause)
2737  {
2738  wc->orderClause = orderClause;
2739  wc->copiedOrder = false;
2740  }
2741  else
2742  {
2743  wc->orderClause = copyObject(refwc->orderClause);
2744  wc->copiedOrder = true;
2745  }
2746  }
2747  else
2748  {
2749  wc->orderClause = orderClause;
2750  wc->copiedOrder = false;
2751  }
2752  if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
2753  {
2754  /*
2755  * Use this message if this is a WINDOW clause, or if it's an OVER
2756  * clause that includes ORDER BY or framing clauses. (We already
2757  * rejected PARTITION BY above, so no need to check that.)
2758  */
2759  if (windef->name ||
2760  orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
2761  ereport(ERROR,
2762  (errcode(ERRCODE_WINDOWING_ERROR),
2763  errmsg("cannot copy window \"%s\" because it has a frame clause",
2764  windef->refname),
2765  parser_errposition(pstate, windef->location)));
2766  /* Else this clause is just OVER (foo), so say this: */
2767  ereport(ERROR,
2768  (errcode(ERRCODE_WINDOWING_ERROR),
2769  errmsg("cannot copy window \"%s\" because it has a frame clause",
2770  windef->refname),
2771  errhint("Omit the parentheses in this OVER clause."),
2772  parser_errposition(pstate, windef->location)));
2773  }
2774  wc->frameOptions = windef->frameOptions;
2775 
2776  /*
2777  * RANGE offset PRECEDING/FOLLOWING requires exactly one ORDER BY
2778  * column; check that and get its sort opfamily info.
2779  */
2780  if ((wc->frameOptions & FRAMEOPTION_RANGE) &&
2783  {
2784  SortGroupClause *sortcl;
2785  Node *sortkey;
2786  int16 rangestrategy;
2787 
2788  if (list_length(wc->orderClause) != 1)
2789  ereport(ERROR,
2790  (errcode(ERRCODE_WINDOWING_ERROR),
2791  errmsg("RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column"),
2792  parser_errposition(pstate, windef->location)));
2793  sortcl = linitial_node(SortGroupClause, wc->orderClause);
2794  sortkey = get_sortgroupclause_expr(sortcl, *targetlist);
2795  /* Find the sort operator in pg_amop */
2796  if (!get_ordering_op_properties(sortcl->sortop,
2797  &rangeopfamily,
2798  &rangeopcintype,
2799  &rangestrategy))
2800  elog(ERROR, "operator %u is not a valid ordering operator",
2801  sortcl->sortop);
2802  /* Record properties of sort ordering */
2803  wc->inRangeColl = exprCollation(sortkey);
2804  wc->inRangeAsc = (rangestrategy == BTLessStrategyNumber);
2805  wc->inRangeNullsFirst = sortcl->nulls_first;
2806  }
2807 
2808  /* Per spec, GROUPS mode requires an ORDER BY clause */
2809  if (wc->frameOptions & FRAMEOPTION_GROUPS)
2810  {
2811  if (wc->orderClause == NIL)
2812  ereport(ERROR,
2813  (errcode(ERRCODE_WINDOWING_ERROR),
2814  errmsg("GROUPS mode requires an ORDER BY clause"),
2815  parser_errposition(pstate, windef->location)));
2816  }
2817 
2818  /* Process frame offset expressions */
2819  wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
2820  rangeopfamily, rangeopcintype,
2821  &wc->startInRangeFunc,
2822  windef->startOffset);
2823  wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
2824  rangeopfamily, rangeopcintype,
2825  &wc->endInRangeFunc,
2826  windef->endOffset);
2827  wc->runCondition = NIL;
2828  wc->winref = winref;
2829 
2830  result = lappend(result, wc);
2831  }
2832 
2833  return result;
2834 }
signed short int16
Definition: c.h:477
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
Definition: lsyscache.c:206
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:764
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
List * transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
static Node * transformFrameOffset(ParseState *pstate, int frameOptions, Oid rangeopfamily, Oid rangeopcintype, Oid *inRangeFunc, Node *clause)
static WindowClause * findWindowClause(List *wclist, const char *name)
@ EXPR_KIND_WINDOW_PARTITION
Definition: parse_node.h:49
@ EXPR_KIND_WINDOW_ORDER
Definition: parse_node.h:50
#define FRAMEOPTION_END_OFFSET
Definition: parsenodes.h:593
#define FRAMEOPTION_START_OFFSET
Definition: parsenodes.h:591
#define FRAMEOPTION_RANGE
Definition: parsenodes.h:573
#define FRAMEOPTION_GROUPS
Definition: parsenodes.h:575
#define FRAMEOPTION_DEFAULTS
Definition: parsenodes.h:599
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial_node(type, l)
Definition: pg_list.h:181
#define BTLessStrategyNumber
Definition: stratnum.h:29
Oid endInRangeFunc
Definition: parsenodes.h:1485
bool copiedOrder
Definition: parsenodes.h:1494
Node * startOffset
Definition: parsenodes.h:1478
char * refname
Definition: parsenodes.h:1473
List * partitionClause
Definition: parsenodes.h:1474
Oid startInRangeFunc
Definition: parsenodes.h:1483
bool inRangeNullsFirst
Definition: parsenodes.h:1491
Node * endOffset
Definition: parsenodes.h:1479
List * runCondition
Definition: parsenodes.h:1481
List * orderClause
Definition: parsenodes.h:1476
List * orderClause
Definition: parsenodes.h:558
List * partitionClause
Definition: parsenodes.h:557
Node * startOffset
Definition: parsenodes.h:560
char * refname
Definition: parsenodes.h:556
Node * endOffset
Definition: parsenodes.h:561
int frameOptions
Definition: parsenodes.h:559
int location
Definition: parsenodes.h:562
char * name
Definition: parsenodes.h:555
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:379

References BTLessStrategyNumber, 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_node, 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, WindowClause::runCondition, SortGroupClause::sortop, WindowClause::startInRangeFunc, WindowDef::startOffset, WindowClause::startOffset, transformFrameOffset(), transformGroupClause(), transformSortClause(), and WindowClause::winref.

Referenced by transformPLAssignStmt(), and transformSelectStmt().