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