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 3211 of file parse_clause.c.

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

Referenced by transformAggregateCall(), and transformSortClause().

3213 {
3214  Oid restype = exprType((Node *) tle->expr);
3215  Oid sortop;
3216  Oid eqop;
3217  bool hashable;
3218  bool reverse;
3219  int location;
3220  ParseCallbackState pcbstate;
3221 
3222  /* if tlist item is an UNKNOWN literal, change it to TEXT */
3223  if (restype == UNKNOWNOID)
3224  {
3225  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3226  restype, TEXTOID, -1,
3229  -1);
3230  restype = TEXTOID;
3231  }
3232 
3233  /*
3234  * Rather than clutter the API of get_sort_group_operators and the other
3235  * functions we're about to use, make use of error context callback to
3236  * mark any error reports with a parse position. We point to the operator
3237  * location if present, else to the expression being sorted. (NB: use the
3238  * original untransformed expression here; the TLE entry might well point
3239  * at a duplicate expression in the regular SELECT list.)
3240  */
3241  location = sortby->location;
3242  if (location < 0)
3243  location = exprLocation(sortby->node);
3244  setup_parser_errposition_callback(&pcbstate, pstate, location);
3245 
3246  /* determine the sortop, eqop, and directionality */
3247  switch (sortby->sortby_dir)
3248  {
3249  case SORTBY_DEFAULT:
3250  case SORTBY_ASC:
3251  get_sort_group_operators(restype,
3252  true, true, false,
3253  &sortop, &eqop, NULL,
3254  &hashable);
3255  reverse = false;
3256  break;
3257  case SORTBY_DESC:
3258  get_sort_group_operators(restype,
3259  false, true, true,
3260  NULL, &eqop, &sortop,
3261  &hashable);
3262  reverse = true;
3263  break;
3264  case SORTBY_USING:
3265  Assert(sortby->useOp != NIL);
3266  sortop = compatible_oper_opid(sortby->useOp,
3267  restype,
3268  restype,
3269  false);
3270 
3271  /*
3272  * Verify it's a valid ordering operator, fetch the corresponding
3273  * equality operator, and determine whether to consider it like
3274  * ASC or DESC.
3275  */
3276  eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3277  if (!OidIsValid(eqop))
3278  ereport(ERROR,
3279  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3280  errmsg("operator %s is not a valid ordering operator",
3281  strVal(llast(sortby->useOp))),
3282  errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3283 
3284  /*
3285  * Also see if the equality operator is hashable.
3286  */
3287  hashable = op_hashjoinable(eqop, restype);
3288  break;
3289  default:
3290  elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3291  sortop = InvalidOid; /* keep compiler quiet */
3292  eqop = InvalidOid;
3293  hashable = false;
3294  reverse = false;
3295  break;
3296  }
3297 
3299 
3300  /* avoid making duplicate sortlist entries */
3301  if (!targetIsInSortList(tle, sortop, sortlist))
3302  {
3304 
3305  sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3306 
3307  sortcl->eqop = eqop;
3308  sortcl->sortop = sortop;
3309  sortcl->hashable = hashable;
3310 
3311  switch (sortby->sortby_nulls)
3312  {
3313  case SORTBY_NULLS_DEFAULT:
3314  /* NULLS FIRST is default for DESC; other way for ASC */
3315  sortcl->nulls_first = reverse;
3316  break;
3317  case SORTBY_NULLS_FIRST:
3318  sortcl->nulls_first = true;
3319  break;
3320  case SORTBY_NULLS_LAST:
3321  sortcl->nulls_first = false;
3322  break;
3323  default:
3324  elog(ERROR, "unrecognized sortby_nulls: %d",
3325  sortby->sortby_nulls);
3326  break;
3327  }
3328 
3329  sortlist = lappend(sortlist, sortcl);
3330  }
3331 
3332  return sortlist;
3333 }
#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:1186
SortByDir sortby_dir
Definition: parsenodes.h:469
#define TEXTOID
Definition: pg_type.h:324
List * useOp
Definition: parsenodes.h:471
Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse)
Definition: lsyscache.c:264
#define llast(l)
Definition: pg_list.h:131
Index tleSortGroupRef
Definition: parsenodes.h:1190
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:509
#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:532
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:161
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1246
#define ERROR
Definition: elog.h:43
SortByNulls sortby_nulls
Definition: parsenodes.h:470
#define ereport(elevel, rest)
Definition: elog.h:122
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
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:557
#define Assert(condition)
Definition: c.h:664
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:1368
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define UNKNOWNOID
Definition: pg_type.h:431
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:468
int location
Definition: parsenodes.h:472
Index assignSortGroupRef ( TargetEntry tle,
List tlist 
)

Definition at line 3409 of file parse_clause.c.

References lfirst, and TargetEntry::ressortgroupref.

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

3410 {
3411  Index maxRef;
3412  ListCell *l;
3413 
3414  if (tle->ressortgroupref) /* already has one? */
3415  return tle->ressortgroupref;
3416 
3417  /* easiest way to pick an unused refnumber: max used + 1 */
3418  maxRef = 0;
3419  foreach(l, tlist)
3420  {
3421  Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3422 
3423  if (ref > maxRef)
3424  maxRef = ref;
3425  }
3426  tle->ressortgroupref = maxRef + 1;
3427  return tle->ressortgroupref;
3428 }
unsigned int Index
Definition: c.h:359
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1371
bool interpretOidsOption ( List defList,
bool  allowOids 
)

Definition at line 255 of file parse_clause.c.

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

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

256 {
257  ListCell *cell;
258 
259  /* Scan list to see if OIDS was included */
260  foreach(cell, defList)
261  {
262  DefElem *def = (DefElem *) lfirst(cell);
263 
264  if (def->defnamespace == NULL &&
265  pg_strcasecmp(def->defname, "oids") == 0)
266  {
267  if (!allowOids)
268  ereport(ERROR,
269  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
270  errmsg("unrecognized parameter \"%s\"",
271  def->defname)));
272  return defGetBoolean(def);
273  }
274  }
275 
276  /* Force no-OIDS result if caller disallows OIDS. */
277  if (!allowOids)
278  return false;
279 
280  /* OIDS option was not specified, so use default. */
281  return default_with_oids;
282 }
char * defnamespace
Definition: parsenodes.h:718
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:448
bool defGetBoolean(DefElem *def)
Definition: define.c:111
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define lfirst(lc)
Definition: pg_list.h:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:719
int setTargetTable ( ParseState pstate,
RangeVar relation,
bool  inh,
bool  alsoSource,
AclMode  requiredPerms 
)

Definition at line 181 of file parse_clause.c.

References addRangeTableEntryForRelation(), addRTEtoQuery(), RangeVar::alias, Assert, ereport, errcode(), errmsg(), ERROR, getRTEForSpecialRelationTypes(), heap_close, list_length(), NoLock, ParseState::p_rtable, ParseState::p_target_rangetblentry, ParseState::p_target_relation, parserOpenTable(), RangeVar::relname, RangeTblEntry::requiredPerms, RowExclusiveLock, and rt_fetch.

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

183 {
184  RangeTblEntry *rte;
185  int rtindex;
186 
187  /* So far special relations are immutable; so they cannot be targets. */
188  rte = getRTEForSpecialRelationTypes(pstate, relation);
189  if (rte != NULL)
190  ereport(ERROR,
191  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
192  errmsg("relation \"%s\" cannot be the target of a modifying statement",
193  relation->relname)));
194 
195  /* Close old target; this could only happen for multi-action rules */
196  if (pstate->p_target_relation != NULL)
198 
199  /*
200  * Open target rel and grab suitable lock (which we will hold till end of
201  * transaction).
202  *
203  * free_parsestate() will eventually do the corresponding heap_close(),
204  * but *not* release the lock.
205  */
206  pstate->p_target_relation = parserOpenTable(pstate, relation,
208 
209  /*
210  * Now build an RTE.
211  */
212  rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
213  relation->alias, inh, false);
214  pstate->p_target_rangetblentry = rte;
215 
216  /* assume new rte is at end */
217  rtindex = list_length(pstate->p_rtable);
218  Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
219 
220  /*
221  * Override addRangeTableEntry's default ACL_SELECT permissions check, and
222  * instead mark target table as requiring exactly the specified
223  * permissions.
224  *
225  * If we find an explicit reference to the rel later during parse
226  * analysis, we will add the ACL_SELECT bit back again; see
227  * markVarForSelectPriv and its callers.
228  */
229  rte->requiredPerms = requiredPerms;
230 
231  /*
232  * If UPDATE/DELETE, add table to joinlist and namespace.
233  *
234  * Note: some callers know that they can find the new ParseNamespaceItem
235  * at the end of the pstate->p_namespace list. This is a bit ugly but not
236  * worth complicating this function's signature for.
237  */
238  if (alsoSource)
239  addRTEtoQuery(pstate, rte, true, true, true);
240 
241  return rtindex;
242 }
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
int errcode(int sqlerrcode)
Definition: elog.c:575
AclMode requiredPerms
Definition: parsenodes.h:1053
#define heap_close(r, l)
Definition: heapam.h:97
char * relname
Definition: primnodes.h:68
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
#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:183
static RangeTblEntry * getRTEForSpecialRelationTypes(ParseState *pstate, RangeVar *rv)
#define Assert(condition)
Definition: c.h:664
static int list_length(const List *l)
Definition: pg_list.h:89
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:182
Alias * alias
Definition: primnodes.h:72
List * p_rtable
Definition: parse_node.h:172
bool targetIsInSortList ( TargetEntry tle,
Oid  sortop,
List sortList 
)

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

3451 {
3452  Index ref = tle->ressortgroupref;
3453  ListCell *l;
3454 
3455  /* no need to scan list if tle has no marker */
3456  if (ref == 0)
3457  return false;
3458 
3459  foreach(l, sortList)
3460  {
3461  SortGroupClause *scl = (SortGroupClause *) lfirst(l);
3462 
3463  if (scl->tleSortGroupRef == ref &&
3464  (sortop == InvalidOid ||
3465  sortop == scl->sortop ||
3466  sortop == get_commutator(scl->sortop)))
3467  return true;
3468  }
3469  return false;
3470 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1313
Index tleSortGroupRef
Definition: parsenodes.h:1190
unsigned int Index
Definition: c.h:359
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1371
List* transformDistinctClause ( ParseState pstate,
List **  targetlist,
List sortClause,
bool  is_agg 
)

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

2809 {
2810  List *result = NIL;
2811  ListCell *slitem;
2812  ListCell *tlitem;
2813 
2814  /*
2815  * The distinctClause should consist of all ORDER BY items followed by all
2816  * other non-resjunk targetlist items. There must not be any resjunk
2817  * ORDER BY items --- that would imply that we are sorting by a value that
2818  * isn't necessarily unique within a DISTINCT group, so the results
2819  * wouldn't be well-defined. This construction ensures we follow the rule
2820  * that sortClause and distinctClause match; in fact the sortClause will
2821  * always be a prefix of distinctClause.
2822  *
2823  * Note a corner case: the same TLE could be in the ORDER BY list multiple
2824  * times with different sortops. We have to include it in the
2825  * distinctClause the same way to preserve the prefix property. The net
2826  * effect will be that the TLE value will be made unique according to both
2827  * sortops.
2828  */
2829  foreach(slitem, sortClause)
2830  {
2831  SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
2832  TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
2833 
2834  if (tle->resjunk)
2835  ereport(ERROR,
2836  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2837  is_agg ?
2838  errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
2839  errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
2840  parser_errposition(pstate,
2841  exprLocation((Node *) tle->expr))));
2842  result = lappend(result, copyObject(scl));
2843  }
2844 
2845  /*
2846  * Now add any remaining non-resjunk tlist items, using default sort/group
2847  * semantics for their data types.
2848  */
2849  foreach(tlitem, *targetlist)
2850  {
2851  TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
2852 
2853  if (tle->resjunk)
2854  continue; /* ignore junk */
2855  result = addTargetToGroupList(pstate, tle,
2856  result, *targetlist,
2857  exprLocation((Node *) tle->expr));
2858  }
2859 
2860  /*
2861  * Complain if we found nothing to make DISTINCT. Returning an empty list
2862  * would cause the parsed Query to look like it didn't have DISTINCT, with
2863  * results that would probably surprise the user. Note: this case is
2864  * presently impossible for aggregates because of grammar restrictions,
2865  * but we check anyway.
2866  */
2867  if (result == NIL)
2868  ereport(ERROR,
2869  (errcode(ERRCODE_SYNTAX_ERROR),
2870  is_agg ?
2871  errmsg("an aggregate with DISTINCT must have at least one argument") :
2872  errmsg("SELECT DISTINCT must have at least one column")));
2873 
2874  return result;
2875 }
#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:1186
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
bool resjunk
Definition: primnodes.h:1375
#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:1368
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define copyObject(obj)
Definition: nodes.h:622
Definition: pg_list.h:45
List* transformDistinctOnClause ( ParseState pstate,
List distinctlist,
List **  targetlist,
List sortClause 
)

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

2893 {
2894  List *result = NIL;
2895  List *sortgrouprefs = NIL;
2896  bool skipped_sortitem;
2897  ListCell *lc;
2898  ListCell *lc2;
2899 
2900  /*
2901  * Add all the DISTINCT ON expressions to the tlist (if not already
2902  * present, they are added as resjunk items). Assign sortgroupref numbers
2903  * to them, and make a list of these numbers. (NB: we rely below on the
2904  * sortgrouprefs list being one-for-one with the original distinctlist.
2905  * Also notice that we could have duplicate DISTINCT ON expressions and
2906  * hence duplicate entries in sortgrouprefs.)
2907  */
2908  foreach(lc, distinctlist)
2909  {
2910  Node *dexpr = (Node *) lfirst(lc);
2911  int sortgroupref;
2912  TargetEntry *tle;
2913 
2914  tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
2916  sortgroupref = assignSortGroupRef(tle, *targetlist);
2917  sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
2918  }
2919 
2920  /*
2921  * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
2922  * semantics from ORDER BY items that match DISTINCT ON items, and also
2923  * adopt their column sort order. We insist that the distinctClause and
2924  * sortClause match, so throw error if we find the need to add any more
2925  * distinctClause items after we've skipped an ORDER BY item that wasn't
2926  * in DISTINCT ON.
2927  */
2928  skipped_sortitem = false;
2929  foreach(lc, sortClause)
2930  {
2931  SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
2932 
2933  if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
2934  {
2935  if (skipped_sortitem)
2936  ereport(ERROR,
2937  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2938  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2939  parser_errposition(pstate,
2941  sortgrouprefs,
2942  distinctlist))));
2943  else
2944  result = lappend(result, copyObject(scl));
2945  }
2946  else
2947  skipped_sortitem = true;
2948  }
2949 
2950  /*
2951  * Now add any remaining DISTINCT ON items, using default sort/group
2952  * semantics for their data types. (Note: this is pretty questionable; if
2953  * the ORDER BY list doesn't include all the DISTINCT ON items and more
2954  * besides, you certainly aren't using DISTINCT ON in the intended way,
2955  * and you probably aren't going to get consistent results. It might be
2956  * better to throw an error or warning here. But historically we've
2957  * allowed it, so keep doing so.)
2958  */
2959  forboth(lc, distinctlist, lc2, sortgrouprefs)
2960  {
2961  Node *dexpr = (Node *) lfirst(lc);
2962  int sortgroupref = lfirst_int(lc2);
2963  TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
2964 
2965  if (targetIsInSortList(tle, InvalidOid, result))
2966  continue; /* already in list (with some semantics) */
2967  if (skipped_sortitem)
2968  ereport(ERROR,
2969  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2970  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2971  parser_errposition(pstate, exprLocation(dexpr))));
2972  result = addTargetToGroupList(pstate, tle,
2973  result, *targetlist,
2974  exprLocation(dexpr));
2975  }
2976 
2977  /*
2978  * An empty result list is impossible here because of grammar
2979  * restrictions.
2980  */
2981  Assert(result != NIL);
2982 
2983  return result;
2984 }
#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:180
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Index tleSortGroupRef
Definition: parsenodes.h:1190
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
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:664
#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:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define copyObject(obj)
Definition: nodes.h:622
Definition: pg_list.h:45
void transformFromClause ( ParseState pstate,
List frmList 
)

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

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

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

Referenced by transformSelectStmt(), and transformWindowDefinitions().

2506 {
2507  List *result = NIL;
2508  List *flat_grouplist;
2509  List *gsets = NIL;
2510  ListCell *gl;
2511  bool hasGroupingSets = false;
2512  Bitmapset *seen_local = NULL;
2513 
2514  /*
2515  * Recursively flatten implicit RowExprs. (Technically this is only needed
2516  * for GROUP BY, per the syntax rules for grouping sets, but we do it
2517  * anyway.)
2518  */
2519  flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
2520  true,
2521  &hasGroupingSets);
2522 
2523  /*
2524  * If the list is now empty, but hasGroupingSets is true, it's because we
2525  * elided redundant empty grouping sets. Restore a single empty grouping
2526  * set to leave a canonical form: GROUP BY ()
2527  */
2528 
2529  if (flat_grouplist == NIL && hasGroupingSets)
2530  {
2532  NIL,
2533  exprLocation((Node *) grouplist)));
2534  }
2535 
2536  foreach(gl, flat_grouplist)
2537  {
2538  Node *gexpr = (Node *) lfirst(gl);
2539 
2540  if (IsA(gexpr, GroupingSet))
2541  {
2542  GroupingSet *gset = (GroupingSet *) gexpr;
2543 
2544  switch (gset->kind)
2545  {
2546  case GROUPING_SET_EMPTY:
2547  gsets = lappend(gsets, gset);
2548  break;
2549  case GROUPING_SET_SIMPLE:
2550  /* can't happen */
2551  Assert(false);
2552  break;
2553  case GROUPING_SET_SETS:
2554  case GROUPING_SET_CUBE:
2555  case GROUPING_SET_ROLLUP:
2556  gsets = lappend(gsets,
2557  transformGroupingSet(&result,
2558  pstate, gset,
2559  targetlist, sortClause,
2560  exprKind, useSQL99, true));
2561  break;
2562  }
2563  }
2564  else
2565  {
2566  Index ref = transformGroupClauseExpr(&result, seen_local,
2567  pstate, gexpr,
2568  targetlist, sortClause,
2569  exprKind, useSQL99, true);
2570 
2571  if (ref > 0)
2572  {
2573  seen_local = bms_add_member(seen_local, ref);
2574  if (hasGroupingSets)
2575  gsets = lappend(gsets,
2577  list_make1_int(ref),
2578  exprLocation(gexpr)));
2579  }
2580  }
2581  }
2582 
2583  /* parser should prevent this */
2584  Assert(gsets == NIL || groupingSets != NULL);
2585 
2586  if (groupingSets)
2587  *groupingSets = gsets;
2588 
2589  return result;
2590 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Definition: nodes.h:509
return result
Definition: formatting.c:1633
static Node * transformGroupingSet(List **flatresult, ParseState *pstate, GroupingSet *gset, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
GroupingSetKind kind
Definition: parsenodes.h:1259
#define list_make1(x1)
Definition: pg_list.h:139
static Node * flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
#define list_make1_int(x1)
Definition: pg_list.h:145
List * lappend(List *list, void *datum)
Definition: list.c:128
unsigned int Index
Definition: c.h:359
#define Assert(condition)
Definition: c.h:664
#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:605
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 1762 of file parse_clause.c.

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

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

1764 {
1765  Node *qual;
1766 
1767  if (clause == NULL)
1768  return NULL;
1769 
1770  qual = transformExpr(pstate, clause, exprKind);
1771 
1772  qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1773 
1774  /* LIMIT can't refer to any variables of the current query */
1775  checkExprIsVarFree(pstate, qual, constructName);
1776 
1777  return qual;
1778 }
static void checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
Definition: nodes.h:509
#define INT8OID
Definition: pg_type.h:304
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 3120 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, 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().

3124 {
3125  InferClause *infer = onConflictClause->infer;
3126 
3127  *arbiterExpr = NIL;
3128  *arbiterWhere = NULL;
3129  *constraint = InvalidOid;
3130 
3131  if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
3132  ereport(ERROR,
3133  (errcode(ERRCODE_SYNTAX_ERROR),
3134  errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
3135  errhint("For example, ON CONFLICT (column_name)."),
3136  parser_errposition(pstate,
3137  exprLocation((Node *) onConflictClause))));
3138 
3139  /*
3140  * To simplify certain aspects of its design, speculative insertion into
3141  * system catalogs is disallowed
3142  */
3143  if (IsCatalogRelation(pstate->p_target_relation))
3144  ereport(ERROR,
3145  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3146  errmsg("ON CONFLICT is not supported with system catalog tables"),
3147  parser_errposition(pstate,
3148  exprLocation((Node *) onConflictClause))));
3149 
3150  /* Same applies to table used by logical decoding as catalog table */
3152  ereport(ERROR,
3153  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3154  errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
3156  parser_errposition(pstate,
3157  exprLocation((Node *) onConflictClause))));
3158 
3159  /* ON CONFLICT DO NOTHING does not require an inference clause */
3160  if (infer)
3161  {
3162  List *save_namespace;
3163 
3164  /*
3165  * While we process the arbiter expressions, accept only non-qualified
3166  * references to the target table. Hide any other relations.
3167  */
3168  save_namespace = pstate->p_namespace;
3169  pstate->p_namespace = NIL;
3170  addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
3171  false, false, true);
3172 
3173  if (infer->indexElems)
3174  *arbiterExpr = resolve_unique_index_expr(pstate, infer,
3175  pstate->p_target_relation);
3176 
3177  /*
3178  * Handling inference WHERE clause (for partial unique index
3179  * inference)
3180  */
3181  if (infer->whereClause)
3182  *arbiterWhere = transformExpr(pstate, infer->whereClause,
3184 
3185  pstate->p_namespace = save_namespace;
3186 
3187  if (infer->conname)
3189  infer->conname, false);
3190  }
3191 
3192  /*
3193  * It's convenient to form a list of expressions based on the
3194  * representation used by CREATE INDEX, since the same restrictions are
3195  * appropriate (e.g. on subqueries). However, from here on, a dedicated
3196  * primnode representation is used for inference elements, and so
3197  * assign_query_collations() can be trusted to do the right thing with the
3198  * post parse analysis query tree inference clause representation.
3199  */
3200 }
#define NIL
Definition: pg_list.h:69
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:92
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
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:146
List * indexElems
Definition: parsenodes.h:1337
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
List * p_namespace
Definition: parse_node.h:176
char * conname
Definition: parsenodes.h:1339
#define RelationGetRelationName(relation)
Definition: rel.h:436
#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:183
InferClause * infer
Definition: parsenodes.h:1353
#define RelationIsUsedAsCatalogTable(relation)
Definition: rel.h:315
#define InvalidOid
Definition: postgres_ext.h:36
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Node * whereClause
Definition: parsenodes.h:1338
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:182
OnConflictAction action
Definition: parsenodes.h:1352
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:416
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 2603 of file parse_clause.c.

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

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

2608 {
2609  List *sortlist = NIL;
2610  ListCell *olitem;
2611 
2612  foreach(olitem, orderlist)
2613  {
2614  SortBy *sortby = (SortBy *) lfirst(olitem);
2615  TargetEntry *tle;
2616 
2617  if (useSQL99)
2618  tle = findTargetlistEntrySQL99(pstate, sortby->node,
2619  targetlist, exprKind);
2620  else
2621  tle = findTargetlistEntrySQL92(pstate, sortby->node,
2622  targetlist, exprKind);
2623 
2624  sortlist = addTargetToSortList(pstate, tle,
2625  sortlist, *targetlist, sortby);
2626  }
2627 
2628  return sortlist;
2629 }
#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:468
Definition: pg_list.h:45
Node* transformWhereClause ( ParseState pstate,
Node clause,
ParseExprKind  exprKind,
const char *  constructName 
)

Definition at line 1735 of file parse_clause.c.

References coerce_to_boolean(), and transformExpr().

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

1737 {
1738  Node *qual;
1739 
1740  if (clause == NULL)
1741  return NULL;
1742 
1743  qual = transformExpr(pstate, clause, exprKind);
1744 
1745  qual = coerce_to_boolean(pstate, qual, constructName);
1746 
1747  return qual;
1748 }
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
Definition: nodes.h:509
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
List* transformWindowDefinitions ( ParseState pstate,
List windowdefs,
List **  targetlist 
)

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

2639 {
2640  List *result = NIL;
2641  Index winref = 0;
2642  ListCell *lc;
2643 
2644  foreach(lc, windowdefs)
2645  {
2646  WindowDef *windef = (WindowDef *) lfirst(lc);
2647  WindowClause *refwc = NULL;
2648  List *partitionClause;
2649  List *orderClause;
2650  WindowClause *wc;
2651 
2652  winref++;
2653 
2654  /*
2655  * Check for duplicate window names.
2656  */
2657  if (windef->name &&
2658  findWindowClause(result, windef->name) != NULL)
2659  ereport(ERROR,
2660  (errcode(ERRCODE_WINDOWING_ERROR),
2661  errmsg("window \"%s\" is already defined", windef->name),
2662  parser_errposition(pstate, windef->location)));
2663 
2664  /*
2665  * If it references a previous window, look that up.
2666  */
2667  if (windef->refname)
2668  {
2669  refwc = findWindowClause(result, windef->refname);
2670  if (refwc == NULL)
2671  ereport(ERROR,
2672  (errcode(ERRCODE_UNDEFINED_OBJECT),
2673  errmsg("window \"%s\" does not exist",
2674  windef->refname),
2675  parser_errposition(pstate, windef->location)));
2676  }
2677 
2678  /*
2679  * Transform PARTITION and ORDER specs, if any. These are treated
2680  * almost exactly like top-level GROUP BY and ORDER BY clauses,
2681  * including the special handling of nondefault operator semantics.
2682  */
2683  orderClause = transformSortClause(pstate,
2684  windef->orderClause,
2685  targetlist,
2687  true /* force SQL99 rules */ );
2688  partitionClause = transformGroupClause(pstate,
2689  windef->partitionClause,
2690  NULL,
2691  targetlist,
2692  orderClause,
2694  true /* force SQL99 rules */ );
2695 
2696  /*
2697  * And prepare the new WindowClause.
2698  */
2699  wc = makeNode(WindowClause);
2700  wc->name = windef->name;
2701  wc->refname = windef->refname;
2702 
2703  /*
2704  * Per spec, a windowdef that references a previous one copies the
2705  * previous partition clause (and mustn't specify its own). It can
2706  * specify its own ordering clause, but only if the previous one had
2707  * none. It always specifies its own frame clause, and the previous
2708  * one must not have a frame clause. Yeah, it's bizarre that each of
2709  * these cases works differently, but SQL:2008 says so; see 7.11
2710  * <window clause> syntax rule 10 and general rule 1. The frame
2711  * clause rule is especially bizarre because it makes "OVER foo"
2712  * different from "OVER (foo)", and requires the latter to throw an
2713  * error if foo has a nondefault frame clause. Well, ours not to
2714  * reason why, but we do go out of our way to throw a useful error
2715  * message for such cases.
2716  */
2717  if (refwc)
2718  {
2719  if (partitionClause)
2720  ereport(ERROR,
2721  (errcode(ERRCODE_WINDOWING_ERROR),
2722  errmsg("cannot override PARTITION BY clause of window \"%s\"",
2723  windef->refname),
2724  parser_errposition(pstate, windef->location)));
2726  }
2727  else
2728  wc->partitionClause = partitionClause;
2729  if (refwc)
2730  {
2731  if (orderClause && refwc->orderClause)
2732  ereport(ERROR,
2733  (errcode(ERRCODE_WINDOWING_ERROR),
2734  errmsg("cannot override ORDER BY clause of window \"%s\"",
2735  windef->refname),
2736  parser_errposition(pstate, windef->location)));
2737  if (orderClause)
2738  {
2739  wc->orderClause = orderClause;
2740  wc->copiedOrder = false;
2741  }
2742  else
2743  {
2744  wc->orderClause = copyObject(refwc->orderClause);
2745  wc->copiedOrder = true;
2746  }
2747  }
2748  else
2749  {
2750  wc->orderClause = orderClause;
2751  wc->copiedOrder = false;
2752  }
2753  if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
2754  {
2755  /*
2756  * Use this message if this is a WINDOW clause, or if it's an OVER
2757  * clause that includes ORDER BY or framing clauses. (We already
2758  * rejected PARTITION BY above, so no need to check that.)
2759  */
2760  if (windef->name ||
2761  orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
2762  ereport(ERROR,
2763  (errcode(ERRCODE_WINDOWING_ERROR),
2764  errmsg("cannot copy window \"%s\" because it has a frame clause",
2765  windef->refname),
2766  parser_errposition(pstate, windef->location)));
2767  /* Else this clause is just OVER (foo), so say this: */
2768  ereport(ERROR,
2769  (errcode(ERRCODE_WINDOWING_ERROR),
2770  errmsg("cannot copy window \"%s\" because it has a frame clause",
2771  windef->refname),
2772  errhint("Omit the parentheses in this OVER clause."),
2773  parser_errposition(pstate, windef->location)));
2774  }
2775  wc->frameOptions = windef->frameOptions;
2776  /* Process frame offset expressions */
2777  wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
2778  windef->startOffset);
2779  wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
2780  windef->endOffset);
2781  wc->winref = winref;
2782 
2783  result = lappend(result, wc);
2784  }
2785 
2786  return result;
2787 }
List * partitionClause
Definition: parsenodes.h:488
char * refname
Definition: parsenodes.h:1282
#define NIL
Definition: pg_list.h:69
bool copiedOrder
Definition: parsenodes.h:1289
int errhint(const char *fmt,...)
Definition: elog.c:987
#define FRAMEOPTION_DEFAULTS
Definition: parsenodes.h:524
int frameOptions
Definition: parsenodes.h:490
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
char * name
Definition: parsenodes.h:486
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
static Node * transformFrameOffset(ParseState *pstate, int frameOptions, Node *clause)
Node * startOffset
Definition: parsenodes.h:491
#define ERROR
Definition: elog.h:43
List * partitionClause
Definition: parsenodes.h:1283
int location
Definition: parsenodes.h:493
Node * endOffset
Definition: parsenodes.h:492
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:1286
List * orderClause
Definition: parsenodes.h:489
unsigned int Index
Definition: c.h:359
#define makeNode(_type_)
Definition: nodes.h:557
#define lfirst(lc)
Definition: pg_list.h:106
Node * endOffset
Definition: parsenodes.h:1287
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
List * orderClause
Definition: parsenodes.h:1284
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * refname
Definition: parsenodes.h:487
#define copyObject(obj)
Definition: nodes.h:622
Definition: pg_list.h:45
static WindowClause * findWindowClause(List *wclist, const char *name)