PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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)
 
bool interpretOidsOption (List *defList, bool allowOids)
 
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

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

Definition at line 3148 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, NULL, 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(), TEXTOID, SortGroupClause::tleSortGroupRef, UNKNOWNOID, and SortBy::useOp.

Referenced by transformAggregateCall(), and transformSortClause().

3150 {
3151  Oid restype = exprType((Node *) tle->expr);
3152  Oid sortop;
3153  Oid eqop;
3154  bool hashable;
3155  bool reverse;
3156  int location;
3157  ParseCallbackState pcbstate;
3158 
3159  /* if tlist item is an UNKNOWN literal, change it to TEXT */
3160  if (restype == UNKNOWNOID)
3161  {
3162  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3163  restype, TEXTOID, -1,
3166  -1);
3167  restype = TEXTOID;
3168  }
3169 
3170  /*
3171  * Rather than clutter the API of get_sort_group_operators and the other
3172  * functions we're about to use, make use of error context callback to
3173  * mark any error reports with a parse position. We point to the operator
3174  * location if present, else to the expression being sorted. (NB: use the
3175  * original untransformed expression here; the TLE entry might well point
3176  * at a duplicate expression in the regular SELECT list.)
3177  */
3178  location = sortby->location;
3179  if (location < 0)
3180  location = exprLocation(sortby->node);
3181  setup_parser_errposition_callback(&pcbstate, pstate, location);
3182 
3183  /* determine the sortop, eqop, and directionality */
3184  switch (sortby->sortby_dir)
3185  {
3186  case SORTBY_DEFAULT:
3187  case SORTBY_ASC:
3188  get_sort_group_operators(restype,
3189  true, true, false,
3190  &sortop, &eqop, NULL,
3191  &hashable);
3192  reverse = false;
3193  break;
3194  case SORTBY_DESC:
3195  get_sort_group_operators(restype,
3196  false, true, true,
3197  NULL, &eqop, &sortop,
3198  &hashable);
3199  reverse = true;
3200  break;
3201  case SORTBY_USING:
3202  Assert(sortby->useOp != NIL);
3203  sortop = compatible_oper_opid(sortby->useOp,
3204  restype,
3205  restype,
3206  false);
3207 
3208  /*
3209  * Verify it's a valid ordering operator, fetch the corresponding
3210  * equality operator, and determine whether to consider it like
3211  * ASC or DESC.
3212  */
3213  eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3214  if (!OidIsValid(eqop))
3215  ereport(ERROR,
3216  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3217  errmsg("operator %s is not a valid ordering operator",
3218  strVal(llast(sortby->useOp))),
3219  errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3220 
3221  /*
3222  * Also see if the equality operator is hashable.
3223  */
3224  hashable = op_hashjoinable(eqop, restype);
3225  break;
3226  default:
3227  elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3228  sortop = InvalidOid; /* keep compiler quiet */
3229  eqop = InvalidOid;
3230  hashable = false;
3231  reverse = false;
3232  break;
3233  }
3234 
3236 
3237  /* avoid making duplicate sortlist entries */
3238  if (!targetIsInSortList(tle, sortop, sortlist))
3239  {
3241 
3242  sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3243 
3244  sortcl->eqop = eqop;
3245  sortcl->sortop = sortop;
3246  sortcl->hashable = hashable;
3247 
3248  switch (sortby->sortby_nulls)
3249  {
3250  case SORTBY_NULLS_DEFAULT:
3251  /* NULLS FIRST is default for DESC; other way for ASC */
3252  sortcl->nulls_first = reverse;
3253  break;
3254  case SORTBY_NULLS_FIRST:
3255  sortcl->nulls_first = true;
3256  break;
3257  case SORTBY_NULLS_LAST:
3258  sortcl->nulls_first = false;
3259  break;
3260  default:
3261  elog(ERROR, "unrecognized sortby_nulls: %d",
3262  sortby->sortby_nulls);
3263  break;
3264  }
3265 
3266  sortlist = lappend(sortlist, sortcl);
3267  }
3268 
3269  return sortlist;
3270 }
#define NIL
Definition: pg_list.h:69
Index assignSortGroupRef(TargetEntry *tle, List *tlist)
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1204
SortByDir sortby_dir
Definition: parsenodes.h:461
#define TEXTOID
Definition: pg_type.h:324
List * useOp
Definition: parsenodes.h:463
Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse)
Definition: lsyscache.c:264
#define llast(l)
Definition: pg_list.h:126
Index tleSortGroupRef
Definition: parsenodes.h:1141
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:506
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:159
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1214
#define ERROR
Definition: elog.h:43
SortByNulls sortby_nulls
Definition: parsenodes.h:462
#define ereport(elevel, rest)
Definition: elog.h:122
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:143
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define makeNode(_type_)
Definition: nodes.h:554
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
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:1352
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define UNKNOWNOID
Definition: pg_type.h:427
int errmsg(const char *fmt,...)
Definition: elog.c:797
Oid compatible_oper_opid(List *op, Oid arg1, Oid arg2, bool noError)
Definition: parse_oper.c:494
#define elog
Definition: elog.h:219
Node * node
Definition: parsenodes.h:460
int location
Definition: parsenodes.h:464
Index assignSortGroupRef ( TargetEntry tle,
List tlist 
)

Definition at line 3346 of file parse_clause.c.

References lfirst, and TargetEntry::ressortgroupref.

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

3347 {
3348  Index maxRef;
3349  ListCell *l;
3350 
3351  if (tle->ressortgroupref) /* already has one? */
3352  return tle->ressortgroupref;
3353 
3354  /* easiest way to pick an unused refnumber: max used + 1 */
3355  maxRef = 0;
3356  foreach(l, tlist)
3357  {
3358  Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3359 
3360  if (ref > maxRef)
3361  maxRef = ref;
3362  }
3363  tle->ressortgroupref = maxRef + 1;
3364  return tle->ressortgroupref;
3365 }
unsigned int Index
Definition: c.h:365
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1355
bool interpretOidsOption ( List defList,
bool  allowOids 
)

Definition at line 244 of file parse_clause.c.

References default_with_oids, defGetBoolean(), DefElem::defname, DefElem::defnamespace, ereport, errcode(), errmsg(), ERROR, lfirst, NULL, and pg_strcasecmp().

Referenced by DefineRelation(), GetIntoRelEFlags(), and transformCreateStmt().

245 {
246  ListCell *cell;
247 
248  /* Scan list to see if OIDS was included */
249  foreach(cell, defList)
250  {
251  DefElem *def = (DefElem *) lfirst(cell);
252 
253  if (def->defnamespace == NULL &&
254  pg_strcasecmp(def->defname, "oids") == 0)
255  {
256  if (!allowOids)
257  ereport(ERROR,
258  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
259  errmsg("unrecognized parameter \"%s\"",
260  def->defname)));
261  return defGetBoolean(def);
262  }
263  }
264 
265  /* Force no-OIDS result if caller disallows OIDS. */
266  if (!allowOids)
267  return false;
268 
269  /* OIDS option was not specified, so use default. */
270  return default_with_oids;
271 }
char * defnamespace
Definition: parsenodes.h:707
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
bool default_with_oids
Definition: guc.c:451
bool defGetBoolean(DefElem *def)
Definition: define.c:111
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:708
int setTargetTable ( ParseState pstate,
RangeVar relation,
bool  inh,
bool  alsoSource,
AclMode  requiredPerms 
)

Definition at line 178 of file parse_clause.c.

References addRangeTableEntryForRelation(), addRTEtoQuery(), RangeVar::alias, Assert, heap_close, list_length(), NoLock, NULL, ParseState::p_rtable, ParseState::p_target_rangetblentry, ParseState::p_target_relation, parserOpenTable(), RangeTblEntry::requiredPerms, RowExclusiveLock, and rt_fetch.

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

180 {
181  RangeTblEntry *rte;
182  int rtindex;
183 
184  /* Close old target; this could only happen for multi-action rules */
185  if (pstate->p_target_relation != NULL)
187 
188  /*
189  * Open target rel and grab suitable lock (which we will hold till end of
190  * transaction).
191  *
192  * free_parsestate() will eventually do the corresponding heap_close(),
193  * but *not* release the lock.
194  */
195  pstate->p_target_relation = parserOpenTable(pstate, relation,
197 
198  /*
199  * Now build an RTE.
200  */
201  rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
202  relation->alias, inh, false);
203  pstate->p_target_rangetblentry = rte;
204 
205  /* assume new rte is at end */
206  rtindex = list_length(pstate->p_rtable);
207  Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
208 
209  /*
210  * Override addRangeTableEntry's default ACL_SELECT permissions check, and
211  * instead mark target table as requiring exactly the specified
212  * permissions.
213  *
214  * If we find an explicit reference to the rel later during parse
215  * analysis, we will add the ACL_SELECT bit back again; see
216  * markVarForSelectPriv and its callers.
217  */
218  rte->requiredPerms = requiredPerms;
219 
220  /*
221  * If UPDATE/DELETE, add table to joinlist and namespace.
222  *
223  * Note: some callers know that they can find the new ParseNamespaceItem
224  * at the end of the pstate->p_namespace list. This is a bit ugly but not
225  * worth complicating this function's signature for.
226  */
227  if (alsoSource)
228  addRTEtoQuery(pstate, rte, true, true, true);
229 
230  return rtindex;
231 }
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
AclMode requiredPerms
Definition: parsenodes.h:1004
#define heap_close(r, l)
Definition: heapam.h:97
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:179
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
Relation p_target_relation
Definition: parse_node.h:178
Alias * alias
Definition: primnodes.h:72
List * p_rtable
Definition: parse_node.h:168
bool targetIsInSortList ( TargetEntry tle,
Oid  sortop,
List sortList 
)

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

3388 {
3389  Index ref = tle->ressortgroupref;
3390  ListCell *l;
3391 
3392  /* no need to scan list if tle has no marker */
3393  if (ref == 0)
3394  return false;
3395 
3396  foreach(l, sortList)
3397  {
3398  SortGroupClause *scl = (SortGroupClause *) lfirst(l);
3399 
3400  if (scl->tleSortGroupRef == ref &&
3401  (sortop == InvalidOid ||
3402  sortop == scl->sortop ||
3403  sortop == get_commutator(scl->sortop)))
3404  return true;
3405  }
3406  return false;
3407 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1281
Index tleSortGroupRef
Definition: parsenodes.h:1141
unsigned int Index
Definition: c.h:365
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1355
List* transformDistinctClause ( ParseState pstate,
List **  targetlist,
List sortClause,
bool  is_agg 
)

Definition at line 2744 of file parse_clause.c.

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

Referenced by transformAggregateCall(), and transformSelectStmt().

2746 {
2747  List *result = NIL;
2748  ListCell *slitem;
2749  ListCell *tlitem;
2750 
2751  /*
2752  * The distinctClause should consist of all ORDER BY items followed by all
2753  * other non-resjunk targetlist items. There must not be any resjunk
2754  * ORDER BY items --- that would imply that we are sorting by a value that
2755  * isn't necessarily unique within a DISTINCT group, so the results
2756  * wouldn't be well-defined. This construction ensures we follow the rule
2757  * that sortClause and distinctClause match; in fact the sortClause will
2758  * always be a prefix of distinctClause.
2759  *
2760  * Note a corner case: the same TLE could be in the ORDER BY list multiple
2761  * times with different sortops. We have to include it in the
2762  * distinctClause the same way to preserve the prefix property. The net
2763  * effect will be that the TLE value will be made unique according to both
2764  * sortops.
2765  */
2766  foreach(slitem, sortClause)
2767  {
2768  SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
2769  TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
2770 
2771  if (tle->resjunk)
2772  ereport(ERROR,
2773  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2774  is_agg ?
2775  errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
2776  errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
2777  parser_errposition(pstate,
2778  exprLocation((Node *) tle->expr))));
2779  result = lappend(result, copyObject(scl));
2780  }
2781 
2782  /*
2783  * Now add any remaining non-resjunk tlist items, using default sort/group
2784  * semantics for their data types.
2785  */
2786  foreach(tlitem, *targetlist)
2787  {
2788  TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
2789 
2790  if (tle->resjunk)
2791  continue; /* ignore junk */
2792  result = addTargetToGroupList(pstate, tle,
2793  result, *targetlist,
2794  exprLocation((Node *) tle->expr));
2795  }
2796 
2797  /*
2798  * Complain if we found nothing to make DISTINCT. Returning an empty list
2799  * would cause the parsed Query to look like it didn't have DISTINCT, with
2800  * results that would probably surprise the user. Note: this case is
2801  * presently impossible for aggregates because of grammar restrictions,
2802  * but we check anyway.
2803  */
2804  if (result == NIL)
2805  ereport(ERROR,
2806  (errcode(ERRCODE_SYNTAX_ERROR),
2807  is_agg ?
2808  errmsg("an aggregate with DISTINCT must have at least one argument") :
2809  errmsg("SELECT DISTINCT must have at least one column")));
2810 
2811  return result;
2812 }
#define NIL
Definition: pg_list.h:69
static List * addTargetToGroupList(ParseState *pstate, TargetEntry *tle, List *grouplist, List *targetlist, int location)
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:370
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1204
Definition: nodes.h:506
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
void * copyObject(const void *from)
Definition: copyfuncs.c:4619
bool resjunk
Definition: primnodes.h:1359
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1352
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: pg_list.h:45
List* transformDistinctOnClause ( ParseState pstate,
List distinctlist,
List **  targetlist,
List sortClause 
)

Definition at line 2828 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(), result, targetIsInSortList(), and SortGroupClause::tleSortGroupRef.

Referenced by transformSelectStmt().

2830 {
2831  List *result = NIL;
2832  List *sortgrouprefs = NIL;
2833  bool skipped_sortitem;
2834  ListCell *lc;
2835  ListCell *lc2;
2836 
2837  /*
2838  * Add all the DISTINCT ON expressions to the tlist (if not already
2839  * present, they are added as resjunk items). Assign sortgroupref numbers
2840  * to them, and make a list of these numbers. (NB: we rely below on the
2841  * sortgrouprefs list being one-for-one with the original distinctlist.
2842  * Also notice that we could have duplicate DISTINCT ON expressions and
2843  * hence duplicate entries in sortgrouprefs.)
2844  */
2845  foreach(lc, distinctlist)
2846  {
2847  Node *dexpr = (Node *) lfirst(lc);
2848  int sortgroupref;
2849  TargetEntry *tle;
2850 
2851  tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
2853  sortgroupref = assignSortGroupRef(tle, *targetlist);
2854  sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
2855  }
2856 
2857  /*
2858  * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
2859  * semantics from ORDER BY items that match DISTINCT ON items, and also
2860  * adopt their column sort order. We insist that the distinctClause and
2861  * sortClause match, so throw error if we find the need to add any more
2862  * distinctClause items after we've skipped an ORDER BY item that wasn't
2863  * in DISTINCT ON.
2864  */
2865  skipped_sortitem = false;
2866  foreach(lc, sortClause)
2867  {
2868  SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
2869 
2870  if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
2871  {
2872  if (skipped_sortitem)
2873  ereport(ERROR,
2874  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2875  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2876  parser_errposition(pstate,
2878  sortgrouprefs,
2879  distinctlist))));
2880  else
2881  result = lappend(result, copyObject(scl));
2882  }
2883  else
2884  skipped_sortitem = true;
2885  }
2886 
2887  /*
2888  * Now add any remaining DISTINCT ON items, using default sort/group
2889  * semantics for their data types. (Note: this is pretty questionable; if
2890  * the ORDER BY list doesn't include all the DISTINCT ON items and more
2891  * besides, you certainly aren't using DISTINCT ON in the intended way,
2892  * and you probably aren't going to get consistent results. It might be
2893  * better to throw an error or warning here. But historically we've
2894  * allowed it, so keep doing so.)
2895  */
2896  forboth(lc, distinctlist, lc2, sortgrouprefs)
2897  {
2898  Node *dexpr = (Node *) lfirst(lc);
2899  int sortgroupref = lfirst_int(lc2);
2900  TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
2901 
2902  if (targetIsInSortList(tle, InvalidOid, result))
2903  continue; /* already in list (with some semantics) */
2904  if (skipped_sortitem)
2905  ereport(ERROR,
2906  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2907  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2908  parser_errposition(pstate, exprLocation(dexpr))));
2909  result = addTargetToGroupList(pstate, tle,
2910  result, *targetlist,
2911  exprLocation(dexpr));
2912  }
2913 
2914  /*
2915  * An empty result list is impossible here because of grammar
2916  * restrictions.
2917  */
2918  Assert(result != NIL);
2919 
2920  return result;
2921 }
#define NIL
Definition: pg_list.h:69
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:174
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1204
Index tleSortGroupRef
Definition: parsenodes.h:1141
Definition: nodes.h:506
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
void * copyObject(const void *from)
Definition: copyfuncs.c:4619
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:107
bool list_member_int(const List *list, int datum)
Definition: list.c:485
#define ereport(elevel, rest)
Definition: elog.h:122
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:348
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static TargetEntry * findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: pg_list.h:45
void transformFromClause ( ParseState pstate,
List frmList 
)

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

113 {
114  ListCell *fl;
115 
116  /*
117  * The grammar will have produced a list of RangeVars, RangeSubselects,
118  * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
119  * entries to the rtable), check for duplicate refnames, and then add it
120  * to the joinlist and namespace.
121  *
122  * Note we must process the items left-to-right for proper handling of
123  * LATERAL references.
124  */
125  foreach(fl, frmList)
126  {
127  Node *n = lfirst(fl);
128  RangeTblEntry *rte;
129  int rtindex;
130  List *namespace;
131 
132  n = transformFromClauseItem(pstate, n,
133  &rte,
134  &rtindex,
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:506
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * p_namespace
Definition: parse_node.h:172
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:128
#define lfirst(lc)
Definition: pg_list.h:106
static Node * transformFromClauseItem(ParseState *pstate, Node *n, RangeTblEntry **top_rte, int *top_rti, List **namespace)
List * p_joinlist
Definition: parse_node.h:170
Definition: pg_list.h:45
List* transformGroupClause ( ParseState pstate,
List grouplist,
List **  groupingSets,
List **  targetlist,
List sortClause,
ParseExprKind  exprKind,
bool  useSQL99 
)

Definition at line 2440 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, NULL, result, transformGroupClauseExpr(), and transformGroupingSet().

Referenced by transformSelectStmt(), and transformWindowDefinitions().

2443 {
2444  List *result = NIL;
2445  List *flat_grouplist;
2446  List *gsets = NIL;
2447  ListCell *gl;
2448  bool hasGroupingSets = false;
2449  Bitmapset *seen_local = NULL;
2450 
2451  /*
2452  * Recursively flatten implicit RowExprs. (Technically this is only needed
2453  * for GROUP BY, per the syntax rules for grouping sets, but we do it
2454  * anyway.)
2455  */
2456  flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
2457  true,
2458  &hasGroupingSets);
2459 
2460  /*
2461  * If the list is now empty, but hasGroupingSets is true, it's because we
2462  * elided redundant empty grouping sets. Restore a single empty grouping
2463  * set to leave a canonical form: GROUP BY ()
2464  */
2465 
2466  if (flat_grouplist == NIL && hasGroupingSets)
2467  {
2469  NIL,
2470  exprLocation((Node *) grouplist)));
2471  }
2472 
2473  foreach(gl, flat_grouplist)
2474  {
2475  Node *gexpr = (Node *) lfirst(gl);
2476 
2477  if (IsA(gexpr, GroupingSet))
2478  {
2479  GroupingSet *gset = (GroupingSet *) gexpr;
2480 
2481  switch (gset->kind)
2482  {
2483  case GROUPING_SET_EMPTY:
2484  gsets = lappend(gsets, gset);
2485  break;
2486  case GROUPING_SET_SIMPLE:
2487  /* can't happen */
2488  Assert(false);
2489  break;
2490  case GROUPING_SET_SETS:
2491  case GROUPING_SET_CUBE:
2492  case GROUPING_SET_ROLLUP:
2493  gsets = lappend(gsets,
2494  transformGroupingSet(&result,
2495  pstate, gset,
2496  targetlist, sortClause,
2497  exprKind, useSQL99, true));
2498  break;
2499  }
2500  }
2501  else
2502  {
2503  Index ref = transformGroupClauseExpr(&result, seen_local,
2504  pstate, gexpr,
2505  targetlist, sortClause,
2506  exprKind, useSQL99, true);
2507 
2508  if (ref > 0)
2509  {
2510  seen_local = bms_add_member(seen_local, ref);
2511  if (hasGroupingSets)
2512  gsets = lappend(gsets,
2514  list_make1_int(ref),
2515  exprLocation(gexpr)));
2516  }
2517  }
2518  }
2519 
2520  /* parser should prevent this */
2521  Assert(gsets == NIL || groupingSets != NULL);
2522 
2523  if (groupingSets)
2524  *groupingSets = gsets;
2525 
2526  return result;
2527 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1204
Definition: nodes.h:506
return result
Definition: formatting.c:1618
static Node * transformGroupingSet(List **flatresult, ParseState *pstate, GroupingSet *gset, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
GroupingSetKind kind
Definition: parsenodes.h:1210
#define list_make1(x1)
Definition: pg_list.h:133
static Node * flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
#define list_make1_int(x1)
Definition: pg_list.h:139
List * lappend(List *list, void *datum)
Definition: list.c:128
unsigned int Index
Definition: c.h:365
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
GroupingSet * makeGroupingSet(GroupingSetKind kind, List *content, int location)
Definition: makefuncs.c:604
Definition: pg_list.h:45
static Index transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local, ParseState *pstate, Node *gexpr, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
Node* transformLimitClause ( ParseState pstate,
Node clause,
ParseExprKind  exprKind,
const char *  constructName 
)

Definition at line 1699 of file parse_clause.c.

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

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

1701 {
1702  Node *qual;
1703 
1704  if (clause == NULL)
1705  return NULL;
1706 
1707  qual = transformExpr(pstate, clause, exprKind);
1708 
1709  qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1710 
1711  /* LIMIT can't refer to any variables of the current query */
1712  checkExprIsVarFree(pstate, qual, constructName);
1713 
1714  return qual;
1715 }
static void checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:147
Definition: nodes.h:506
#define INT8OID
Definition: pg_type.h:304
#define NULL
Definition: c.h:229
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
void transformOnConflictArbiter ( ParseState pstate,
OnConflictClause onConflictClause,
List **  arbiterExpr,
Node **  arbiterWhere,
Oid constraint 
)

Definition at line 3057 of file parse_clause.c.

References OnConflictClause::action, addRTEtoQuery(), InferClause::conname, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_INDEX_PREDICATE, exprLocation(), get_relation_constraint_oid(), InferClause::indexElems, OnConflictClause::infer, InvalidOid, IsCatalogRelation(), NIL, NULL, ONCONFLICT_UPDATE, ParseState::p_namespace, ParseState::p_target_rangetblentry, ParseState::p_target_relation, parser_errposition(), RelationGetRelationName, RelationGetRelid, RelationIsUsedAsCatalogTable, resolve_unique_index_expr(), transformExpr(), and InferClause::whereClause.

Referenced by transformOnConflictClause().

3061 {
3062  InferClause *infer = onConflictClause->infer;
3063 
3064  *arbiterExpr = NIL;
3065  *arbiterWhere = NULL;
3066  *constraint = InvalidOid;
3067 
3068  if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
3069  ereport(ERROR,
3070  (errcode(ERRCODE_SYNTAX_ERROR),
3071  errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
3072  errhint("For example, ON CONFLICT (column_name)."),
3073  parser_errposition(pstate,
3074  exprLocation((Node *) onConflictClause))));
3075 
3076  /*
3077  * To simplify certain aspects of its design, speculative insertion into
3078  * system catalogs is disallowed
3079  */
3080  if (IsCatalogRelation(pstate->p_target_relation))
3081  ereport(ERROR,
3082  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3083  errmsg("ON CONFLICT is not supported with system catalog tables"),
3084  parser_errposition(pstate,
3085  exprLocation((Node *) onConflictClause))));
3086 
3087  /* Same applies to table used by logical decoding as catalog table */
3089  ereport(ERROR,
3090  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3091  errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
3093  parser_errposition(pstate,
3094  exprLocation((Node *) onConflictClause))));
3095 
3096  /* ON CONFLICT DO NOTHING does not require an inference clause */
3097  if (infer)
3098  {
3099  List *save_namespace;
3100 
3101  /*
3102  * While we process the arbiter expressions, accept only non-qualified
3103  * references to the target table. Hide any other relations.
3104  */
3105  save_namespace = pstate->p_namespace;
3106  pstate->p_namespace = NIL;
3107  addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
3108  false, false, true);
3109 
3110  if (infer->indexElems)
3111  *arbiterExpr = resolve_unique_index_expr(pstate, infer,
3112  pstate->p_target_relation);
3113 
3114  /*
3115  * Handling inference WHERE clause (for partial unique index
3116  * inference)
3117  */
3118  if (infer->whereClause)
3119  *arbiterWhere = transformExpr(pstate, infer->whereClause,
3121 
3122  pstate->p_namespace = save_namespace;
3123 
3124  if (infer->conname)
3126  infer->conname, false);
3127  }
3128 
3129  /*
3130  * It's convenient to form a list of expressions based on the
3131  * representation used by CREATE INDEX, since the same restrictions are
3132  * appropriate (e.g. on subqueries). However, from here on, a dedicated
3133  * primnode representation is used for inference elements, and so
3134  * assign_query_collations() can be trusted to do the right thing with the
3135  * post parse analysis query tree inference clause representation.
3136  */
3137 }
#define NIL
Definition: pg_list.h:69
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:91
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1204
Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:147
List * indexElems
Definition: parsenodes.h:1288
Definition: nodes.h:506
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
List * p_namespace
Definition: parse_node.h:172
char * conname
Definition: parsenodes.h:1290
#define RelationGetRelationName(relation)
Definition: rel.h:437
#define ereport(elevel, rest)
Definition: elog.h:122
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:179
InferClause * infer
Definition: parsenodes.h:1304
#define RelationIsUsedAsCatalogTable(relation)
Definition: rel.h:316
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
Node * whereClause
Definition: parsenodes.h:1289
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:178
OnConflictAction action
Definition: parsenodes.h:1303
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:417
static List * resolve_unique_index_expr(ParseState *pstate, InferClause *infer, Relation heapRel)
List* transformSortClause ( ParseState pstate,
List orderlist,
List **  targetlist,
ParseExprKind  exprKind,
bool  useSQL99 
)

Definition at line 2540 of file parse_clause.c.

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

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

2545 {
2546  List *sortlist = NIL;
2547  ListCell *olitem;
2548 
2549  foreach(olitem, orderlist)
2550  {
2551  SortBy *sortby = (SortBy *) lfirst(olitem);
2552  TargetEntry *tle;
2553 
2554  if (useSQL99)
2555  tle = findTargetlistEntrySQL99(pstate, sortby->node,
2556  targetlist, exprKind);
2557  else
2558  tle = findTargetlistEntrySQL92(pstate, sortby->node,
2559  targetlist, exprKind);
2560 
2561  sortlist = addTargetToSortList(pstate, tle,
2562  sortlist, *targetlist, sortby);
2563  }
2564 
2565  return sortlist;
2566 }
#define NIL
Definition: pg_list.h:69
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:106
static TargetEntry * findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
Node * node
Definition: parsenodes.h:460
Definition: pg_list.h:45
Node* transformWhereClause ( ParseState pstate,
Node clause,
ParseExprKind  exprKind,
const char *  constructName 
)

Definition at line 1672 of file parse_clause.c.

References coerce_to_boolean(), NULL, and transformExpr().

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

1674 {
1675  Node *qual;
1676 
1677  if (clause == NULL)
1678  return NULL;
1679 
1680  qual = transformExpr(pstate, clause, exprKind);
1681 
1682  qual = coerce_to_boolean(pstate, qual, constructName);
1683 
1684  return qual;
1685 }
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:147
Definition: nodes.h:506
#define NULL
Definition: c.h:229
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
List* transformWindowDefinitions ( ParseState pstate,
List windowdefs,
List **  targetlist 
)

Definition at line 2573 of file parse_clause.c.

References WindowClause::copiedOrder, copyObject(), WindowDef::endOffset, WindowClause::endOffset, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, findWindowClause(), FRAMEOPTION_DEFAULTS, WindowDef::frameOptions, WindowClause::frameOptions, lappend(), lfirst, WindowDef::location, makeNode, WindowDef::name, WindowClause::name, NIL, NULL, WindowDef::orderClause, WindowClause::orderClause, parser_errposition(), WindowDef::partitionClause, WindowClause::partitionClause, WindowDef::refname, WindowClause::refname, result, WindowDef::startOffset, WindowClause::startOffset, transformFrameOffset(), transformGroupClause(), transformSortClause(), and WindowClause::winref.

Referenced by transformSelectStmt().

2576 {
2577  List *result = NIL;
2578  Index winref = 0;
2579  ListCell *lc;
2580 
2581  foreach(lc, windowdefs)
2582  {
2583  WindowDef *windef = (WindowDef *) lfirst(lc);
2584  WindowClause *refwc = NULL;
2585  List *partitionClause;
2586  List *orderClause;
2587  WindowClause *wc;
2588 
2589  winref++;
2590 
2591  /*
2592  * Check for duplicate window names.
2593  */
2594  if (windef->name &&
2595  findWindowClause(result, windef->name) != NULL)
2596  ereport(ERROR,
2597  (errcode(ERRCODE_WINDOWING_ERROR),
2598  errmsg("window \"%s\" is already defined", windef->name),
2599  parser_errposition(pstate, windef->location)));
2600 
2601  /*
2602  * If it references a previous window, look that up.
2603  */
2604  if (windef->refname)
2605  {
2606  refwc = findWindowClause(result, windef->refname);
2607  if (refwc == NULL)
2608  ereport(ERROR,
2609  (errcode(ERRCODE_UNDEFINED_OBJECT),
2610  errmsg("window \"%s\" does not exist",
2611  windef->refname),
2612  parser_errposition(pstate, windef->location)));
2613  }
2614 
2615  /*
2616  * Transform PARTITION and ORDER specs, if any. These are treated
2617  * almost exactly like top-level GROUP BY and ORDER BY clauses,
2618  * including the special handling of nondefault operator semantics.
2619  */
2620  orderClause = transformSortClause(pstate,
2621  windef->orderClause,
2622  targetlist,
2624  true /* force SQL99 rules */ );
2625  partitionClause = transformGroupClause(pstate,
2626  windef->partitionClause,
2627  NULL,
2628  targetlist,
2629  orderClause,
2631  true /* force SQL99 rules */ );
2632 
2633  /*
2634  * And prepare the new WindowClause.
2635  */
2636  wc = makeNode(WindowClause);
2637  wc->name = windef->name;
2638  wc->refname = windef->refname;
2639 
2640  /*
2641  * Per spec, a windowdef that references a previous one copies the
2642  * previous partition clause (and mustn't specify its own). It can
2643  * specify its own ordering clause, but only if the previous one had
2644  * none. It always specifies its own frame clause, and the previous
2645  * one must not have a frame clause. Yeah, it's bizarre that each of
2646  * these cases works differently, but SQL:2008 says so; see 7.11
2647  * <window clause> syntax rule 10 and general rule 1. The frame
2648  * clause rule is especially bizarre because it makes "OVER foo"
2649  * different from "OVER (foo)", and requires the latter to throw an
2650  * error if foo has a nondefault frame clause. Well, ours not to
2651  * reason why, but we do go out of our way to throw a useful error
2652  * message for such cases.
2653  */
2654  if (refwc)
2655  {
2656  if (partitionClause)
2657  ereport(ERROR,
2658  (errcode(ERRCODE_WINDOWING_ERROR),
2659  errmsg("cannot override PARTITION BY clause of window \"%s\"",
2660  windef->refname),
2661  parser_errposition(pstate, windef->location)));
2663  }
2664  else
2665  wc->partitionClause = partitionClause;
2666  if (refwc)
2667  {
2668  if (orderClause && refwc->orderClause)
2669  ereport(ERROR,
2670  (errcode(ERRCODE_WINDOWING_ERROR),
2671  errmsg("cannot override ORDER BY clause of window \"%s\"",
2672  windef->refname),
2673  parser_errposition(pstate, windef->location)));
2674  if (orderClause)
2675  {
2676  wc->orderClause = orderClause;
2677  wc->copiedOrder = false;
2678  }
2679  else
2680  {
2681  wc->orderClause = copyObject(refwc->orderClause);
2682  wc->copiedOrder = true;
2683  }
2684  }
2685  else
2686  {
2687  wc->orderClause = orderClause;
2688  wc->copiedOrder = false;
2689  }
2690  if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
2691  {
2692  /*
2693  * Use this message if this is a WINDOW clause, or if it's an OVER
2694  * clause that includes ORDER BY or framing clauses. (We already
2695  * rejected PARTITION BY above, so no need to check that.)
2696  */
2697  if (windef->name ||
2698  orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
2699  ereport(ERROR,
2700  (errcode(ERRCODE_WINDOWING_ERROR),
2701  errmsg("cannot copy window \"%s\" because it has a frame clause",
2702  windef->refname),
2703  parser_errposition(pstate, windef->location)));
2704  /* Else this clause is just OVER (foo), so say this: */
2705  ereport(ERROR,
2706  (errcode(ERRCODE_WINDOWING_ERROR),
2707  errmsg("cannot copy window \"%s\" because it has a frame clause",
2708  windef->refname),
2709  errhint("Omit the parentheses in this OVER clause."),
2710  parser_errposition(pstate, windef->location)));
2711  }
2712  wc->frameOptions = windef->frameOptions;
2713  /* Process frame offset expressions */
2714  wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
2715  windef->startOffset);
2716  wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
2717  windef->endOffset);
2718  wc->winref = winref;
2719 
2720  result = lappend(result, wc);
2721  }
2722 
2723  return result;
2724 }
List * partitionClause
Definition: parsenodes.h:480
char * refname
Definition: parsenodes.h:1233
#define NIL
Definition: pg_list.h:69
bool copiedOrder
Definition: parsenodes.h:1240
int errhint(const char *fmt,...)
Definition: elog.c:987
#define FRAMEOPTION_DEFAULTS
Definition: parsenodes.h:516
int frameOptions
Definition: parsenodes.h:482
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
char * name
Definition: parsenodes.h:478
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
static Node * transformFrameOffset(ParseState *pstate, int frameOptions, Node *clause)
void * copyObject(const void *from)
Definition: copyfuncs.c:4619
Node * startOffset
Definition: parsenodes.h:483
#define ERROR
Definition: elog.h:43
List * partitionClause
Definition: parsenodes.h:1234
int location
Definition: parsenodes.h:485
Node * endOffset
Definition: parsenodes.h:484
List * transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * startOffset
Definition: parsenodes.h:1237
List * orderClause
Definition: parsenodes.h:481
unsigned int Index
Definition: c.h:365
#define makeNode(_type_)
Definition: nodes.h:554
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Node * endOffset
Definition: parsenodes.h:1238
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
List * orderClause
Definition: parsenodes.h:1235
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * refname
Definition: parsenodes.h:479
Definition: pg_list.h:45
static WindowClause * findWindowClause(List *wclist, const char *name)