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

3187 {
3188  Oid restype = exprType((Node *) tle->expr);
3189  Oid sortop;
3190  Oid eqop;
3191  bool hashable;
3192  bool reverse;
3193  int location;
3194  ParseCallbackState pcbstate;
3195 
3196  /* if tlist item is an UNKNOWN literal, change it to TEXT */
3197  if (restype == UNKNOWNOID)
3198  {
3199  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3200  restype, TEXTOID, -1,
3203  -1);
3204  restype = TEXTOID;
3205  }
3206 
3207  /*
3208  * Rather than clutter the API of get_sort_group_operators and the other
3209  * functions we're about to use, make use of error context callback to
3210  * mark any error reports with a parse position. We point to the operator
3211  * location if present, else to the expression being sorted. (NB: use the
3212  * original untransformed expression here; the TLE entry might well point
3213  * at a duplicate expression in the regular SELECT list.)
3214  */
3215  location = sortby->location;
3216  if (location < 0)
3217  location = exprLocation(sortby->node);
3218  setup_parser_errposition_callback(&pcbstate, pstate, location);
3219 
3220  /* determine the sortop, eqop, and directionality */
3221  switch (sortby->sortby_dir)
3222  {
3223  case SORTBY_DEFAULT:
3224  case SORTBY_ASC:
3225  get_sort_group_operators(restype,
3226  true, true, false,
3227  &sortop, &eqop, NULL,
3228  &hashable);
3229  reverse = false;
3230  break;
3231  case SORTBY_DESC:
3232  get_sort_group_operators(restype,
3233  false, true, true,
3234  NULL, &eqop, &sortop,
3235  &hashable);
3236  reverse = true;
3237  break;
3238  case SORTBY_USING:
3239  Assert(sortby->useOp != NIL);
3240  sortop = compatible_oper_opid(sortby->useOp,
3241  restype,
3242  restype,
3243  false);
3244 
3245  /*
3246  * Verify it's a valid ordering operator, fetch the corresponding
3247  * equality operator, and determine whether to consider it like
3248  * ASC or DESC.
3249  */
3250  eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3251  if (!OidIsValid(eqop))
3252  ereport(ERROR,
3253  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3254  errmsg("operator %s is not a valid ordering operator",
3255  strVal(llast(sortby->useOp))),
3256  errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3257 
3258  /*
3259  * Also see if the equality operator is hashable.
3260  */
3261  hashable = op_hashjoinable(eqop, restype);
3262  break;
3263  default:
3264  elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3265  sortop = InvalidOid; /* keep compiler quiet */
3266  eqop = InvalidOid;
3267  hashable = false;
3268  reverse = false;
3269  break;
3270  }
3271 
3273 
3274  /* avoid making duplicate sortlist entries */
3275  if (!targetIsInSortList(tle, sortop, sortlist))
3276  {
3278 
3279  sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3280 
3281  sortcl->eqop = eqop;
3282  sortcl->sortop = sortop;
3283  sortcl->hashable = hashable;
3284 
3285  switch (sortby->sortby_nulls)
3286  {
3287  case SORTBY_NULLS_DEFAULT:
3288  /* NULLS FIRST is default for DESC; other way for ASC */
3289  sortcl->nulls_first = reverse;
3290  break;
3291  case SORTBY_NULLS_FIRST:
3292  sortcl->nulls_first = true;
3293  break;
3294  case SORTBY_NULLS_LAST:
3295  sortcl->nulls_first = false;
3296  break;
3297  default:
3298  elog(ERROR, "unrecognized sortby_nulls: %d",
3299  sortby->sortby_nulls);
3300  break;
3301  }
3302 
3303  sortlist = lappend(sortlist, sortcl);
3304  }
3305 
3306  return sortlist;
3307 }
#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:1214
SortByDir sortby_dir
Definition: parsenodes.h:470
#define TEXTOID
Definition: pg_type.h:324
List * useOp
Definition: parsenodes.h:472
Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse)
Definition: lsyscache.c:264
#define llast(l)
Definition: pg_list.h:131
Index tleSortGroupRef
Definition: parsenodes.h:1157
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:538
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:471
#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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition: parse_oper.c:187
Expr * expr
Definition: primnodes.h:1367
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:469
int location
Definition: parsenodes.h:473
Index assignSortGroupRef ( TargetEntry tle,
List tlist 
)

Definition at line 3383 of file parse_clause.c.

References lfirst, and TargetEntry::ressortgroupref.

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

3384 {
3385  Index maxRef;
3386  ListCell *l;
3387 
3388  if (tle->ressortgroupref) /* already has one? */
3389  return tle->ressortgroupref;
3390 
3391  /* easiest way to pick an unused refnumber: max used + 1 */
3392  maxRef = 0;
3393  foreach(l, tlist)
3394  {
3395  Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3396 
3397  if (ref > maxRef)
3398  maxRef = ref;
3399  }
3400  tle->ressortgroupref = maxRef + 1;
3401  return tle->ressortgroupref;
3402 }
unsigned int Index
Definition: c.h:365
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1370
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, NULL, 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:719
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 NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:720
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, NULL, 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:1020
#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:180
static RangeTblEntry * getRTEForSpecialRelationTypes(ParseState *pstate, RangeVar *rv)
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:179
Alias * alias
Definition: primnodes.h:72
List * p_rtable
Definition: parse_node.h:169
bool targetIsInSortList ( TargetEntry tle,
Oid  sortop,
List sortList 
)

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

3425 {
3426  Index ref = tle->ressortgroupref;
3427  ListCell *l;
3428 
3429  /* no need to scan list if tle has no marker */
3430  if (ref == 0)
3431  return false;
3432 
3433  foreach(l, sortList)
3434  {
3435  SortGroupClause *scl = (SortGroupClause *) lfirst(l);
3436 
3437  if (scl->tleSortGroupRef == ref &&
3438  (sortop == InvalidOid ||
3439  sortop == scl->sortop ||
3440  sortop == get_commutator(scl->sortop)))
3441  return true;
3442  }
3443  return false;
3444 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1313
Index tleSortGroupRef
Definition: parsenodes.h:1157
unsigned int Index
Definition: c.h:365
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1370
List* transformDistinctClause ( ParseState pstate,
List **  targetlist,
List sortClause,
bool  is_agg 
)

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

2783 {
2784  List *result = NIL;
2785  ListCell *slitem;
2786  ListCell *tlitem;
2787 
2788  /*
2789  * The distinctClause should consist of all ORDER BY items followed by all
2790  * other non-resjunk targetlist items. There must not be any resjunk
2791  * ORDER BY items --- that would imply that we are sorting by a value that
2792  * isn't necessarily unique within a DISTINCT group, so the results
2793  * wouldn't be well-defined. This construction ensures we follow the rule
2794  * that sortClause and distinctClause match; in fact the sortClause will
2795  * always be a prefix of distinctClause.
2796  *
2797  * Note a corner case: the same TLE could be in the ORDER BY list multiple
2798  * times with different sortops. We have to include it in the
2799  * distinctClause the same way to preserve the prefix property. The net
2800  * effect will be that the TLE value will be made unique according to both
2801  * sortops.
2802  */
2803  foreach(slitem, sortClause)
2804  {
2805  SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
2806  TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
2807 
2808  if (tle->resjunk)
2809  ereport(ERROR,
2810  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2811  is_agg ?
2812  errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
2813  errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
2814  parser_errposition(pstate,
2815  exprLocation((Node *) tle->expr))));
2816  result = lappend(result, copyObject(scl));
2817  }
2818 
2819  /*
2820  * Now add any remaining non-resjunk tlist items, using default sort/group
2821  * semantics for their data types.
2822  */
2823  foreach(tlitem, *targetlist)
2824  {
2825  TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
2826 
2827  if (tle->resjunk)
2828  continue; /* ignore junk */
2829  result = addTargetToGroupList(pstate, tle,
2830  result, *targetlist,
2831  exprLocation((Node *) tle->expr));
2832  }
2833 
2834  /*
2835  * Complain if we found nothing to make DISTINCT. Returning an empty list
2836  * would cause the parsed Query to look like it didn't have DISTINCT, with
2837  * results that would probably surprise the user. Note: this case is
2838  * presently impossible for aggregates because of grammar restrictions,
2839  * but we check anyway.
2840  */
2841  if (result == NIL)
2842  ereport(ERROR,
2843  (errcode(ERRCODE_SYNTAX_ERROR),
2844  is_agg ?
2845  errmsg("an aggregate with DISTINCT must have at least one argument") :
2846  errmsg("SELECT DISTINCT must have at least one column")));
2847 
2848  return result;
2849 }
#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:1214
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
bool resjunk
Definition: primnodes.h:1374
#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:1367
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:621
Definition: pg_list.h:45
List* transformDistinctOnClause ( ParseState pstate,
List distinctlist,
List **  targetlist,
List sortClause 
)

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

2867 {
2868  List *result = NIL;
2869  List *sortgrouprefs = NIL;
2870  bool skipped_sortitem;
2871  ListCell *lc;
2872  ListCell *lc2;
2873 
2874  /*
2875  * Add all the DISTINCT ON expressions to the tlist (if not already
2876  * present, they are added as resjunk items). Assign sortgroupref numbers
2877  * to them, and make a list of these numbers. (NB: we rely below on the
2878  * sortgrouprefs list being one-for-one with the original distinctlist.
2879  * Also notice that we could have duplicate DISTINCT ON expressions and
2880  * hence duplicate entries in sortgrouprefs.)
2881  */
2882  foreach(lc, distinctlist)
2883  {
2884  Node *dexpr = (Node *) lfirst(lc);
2885  int sortgroupref;
2886  TargetEntry *tle;
2887 
2888  tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
2890  sortgroupref = assignSortGroupRef(tle, *targetlist);
2891  sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
2892  }
2893 
2894  /*
2895  * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
2896  * semantics from ORDER BY items that match DISTINCT ON items, and also
2897  * adopt their column sort order. We insist that the distinctClause and
2898  * sortClause match, so throw error if we find the need to add any more
2899  * distinctClause items after we've skipped an ORDER BY item that wasn't
2900  * in DISTINCT ON.
2901  */
2902  skipped_sortitem = false;
2903  foreach(lc, sortClause)
2904  {
2905  SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
2906 
2907  if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
2908  {
2909  if (skipped_sortitem)
2910  ereport(ERROR,
2911  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2912  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2913  parser_errposition(pstate,
2915  sortgrouprefs,
2916  distinctlist))));
2917  else
2918  result = lappend(result, copyObject(scl));
2919  }
2920  else
2921  skipped_sortitem = true;
2922  }
2923 
2924  /*
2925  * Now add any remaining DISTINCT ON items, using default sort/group
2926  * semantics for their data types. (Note: this is pretty questionable; if
2927  * the ORDER BY list doesn't include all the DISTINCT ON items and more
2928  * besides, you certainly aren't using DISTINCT ON in the intended way,
2929  * and you probably aren't going to get consistent results. It might be
2930  * better to throw an error or warning here. But historically we've
2931  * allowed it, so keep doing so.)
2932  */
2933  forboth(lc, distinctlist, lc2, sortgrouprefs)
2934  {
2935  Node *dexpr = (Node *) lfirst(lc);
2936  int sortgroupref = lfirst_int(lc2);
2937  TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
2938 
2939  if (targetIsInSortList(tle, InvalidOid, result))
2940  continue; /* already in list (with some semantics) */
2941  if (skipped_sortitem)
2942  ereport(ERROR,
2943  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2944  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2945  parser_errposition(pstate, exprLocation(dexpr))));
2946  result = addTargetToGroupList(pstate, tle,
2947  result, *targetlist,
2948  exprLocation(dexpr));
2949  }
2950 
2951  /*
2952  * An empty result list is impossible here because of grammar
2953  * restrictions.
2954  */
2955  Assert(result != NIL);
2956 
2957  return result;
2958 }
#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:1214
Index tleSortGroupRef
Definition: parsenodes.h:1157
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
static int get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
bool list_member_int(const List *list, int datum)
Definition: list.c:485
#define ereport(elevel, rest)
Definition: elog.h:122
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:348
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static TargetEntry * findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define copyObject(obj)
Definition: nodes.h:621
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:173
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:171
Definition: pg_list.h:45
List* transformGroupClause ( ParseState pstate,
List grouplist,
List **  groupingSets,
List **  targetlist,
List sortClause,
ParseExprKind  exprKind,
bool  useSQL99 
)

Definition at line 2477 of file parse_clause.c.

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

Referenced by transformSelectStmt(), and transformWindowDefinitions().

2480 {
2481  List *result = NIL;
2482  List *flat_grouplist;
2483  List *gsets = NIL;
2484  ListCell *gl;
2485  bool hasGroupingSets = false;
2486  Bitmapset *seen_local = NULL;
2487 
2488  /*
2489  * Recursively flatten implicit RowExprs. (Technically this is only needed
2490  * for GROUP BY, per the syntax rules for grouping sets, but we do it
2491  * anyway.)
2492  */
2493  flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
2494  true,
2495  &hasGroupingSets);
2496 
2497  /*
2498  * If the list is now empty, but hasGroupingSets is true, it's because we
2499  * elided redundant empty grouping sets. Restore a single empty grouping
2500  * set to leave a canonical form: GROUP BY ()
2501  */
2502 
2503  if (flat_grouplist == NIL && hasGroupingSets)
2504  {
2506  NIL,
2507  exprLocation((Node *) grouplist)));
2508  }
2509 
2510  foreach(gl, flat_grouplist)
2511  {
2512  Node *gexpr = (Node *) lfirst(gl);
2513 
2514  if (IsA(gexpr, GroupingSet))
2515  {
2516  GroupingSet *gset = (GroupingSet *) gexpr;
2517 
2518  switch (gset->kind)
2519  {
2520  case GROUPING_SET_EMPTY:
2521  gsets = lappend(gsets, gset);
2522  break;
2523  case GROUPING_SET_SIMPLE:
2524  /* can't happen */
2525  Assert(false);
2526  break;
2527  case GROUPING_SET_SETS:
2528  case GROUPING_SET_CUBE:
2529  case GROUPING_SET_ROLLUP:
2530  gsets = lappend(gsets,
2531  transformGroupingSet(&result,
2532  pstate, gset,
2533  targetlist, sortClause,
2534  exprKind, useSQL99, true));
2535  break;
2536  }
2537  }
2538  else
2539  {
2540  Index ref = transformGroupClauseExpr(&result, seen_local,
2541  pstate, gexpr,
2542  targetlist, sortClause,
2543  exprKind, useSQL99, true);
2544 
2545  if (ref > 0)
2546  {
2547  seen_local = bms_add_member(seen_local, ref);
2548  if (hasGroupingSets)
2549  gsets = lappend(gsets,
2551  list_make1_int(ref),
2552  exprLocation(gexpr)));
2553  }
2554  }
2555  }
2556 
2557  /* parser should prevent this */
2558  Assert(gsets == NIL || groupingSets != NULL);
2559 
2560  if (groupingSets)
2561  *groupingSets = gsets;
2562 
2563  return result;
2564 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1214
Definition: nodes.h:509
return result
Definition: formatting.c:1632
static Node * transformGroupingSet(List **flatresult, ParseState *pstate, GroupingSet *gset, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
GroupingSetKind kind
Definition: parsenodes.h:1226
#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:365
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
GroupingSet * makeGroupingSet(GroupingSetKind kind, List *content, int location)
Definition: makefuncs.c: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 1736 of file parse_clause.c.

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

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

1738 {
1739  Node *qual;
1740 
1741  if (clause == NULL)
1742  return NULL;
1743 
1744  qual = transformExpr(pstate, clause, exprKind);
1745 
1746  qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1747 
1748  /* LIMIT can't refer to any variables of the current query */
1749  checkExprIsVarFree(pstate, qual, constructName);
1750 
1751  return qual;
1752 }
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:509
#define INT8OID
Definition: pg_type.h:304
#define NULL
Definition: c.h:229
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
void transformOnConflictArbiter ( ParseState pstate,
OnConflictClause onConflictClause,
List **  arbiterExpr,
Node **  arbiterWhere,
Oid constraint 
)

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

3098 {
3099  InferClause *infer = onConflictClause->infer;
3100 
3101  *arbiterExpr = NIL;
3102  *arbiterWhere = NULL;
3103  *constraint = InvalidOid;
3104 
3105  if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
3106  ereport(ERROR,
3107  (errcode(ERRCODE_SYNTAX_ERROR),
3108  errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
3109  errhint("For example, ON CONFLICT (column_name)."),
3110  parser_errposition(pstate,
3111  exprLocation((Node *) onConflictClause))));
3112 
3113  /*
3114  * To simplify certain aspects of its design, speculative insertion into
3115  * system catalogs is disallowed
3116  */
3117  if (IsCatalogRelation(pstate->p_target_relation))
3118  ereport(ERROR,
3119  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3120  errmsg("ON CONFLICT is not supported with system catalog tables"),
3121  parser_errposition(pstate,
3122  exprLocation((Node *) onConflictClause))));
3123 
3124  /* Same applies to table used by logical decoding as catalog table */
3126  ereport(ERROR,
3127  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3128  errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
3130  parser_errposition(pstate,
3131  exprLocation((Node *) onConflictClause))));
3132 
3133  /* ON CONFLICT DO NOTHING does not require an inference clause */
3134  if (infer)
3135  {
3136  List *save_namespace;
3137 
3138  /*
3139  * While we process the arbiter expressions, accept only non-qualified
3140  * references to the target table. Hide any other relations.
3141  */
3142  save_namespace = pstate->p_namespace;
3143  pstate->p_namespace = NIL;
3144  addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
3145  false, false, true);
3146 
3147  if (infer->indexElems)
3148  *arbiterExpr = resolve_unique_index_expr(pstate, infer,
3149  pstate->p_target_relation);
3150 
3151  /*
3152  * Handling inference WHERE clause (for partial unique index
3153  * inference)
3154  */
3155  if (infer->whereClause)
3156  *arbiterWhere = transformExpr(pstate, infer->whereClause,
3158 
3159  pstate->p_namespace = save_namespace;
3160 
3161  if (infer->conname)
3163  infer->conname, false);
3164  }
3165 
3166  /*
3167  * It's convenient to form a list of expressions based on the
3168  * representation used by CREATE INDEX, since the same restrictions are
3169  * appropriate (e.g. on subqueries). However, from here on, a dedicated
3170  * primnode representation is used for inference elements, and so
3171  * assign_query_collations() can be trusted to do the right thing with the
3172  * post parse analysis query tree inference clause representation.
3173  */
3174 }
#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:1214
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:1304
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:173
char * conname
Definition: parsenodes.h:1306
#define RelationGetRelationName(relation)
Definition: rel.h:437
#define ereport(elevel, rest)
Definition: elog.h:122
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:180
InferClause * infer
Definition: parsenodes.h:1320
#define RelationIsUsedAsCatalogTable(relation)
Definition: rel.h:316
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Node * whereClause
Definition: parsenodes.h:1305
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:179
OnConflictAction action
Definition: parsenodes.h:1319
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:417
static List * resolve_unique_index_expr(ParseState *pstate, InferClause *infer, Relation heapRel)
List* transformSortClause ( ParseState pstate,
List orderlist,
List **  targetlist,
ParseExprKind  exprKind,
bool  useSQL99 
)

Definition at line 2577 of file parse_clause.c.

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

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

2582 {
2583  List *sortlist = NIL;
2584  ListCell *olitem;
2585 
2586  foreach(olitem, orderlist)
2587  {
2588  SortBy *sortby = (SortBy *) lfirst(olitem);
2589  TargetEntry *tle;
2590 
2591  if (useSQL99)
2592  tle = findTargetlistEntrySQL99(pstate, sortby->node,
2593  targetlist, exprKind);
2594  else
2595  tle = findTargetlistEntrySQL92(pstate, sortby->node,
2596  targetlist, exprKind);
2597 
2598  sortlist = addTargetToSortList(pstate, tle,
2599  sortlist, *targetlist, sortby);
2600  }
2601 
2602  return sortlist;
2603 }
#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:469
Definition: pg_list.h:45
Node* transformWhereClause ( ParseState pstate,
Node clause,
ParseExprKind  exprKind,
const char *  constructName 
)

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

1711 {
1712  Node *qual;
1713 
1714  if (clause == NULL)
1715  return NULL;
1716 
1717  qual = transformExpr(pstate, clause, exprKind);
1718 
1719  qual = coerce_to_boolean(pstate, qual, constructName);
1720 
1721  return qual;
1722 }
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:147
Definition: nodes.h:509
#define NULL
Definition: c.h:229
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
List* transformWindowDefinitions ( ParseState pstate,
List windowdefs,
List **  targetlist 
)

Definition at line 2610 of file parse_clause.c.

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

Referenced by transformSelectStmt().

2613 {
2614  List *result = NIL;
2615  Index winref = 0;
2616  ListCell *lc;
2617 
2618  foreach(lc, windowdefs)
2619  {
2620  WindowDef *windef = (WindowDef *) lfirst(lc);
2621  WindowClause *refwc = NULL;
2622  List *partitionClause;
2623  List *orderClause;
2624  WindowClause *wc;
2625 
2626  winref++;
2627 
2628  /*
2629  * Check for duplicate window names.
2630  */
2631  if (windef->name &&
2632  findWindowClause(result, windef->name) != NULL)
2633  ereport(ERROR,
2634  (errcode(ERRCODE_WINDOWING_ERROR),
2635  errmsg("window \"%s\" is already defined", windef->name),
2636  parser_errposition(pstate, windef->location)));
2637 
2638  /*
2639  * If it references a previous window, look that up.
2640  */
2641  if (windef->refname)
2642  {
2643  refwc = findWindowClause(result, windef->refname);
2644  if (refwc == NULL)
2645  ereport(ERROR,
2646  (errcode(ERRCODE_UNDEFINED_OBJECT),
2647  errmsg("window \"%s\" does not exist",
2648  windef->refname),
2649  parser_errposition(pstate, windef->location)));
2650  }
2651 
2652  /*
2653  * Transform PARTITION and ORDER specs, if any. These are treated
2654  * almost exactly like top-level GROUP BY and ORDER BY clauses,
2655  * including the special handling of nondefault operator semantics.
2656  */
2657  orderClause = transformSortClause(pstate,
2658  windef->orderClause,
2659  targetlist,
2661  true /* force SQL99 rules */ );
2662  partitionClause = transformGroupClause(pstate,
2663  windef->partitionClause,
2664  NULL,
2665  targetlist,
2666  orderClause,
2668  true /* force SQL99 rules */ );
2669 
2670  /*
2671  * And prepare the new WindowClause.
2672  */
2673  wc = makeNode(WindowClause);
2674  wc->name = windef->name;
2675  wc->refname = windef->refname;
2676 
2677  /*
2678  * Per spec, a windowdef that references a previous one copies the
2679  * previous partition clause (and mustn't specify its own). It can
2680  * specify its own ordering clause, but only if the previous one had
2681  * none. It always specifies its own frame clause, and the previous
2682  * one must not have a frame clause. Yeah, it's bizarre that each of
2683  * these cases works differently, but SQL:2008 says so; see 7.11
2684  * <window clause> syntax rule 10 and general rule 1. The frame
2685  * clause rule is especially bizarre because it makes "OVER foo"
2686  * different from "OVER (foo)", and requires the latter to throw an
2687  * error if foo has a nondefault frame clause. Well, ours not to
2688  * reason why, but we do go out of our way to throw a useful error
2689  * message for such cases.
2690  */
2691  if (refwc)
2692  {
2693  if (partitionClause)
2694  ereport(ERROR,
2695  (errcode(ERRCODE_WINDOWING_ERROR),
2696  errmsg("cannot override PARTITION BY clause of window \"%s\"",
2697  windef->refname),
2698  parser_errposition(pstate, windef->location)));
2700  }
2701  else
2702  wc->partitionClause = partitionClause;
2703  if (refwc)
2704  {
2705  if (orderClause && refwc->orderClause)
2706  ereport(ERROR,
2707  (errcode(ERRCODE_WINDOWING_ERROR),
2708  errmsg("cannot override ORDER BY clause of window \"%s\"",
2709  windef->refname),
2710  parser_errposition(pstate, windef->location)));
2711  if (orderClause)
2712  {
2713  wc->orderClause = orderClause;
2714  wc->copiedOrder = false;
2715  }
2716  else
2717  {
2718  wc->orderClause = copyObject(refwc->orderClause);
2719  wc->copiedOrder = true;
2720  }
2721  }
2722  else
2723  {
2724  wc->orderClause = orderClause;
2725  wc->copiedOrder = false;
2726  }
2727  if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
2728  {
2729  /*
2730  * Use this message if this is a WINDOW clause, or if it's an OVER
2731  * clause that includes ORDER BY or framing clauses. (We already
2732  * rejected PARTITION BY above, so no need to check that.)
2733  */
2734  if (windef->name ||
2735  orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
2736  ereport(ERROR,
2737  (errcode(ERRCODE_WINDOWING_ERROR),
2738  errmsg("cannot copy window \"%s\" because it has a frame clause",
2739  windef->refname),
2740  parser_errposition(pstate, windef->location)));
2741  /* Else this clause is just OVER (foo), so say this: */
2742  ereport(ERROR,
2743  (errcode(ERRCODE_WINDOWING_ERROR),
2744  errmsg("cannot copy window \"%s\" because it has a frame clause",
2745  windef->refname),
2746  errhint("Omit the parentheses in this OVER clause."),
2747  parser_errposition(pstate, windef->location)));
2748  }
2749  wc->frameOptions = windef->frameOptions;
2750  /* Process frame offset expressions */
2751  wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
2752  windef->startOffset);
2753  wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
2754  windef->endOffset);
2755  wc->winref = winref;
2756 
2757  result = lappend(result, wc);
2758  }
2759 
2760  return result;
2761 }
List * partitionClause
Definition: parsenodes.h:489
char * refname
Definition: parsenodes.h:1249
#define NIL
Definition: pg_list.h:69
bool copiedOrder
Definition: parsenodes.h:1256
int errhint(const char *fmt,...)
Definition: elog.c:987
#define FRAMEOPTION_DEFAULTS
Definition: parsenodes.h:525
int frameOptions
Definition: parsenodes.h:491
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
char * name
Definition: parsenodes.h:487
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
static Node * transformFrameOffset(ParseState *pstate, int frameOptions, Node *clause)
Node * startOffset
Definition: parsenodes.h:492
#define ERROR
Definition: elog.h:43
List * partitionClause
Definition: parsenodes.h:1250
int location
Definition: parsenodes.h:494
Node * endOffset
Definition: parsenodes.h:493
List * transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * startOffset
Definition: parsenodes.h:1253
List * orderClause
Definition: parsenodes.h:490
unsigned int Index
Definition: c.h:365
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Node * endOffset
Definition: parsenodes.h:1254
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
List * orderClause
Definition: parsenodes.h:1251
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * refname
Definition: parsenodes.h:488
#define copyObject(obj)
Definition: nodes.h:621
Definition: pg_list.h:45
static WindowClause * findWindowClause(List *wclist, const char *name)