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

2906 {
2907  Oid restype = exprType((Node *) tle->expr);
2908  Oid sortop;
2909  Oid eqop;
2910  bool hashable;
2911  bool reverse;
2912  int location;
2913  ParseCallbackState pcbstate;
2914 
2915  /* if tlist item is an UNKNOWN literal, change it to TEXT */
2916  if (restype == UNKNOWNOID)
2917  {
2918  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
2919  restype, TEXTOID, -1,
2922  -1);
2923  restype = TEXTOID;
2924  }
2925 
2926  /*
2927  * Rather than clutter the API of get_sort_group_operators and the other
2928  * functions we're about to use, make use of error context callback to
2929  * mark any error reports with a parse position. We point to the operator
2930  * location if present, else to the expression being sorted. (NB: use the
2931  * original untransformed expression here; the TLE entry might well point
2932  * at a duplicate expression in the regular SELECT list.)
2933  */
2934  location = sortby->location;
2935  if (location < 0)
2936  location = exprLocation(sortby->node);
2937  setup_parser_errposition_callback(&pcbstate, pstate, location);
2938 
2939  /* determine the sortop, eqop, and directionality */
2940  switch (sortby->sortby_dir)
2941  {
2942  case SORTBY_DEFAULT:
2943  case SORTBY_ASC:
2944  get_sort_group_operators(restype,
2945  true, true, false,
2946  &sortop, &eqop, NULL,
2947  &hashable);
2948  reverse = false;
2949  break;
2950  case SORTBY_DESC:
2951  get_sort_group_operators(restype,
2952  false, true, true,
2953  NULL, &eqop, &sortop,
2954  &hashable);
2955  reverse = true;
2956  break;
2957  case SORTBY_USING:
2958  Assert(sortby->useOp != NIL);
2959  sortop = compatible_oper_opid(sortby->useOp,
2960  restype,
2961  restype,
2962  false);
2963 
2964  /*
2965  * Verify it's a valid ordering operator, fetch the corresponding
2966  * equality operator, and determine whether to consider it like
2967  * ASC or DESC.
2968  */
2969  eqop = get_equality_op_for_ordering_op(sortop, &reverse);
2970  if (!OidIsValid(eqop))
2971  ereport(ERROR,
2972  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2973  errmsg("operator %s is not a valid ordering operator",
2974  strVal(llast(sortby->useOp))),
2975  errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
2976 
2977  /*
2978  * Also see if the equality operator is hashable.
2979  */
2980  hashable = op_hashjoinable(eqop, restype);
2981  break;
2982  default:
2983  elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
2984  sortop = InvalidOid; /* keep compiler quiet */
2985  eqop = InvalidOid;
2986  hashable = false;
2987  reverse = false;
2988  break;
2989  }
2990 
2992 
2993  /* avoid making duplicate sortlist entries */
2994  if (!targetIsInSortList(tle, sortop, sortlist))
2995  {
2997 
2998  sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
2999 
3000  sortcl->eqop = eqop;
3001  sortcl->sortop = sortop;
3002  sortcl->hashable = hashable;
3003 
3004  switch (sortby->sortby_nulls)
3005  {
3006  case SORTBY_NULLS_DEFAULT:
3007  /* NULLS FIRST is default for DESC; other way for ASC */
3008  sortcl->nulls_first = reverse;
3009  break;
3010  case SORTBY_NULLS_FIRST:
3011  sortcl->nulls_first = true;
3012  break;
3013  case SORTBY_NULLS_LAST:
3014  sortcl->nulls_first = false;
3015  break;
3016  default:
3017  elog(ERROR, "unrecognized sortby_nulls: %d",
3018  sortby->sortby_nulls);
3019  break;
3020  }
3021 
3022  sortlist = lappend(sortlist, sortcl);
3023  }
3024 
3025  return sortlist;
3026 }
#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:1102
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:508
#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:533
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:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
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:185
Expr * expr
Definition: primnodes.h:1330
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define UNKNOWNOID
Definition: pg_type.h:423
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:492
#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 3102 of file parse_clause.c.

References lfirst, and TargetEntry::ressortgroupref.

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

3103 {
3104  Index maxRef;
3105  ListCell *l;
3106 
3107  if (tle->ressortgroupref) /* already has one? */
3108  return tle->ressortgroupref;
3109 
3110  /* easiest way to pick an unused refnumber: max used + 1 */
3111  maxRef = 0;
3112  foreach(l, tlist)
3113  {
3114  Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3115 
3116  if (ref > maxRef)
3117  maxRef = ref;
3118  }
3119  tle->ressortgroupref = maxRef + 1;
3120  return tle->ressortgroupref;
3121 }
unsigned int Index
Definition: c.h:361
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1333
bool interpretOidsOption ( List defList,
bool  allowOids 
)

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

242 {
243  ListCell *cell;
244 
245  /* Scan list to see if OIDS was included */
246  foreach(cell, defList)
247  {
248  DefElem *def = (DefElem *) lfirst(cell);
249 
250  if (def->defnamespace == NULL &&
251  pg_strcasecmp(def->defname, "oids") == 0)
252  {
253  if (!allowOids)
254  ereport(ERROR,
255  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
256  errmsg("unrecognized parameter \"%s\"",
257  def->defname)));
258  return defGetBoolean(def);
259  }
260  }
261 
262  /* Force no-OIDS result if caller disallows OIDS. */
263  if (!allowOids)
264  return false;
265 
266  /* OIDS option was not specified, so use default. */
267  return default_with_oids;
268 }
char * defnamespace
Definition: parsenodes.h:674
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:450
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:226
#define lfirst(lc)
Definition: pg_list.h:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:675
int setTargetTable ( ParseState pstate,
RangeVar relation,
bool  inh,
bool  alsoSource,
AclMode  requiredPerms 
)

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

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

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

3144 {
3145  Index ref = tle->ressortgroupref;
3146  ListCell *l;
3147 
3148  /* no need to scan list if tle has no marker */
3149  if (ref == 0)
3150  return false;
3151 
3152  foreach(l, sortList)
3153  {
3154  SortGroupClause *scl = (SortGroupClause *) lfirst(l);
3155 
3156  if (scl->tleSortGroupRef == ref &&
3157  (sortop == InvalidOid ||
3158  sortop == scl->sortop ||
3159  sortop == get_commutator(scl->sortop)))
3160  return true;
3161  }
3162  return false;
3163 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1281
Index tleSortGroupRef
Definition: parsenodes.h:1102
unsigned int Index
Definition: c.h:361
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1333
List* transformDistinctClause ( ParseState pstate,
List **  targetlist,
List sortClause,
bool  is_agg 
)

Definition at line 2500 of file parse_clause.c.

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

Referenced by transformAggregateCall(), and transformSelectStmt().

2502 {
2503  List *result = NIL;
2504  ListCell *slitem;
2505  ListCell *tlitem;
2506 
2507  /*
2508  * The distinctClause should consist of all ORDER BY items followed by all
2509  * other non-resjunk targetlist items. There must not be any resjunk
2510  * ORDER BY items --- that would imply that we are sorting by a value that
2511  * isn't necessarily unique within a DISTINCT group, so the results
2512  * wouldn't be well-defined. This construction ensures we follow the rule
2513  * that sortClause and distinctClause match; in fact the sortClause will
2514  * always be a prefix of distinctClause.
2515  *
2516  * Note a corner case: the same TLE could be in the ORDER BY list multiple
2517  * times with different sortops. We have to include it in the
2518  * distinctClause the same way to preserve the prefix property. The net
2519  * effect will be that the TLE value will be made unique according to both
2520  * sortops.
2521  */
2522  foreach(slitem, sortClause)
2523  {
2524  SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
2525  TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
2526 
2527  if (tle->resjunk)
2528  ereport(ERROR,
2529  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2530  is_agg ?
2531  errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
2532  errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
2533  parser_errposition(pstate,
2534  exprLocation((Node *) tle->expr))));
2535  result = lappend(result, copyObject(scl));
2536  }
2537 
2538  /*
2539  * Now add any remaining non-resjunk tlist items, using default sort/group
2540  * semantics for their data types.
2541  */
2542  foreach(tlitem, *targetlist)
2543  {
2544  TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
2545 
2546  if (tle->resjunk)
2547  continue; /* ignore junk */
2548  result = addTargetToGroupList(pstate, tle,
2549  result, *targetlist,
2550  exprLocation((Node *) tle->expr));
2551  }
2552 
2553  /*
2554  * Complain if we found nothing to make DISTINCT. Returning an empty list
2555  * would cause the parsed Query to look like it didn't have DISTINCT, with
2556  * results that would probably surprise the user. Note: this case is
2557  * presently impossible for aggregates because of grammar restrictions,
2558  * but we check anyway.
2559  */
2560  if (result == NIL)
2561  ereport(ERROR,
2562  (errcode(ERRCODE_SYNTAX_ERROR),
2563  is_agg ?
2564  errmsg("an aggregate with DISTINCT must have at least one argument") :
2565  errmsg("SELECT DISTINCT must have at least one column")));
2566 
2567  return result;
2568 }
#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:508
int errcode(int sqlerrcode)
Definition: elog.c:575
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
bool resjunk
Definition: primnodes.h:1337
#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:1330
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 2584 of file parse_clause.c.

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

Referenced by transformSelectStmt().

2586 {
2587  List *result = NIL;
2588  List *sortgrouprefs = NIL;
2589  bool skipped_sortitem;
2590  ListCell *lc;
2591  ListCell *lc2;
2592 
2593  /*
2594  * Add all the DISTINCT ON expressions to the tlist (if not already
2595  * present, they are added as resjunk items). Assign sortgroupref numbers
2596  * to them, and make a list of these numbers. (NB: we rely below on the
2597  * sortgrouprefs list being one-for-one with the original distinctlist.
2598  * Also notice that we could have duplicate DISTINCT ON expressions and
2599  * hence duplicate entries in sortgrouprefs.)
2600  */
2601  foreach(lc, distinctlist)
2602  {
2603  Node *dexpr = (Node *) lfirst(lc);
2604  int sortgroupref;
2605  TargetEntry *tle;
2606 
2607  tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
2609  sortgroupref = assignSortGroupRef(tle, *targetlist);
2610  sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
2611  }
2612 
2613  /*
2614  * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
2615  * semantics from ORDER BY items that match DISTINCT ON items, and also
2616  * adopt their column sort order. We insist that the distinctClause and
2617  * sortClause match, so throw error if we find the need to add any more
2618  * distinctClause items after we've skipped an ORDER BY item that wasn't
2619  * in DISTINCT ON.
2620  */
2621  skipped_sortitem = false;
2622  foreach(lc, sortClause)
2623  {
2624  SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
2625 
2626  if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
2627  {
2628  if (skipped_sortitem)
2629  ereport(ERROR,
2630  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2631  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2632  parser_errposition(pstate,
2634  sortgrouprefs,
2635  distinctlist))));
2636  else
2637  result = lappend(result, copyObject(scl));
2638  }
2639  else
2640  skipped_sortitem = true;
2641  }
2642 
2643  /*
2644  * Now add any remaining DISTINCT ON items, using default sort/group
2645  * semantics for their data types. (Note: this is pretty questionable; if
2646  * the ORDER BY list doesn't include all the DISTINCT ON items and more
2647  * besides, you certainly aren't using DISTINCT ON in the intended way,
2648  * and you probably aren't going to get consistent results. It might be
2649  * better to throw an error or warning here. But historically we've
2650  * allowed it, so keep doing so.)
2651  */
2652  forboth(lc, distinctlist, lc2, sortgrouprefs)
2653  {
2654  Node *dexpr = (Node *) lfirst(lc);
2655  int sortgroupref = lfirst_int(lc2);
2656  TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
2657 
2658  if (targetIsInSortList(tle, InvalidOid, result))
2659  continue; /* already in list (with some semantics) */
2660  if (skipped_sortitem)
2661  ereport(ERROR,
2662  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2663  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2664  parser_errposition(pstate, exprLocation(dexpr))));
2665  result = addTargetToGroupList(pstate, tle,
2666  result, *targetlist,
2667  exprLocation(dexpr));
2668  }
2669 
2670  /*
2671  * An empty result list is impossible here because of grammar
2672  * restrictions.
2673  */
2674  Assert(result != NIL);
2675 
2676  return result;
2677 }
#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:1102
Definition: nodes.h:508
int errcode(int sqlerrcode)
Definition: elog.c:575
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
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:670
#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 109 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().

110 {
111  ListCell *fl;
112 
113  /*
114  * The grammar will have produced a list of RangeVars, RangeSubselects,
115  * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
116  * entries to the rtable), check for duplicate refnames, and then add it
117  * to the joinlist and namespace.
118  *
119  * Note we must process the items left-to-right for proper handling of
120  * LATERAL references.
121  */
122  foreach(fl, frmList)
123  {
124  Node *n = lfirst(fl);
125  RangeTblEntry *rte;
126  int rtindex;
127  List *namespace;
128 
129  n = transformFromClauseItem(pstate, n,
130  &rte,
131  &rtindex,
132  &namespace);
133 
134  checkNameSpaceConflicts(pstate, pstate->p_namespace, namespace);
135 
136  /* Mark the new namespace items as visible only to LATERAL */
137  setNamespaceLateralState(namespace, true, true);
138 
139  pstate->p_joinlist = lappend(pstate->p_joinlist, n);
140  pstate->p_namespace = list_concat(pstate->p_namespace, namespace);
141  }
142 
143  /*
144  * We're done parsing the FROM list, so make all namespace items
145  * unconditionally visible. Note that this will also reset lateral_only
146  * for any namespace items that were already present when we were called;
147  * but those should have been that way already.
148  */
149  setNamespaceLateralState(pstate->p_namespace, false, true);
150 }
Definition: nodes.h:508
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)
Definition: parse_clause.c:821
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 2196 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, transformGroupClauseExpr(), and transformGroupingSet().

Referenced by transformSelectStmt(), and transformWindowDefinitions().

2199 {
2200  List *result = NIL;
2201  List *flat_grouplist;
2202  List *gsets = NIL;
2203  ListCell *gl;
2204  bool hasGroupingSets = false;
2205  Bitmapset *seen_local = NULL;
2206 
2207  /*
2208  * Recursively flatten implicit RowExprs. (Technically this is only needed
2209  * for GROUP BY, per the syntax rules for grouping sets, but we do it
2210  * anyway.)
2211  */
2212  flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
2213  true,
2214  &hasGroupingSets);
2215 
2216  /*
2217  * If the list is now empty, but hasGroupingSets is true, it's because we
2218  * elided redundant empty grouping sets. Restore a single empty grouping
2219  * set to leave a canonical form: GROUP BY ()
2220  */
2221 
2222  if (flat_grouplist == NIL && hasGroupingSets)
2223  {
2225  NIL,
2226  exprLocation((Node *) grouplist)));
2227  }
2228 
2229  foreach(gl, flat_grouplist)
2230  {
2231  Node *gexpr = (Node *) lfirst(gl);
2232 
2233  if (IsA(gexpr, GroupingSet))
2234  {
2235  GroupingSet *gset = (GroupingSet *) gexpr;
2236 
2237  switch (gset->kind)
2238  {
2239  case GROUPING_SET_EMPTY:
2240  gsets = lappend(gsets, gset);
2241  break;
2242  case GROUPING_SET_SIMPLE:
2243  /* can't happen */
2244  Assert(false);
2245  break;
2246  case GROUPING_SET_SETS:
2247  case GROUPING_SET_CUBE:
2248  case GROUPING_SET_ROLLUP:
2249  gsets = lappend(gsets,
2250  transformGroupingSet(&result,
2251  pstate, gset,
2252  targetlist, sortClause,
2253  exprKind, useSQL99, true));
2254  break;
2255  }
2256  }
2257  else
2258  {
2259  Index ref = transformGroupClauseExpr(&result, seen_local,
2260  pstate, gexpr,
2261  targetlist, sortClause,
2262  exprKind, useSQL99, true);
2263 
2264  if (ref > 0)
2265  {
2266  seen_local = bms_add_member(seen_local, ref);
2267  if (hasGroupingSets)
2268  gsets = lappend(gsets,
2270  list_make1_int(ref),
2271  exprLocation(gexpr)));
2272  }
2273  }
2274  }
2275 
2276  /* parser should prevent this */
2277  Assert(gsets == NIL || groupingSets != NULL);
2278 
2279  if (groupingSets)
2280  *groupingSets = gsets;
2281 
2282  return result;
2283 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1204
Definition: nodes.h:508
static Node * transformGroupingSet(List **flatresult, ParseState *pstate, GroupingSet *gset, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
GroupingSetKind kind
Definition: parsenodes.h:1171
#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:361
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
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 1455 of file parse_clause.c.

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

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

1457 {
1458  Node *qual;
1459 
1460  if (clause == NULL)
1461  return NULL;
1462 
1463  qual = transformExpr(pstate, clause, exprKind);
1464 
1465  qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1466 
1467  /* LIMIT can't refer to any variables of the current query */
1468  checkExprIsVarFree(pstate, qual, constructName);
1469 
1470  return qual;
1471 }
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:508
#define INT8OID
Definition: pg_type.h:304
#define NULL
Definition: c.h:226
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 2813 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().

2817 {
2818  InferClause *infer = onConflictClause->infer;
2819 
2820  *arbiterExpr = NIL;
2821  *arbiterWhere = NULL;
2822  *constraint = InvalidOid;
2823 
2824  if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
2825  ereport(ERROR,
2826  (errcode(ERRCODE_SYNTAX_ERROR),
2827  errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
2828  errhint("For example, ON CONFLICT (column_name)."),
2829  parser_errposition(pstate,
2830  exprLocation((Node *) onConflictClause))));
2831 
2832  /*
2833  * To simplify certain aspects of its design, speculative insertion into
2834  * system catalogs is disallowed
2835  */
2836  if (IsCatalogRelation(pstate->p_target_relation))
2837  ereport(ERROR,
2838  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2839  errmsg("ON CONFLICT is not supported with system catalog tables"),
2840  parser_errposition(pstate,
2841  exprLocation((Node *) onConflictClause))));
2842 
2843  /* Same applies to table used by logical decoding as catalog table */
2845  ereport(ERROR,
2846  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2847  errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
2849  parser_errposition(pstate,
2850  exprLocation((Node *) onConflictClause))));
2851 
2852  /* ON CONFLICT DO NOTHING does not require an inference clause */
2853  if (infer)
2854  {
2855  List *save_namespace;
2856 
2857  /*
2858  * While we process the arbiter expressions, accept only non-qualified
2859  * references to the target table. Hide any other relations.
2860  */
2861  save_namespace = pstate->p_namespace;
2862  pstate->p_namespace = NIL;
2863  addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
2864  false, false, true);
2865 
2866  if (infer->indexElems)
2867  *arbiterExpr = resolve_unique_index_expr(pstate, infer,
2868  pstate->p_target_relation);
2869 
2870  /*
2871  * Handling inference WHERE clause (for partial unique index
2872  * inference)
2873  */
2874  if (infer->whereClause)
2875  *arbiterWhere = transformExpr(pstate, infer->whereClause,
2877 
2878  pstate->p_namespace = save_namespace;
2879 
2880  if (infer->conname)
2882  infer->conname, false);
2883  }
2884 
2885  /*
2886  * It's convenient to form a list of expressions based on the
2887  * representation used by CREATE INDEX, since the same restrictions are
2888  * appropriate (e.g. on subqueries). However, from here on, a dedicated
2889  * primnode representation is used for inference elements, and so
2890  * assign_query_collations() can be trusted to do the right thing with the
2891  * post parse analysis query tree inference clause representation.
2892  */
2893 }
#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:1249
Definition: nodes.h:508
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:1251
#define RelationGetRelationName(relation)
Definition: rel.h:433
#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:1265
#define RelationIsUsedAsCatalogTable(relation)
Definition: rel.h:312
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:226
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
Node * whereClause
Definition: parsenodes.h:1250
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:178
OnConflictAction action
Definition: parsenodes.h:1264
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:413
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 2296 of file parse_clause.c.

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

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

2301 {
2302  List *sortlist = NIL;
2303  ListCell *olitem;
2304 
2305  foreach(olitem, orderlist)
2306  {
2307  SortBy *sortby = (SortBy *) lfirst(olitem);
2308  TargetEntry *tle;
2309 
2310  if (useSQL99)
2311  tle = findTargetlistEntrySQL99(pstate, sortby->node,
2312  targetlist, exprKind);
2313  else
2314  tle = findTargetlistEntrySQL92(pstate, sortby->node,
2315  targetlist, exprKind);
2316 
2317  sortlist = addTargetToSortList(pstate, tle,
2318  sortlist, *targetlist, sortby);
2319  }
2320 
2321  return sortlist;
2322 }
#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 1428 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().

1430 {
1431  Node *qual;
1432 
1433  if (clause == NULL)
1434  return NULL;
1435 
1436  qual = transformExpr(pstate, clause, exprKind);
1437 
1438  qual = coerce_to_boolean(pstate, qual, constructName);
1439 
1440  return qual;
1441 }
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:147
Definition: nodes.h:508
#define NULL
Definition: c.h:226
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
List* transformWindowDefinitions ( ParseState pstate,
List windowdefs,
List **  targetlist 
)

Definition at line 2329 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, WindowDef::startOffset, WindowClause::startOffset, transformFrameOffset(), transformGroupClause(), transformSortClause(), and WindowClause::winref.

Referenced by transformSelectStmt().

2332 {
2333  List *result = NIL;
2334  Index winref = 0;
2335  ListCell *lc;
2336 
2337  foreach(lc, windowdefs)
2338  {
2339  WindowDef *windef = (WindowDef *) lfirst(lc);
2340  WindowClause *refwc = NULL;
2341  List *partitionClause;
2342  List *orderClause;
2343  WindowClause *wc;
2344 
2345  winref++;
2346 
2347  /*
2348  * Check for duplicate window names.
2349  */
2350  if (windef->name &&
2351  findWindowClause(result, windef->name) != NULL)
2352  ereport(ERROR,
2353  (errcode(ERRCODE_WINDOWING_ERROR),
2354  errmsg("window \"%s\" is already defined", windef->name),
2355  parser_errposition(pstate, windef->location)));
2356 
2357  /*
2358  * If it references a previous window, look that up.
2359  */
2360  if (windef->refname)
2361  {
2362  refwc = findWindowClause(result, windef->refname);
2363  if (refwc == NULL)
2364  ereport(ERROR,
2365  (errcode(ERRCODE_UNDEFINED_OBJECT),
2366  errmsg("window \"%s\" does not exist",
2367  windef->refname),
2368  parser_errposition(pstate, windef->location)));
2369  }
2370 
2371  /*
2372  * Transform PARTITION and ORDER specs, if any. These are treated
2373  * almost exactly like top-level GROUP BY and ORDER BY clauses,
2374  * including the special handling of nondefault operator semantics.
2375  */
2376  orderClause = transformSortClause(pstate,
2377  windef->orderClause,
2378  targetlist,
2380  true /* force SQL99 rules */ );
2381  partitionClause = transformGroupClause(pstate,
2382  windef->partitionClause,
2383  NULL,
2384  targetlist,
2385  orderClause,
2387  true /* force SQL99 rules */ );
2388 
2389  /*
2390  * And prepare the new WindowClause.
2391  */
2392  wc = makeNode(WindowClause);
2393  wc->name = windef->name;
2394  wc->refname = windef->refname;
2395 
2396  /*
2397  * Per spec, a windowdef that references a previous one copies the
2398  * previous partition clause (and mustn't specify its own). It can
2399  * specify its own ordering clause, but only if the previous one had
2400  * none. It always specifies its own frame clause, and the previous
2401  * one must not have a frame clause. Yeah, it's bizarre that each of
2402  * these cases works differently, but SQL:2008 says so; see 7.11
2403  * <window clause> syntax rule 10 and general rule 1. The frame
2404  * clause rule is especially bizarre because it makes "OVER foo"
2405  * different from "OVER (foo)", and requires the latter to throw an
2406  * error if foo has a nondefault frame clause. Well, ours not to
2407  * reason why, but we do go out of our way to throw a useful error
2408  * message for such cases.
2409  */
2410  if (refwc)
2411  {
2412  if (partitionClause)
2413  ereport(ERROR,
2414  (errcode(ERRCODE_WINDOWING_ERROR),
2415  errmsg("cannot override PARTITION BY clause of window \"%s\"",
2416  windef->refname),
2417  parser_errposition(pstate, windef->location)));
2419  }
2420  else
2421  wc->partitionClause = partitionClause;
2422  if (refwc)
2423  {
2424  if (orderClause && refwc->orderClause)
2425  ereport(ERROR,
2426  (errcode(ERRCODE_WINDOWING_ERROR),
2427  errmsg("cannot override ORDER BY clause of window \"%s\"",
2428  windef->refname),
2429  parser_errposition(pstate, windef->location)));
2430  if (orderClause)
2431  {
2432  wc->orderClause = orderClause;
2433  wc->copiedOrder = false;
2434  }
2435  else
2436  {
2437  wc->orderClause = copyObject(refwc->orderClause);
2438  wc->copiedOrder = true;
2439  }
2440  }
2441  else
2442  {
2443  wc->orderClause = orderClause;
2444  wc->copiedOrder = false;
2445  }
2446  if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
2447  {
2448  /*
2449  * Use this message if this is a WINDOW clause, or if it's an OVER
2450  * clause that includes ORDER BY or framing clauses. (We already
2451  * rejected PARTITION BY above, so no need to check that.)
2452  */
2453  if (windef->name ||
2454  orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
2455  ereport(ERROR,
2456  (errcode(ERRCODE_WINDOWING_ERROR),
2457  errmsg("cannot copy window \"%s\" because it has a frame clause",
2458  windef->refname),
2459  parser_errposition(pstate, windef->location)));
2460  /* Else this clause is just OVER (foo), so say this: */
2461  ereport(ERROR,
2462  (errcode(ERRCODE_WINDOWING_ERROR),
2463  errmsg("cannot copy window \"%s\" because it has a frame clause",
2464  windef->refname),
2465  errhint("Omit the parentheses in this OVER clause."),
2466  parser_errposition(pstate, windef->location)));
2467  }
2468  wc->frameOptions = windef->frameOptions;
2469  /* Process frame offset expressions */
2470  wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
2471  windef->startOffset);
2472  wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
2473  windef->endOffset);
2474  wc->winref = winref;
2475 
2476  result = lappend(result, wc);
2477  }
2478 
2479  return result;
2480 }
List * partitionClause
Definition: parsenodes.h:480
char * refname
Definition: parsenodes.h:1194
#define NIL
Definition: pg_list.h:69
bool copiedOrder
Definition: parsenodes.h:1201
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
static Node * transformFrameOffset(ParseState *pstate, int frameOptions, Node *clause)
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
Node * startOffset
Definition: parsenodes.h:483
#define ERROR
Definition: elog.h:43
List * partitionClause
Definition: parsenodes.h:1195
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:1198
List * orderClause
Definition: parsenodes.h:481
unsigned int Index
Definition: c.h:361
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
Node * endOffset
Definition: parsenodes.h:1199
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
List * orderClause
Definition: parsenodes.h:1196
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)