1 /*-------------------------------------------------------------------------
2  *
3  * parse_clause.c
4  *	  handle clauses in parser
5  *
6  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *	  src/backend/parser/parse_clause.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "miscadmin.h"
19 
20 #include "access/heapam.h"
21 #include "access/htup_details.h"
22 #include "access/nbtree.h"
23 #include "access/tsmapi.h"
24 #include "catalog/catalog.h"
25 #include "catalog/heap.h"
26 #include "catalog/pg_am.h"
27 #include "catalog/pg_amproc.h"
28 #include "catalog/pg_collation.h"
29 #include "catalog/pg_constraint.h"
30 #include "catalog/pg_type.h"
31 #include "commands/defrem.h"
32 #include "nodes/makefuncs.h"
33 #include "nodes/nodeFuncs.h"
34 #include "optimizer/tlist.h"
35 #include "optimizer/var.h"
36 #include "parser/analyze.h"
37 #include "parser/parsetree.h"
38 #include "parser/parser.h"
39 #include "parser/parse_clause.h"
40 #include "parser/parse_coerce.h"
41 #include "parser/parse_collate.h"
42 #include "parser/parse_expr.h"
43 #include "parser/parse_func.h"
44 #include "parser/parse_oper.h"
45 #include "parser/parse_relation.h"
46 #include "parser/parse_target.h"
47 #include "parser/parse_type.h"
48 #include "rewrite/rewriteManip.h"
49 #include "utils/builtins.h"
50 #include "utils/guc.h"
51 #include "utils/catcache.h"
52 #include "utils/lsyscache.h"
53 #include "utils/syscache.h"
54 #include "utils/rel.h"
55 
56 
57 /* Convenience macro for the most common makeNamespaceItem() case */
58 #define makeDefaultNSItem(rte)	makeNamespaceItem(rte, true, true, false, true)
59 
60 static void extractRemainingColumns(List *common_colnames,
61 						List *src_colnames, List *src_colvars,
62 						List **res_colnames, List **res_colvars);
63 static Node *transformJoinUsingClause(ParseState *pstate,
64 						 RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
65 						 List *leftVars, List *rightVars);
66 static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
67 					  List *namespace);
68 static RangeTblEntry *getRTEForSpecialRelationTypes(ParseState *pstate,
69 							  RangeVar *rv);
70 static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
71 static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
72 						RangeSubselect *r);
73 static RangeTblEntry *transformRangeFunction(ParseState *pstate,
74 					   RangeFunction *r);
75 static RangeTblEntry *transformRangeTableFunc(ParseState *pstate,
76 						RangeTableFunc *t);
77 static TableSampleClause *transformRangeTableSample(ParseState *pstate,
78 						  RangeTableSample *rts);
79 static Node *transformFromClauseItem(ParseState *pstate, Node *n,
80 						RangeTblEntry **top_rte, int *top_rti,
81 						List **namespace);
82 static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
83 				   Var *l_colvar, Var *r_colvar);
84 static ParseNamespaceItem *makeNamespaceItem(RangeTblEntry *rte,
85 				  bool rel_visible, bool cols_visible,
86 				  bool lateral_only, bool lateral_ok);
87 static void setNamespaceColumnVisibility(List *namespace, bool cols_visible);
88 static void setNamespaceLateralState(List *namespace,
89 						 bool lateral_only, bool lateral_ok);
90 static void checkExprIsVarFree(ParseState *pstate, Node *n,
91 				   const char *constructName);
92 static TargetEntry *findTargetlistEntrySQL92(ParseState *pstate, Node *node,
93 						 List **tlist, ParseExprKind exprKind);
94 static TargetEntry *findTargetlistEntrySQL99(ParseState *pstate, Node *node,
95 						 List **tlist, ParseExprKind exprKind);
96 static int get_matching_location(int sortgroupref,
97 					  List *sortgrouprefs, List *exprs);
98 static List *resolve_unique_index_expr(ParseState *pstate, InferClause *infer,
99 						  Relation heapRel);
100 static List *addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
101 					 List *grouplist, List *targetlist, int location);
102 static WindowClause *findWindowClause(List *wclist, const char *name);
103 static Node *transformFrameOffset(ParseState *pstate, int frameOptions,
104 					 Oid rangeopfamily, Oid rangeopcintype, Oid *inRangeFunc,
105 					 Node *clause);
106 
107 
108 /*
109  * transformFromClause -
110  *	  Process the FROM clause and add items to the query's range table,
111  *	  joinlist, and namespace.
112  *
113  * Note: we assume that the pstate's p_rtable, p_joinlist, and p_namespace
114  * lists were initialized to NIL when the pstate was created.
115  * We will add onto any entries already present --- this is needed for rule
116  * processing, as well as for UPDATE and DELETE.
117  */
118 void
transformFromClause(ParseState * pstate,List * frmList)119 transformFromClause(ParseState *pstate, List *frmList)
120 {
121 	ListCell   *fl;
122 
123 	/*
124 	 * The grammar will have produced a list of RangeVars, RangeSubselects,
125 	 * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
126 	 * entries to the rtable), check for duplicate refnames, and then add it
127 	 * to the joinlist and namespace.
128 	 *
129 	 * Note we must process the items left-to-right for proper handling of
130 	 * LATERAL references.
131 	 */
132 	foreach(fl, frmList)
133 	{
134 		Node	   *n = lfirst(fl);
135 		RangeTblEntry *rte;
136 		int			rtindex;
137 		List	   *namespace;
138 
139 		n = transformFromClauseItem(pstate, n,
140 									&rte,
141 									&rtindex,
142 									&namespace);
143 
144 		checkNameSpaceConflicts(pstate, pstate->p_namespace, namespace);
145 
146 		/* Mark the new namespace items as visible only to LATERAL */
147 		setNamespaceLateralState(namespace, true, true);
148 
149 		pstate->p_joinlist = lappend(pstate->p_joinlist, n);
150 		pstate->p_namespace = list_concat(pstate->p_namespace, namespace);
151 	}
152 
153 	/*
154 	 * We're done parsing the FROM list, so make all namespace items
155 	 * unconditionally visible.  Note that this will also reset lateral_only
156 	 * for any namespace items that were already present when we were called;
157 	 * but those should have been that way already.
158 	 */
159 	setNamespaceLateralState(pstate->p_namespace, false, true);
160 }
161 
162 /*
163  * setTargetTable
164  *	  Add the target relation of INSERT/UPDATE/DELETE to the range table,
165  *	  and make the special links to it in the ParseState.
166  *
167  *	  We also open the target relation and acquire a write lock on it.
168  *	  This must be done before processing the FROM list, in case the target
169  *	  is also mentioned as a source relation --- we want to be sure to grab
170  *	  the write lock before any read lock.
171  *
172  *	  If alsoSource is true, add the target to the query's joinlist and
173  *	  namespace.  For INSERT, we don't want the target to be joined to;
174  *	  it's a destination of tuples, not a source.   For UPDATE/DELETE,
175  *	  we do need to scan or join the target.  (NOTE: we do not bother
176  *	  to check for namespace conflict; we assume that the namespace was
177  *	  initially empty in these cases.)
178  *
179  *	  Finally, we mark the relation as requiring the permissions specified
180  *	  by requiredPerms.
181  *
182  *	  Returns the rangetable index of the target relation.
183  */
184 int
setTargetTable(ParseState * pstate,RangeVar * relation,bool inh,bool alsoSource,AclMode requiredPerms)185 setTargetTable(ParseState *pstate, RangeVar *relation,
186 			   bool inh, bool alsoSource, AclMode requiredPerms)
187 {
188 	RangeTblEntry *rte;
189 	int			rtindex;
190 
191 	/*
192 	 * ENRs hide tables of the same name, so we need to check for them first.
193 	 * In contrast, CTEs don't hide tables (for this purpose).
194 	 */
195 	if (relation->schemaname == NULL &&
196 		scanNameSpaceForENR(pstate, relation->relname))
197 		ereport(ERROR,
198 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
199 				 errmsg("relation \"%s\" cannot be the target of a modifying statement",
200 						relation->relname)));
201 
202 	/* Close old target; this could only happen for multi-action rules */
203 	if (pstate->p_target_relation != NULL)
204 		heap_close(pstate->p_target_relation, NoLock);
205 
206 	/*
207 	 * Open target rel and grab suitable lock (which we will hold till end of
208 	 * transaction).
209 	 *
210 	 * free_parsestate() will eventually do the corresponding heap_close(),
211 	 * but *not* release the lock.
212 	 */
213 	pstate->p_target_relation = parserOpenTable(pstate, relation,
214 												RowExclusiveLock);
215 
216 	/*
217 	 * Now build an RTE.
218 	 */
219 	rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
220 										relation->alias, inh, false);
221 	pstate->p_target_rangetblentry = rte;
222 
223 	/* assume new rte is at end */
224 	rtindex = list_length(pstate->p_rtable);
225 	Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
226 
227 	/*
228 	 * Override addRangeTableEntry's default ACL_SELECT permissions check, and
229 	 * instead mark target table as requiring exactly the specified
230 	 * permissions.
231 	 *
232 	 * If we find an explicit reference to the rel later during parse
233 	 * analysis, we will add the ACL_SELECT bit back again; see
234 	 * markVarForSelectPriv and its callers.
235 	 */
236 	rte->requiredPerms = requiredPerms;
237 
238 	/*
239 	 * If UPDATE/DELETE, add table to joinlist and namespace.
240 	 *
241 	 * Note: some callers know that they can find the new ParseNamespaceItem
242 	 * at the end of the pstate->p_namespace list.  This is a bit ugly but not
243 	 * worth complicating this function's signature for.
244 	 */
245 	if (alsoSource)
246 		addRTEtoQuery(pstate, rte, true, true, true);
247 
248 	return rtindex;
249 }
250 
251 /*
252  * Given a relation-options list (of DefElems), return true iff the specified
253  * table/result set should be created with OIDs. This needs to be done after
254  * parsing the query string because the return value can depend upon the
255  * default_with_oids GUC var.
256  *
257  * In some situations, we want to reject an OIDS option even if it's present.
258  * That's (rather messily) handled here rather than reloptions.c, because that
259  * code explicitly punts checking for oids to here.
260  */
261 bool
interpretOidsOption(List * defList,bool allowOids)262 interpretOidsOption(List *defList, bool allowOids)
263 {
264 	ListCell   *cell;
265 
266 	/* Scan list to see if OIDS was included */
267 	foreach(cell, defList)
268 	{
269 		DefElem    *def = (DefElem *) lfirst(cell);
270 
271 		if (def->defnamespace == NULL &&
272 			strcmp(def->defname, "oids") == 0)
273 		{
274 			if (!allowOids)
275 				ereport(ERROR,
276 						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
277 						 errmsg("unrecognized parameter \"%s\"",
278 								def->defname)));
279 			return defGetBoolean(def);
280 		}
281 	}
282 
283 	/* Force no-OIDS result if caller disallows OIDS. */
284 	if (!allowOids)
285 		return false;
286 
287 	/* OIDS option was not specified, so use default. */
288 	return default_with_oids;
289 }
290 
291 /*
292  * Extract all not-in-common columns from column lists of a source table
293  */
294 static void
extractRemainingColumns(List * common_colnames,List * src_colnames,List * src_colvars,List ** res_colnames,List ** res_colvars)295 extractRemainingColumns(List *common_colnames,
296 						List *src_colnames, List *src_colvars,
297 						List **res_colnames, List **res_colvars)
298 {
299 	List	   *new_colnames = NIL;
300 	List	   *new_colvars = NIL;
301 	ListCell   *lnames,
302 			   *lvars;
303 
304 	Assert(list_length(src_colnames) == list_length(src_colvars));
305 
306 	forboth(lnames, src_colnames, lvars, src_colvars)
307 	{
308 		char	   *colname = strVal(lfirst(lnames));
309 		bool		match = false;
310 		ListCell   *cnames;
311 
312 		foreach(cnames, common_colnames)
313 		{
314 			char	   *ccolname = strVal(lfirst(cnames));
315 
316 			if (strcmp(colname, ccolname) == 0)
317 			{
318 				match = true;
319 				break;
320 			}
321 		}
322 
323 		if (!match)
324 		{
325 			new_colnames = lappend(new_colnames, lfirst(lnames));
326 			new_colvars = lappend(new_colvars, lfirst(lvars));
327 		}
328 	}
329 
330 	*res_colnames = new_colnames;
331 	*res_colvars = new_colvars;
332 }
333 
334 /* transformJoinUsingClause()
335  *	  Build a complete ON clause from a partially-transformed USING list.
336  *	  We are given lists of nodes representing left and right match columns.
337  *	  Result is a transformed qualification expression.
338  */
339 static Node *
transformJoinUsingClause(ParseState * pstate,RangeTblEntry * leftRTE,RangeTblEntry * rightRTE,List * leftVars,List * rightVars)340 transformJoinUsingClause(ParseState *pstate,
341 						 RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
342 						 List *leftVars, List *rightVars)
343 {
344 	Node	   *result;
345 	List	   *andargs = NIL;
346 	ListCell   *lvars,
347 			   *rvars;
348 
349 	/*
350 	 * We cheat a little bit here by building an untransformed operator tree
351 	 * whose leaves are the already-transformed Vars.  This requires collusion
352 	 * from transformExpr(), which normally could be expected to complain
353 	 * about already-transformed subnodes.  However, this does mean that we
354 	 * have to mark the columns as requiring SELECT privilege for ourselves;
355 	 * transformExpr() won't do it.
356 	 */
357 	forboth(lvars, leftVars, rvars, rightVars)
358 	{
359 		Var		   *lvar = (Var *) lfirst(lvars);
360 		Var		   *rvar = (Var *) lfirst(rvars);
361 		A_Expr	   *e;
362 
363 		/* Require read access to the join variables */
364 		markVarForSelectPriv(pstate, lvar, leftRTE);
365 		markVarForSelectPriv(pstate, rvar, rightRTE);
366 
367 		/* Now create the lvar = rvar join condition */
368 		e = makeSimpleA_Expr(AEXPR_OP, "=",
369 							 (Node *) copyObject(lvar), (Node *) copyObject(rvar),
370 							 -1);
371 
372 		/* Prepare to combine into an AND clause, if multiple join columns */
373 		andargs = lappend(andargs, e);
374 	}
375 
376 	/* Only need an AND if there's more than one join column */
377 	if (list_length(andargs) == 1)
378 		result = (Node *) linitial(andargs);
379 	else
380 		result = (Node *) makeBoolExpr(AND_EXPR, andargs, -1);
381 
382 	/*
383 	 * Since the references are already Vars, and are certainly from the input
384 	 * relations, we don't have to go through the same pushups that
385 	 * transformJoinOnClause() does.  Just invoke transformExpr() to fix up
386 	 * the operators, and we're done.
387 	 */
388 	result = transformExpr(pstate, result, EXPR_KIND_JOIN_USING);
389 
390 	result = coerce_to_boolean(pstate, result, "JOIN/USING");
391 
392 	return result;
393 }
394 
395 /* transformJoinOnClause()
396  *	  Transform the qual conditions for JOIN/ON.
397  *	  Result is a transformed qualification expression.
398  */
399 static Node *
transformJoinOnClause(ParseState * pstate,JoinExpr * j,List * namespace)400 transformJoinOnClause(ParseState *pstate, JoinExpr *j, List *namespace)
401 {
402 	Node	   *result;
403 	List	   *save_namespace;
404 
405 	/*
406 	 * The namespace that the join expression should see is just the two
407 	 * subtrees of the JOIN plus any outer references from upper pstate
408 	 * levels.  Temporarily set this pstate's namespace accordingly.  (We need
409 	 * not check for refname conflicts, because transformFromClauseItem()
410 	 * already did.)  All namespace items are marked visible regardless of
411 	 * LATERAL state.
412 	 */
413 	setNamespaceLateralState(namespace, false, true);
414 
415 	save_namespace = pstate->p_namespace;
416 	pstate->p_namespace = namespace;
417 
418 	result = transformWhereClause(pstate, j->quals,
419 								  EXPR_KIND_JOIN_ON, "JOIN/ON");
420 
421 	pstate->p_namespace = save_namespace;
422 
423 	return result;
424 }
425 
426 /*
427  * transformTableEntry --- transform a RangeVar (simple relation reference)
428  */
429 static RangeTblEntry *
transformTableEntry(ParseState * pstate,RangeVar * r)430 transformTableEntry(ParseState *pstate, RangeVar *r)
431 {
432 	RangeTblEntry *rte;
433 
434 	/* We need only build a range table entry */
435 	rte = addRangeTableEntry(pstate, r, r->alias, r->inh, true);
436 
437 	return rte;
438 }
439 
440 /*
441  * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
442  */
443 static RangeTblEntry *
transformRangeSubselect(ParseState * pstate,RangeSubselect * r)444 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
445 {
446 	Query	   *query;
447 	RangeTblEntry *rte;
448 
449 	/*
450 	 * We require user to supply an alias for a subselect, per SQL92. To relax
451 	 * this, we'd have to be prepared to gin up a unique alias for an
452 	 * unlabeled subselect.  (This is just elog, not ereport, because the
453 	 * grammar should have enforced it already.  It'd probably be better to
454 	 * report the error here, but we don't have a good error location here.)
455 	 */
456 	if (r->alias == NULL)
457 		elog(ERROR, "subquery in FROM must have an alias");
458 
459 	/*
460 	 * Set p_expr_kind to show this parse level is recursing to a subselect.
461 	 * We can't be nested within any expression, so don't need save-restore
462 	 * logic here.
463 	 */
464 	Assert(pstate->p_expr_kind == EXPR_KIND_NONE);
465 	pstate->p_expr_kind = EXPR_KIND_FROM_SUBSELECT;
466 
467 	/*
468 	 * If the subselect is LATERAL, make lateral_only names of this level
469 	 * visible to it.  (LATERAL can't nest within a single pstate level, so we
470 	 * don't need save/restore logic here.)
471 	 */
472 	Assert(!pstate->p_lateral_active);
473 	pstate->p_lateral_active = r->lateral;
474 
475 	/*
476 	 * Analyze and transform the subquery.
477 	 */
478 	query = parse_sub_analyze(r->subquery, pstate, NULL,
479 							  isLockedRefname(pstate, r->alias->aliasname),
480 							  true);
481 
482 	/* Restore state */
483 	pstate->p_lateral_active = false;
484 	pstate->p_expr_kind = EXPR_KIND_NONE;
485 
486 	/*
487 	 * Check that we got a SELECT.  Anything else should be impossible given
488 	 * restrictions of the grammar, but check anyway.
489 	 */
490 	if (!IsA(query, Query) ||
491 		query->commandType != CMD_SELECT)
492 		elog(ERROR, "unexpected non-SELECT command in subquery in FROM");
493 
494 	/*
495 	 * OK, build an RTE for the subquery.
496 	 */
497 	rte = addRangeTableEntryForSubquery(pstate,
498 										query,
499 										r->alias,
500 										r->lateral,
501 										true);
502 
503 	return rte;
504 }
505 
506 
507 /*
508  * transformRangeFunction --- transform a function call appearing in FROM
509  */
510 static RangeTblEntry *
transformRangeFunction(ParseState * pstate,RangeFunction * r)511 transformRangeFunction(ParseState *pstate, RangeFunction *r)
512 {
513 	List	   *funcexprs = NIL;
514 	List	   *funcnames = NIL;
515 	List	   *coldeflists = NIL;
516 	bool		is_lateral;
517 	RangeTblEntry *rte;
518 	ListCell   *lc;
519 
520 	/*
521 	 * We make lateral_only names of this level visible, whether or not the
522 	 * RangeFunction is explicitly marked LATERAL.  This is needed for SQL
523 	 * spec compliance in the case of UNNEST(), and seems useful on
524 	 * convenience grounds for all functions in FROM.
525 	 *
526 	 * (LATERAL can't nest within a single pstate level, so we don't need
527 	 * save/restore logic here.)
528 	 */
529 	Assert(!pstate->p_lateral_active);
530 	pstate->p_lateral_active = true;
531 
532 	/*
533 	 * Transform the raw expressions.
534 	 *
535 	 * While transforming, also save function names for possible use as alias
536 	 * and column names.  We use the same transformation rules as for a SELECT
537 	 * output expression.  For a FuncCall node, the result will be the
538 	 * function name, but it is possible for the grammar to hand back other
539 	 * node types.
540 	 *
541 	 * We have to get this info now, because FigureColname only works on raw
542 	 * parsetrees.  Actually deciding what to do with the names is left up to
543 	 * addRangeTableEntryForFunction.
544 	 *
545 	 * Likewise, collect column definition lists if there were any.  But
546 	 * complain if we find one here and the RangeFunction has one too.
547 	 */
548 	foreach(lc, r->functions)
549 	{
550 		List	   *pair = (List *) lfirst(lc);
551 		Node	   *fexpr;
552 		List	   *coldeflist;
553 		Node	   *newfexpr;
554 		Node	   *last_srf;
555 
556 		/* Disassemble the function-call/column-def-list pairs */
557 		Assert(list_length(pair) == 2);
558 		fexpr = (Node *) linitial(pair);
559 		coldeflist = (List *) lsecond(pair);
560 
561 		/*
562 		 * If we find a function call unnest() with more than one argument and
563 		 * no special decoration, transform it into separate unnest() calls on
564 		 * each argument.  This is a kluge, for sure, but it's less nasty than
565 		 * other ways of implementing the SQL-standard UNNEST() syntax.
566 		 *
567 		 * If there is any decoration (including a coldeflist), we don't
568 		 * transform, which probably means a no-such-function error later.  We
569 		 * could alternatively throw an error right now, but that doesn't seem
570 		 * tremendously helpful.  If someone is using any such decoration,
571 		 * then they're not using the SQL-standard syntax, and they're more
572 		 * likely expecting an un-tweaked function call.
573 		 *
574 		 * Note: the transformation changes a non-schema-qualified unnest()
575 		 * function name into schema-qualified pg_catalog.unnest().  This
576 		 * choice is also a bit debatable, but it seems reasonable to force
577 		 * use of built-in unnest() when we make this transformation.
578 		 */
579 		if (IsA(fexpr, FuncCall))
580 		{
581 			FuncCall   *fc = (FuncCall *) fexpr;
582 
583 			if (list_length(fc->funcname) == 1 &&
584 				strcmp(strVal(linitial(fc->funcname)), "unnest") == 0 &&
585 				list_length(fc->args) > 1 &&
586 				fc->agg_order == NIL &&
587 				fc->agg_filter == NULL &&
588 				!fc->agg_star &&
589 				!fc->agg_distinct &&
590 				!fc->func_variadic &&
591 				fc->over == NULL &&
592 				coldeflist == NIL)
593 			{
594 				ListCell   *lc;
595 
596 				foreach(lc, fc->args)
597 				{
598 					Node	   *arg = (Node *) lfirst(lc);
599 					FuncCall   *newfc;
600 
601 					last_srf = pstate->p_last_srf;
602 
603 					newfc = makeFuncCall(SystemFuncName("unnest"),
604 										 list_make1(arg),
605 										 fc->location);
606 
607 					newfexpr = transformExpr(pstate, (Node *) newfc,
608 											 EXPR_KIND_FROM_FUNCTION);
609 
610 					/* nodeFunctionscan.c requires SRFs to be at top level */
611 					if (pstate->p_last_srf != last_srf &&
612 						pstate->p_last_srf != newfexpr)
613 						ereport(ERROR,
614 								(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
615 								 errmsg("set-returning functions must appear at top level of FROM"),
616 								 parser_errposition(pstate,
617 													exprLocation(pstate->p_last_srf))));
618 
619 					funcexprs = lappend(funcexprs, newfexpr);
620 
621 					funcnames = lappend(funcnames,
622 										FigureColname((Node *) newfc));
623 
624 					/* coldeflist is empty, so no error is possible */
625 
626 					coldeflists = lappend(coldeflists, coldeflist);
627 				}
628 				continue;		/* done with this function item */
629 			}
630 		}
631 
632 		/* normal case ... */
633 		last_srf = pstate->p_last_srf;
634 
635 		newfexpr = transformExpr(pstate, fexpr,
636 								 EXPR_KIND_FROM_FUNCTION);
637 
638 		/* nodeFunctionscan.c requires SRFs to be at top level */
639 		if (pstate->p_last_srf != last_srf &&
640 			pstate->p_last_srf != newfexpr)
641 			ereport(ERROR,
642 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
643 					 errmsg("set-returning functions must appear at top level of FROM"),
644 					 parser_errposition(pstate,
645 										exprLocation(pstate->p_last_srf))));
646 
647 		funcexprs = lappend(funcexprs, newfexpr);
648 
649 		funcnames = lappend(funcnames,
650 							FigureColname(fexpr));
651 
652 		if (coldeflist && r->coldeflist)
653 			ereport(ERROR,
654 					(errcode(ERRCODE_SYNTAX_ERROR),
655 					 errmsg("multiple column definition lists are not allowed for the same function"),
656 					 parser_errposition(pstate,
657 										exprLocation((Node *) r->coldeflist))));
658 
659 		coldeflists = lappend(coldeflists, coldeflist);
660 	}
661 
662 	pstate->p_lateral_active = false;
663 
664 	/*
665 	 * We must assign collations now so that the RTE exposes correct collation
666 	 * info for Vars created from it.
667 	 */
668 	assign_list_collations(pstate, funcexprs);
669 
670 	/*
671 	 * Install the top-level coldeflist if there was one (we already checked
672 	 * that there was no conflicting per-function coldeflist).
673 	 *
674 	 * We only allow this when there's a single function (even after UNNEST
675 	 * expansion) and no WITH ORDINALITY.  The reason for the latter
676 	 * restriction is that it's not real clear whether the ordinality column
677 	 * should be in the coldeflist, and users are too likely to make mistakes
678 	 * in one direction or the other.  Putting the coldeflist inside ROWS
679 	 * FROM() is much clearer in this case.
680 	 */
681 	if (r->coldeflist)
682 	{
683 		if (list_length(funcexprs) != 1)
684 		{
685 			if (r->is_rowsfrom)
686 				ereport(ERROR,
687 						(errcode(ERRCODE_SYNTAX_ERROR),
688 						 errmsg("ROWS FROM() with multiple functions cannot have a column definition list"),
689 						 errhint("Put a separate column definition list for each function inside ROWS FROM()."),
690 						 parser_errposition(pstate,
691 											exprLocation((Node *) r->coldeflist))));
692 			else
693 				ereport(ERROR,
694 						(errcode(ERRCODE_SYNTAX_ERROR),
695 						 errmsg("UNNEST() with multiple arguments cannot have a column definition list"),
696 						 errhint("Use separate UNNEST() calls inside ROWS FROM(), and attach a column definition list to each one."),
697 						 parser_errposition(pstate,
698 											exprLocation((Node *) r->coldeflist))));
699 		}
700 		if (r->ordinality)
701 			ereport(ERROR,
702 					(errcode(ERRCODE_SYNTAX_ERROR),
703 					 errmsg("WITH ORDINALITY cannot be used with a column definition list"),
704 					 errhint("Put the column definition list inside ROWS FROM()."),
705 					 parser_errposition(pstate,
706 										exprLocation((Node *) r->coldeflist))));
707 
708 		coldeflists = list_make1(r->coldeflist);
709 	}
710 
711 	/*
712 	 * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
713 	 * there are any lateral cross-references in it.
714 	 */
715 	is_lateral = r->lateral || contain_vars_of_level((Node *) funcexprs, 0);
716 
717 	/*
718 	 * OK, build an RTE for the function.
719 	 */
720 	rte = addRangeTableEntryForFunction(pstate,
721 										funcnames, funcexprs, coldeflists,
722 										r, is_lateral, true);
723 
724 	return rte;
725 }
726 
727 /*
728  * transformRangeTableFunc -
729  *			Transform a raw RangeTableFunc into TableFunc.
730  *
731  * Transform the namespace clauses, the document-generating expression, the
732  * row-generating expression, the column-generating expressions, and the
733  * default value expressions.
734  */
735 static RangeTblEntry *
transformRangeTableFunc(ParseState * pstate,RangeTableFunc * rtf)736 transformRangeTableFunc(ParseState *pstate, RangeTableFunc *rtf)
737 {
738 	TableFunc  *tf = makeNode(TableFunc);
739 	const char *constructName;
740 	Oid			docType;
741 	RangeTblEntry *rte;
742 	bool		is_lateral;
743 	ListCell   *col;
744 	char	  **names;
745 	int			colno;
746 
747 	/* Currently only XMLTABLE is supported */
748 	constructName = "XMLTABLE";
749 	docType = XMLOID;
750 
751 	/*
752 	 * We make lateral_only names of this level visible, whether or not the
753 	 * RangeTableFunc is explicitly marked LATERAL.  This is needed for SQL
754 	 * spec compliance and seems useful on convenience grounds for all
755 	 * functions in FROM.
756 	 *
757 	 * (LATERAL can't nest within a single pstate level, so we don't need
758 	 * save/restore logic here.)
759 	 */
760 	Assert(!pstate->p_lateral_active);
761 	pstate->p_lateral_active = true;
762 
763 	/* Transform and apply typecast to the row-generating expression ... */
764 	Assert(rtf->rowexpr != NULL);
765 	tf->rowexpr = coerce_to_specific_type(pstate,
766 										  transformExpr(pstate, rtf->rowexpr, EXPR_KIND_FROM_FUNCTION),
767 										  TEXTOID,
768 										  constructName);
769 	assign_expr_collations(pstate, tf->rowexpr);
770 
771 	/* ... and to the document itself */
772 	Assert(rtf->docexpr != NULL);
773 	tf->docexpr = coerce_to_specific_type(pstate,
774 										  transformExpr(pstate, rtf->docexpr, EXPR_KIND_FROM_FUNCTION),
775 										  docType,
776 										  constructName);
777 	assign_expr_collations(pstate, tf->docexpr);
778 
779 	/* undef ordinality column number */
780 	tf->ordinalitycol = -1;
781 
782 	/* Process column specs */
783 	names = palloc(sizeof(char *) * list_length(rtf->columns));
784 
785 	colno = 0;
786 	foreach(col, rtf->columns)
787 	{
788 		RangeTableFuncCol *rawc = (RangeTableFuncCol *) lfirst(col);
789 		Oid			typid;
790 		int32		typmod;
791 		Node	   *colexpr;
792 		Node	   *coldefexpr;
793 		int			j;
794 
795 		tf->colnames = lappend(tf->colnames,
796 							   makeString(pstrdup(rawc->colname)));
797 
798 		/*
799 		 * Determine the type and typmod for the new column. FOR ORDINALITY
800 		 * columns are INTEGER per spec; the others are user-specified.
801 		 */
802 		if (rawc->for_ordinality)
803 		{
804 			if (tf->ordinalitycol != -1)
805 				ereport(ERROR,
806 						(errcode(ERRCODE_SYNTAX_ERROR),
807 						 errmsg("only one FOR ORDINALITY column is allowed"),
808 						 parser_errposition(pstate, rawc->location)));
809 
810 			typid = INT4OID;
811 			typmod = -1;
812 			tf->ordinalitycol = colno;
813 		}
814 		else
815 		{
816 			if (rawc->typeName->setof)
817 				ereport(ERROR,
818 						(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
819 						 errmsg("column \"%s\" cannot be declared SETOF",
820 								rawc->colname),
821 						 parser_errposition(pstate, rawc->location)));
822 
823 			typenameTypeIdAndMod(pstate, rawc->typeName,
824 								 &typid, &typmod);
825 		}
826 
827 		tf->coltypes = lappend_oid(tf->coltypes, typid);
828 		tf->coltypmods = lappend_int(tf->coltypmods, typmod);
829 		tf->colcollations = lappend_oid(tf->colcollations,
830 										type_is_collatable(typid) ? DEFAULT_COLLATION_OID : InvalidOid);
831 
832 		/* Transform the PATH and DEFAULT expressions */
833 		if (rawc->colexpr)
834 		{
835 			colexpr = coerce_to_specific_type(pstate,
836 											  transformExpr(pstate, rawc->colexpr,
837 															EXPR_KIND_FROM_FUNCTION),
838 											  TEXTOID,
839 											  constructName);
840 			assign_expr_collations(pstate, colexpr);
841 		}
842 		else
843 			colexpr = NULL;
844 
845 		if (rawc->coldefexpr)
846 		{
847 			coldefexpr = coerce_to_specific_type_typmod(pstate,
848 														transformExpr(pstate, rawc->coldefexpr,
849 																	  EXPR_KIND_FROM_FUNCTION),
850 														typid, typmod,
851 														constructName);
852 			assign_expr_collations(pstate, coldefexpr);
853 		}
854 		else
855 			coldefexpr = NULL;
856 
857 		tf->colexprs = lappend(tf->colexprs, colexpr);
858 		tf->coldefexprs = lappend(tf->coldefexprs, coldefexpr);
859 
860 		if (rawc->is_not_null)
861 			tf->notnulls = bms_add_member(tf->notnulls, colno);
862 
863 		/* make sure column names are unique */
864 		for (j = 0; j < colno; j++)
865 			if (strcmp(names[j], rawc->colname) == 0)
866 				ereport(ERROR,
867 						(errcode(ERRCODE_SYNTAX_ERROR),
868 						 errmsg("column name \"%s\" is not unique",
869 								rawc->colname),
870 						 parser_errposition(pstate, rawc->location)));
871 		names[colno] = rawc->colname;
872 
873 		colno++;
874 	}
875 	pfree(names);
876 
877 	/* Namespaces, if any, also need to be transformed */
878 	if (rtf->namespaces != NIL)
879 	{
880 		ListCell   *ns;
881 		ListCell   *lc2;
882 		List	   *ns_uris = NIL;
883 		List	   *ns_names = NIL;
884 		bool		default_ns_seen = false;
885 
886 		foreach(ns, rtf->namespaces)
887 		{
888 			ResTarget  *r = (ResTarget *) lfirst(ns);
889 			Node	   *ns_uri;
890 
891 			Assert(IsA(r, ResTarget));
892 			ns_uri = transformExpr(pstate, r->val, EXPR_KIND_FROM_FUNCTION);
893 			ns_uri = coerce_to_specific_type(pstate, ns_uri,
894 											 TEXTOID, constructName);
895 			assign_expr_collations(pstate, ns_uri);
896 			ns_uris = lappend(ns_uris, ns_uri);
897 
898 			/* Verify consistency of name list: no dupes, only one DEFAULT */
899 			if (r->name != NULL)
900 			{
901 				foreach(lc2, ns_names)
902 				{
903 					Value	   *ns_node = (Value *) lfirst(lc2);
904 
905 					if (ns_node == NULL)
906 						continue;
907 					if (strcmp(strVal(ns_node), r->name) == 0)
908 						ereport(ERROR,
909 								(errcode(ERRCODE_SYNTAX_ERROR),
910 								 errmsg("namespace name \"%s\" is not unique",
911 										r->name),
912 								 parser_errposition(pstate, r->location)));
913 				}
914 			}
915 			else
916 			{
917 				if (default_ns_seen)
918 					ereport(ERROR,
919 							(errcode(ERRCODE_SYNTAX_ERROR),
920 							 errmsg("only one default namespace is allowed"),
921 							 parser_errposition(pstate, r->location)));
922 				default_ns_seen = true;
923 			}
924 
925 			/* We represent DEFAULT by a null pointer */
926 			ns_names = lappend(ns_names,
927 							   r->name ? makeString(r->name) : NULL);
928 		}
929 
930 		tf->ns_uris = ns_uris;
931 		tf->ns_names = ns_names;
932 	}
933 
934 	tf->location = rtf->location;
935 
936 	pstate->p_lateral_active = false;
937 
938 	/*
939 	 * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
940 	 * there are any lateral cross-references in it.
941 	 */
942 	is_lateral = rtf->lateral || contain_vars_of_level((Node *) tf, 0);
943 
944 	rte = addRangeTableEntryForTableFunc(pstate,
945 										 tf, rtf->alias, is_lateral, true);
946 
947 	return rte;
948 }
949 
950 /*
951  * transformRangeTableSample --- transform a TABLESAMPLE clause
952  *
953  * Caller has already transformed rts->relation, we just have to validate
954  * the remaining fields and create a TableSampleClause node.
955  */
956 static TableSampleClause *
transformRangeTableSample(ParseState * pstate,RangeTableSample * rts)957 transformRangeTableSample(ParseState *pstate, RangeTableSample *rts)
958 {
959 	TableSampleClause *tablesample;
960 	Oid			handlerOid;
961 	Oid			funcargtypes[1];
962 	TsmRoutine *tsm;
963 	List	   *fargs;
964 	ListCell   *larg,
965 			   *ltyp;
966 
967 	/*
968 	 * To validate the sample method name, look up the handler function, which
969 	 * has the same name, one dummy INTERNAL argument, and a result type of
970 	 * tsm_handler.  (Note: tablesample method names are not schema-qualified
971 	 * in the SQL standard; but since they are just functions to us, we allow
972 	 * schema qualification to resolve any potential ambiguity.)
973 	 */
974 	funcargtypes[0] = INTERNALOID;
975 
976 	handlerOid = LookupFuncName(rts->method, 1, funcargtypes, true);
977 
978 	/* we want error to complain about no-such-method, not no-such-function */
979 	if (!OidIsValid(handlerOid))
980 		ereport(ERROR,
981 				(errcode(ERRCODE_UNDEFINED_OBJECT),
982 				 errmsg("tablesample method %s does not exist",
983 						NameListToString(rts->method)),
984 				 parser_errposition(pstate, rts->location)));
985 
986 	/* check that handler has correct return type */
987 	if (get_func_rettype(handlerOid) != TSM_HANDLEROID)
988 		ereport(ERROR,
989 				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
990 				 errmsg("function %s must return type %s",
991 						NameListToString(rts->method), "tsm_handler"),
992 				 parser_errposition(pstate, rts->location)));
993 
994 	/* OK, run the handler to get TsmRoutine, for argument type info */
995 	tsm = GetTsmRoutine(handlerOid);
996 
997 	tablesample = makeNode(TableSampleClause);
998 	tablesample->tsmhandler = handlerOid;
999 
1000 	/* check user provided the expected number of arguments */
1001 	if (list_length(rts->args) != list_length(tsm->parameterTypes))
1002 		ereport(ERROR,
1003 				(errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
1004 				 errmsg_plural("tablesample method %s requires %d argument, not %d",
1005 							   "tablesample method %s requires %d arguments, not %d",
1006 							   list_length(tsm->parameterTypes),
1007 							   NameListToString(rts->method),
1008 							   list_length(tsm->parameterTypes),
1009 							   list_length(rts->args)),
1010 				 parser_errposition(pstate, rts->location)));
1011 
1012 	/*
1013 	 * Transform the arguments, typecasting them as needed.  Note we must also
1014 	 * assign collations now, because assign_query_collations() doesn't
1015 	 * examine any substructure of RTEs.
1016 	 */
1017 	fargs = NIL;
1018 	forboth(larg, rts->args, ltyp, tsm->parameterTypes)
1019 	{
1020 		Node	   *arg = (Node *) lfirst(larg);
1021 		Oid			argtype = lfirst_oid(ltyp);
1022 
1023 		arg = transformExpr(pstate, arg, EXPR_KIND_FROM_FUNCTION);
1024 		arg = coerce_to_specific_type(pstate, arg, argtype, "TABLESAMPLE");
1025 		assign_expr_collations(pstate, arg);
1026 		fargs = lappend(fargs, arg);
1027 	}
1028 	tablesample->args = fargs;
1029 
1030 	/* Process REPEATABLE (seed) */
1031 	if (rts->repeatable != NULL)
1032 	{
1033 		Node	   *arg;
1034 
1035 		if (!tsm->repeatable_across_queries)
1036 			ereport(ERROR,
1037 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1038 					 errmsg("tablesample method %s does not support REPEATABLE",
1039 							NameListToString(rts->method)),
1040 					 parser_errposition(pstate, rts->location)));
1041 
1042 		arg = transformExpr(pstate, rts->repeatable, EXPR_KIND_FROM_FUNCTION);
1043 		arg = coerce_to_specific_type(pstate, arg, FLOAT8OID, "REPEATABLE");
1044 		assign_expr_collations(pstate, arg);
1045 		tablesample->repeatable = (Expr *) arg;
1046 	}
1047 	else
1048 		tablesample->repeatable = NULL;
1049 
1050 	return tablesample;
1051 }
1052 
1053 /*
1054  * getRTEForSpecialRelationTypes
1055  *
1056  * If given RangeVar refers to a CTE or an EphemeralNamedRelation,
1057  * build and return an appropriate RTE, otherwise return NULL
1058  */
1059 static RangeTblEntry *
getRTEForSpecialRelationTypes(ParseState * pstate,RangeVar * rv)1060 getRTEForSpecialRelationTypes(ParseState *pstate, RangeVar *rv)
1061 {
1062 	CommonTableExpr *cte;
1063 	Index		levelsup;
1064 	RangeTblEntry *rte;
1065 
1066 	/*
1067 	 * if it is a qualified name, it can't be a CTE or tuplestore reference
1068 	 */
1069 	if (rv->schemaname)
1070 		return NULL;
1071 
1072 	cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
1073 	if (cte)
1074 		rte = addRangeTableEntryForCTE(pstate, cte, levelsup, rv, true);
1075 	else if (scanNameSpaceForENR(pstate, rv->relname))
1076 		rte = addRangeTableEntryForENR(pstate, rv, true);
1077 	else
1078 		rte = NULL;
1079 
1080 	return rte;
1081 }
1082 
1083 /*
1084  * transformFromClauseItem -
1085  *	  Transform a FROM-clause item, adding any required entries to the
1086  *	  range table list being built in the ParseState, and return the
1087  *	  transformed item ready to include in the joinlist.  Also build a
1088  *	  ParseNamespaceItem list describing the names exposed by this item.
1089  *	  This routine can recurse to handle SQL92 JOIN expressions.
1090  *
1091  * The function return value is the node to add to the jointree (a
1092  * RangeTblRef or JoinExpr).  Additional output parameters are:
1093  *
1094  * *top_rte: receives the RTE corresponding to the jointree item.
1095  * (We could extract this from the function return node, but it saves cycles
1096  * to pass it back separately.)
1097  *
1098  * *top_rti: receives the rangetable index of top_rte.  (Ditto.)
1099  *
1100  * *namespace: receives a List of ParseNamespaceItems for the RTEs exposed
1101  * as table/column names by this item.  (The lateral_only flags in these items
1102  * are indeterminate and should be explicitly set by the caller before use.)
1103  */
1104 static Node *
transformFromClauseItem(ParseState * pstate,Node * n,RangeTblEntry ** top_rte,int * top_rti,List ** namespace)1105 transformFromClauseItem(ParseState *pstate, Node *n,
1106 						RangeTblEntry **top_rte, int *top_rti,
1107 						List **namespace)
1108 {
1109 	if (IsA(n, RangeVar))
1110 	{
1111 		/* Plain relation reference, or perhaps a CTE reference */
1112 		RangeVar   *rv = (RangeVar *) n;
1113 		RangeTblRef *rtr;
1114 		RangeTblEntry *rte;
1115 		int			rtindex;
1116 
1117 		/* Check if it's a CTE or tuplestore reference */
1118 		rte = getRTEForSpecialRelationTypes(pstate, rv);
1119 
1120 		/* if not found above, must be a table reference */
1121 		if (!rte)
1122 			rte = transformTableEntry(pstate, rv);
1123 
1124 		/* assume new rte is at end */
1125 		rtindex = list_length(pstate->p_rtable);
1126 		Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1127 		*top_rte = rte;
1128 		*top_rti = rtindex;
1129 		*namespace = list_make1(makeDefaultNSItem(rte));
1130 		rtr = makeNode(RangeTblRef);
1131 		rtr->rtindex = rtindex;
1132 		return (Node *) rtr;
1133 	}
1134 	else if (IsA(n, RangeSubselect))
1135 	{
1136 		/* sub-SELECT is like a plain relation */
1137 		RangeTblRef *rtr;
1138 		RangeTblEntry *rte;
1139 		int			rtindex;
1140 
1141 		rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
1142 		/* assume new rte is at end */
1143 		rtindex = list_length(pstate->p_rtable);
1144 		Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1145 		*top_rte = rte;
1146 		*top_rti = rtindex;
1147 		*namespace = list_make1(makeDefaultNSItem(rte));
1148 		rtr = makeNode(RangeTblRef);
1149 		rtr->rtindex = rtindex;
1150 		return (Node *) rtr;
1151 	}
1152 	else if (IsA(n, RangeFunction))
1153 	{
1154 		/* function is like a plain relation */
1155 		RangeTblRef *rtr;
1156 		RangeTblEntry *rte;
1157 		int			rtindex;
1158 
1159 		rte = transformRangeFunction(pstate, (RangeFunction *) n);
1160 		/* assume new rte is at end */
1161 		rtindex = list_length(pstate->p_rtable);
1162 		Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1163 		*top_rte = rte;
1164 		*top_rti = rtindex;
1165 		*namespace = list_make1(makeDefaultNSItem(rte));
1166 		rtr = makeNode(RangeTblRef);
1167 		rtr->rtindex = rtindex;
1168 		return (Node *) rtr;
1169 	}
1170 	else if (IsA(n, RangeTableFunc))
1171 	{
1172 		/* table function is like a plain relation */
1173 		RangeTblRef *rtr;
1174 		RangeTblEntry *rte;
1175 		int			rtindex;
1176 
1177 		rte = transformRangeTableFunc(pstate, (RangeTableFunc *) n);
1178 		/* assume new rte is at end */
1179 		rtindex = list_length(pstate->p_rtable);
1180 		Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1181 		*top_rte = rte;
1182 		*top_rti = rtindex;
1183 		*namespace = list_make1(makeDefaultNSItem(rte));
1184 		rtr = makeNode(RangeTblRef);
1185 		rtr->rtindex = rtindex;
1186 		return (Node *) rtr;
1187 	}
1188 	else if (IsA(n, RangeTableSample))
1189 	{
1190 		/* TABLESAMPLE clause (wrapping some other valid FROM node) */
1191 		RangeTableSample *rts = (RangeTableSample *) n;
1192 		Node	   *rel;
1193 		RangeTblRef *rtr;
1194 		RangeTblEntry *rte;
1195 
1196 		/* Recursively transform the contained relation */
1197 		rel = transformFromClauseItem(pstate, rts->relation,
1198 									  top_rte, top_rti, namespace);
1199 		/* Currently, grammar could only return a RangeVar as contained rel */
1200 		rtr = castNode(RangeTblRef, rel);
1201 		rte = rt_fetch(rtr->rtindex, pstate->p_rtable);
1202 		/* We only support this on plain relations and matviews */
1203 		if (rte->relkind != RELKIND_RELATION &&
1204 			rte->relkind != RELKIND_MATVIEW &&
1205 			rte->relkind != RELKIND_PARTITIONED_TABLE)
1206 			ereport(ERROR,
1207 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1208 					 errmsg("TABLESAMPLE clause can only be applied to tables and materialized views"),
1209 					 parser_errposition(pstate, exprLocation(rts->relation))));
1210 
1211 		/* Transform TABLESAMPLE details and attach to the RTE */
1212 		rte->tablesample = transformRangeTableSample(pstate, rts);
1213 		return (Node *) rtr;
1214 	}
1215 	else if (IsA(n, JoinExpr))
1216 	{
1217 		/* A newfangled join expression */
1218 		JoinExpr   *j = (JoinExpr *) n;
1219 		RangeTblEntry *l_rte;
1220 		RangeTblEntry *r_rte;
1221 		int			l_rtindex;
1222 		int			r_rtindex;
1223 		List	   *l_namespace,
1224 				   *r_namespace,
1225 				   *my_namespace,
1226 				   *l_colnames,
1227 				   *r_colnames,
1228 				   *res_colnames,
1229 				   *l_colvars,
1230 				   *r_colvars,
1231 				   *res_colvars;
1232 		bool		lateral_ok;
1233 		int			sv_namespace_length;
1234 		RangeTblEntry *rte;
1235 		int			k;
1236 
1237 		/*
1238 		 * Recursively process the left subtree, then the right.  We must do
1239 		 * it in this order for correct visibility of LATERAL references.
1240 		 */
1241 		j->larg = transformFromClauseItem(pstate, j->larg,
1242 										  &l_rte,
1243 										  &l_rtindex,
1244 										  &l_namespace);
1245 
1246 		/*
1247 		 * Make the left-side RTEs available for LATERAL access within the
1248 		 * right side, by temporarily adding them to the pstate's namespace
1249 		 * list.  Per SQL:2008, if the join type is not INNER or LEFT then the
1250 		 * left-side names must still be exposed, but it's an error to
1251 		 * reference them.  (Stupid design, but that's what it says.)  Hence,
1252 		 * we always push them into the namespace, but mark them as not
1253 		 * lateral_ok if the jointype is wrong.
1254 		 *
1255 		 * Notice that we don't require the merged namespace list to be
1256 		 * conflict-free.  See the comments for scanNameSpaceForRefname().
1257 		 *
1258 		 * NB: this coding relies on the fact that list_concat is not
1259 		 * destructive to its second argument.
1260 		 */
1261 		lateral_ok = (j->jointype == JOIN_INNER || j->jointype == JOIN_LEFT);
1262 		setNamespaceLateralState(l_namespace, true, lateral_ok);
1263 
1264 		sv_namespace_length = list_length(pstate->p_namespace);
1265 		pstate->p_namespace = list_concat(pstate->p_namespace, l_namespace);
1266 
1267 		/* And now we can process the RHS */
1268 		j->rarg = transformFromClauseItem(pstate, j->rarg,
1269 										  &r_rte,
1270 										  &r_rtindex,
1271 										  &r_namespace);
1272 
1273 		/* Remove the left-side RTEs from the namespace list again */
1274 		pstate->p_namespace = list_truncate(pstate->p_namespace,
1275 											sv_namespace_length);
1276 
1277 		/*
1278 		 * Check for conflicting refnames in left and right subtrees. Must do
1279 		 * this because higher levels will assume I hand back a self-
1280 		 * consistent namespace list.
1281 		 */
1282 		checkNameSpaceConflicts(pstate, l_namespace, r_namespace);
1283 
1284 		/*
1285 		 * Generate combined namespace info for possible use below.
1286 		 */
1287 		my_namespace = list_concat(l_namespace, r_namespace);
1288 
1289 		/*
1290 		 * Extract column name and var lists from both subtrees
1291 		 *
1292 		 * Note: expandRTE returns new lists, safe for me to modify
1293 		 */
1294 		expandRTE(l_rte, l_rtindex, 0, -1, false,
1295 				  &l_colnames, &l_colvars);
1296 		expandRTE(r_rte, r_rtindex, 0, -1, false,
1297 				  &r_colnames, &r_colvars);
1298 
1299 		/*
1300 		 * Natural join does not explicitly specify columns; must generate
1301 		 * columns to join. Need to run through the list of columns from each
1302 		 * table or join result and match up the column names. Use the first
1303 		 * table, and check every column in the second table for a match.
1304 		 * (We'll check that the matches were unique later on.) The result of
1305 		 * this step is a list of column names just like an explicitly-written
1306 		 * USING list.
1307 		 */
1308 		if (j->isNatural)
1309 		{
1310 			List	   *rlist = NIL;
1311 			ListCell   *lx,
1312 					   *rx;
1313 
1314 			Assert(j->usingClause == NIL);	/* shouldn't have USING() too */
1315 
1316 			foreach(lx, l_colnames)
1317 			{
1318 				char	   *l_colname = strVal(lfirst(lx));
1319 				Value	   *m_name = NULL;
1320 
1321 				foreach(rx, r_colnames)
1322 				{
1323 					char	   *r_colname = strVal(lfirst(rx));
1324 
1325 					if (strcmp(l_colname, r_colname) == 0)
1326 					{
1327 						m_name = makeString(l_colname);
1328 						break;
1329 					}
1330 				}
1331 
1332 				/* matched a right column? then keep as join column... */
1333 				if (m_name != NULL)
1334 					rlist = lappend(rlist, m_name);
1335 			}
1336 
1337 			j->usingClause = rlist;
1338 		}
1339 
1340 		/*
1341 		 * Now transform the join qualifications, if any.
1342 		 */
1343 		res_colnames = NIL;
1344 		res_colvars = NIL;
1345 
1346 		if (j->usingClause)
1347 		{
1348 			/*
1349 			 * JOIN/USING (or NATURAL JOIN, as transformed above). Transform
1350 			 * the list into an explicit ON-condition, and generate a list of
1351 			 * merged result columns.
1352 			 */
1353 			List	   *ucols = j->usingClause;
1354 			List	   *l_usingvars = NIL;
1355 			List	   *r_usingvars = NIL;
1356 			ListCell   *ucol;
1357 
1358 			Assert(j->quals == NULL);	/* shouldn't have ON() too */
1359 
1360 			foreach(ucol, ucols)
1361 			{
1362 				char	   *u_colname = strVal(lfirst(ucol));
1363 				ListCell   *col;
1364 				int			ndx;
1365 				int			l_index = -1;
1366 				int			r_index = -1;
1367 				Var		   *l_colvar,
1368 						   *r_colvar;
1369 
1370 				/* Check for USING(foo,foo) */
1371 				foreach(col, res_colnames)
1372 				{
1373 					char	   *res_colname = strVal(lfirst(col));
1374 
1375 					if (strcmp(res_colname, u_colname) == 0)
1376 						ereport(ERROR,
1377 								(errcode(ERRCODE_DUPLICATE_COLUMN),
1378 								 errmsg("column name \"%s\" appears more than once in USING clause",
1379 										u_colname)));
1380 				}
1381 
1382 				/* Find it in left input */
1383 				ndx = 0;
1384 				foreach(col, l_colnames)
1385 				{
1386 					char	   *l_colname = strVal(lfirst(col));
1387 
1388 					if (strcmp(l_colname, u_colname) == 0)
1389 					{
1390 						if (l_index >= 0)
1391 							ereport(ERROR,
1392 									(errcode(ERRCODE_AMBIGUOUS_COLUMN),
1393 									 errmsg("common column name \"%s\" appears more than once in left table",
1394 											u_colname)));
1395 						l_index = ndx;
1396 					}
1397 					ndx++;
1398 				}
1399 				if (l_index < 0)
1400 					ereport(ERROR,
1401 							(errcode(ERRCODE_UNDEFINED_COLUMN),
1402 							 errmsg("column \"%s\" specified in USING clause does not exist in left table",
1403 									u_colname)));
1404 
1405 				/* Find it in right input */
1406 				ndx = 0;
1407 				foreach(col, r_colnames)
1408 				{
1409 					char	   *r_colname = strVal(lfirst(col));
1410 
1411 					if (strcmp(r_colname, u_colname) == 0)
1412 					{
1413 						if (r_index >= 0)
1414 							ereport(ERROR,
1415 									(errcode(ERRCODE_AMBIGUOUS_COLUMN),
1416 									 errmsg("common column name \"%s\" appears more than once in right table",
1417 											u_colname)));
1418 						r_index = ndx;
1419 					}
1420 					ndx++;
1421 				}
1422 				if (r_index < 0)
1423 					ereport(ERROR,
1424 							(errcode(ERRCODE_UNDEFINED_COLUMN),
1425 							 errmsg("column \"%s\" specified in USING clause does not exist in right table",
1426 									u_colname)));
1427 
1428 				l_colvar = list_nth(l_colvars, l_index);
1429 				l_usingvars = lappend(l_usingvars, l_colvar);
1430 				r_colvar = list_nth(r_colvars, r_index);
1431 				r_usingvars = lappend(r_usingvars, r_colvar);
1432 
1433 				res_colnames = lappend(res_colnames, lfirst(ucol));
1434 				res_colvars = lappend(res_colvars,
1435 									  buildMergedJoinVar(pstate,
1436 														 j->jointype,
1437 														 l_colvar,
1438 														 r_colvar));
1439 			}
1440 
1441 			j->quals = transformJoinUsingClause(pstate,
1442 												l_rte,
1443 												r_rte,
1444 												l_usingvars,
1445 												r_usingvars);
1446 		}
1447 		else if (j->quals)
1448 		{
1449 			/* User-written ON-condition; transform it */
1450 			j->quals = transformJoinOnClause(pstate, j, my_namespace);
1451 		}
1452 		else
1453 		{
1454 			/* CROSS JOIN: no quals */
1455 		}
1456 
1457 		/* Add remaining columns from each side to the output columns */
1458 		extractRemainingColumns(res_colnames,
1459 								l_colnames, l_colvars,
1460 								&l_colnames, &l_colvars);
1461 		extractRemainingColumns(res_colnames,
1462 								r_colnames, r_colvars,
1463 								&r_colnames, &r_colvars);
1464 		res_colnames = list_concat(res_colnames, l_colnames);
1465 		res_colvars = list_concat(res_colvars, l_colvars);
1466 		res_colnames = list_concat(res_colnames, r_colnames);
1467 		res_colvars = list_concat(res_colvars, r_colvars);
1468 
1469 		/*
1470 		 * Check alias (AS clause), if any.
1471 		 */
1472 		if (j->alias)
1473 		{
1474 			if (j->alias->colnames != NIL)
1475 			{
1476 				if (list_length(j->alias->colnames) > list_length(res_colnames))
1477 					ereport(ERROR,
1478 							(errcode(ERRCODE_SYNTAX_ERROR),
1479 							 errmsg("column alias list for \"%s\" has too many entries",
1480 									j->alias->aliasname)));
1481 			}
1482 		}
1483 
1484 		/*
1485 		 * Now build an RTE for the result of the join
1486 		 */
1487 		rte = addRangeTableEntryForJoin(pstate,
1488 										res_colnames,
1489 										j->jointype,
1490 										res_colvars,
1491 										j->alias,
1492 										true);
1493 
1494 		/* assume new rte is at end */
1495 		j->rtindex = list_length(pstate->p_rtable);
1496 		Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
1497 
1498 		*top_rte = rte;
1499 		*top_rti = j->rtindex;
1500 
1501 		/* make a matching link to the JoinExpr for later use */
1502 		for (k = list_length(pstate->p_joinexprs) + 1; k < j->rtindex; k++)
1503 			pstate->p_joinexprs = lappend(pstate->p_joinexprs, NULL);
1504 		pstate->p_joinexprs = lappend(pstate->p_joinexprs, j);
1505 		Assert(list_length(pstate->p_joinexprs) == j->rtindex);
1506 
1507 		/*
1508 		 * Prepare returned namespace list.  If the JOIN has an alias then it
1509 		 * hides the contained RTEs completely; otherwise, the contained RTEs
1510 		 * are still visible as table names, but are not visible for
1511 		 * unqualified column-name access.
1512 		 *
1513 		 * Note: if there are nested alias-less JOINs, the lower-level ones
1514 		 * will remain in the list although they have neither p_rel_visible
1515 		 * nor p_cols_visible set.  We could delete such list items, but it's
1516 		 * unclear that it's worth expending cycles to do so.
1517 		 */
1518 		if (j->alias != NULL)
1519 			my_namespace = NIL;
1520 		else
1521 			setNamespaceColumnVisibility(my_namespace, false);
1522 
1523 		/*
1524 		 * The join RTE itself is always made visible for unqualified column
1525 		 * names.  It's visible as a relation name only if it has an alias.
1526 		 */
1527 		*namespace = lappend(my_namespace,
1528 							 makeNamespaceItem(rte,
1529 											   (j->alias != NULL),
1530 											   true,
1531 											   false,
1532 											   true));
1533 
1534 		return (Node *) j;
1535 	}
1536 	else
1537 		elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
1538 	return NULL;				/* can't get here, keep compiler quiet */
1539 }
1540 
1541 /*
1542  * buildMergedJoinVar -
1543  *	  generate a suitable replacement expression for a merged join column
1544  */
1545 static Node *
buildMergedJoinVar(ParseState * pstate,JoinType jointype,Var * l_colvar,Var * r_colvar)1546 buildMergedJoinVar(ParseState *pstate, JoinType jointype,
1547 				   Var *l_colvar, Var *r_colvar)
1548 {
1549 	Oid			outcoltype;
1550 	int32		outcoltypmod;
1551 	Node	   *l_node,
1552 			   *r_node,
1553 			   *res_node;
1554 
1555 	/*
1556 	 * Choose output type if input types are dissimilar.
1557 	 */
1558 	outcoltype = l_colvar->vartype;
1559 	outcoltypmod = l_colvar->vartypmod;
1560 	if (outcoltype != r_colvar->vartype)
1561 	{
1562 		outcoltype = select_common_type(pstate,
1563 										list_make2(l_colvar, r_colvar),
1564 										"JOIN/USING",
1565 										NULL);
1566 		outcoltypmod = -1;		/* ie, unknown */
1567 	}
1568 	else if (outcoltypmod != r_colvar->vartypmod)
1569 	{
1570 		/* same type, but not same typmod */
1571 		outcoltypmod = -1;		/* ie, unknown */
1572 	}
1573 
1574 	/*
1575 	 * Insert coercion functions if needed.  Note that a difference in typmod
1576 	 * can only happen if input has typmod but outcoltypmod is -1. In that
1577 	 * case we insert a RelabelType to clearly mark that result's typmod is
1578 	 * not same as input.  We never need coerce_type_typmod.
1579 	 */
1580 	if (l_colvar->vartype != outcoltype)
1581 		l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
1582 							 outcoltype, outcoltypmod,
1583 							 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1584 	else if (l_colvar->vartypmod != outcoltypmod)
1585 		l_node = (Node *) makeRelabelType((Expr *) l_colvar,
1586 										  outcoltype, outcoltypmod,
1587 										  InvalidOid,	/* fixed below */
1588 										  COERCE_IMPLICIT_CAST);
1589 	else
1590 		l_node = (Node *) l_colvar;
1591 
1592 	if (r_colvar->vartype != outcoltype)
1593 		r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
1594 							 outcoltype, outcoltypmod,
1595 							 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1596 	else if (r_colvar->vartypmod != outcoltypmod)
1597 		r_node = (Node *) makeRelabelType((Expr *) r_colvar,
1598 										  outcoltype, outcoltypmod,
1599 										  InvalidOid,	/* fixed below */
1600 										  COERCE_IMPLICIT_CAST);
1601 	else
1602 		r_node = (Node *) r_colvar;
1603 
1604 	/*
1605 	 * Choose what to emit
1606 	 */
1607 	switch (jointype)
1608 	{
1609 		case JOIN_INNER:
1610 
1611 			/*
1612 			 * We can use either var; prefer non-coerced one if available.
1613 			 */
1614 			if (IsA(l_node, Var))
1615 				res_node = l_node;
1616 			else if (IsA(r_node, Var))
1617 				res_node = r_node;
1618 			else
1619 				res_node = l_node;
1620 			break;
1621 		case JOIN_LEFT:
1622 			/* Always use left var */
1623 			res_node = l_node;
1624 			break;
1625 		case JOIN_RIGHT:
1626 			/* Always use right var */
1627 			res_node = r_node;
1628 			break;
1629 		case JOIN_FULL:
1630 			{
1631 				/*
1632 				 * Here we must build a COALESCE expression to ensure that the
1633 				 * join output is non-null if either input is.
1634 				 */
1635 				CoalesceExpr *c = makeNode(CoalesceExpr);
1636 
1637 				c->coalescetype = outcoltype;
1638 				/* coalescecollid will get set below */
1639 				c->args = list_make2(l_node, r_node);
1640 				c->location = -1;
1641 				res_node = (Node *) c;
1642 				break;
1643 			}
1644 		default:
1645 			elog(ERROR, "unrecognized join type: %d", (int) jointype);
1646 			res_node = NULL;	/* keep compiler quiet */
1647 			break;
1648 	}
1649 
1650 	/*
1651 	 * Apply assign_expr_collations to fix up the collation info in the
1652 	 * coercion and CoalesceExpr nodes, if we made any.  This must be done now
1653 	 * so that the join node's alias vars show correct collation info.
1654 	 */
1655 	assign_expr_collations(pstate, res_node);
1656 
1657 	return res_node;
1658 }
1659 
1660 /*
1661  * makeNamespaceItem -
1662  *	  Convenience subroutine to construct a ParseNamespaceItem.
1663  */
1664 static ParseNamespaceItem *
makeNamespaceItem(RangeTblEntry * rte,bool rel_visible,bool cols_visible,bool lateral_only,bool lateral_ok)1665 makeNamespaceItem(RangeTblEntry *rte, bool rel_visible, bool cols_visible,
1666 				  bool lateral_only, bool lateral_ok)
1667 {
1668 	ParseNamespaceItem *nsitem;
1669 
1670 	nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1671 	nsitem->p_rte = rte;
1672 	nsitem->p_rel_visible = rel_visible;
1673 	nsitem->p_cols_visible = cols_visible;
1674 	nsitem->p_lateral_only = lateral_only;
1675 	nsitem->p_lateral_ok = lateral_ok;
1676 	return nsitem;
1677 }
1678 
1679 /*
1680  * setNamespaceColumnVisibility -
1681  *	  Convenience subroutine to update cols_visible flags in a namespace list.
1682  */
1683 static void
setNamespaceColumnVisibility(List * namespace,bool cols_visible)1684 setNamespaceColumnVisibility(List *namespace, bool cols_visible)
1685 {
1686 	ListCell   *lc;
1687 
1688 	foreach(lc, namespace)
1689 	{
1690 		ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1691 
1692 		nsitem->p_cols_visible = cols_visible;
1693 	}
1694 }
1695 
1696 /*
1697  * setNamespaceLateralState -
1698  *	  Convenience subroutine to update LATERAL flags in a namespace list.
1699  */
1700 static void
setNamespaceLateralState(List * namespace,bool lateral_only,bool lateral_ok)1701 setNamespaceLateralState(List *namespace, bool lateral_only, bool lateral_ok)
1702 {
1703 	ListCell   *lc;
1704 
1705 	foreach(lc, namespace)
1706 	{
1707 		ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1708 
1709 		nsitem->p_lateral_only = lateral_only;
1710 		nsitem->p_lateral_ok = lateral_ok;
1711 	}
1712 }
1713 
1714 
1715 /*
1716  * transformWhereClause -
1717  *	  Transform the qualification and make sure it is of type boolean.
1718  *	  Used for WHERE and allied clauses.
1719  *
1720  * constructName does not affect the semantics, but is used in error messages
1721  */
1722 Node *
transformWhereClause(ParseState * pstate,Node * clause,ParseExprKind exprKind,const char * constructName)1723 transformWhereClause(ParseState *pstate, Node *clause,
1724 					 ParseExprKind exprKind, const char *constructName)
1725 {
1726 	Node	   *qual;
1727 
1728 	if (clause == NULL)
1729 		return NULL;
1730 
1731 	qual = transformExpr(pstate, clause, exprKind);
1732 
1733 	qual = coerce_to_boolean(pstate, qual, constructName);
1734 
1735 	return qual;
1736 }
1737 
1738 
1739 /*
1740  * transformLimitClause -
1741  *	  Transform the expression and make sure it is of type bigint.
1742  *	  Used for LIMIT and allied clauses.
1743  *
1744  * Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
1745  * rather than int4 as before.
1746  *
1747  * constructName does not affect the semantics, but is used in error messages
1748  */
1749 Node *
transformLimitClause(ParseState * pstate,Node * clause,ParseExprKind exprKind,const char * constructName)1750 transformLimitClause(ParseState *pstate, Node *clause,
1751 					 ParseExprKind exprKind, const char *constructName)
1752 {
1753 	Node	   *qual;
1754 
1755 	if (clause == NULL)
1756 		return NULL;
1757 
1758 	qual = transformExpr(pstate, clause, exprKind);
1759 
1760 	qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1761 
1762 	/* LIMIT can't refer to any variables of the current query */
1763 	checkExprIsVarFree(pstate, qual, constructName);
1764 
1765 	return qual;
1766 }
1767 
1768 /*
1769  * checkExprIsVarFree
1770  *		Check that given expr has no Vars of the current query level
1771  *		(aggregates and window functions should have been rejected already).
1772  *
1773  * This is used to check expressions that have to have a consistent value
1774  * across all rows of the query, such as a LIMIT.  Arguably it should reject
1775  * volatile functions, too, but we don't do that --- whatever value the
1776  * function gives on first execution is what you get.
1777  *
1778  * constructName does not affect the semantics, but is used in error messages
1779  */
1780 static void
checkExprIsVarFree(ParseState * pstate,Node * n,const char * constructName)1781 checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
1782 {
1783 	if (contain_vars_of_level(n, 0))
1784 	{
1785 		ereport(ERROR,
1786 				(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1787 		/* translator: %s is name of a SQL construct, eg LIMIT */
1788 				 errmsg("argument of %s must not contain variables",
1789 						constructName),
1790 				 parser_errposition(pstate,
1791 									locate_var_of_level(n, 0))));
1792 	}
1793 }
1794 
1795 
1796 /*
1797  * checkTargetlistEntrySQL92 -
1798  *	  Validate a targetlist entry found by findTargetlistEntrySQL92
1799  *
1800  * When we select a pre-existing tlist entry as a result of syntax such
1801  * as "GROUP BY 1", we have to make sure it is acceptable for use in the
1802  * indicated clause type; transformExpr() will have treated it as a regular
1803  * targetlist item.
1804  */
1805 static void
checkTargetlistEntrySQL92(ParseState * pstate,TargetEntry * tle,ParseExprKind exprKind)1806 checkTargetlistEntrySQL92(ParseState *pstate, TargetEntry *tle,
1807 						  ParseExprKind exprKind)
1808 {
1809 	switch (exprKind)
1810 	{
1811 		case EXPR_KIND_GROUP_BY:
1812 			/* reject aggregates and window functions */
1813 			if (pstate->p_hasAggs &&
1814 				contain_aggs_of_level((Node *) tle->expr, 0))
1815 				ereport(ERROR,
1816 						(errcode(ERRCODE_GROUPING_ERROR),
1817 				/* translator: %s is name of a SQL construct, eg GROUP BY */
1818 						 errmsg("aggregate functions are not allowed in %s",
1819 								ParseExprKindName(exprKind)),
1820 						 parser_errposition(pstate,
1821 											locate_agg_of_level((Node *) tle->expr, 0))));
1822 			if (pstate->p_hasWindowFuncs &&
1823 				contain_windowfuncs((Node *) tle->expr))
1824 				ereport(ERROR,
1825 						(errcode(ERRCODE_WINDOWING_ERROR),
1826 				/* translator: %s is name of a SQL construct, eg GROUP BY */
1827 						 errmsg("window functions are not allowed in %s",
1828 								ParseExprKindName(exprKind)),
1829 						 parser_errposition(pstate,
1830 											locate_windowfunc((Node *) tle->expr))));
1831 			break;
1832 		case EXPR_KIND_ORDER_BY:
1833 			/* no extra checks needed */
1834 			break;
1835 		case EXPR_KIND_DISTINCT_ON:
1836 			/* no extra checks needed */
1837 			break;
1838 		default:
1839 			elog(ERROR, "unexpected exprKind in checkTargetlistEntrySQL92");
1840 			break;
1841 	}
1842 }
1843 
1844 /*
1845  *	findTargetlistEntrySQL92 -
1846  *	  Returns the targetlist entry matching the given (untransformed) node.
1847  *	  If no matching entry exists, one is created and appended to the target
1848  *	  list as a "resjunk" node.
1849  *
1850  * This function supports the old SQL92 ORDER BY interpretation, where the
1851  * expression is an output column name or number.  If we fail to find a
1852  * match of that sort, we fall through to the SQL99 rules.  For historical
1853  * reasons, Postgres also allows this interpretation for GROUP BY, though
1854  * the standard never did.  However, for GROUP BY we prefer a SQL99 match.
1855  * This function is *not* used for WINDOW definitions.
1856  *
1857  * node		the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
1858  * tlist	the target list (passed by reference so we can append to it)
1859  * exprKind identifies clause type being processed
1860  */
1861 static TargetEntry *
findTargetlistEntrySQL92(ParseState * pstate,Node * node,List ** tlist,ParseExprKind exprKind)1862 findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist,
1863 						 ParseExprKind exprKind)
1864 {
1865 	ListCell   *tl;
1866 
1867 	/*----------
1868 	 * Handle two special cases as mandated by the SQL92 spec:
1869 	 *
1870 	 * 1. Bare ColumnName (no qualifier or subscripts)
1871 	 *	  For a bare identifier, we search for a matching column name
1872 	 *	  in the existing target list.  Multiple matches are an error
1873 	 *	  unless they refer to identical values; for example,
1874 	 *	  we allow	SELECT a, a FROM table ORDER BY a
1875 	 *	  but not	SELECT a AS b, b FROM table ORDER BY b
1876 	 *	  If no match is found, we fall through and treat the identifier
1877 	 *	  as an expression.
1878 	 *	  For GROUP BY, it is incorrect to match the grouping item against
1879 	 *	  targetlist entries: according to SQL92, an identifier in GROUP BY
1880 	 *	  is a reference to a column name exposed by FROM, not to a target
1881 	 *	  list column.  However, many implementations (including pre-7.0
1882 	 *	  PostgreSQL) accept this anyway.  So for GROUP BY, we look first
1883 	 *	  to see if the identifier matches any FROM column name, and only
1884 	 *	  try for a targetlist name if it doesn't.  This ensures that we
1885 	 *	  adhere to the spec in the case where the name could be both.
1886 	 *	  DISTINCT ON isn't in the standard, so we can do what we like there;
1887 	 *	  we choose to make it work like ORDER BY, on the rather flimsy
1888 	 *	  grounds that ordinary DISTINCT works on targetlist entries.
1889 	 *
1890 	 * 2. IntegerConstant
1891 	 *	  This means to use the n'th item in the existing target list.
1892 	 *	  Note that it would make no sense to order/group/distinct by an
1893 	 *	  actual constant, so this does not create a conflict with SQL99.
1894 	 *	  GROUP BY column-number is not allowed by SQL92, but since
1895 	 *	  the standard has no other behavior defined for this syntax,
1896 	 *	  we may as well accept this common extension.
1897 	 *
1898 	 * Note that pre-existing resjunk targets must not be used in either case,
1899 	 * since the user didn't write them in his SELECT list.
1900 	 *
1901 	 * If neither special case applies, fall through to treat the item as
1902 	 * an expression per SQL99.
1903 	 *----------
1904 	 */
1905 	if (IsA(node, ColumnRef) &&
1906 		list_length(((ColumnRef *) node)->fields) == 1 &&
1907 		IsA(linitial(((ColumnRef *) node)->fields), String))
1908 	{
1909 		char	   *name = strVal(linitial(((ColumnRef *) node)->fields));
1910 		int			location = ((ColumnRef *) node)->location;
1911 
1912 		if (exprKind == EXPR_KIND_GROUP_BY)
1913 		{
1914 			/*
1915 			 * In GROUP BY, we must prefer a match against a FROM-clause
1916 			 * column to one against the targetlist.  Look to see if there is
1917 			 * a matching column.  If so, fall through to use SQL99 rules.
1918 			 * NOTE: if name could refer ambiguously to more than one column
1919 			 * name exposed by FROM, colNameToVar will ereport(ERROR). That's
1920 			 * just what we want here.
1921 			 *
1922 			 * Small tweak for 7.4.3: ignore matches in upper query levels.
1923 			 * This effectively changes the search order for bare names to (1)
1924 			 * local FROM variables, (2) local targetlist aliases, (3) outer
1925 			 * FROM variables, whereas before it was (1) (3) (2). SQL92 and
1926 			 * SQL99 do not allow GROUPing BY an outer reference, so this
1927 			 * breaks no cases that are legal per spec, and it seems a more
1928 			 * self-consistent behavior.
1929 			 */
1930 			if (colNameToVar(pstate, name, true, location) != NULL)
1931 				name = NULL;
1932 		}
1933 
1934 		if (name != NULL)
1935 		{
1936 			TargetEntry *target_result = NULL;
1937 
1938 			foreach(tl, *tlist)
1939 			{
1940 				TargetEntry *tle = (TargetEntry *) lfirst(tl);
1941 
1942 				if (!tle->resjunk &&
1943 					strcmp(tle->resname, name) == 0)
1944 				{
1945 					if (target_result != NULL)
1946 					{
1947 						if (!equal(target_result->expr, tle->expr))
1948 							ereport(ERROR,
1949 									(errcode(ERRCODE_AMBIGUOUS_COLUMN),
1950 
1951 							/*------
1952 							  translator: first %s is name of a SQL construct, eg ORDER BY */
1953 									 errmsg("%s \"%s\" is ambiguous",
1954 											ParseExprKindName(exprKind),
1955 											name),
1956 									 parser_errposition(pstate, location)));
1957 					}
1958 					else
1959 						target_result = tle;
1960 					/* Stay in loop to check for ambiguity */
1961 				}
1962 			}
1963 			if (target_result != NULL)
1964 			{
1965 				/* return the first match, after suitable validation */
1966 				checkTargetlistEntrySQL92(pstate, target_result, exprKind);
1967 				return target_result;
1968 			}
1969 		}
1970 	}
1971 	if (IsA(node, A_Const))
1972 	{
1973 		Value	   *val = &((A_Const *) node)->val;
1974 		int			location = ((A_Const *) node)->location;
1975 		int			targetlist_pos = 0;
1976 		int			target_pos;
1977 
1978 		if (!IsA(val, Integer))
1979 			ereport(ERROR,
1980 					(errcode(ERRCODE_SYNTAX_ERROR),
1981 			/* translator: %s is name of a SQL construct, eg ORDER BY */
1982 					 errmsg("non-integer constant in %s",
1983 							ParseExprKindName(exprKind)),
1984 					 parser_errposition(pstate, location)));
1985 
1986 		target_pos = intVal(val);
1987 		foreach(tl, *tlist)
1988 		{
1989 			TargetEntry *tle = (TargetEntry *) lfirst(tl);
1990 
1991 			if (!tle->resjunk)
1992 			{
1993 				if (++targetlist_pos == target_pos)
1994 				{
1995 					/* return the unique match, after suitable validation */
1996 					checkTargetlistEntrySQL92(pstate, tle, exprKind);
1997 					return tle;
1998 				}
1999 			}
2000 		}
2001 		ereport(ERROR,
2002 				(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2003 		/* translator: %s is name of a SQL construct, eg ORDER BY */
2004 				 errmsg("%s position %d is not in select list",
2005 						ParseExprKindName(exprKind), target_pos),
2006 				 parser_errposition(pstate, location)));
2007 	}
2008 
2009 	/*
2010 	 * Otherwise, we have an expression, so process it per SQL99 rules.
2011 	 */
2012 	return findTargetlistEntrySQL99(pstate, node, tlist, exprKind);
2013 }
2014 
2015 /*
2016  *	findTargetlistEntrySQL99 -
2017  *	  Returns the targetlist entry matching the given (untransformed) node.
2018  *	  If no matching entry exists, one is created and appended to the target
2019  *	  list as a "resjunk" node.
2020  *
2021  * This function supports the SQL99 interpretation, wherein the expression
2022  * is just an ordinary expression referencing input column names.
2023  *
2024  * node		the ORDER BY, GROUP BY, etc expression to be matched
2025  * tlist	the target list (passed by reference so we can append to it)
2026  * exprKind identifies clause type being processed
2027  */
2028 static TargetEntry *
findTargetlistEntrySQL99(ParseState * pstate,Node * node,List ** tlist,ParseExprKind exprKind)2029 findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist,
2030 						 ParseExprKind exprKind)
2031 {
2032 	TargetEntry *target_result;
2033 	ListCell   *tl;
2034 	Node	   *expr;
2035 
2036 	/*
2037 	 * Convert the untransformed node to a transformed expression, and search
2038 	 * for a match in the tlist.  NOTE: it doesn't really matter whether there
2039 	 * is more than one match.  Also, we are willing to match an existing
2040 	 * resjunk target here, though the SQL92 cases above must ignore resjunk
2041 	 * targets.
2042 	 */
2043 	expr = transformExpr(pstate, node, exprKind);
2044 
2045 	foreach(tl, *tlist)
2046 	{
2047 		TargetEntry *tle = (TargetEntry *) lfirst(tl);
2048 		Node	   *texpr;
2049 
2050 		/*
2051 		 * Ignore any implicit cast on the existing tlist expression.
2052 		 *
2053 		 * This essentially allows the ORDER/GROUP/etc item to adopt the same
2054 		 * datatype previously selected for a textually-equivalent tlist item.
2055 		 * There can't be any implicit cast at top level in an ordinary SELECT
2056 		 * tlist at this stage, but the case does arise with ORDER BY in an
2057 		 * aggregate function.
2058 		 */
2059 		texpr = strip_implicit_coercions((Node *) tle->expr);
2060 
2061 		if (equal(expr, texpr))
2062 			return tle;
2063 	}
2064 
2065 	/*
2066 	 * If no matches, construct a new target entry which is appended to the
2067 	 * end of the target list.  This target is given resjunk = true so that it
2068 	 * will not be projected into the final tuple.
2069 	 */
2070 	target_result = transformTargetEntry(pstate, node, expr, exprKind,
2071 										 NULL, true);
2072 
2073 	*tlist = lappend(*tlist, target_result);
2074 
2075 	return target_result;
2076 }
2077 
2078 /*-------------------------------------------------------------------------
2079  * Flatten out parenthesized sublists in grouping lists, and some cases
2080  * of nested grouping sets.
2081  *
2082  * Inside a grouping set (ROLLUP, CUBE, or GROUPING SETS), we expect the
2083  * content to be nested no more than 2 deep: i.e. ROLLUP((a,b),(c,d)) is
2084  * ok, but ROLLUP((a,(b,c)),d) is flattened to ((a,b,c),d), which we then
2085  * (later) normalize to ((a,b,c),(d)).
2086  *
2087  * CUBE or ROLLUP can be nested inside GROUPING SETS (but not the reverse),
2088  * and we leave that alone if we find it. But if we see GROUPING SETS inside
2089  * GROUPING SETS, we can flatten and normalize as follows:
2090  *	 GROUPING SETS (a, (b,c), GROUPING SETS ((c,d),(e)), (f,g))
2091  * becomes
2092  *	 GROUPING SETS ((a), (b,c), (c,d), (e), (f,g))
2093  *
2094  * This is per the spec's syntax transformations, but these are the only such
2095  * transformations we do in parse analysis, so that queries retain the
2096  * originally specified grouping set syntax for CUBE and ROLLUP as much as
2097  * possible when deparsed. (Full expansion of the result into a list of
2098  * grouping sets is left to the planner.)
2099  *
2100  * When we're done, the resulting list should contain only these possible
2101  * elements:
2102  *	 - an expression
2103  *	 - a CUBE or ROLLUP with a list of expressions nested 2 deep
2104  *	 - a GROUPING SET containing any of:
2105  *		- expression lists
2106  *		- empty grouping sets
2107  *		- CUBE or ROLLUP nodes with lists nested 2 deep
2108  * The return is a new list, but doesn't deep-copy the old nodes except for
2109  * GroupingSet nodes.
2110  *
2111  * As a side effect, flag whether the list has any GroupingSet nodes.
2112  *-------------------------------------------------------------------------
2113  */
2114 static Node *
flatten_grouping_sets(Node * expr,bool toplevel,bool * hasGroupingSets)2115 flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
2116 {
2117 	/* just in case of pathological input */
2118 	check_stack_depth();
2119 
2120 	if (expr == (Node *) NIL)
2121 		return (Node *) NIL;
2122 
2123 	switch (expr->type)
2124 	{
2125 		case T_RowExpr:
2126 			{
2127 				RowExpr    *r = (RowExpr *) expr;
2128 
2129 				if (r->row_format == COERCE_IMPLICIT_CAST)
2130 					return flatten_grouping_sets((Node *) r->args,
2131 												 false, NULL);
2132 			}
2133 			break;
2134 		case T_GroupingSet:
2135 			{
2136 				GroupingSet *gset = (GroupingSet *) expr;
2137 				ListCell   *l2;
2138 				List	   *result_set = NIL;
2139 
2140 				if (hasGroupingSets)
2141 					*hasGroupingSets = true;
2142 
2143 				/*
2144 				 * at the top level, we skip over all empty grouping sets; the
2145 				 * caller can supply the canonical GROUP BY () if nothing is
2146 				 * left.
2147 				 */
2148 
2149 				if (toplevel && gset->kind == GROUPING_SET_EMPTY)
2150 					return (Node *) NIL;
2151 
2152 				foreach(l2, gset->content)
2153 				{
2154 					Node	   *n1 = lfirst(l2);
2155 					Node	   *n2 = flatten_grouping_sets(n1, false, NULL);
2156 
2157 					if (IsA(n1, GroupingSet) &&
2158 						((GroupingSet *) n1)->kind == GROUPING_SET_SETS)
2159 					{
2160 						result_set = list_concat(result_set, (List *) n2);
2161 					}
2162 					else
2163 						result_set = lappend(result_set, n2);
2164 				}
2165 
2166 				/*
2167 				 * At top level, keep the grouping set node; but if we're in a
2168 				 * nested grouping set, then we need to concat the flattened
2169 				 * result into the outer list if it's simply nested.
2170 				 */
2171 
2172 				if (toplevel || (gset->kind != GROUPING_SET_SETS))
2173 				{
2174 					return (Node *) makeGroupingSet(gset->kind, result_set, gset->location);
2175 				}
2176 				else
2177 					return (Node *) result_set;
2178 			}
2179 		case T_List:
2180 			{
2181 				List	   *result = NIL;
2182 				ListCell   *l;
2183 
2184 				foreach(l, (List *) expr)
2185 				{
2186 					Node	   *n = flatten_grouping_sets(lfirst(l), toplevel, hasGroupingSets);
2187 
2188 					if (n != (Node *) NIL)
2189 					{
2190 						if (IsA(n, List))
2191 							result = list_concat(result, (List *) n);
2192 						else
2193 							result = lappend(result, n);
2194 					}
2195 				}
2196 
2197 				return (Node *) result;
2198 			}
2199 		default:
2200 			break;
2201 	}
2202 
2203 	return expr;
2204 }
2205 
2206 /*
2207  * Transform a single expression within a GROUP BY clause or grouping set.
2208  *
2209  * The expression is added to the targetlist if not already present, and to the
2210  * flatresult list (which will become the groupClause) if not already present
2211  * there.  The sortClause is consulted for operator and sort order hints.
2212  *
2213  * Returns the ressortgroupref of the expression.
2214  *
2215  * flatresult	reference to flat list of SortGroupClause nodes
2216  * seen_local	bitmapset of sortgrouprefs already seen at the local level
2217  * pstate		ParseState
2218  * gexpr		node to transform
2219  * targetlist	reference to TargetEntry list
2220  * sortClause	ORDER BY clause (SortGroupClause nodes)
2221  * exprKind		expression kind
2222  * useSQL99		SQL99 rather than SQL92 syntax
2223  * toplevel		false if within any grouping set
2224  */
2225 static Index
transformGroupClauseExpr(List ** flatresult,Bitmapset * seen_local,ParseState * pstate,Node * gexpr,List ** targetlist,List * sortClause,ParseExprKind exprKind,bool useSQL99,bool toplevel)2226 transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local,
2227 						 ParseState *pstate, Node *gexpr,
2228 						 List **targetlist, List *sortClause,
2229 						 ParseExprKind exprKind, bool useSQL99, bool toplevel)
2230 {
2231 	TargetEntry *tle;
2232 	bool		found = false;
2233 
2234 	if (useSQL99)
2235 		tle = findTargetlistEntrySQL99(pstate, gexpr,
2236 									   targetlist, exprKind);
2237 	else
2238 		tle = findTargetlistEntrySQL92(pstate, gexpr,
2239 									   targetlist, exprKind);
2240 
2241 	if (tle->ressortgroupref > 0)
2242 	{
2243 		ListCell   *sl;
2244 
2245 		/*
2246 		 * Eliminate duplicates (GROUP BY x, x) but only at local level.
2247 		 * (Duplicates in grouping sets can affect the number of returned
2248 		 * rows, so can't be dropped indiscriminately.)
2249 		 *
2250 		 * Since we don't care about anything except the sortgroupref, we can
2251 		 * use a bitmapset rather than scanning lists.
2252 		 */
2253 		if (bms_is_member(tle->ressortgroupref, seen_local))
2254 			return 0;
2255 
2256 		/*
2257 		 * If we're already in the flat clause list, we don't need to consider
2258 		 * adding ourselves again.
2259 		 */
2260 		found = targetIsInSortList(tle, InvalidOid, *flatresult);
2261 		if (found)
2262 			return tle->ressortgroupref;
2263 
2264 		/*
2265 		 * If the GROUP BY tlist entry also appears in ORDER BY, copy operator
2266 		 * info from the (first) matching ORDER BY item.  This means that if
2267 		 * you write something like "GROUP BY foo ORDER BY foo USING <<<", the
2268 		 * GROUP BY operation silently takes on the equality semantics implied
2269 		 * by the ORDER BY.  There are two reasons to do this: it improves the
2270 		 * odds that we can implement both GROUP BY and ORDER BY with a single
2271 		 * sort step, and it allows the user to choose the equality semantics
2272 		 * used by GROUP BY, should she be working with a datatype that has
2273 		 * more than one equality operator.
2274 		 *
2275 		 * If we're in a grouping set, though, we force our requested ordering
2276 		 * to be NULLS LAST, because if we have any hope of using a sorted agg
2277 		 * for the job, we're going to be tacking on generated NULL values
2278 		 * after the corresponding groups. If the user demands nulls first,
2279 		 * another sort step is going to be inevitable, but that's the
2280 		 * planner's problem.
2281 		 */
2282 
2283 		foreach(sl, sortClause)
2284 		{
2285 			SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
2286 
2287 			if (sc->tleSortGroupRef == tle->ressortgroupref)
2288 			{
2289 				SortGroupClause *grpc = copyObject(sc);
2290 
2291 				if (!toplevel)
2292 					grpc->nulls_first = false;
2293 				*flatresult = lappend(*flatresult, grpc);
2294 				found = true;
2295 				break;
2296 			}
2297 		}
2298 	}
2299 
2300 	/*
2301 	 * If no match in ORDER BY, just add it to the result using default
2302 	 * sort/group semantics.
2303 	 */
2304 	if (!found)
2305 		*flatresult = addTargetToGroupList(pstate, tle,
2306 										   *flatresult, *targetlist,
2307 										   exprLocation(gexpr));
2308 
2309 	/*
2310 	 * _something_ must have assigned us a sortgroupref by now...
2311 	 */
2312 
2313 	return tle->ressortgroupref;
2314 }
2315 
2316 /*
2317  * Transform a list of expressions within a GROUP BY clause or grouping set.
2318  *
2319  * The list of expressions belongs to a single clause within which duplicates
2320  * can be safely eliminated.
2321  *
2322  * Returns an integer list of ressortgroupref values.
2323  *
2324  * flatresult	reference to flat list of SortGroupClause nodes
2325  * pstate		ParseState
2326  * list			nodes to transform
2327  * targetlist	reference to TargetEntry list
2328  * sortClause	ORDER BY clause (SortGroupClause nodes)
2329  * exprKind		expression kind
2330  * useSQL99		SQL99 rather than SQL92 syntax
2331  * toplevel		false if within any grouping set
2332  */
2333 static List *
transformGroupClauseList(List ** flatresult,ParseState * pstate,List * list,List ** targetlist,List * sortClause,ParseExprKind exprKind,bool useSQL99,bool toplevel)2334 transformGroupClauseList(List **flatresult,
2335 						 ParseState *pstate, List *list,
2336 						 List **targetlist, List *sortClause,
2337 						 ParseExprKind exprKind, bool useSQL99, bool toplevel)
2338 {
2339 	Bitmapset  *seen_local = NULL;
2340 	List	   *result = NIL;
2341 	ListCell   *gl;
2342 
2343 	foreach(gl, list)
2344 	{
2345 		Node	   *gexpr = (Node *) lfirst(gl);
2346 
2347 		Index		ref = transformGroupClauseExpr(flatresult,
2348 												   seen_local,
2349 												   pstate,
2350 												   gexpr,
2351 												   targetlist,
2352 												   sortClause,
2353 												   exprKind,
2354 												   useSQL99,
2355 												   toplevel);
2356 
2357 		if (ref > 0)
2358 		{
2359 			seen_local = bms_add_member(seen_local, ref);
2360 			result = lappend_int(result, ref);
2361 		}
2362 	}
2363 
2364 	return result;
2365 }
2366 
2367 /*
2368  * Transform a grouping set and (recursively) its content.
2369  *
2370  * The grouping set might be a GROUPING SETS node with other grouping sets
2371  * inside it, but SETS within SETS have already been flattened out before
2372  * reaching here.
2373  *
2374  * Returns the transformed node, which now contains SIMPLE nodes with lists
2375  * of ressortgrouprefs rather than expressions.
2376  *
2377  * flatresult	reference to flat list of SortGroupClause nodes
2378  * pstate		ParseState
2379  * gset			grouping set to transform
2380  * targetlist	reference to TargetEntry list
2381  * sortClause	ORDER BY clause (SortGroupClause nodes)
2382  * exprKind		expression kind
2383  * useSQL99		SQL99 rather than SQL92 syntax
2384  * toplevel		false if within any grouping set
2385  */
2386 static Node *
transformGroupingSet(List ** flatresult,ParseState * pstate,GroupingSet * gset,List ** targetlist,List * sortClause,ParseExprKind exprKind,bool useSQL99,bool toplevel)2387 transformGroupingSet(List **flatresult,
2388 					 ParseState *pstate, GroupingSet *gset,
2389 					 List **targetlist, List *sortClause,
2390 					 ParseExprKind exprKind, bool useSQL99, bool toplevel)
2391 {
2392 	ListCell   *gl;
2393 	List	   *content = NIL;
2394 
2395 	Assert(toplevel || gset->kind != GROUPING_SET_SETS);
2396 
2397 	foreach(gl, gset->content)
2398 	{
2399 		Node	   *n = lfirst(gl);
2400 
2401 		if (IsA(n, List))
2402 		{
2403 			List	   *l = transformGroupClauseList(flatresult,
2404 													 pstate, (List *) n,
2405 													 targetlist, sortClause,
2406 													 exprKind, useSQL99, false);
2407 
2408 			content = lappend(content, makeGroupingSet(GROUPING_SET_SIMPLE,
2409 													   l,
2410 													   exprLocation(n)));
2411 		}
2412 		else if (IsA(n, GroupingSet))
2413 		{
2414 			GroupingSet *gset2 = (GroupingSet *) lfirst(gl);
2415 
2416 			content = lappend(content, transformGroupingSet(flatresult,
2417 															pstate, gset2,
2418 															targetlist, sortClause,
2419 															exprKind, useSQL99, false));
2420 		}
2421 		else
2422 		{
2423 			Index		ref = transformGroupClauseExpr(flatresult,
2424 													   NULL,
2425 													   pstate,
2426 													   n,
2427 													   targetlist,
2428 													   sortClause,
2429 													   exprKind,
2430 													   useSQL99,
2431 													   false);
2432 
2433 			content = lappend(content, makeGroupingSet(GROUPING_SET_SIMPLE,
2434 													   list_make1_int(ref),
2435 													   exprLocation(n)));
2436 		}
2437 	}
2438 
2439 	/* Arbitrarily cap the size of CUBE, which has exponential growth */
2440 	if (gset->kind == GROUPING_SET_CUBE)
2441 	{
2442 		if (list_length(content) > 12)
2443 			ereport(ERROR,
2444 					(errcode(ERRCODE_TOO_MANY_COLUMNS),
2445 					 errmsg("CUBE is limited to 12 elements"),
2446 					 parser_errposition(pstate, gset->location)));
2447 	}
2448 
2449 	return (Node *) makeGroupingSet(gset->kind, content, gset->location);
2450 }
2451 
2452 
2453 /*
2454  * transformGroupClause -
2455  *	  transform a GROUP BY clause
2456  *
2457  * GROUP BY items will be added to the targetlist (as resjunk columns)
2458  * if not already present, so the targetlist must be passed by reference.
2459  *
2460  * This is also used for window PARTITION BY clauses (which act almost the
2461  * same, but are always interpreted per SQL99 rules).
2462  *
2463  * Grouping sets make this a lot more complex than it was. Our goal here is
2464  * twofold: we make a flat list of SortGroupClause nodes referencing each
2465  * distinct expression used for grouping, with those expressions added to the
2466  * targetlist if needed. At the same time, we build the groupingSets tree,
2467  * which stores only ressortgrouprefs as integer lists inside GroupingSet nodes
2468  * (possibly nested, but limited in depth: a GROUPING_SET_SETS node can contain
2469  * nested SIMPLE, CUBE or ROLLUP nodes, but not more sets - we flatten that
2470  * out; while CUBE and ROLLUP can contain only SIMPLE nodes).
2471  *
2472  * We skip much of the hard work if there are no grouping sets.
2473  *
2474  * One subtlety is that the groupClause list can end up empty while the
2475  * groupingSets list is not; this happens if there are only empty grouping
2476  * sets, or an explicit GROUP BY (). This has the same effect as specifying
2477  * aggregates or a HAVING clause with no GROUP BY; the output is one row per
2478  * grouping set even if the input is empty.
2479  *
2480  * Returns the transformed (flat) groupClause.
2481  *
2482  * pstate		ParseState
2483  * grouplist	clause to transform
2484  * groupingSets reference to list to contain the grouping set tree
2485  * targetlist	reference to TargetEntry list
2486  * sortClause	ORDER BY clause (SortGroupClause nodes)
2487  * exprKind		expression kind
2488  * useSQL99		SQL99 rather than SQL92 syntax
2489  */
2490 List *
transformGroupClause(ParseState * pstate,List * grouplist,List ** groupingSets,List ** targetlist,List * sortClause,ParseExprKind exprKind,bool useSQL99)2491 transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets,
2492 					 List **targetlist, List *sortClause,
2493 					 ParseExprKind exprKind, bool useSQL99)
2494 {
2495 	List	   *result = NIL;
2496 	List	   *flat_grouplist;
2497 	List	   *gsets = NIL;
2498 	ListCell   *gl;
2499 	bool		hasGroupingSets = false;
2500 	Bitmapset  *seen_local = NULL;
2501 
2502 	/*
2503 	 * Recursively flatten implicit RowExprs. (Technically this is only needed
2504 	 * for GROUP BY, per the syntax rules for grouping sets, but we do it
2505 	 * anyway.)
2506 	 */
2507 	flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
2508 													true,
2509 													&hasGroupingSets);
2510 
2511 	/*
2512 	 * If the list is now empty, but hasGroupingSets is true, it's because we
2513 	 * elided redundant empty grouping sets. Restore a single empty grouping
2514 	 * set to leave a canonical form: GROUP BY ()
2515 	 */
2516 
2517 	if (flat_grouplist == NIL && hasGroupingSets)
2518 	{
2519 		flat_grouplist = list_make1(makeGroupingSet(GROUPING_SET_EMPTY,
2520 													NIL,
2521 													exprLocation((Node *) grouplist)));
2522 	}
2523 
2524 	foreach(gl, flat_grouplist)
2525 	{
2526 		Node	   *gexpr = (Node *) lfirst(gl);
2527 
2528 		if (IsA(gexpr, GroupingSet))
2529 		{
2530 			GroupingSet *gset = (GroupingSet *) gexpr;
2531 
2532 			switch (gset->kind)
2533 			{
2534 				case GROUPING_SET_EMPTY:
2535 					gsets = lappend(gsets, gset);
2536 					break;
2537 				case GROUPING_SET_SIMPLE:
2538 					/* can't happen */
2539 					Assert(false);
2540 					break;
2541 				case GROUPING_SET_SETS:
2542 				case GROUPING_SET_CUBE:
2543 				case GROUPING_SET_ROLLUP:
2544 					gsets = lappend(gsets,
2545 									transformGroupingSet(&result,
2546 														 pstate, gset,
2547 														 targetlist, sortClause,
2548 														 exprKind, useSQL99, true));
2549 					break;
2550 			}
2551 		}
2552 		else
2553 		{
2554 			Index		ref = transformGroupClauseExpr(&result, seen_local,
2555 													   pstate, gexpr,
2556 													   targetlist, sortClause,
2557 													   exprKind, useSQL99, true);
2558 
2559 			if (ref > 0)
2560 			{
2561 				seen_local = bms_add_member(seen_local, ref);
2562 				if (hasGroupingSets)
2563 					gsets = lappend(gsets,
2564 									makeGroupingSet(GROUPING_SET_SIMPLE,
2565 													list_make1_int(ref),
2566 													exprLocation(gexpr)));
2567 			}
2568 		}
2569 	}
2570 
2571 	/* parser should prevent this */
2572 	Assert(gsets == NIL || groupingSets != NULL);
2573 
2574 	if (groupingSets)
2575 		*groupingSets = gsets;
2576 
2577 	return result;
2578 }
2579 
2580 /*
2581  * transformSortClause -
2582  *	  transform an ORDER BY clause
2583  *
2584  * ORDER BY items will be added to the targetlist (as resjunk columns)
2585  * if not already present, so the targetlist must be passed by reference.
2586  *
2587  * This is also used for window and aggregate ORDER BY clauses (which act
2588  * almost the same, but are always interpreted per SQL99 rules).
2589  */
2590 List *
transformSortClause(ParseState * pstate,List * orderlist,List ** targetlist,ParseExprKind exprKind,bool useSQL99)2591 transformSortClause(ParseState *pstate,
2592 					List *orderlist,
2593 					List **targetlist,
2594 					ParseExprKind exprKind,
2595 					bool useSQL99)
2596 {
2597 	List	   *sortlist = NIL;
2598 	ListCell   *olitem;
2599 
2600 	foreach(olitem, orderlist)
2601 	{
2602 		SortBy	   *sortby = (SortBy *) lfirst(olitem);
2603 		TargetEntry *tle;
2604 
2605 		if (useSQL99)
2606 			tle = findTargetlistEntrySQL99(pstate, sortby->node,
2607 										   targetlist, exprKind);
2608 		else
2609 			tle = findTargetlistEntrySQL92(pstate, sortby->node,
2610 										   targetlist, exprKind);
2611 
2612 		sortlist = addTargetToSortList(pstate, tle,
2613 									   sortlist, *targetlist, sortby);
2614 	}
2615 
2616 	return sortlist;
2617 }
2618 
2619 /*
2620  * transformWindowDefinitions -
2621  *		transform window definitions (WindowDef to WindowClause)
2622  */
2623 List *
transformWindowDefinitions(ParseState * pstate,List * windowdefs,List ** targetlist)2624 transformWindowDefinitions(ParseState *pstate,
2625 						   List *windowdefs,
2626 						   List **targetlist)
2627 {
2628 	List	   *result = NIL;
2629 	Index		winref = 0;
2630 	ListCell   *lc;
2631 
2632 	foreach(lc, windowdefs)
2633 	{
2634 		WindowDef  *windef = (WindowDef *) lfirst(lc);
2635 		WindowClause *refwc = NULL;
2636 		List	   *partitionClause;
2637 		List	   *orderClause;
2638 		Oid			rangeopfamily = InvalidOid;
2639 		Oid			rangeopcintype = InvalidOid;
2640 		WindowClause *wc;
2641 
2642 		winref++;
2643 
2644 		/*
2645 		 * Check for duplicate window names.
2646 		 */
2647 		if (windef->name &&
2648 			findWindowClause(result, windef->name) != NULL)
2649 			ereport(ERROR,
2650 					(errcode(ERRCODE_WINDOWING_ERROR),
2651 					 errmsg("window \"%s\" is already defined", windef->name),
2652 					 parser_errposition(pstate, windef->location)));
2653 
2654 		/*
2655 		 * If it references a previous window, look that up.
2656 		 */
2657 		if (windef->refname)
2658 		{
2659 			refwc = findWindowClause(result, windef->refname);
2660 			if (refwc == NULL)
2661 				ereport(ERROR,
2662 						(errcode(ERRCODE_UNDEFINED_OBJECT),
2663 						 errmsg("window \"%s\" does not exist",
2664 								windef->refname),
2665 						 parser_errposition(pstate, windef->location)));
2666 		}
2667 
2668 		/*
2669 		 * Transform PARTITION and ORDER specs, if any.  These are treated
2670 		 * almost exactly like top-level GROUP BY and ORDER BY clauses,
2671 		 * including the special handling of nondefault operator semantics.
2672 		 */
2673 		orderClause = transformSortClause(pstate,
2674 										  windef->orderClause,
2675 										  targetlist,
2676 										  EXPR_KIND_WINDOW_ORDER,
2677 										  true /* force SQL99 rules */ );
2678 		partitionClause = transformGroupClause(pstate,
2679 											   windef->partitionClause,
2680 											   NULL,
2681 											   targetlist,
2682 											   orderClause,
2683 											   EXPR_KIND_WINDOW_PARTITION,
2684 											   true /* force SQL99 rules */ );
2685 
2686 		/*
2687 		 * And prepare the new WindowClause.
2688 		 */
2689 		wc = makeNode(WindowClause);
2690 		wc->name = windef->name;
2691 		wc->refname = windef->refname;
2692 
2693 		/*
2694 		 * Per spec, a windowdef that references a previous one copies the
2695 		 * previous partition clause (and mustn't specify its own).  It can
2696 		 * specify its own ordering clause, but only if the previous one had
2697 		 * none.  It always specifies its own frame clause, and the previous
2698 		 * one must not have a frame clause.  Yeah, it's bizarre that each of
2699 		 * these cases works differently, but SQL:2008 says so; see 7.11
2700 		 * <window clause> syntax rule 10 and general rule 1.  The frame
2701 		 * clause rule is especially bizarre because it makes "OVER foo"
2702 		 * different from "OVER (foo)", and requires the latter to throw an
2703 		 * error if foo has a nondefault frame clause.  Well, ours not to
2704 		 * reason why, but we do go out of our way to throw a useful error
2705 		 * message for such cases.
2706 		 */
2707 		if (refwc)
2708 		{
2709 			if (partitionClause)
2710 				ereport(ERROR,
2711 						(errcode(ERRCODE_WINDOWING_ERROR),
2712 						 errmsg("cannot override PARTITION BY clause of window \"%s\"",
2713 								windef->refname),
2714 						 parser_errposition(pstate, windef->location)));
2715 			wc->partitionClause = copyObject(refwc->partitionClause);
2716 		}
2717 		else
2718 			wc->partitionClause = partitionClause;
2719 		if (refwc)
2720 		{
2721 			if (orderClause && refwc->orderClause)
2722 				ereport(ERROR,
2723 						(errcode(ERRCODE_WINDOWING_ERROR),
2724 						 errmsg("cannot override ORDER BY clause of window \"%s\"",
2725 								windef->refname),
2726 						 parser_errposition(pstate, windef->location)));
2727 			if (orderClause)
2728 			{
2729 				wc->orderClause = orderClause;
2730 				wc->copiedOrder = false;
2731 			}
2732 			else
2733 			{
2734 				wc->orderClause = copyObject(refwc->orderClause);
2735 				wc->copiedOrder = true;
2736 			}
2737 		}
2738 		else
2739 		{
2740 			wc->orderClause = orderClause;
2741 			wc->copiedOrder = false;
2742 		}
2743 		if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
2744 		{
2745 			/*
2746 			 * Use this message if this is a WINDOW clause, or if it's an OVER
2747 			 * clause that includes ORDER BY or framing clauses.  (We already
2748 			 * rejected PARTITION BY above, so no need to check that.)
2749 			 */
2750 			if (windef->name ||
2751 				orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
2752 				ereport(ERROR,
2753 						(errcode(ERRCODE_WINDOWING_ERROR),
2754 						 errmsg("cannot copy window \"%s\" because it has a frame clause",
2755 								windef->refname),
2756 						 parser_errposition(pstate, windef->location)));
2757 			/* Else this clause is just OVER (foo), so say this: */
2758 			ereport(ERROR,
2759 					(errcode(ERRCODE_WINDOWING_ERROR),
2760 					 errmsg("cannot copy window \"%s\" because it has a frame clause",
2761 							windef->refname),
2762 					 errhint("Omit the parentheses in this OVER clause."),
2763 					 parser_errposition(pstate, windef->location)));
2764 		}
2765 		wc->frameOptions = windef->frameOptions;
2766 
2767 		/*
2768 		 * RANGE offset PRECEDING/FOLLOWING requires exactly one ORDER BY
2769 		 * column; check that and get its sort opfamily info.
2770 		 */
2771 		if ((wc->frameOptions & FRAMEOPTION_RANGE) &&
2772 			(wc->frameOptions & (FRAMEOPTION_START_OFFSET |
2773 								 FRAMEOPTION_END_OFFSET)))
2774 		{
2775 			SortGroupClause *sortcl;
2776 			Node	   *sortkey;
2777 			int16		rangestrategy;
2778 
2779 			if (list_length(wc->orderClause) != 1)
2780 				ereport(ERROR,
2781 						(errcode(ERRCODE_WINDOWING_ERROR),
2782 						 errmsg("RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column"),
2783 						 parser_errposition(pstate, windef->location)));
2784 			sortcl = castNode(SortGroupClause, linitial(wc->orderClause));
2785 			sortkey = get_sortgroupclause_expr(sortcl, *targetlist);
2786 			/* Find the sort operator in pg_amop */
2787 			if (!get_ordering_op_properties(sortcl->sortop,
2788 											&rangeopfamily,
2789 											&rangeopcintype,
2790 											&rangestrategy))
2791 				elog(ERROR, "operator %u is not a valid ordering operator",
2792 					 sortcl->sortop);
2793 			/* Record properties of sort ordering */
2794 			wc->inRangeColl = exprCollation(sortkey);
2795 			wc->inRangeAsc = (rangestrategy == BTLessStrategyNumber);
2796 			wc->inRangeNullsFirst = sortcl->nulls_first;
2797 		}
2798 
2799 		/* Per spec, GROUPS mode requires an ORDER BY clause */
2800 		if (wc->frameOptions & FRAMEOPTION_GROUPS)
2801 		{
2802 			if (wc->orderClause == NIL)
2803 				ereport(ERROR,
2804 						(errcode(ERRCODE_WINDOWING_ERROR),
2805 						 errmsg("GROUPS mode requires an ORDER BY clause"),
2806 						 parser_errposition(pstate, windef->location)));
2807 		}
2808 
2809 		/* Process frame offset expressions */
2810 		wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
2811 											   rangeopfamily, rangeopcintype,
2812 											   &wc->startInRangeFunc,
2813 											   windef->startOffset);
2814 		wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
2815 											 rangeopfamily, rangeopcintype,
2816 											 &wc->endInRangeFunc,
2817 											 windef->endOffset);
2818 		wc->winref = winref;
2819 
2820 		result = lappend(result, wc);
2821 	}
2822 
2823 	return result;
2824 }
2825 
2826 /*
2827  * transformDistinctClause -
2828  *	  transform a DISTINCT clause
2829  *
2830  * Since we may need to add items to the query's targetlist, that list
2831  * is passed by reference.
2832  *
2833  * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
2834  * possible into the distinctClause.  This avoids a possible need to re-sort,
2835  * and allows the user to choose the equality semantics used by DISTINCT,
2836  * should she be working with a datatype that has more than one equality
2837  * operator.
2838  *
2839  * is_agg is true if we are transforming an aggregate(DISTINCT ...)
2840  * function call.  This does not affect any behavior, only the phrasing
2841  * of error messages.
2842  */
2843 List *
transformDistinctClause(ParseState * pstate,List ** targetlist,List * sortClause,bool is_agg)2844 transformDistinctClause(ParseState *pstate,
2845 						List **targetlist, List *sortClause, bool is_agg)
2846 {
2847 	List	   *result = NIL;
2848 	ListCell   *slitem;
2849 	ListCell   *tlitem;
2850 
2851 	/*
2852 	 * The distinctClause should consist of all ORDER BY items followed by all
2853 	 * other non-resjunk targetlist items.  There must not be any resjunk
2854 	 * ORDER BY items --- that would imply that we are sorting by a value that
2855 	 * isn't necessarily unique within a DISTINCT group, so the results
2856 	 * wouldn't be well-defined.  This construction ensures we follow the rule
2857 	 * that sortClause and distinctClause match; in fact the sortClause will
2858 	 * always be a prefix of distinctClause.
2859 	 *
2860 	 * Note a corner case: the same TLE could be in the ORDER BY list multiple
2861 	 * times with different sortops.  We have to include it in the
2862 	 * distinctClause the same way to preserve the prefix property. The net
2863 	 * effect will be that the TLE value will be made unique according to both
2864 	 * sortops.
2865 	 */
2866 	foreach(slitem, sortClause)
2867 	{
2868 		SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
2869 		TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
2870 
2871 		if (tle->resjunk)
2872 			ereport(ERROR,
2873 					(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2874 					 is_agg ?
2875 					 errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
2876 					 errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
2877 					 parser_errposition(pstate,
2878 										exprLocation((Node *) tle->expr))));
2879 		result = lappend(result, copyObject(scl));
2880 	}
2881 
2882 	/*
2883 	 * Now add any remaining non-resjunk tlist items, using default sort/group
2884 	 * semantics for their data types.
2885 	 */
2886 	foreach(tlitem, *targetlist)
2887 	{
2888 		TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
2889 
2890 		if (tle->resjunk)
2891 			continue;			/* ignore junk */
2892 		result = addTargetToGroupList(pstate, tle,
2893 									  result, *targetlist,
2894 									  exprLocation((Node *) tle->expr));
2895 	}
2896 
2897 	/*
2898 	 * Complain if we found nothing to make DISTINCT.  Returning an empty list
2899 	 * would cause the parsed Query to look like it didn't have DISTINCT, with
2900 	 * results that would probably surprise the user.  Note: this case is
2901 	 * presently impossible for aggregates because of grammar restrictions,
2902 	 * but we check anyway.
2903 	 */
2904 	if (result == NIL)
2905 		ereport(ERROR,
2906 				(errcode(ERRCODE_SYNTAX_ERROR),
2907 				 is_agg ?
2908 				 errmsg("an aggregate with DISTINCT must have at least one argument") :
2909 				 errmsg("SELECT DISTINCT must have at least one column")));
2910 
2911 	return result;
2912 }
2913 
2914 /*
2915  * transformDistinctOnClause -
2916  *	  transform a DISTINCT ON clause
2917  *
2918  * Since we may need to add items to the query's targetlist, that list
2919  * is passed by reference.
2920  *
2921  * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
2922  * possible into the distinctClause.  This avoids a possible need to re-sort,
2923  * and allows the user to choose the equality semantics used by DISTINCT,
2924  * should she be working with a datatype that has more than one equality
2925  * operator.
2926  */
2927 List *
transformDistinctOnClause(ParseState * pstate,List * distinctlist,List ** targetlist,List * sortClause)2928 transformDistinctOnClause(ParseState *pstate, List *distinctlist,
2929 						  List **targetlist, List *sortClause)
2930 {
2931 	List	   *result = NIL;
2932 	List	   *sortgrouprefs = NIL;
2933 	bool		skipped_sortitem;
2934 	ListCell   *lc;
2935 	ListCell   *lc2;
2936 
2937 	/*
2938 	 * Add all the DISTINCT ON expressions to the tlist (if not already
2939 	 * present, they are added as resjunk items).  Assign sortgroupref numbers
2940 	 * to them, and make a list of these numbers.  (NB: we rely below on the
2941 	 * sortgrouprefs list being one-for-one with the original distinctlist.
2942 	 * Also notice that we could have duplicate DISTINCT ON expressions and
2943 	 * hence duplicate entries in sortgrouprefs.)
2944 	 */
2945 	foreach(lc, distinctlist)
2946 	{
2947 		Node	   *dexpr = (Node *) lfirst(lc);
2948 		int			sortgroupref;
2949 		TargetEntry *tle;
2950 
2951 		tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
2952 									   EXPR_KIND_DISTINCT_ON);
2953 		sortgroupref = assignSortGroupRef(tle, *targetlist);
2954 		sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
2955 	}
2956 
2957 	/*
2958 	 * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
2959 	 * semantics from ORDER BY items that match DISTINCT ON items, and also
2960 	 * adopt their column sort order.  We insist that the distinctClause and
2961 	 * sortClause match, so throw error if we find the need to add any more
2962 	 * distinctClause items after we've skipped an ORDER BY item that wasn't
2963 	 * in DISTINCT ON.
2964 	 */
2965 	skipped_sortitem = false;
2966 	foreach(lc, sortClause)
2967 	{
2968 		SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
2969 
2970 		if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
2971 		{
2972 			if (skipped_sortitem)
2973 				ereport(ERROR,
2974 						(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2975 						 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2976 						 parser_errposition(pstate,
2977 											get_matching_location(scl->tleSortGroupRef,
2978 																  sortgrouprefs,
2979 																  distinctlist))));
2980 			else
2981 				result = lappend(result, copyObject(scl));
2982 		}
2983 		else
2984 			skipped_sortitem = true;
2985 	}
2986 
2987 	/*
2988 	 * Now add any remaining DISTINCT ON items, using default sort/group
2989 	 * semantics for their data types.  (Note: this is pretty questionable; if
2990 	 * the ORDER BY list doesn't include all the DISTINCT ON items and more
2991 	 * besides, you certainly aren't using DISTINCT ON in the intended way,
2992 	 * and you probably aren't going to get consistent results.  It might be
2993 	 * better to throw an error or warning here.  But historically we've
2994 	 * allowed it, so keep doing so.)
2995 	 */
2996 	forboth(lc, distinctlist, lc2, sortgrouprefs)
2997 	{
2998 		Node	   *dexpr = (Node *) lfirst(lc);
2999 		int			sortgroupref = lfirst_int(lc2);
3000 		TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
3001 
3002 		if (targetIsInSortList(tle, InvalidOid, result))
3003 			continue;			/* already in list (with some semantics) */
3004 		if (skipped_sortitem)
3005 			ereport(ERROR,
3006 					(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3007 					 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
3008 					 parser_errposition(pstate, exprLocation(dexpr))));
3009 		result = addTargetToGroupList(pstate, tle,
3010 									  result, *targetlist,
3011 									  exprLocation(dexpr));
3012 	}
3013 
3014 	/*
3015 	 * An empty result list is impossible here because of grammar
3016 	 * restrictions.
3017 	 */
3018 	Assert(result != NIL);
3019 
3020 	return result;
3021 }
3022 
3023 /*
3024  * get_matching_location
3025  *		Get the exprLocation of the exprs member corresponding to the
3026  *		(first) member of sortgrouprefs that equals sortgroupref.
3027  *
3028  * This is used so that we can point at a troublesome DISTINCT ON entry.
3029  * (Note that we need to use the original untransformed DISTINCT ON list
3030  * item, as whatever TLE it corresponds to will very possibly have a
3031  * parse location pointing to some matching entry in the SELECT list
3032  * or ORDER BY list.)
3033  */
3034 static int
get_matching_location(int sortgroupref,List * sortgrouprefs,List * exprs)3035 get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
3036 {
3037 	ListCell   *lcs;
3038 	ListCell   *lce;
3039 
3040 	forboth(lcs, sortgrouprefs, lce, exprs)
3041 	{
3042 		if (lfirst_int(lcs) == sortgroupref)
3043 			return exprLocation((Node *) lfirst(lce));
3044 	}
3045 	/* if no match, caller blew it */
3046 	elog(ERROR, "get_matching_location: no matching sortgroupref");
3047 	return -1;					/* keep compiler quiet */
3048 }
3049 
3050 /*
3051  * resolve_unique_index_expr
3052  *		Infer a unique index from a list of indexElems, for ON
3053  *		CONFLICT clause
3054  *
3055  * Perform parse analysis of expressions and columns appearing within ON
3056  * CONFLICT clause.  During planning, the returned list of expressions is used
3057  * to infer which unique index to use.
3058  */
3059 static List *
resolve_unique_index_expr(ParseState * pstate,InferClause * infer,Relation heapRel)3060 resolve_unique_index_expr(ParseState *pstate, InferClause *infer,
3061 						  Relation heapRel)
3062 {
3063 	List	   *result = NIL;
3064 	ListCell   *l;
3065 
3066 	foreach(l, infer->indexElems)
3067 	{
3068 		IndexElem  *ielem = (IndexElem *) lfirst(l);
3069 		InferenceElem *pInfer = makeNode(InferenceElem);
3070 		Node	   *parse;
3071 
3072 		/*
3073 		 * Raw grammar re-uses CREATE INDEX infrastructure for unique index
3074 		 * inference clause, and so will accept opclasses by name and so on.
3075 		 *
3076 		 * Make no attempt to match ASC or DESC ordering or NULLS FIRST/NULLS
3077 		 * LAST ordering, since those are not significant for inference
3078 		 * purposes (any unique index matching the inference specification in
3079 		 * other regards is accepted indifferently).  Actively reject this as
3080 		 * wrong-headed.
3081 		 */
3082 		if (ielem->ordering != SORTBY_DEFAULT)
3083 			ereport(ERROR,
3084 					(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3085 					 errmsg("ASC/DESC is not allowed in ON CONFLICT clause"),
3086 					 parser_errposition(pstate,
3087 										exprLocation((Node *) infer))));
3088 		if (ielem->nulls_ordering != SORTBY_NULLS_DEFAULT)
3089 			ereport(ERROR,
3090 					(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3091 					 errmsg("NULLS FIRST/LAST is not allowed in ON CONFLICT clause"),
3092 					 parser_errposition(pstate,
3093 										exprLocation((Node *) infer))));
3094 
3095 		if (!ielem->expr)
3096 		{
3097 			/* Simple index attribute */
3098 			ColumnRef  *n;
3099 
3100 			/*
3101 			 * Grammar won't have built raw expression for us in event of
3102 			 * plain column reference.  Create one directly, and perform
3103 			 * expression transformation.  Planner expects this, and performs
3104 			 * its own normalization for the purposes of matching against
3105 			 * pg_index.
3106 			 */
3107 			n = makeNode(ColumnRef);
3108 			n->fields = list_make1(makeString(ielem->name));
3109 			/* Location is approximately that of inference specification */
3110 			n->location = infer->location;
3111 			parse = (Node *) n;
3112 		}
3113 		else
3114 		{
3115 			/* Do parse transformation of the raw expression */
3116 			parse = (Node *) ielem->expr;
3117 		}
3118 
3119 		/*
3120 		 * transformExpr() will reject subqueries, aggregates, window
3121 		 * functions, and SRFs, based on being passed
3122 		 * EXPR_KIND_INDEX_EXPRESSION.  So we needn't worry about those
3123 		 * further ... not that they would match any available index
3124 		 * expression anyway.
3125 		 */
3126 		pInfer->expr = transformExpr(pstate, parse, EXPR_KIND_INDEX_EXPRESSION);
3127 
3128 		/* Perform lookup of collation and operator class as required */
3129 		if (!ielem->collation)
3130 			pInfer->infercollid = InvalidOid;
3131 		else
3132 			pInfer->infercollid = LookupCollation(pstate, ielem->collation,
3133 												  exprLocation(pInfer->expr));
3134 
3135 		if (!ielem->opclass)
3136 			pInfer->inferopclass = InvalidOid;
3137 		else
3138 			pInfer->inferopclass = get_opclass_oid(BTREE_AM_OID,
3139 												   ielem->opclass, false);
3140 
3141 		result = lappend(result, pInfer);
3142 	}
3143 
3144 	return result;
3145 }
3146 
3147 /*
3148  * transformOnConflictArbiter -
3149  *		transform arbiter expressions in an ON CONFLICT clause.
3150  *
3151  * Transformed expressions used to infer one unique index relation to serve as
3152  * an ON CONFLICT arbiter.  Partial unique indexes may be inferred using WHERE
3153  * clause from inference specification clause.
3154  */
3155 void
transformOnConflictArbiter(ParseState * pstate,OnConflictClause * onConflictClause,List ** arbiterExpr,Node ** arbiterWhere,Oid * constraint)3156 transformOnConflictArbiter(ParseState *pstate,
3157 						   OnConflictClause *onConflictClause,
3158 						   List **arbiterExpr, Node **arbiterWhere,
3159 						   Oid *constraint)
3160 {
3161 	InferClause *infer = onConflictClause->infer;
3162 
3163 	*arbiterExpr = NIL;
3164 	*arbiterWhere = NULL;
3165 	*constraint = InvalidOid;
3166 
3167 	if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
3168 		ereport(ERROR,
3169 				(errcode(ERRCODE_SYNTAX_ERROR),
3170 				 errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
3171 				 errhint("For example, ON CONFLICT (column_name)."),
3172 				 parser_errposition(pstate,
3173 									exprLocation((Node *) onConflictClause))));
3174 
3175 	/*
3176 	 * To simplify certain aspects of its design, speculative insertion into
3177 	 * system catalogs is disallowed
3178 	 */
3179 	if (IsCatalogRelation(pstate->p_target_relation))
3180 		ereport(ERROR,
3181 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3182 				 errmsg("ON CONFLICT is not supported with system catalog tables"),
3183 				 parser_errposition(pstate,
3184 									exprLocation((Node *) onConflictClause))));
3185 
3186 	/* Same applies to table used by logical decoding as catalog table */
3187 	if (RelationIsUsedAsCatalogTable(pstate->p_target_relation))
3188 		ereport(ERROR,
3189 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3190 				 errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
3191 						RelationGetRelationName(pstate->p_target_relation)),
3192 				 parser_errposition(pstate,
3193 									exprLocation((Node *) onConflictClause))));
3194 
3195 	/* ON CONFLICT DO NOTHING does not require an inference clause */
3196 	if (infer)
3197 	{
3198 		List	   *save_namespace;
3199 
3200 		/*
3201 		 * While we process the arbiter expressions, accept only non-qualified
3202 		 * references to the target table. Hide any other relations.
3203 		 */
3204 		save_namespace = pstate->p_namespace;
3205 		pstate->p_namespace = NIL;
3206 		addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
3207 					  false, false, true);
3208 
3209 		if (infer->indexElems)
3210 			*arbiterExpr = resolve_unique_index_expr(pstate, infer,
3211 													 pstate->p_target_relation);
3212 
3213 		/*
3214 		 * Handling inference WHERE clause (for partial unique index
3215 		 * inference)
3216 		 */
3217 		if (infer->whereClause)
3218 			*arbiterWhere = transformExpr(pstate, infer->whereClause,
3219 										  EXPR_KIND_INDEX_PREDICATE);
3220 
3221 		pstate->p_namespace = save_namespace;
3222 
3223 		/*
3224 		 * If the arbiter is specified by constraint name, get the constraint
3225 		 * OID and mark the constrained columns as requiring SELECT privilege,
3226 		 * in the same way as would have happened if the arbiter had been
3227 		 * specified by explicit reference to the constraint's index columns.
3228 		 */
3229 		if (infer->conname)
3230 		{
3231 			Oid			relid = RelationGetRelid(pstate->p_target_relation);
3232 			RangeTblEntry *rte = pstate->p_target_rangetblentry;
3233 			Bitmapset  *conattnos;
3234 
3235 			conattnos = get_relation_constraint_attnos(relid, infer->conname,
3236 													   false, constraint);
3237 
3238 			/* Make sure the rel as a whole is marked for SELECT access */
3239 			rte->requiredPerms |= ACL_SELECT;
3240 			/* Mark the constrained columns as requiring SELECT access */
3241 			rte->selectedCols = bms_add_members(rte->selectedCols, conattnos);
3242 		}
3243 	}
3244 
3245 	/*
3246 	 * It's convenient to form a list of expressions based on the
3247 	 * representation used by CREATE INDEX, since the same restrictions are
3248 	 * appropriate (e.g. on subqueries).  However, from here on, a dedicated
3249 	 * primnode representation is used for inference elements, and so
3250 	 * assign_query_collations() can be trusted to do the right thing with the
3251 	 * post parse analysis query tree inference clause representation.
3252 	 */
3253 }
3254 
3255 /*
3256  * addTargetToSortList
3257  *		If the given targetlist entry isn't already in the SortGroupClause
3258  *		list, add it to the end of the list, using the given sort ordering
3259  *		info.
3260  *
3261  * Returns the updated SortGroupClause list.
3262  */
3263 List *
addTargetToSortList(ParseState * pstate,TargetEntry * tle,List * sortlist,List * targetlist,SortBy * sortby)3264 addTargetToSortList(ParseState *pstate, TargetEntry *tle,
3265 					List *sortlist, List *targetlist, SortBy *sortby)
3266 {
3267 	Oid			restype = exprType((Node *) tle->expr);
3268 	Oid			sortop;
3269 	Oid			eqop;
3270 	bool		hashable;
3271 	bool		reverse;
3272 	int			location;
3273 	ParseCallbackState pcbstate;
3274 
3275 	/* if tlist item is an UNKNOWN literal, change it to TEXT */
3276 	if (restype == UNKNOWNOID)
3277 	{
3278 		tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3279 										 restype, TEXTOID, -1,
3280 										 COERCION_IMPLICIT,
3281 										 COERCE_IMPLICIT_CAST,
3282 										 -1);
3283 		restype = TEXTOID;
3284 	}
3285 
3286 	/*
3287 	 * Rather than clutter the API of get_sort_group_operators and the other
3288 	 * functions we're about to use, make use of error context callback to
3289 	 * mark any error reports with a parse position.  We point to the operator
3290 	 * location if present, else to the expression being sorted.  (NB: use the
3291 	 * original untransformed expression here; the TLE entry might well point
3292 	 * at a duplicate expression in the regular SELECT list.)
3293 	 */
3294 	location = sortby->location;
3295 	if (location < 0)
3296 		location = exprLocation(sortby->node);
3297 	setup_parser_errposition_callback(&pcbstate, pstate, location);
3298 
3299 	/* determine the sortop, eqop, and directionality */
3300 	switch (sortby->sortby_dir)
3301 	{
3302 		case SORTBY_DEFAULT:
3303 		case SORTBY_ASC:
3304 			get_sort_group_operators(restype,
3305 									 true, true, false,
3306 									 &sortop, &eqop, NULL,
3307 									 &hashable);
3308 			reverse = false;
3309 			break;
3310 		case SORTBY_DESC:
3311 			get_sort_group_operators(restype,
3312 									 false, true, true,
3313 									 NULL, &eqop, &sortop,
3314 									 &hashable);
3315 			reverse = true;
3316 			break;
3317 		case SORTBY_USING:
3318 			Assert(sortby->useOp != NIL);
3319 			sortop = compatible_oper_opid(sortby->useOp,
3320 										  restype,
3321 										  restype,
3322 										  false);
3323 
3324 			/*
3325 			 * Verify it's a valid ordering operator, fetch the corresponding
3326 			 * equality operator, and determine whether to consider it like
3327 			 * ASC or DESC.
3328 			 */
3329 			eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3330 			if (!OidIsValid(eqop))
3331 				ereport(ERROR,
3332 						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
3333 						 errmsg("operator %s is not a valid ordering operator",
3334 								strVal(llast(sortby->useOp))),
3335 						 errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3336 
3337 			/*
3338 			 * Also see if the equality operator is hashable.
3339 			 */
3340 			hashable = op_hashjoinable(eqop, restype);
3341 			break;
3342 		default:
3343 			elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3344 			sortop = InvalidOid;	/* keep compiler quiet */
3345 			eqop = InvalidOid;
3346 			hashable = false;
3347 			reverse = false;
3348 			break;
3349 	}
3350 
3351 	cancel_parser_errposition_callback(&pcbstate);
3352 
3353 	/* avoid making duplicate sortlist entries */
3354 	if (!targetIsInSortList(tle, sortop, sortlist))
3355 	{
3356 		SortGroupClause *sortcl = makeNode(SortGroupClause);
3357 
3358 		sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3359 
3360 		sortcl->eqop = eqop;
3361 		sortcl->sortop = sortop;
3362 		sortcl->hashable = hashable;
3363 
3364 		switch (sortby->sortby_nulls)
3365 		{
3366 			case SORTBY_NULLS_DEFAULT:
3367 				/* NULLS FIRST is default for DESC; other way for ASC */
3368 				sortcl->nulls_first = reverse;
3369 				break;
3370 			case SORTBY_NULLS_FIRST:
3371 				sortcl->nulls_first = true;
3372 				break;
3373 			case SORTBY_NULLS_LAST:
3374 				sortcl->nulls_first = false;
3375 				break;
3376 			default:
3377 				elog(ERROR, "unrecognized sortby_nulls: %d",
3378 					 sortby->sortby_nulls);
3379 				break;
3380 		}
3381 
3382 		sortlist = lappend(sortlist, sortcl);
3383 	}
3384 
3385 	return sortlist;
3386 }
3387 
3388 /*
3389  * addTargetToGroupList
3390  *		If the given targetlist entry isn't already in the SortGroupClause
3391  *		list, add it to the end of the list, using default sort/group
3392  *		semantics.
3393  *
3394  * This is very similar to addTargetToSortList, except that we allow the
3395  * case where only a grouping (equality) operator can be found, and that
3396  * the TLE is considered "already in the list" if it appears there with any
3397  * sorting semantics.
3398  *
3399  * location is the parse location to be fingered in event of trouble.  Note
3400  * that we can't rely on exprLocation(tle->expr), because that might point
3401  * to a SELECT item that matches the GROUP BY item; it'd be pretty confusing
3402  * to report such a location.
3403  *
3404  * Returns the updated SortGroupClause list.
3405  */
3406 static List *
addTargetToGroupList(ParseState * pstate,TargetEntry * tle,List * grouplist,List * targetlist,int location)3407 addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
3408 					 List *grouplist, List *targetlist, int location)
3409 {
3410 	Oid			restype = exprType((Node *) tle->expr);
3411 
3412 	/* if tlist item is an UNKNOWN literal, change it to TEXT */
3413 	if (restype == UNKNOWNOID)
3414 	{
3415 		tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3416 										 restype, TEXTOID, -1,
3417 										 COERCION_IMPLICIT,
3418 										 COERCE_IMPLICIT_CAST,
3419 										 -1);
3420 		restype = TEXTOID;
3421 	}
3422 
3423 	/* avoid making duplicate grouplist entries */
3424 	if (!targetIsInSortList(tle, InvalidOid, grouplist))
3425 	{
3426 		SortGroupClause *grpcl = makeNode(SortGroupClause);
3427 		Oid			sortop;
3428 		Oid			eqop;
3429 		bool		hashable;
3430 		ParseCallbackState pcbstate;
3431 
3432 		setup_parser_errposition_callback(&pcbstate, pstate, location);
3433 
3434 		/* determine the eqop and optional sortop */
3435 		get_sort_group_operators(restype,
3436 								 false, true, false,
3437 								 &sortop, &eqop, NULL,
3438 								 &hashable);
3439 
3440 		cancel_parser_errposition_callback(&pcbstate);
3441 
3442 		grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3443 		grpcl->eqop = eqop;
3444 		grpcl->sortop = sortop;
3445 		grpcl->nulls_first = false; /* OK with or without sortop */
3446 		grpcl->hashable = hashable;
3447 
3448 		grouplist = lappend(grouplist, grpcl);
3449 	}
3450 
3451 	return grouplist;
3452 }
3453 
3454 /*
3455  * assignSortGroupRef
3456  *	  Assign the targetentry an unused ressortgroupref, if it doesn't
3457  *	  already have one.  Return the assigned or pre-existing refnumber.
3458  *
3459  * 'tlist' is the targetlist containing (or to contain) the given targetentry.
3460  */
3461 Index
assignSortGroupRef(TargetEntry * tle,List * tlist)3462 assignSortGroupRef(TargetEntry *tle, List *tlist)
3463 {
3464 	Index		maxRef;
3465 	ListCell   *l;
3466 
3467 	if (tle->ressortgroupref)	/* already has one? */
3468 		return tle->ressortgroupref;
3469 
3470 	/* easiest way to pick an unused refnumber: max used + 1 */
3471 	maxRef = 0;
3472 	foreach(l, tlist)
3473 	{
3474 		Index		ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3475 
3476 		if (ref > maxRef)
3477 			maxRef = ref;
3478 	}
3479 	tle->ressortgroupref = maxRef + 1;
3480 	return tle->ressortgroupref;
3481 }
3482 
3483 /*
3484  * targetIsInSortList
3485  *		Is the given target item already in the sortlist?
3486  *		If sortop is not InvalidOid, also test for a match to the sortop.
3487  *
3488  * It is not an oversight that this function ignores the nulls_first flag.
3489  * We check sortop when determining if an ORDER BY item is redundant with
3490  * earlier ORDER BY items, because it's conceivable that "ORDER BY
3491  * foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
3492  * values that < considers equal.  We need not check nulls_first
3493  * however, because a lower-order column with the same sortop but
3494  * opposite nulls direction is redundant.  Also, we can consider
3495  * ORDER BY foo ASC, foo DESC redundant, so check for a commutator match.
3496  *
3497  * Works for both ordering and grouping lists (sortop would normally be
3498  * InvalidOid when considering grouping).  Note that the main reason we need
3499  * this routine (and not just a quick test for nonzeroness of ressortgroupref)
3500  * is that a TLE might be in only one of the lists.
3501  */
3502 bool
targetIsInSortList(TargetEntry * tle,Oid sortop,List * sortList)3503 targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
3504 {
3505 	Index		ref = tle->ressortgroupref;
3506 	ListCell   *l;
3507 
3508 	/* no need to scan list if tle has no marker */
3509 	if (ref == 0)
3510 		return false;
3511 
3512 	foreach(l, sortList)
3513 	{
3514 		SortGroupClause *scl = (SortGroupClause *) lfirst(l);
3515 
3516 		if (scl->tleSortGroupRef == ref &&
3517 			(sortop == InvalidOid ||
3518 			 sortop == scl->sortop ||
3519 			 sortop == get_commutator(scl->sortop)))
3520 			return true;
3521 	}
3522 	return false;
3523 }
3524 
3525 /*
3526  * findWindowClause
3527  *		Find the named WindowClause in the list, or return NULL if not there
3528  */
3529 static WindowClause *
findWindowClause(List * wclist,const char * name)3530 findWindowClause(List *wclist, const char *name)
3531 {
3532 	ListCell   *l;
3533 
3534 	foreach(l, wclist)
3535 	{
3536 		WindowClause *wc = (WindowClause *) lfirst(l);
3537 
3538 		if (wc->name && strcmp(wc->name, name) == 0)
3539 			return wc;
3540 	}
3541 
3542 	return NULL;
3543 }
3544 
3545 /*
3546  * transformFrameOffset
3547  *		Process a window frame offset expression
3548  *
3549  * In RANGE mode, rangeopfamily is the sort opfamily for the input ORDER BY
3550  * column, and rangeopcintype is the input data type the sort operator is
3551  * registered with.  We expect the in_range function to be registered with
3552  * that same type.  (In binary-compatible cases, it might be different from
3553  * the input column's actual type, so we can't use that for the lookups.)
3554  * We'll return the OID of the in_range function to *inRangeFunc.
3555  */
3556 static Node *
transformFrameOffset(ParseState * pstate,int frameOptions,Oid rangeopfamily,Oid rangeopcintype,Oid * inRangeFunc,Node * clause)3557 transformFrameOffset(ParseState *pstate, int frameOptions,
3558 					 Oid rangeopfamily, Oid rangeopcintype, Oid *inRangeFunc,
3559 					 Node *clause)
3560 {
3561 	const char *constructName = NULL;
3562 	Node	   *node;
3563 
3564 	*inRangeFunc = InvalidOid;	/* default result */
3565 
3566 	/* Quick exit if no offset expression */
3567 	if (clause == NULL)
3568 		return NULL;
3569 
3570 	if (frameOptions & FRAMEOPTION_ROWS)
3571 	{
3572 		/* Transform the raw expression tree */
3573 		node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_ROWS);
3574 
3575 		/*
3576 		 * Like LIMIT clause, simply coerce to int8
3577 		 */
3578 		constructName = "ROWS";
3579 		node = coerce_to_specific_type(pstate, node, INT8OID, constructName);
3580 	}
3581 	else if (frameOptions & FRAMEOPTION_RANGE)
3582 	{
3583 		/*
3584 		 * We must look up the in_range support function that's to be used,
3585 		 * possibly choosing one of several, and coerce the "offset" value to
3586 		 * the appropriate input type.
3587 		 */
3588 		Oid			nodeType;
3589 		Oid			preferredType;
3590 		int			nfuncs = 0;
3591 		int			nmatches = 0;
3592 		Oid			selectedType = InvalidOid;
3593 		Oid			selectedFunc = InvalidOid;
3594 		CatCList   *proclist;
3595 		int			i;
3596 
3597 		/* Transform the raw expression tree */
3598 		node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_RANGE);
3599 		nodeType = exprType(node);
3600 
3601 		/*
3602 		 * If there are multiple candidates, we'll prefer the one that exactly
3603 		 * matches nodeType; or if nodeType is as yet unknown, prefer the one
3604 		 * that exactly matches the sort column type.  (The second rule is
3605 		 * like what we do for "known_type operator unknown".)
3606 		 */
3607 		preferredType = (nodeType != UNKNOWNOID) ? nodeType : rangeopcintype;
3608 
3609 		/* Find the in_range support functions applicable to this case */
3610 		proclist = SearchSysCacheList2(AMPROCNUM,
3611 									   ObjectIdGetDatum(rangeopfamily),
3612 									   ObjectIdGetDatum(rangeopcintype));
3613 		for (i = 0; i < proclist->n_members; i++)
3614 		{
3615 			HeapTuple	proctup = &proclist->members[i]->tuple;
3616 			Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
3617 
3618 			/* The search will find all support proc types; ignore others */
3619 			if (procform->amprocnum != BTINRANGE_PROC)
3620 				continue;
3621 			nfuncs++;
3622 
3623 			/* Ignore function if given value can't be coerced to that type */
3624 			if (!can_coerce_type(1, &nodeType, &procform->amprocrighttype,
3625 								 COERCION_IMPLICIT))
3626 				continue;
3627 			nmatches++;
3628 
3629 			/* Remember preferred match, or any match if didn't find that */
3630 			if (selectedType != preferredType)
3631 			{
3632 				selectedType = procform->amprocrighttype;
3633 				selectedFunc = procform->amproc;
3634 			}
3635 		}
3636 		ReleaseCatCacheList(proclist);
3637 
3638 		/*
3639 		 * Throw error if needed.  It seems worth taking the trouble to
3640 		 * distinguish "no support at all" from "you didn't match any
3641 		 * available offset type".
3642 		 */
3643 		if (nfuncs == 0)
3644 			ereport(ERROR,
3645 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3646 					 errmsg("RANGE with offset PRECEDING/FOLLOWING is not supported for column type %s",
3647 							format_type_be(rangeopcintype)),
3648 					 parser_errposition(pstate, exprLocation(node))));
3649 		if (nmatches == 0)
3650 			ereport(ERROR,
3651 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3652 					 errmsg("RANGE with offset PRECEDING/FOLLOWING is not supported for column type %s and offset type %s",
3653 							format_type_be(rangeopcintype),
3654 							format_type_be(nodeType)),
3655 					 errhint("Cast the offset value to an appropriate type."),
3656 					 parser_errposition(pstate, exprLocation(node))));
3657 		if (nmatches != 1 && selectedType != preferredType)
3658 			ereport(ERROR,
3659 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3660 					 errmsg("RANGE with offset PRECEDING/FOLLOWING has multiple interpretations for column type %s and offset type %s",
3661 							format_type_be(rangeopcintype),
3662 							format_type_be(nodeType)),
3663 					 errhint("Cast the offset value to the exact intended type."),
3664 					 parser_errposition(pstate, exprLocation(node))));
3665 
3666 		/* OK, coerce the offset to the right type */
3667 		constructName = "RANGE";
3668 		node = coerce_to_specific_type(pstate, node,
3669 									   selectedType, constructName);
3670 		*inRangeFunc = selectedFunc;
3671 	}
3672 	else if (frameOptions & FRAMEOPTION_GROUPS)
3673 	{
3674 		/* Transform the raw expression tree */
3675 		node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_GROUPS);
3676 
3677 		/*
3678 		 * Like LIMIT clause, simply coerce to int8
3679 		 */
3680 		constructName = "GROUPS";
3681 		node = coerce_to_specific_type(pstate, node, INT8OID, constructName);
3682 	}
3683 	else
3684 	{
3685 		Assert(false);
3686 		node = NULL;
3687 	}
3688 
3689 	/* Disallow variables in frame offsets */
3690 	checkExprIsVarFree(pstate, node, constructName);
3691 
3692 	return node;
3693 }
3694