PostgreSQL Source Code git master
Loading...
Searching...
No Matches
parse_clause.h File Reference
Include dependency graph for parse_clause.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void transformFromClause (ParseState *pstate, List *frmList)
 
int setTargetTable (ParseState *pstate, RangeVar *relation, bool inh, bool alsoSource, AclMode requiredPerms)
 
NodetransformWhereClause (ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
 
NodetransformLimitClause (ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName, LimitOption limitOption)
 
ListtransformGroupClause (ParseState *pstate, List *grouplist, bool groupByAll, 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)
 
ParseNamespaceItemtransformJsonTable (ParseState *pstate, JsonTable *jt)
 

Function Documentation

◆ addTargetToSortList()

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

Definition at line 3605 of file parse_clause.c.

3607{
3608 Oid restype = exprType((Node *) tle->expr);
3609 Oid sortop;
3610 Oid eqop;
3611 bool hashable;
3612 bool reverse;
3613 int location;
3615
3616 /* if tlist item is an UNKNOWN literal, change it to TEXT */
3617 if (restype == UNKNOWNOID)
3618 {
3619 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3620 restype, TEXTOID, -1,
3623 -1);
3624 restype = TEXTOID;
3625 }
3626
3627 /*
3628 * Rather than clutter the API of get_sort_group_operators and the other
3629 * functions we're about to use, make use of error context callback to
3630 * mark any error reports with a parse position. We point to the operator
3631 * location if present, else to the expression being sorted. (NB: use the
3632 * original untransformed expression here; the TLE entry might well point
3633 * at a duplicate expression in the regular SELECT list.)
3634 */
3635 location = sortby->location;
3636 if (location < 0)
3637 location = exprLocation(sortby->node);
3638 setup_parser_errposition_callback(&pcbstate, pstate, location);
3639
3640 /* determine the sortop, eqop, and directionality */
3641 switch (sortby->sortby_dir)
3642 {
3643 case SORTBY_DEFAULT:
3644 case SORTBY_ASC:
3646 true, true, false,
3647 &sortop, &eqop, NULL,
3648 &hashable);
3649 reverse = false;
3650 break;
3651 case SORTBY_DESC:
3653 false, true, true,
3654 NULL, &eqop, &sortop,
3655 &hashable);
3656 reverse = true;
3657 break;
3658 case SORTBY_USING:
3659 Assert(sortby->useOp != NIL);
3660 sortop = compatible_oper_opid(sortby->useOp,
3661 restype,
3662 restype,
3663 false);
3664
3665 /*
3666 * Verify it's a valid ordering operator, fetch the corresponding
3667 * equality operator, and determine whether to consider it like
3668 * ASC or DESC.
3669 */
3670 eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3671 if (!OidIsValid(eqop))
3672 ereport(ERROR,
3674 errmsg("operator %s is not a valid ordering operator",
3675 strVal(llast(sortby->useOp))),
3676 errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3677
3678 /*
3679 * Also see if the equality operator is hashable.
3680 */
3681 hashable = op_hashjoinable(eqop, restype);
3682 break;
3683 default:
3684 elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3685 sortop = InvalidOid; /* keep compiler quiet */
3686 eqop = InvalidOid;
3687 hashable = false;
3688 reverse = false;
3689 break;
3690 }
3691
3693
3694 /* avoid making duplicate sortlist entries */
3695 if (!targetIsInSortList(tle, sortop, sortlist))
3696 {
3698
3699 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3700
3701 sortcl->eqop = eqop;
3702 sortcl->sortop = sortop;
3703 sortcl->hashable = hashable;
3704 sortcl->reverse_sort = reverse;
3705
3706 switch (sortby->sortby_nulls)
3707 {
3709 /* NULLS FIRST is default for DESC; other way for ASC */
3710 sortcl->nulls_first = reverse;
3711 break;
3712 case SORTBY_NULLS_FIRST:
3713 sortcl->nulls_first = true;
3714 break;
3715 case SORTBY_NULLS_LAST:
3716 sortcl->nulls_first = false;
3717 break;
3718 default:
3719 elog(ERROR, "unrecognized sortby_nulls: %d",
3720 sortby->sortby_nulls);
3721 break;
3722 }
3723
3725 }
3726
3727 return sortlist;
3728}
#define Assert(condition)
Definition c.h:943
#define OidIsValid(objectId)
Definition c.h:858
int errcode(int sqlerrcode)
Definition elog.c:875
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
List * lappend(List *list, void *datum)
Definition list.c:339
Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse)
Definition lsyscache.c:326
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition lsyscache.c:1630
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
int exprLocation(const Node *expr)
Definition nodeFuncs.c:1403
#define makeNode(_type_)
Definition nodes.h:161
static char * errmsg
Index assignSortGroupRef(TargetEntry *tle, List *tlist)
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition parse_node.c:156
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition parse_node.c:140
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:183
Oid compatible_oper_opid(List *op, Oid arg1, Oid arg2, bool noError)
Definition parse_oper.c:492
@ SORTBY_NULLS_DEFAULT
Definition parsenodes.h:54
@ SORTBY_NULLS_LAST
Definition parsenodes.h:56
@ SORTBY_NULLS_FIRST
Definition parsenodes.h:55
@ SORTBY_USING
Definition parsenodes.h:49
@ SORTBY_DESC
Definition parsenodes.h:48
@ SORTBY_ASC
Definition parsenodes.h:47
@ SORTBY_DEFAULT
Definition parsenodes.h:46
#define llast(l)
Definition pg_list.h:198
#define NIL
Definition pg_list.h:68
#define InvalidOid
unsigned int Oid
static int fb(int x)
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
@ COERCION_IMPLICIT
Definition primnodes.h:747
Definition nodes.h:135
#define strVal(v)
Definition value.h:82

References Assert, assignSortGroupRef(), cancel_parser_errposition_callback(), COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, compatible_oper_opid(), elog, ereport, errcode(), errhint(), errmsg, ERROR, exprLocation(), exprType(), fb(), get_equality_op_for_ordering_op(), get_sort_group_operators(), InvalidOid, lappend(), llast, makeNode, NIL, OidIsValid, op_hashjoinable(), setup_parser_errposition_callback(), SORTBY_ASC, SORTBY_DEFAULT, SORTBY_DESC, SORTBY_NULLS_DEFAULT, SORTBY_NULLS_FIRST, SORTBY_NULLS_LAST, SORTBY_USING, strVal, and targetIsInSortList().

Referenced by transformAggregateCall(), and transformSortClause().

◆ assignSortGroupRef()

Index assignSortGroupRef ( TargetEntry tle,
List tlist 
)
extern

Definition at line 3806 of file parse_clause.c.

3807{
3808 Index maxRef;
3809 ListCell *l;
3810
3811 if (tle->ressortgroupref) /* already has one? */
3812 return tle->ressortgroupref;
3813
3814 /* easiest way to pick an unused refnumber: max used + 1 */
3815 maxRef = 0;
3816 foreach(l, tlist)
3817 {
3818 Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3819
3820 if (ref > maxRef)
3821 maxRef = ref;
3822 }
3823 tle->ressortgroupref = maxRef + 1;
3824 return tle->ressortgroupref;
3825}
unsigned int Index
Definition c.h:698
#define lfirst(lc)
Definition pg_list.h:172

References fb(), and lfirst.

Referenced by addTargetToGroupList(), addTargetToSortList(), build_minmax_path(), create_unique_paths(), generate_setop_child_grouplist(), and transformDistinctOnClause().

◆ setTargetTable()

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

Definition at line 182 of file parse_clause.c.

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

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

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

◆ targetIsInSortList()

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

Definition at line 3847 of file parse_clause.c.

3848{
3849 Index ref = tle->ressortgroupref;
3850 ListCell *l;
3851
3852 /* no need to scan list if tle has no marker */
3853 if (ref == 0)
3854 return false;
3855
3856 foreach(l, sortList)
3857 {
3859
3860 if (scl->tleSortGroupRef == ref &&
3861 (sortop == InvalidOid ||
3862 sortop == scl->sortop ||
3863 sortop == get_commutator(scl->sortop)))
3864 return true;
3865 }
3866 return false;
3867}
Oid get_commutator(Oid opno)
Definition lsyscache.c:1702

References fb(), get_commutator(), InvalidOid, and lfirst.

Referenced by addTargetToGroupList(), addTargetToSortList(), check_output_expressions(), examine_simple_variable(), targetIsInAllPartitionLists(), transformDistinctOnClause(), and transformGroupClauseExpr().

◆ transformDistinctClause()

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

Definition at line 3190 of file parse_clause.c.

3192{
3193 List *result = NIL;
3196
3197 /*
3198 * The distinctClause should consist of all ORDER BY items followed by all
3199 * other non-resjunk targetlist items. There must not be any resjunk
3200 * ORDER BY items --- that would imply that we are sorting by a value that
3201 * isn't necessarily unique within a DISTINCT group, so the results
3202 * wouldn't be well-defined. This construction ensures we follow the rule
3203 * that sortClause and distinctClause match; in fact the sortClause will
3204 * always be a prefix of distinctClause.
3205 *
3206 * Note a corner case: the same TLE could be in the ORDER BY list multiple
3207 * times with different sortops. We have to include it in the
3208 * distinctClause the same way to preserve the prefix property. The net
3209 * effect will be that the TLE value will be made unique according to both
3210 * sortops.
3211 */
3212 foreach(slitem, sortClause)
3213 {
3215 TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
3216
3217 if (tle->resjunk)
3218 ereport(ERROR,
3220 is_agg ?
3221 errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
3222 errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
3223 parser_errposition(pstate,
3224 exprLocation((Node *) tle->expr))));
3226 }
3227
3228 /*
3229 * Now add any remaining non-resjunk tlist items, using default sort/group
3230 * semantics for their data types.
3231 */
3232 foreach(tlitem, *targetlist)
3233 {
3235
3236 if (tle->resjunk)
3237 continue; /* ignore junk */
3239 result, *targetlist,
3240 exprLocation((Node *) tle->expr));
3241 }
3242
3243 /*
3244 * Complain if we found nothing to make DISTINCT. Returning an empty list
3245 * would cause the parsed Query to look like it didn't have DISTINCT, with
3246 * results that would probably surprise the user. Note: this case is
3247 * presently impossible for aggregates because of grammar restrictions,
3248 * but we check anyway.
3249 */
3250 if (result == NIL)
3251 ereport(ERROR,
3253 is_agg ?
3254 errmsg("an aggregate with DISTINCT must have at least one argument") :
3255 errmsg("SELECT DISTINCT must have at least one column")));
3256
3257 return result;
3258}
uint32 result
#define copyObject(obj)
Definition nodes.h:232
static List * addTargetToGroupList(ParseState *pstate, TargetEntry *tle, List *grouplist, List *targetlist, int location)
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
Definition pg_list.h:54
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition tlist.c:376

References addTargetToGroupList(), copyObject, ereport, errcode(), errmsg, ERROR, exprLocation(), fb(), get_sortgroupclause_tle(), lappend(), lfirst, NIL, parser_errposition(), and result.

Referenced by transformAggregateCall(), and transformSelectStmt().

◆ transformDistinctOnClause()

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

Definition at line 3274 of file parse_clause.c.

3276{
3277 List *result = NIL;
3279 bool skipped_sortitem;
3280 ListCell *lc;
3281 ListCell *lc2;
3282
3283 /*
3284 * Add all the DISTINCT ON expressions to the tlist (if not already
3285 * present, they are added as resjunk items). Assign sortgroupref numbers
3286 * to them, and make a list of these numbers. (NB: we rely below on the
3287 * sortgrouprefs list being one-for-one with the original distinctlist.
3288 * Also notice that we could have duplicate DISTINCT ON expressions and
3289 * hence duplicate entries in sortgrouprefs.)
3290 */
3291 foreach(lc, distinctlist)
3292 {
3293 Node *dexpr = (Node *) lfirst(lc);
3294 int sortgroupref;
3296
3297 tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
3299 sortgroupref = assignSortGroupRef(tle, *targetlist);
3300 sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
3301 }
3302
3303 /*
3304 * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
3305 * semantics from ORDER BY items that match DISTINCT ON items, and also
3306 * adopt their column sort order. We insist that the distinctClause and
3307 * sortClause match, so throw error if we find the need to add any more
3308 * distinctClause items after we've skipped an ORDER BY item that wasn't
3309 * in DISTINCT ON.
3310 */
3311 skipped_sortitem = false;
3312 foreach(lc, sortClause)
3313 {
3315
3316 if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
3317 {
3318 if (skipped_sortitem)
3319 ereport(ERROR,
3321 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
3322 parser_errposition(pstate,
3323 get_matching_location(scl->tleSortGroupRef,
3325 distinctlist))));
3326 else
3328 }
3329 else
3330 skipped_sortitem = true;
3331 }
3332
3333 /*
3334 * Now add any remaining DISTINCT ON items, using default sort/group
3335 * semantics for their data types. (Note: this is pretty questionable; if
3336 * the ORDER BY list doesn't include all the DISTINCT ON items and more
3337 * besides, you certainly aren't using DISTINCT ON in the intended way,
3338 * and you probably aren't going to get consistent results. It might be
3339 * better to throw an error or warning here. But historically we've
3340 * allowed it, so keep doing so.)
3341 */
3343 {
3344 Node *dexpr = (Node *) lfirst(lc);
3345 int sortgroupref = lfirst_int(lc2);
3346 TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
3347
3349 continue; /* already in list (with some semantics) */
3350 if (skipped_sortitem)
3351 ereport(ERROR,
3353 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
3356 result, *targetlist,
3358 }
3359
3360 /*
3361 * An empty result list is impossible here because of grammar
3362 * restrictions.
3363 */
3364 Assert(result != NIL);
3365
3366 return result;
3367}
List * lappend_int(List *list, int datum)
Definition list.c:357
bool list_member_int(const List *list, int datum)
Definition list.c:702
static int get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
static TargetEntry * findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
@ EXPR_KIND_DISTINCT_ON
Definition parse_node.h:62
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:550
#define lfirst_int(lc)
Definition pg_list.h:173
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition tlist.c:354

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

Referenced by transformSelectStmt().

◆ transformFromClause()

void transformFromClause ( ParseState pstate,
List frmList 
)
extern

Definition at line 116 of file parse_clause.c.

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

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

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

◆ transformGroupClause()

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

Definition at line 2778 of file parse_clause.c.

2782{
2783 List *result = NIL;
2785 List *gsets = NIL;
2786 ListCell *gl;
2787 bool hasGroupingSets = false;
2789
2790 /* Handle GROUP BY ALL */
2791 if (groupByAll)
2792 {
2793 /* There cannot have been any explicit grouplist items */
2794 Assert(grouplist == NIL);
2795
2796 /* Iterate over targets, adding acceptable ones to the result list */
2797 foreach_ptr(TargetEntry, tle, *targetlist)
2798 {
2799 /* Ignore junk TLEs */
2800 if (tle->resjunk)
2801 continue;
2802
2803 /*
2804 * TLEs containing aggregates are not okay to add to GROUP BY
2805 * (compare checkTargetlistEntrySQL92). But the SQL standard
2806 * directs us to skip them, so it's fine.
2807 */
2808 if (pstate->p_hasAggs &&
2809 contain_aggs_of_level((Node *) tle->expr, 0))
2810 continue;
2811
2812 /*
2813 * Likewise, TLEs containing window functions are not okay to add
2814 * to GROUP BY. At this writing, the SQL standard is silent on
2815 * what to do with them, but by analogy to aggregates we'll just
2816 * skip them.
2817 */
2818 if (pstate->p_hasWindowFuncs &&
2819 contain_windowfuncs((Node *) tle->expr))
2820 continue;
2821
2822 /*
2823 * Otherwise, add the TLE to the result using default sort/group
2824 * semantics. We specify the parse location as the TLE's
2825 * location, despite the comment for addTargetToGroupList
2826 * discouraging that. The only other thing we could point to is
2827 * the ALL keyword, which seems unhelpful when there are multiple
2828 * TLEs.
2829 */
2831 result, *targetlist,
2832 exprLocation((Node *) tle->expr));
2833 }
2834
2835 /* If we found any acceptable targets, we're done */
2836 if (result != NIL)
2837 return result;
2838
2839 /*
2840 * Otherwise, the SQL standard says to treat it like "GROUP BY ()".
2841 * Build a representation of that, and let the rest of this function
2842 * handle it.
2843 */
2845 }
2846
2847 /*
2848 * Recursively flatten implicit RowExprs. (Technically this is only needed
2849 * for GROUP BY, per the syntax rules for grouping sets, but we do it
2850 * anyway.)
2851 */
2853 true,
2855
2856 /*
2857 * If the list is now empty, but hasGroupingSets is true, it's because we
2858 * elided redundant empty grouping sets. Restore a single empty grouping
2859 * set to leave a canonical form: GROUP BY ()
2860 */
2861
2863 {
2865 NIL,
2867 }
2868
2869 foreach(gl, flat_grouplist)
2870 {
2871 Node *gexpr = (Node *) lfirst(gl);
2872
2873 if (IsA(gexpr, GroupingSet))
2874 {
2876
2877 switch (gset->kind)
2878 {
2879 case GROUPING_SET_EMPTY:
2880 gsets = lappend(gsets, gset);
2881 break;
2883 /* can't happen */
2884 Assert(false);
2885 break;
2886 case GROUPING_SET_SETS:
2887 case GROUPING_SET_CUBE:
2889 gsets = lappend(gsets,
2891 pstate, gset,
2892 targetlist, sortClause,
2893 exprKind, useSQL99, true));
2894 break;
2895 }
2896 }
2897 else
2898 {
2900 pstate, gexpr,
2901 targetlist, sortClause,
2902 exprKind, useSQL99, true);
2903
2904 if (ref > 0)
2905 {
2907 if (hasGroupingSets)
2908 gsets = lappend(gsets,
2912 }
2913 }
2914 }
2915
2916 /* parser should prevent this */
2917 Assert(gsets == NIL || groupingSets != NULL);
2918
2919 if (groupingSets)
2920 *groupingSets = gsets;
2921
2922 return result;
2923}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
GroupingSet * makeGroupingSet(GroupingSetKind kind, List *content, int location)
Definition makefuncs.c:892
#define IsA(nodeptr, _type_)
Definition nodes.h:164
static Node * flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
static Index transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local, ParseState *pstate, Node *gexpr, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
static Node * transformGroupingSet(List **flatresult, ParseState *pstate, GroupingSet *gset, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
@ GROUPING_SET_CUBE
@ GROUPING_SET_SIMPLE
@ GROUPING_SET_ROLLUP
@ GROUPING_SET_SETS
@ GROUPING_SET_EMPTY
#define list_make1(x1)
Definition pg_list.h:244
#define foreach_ptr(type, var, lst)
Definition pg_list.h:501
#define list_make1_int(x1)
Definition pg_list.h:259
bool contain_windowfuncs(Node *node)
bool contain_aggs_of_level(Node *node, int levelsup)
bool p_hasWindowFuncs
Definition parse_node.h:247
bool p_hasAggs
Definition parse_node.h:246

References addTargetToGroupList(), Assert, bms_add_member(), contain_aggs_of_level(), contain_windowfuncs(), exprLocation(), fb(), flatten_grouping_sets(), foreach_ptr, GROUPING_SET_CUBE, GROUPING_SET_EMPTY, GROUPING_SET_ROLLUP, GROUPING_SET_SETS, GROUPING_SET_SIMPLE, IsA, lappend(), lfirst, list_make1, list_make1_int, makeGroupingSet(), NIL, ParseState::p_hasAggs, ParseState::p_hasWindowFuncs, result, transformGroupClauseExpr(), and transformGroupingSet().

Referenced by transformSelectStmt(), and transformWindowDefinitions().

◆ transformJsonTable()

ParseNamespaceItem * transformJsonTable ( ParseState pstate,
JsonTable jt 
)
extern

Definition at line 74 of file parse_jsontable.c.

75{
76 TableFunc *tf;
78 JsonExpr *je;
80 bool is_lateral;
81 JsonTableParseContext cxt = {pstate};
82
83 Assert(IsA(rootPathSpec->string, A_Const) &&
84 castNode(A_Const, rootPathSpec->string)->val.node.type == T_String);
85
86 if (jt->on_error &&
92 errmsg("invalid %s behavior", "ON ERROR"),
93 errdetail("Only EMPTY [ ARRAY ] or ERROR is allowed in the top-level ON ERROR clause."),
95
96 cxt.pathNameId = 0;
97 if (rootPathSpec->name == NULL)
101
102 /*
103 * We make lateral_only names of this level visible, whether or not the
104 * RangeTableFunc is explicitly marked LATERAL. This is needed for SQL
105 * spec compliance and seems useful on convenience grounds for all
106 * functions in FROM.
107 *
108 * (LATERAL can't nest within a single pstate level, so we don't need
109 * save/restore logic here.)
110 */
111 Assert(!pstate->p_lateral_active);
112 pstate->p_lateral_active = true;
113
114 tf = makeNode(TableFunc);
116
117 /*
118 * Transform JsonFuncExpr representing the top JSON_TABLE context_item and
119 * pathspec into a dummy JSON_TABLE_OP JsonExpr.
120 */
122 jfe->op = JSON_TABLE_OP;
123 jfe->context_item = jt->context_item;
124 jfe->pathspec = (Node *) rootPathSpec->string;
125 jfe->passing = jt->passing;
126 jfe->on_empty = NULL;
127 jfe->on_error = jt->on_error;
128 jfe->location = jt->location;
130
131 /*
132 * Create a JsonTablePlan that will generate row pattern that becomes
133 * source data for JSON path expressions in jt->columns. This also adds
134 * the columns' transformed JsonExpr nodes into tf->colvalexprs.
135 */
136 cxt.jt = jt;
137 cxt.tf = tf;
138 tf->plan = (Node *) transformJsonTableColumns(&cxt, jt->columns,
139 jt->passing,
141
142 /*
143 * Copy the transformed PASSING arguments into the TableFunc node, because
144 * they are evaluated separately from the JsonExpr that we just put in
145 * TableFunc.docexpr. JsonExpr.passing_values is still kept around for
146 * get_json_table().
147 */
148 je = (JsonExpr *) tf->docexpr;
149 tf->passingvalexprs = copyObject(je->passing_values);
150
151 tf->ordinalitycol = -1; /* undefine ordinality column number */
152 tf->location = jt->location;
153
154 pstate->p_lateral_active = false;
155
156 /*
157 * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
158 * there are any lateral cross-references in it.
159 */
160 is_lateral = jt->lateral || contain_vars_of_level((Node *) tf, 0);
161
162 return addRangeTableEntryForTableFunc(pstate,
163 tf, jt->alias, is_lateral, true);
164}
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define castNode(_type_, nodeptr)
Definition nodes.h:182
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition parse_expr.c:121
static char * generateJsonTablePathName(JsonTableParseContext *cxt)
static JsonTablePlan * transformJsonTableColumns(JsonTableParseContext *cxt, List *columns, List *passingArgs, JsonTablePathSpec *pathspec)
static void CheckDuplicateColumnOrPathNames(JsonTableParseContext *cxt, List *columns)
@ EXPR_KIND_FROM_FUNCTION
Definition parse_node.h:45
ParseNamespaceItem * addRangeTableEntryForTableFunc(ParseState *pstate, TableFunc *tf, Alias *alias, bool lateral, bool inFromCl)
@ TFT_JSON_TABLE
Definition primnodes.h:102
@ JSON_BEHAVIOR_ERROR
Definition primnodes.h:1806
@ JSON_BEHAVIOR_EMPTY
Definition primnodes.h:1807
@ JSON_BEHAVIOR_EMPTY_ARRAY
Definition primnodes.h:1811
@ JSON_TABLE_OP
Definition primnodes.h:1845
ParseLoc location
Definition primnodes.h:1833
JsonBehaviorType btype
Definition primnodes.h:1830
JsonBehavior * on_error
List * columns
JsonTablePathSpec * pathspec
Alias * alias
List * passing
JsonValueExpr * context_item
ParseLoc location
bool p_lateral_active
Definition parse_node.h:224
ParseLoc location
Definition primnodes.h:147
Node * docexpr
Definition primnodes.h:121
TableFuncType functype
Definition primnodes.h:115
bool contain_vars_of_level(Node *node, int levelsup)
Definition var.c:444

References addRangeTableEntryForTableFunc(), JsonTable::alias, Assert, JsonBehavior::btype, castNode, CheckDuplicateColumnOrPathNames(), JsonTable::columns, contain_vars_of_level(), JsonTable::context_item, copyObject, TableFunc::docexpr, ereport, errcode(), errdetail(), errmsg, ERROR, EXPR_KIND_FROM_FUNCTION, fb(), TableFunc::functype, generateJsonTablePathName(), IsA, JSON_BEHAVIOR_EMPTY, JSON_BEHAVIOR_EMPTY_ARRAY, JSON_BEHAVIOR_ERROR, JSON_TABLE_OP, JsonTableParseContext::jt, JsonTable::lateral, list_make1, JsonTable::location, TableFunc::location, JsonBehavior::location, makeNode, JsonTable::on_error, ParseState::p_lateral_active, parser_errposition(), JsonTable::passing, JsonTableParseContext::pathNameId, JsonTableParseContext::pathNames, JsonTable::pathspec, JsonTableParseContext::tf, TFT_JSON_TABLE, transformExpr(), and transformJsonTableColumns().

Referenced by transformFromClauseItem().

◆ transformLimitClause()

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

Definition at line 2023 of file parse_clause.c.

2026{
2027 Node *qual;
2028
2029 if (clause == NULL)
2030 return NULL;
2031
2032 qual = transformExpr(pstate, clause, exprKind);
2033
2034 qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
2035
2036 /* LIMIT can't refer to any variables of the current query */
2037 checkExprIsVarFree(pstate, qual, constructName);
2038
2039 /*
2040 * Don't allow NULLs in FETCH FIRST .. WITH TIES. This test is ugly and
2041 * extremely simplistic, in that you can pass a NULL anyway by hiding it
2042 * inside an expression -- but this protects ruleutils against emitting an
2043 * unadorned NULL that's not accepted back by the grammar.
2044 */
2045 if (exprKind == EXPR_KIND_LIMIT && limitOption == LIMIT_OPTION_WITH_TIES &&
2046 IsA(clause, A_Const) && castNode(A_Const, clause)->isnull)
2047 ereport(ERROR,
2049 errmsg("row count cannot be null in FETCH FIRST ... WITH TIES clause")));
2050
2051 return qual;
2052}
@ LIMIT_OPTION_WITH_TIES
Definition nodes.h:443
static void checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
@ EXPR_KIND_LIMIT
Definition parse_node.h:63

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

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

◆ transformOnConflictArbiter()

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

Definition at line 3507 of file parse_clause.c.

3511{
3512 InferClause *infer = onConflictClause->infer;
3513
3514 *arbiterExpr = NIL;
3515 *arbiterWhere = NULL;
3516 *constraint = InvalidOid;
3517
3518 if ((onConflictClause->action == ONCONFLICT_UPDATE ||
3519 onConflictClause->action == ONCONFLICT_SELECT) && !infer)
3520 ereport(ERROR,
3522 errmsg("ON CONFLICT DO %s requires inference specification or constraint name",
3523 onConflictClause->action == ONCONFLICT_UPDATE ? "UPDATE" : "SELECT"),
3524 errhint("For example, ON CONFLICT (column_name)."),
3525 parser_errposition(pstate,
3526 exprLocation((Node *) onConflictClause)));
3527
3528 /*
3529 * To simplify certain aspects of its design, speculative insertion into
3530 * system catalogs is disallowed
3531 */
3533 ereport(ERROR,
3535 errmsg("ON CONFLICT is not supported with system catalog tables"),
3536 parser_errposition(pstate,
3537 exprLocation((Node *) onConflictClause))));
3538
3539 /* Same applies to table used by logical decoding as catalog table */
3541 ereport(ERROR,
3543 errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
3545 parser_errposition(pstate,
3546 exprLocation((Node *) onConflictClause))));
3547
3548 /* ON CONFLICT DO NOTHING does not require an inference clause */
3549 if (infer)
3550 {
3551 if (infer->indexElems)
3552 *arbiterExpr = resolve_unique_index_expr(pstate, infer,
3553 pstate->p_target_relation);
3554
3555 /*
3556 * Handling inference WHERE clause (for partial unique index
3557 * inference)
3558 */
3559 if (infer->whereClause)
3560 *arbiterWhere = transformExpr(pstate, infer->whereClause,
3562
3563 /*
3564 * If the arbiter is specified by constraint name, get the constraint
3565 * OID and mark the constrained columns as requiring SELECT privilege,
3566 * in the same way as would have happened if the arbiter had been
3567 * specified by explicit reference to the constraint's index columns.
3568 */
3569 if (infer->conname)
3570 {
3571 Oid relid = RelationGetRelid(pstate->p_target_relation);
3574
3576 false, constraint);
3577
3578 /* Make sure the rel as a whole is marked for SELECT access */
3579 perminfo->requiredPerms |= ACL_SELECT;
3580 /* Mark the constrained columns as requiring SELECT access */
3581 perminfo->selectedCols = bms_add_members(perminfo->selectedCols,
3582 conattnos);
3583 }
3584 }
3585
3586 /*
3587 * It's convenient to form a list of expressions based on the
3588 * representation used by CREATE INDEX, since the same restrictions are
3589 * appropriate (e.g. on subqueries). However, from here on, a dedicated
3590 * primnode representation is used for inference elements, and so
3591 * assign_query_collations() can be trusted to do the right thing with the
3592 * post parse analysis query tree inference clause representation.
3593 */
3594}
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:901
bool IsCatalogRelation(Relation relation)
Definition catalog.c:104
@ ONCONFLICT_SELECT
Definition nodes.h:431
@ ONCONFLICT_UPDATE
Definition nodes.h:430
static List * resolve_unique_index_expr(ParseState *pstate, InferClause *infer, Relation heapRel)
@ EXPR_KIND_INDEX_PREDICATE
Definition parse_node.h:74
#define ACL_SELECT
Definition parsenodes.h:77
Bitmapset * get_relation_constraint_attnos(Oid relid, const char *conname, bool missing_ok, Oid *constraintOid)
#define RelationGetRelid(relation)
Definition rel.h:516
#define RelationIsUsedAsCatalogTable(relation)
Definition rel.h:399
#define RelationGetRelationName(relation)
Definition rel.h:550
char * conname
List * indexElems
Node * whereClause
InferClause * infer
OnConflictAction action

References ACL_SELECT, OnConflictClause::action, bms_add_members(), InferClause::conname, ereport, errcode(), errhint(), errmsg, ERROR, EXPR_KIND_INDEX_PREDICATE, exprLocation(), fb(), get_relation_constraint_attnos(), InferClause::indexElems, OnConflictClause::infer, InvalidOid, IsCatalogRelation(), NIL, ONCONFLICT_SELECT, ONCONFLICT_UPDATE, ParseNamespaceItem::p_perminfo, ParseState::p_target_nsitem, ParseState::p_target_relation, parser_errposition(), RelationGetRelationName, RelationGetRelid, RelationIsUsedAsCatalogTable, resolve_unique_index_expr(), transformExpr(), and InferClause::whereClause.

Referenced by transformOnConflictClause().

◆ transformSortClause()

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

Definition at line 2936 of file parse_clause.c.

2941{
2942 List *sortlist = NIL;
2944
2945 foreach(olitem, orderlist)
2946 {
2949
2950 if (useSQL99)
2951 tle = findTargetlistEntrySQL99(pstate, sortby->node,
2952 targetlist, exprKind);
2953 else
2954 tle = findTargetlistEntrySQL92(pstate, sortby->node,
2955 targetlist, exprKind);
2956
2958 sortlist, *targetlist, sortby);
2959 }
2960
2961 return sortlist;
2962}
static TargetEntry * findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
List * addTargetToSortList(ParseState *pstate, TargetEntry *tle, List *sortlist, List *targetlist, SortBy *sortby)

References addTargetToSortList(), fb(), findTargetlistEntrySQL92(), findTargetlistEntrySQL99(), lfirst, and NIL.

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

◆ transformWhereClause()

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

Definition at line 1996 of file parse_clause.c.

1998{
1999 Node *qual;
2000
2001 if (clause == NULL)
2002 return NULL;
2003
2004 qual = transformExpr(pstate, clause, exprKind);
2005
2006 qual = coerce_to_boolean(pstate, qual, constructName);
2007
2008 return qual;
2009}
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)

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

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

◆ transformWindowDefinitions()

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

Definition at line 2969 of file parse_clause.c.

2972{
2973 List *result = NIL;
2974 Index winref = 0;
2975 ListCell *lc;
2976
2977 foreach(lc, windowdefs)
2978 {
2981 List *partitionClause;
2982 List *orderClause;
2985 WindowClause *wc;
2986
2987 winref++;
2988
2989 /*
2990 * Check for duplicate window names.
2991 */
2992 if (windef->name &&
2994 ereport(ERROR,
2996 errmsg("window \"%s\" is already defined", windef->name),
2997 parser_errposition(pstate, windef->location)));
2998
2999 /*
3000 * If it references a previous window, look that up.
3001 */
3002 if (windef->refname)
3003 {
3004 refwc = findWindowClause(result, windef->refname);
3005 if (refwc == NULL)
3006 ereport(ERROR,
3008 errmsg("window \"%s\" does not exist",
3009 windef->refname),
3010 parser_errposition(pstate, windef->location)));
3011 }
3012
3013 /*
3014 * Transform PARTITION and ORDER specs, if any. These are treated
3015 * almost exactly like top-level GROUP BY and ORDER BY clauses,
3016 * including the special handling of nondefault operator semantics.
3017 */
3018 orderClause = transformSortClause(pstate,
3019 windef->orderClause,
3020 targetlist,
3022 true /* force SQL99 rules */ );
3023 partitionClause = transformGroupClause(pstate,
3024 windef->partitionClause,
3025 false /* not GROUP BY ALL */ ,
3026 NULL,
3027 targetlist,
3028 orderClause,
3030 true /* force SQL99 rules */ );
3031
3032 /*
3033 * And prepare the new WindowClause.
3034 */
3035 wc = makeNode(WindowClause);
3036 wc->name = windef->name;
3037 wc->refname = windef->refname;
3038
3039 /*
3040 * Per spec, a windowdef that references a previous one copies the
3041 * previous partition clause (and mustn't specify its own). It can
3042 * specify its own ordering clause, but only if the previous one had
3043 * none. It always specifies its own frame clause, and the previous
3044 * one must not have a frame clause. Yeah, it's bizarre that each of
3045 * these cases works differently, but SQL:2008 says so; see 7.11
3046 * <window clause> syntax rule 10 and general rule 1. The frame
3047 * clause rule is especially bizarre because it makes "OVER foo"
3048 * different from "OVER (foo)", and requires the latter to throw an
3049 * error if foo has a nondefault frame clause. Well, ours not to
3050 * reason why, but we do go out of our way to throw a useful error
3051 * message for such cases.
3052 */
3053 if (refwc)
3054 {
3055 if (partitionClause)
3056 ereport(ERROR,
3058 errmsg("cannot override PARTITION BY clause of window \"%s\"",
3059 windef->refname),
3060 parser_errposition(pstate, windef->location)));
3061 wc->partitionClause = copyObject(refwc->partitionClause);
3062 }
3063 else
3064 wc->partitionClause = partitionClause;
3065 if (refwc)
3066 {
3067 if (orderClause && refwc->orderClause)
3068 ereport(ERROR,
3070 errmsg("cannot override ORDER BY clause of window \"%s\"",
3071 windef->refname),
3072 parser_errposition(pstate, windef->location)));
3073 if (orderClause)
3074 {
3075 wc->orderClause = orderClause;
3076 wc->copiedOrder = false;
3077 }
3078 else
3079 {
3080 wc->orderClause = copyObject(refwc->orderClause);
3081 wc->copiedOrder = true;
3082 }
3083 }
3084 else
3085 {
3086 wc->orderClause = orderClause;
3087 wc->copiedOrder = false;
3088 }
3089 if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
3090 {
3091 /*
3092 * Use this message if this is a WINDOW clause, or if it's an OVER
3093 * clause that includes ORDER BY or framing clauses. (We already
3094 * rejected PARTITION BY above, so no need to check that.)
3095 */
3096 if (windef->name ||
3097 orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
3098 ereport(ERROR,
3100 errmsg("cannot copy window \"%s\" because it has a frame clause",
3101 windef->refname),
3102 parser_errposition(pstate, windef->location)));
3103 /* Else this clause is just OVER (foo), so say this: */
3104 ereport(ERROR,
3106 errmsg("cannot copy window \"%s\" because it has a frame clause",
3107 windef->refname),
3108 errhint("Omit the parentheses in this OVER clause."),
3109 parser_errposition(pstate, windef->location)));
3110 }
3111 wc->frameOptions = windef->frameOptions;
3112
3113 /*
3114 * RANGE offset PRECEDING/FOLLOWING requires exactly one ORDER BY
3115 * column; check that and get its sort opfamily info.
3116 */
3117 if ((wc->frameOptions & FRAMEOPTION_RANGE) &&
3120 {
3122 Node *sortkey;
3124
3125 if (list_length(wc->orderClause) != 1)
3126 ereport(ERROR,
3128 errmsg("RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column"),
3129 parser_errposition(pstate, windef->location)));
3131 sortkey = get_sortgroupclause_expr(sortcl, *targetlist);
3132 /* Find the sort operator in pg_amop */
3136 &rangecmptype))
3137 elog(ERROR, "operator %u is not a valid ordering operator",
3138 sortcl->sortop);
3139 /* Record properties of sort ordering */
3140 wc->inRangeColl = exprCollation(sortkey);
3141 wc->inRangeAsc = !sortcl->reverse_sort;
3142 wc->inRangeNullsFirst = sortcl->nulls_first;
3143 }
3144
3145 /* Per spec, GROUPS mode requires an ORDER BY clause */
3147 {
3148 if (wc->orderClause == NIL)
3149 ereport(ERROR,
3151 errmsg("GROUPS mode requires an ORDER BY clause"),
3152 parser_errposition(pstate, windef->location)));
3153 }
3154
3155 /* Process frame offset expressions */
3158 &wc->startInRangeFunc,
3162 &wc->endInRangeFunc,
3163 windef->endOffset);
3164 wc->winref = winref;
3165
3166 result = lappend(result, wc);
3167 }
3168
3169 return result;
3170}
CompareType
Definition cmptype.h:32
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, CompareType *cmptype)
Definition lsyscache.c:261
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
List * transformGroupClause(ParseState *pstate, List *grouplist, bool groupByAll, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
static Node * transformFrameOffset(ParseState *pstate, int frameOptions, Oid rangeopfamily, Oid rangeopcintype, Oid *inRangeFunc, Node *clause)
static WindowClause * findWindowClause(List *wclist, const char *name)
@ EXPR_KIND_WINDOW_PARTITION
Definition parse_node.h:49
@ EXPR_KIND_WINDOW_ORDER
Definition parse_node.h:50
#define FRAMEOPTION_END_OFFSET
Definition parsenodes.h:633
#define FRAMEOPTION_START_OFFSET
Definition parsenodes.h:631
#define FRAMEOPTION_RANGE
Definition parsenodes.h:613
#define FRAMEOPTION_GROUPS
Definition parsenodes.h:615
#define FRAMEOPTION_DEFAULTS
Definition parsenodes.h:639
static int list_length(const List *l)
Definition pg_list.h:152
#define linitial_node(type, l)
Definition pg_list.h:181
Node * startOffset
List * partitionClause
Node * endOffset
List * orderClause
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
Definition tlist.c:388

References copyObject, elog, WindowClause::endOffset, ereport, errcode(), errhint(), errmsg, ERROR, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, exprCollation(), fb(), findWindowClause(), FRAMEOPTION_DEFAULTS, FRAMEOPTION_END_OFFSET, FRAMEOPTION_GROUPS, FRAMEOPTION_RANGE, FRAMEOPTION_START_OFFSET, WindowClause::frameOptions, get_ordering_op_properties(), get_sortgroupclause_expr(), InvalidOid, lappend(), lfirst, linitial_node, list_length(), makeNode, NIL, WindowClause::orderClause, parser_errposition(), WindowClause::partitionClause, result, WindowClause::startOffset, transformFrameOffset(), transformGroupClause(), transformSortClause(), and WindowClause::winref.

Referenced by transformSelectStmt().