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

3550{
3551 Oid restype = exprType((Node *) tle->expr);
3552 Oid sortop;
3553 Oid eqop;
3554 bool hashable;
3555 bool reverse;
3556 int location;
3558
3559 /* if tlist item is an UNKNOWN literal, change it to TEXT */
3560 if (restype == UNKNOWNOID)
3561 {
3562 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3563 restype, TEXTOID, -1,
3566 -1);
3567 restype = TEXTOID;
3568 }
3569
3570 /*
3571 * Rather than clutter the API of get_sort_group_operators and the other
3572 * functions we're about to use, make use of error context callback to
3573 * mark any error reports with a parse position. We point to the operator
3574 * location if present, else to the expression being sorted. (NB: use the
3575 * original untransformed expression here; the TLE entry might well point
3576 * at a duplicate expression in the regular SELECT list.)
3577 */
3578 location = sortby->location;
3579 if (location < 0)
3580 location = exprLocation(sortby->node);
3581 setup_parser_errposition_callback(&pcbstate, pstate, location);
3582
3583 /* determine the sortop, eqop, and directionality */
3584 switch (sortby->sortby_dir)
3585 {
3586 case SORTBY_DEFAULT:
3587 case SORTBY_ASC:
3589 true, true, false,
3590 &sortop, &eqop, NULL,
3591 &hashable);
3592 reverse = false;
3593 break;
3594 case SORTBY_DESC:
3596 false, true, true,
3597 NULL, &eqop, &sortop,
3598 &hashable);
3599 reverse = true;
3600 break;
3601 case SORTBY_USING:
3602 Assert(sortby->useOp != NIL);
3603 sortop = compatible_oper_opid(sortby->useOp,
3604 restype,
3605 restype,
3606 false);
3607
3608 /*
3609 * Verify it's a valid ordering operator, fetch the corresponding
3610 * equality operator, and determine whether to consider it like
3611 * ASC or DESC.
3612 */
3613 eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3614 if (!OidIsValid(eqop))
3615 ereport(ERROR,
3617 errmsg("operator %s is not a valid ordering operator",
3618 strVal(llast(sortby->useOp))),
3619 errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3620
3621 /*
3622 * Also see if the equality operator is hashable.
3623 */
3624 hashable = op_hashjoinable(eqop, restype);
3625 break;
3626 default:
3627 elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3628 sortop = InvalidOid; /* keep compiler quiet */
3629 eqop = InvalidOid;
3630 hashable = false;
3631 reverse = false;
3632 break;
3633 }
3634
3636
3637 /* avoid making duplicate sortlist entries */
3638 if (!targetIsInSortList(tle, sortop, sortlist))
3639 {
3641
3642 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3643
3644 sortcl->eqop = eqop;
3645 sortcl->sortop = sortop;
3646 sortcl->hashable = hashable;
3647 sortcl->reverse_sort = reverse;
3648
3649 switch (sortby->sortby_nulls)
3650 {
3652 /* NULLS FIRST is default for DESC; other way for ASC */
3653 sortcl->nulls_first = reverse;
3654 break;
3655 case SORTBY_NULLS_FIRST:
3656 sortcl->nulls_first = true;
3657 break;
3658 case SORTBY_NULLS_LAST:
3659 sortcl->nulls_first = false;
3660 break;
3661 default:
3662 elog(ERROR, "unrecognized sortby_nulls: %d",
3663 sortby->sortby_nulls);
3664 break;
3665 }
3666
3668 }
3669
3670 return sortlist;
3671}
#define Assert(condition)
Definition c.h:1002
#define OidIsValid(objectId)
Definition c.h:917
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:1739
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
int exprLocation(const Node *expr)
Definition nodeFuncs.c:1403
#define makeNode(_type_)
Definition nodes.h:159
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:497
@ 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:759
@ COERCION_IMPLICIT
Definition primnodes.h:737
Definition nodes.h:133
#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 3749 of file parse_clause.c.

3750{
3751 Index maxRef;
3752 ListCell *l;
3753
3754 if (tle->ressortgroupref) /* already has one? */
3755 return tle->ressortgroupref;
3756
3757 /* easiest way to pick an unused refnumber: max used + 1 */
3758 maxRef = 0;
3759 foreach(l, tlist)
3760 {
3761 Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3762
3763 if (ref > maxRef)
3764 maxRef = ref;
3765 }
3766 tle->ressortgroupref = maxRef + 1;
3767 return tle->ressortgroupref;
3768}
unsigned int Index
Definition c.h:757
#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 3790 of file parse_clause.c.

3791{
3792 Index ref = tle->ressortgroupref;
3793 ListCell *l;
3794
3795 /* no need to scan list if tle has no marker */
3796 if (ref == 0)
3797 return false;
3798
3799 foreach(l, sortList)
3800 {
3802
3803 if (scl->tleSortGroupRef == ref &&
3804 (sortop == InvalidOid ||
3805 sortop == scl->sortop ||
3806 sortop == get_commutator(scl->sortop)))
3807 return true;
3808 }
3809 return false;
3810}
Oid get_commutator(Oid opno)
Definition lsyscache.c:1823

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

3135{
3136 List *result = NIL;
3139
3140 /*
3141 * The distinctClause should consist of all ORDER BY items followed by all
3142 * other non-resjunk targetlist items. There must not be any resjunk
3143 * ORDER BY items --- that would imply that we are sorting by a value that
3144 * isn't necessarily unique within a DISTINCT group, so the results
3145 * wouldn't be well-defined. This construction ensures we follow the rule
3146 * that sortClause and distinctClause match; in fact the sortClause will
3147 * always be a prefix of distinctClause.
3148 *
3149 * Note a corner case: the same TLE could be in the ORDER BY list multiple
3150 * times with different sortops. We have to include it in the
3151 * distinctClause the same way to preserve the prefix property. The net
3152 * effect will be that the TLE value will be made unique according to both
3153 * sortops.
3154 */
3155 foreach(slitem, sortClause)
3156 {
3158 TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
3159
3160 if (tle->resjunk)
3161 ereport(ERROR,
3163 is_agg ?
3164 errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
3165 errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
3166 parser_errposition(pstate,
3167 exprLocation((Node *) tle->expr))));
3169 }
3170
3171 /*
3172 * Now add any remaining non-resjunk tlist items, using default sort/group
3173 * semantics for their data types.
3174 */
3175 foreach(tlitem, *targetlist)
3176 {
3178
3179 if (tle->resjunk)
3180 continue; /* ignore junk */
3182 result, *targetlist,
3183 exprLocation((Node *) tle->expr));
3184 }
3185
3186 /*
3187 * Complain if we found nothing to make DISTINCT. Returning an empty list
3188 * would cause the parsed Query to look like it didn't have DISTINCT, with
3189 * results that would probably surprise the user. Note: this case is
3190 * presently impossible for aggregates because of grammar restrictions,
3191 * but we check anyway.
3192 */
3193 if (result == NIL)
3194 ereport(ERROR,
3196 is_agg ?
3197 errmsg("an aggregate with DISTINCT must have at least one argument") :
3198 errmsg("SELECT DISTINCT must have at least one column")));
3199
3200 return result;
3201}
uint32 result
#define copyObject(obj)
Definition nodes.h:230
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 3217 of file parse_clause.c.

3219{
3220 List *result = NIL;
3222 bool skipped_sortitem;
3223 ListCell *lc;
3224 ListCell *lc2;
3225
3226 /*
3227 * Add all the DISTINCT ON expressions to the tlist (if not already
3228 * present, they are added as resjunk items). Assign sortgroupref numbers
3229 * to them, and make a list of these numbers. (NB: we rely below on the
3230 * sortgrouprefs list being one-for-one with the original distinctlist.
3231 * Also notice that we could have duplicate DISTINCT ON expressions and
3232 * hence duplicate entries in sortgrouprefs.)
3233 */
3234 foreach(lc, distinctlist)
3235 {
3236 Node *dexpr = (Node *) lfirst(lc);
3237 int sortgroupref;
3239
3240 tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
3242 sortgroupref = assignSortGroupRef(tle, *targetlist);
3243 sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
3244 }
3245
3246 /*
3247 * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
3248 * semantics from ORDER BY items that match DISTINCT ON items, and also
3249 * adopt their column sort order. We insist that the distinctClause and
3250 * sortClause match, so throw error if we find the need to add any more
3251 * distinctClause items after we've skipped an ORDER BY item that wasn't
3252 * in DISTINCT ON.
3253 */
3254 skipped_sortitem = false;
3255 foreach(lc, sortClause)
3256 {
3258
3259 if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
3260 {
3261 if (skipped_sortitem)
3262 ereport(ERROR,
3264 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
3265 parser_errposition(pstate,
3266 get_matching_location(scl->tleSortGroupRef,
3268 distinctlist))));
3269 else
3271 }
3272 else
3273 skipped_sortitem = true;
3274 }
3275
3276 /*
3277 * Now add any remaining DISTINCT ON items, using default sort/group
3278 * semantics for their data types. (Note: this is pretty questionable; if
3279 * the ORDER BY list doesn't include all the DISTINCT ON items and more
3280 * besides, you certainly aren't using DISTINCT ON in the intended way,
3281 * and you probably aren't going to get consistent results. It might be
3282 * better to throw an error or warning here. But historically we've
3283 * allowed it, so keep doing so.)
3284 */
3286 {
3287 Node *dexpr = (Node *) lfirst(lc);
3288 int sortgroupref = lfirst_int(lc2);
3289 TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
3290
3292 continue; /* already in list (with some semantics) */
3293 if (skipped_sortitem)
3294 ereport(ERROR,
3296 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
3299 result, *targetlist,
3301 }
3302
3303 /*
3304 * An empty result list is impossible here because of grammar
3305 * restrictions.
3306 */
3307 Assert(result != NIL);
3308
3309 return result;
3310}
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,
List **  groupingSets,
List **  targetlist,
List sortClause,
ParseExprKind  exprKind,
bool  useSQL99 
)
extern

Definition at line 2780 of file parse_clause.c.

2783{
2784 List *result = NIL;
2786 List *gsets = NIL;
2787 ListCell *gl;
2788 bool hasGroupingSets = false;
2790
2791 /*
2792 * Recursively flatten implicit RowExprs. (Technically this is only needed
2793 * for GROUP BY, per the syntax rules for grouping sets, but we do it
2794 * anyway.)
2795 */
2797 true,
2799
2800 /*
2801 * If the list is now empty, but hasGroupingSets is true, it's because we
2802 * elided redundant empty grouping sets. Restore a single empty grouping
2803 * set to leave a canonical form: GROUP BY ()
2804 */
2805
2807 {
2809 NIL,
2811 }
2812
2813 foreach(gl, flat_grouplist)
2814 {
2815 Node *gexpr = (Node *) lfirst(gl);
2816
2817 if (IsA(gexpr, GroupingSet))
2818 {
2820
2821 switch (gset->kind)
2822 {
2823 case GROUPING_SET_EMPTY:
2824 gsets = lappend(gsets, gset);
2825 break;
2827 /* can't happen */
2828 Assert(false);
2829 break;
2830 case GROUPING_SET_SETS:
2831 case GROUPING_SET_CUBE:
2833 gsets = lappend(gsets,
2835 pstate, gset,
2836 targetlist, sortClause,
2837 exprKind, useSQL99, true));
2838 break;
2839 }
2840 }
2841 else
2842 {
2844 pstate, gexpr,
2845 targetlist, sortClause,
2846 exprKind, useSQL99, true);
2847
2848 if (ref > 0)
2849 {
2851 if (hasGroupingSets)
2852 gsets = lappend(gsets,
2856 }
2857 }
2858 }
2859
2860 /* parser should prevent this */
2861 Assert(gsets == NIL || groupingSets != NULL);
2862
2863 if (groupingSets)
2864 *groupingSets = gsets;
2865
2866 return result;
2867}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:934
GroupingSet * makeGroupingSet(GroupingSetKind kind, List *content, int location)
Definition makefuncs.c:892
#define IsA(nodeptr, _type_)
Definition nodes.h:162
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 list_make1_int(x1)
Definition pg_list.h:259

References Assert, bms_add_member(), exprLocation(), fb(), flatten_grouping_sets(), 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, result, transformGroupClauseExpr(), and transformGroupingSet().

Referenced by transformSelectStmt(), and transformWindowDefinitions().

◆ transformJsonTable()

ParseNamespaceItem * transformJsonTable ( ParseState pstate,
JsonTable jt 
)
extern

Definition at line 84 of file parse_jsontable.c.

85{
86 TableFunc *tf;
88 JsonExpr *je;
91 bool is_lateral;
92 JsonTableParseContext cxt = {pstate};
93
94 Assert(IsA(rootPathSpec->string, A_Const) &&
95 castNode(A_Const, rootPathSpec->string)->val.node.type == T_String);
96
97 if (jt->on_error &&
103 errmsg("invalid %s behavior", "ON ERROR"),
104 errdetail("Only EMPTY [ ARRAY ] or ERROR is allowed in the top-level ON ERROR clause."),
105 parser_errposition(pstate, jt->on_error->location));
106
107 cxt.pathNameId = 0;
108
109 /*
110 * Collect the user-supplied path and column names, checking that they are
111 * distinct. If the row pattern path has an explicit name, it shares this
112 * namespace, so seed the list with it.
113 */
114 if (rootPathSpec->name != NULL)
115 cxt.pathNames = list_make1(rootPathSpec->name);
117
118 /*
119 * Generate a name for the row pattern path if it was not given one. Path
120 * names are optional for every path, including when a PLAN clause is
121 * present; a specific PLAN() can only reference named paths, so an
122 * unnamed path that the plan must mention is caught later as a path name
123 * mismatch or a path not covered by the plan. We generate the name only
124 * after collecting the user-supplied names above, so that it cannot
125 * collide with any of them.
126 */
127 if (rootPathSpec->name == NULL)
129
130 /*
131 * We make lateral_only names of this level visible, whether or not the
132 * RangeTableFunc is explicitly marked LATERAL. This is needed for SQL
133 * spec compliance and seems useful on convenience grounds for all
134 * functions in FROM.
135 *
136 * (LATERAL can't nest within a single pstate level, so we don't need
137 * save/restore logic here.)
138 */
139 Assert(!pstate->p_lateral_active);
140 pstate->p_lateral_active = true;
141
142 tf = makeNode(TableFunc);
144
145 /*
146 * Transform JsonFuncExpr representing the top JSON_TABLE context_item and
147 * pathspec into a dummy JSON_TABLE_OP JsonExpr.
148 */
150 jfe->op = JSON_TABLE_OP;
151 jfe->context_item = jt->context_item;
152 jfe->pathspec = (Node *) rootPathSpec->string;
153 jfe->passing = jt->passing;
154 jfe->on_empty = NULL;
155 jfe->on_error = jt->on_error;
156 jfe->location = jt->location;
158
159 /*
160 * Create a JsonTablePlan that will generate row pattern that becomes
161 * source data for JSON path expressions in jt->columns. This also adds
162 * the columns' transformed JsonExpr nodes into tf->colvalexprs.
163 */
164 cxt.jt = jt;
165 cxt.tf = tf;
166 tf->plan = (Node *) transformJsonTableColumns(&cxt, plan, jt->columns,
167 jt->passing,
169
170 /*
171 * Copy the transformed PASSING arguments into the TableFunc node, because
172 * they are evaluated separately from the JsonExpr that we just put in
173 * TableFunc.docexpr. JsonExpr.passing_values is still kept around for
174 * get_json_table().
175 */
176 je = (JsonExpr *) tf->docexpr;
177 tf->passingvalexprs = copyObject(je->passing_values);
178
179 tf->ordinalitycol = -1; /* undefine ordinality column number */
180 tf->location = jt->location;
181
182 pstate->p_lateral_active = false;
183
184 /*
185 * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
186 * there are any lateral cross-references in it.
187 */
188 is_lateral = jt->lateral || contain_vars_of_level((Node *) tf, 0);
189
190 return addRangeTableEntryForTableFunc(pstate,
191 tf, jt->alias, is_lateral, true);
192}
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define castNode(_type_, nodeptr)
Definition nodes.h:180
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition parse_expr.c:121
static char * generateJsonTablePathName(JsonTableParseContext *cxt)
static JsonTablePlan * transformJsonTableColumns(JsonTableParseContext *cxt, JsonTablePlanSpec *planspec, 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)
#define plan(x)
Definition pg_regress.c:164
@ TFT_JSON_TABLE
Definition primnodes.h:102
@ JSON_BEHAVIOR_ERROR
Definition primnodes.h:1787
@ JSON_BEHAVIOR_EMPTY
Definition primnodes.h:1788
@ JSON_BEHAVIOR_EMPTY_ARRAY
Definition primnodes.h:1792
@ JSON_TABLE_OP
Definition primnodes.h:1826
ParseLoc location
Definition primnodes.h:1814
JsonBehaviorType btype
Definition primnodes.h:1811
JsonBehavior * on_error
List * columns
JsonTablePathSpec * pathspec
Alias * alias
List * passing
JsonTablePlanSpec * planspec
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, plan, JsonTable::planspec, 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 2029 of file parse_clause.c.

2032{
2033 Node *qual;
2034
2035 if (clause == NULL)
2036 return NULL;
2037
2038 qual = transformExpr(pstate, clause, exprKind);
2039
2040 qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
2041
2042 /* LIMIT can't refer to any variables of the current query */
2043 checkExprIsVarFree(pstate, qual, constructName);
2044
2045 /*
2046 * Don't allow NULLs in FETCH FIRST .. WITH TIES. This test is ugly and
2047 * extremely simplistic, in that you can pass a NULL anyway by hiding it
2048 * inside an expression -- but this protects ruleutils against emitting an
2049 * unadorned NULL that's not accepted back by the grammar.
2050 */
2051 if (exprKind == EXPR_KIND_LIMIT && limitOption == LIMIT_OPTION_WITH_TIES &&
2052 IsA(clause, A_Const) && castNode(A_Const, clause)->isnull)
2053 ereport(ERROR,
2055 errmsg("row count cannot be null in FETCH FIRST ... WITH TIES clause")));
2056
2057 return qual;
2058}
@ LIMIT_OPTION_WITH_TIES
Definition nodes.h:441
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 3450 of file parse_clause.c.

3454{
3455 InferClause *infer = onConflictClause->infer;
3456
3457 *arbiterExpr = NIL;
3458 *arbiterWhere = NULL;
3459 *constraint = InvalidOid;
3460
3461 if ((onConflictClause->action == ONCONFLICT_UPDATE ||
3462 onConflictClause->action == ONCONFLICT_SELECT) && !infer)
3463 ereport(ERROR,
3465 errmsg("ON CONFLICT DO %s requires inference specification or constraint name",
3466 onConflictClause->action == ONCONFLICT_UPDATE ? "UPDATE" : "SELECT"),
3467 errhint("For example, ON CONFLICT (column_name)."),
3468 parser_errposition(pstate,
3469 exprLocation((Node *) onConflictClause)));
3470
3471 /*
3472 * To simplify certain aspects of its design, speculative insertion into
3473 * system catalogs is disallowed
3474 */
3476 ereport(ERROR,
3478 errmsg("ON CONFLICT is not supported with system catalog tables"),
3479 parser_errposition(pstate,
3480 exprLocation((Node *) onConflictClause))));
3481
3482 /* Same applies to table used by logical decoding as catalog table */
3484 ereport(ERROR,
3486 errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
3488 parser_errposition(pstate,
3489 exprLocation((Node *) onConflictClause))));
3490
3491 /* ON CONFLICT DO NOTHING does not require an inference clause */
3492 if (infer)
3493 {
3494 if (infer->indexElems)
3495 *arbiterExpr = resolve_unique_index_expr(pstate, infer,
3496 pstate->p_target_relation);
3497
3498 /*
3499 * Handling inference WHERE clause (for partial unique index
3500 * inference)
3501 */
3502 if (infer->whereClause)
3503 *arbiterWhere = transformExpr(pstate, infer->whereClause,
3505
3506 /*
3507 * If the arbiter is specified by constraint name, get the constraint
3508 * OID and mark the constrained columns as requiring SELECT privilege,
3509 * in the same way as would have happened if the arbiter had been
3510 * specified by explicit reference to the constraint's index columns.
3511 */
3512 if (infer->conname)
3513 {
3514 Oid relid = RelationGetRelid(pstate->p_target_relation);
3517
3519 false, constraint);
3520
3521 /* Make sure the rel as a whole is marked for SELECT access */
3522 perminfo->requiredPerms |= ACL_SELECT;
3523 /* Mark the constrained columns as requiring SELECT access */
3524 perminfo->selectedCols = bms_add_members(perminfo->selectedCols,
3525 conattnos);
3526 }
3527 }
3528
3529 /*
3530 * It's convenient to form a list of expressions based on the
3531 * representation used by CREATE INDEX, since the same restrictions are
3532 * appropriate (e.g. on subqueries). However, from here on, a dedicated
3533 * primnode representation is used for inference elements, and so
3534 * assign_query_collations() can be trusted to do the right thing with the
3535 * post parse analysis query tree inference clause representation.
3536 */
3537}
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1036
bool IsCatalogRelation(Relation relation)
Definition catalog.c:106
@ ONCONFLICT_SELECT
Definition nodes.h:429
@ ONCONFLICT_UPDATE
Definition nodes.h:428
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 2880 of file parse_clause.c.

2885{
2886 List *sortlist = NIL;
2888
2889 foreach(olitem, orderlist)
2890 {
2893
2894 if (useSQL99)
2895 tle = findTargetlistEntrySQL99(pstate, sortby->node,
2896 targetlist, exprKind);
2897 else
2898 tle = findTargetlistEntrySQL92(pstate, sortby->node,
2899 targetlist, exprKind);
2900
2902 sortlist, *targetlist, sortby);
2903 }
2904
2905 return sortlist;
2906}
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 2002 of file parse_clause.c.

2004{
2005 Node *qual;
2006
2007 if (clause == NULL)
2008 return NULL;
2009
2010 qual = transformExpr(pstate, clause, exprKind);
2011
2012 qual = coerce_to_boolean(pstate, qual, constructName);
2013
2014 return qual;
2015}
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 2913 of file parse_clause.c.

2916{
2917 List *result = NIL;
2918 Index winref = 0;
2919 ListCell *lc;
2920
2921 foreach(lc, windowdefs)
2922 {
2925 List *partitionClause;
2926 List *orderClause;
2929 WindowClause *wc;
2930
2931 winref++;
2932
2933 /*
2934 * Check for duplicate window names.
2935 */
2936 if (windef->name &&
2938 ereport(ERROR,
2940 errmsg("window \"%s\" is already defined", windef->name),
2941 parser_errposition(pstate, windef->location)));
2942
2943 /*
2944 * If it references a previous window, look that up.
2945 */
2946 if (windef->refname)
2947 {
2948 refwc = findWindowClause(result, windef->refname);
2949 if (refwc == NULL)
2950 ereport(ERROR,
2952 errmsg("window \"%s\" does not exist",
2953 windef->refname),
2954 parser_errposition(pstate, windef->location)));
2955 }
2956
2957 /*
2958 * Transform PARTITION and ORDER specs, if any. These are treated
2959 * almost exactly like top-level GROUP BY and ORDER BY clauses,
2960 * including the special handling of nondefault operator semantics.
2961 */
2962 orderClause = transformSortClause(pstate,
2963 windef->orderClause,
2964 targetlist,
2966 true /* force SQL99 rules */ );
2967 partitionClause = transformGroupClause(pstate,
2968 windef->partitionClause,
2969 NULL,
2970 targetlist,
2971 orderClause,
2973 true /* force SQL99 rules */ );
2974
2975 /*
2976 * And prepare the new WindowClause.
2977 */
2978 wc = makeNode(WindowClause);
2979 wc->name = windef->name;
2980 wc->refname = windef->refname;
2981
2982 /*
2983 * Per spec, a windowdef that references a previous one copies the
2984 * previous partition clause (and mustn't specify its own). It can
2985 * specify its own ordering clause, but only if the previous one had
2986 * none. It always specifies its own frame clause, and the previous
2987 * one must not have a frame clause. Yeah, it's bizarre that each of
2988 * these cases works differently, but SQL:2008 says so; see 7.11
2989 * <window clause> syntax rule 10 and general rule 1. The frame
2990 * clause rule is especially bizarre because it makes "OVER foo"
2991 * different from "OVER (foo)", and requires the latter to throw an
2992 * error if foo has a nondefault frame clause. Well, ours not to
2993 * reason why, but we do go out of our way to throw a useful error
2994 * message for such cases.
2995 */
2996 if (refwc)
2997 {
2998 if (partitionClause)
2999 ereport(ERROR,
3001 errmsg("cannot override PARTITION BY clause of window \"%s\"",
3002 windef->refname),
3003 parser_errposition(pstate, windef->location)));
3004 wc->partitionClause = copyObject(refwc->partitionClause);
3005 }
3006 else
3007 wc->partitionClause = partitionClause;
3008 if (refwc)
3009 {
3010 if (orderClause && refwc->orderClause)
3011 ereport(ERROR,
3013 errmsg("cannot override ORDER BY clause of window \"%s\"",
3014 windef->refname),
3015 parser_errposition(pstate, windef->location)));
3016 if (orderClause)
3017 {
3018 wc->orderClause = orderClause;
3019 wc->copiedOrder = false;
3020 }
3021 else
3022 {
3023 wc->orderClause = copyObject(refwc->orderClause);
3024 wc->copiedOrder = true;
3025 }
3026 }
3027 else
3028 {
3029 wc->orderClause = orderClause;
3030 wc->copiedOrder = false;
3031 }
3032 if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
3033 {
3034 /*
3035 * Use this message if this is a WINDOW clause, or if it's an OVER
3036 * clause that includes ORDER BY or framing clauses. (We already
3037 * rejected PARTITION BY above, so no need to check that.)
3038 */
3039 if (windef->name ||
3040 orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
3041 ereport(ERROR,
3043 errmsg("cannot copy window \"%s\" because it has a frame clause",
3044 windef->refname),
3045 parser_errposition(pstate, windef->location)));
3046 /* Else this clause is just OVER (foo), so say this: */
3047 ereport(ERROR,
3049 errmsg("cannot copy window \"%s\" because it has a frame clause",
3050 windef->refname),
3051 errhint("Omit the parentheses in this OVER clause."),
3052 parser_errposition(pstate, windef->location)));
3053 }
3054 wc->frameOptions = windef->frameOptions;
3055
3056 /*
3057 * RANGE offset PRECEDING/FOLLOWING requires exactly one ORDER BY
3058 * column; check that and get its sort opfamily info.
3059 */
3060 if ((wc->frameOptions & FRAMEOPTION_RANGE) &&
3063 {
3065 Node *sortkey;
3067
3068 if (list_length(wc->orderClause) != 1)
3069 ereport(ERROR,
3071 errmsg("RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column"),
3072 parser_errposition(pstate, windef->location)));
3074 sortkey = get_sortgroupclause_expr(sortcl, *targetlist);
3075 /* Find the sort operator in pg_amop */
3079 &rangecmptype))
3080 elog(ERROR, "operator %u is not a valid ordering operator",
3081 sortcl->sortop);
3082 /* Record properties of sort ordering */
3083 wc->inRangeColl = exprCollation(sortkey);
3084 wc->inRangeAsc = !sortcl->reverse_sort;
3085 wc->inRangeNullsFirst = sortcl->nulls_first;
3086 }
3087
3088 /* Per spec, GROUPS mode requires an ORDER BY clause */
3090 {
3091 if (wc->orderClause == NIL)
3092 ereport(ERROR,
3094 errmsg("GROUPS mode requires an ORDER BY clause"),
3095 parser_errposition(pstate, windef->location)));
3096 }
3097
3098 /* Process frame offset expressions */
3101 &wc->startInRangeFunc,
3105 &wc->endInRangeFunc,
3106 windef->endOffset);
3107 wc->winref = winref;
3108
3109 result = lappend(result, wc);
3110 }
3111
3112 return result;
3113}
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, 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:634
#define FRAMEOPTION_START_OFFSET
Definition parsenodes.h:632
#define FRAMEOPTION_RANGE
Definition parsenodes.h:614
#define FRAMEOPTION_GROUPS
Definition parsenodes.h:616
#define FRAMEOPTION_DEFAULTS
Definition parsenodes.h:640
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().