]> granicus.if.org Git - postgresql/blob - src/backend/parser/parse_clause.c
Improve the error message given for modifying a window with frame clause.
[postgresql] / src / backend / parser / parse_clause.c
1 /*-------------------------------------------------------------------------
2  *
3  * parse_clause.c
4  *        handle clauses in parser
5  *
6  * Portions Copyright (c) 1996-2013, 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 "access/heapam.h"
19 #include "catalog/heap.h"
20 #include "catalog/pg_type.h"
21 #include "commands/defrem.h"
22 #include "nodes/makefuncs.h"
23 #include "nodes/nodeFuncs.h"
24 #include "optimizer/tlist.h"
25 #include "parser/analyze.h"
26 #include "parser/parsetree.h"
27 #include "parser/parse_clause.h"
28 #include "parser/parse_coerce.h"
29 #include "parser/parse_collate.h"
30 #include "parser/parse_expr.h"
31 #include "parser/parse_oper.h"
32 #include "parser/parse_relation.h"
33 #include "parser/parse_target.h"
34 #include "rewrite/rewriteManip.h"
35 #include "utils/guc.h"
36 #include "utils/lsyscache.h"
37 #include "utils/rel.h"
38
39
40 /* Convenience macro for the most common makeNamespaceItem() case */
41 #define makeDefaultNSItem(rte)  makeNamespaceItem(rte, true, true, false, true)
42
43 static void extractRemainingColumns(List *common_colnames,
44                                                 List *src_colnames, List *src_colvars,
45                                                 List **res_colnames, List **res_colvars);
46 static Node *transformJoinUsingClause(ParseState *pstate,
47                                                  RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
48                                                  List *leftVars, List *rightVars);
49 static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
50                                           List *namespace);
51 static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
52 static RangeTblEntry *transformCTEReference(ParseState *pstate, RangeVar *r,
53                                           CommonTableExpr *cte, Index levelsup);
54 static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
55                                                 RangeSubselect *r);
56 static RangeTblEntry *transformRangeFunction(ParseState *pstate,
57                                            RangeFunction *r);
58 static Node *transformFromClauseItem(ParseState *pstate, Node *n,
59                                                 RangeTblEntry **top_rte, int *top_rti,
60                                                 List **namespace);
61 static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
62                                    Var *l_colvar, Var *r_colvar);
63 static ParseNamespaceItem *makeNamespaceItem(RangeTblEntry *rte,
64                                   bool rel_visible, bool cols_visible,
65                                   bool lateral_only, bool lateral_ok);
66 static void setNamespaceColumnVisibility(List *namespace, bool cols_visible);
67 static void setNamespaceLateralState(List *namespace,
68                                                  bool lateral_only, bool lateral_ok);
69 static void checkExprIsVarFree(ParseState *pstate, Node *n,
70                                    const char *constructName);
71 static TargetEntry *findTargetlistEntrySQL92(ParseState *pstate, Node *node,
72                                                  List **tlist, ParseExprKind exprKind);
73 static TargetEntry *findTargetlistEntrySQL99(ParseState *pstate, Node *node,
74                                                  List **tlist, ParseExprKind exprKind);
75 static int get_matching_location(int sortgroupref,
76                                           List *sortgrouprefs, List *exprs);
77 static List *addTargetToSortList(ParseState *pstate, TargetEntry *tle,
78                                         List *sortlist, List *targetlist, SortBy *sortby,
79                                         bool resolveUnknown);
80 static List *addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
81                                          List *grouplist, List *targetlist, int location,
82                                          bool resolveUnknown);
83 static WindowClause *findWindowClause(List *wclist, const char *name);
84 static Node *transformFrameOffset(ParseState *pstate, int frameOptions,
85                                          Node *clause);
86
87
88 /*
89  * transformFromClause -
90  *        Process the FROM clause and add items to the query's range table,
91  *        joinlist, and namespace.
92  *
93  * Note: we assume that the pstate's p_rtable, p_joinlist, and p_namespace
94  * lists were initialized to NIL when the pstate was created.
95  * We will add onto any entries already present --- this is needed for rule
96  * processing, as well as for UPDATE and DELETE.
97  */
98 void
99 transformFromClause(ParseState *pstate, List *frmList)
100 {
101         ListCell   *fl;
102
103         /*
104          * The grammar will have produced a list of RangeVars, RangeSubselects,
105          * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
106          * entries to the rtable), check for duplicate refnames, and then add it
107          * to the joinlist and namespace.
108          *
109          * Note we must process the items left-to-right for proper handling of
110          * LATERAL references.
111          */
112         foreach(fl, frmList)
113         {
114                 Node       *n = lfirst(fl);
115                 RangeTblEntry *rte;
116                 int                     rtindex;
117                 List       *namespace;
118
119                 n = transformFromClauseItem(pstate, n,
120                                                                         &rte,
121                                                                         &rtindex,
122                                                                         &namespace);
123
124                 checkNameSpaceConflicts(pstate, pstate->p_namespace, namespace);
125
126                 /* Mark the new namespace items as visible only to LATERAL */
127                 setNamespaceLateralState(namespace, true, true);
128
129                 pstate->p_joinlist = lappend(pstate->p_joinlist, n);
130                 pstate->p_namespace = list_concat(pstate->p_namespace, namespace);
131         }
132
133         /*
134          * We're done parsing the FROM list, so make all namespace items
135          * unconditionally visible.  Note that this will also reset lateral_only
136          * for any namespace items that were already present when we were called;
137          * but those should have been that way already.
138          */
139         setNamespaceLateralState(pstate->p_namespace, false, true);
140 }
141
142 /*
143  * setTargetTable
144  *        Add the target relation of INSERT/UPDATE/DELETE to the range table,
145  *        and make the special links to it in the ParseState.
146  *
147  *        We also open the target relation and acquire a write lock on it.
148  *        This must be done before processing the FROM list, in case the target
149  *        is also mentioned as a source relation --- we want to be sure to grab
150  *        the write lock before any read lock.
151  *
152  *        If alsoSource is true, add the target to the query's joinlist and
153  *        namespace.  For INSERT, we don't want the target to be joined to;
154  *        it's a destination of tuples, not a source.   For UPDATE/DELETE,
155  *        we do need to scan or join the target.  (NOTE: we do not bother
156  *        to check for namespace conflict; we assume that the namespace was
157  *        initially empty in these cases.)
158  *
159  *        Finally, we mark the relation as requiring the permissions specified
160  *        by requiredPerms.
161  *
162  *        Returns the rangetable index of the target relation.
163  */
164 int
165 setTargetTable(ParseState *pstate, RangeVar *relation,
166                            bool inh, bool alsoSource, AclMode requiredPerms)
167 {
168         RangeTblEntry *rte;
169         int                     rtindex;
170
171         /* Close old target; this could only happen for multi-action rules */
172         if (pstate->p_target_relation != NULL)
173                 heap_close(pstate->p_target_relation, NoLock);
174
175         /*
176          * Open target rel and grab suitable lock (which we will hold till end of
177          * transaction).
178          *
179          * free_parsestate() will eventually do the corresponding heap_close(),
180          * but *not* release the lock.
181          */
182         pstate->p_target_relation = parserOpenTable(pstate, relation,
183                                                                                                 RowExclusiveLock);
184
185         /*
186          * Now build an RTE.
187          */
188         rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
189                                                                                 relation->alias, inh, false);
190         pstate->p_target_rangetblentry = rte;
191
192         /* assume new rte is at end */
193         rtindex = list_length(pstate->p_rtable);
194         Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
195
196         /*
197          * Override addRangeTableEntry's default ACL_SELECT permissions check, and
198          * instead mark target table as requiring exactly the specified
199          * permissions.
200          *
201          * If we find an explicit reference to the rel later during parse
202          * analysis, we will add the ACL_SELECT bit back again; see
203          * markVarForSelectPriv and its callers.
204          */
205         rte->requiredPerms = requiredPerms;
206
207         /*
208          * If UPDATE/DELETE, add table to joinlist and namespace.
209          */
210         if (alsoSource)
211                 addRTEtoQuery(pstate, rte, true, true, true);
212
213         return rtindex;
214 }
215
216 /*
217  * Simplify InhOption (yes/no/default) into boolean yes/no.
218  *
219  * The reason we do things this way is that we don't want to examine the
220  * SQL_inheritance option flag until parse_analyze() is run.    Otherwise,
221  * we'd do the wrong thing with query strings that intermix SET commands
222  * with queries.
223  */
224 bool
225 interpretInhOption(InhOption inhOpt)
226 {
227         switch (inhOpt)
228         {
229                 case INH_NO:
230                         return false;
231                 case INH_YES:
232                         return true;
233                 case INH_DEFAULT:
234                         return SQL_inheritance;
235         }
236         elog(ERROR, "bogus InhOption value: %d", inhOpt);
237         return false;                           /* keep compiler quiet */
238 }
239
240 /*
241  * Given a relation-options list (of DefElems), return true iff the specified
242  * table/result set should be created with OIDs. This needs to be done after
243  * parsing the query string because the return value can depend upon the
244  * default_with_oids GUC var.
245  *
246  * In some situations, we want to reject an OIDS option even if it's present.
247  * That's (rather messily) handled here rather than reloptions.c, because that
248  * code explicitly punts checking for oids to here.
249  */
250 bool
251 interpretOidsOption(List *defList, bool allowOids)
252 {
253         ListCell   *cell;
254
255         /* Scan list to see if OIDS was included */
256         foreach(cell, defList)
257         {
258                 DefElem    *def = (DefElem *) lfirst(cell);
259
260                 if (def->defnamespace == NULL &&
261                         pg_strcasecmp(def->defname, "oids") == 0)
262                 {
263                         if (!allowOids)
264                                 ereport(ERROR,
265                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
266                                                  errmsg("unrecognized parameter \"%s\"",
267                                                                 def->defname)));
268                         return defGetBoolean(def);
269                 }
270         }
271
272         /* Force no-OIDS result if caller disallows OIDS. */
273         if (!allowOids)
274                 return false;
275
276         /* OIDS option was not specified, so use default. */
277         return default_with_oids;
278 }
279
280 /*
281  * Extract all not-in-common columns from column lists of a source table
282  */
283 static void
284 extractRemainingColumns(List *common_colnames,
285                                                 List *src_colnames, List *src_colvars,
286                                                 List **res_colnames, List **res_colvars)
287 {
288         List       *new_colnames = NIL;
289         List       *new_colvars = NIL;
290         ListCell   *lnames,
291                            *lvars;
292
293         Assert(list_length(src_colnames) == list_length(src_colvars));
294
295         forboth(lnames, src_colnames, lvars, src_colvars)
296         {
297                 char       *colname = strVal(lfirst(lnames));
298                 bool            match = false;
299                 ListCell   *cnames;
300
301                 foreach(cnames, common_colnames)
302                 {
303                         char       *ccolname = strVal(lfirst(cnames));
304
305                         if (strcmp(colname, ccolname) == 0)
306                         {
307                                 match = true;
308                                 break;
309                         }
310                 }
311
312                 if (!match)
313                 {
314                         new_colnames = lappend(new_colnames, lfirst(lnames));
315                         new_colvars = lappend(new_colvars, lfirst(lvars));
316                 }
317         }
318
319         *res_colnames = new_colnames;
320         *res_colvars = new_colvars;
321 }
322
323 /* transformJoinUsingClause()
324  *        Build a complete ON clause from a partially-transformed USING list.
325  *        We are given lists of nodes representing left and right match columns.
326  *        Result is a transformed qualification expression.
327  */
328 static Node *
329 transformJoinUsingClause(ParseState *pstate,
330                                                  RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
331                                                  List *leftVars, List *rightVars)
332 {
333         Node       *result = NULL;
334         ListCell   *lvars,
335                            *rvars;
336
337         /*
338          * We cheat a little bit here by building an untransformed operator tree
339          * whose leaves are the already-transformed Vars.  This is OK because
340          * transformExpr() won't complain about already-transformed subnodes.
341          * However, this does mean that we have to mark the columns as requiring
342          * SELECT privilege for ourselves; transformExpr() won't do it.
343          */
344         forboth(lvars, leftVars, rvars, rightVars)
345         {
346                 Var                *lvar = (Var *) lfirst(lvars);
347                 Var                *rvar = (Var *) lfirst(rvars);
348                 A_Expr     *e;
349
350                 /* Require read access to the join variables */
351                 markVarForSelectPriv(pstate, lvar, leftRTE);
352                 markVarForSelectPriv(pstate, rvar, rightRTE);
353
354                 /* Now create the lvar = rvar join condition */
355                 e = makeSimpleA_Expr(AEXPR_OP, "=",
356                                                          copyObject(lvar), copyObject(rvar),
357                                                          -1);
358
359                 /* And combine into an AND clause, if multiple join columns */
360                 if (result == NULL)
361                         result = (Node *) e;
362                 else
363                 {
364                         A_Expr     *a;
365
366                         a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e, -1);
367                         result = (Node *) a;
368                 }
369         }
370
371         /*
372          * Since the references are already Vars, and are certainly from the input
373          * relations, we don't have to go through the same pushups that
374          * transformJoinOnClause() does.  Just invoke transformExpr() to fix up
375          * the operators, and we're done.
376          */
377         result = transformExpr(pstate, result, EXPR_KIND_JOIN_USING);
378
379         result = coerce_to_boolean(pstate, result, "JOIN/USING");
380
381         return result;
382 }
383
384 /* transformJoinOnClause()
385  *        Transform the qual conditions for JOIN/ON.
386  *        Result is a transformed qualification expression.
387  */
388 static Node *
389 transformJoinOnClause(ParseState *pstate, JoinExpr *j, List *namespace)
390 {
391         Node       *result;
392         List       *save_namespace;
393
394         /*
395          * The namespace that the join expression should see is just the two
396          * subtrees of the JOIN plus any outer references from upper pstate
397          * levels.      Temporarily set this pstate's namespace accordingly.  (We need
398          * not check for refname conflicts, because transformFromClauseItem()
399          * already did.)  All namespace items are marked visible regardless of
400          * LATERAL state.
401          */
402         setNamespaceLateralState(namespace, false, true);
403
404         save_namespace = pstate->p_namespace;
405         pstate->p_namespace = namespace;
406
407         result = transformWhereClause(pstate, j->quals,
408                                                                   EXPR_KIND_JOIN_ON, "JOIN/ON");
409
410         pstate->p_namespace = save_namespace;
411
412         return result;
413 }
414
415 /*
416  * transformTableEntry --- transform a RangeVar (simple relation reference)
417  */
418 static RangeTblEntry *
419 transformTableEntry(ParseState *pstate, RangeVar *r)
420 {
421         RangeTblEntry *rte;
422
423         /* We need only build a range table entry */
424         rte = addRangeTableEntry(pstate, r, r->alias,
425                                                          interpretInhOption(r->inhOpt), true);
426
427         return rte;
428 }
429
430 /*
431  * transformCTEReference --- transform a RangeVar that references a common
432  * table expression (ie, a sub-SELECT defined in a WITH clause)
433  */
434 static RangeTblEntry *
435 transformCTEReference(ParseState *pstate, RangeVar *r,
436                                           CommonTableExpr *cte, Index levelsup)
437 {
438         RangeTblEntry *rte;
439
440         rte = addRangeTableEntryForCTE(pstate, cte, levelsup, r, true);
441
442         return rte;
443 }
444
445 /*
446  * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
447  */
448 static RangeTblEntry *
449 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
450 {
451         Query      *query;
452         RangeTblEntry *rte;
453
454         /*
455          * We require user to supply an alias for a subselect, per SQL92. To relax
456          * this, we'd have to be prepared to gin up a unique alias for an
457          * unlabeled subselect.  (This is just elog, not ereport, because the
458          * grammar should have enforced it already.  It'd probably be better to
459          * report the error here, but we don't have a good error location here.)
460          */
461         if (r->alias == NULL)
462                 elog(ERROR, "subquery in FROM must have an alias");
463
464         /*
465          * Set p_expr_kind to show this parse level is recursing to a subselect.
466          * We can't be nested within any expression, so don't need save-restore
467          * logic here.
468          */
469         Assert(pstate->p_expr_kind == EXPR_KIND_NONE);
470         pstate->p_expr_kind = EXPR_KIND_FROM_SUBSELECT;
471
472         /*
473          * If the subselect is LATERAL, make lateral_only names of this level
474          * visible to it.  (LATERAL can't nest within a single pstate level, so we
475          * don't need save/restore logic here.)
476          */
477         Assert(!pstate->p_lateral_active);
478         pstate->p_lateral_active = r->lateral;
479
480         /*
481          * Analyze and transform the subquery.
482          */
483         query = parse_sub_analyze(r->subquery, pstate, NULL,
484                                                           isLockedRefname(pstate, r->alias->aliasname));
485
486         /* Restore state */
487         pstate->p_lateral_active = false;
488         pstate->p_expr_kind = EXPR_KIND_NONE;
489
490         /*
491          * Check that we got something reasonable.      Many of these conditions are
492          * impossible given restrictions of the grammar, but check 'em anyway.
493          */
494         if (!IsA(query, Query) ||
495                 query->commandType != CMD_SELECT ||
496                 query->utilityStmt != NULL)
497                 elog(ERROR, "unexpected non-SELECT command in subquery in FROM");
498
499         /*
500          * OK, build an RTE for the subquery.
501          */
502         rte = addRangeTableEntryForSubquery(pstate,
503                                                                                 query,
504                                                                                 r->alias,
505                                                                                 r->lateral,
506                                                                                 true);
507
508         return rte;
509 }
510
511
512 /*
513  * transformRangeFunction --- transform a function call appearing in FROM
514  */
515 static RangeTblEntry *
516 transformRangeFunction(ParseState *pstate, RangeFunction *r)
517 {
518         Node       *funcexpr;
519         char       *funcname;
520         bool            is_lateral;
521         RangeTblEntry *rte;
522
523         /*
524          * Get function name for possible use as alias.  We use the same
525          * transformation rules as for a SELECT output expression.      For a FuncCall
526          * node, the result will be the function name, but it is possible for the
527          * grammar to hand back other node types.
528          */
529         funcname = FigureColname(r->funccallnode);
530
531         /*
532          * We make lateral_only names of this level visible, whether or not the
533          * function is explicitly marked LATERAL.  This is needed for SQL spec
534          * compliance in the case of UNNEST(), and seems useful on convenience
535          * grounds for all functions in FROM.
536          *
537          * (LATERAL can't nest within a single pstate level, so we don't need
538          * save/restore logic here.)
539          */
540         Assert(!pstate->p_lateral_active);
541         pstate->p_lateral_active = true;
542
543         /*
544          * Transform the raw expression.
545          */
546         funcexpr = transformExpr(pstate, r->funccallnode, EXPR_KIND_FROM_FUNCTION);
547
548         pstate->p_lateral_active = false;
549
550         /*
551          * We must assign collations now so that we can fill funccolcollations.
552          */
553         assign_expr_collations(pstate, funcexpr);
554
555         /*
556          * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
557          * there are any lateral cross-references in it.
558          */
559         is_lateral = r->lateral || contain_vars_of_level(funcexpr, 0);
560
561         /*
562          * OK, build an RTE for the function.
563          */
564         rte = addRangeTableEntryForFunction(pstate, funcname, funcexpr,
565                                                                                 r, is_lateral, true);
566
567         /*
568          * If a coldeflist was supplied, ensure it defines a legal set of names
569          * (no duplicates) and datatypes (no pseudo-types, for instance).
570          * addRangeTableEntryForFunction looked up the type names but didn't check
571          * them further than that.
572          */
573         if (r->coldeflist)
574         {
575                 TupleDesc       tupdesc;
576
577                 tupdesc = BuildDescFromLists(rte->eref->colnames,
578                                                                          rte->funccoltypes,
579                                                                          rte->funccoltypmods,
580                                                                          rte->funccolcollations);
581                 CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE, false);
582         }
583
584         return rte;
585 }
586
587
588 /*
589  * transformFromClauseItem -
590  *        Transform a FROM-clause item, adding any required entries to the
591  *        range table list being built in the ParseState, and return the
592  *        transformed item ready to include in the joinlist.  Also build a
593  *        ParseNamespaceItem list describing the names exposed by this item.
594  *        This routine can recurse to handle SQL92 JOIN expressions.
595  *
596  * The function return value is the node to add to the jointree (a
597  * RangeTblRef or JoinExpr).  Additional output parameters are:
598  *
599  * *top_rte: receives the RTE corresponding to the jointree item.
600  * (We could extract this from the function return node, but it saves cycles
601  * to pass it back separately.)
602  *
603  * *top_rti: receives the rangetable index of top_rte.  (Ditto.)
604  *
605  * *namespace: receives a List of ParseNamespaceItems for the RTEs exposed
606  * as table/column names by this item.  (The lateral_only flags in these items
607  * are indeterminate and should be explicitly set by the caller before use.)
608  */
609 static Node *
610 transformFromClauseItem(ParseState *pstate, Node *n,
611                                                 RangeTblEntry **top_rte, int *top_rti,
612                                                 List **namespace)
613 {
614         if (IsA(n, RangeVar))
615         {
616                 /* Plain relation reference, or perhaps a CTE reference */
617                 RangeVar   *rv = (RangeVar *) n;
618                 RangeTblRef *rtr;
619                 RangeTblEntry *rte = NULL;
620                 int                     rtindex;
621
622                 /* if it is an unqualified name, it might be a CTE reference */
623                 if (!rv->schemaname)
624                 {
625                         CommonTableExpr *cte;
626                         Index           levelsup;
627
628                         cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
629                         if (cte)
630                                 rte = transformCTEReference(pstate, rv, cte, levelsup);
631                 }
632
633                 /* if not found as a CTE, must be a table reference */
634                 if (!rte)
635                         rte = transformTableEntry(pstate, rv);
636
637                 /* assume new rte is at end */
638                 rtindex = list_length(pstate->p_rtable);
639                 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
640                 *top_rte = rte;
641                 *top_rti = rtindex;
642                 *namespace = list_make1(makeDefaultNSItem(rte));
643                 rtr = makeNode(RangeTblRef);
644                 rtr->rtindex = rtindex;
645                 return (Node *) rtr;
646         }
647         else if (IsA(n, RangeSubselect))
648         {
649                 /* sub-SELECT is like a plain relation */
650                 RangeTblRef *rtr;
651                 RangeTblEntry *rte;
652                 int                     rtindex;
653
654                 rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
655                 /* assume new rte is at end */
656                 rtindex = list_length(pstate->p_rtable);
657                 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
658                 *top_rte = rte;
659                 *top_rti = rtindex;
660                 *namespace = list_make1(makeDefaultNSItem(rte));
661                 rtr = makeNode(RangeTblRef);
662                 rtr->rtindex = rtindex;
663                 return (Node *) rtr;
664         }
665         else if (IsA(n, RangeFunction))
666         {
667                 /* function is like a plain relation */
668                 RangeTblRef *rtr;
669                 RangeTblEntry *rte;
670                 int                     rtindex;
671
672                 rte = transformRangeFunction(pstate, (RangeFunction *) n);
673                 /* assume new rte is at end */
674                 rtindex = list_length(pstate->p_rtable);
675                 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
676                 *top_rte = rte;
677                 *top_rti = rtindex;
678                 *namespace = list_make1(makeDefaultNSItem(rte));
679                 rtr = makeNode(RangeTblRef);
680                 rtr->rtindex = rtindex;
681                 return (Node *) rtr;
682         }
683         else if (IsA(n, JoinExpr))
684         {
685                 /* A newfangled join expression */
686                 JoinExpr   *j = (JoinExpr *) n;
687                 RangeTblEntry *l_rte;
688                 RangeTblEntry *r_rte;
689                 int                     l_rtindex;
690                 int                     r_rtindex;
691                 List       *l_namespace,
692                                    *r_namespace,
693                                    *my_namespace,
694                                    *l_colnames,
695                                    *r_colnames,
696                                    *res_colnames,
697                                    *l_colvars,
698                                    *r_colvars,
699                                    *res_colvars;
700                 bool            lateral_ok;
701                 int                     sv_namespace_length;
702                 RangeTblEntry *rte;
703                 int                     k;
704
705                 /*
706                  * Recursively process the left subtree, then the right.  We must do
707                  * it in this order for correct visibility of LATERAL references.
708                  */
709                 j->larg = transformFromClauseItem(pstate, j->larg,
710                                                                                   &l_rte,
711                                                                                   &l_rtindex,
712                                                                                   &l_namespace);
713
714                 /*
715                  * Make the left-side RTEs available for LATERAL access within the
716                  * right side, by temporarily adding them to the pstate's namespace
717                  * list.  Per SQL:2008, if the join type is not INNER or LEFT then the
718                  * left-side names must still be exposed, but it's an error to
719                  * reference them.      (Stupid design, but that's what it says.)  Hence,
720                  * we always push them into the namespace, but mark them as not
721                  * lateral_ok if the jointype is wrong.
722                  *
723                  * NB: this coding relies on the fact that list_concat is not
724                  * destructive to its second argument.
725                  */
726                 lateral_ok = (j->jointype == JOIN_INNER || j->jointype == JOIN_LEFT);
727                 setNamespaceLateralState(l_namespace, true, lateral_ok);
728
729                 checkNameSpaceConflicts(pstate, pstate->p_namespace, l_namespace);
730
731                 sv_namespace_length = list_length(pstate->p_namespace);
732                 pstate->p_namespace = list_concat(pstate->p_namespace, l_namespace);
733
734                 /* And now we can process the RHS */
735                 j->rarg = transformFromClauseItem(pstate, j->rarg,
736                                                                                   &r_rte,
737                                                                                   &r_rtindex,
738                                                                                   &r_namespace);
739
740                 /* Remove the left-side RTEs from the namespace list again */
741                 pstate->p_namespace = list_truncate(pstate->p_namespace,
742                                                                                         sv_namespace_length);
743
744                 /*
745                  * Check for conflicting refnames in left and right subtrees. Must do
746                  * this because higher levels will assume I hand back a self-
747                  * consistent namespace list.
748                  */
749                 checkNameSpaceConflicts(pstate, l_namespace, r_namespace);
750
751                 /*
752                  * Generate combined namespace info for possible use below.
753                  */
754                 my_namespace = list_concat(l_namespace, r_namespace);
755
756                 /*
757                  * Extract column name and var lists from both subtrees
758                  *
759                  * Note: expandRTE returns new lists, safe for me to modify
760                  */
761                 expandRTE(l_rte, l_rtindex, 0, -1, false,
762                                   &l_colnames, &l_colvars);
763                 expandRTE(r_rte, r_rtindex, 0, -1, false,
764                                   &r_colnames, &r_colvars);
765
766                 /*
767                  * Natural join does not explicitly specify columns; must generate
768                  * columns to join. Need to run through the list of columns from each
769                  * table or join result and match up the column names. Use the first
770                  * table, and check every column in the second table for a match.
771                  * (We'll check that the matches were unique later on.) The result of
772                  * this step is a list of column names just like an explicitly-written
773                  * USING list.
774                  */
775                 if (j->isNatural)
776                 {
777                         List       *rlist = NIL;
778                         ListCell   *lx,
779                                            *rx;
780
781                         Assert(j->usingClause == NIL);          /* shouldn't have USING() too */
782
783                         foreach(lx, l_colnames)
784                         {
785                                 char       *l_colname = strVal(lfirst(lx));
786                                 Value      *m_name = NULL;
787
788                                 foreach(rx, r_colnames)
789                                 {
790                                         char       *r_colname = strVal(lfirst(rx));
791
792                                         if (strcmp(l_colname, r_colname) == 0)
793                                         {
794                                                 m_name = makeString(l_colname);
795                                                 break;
796                                         }
797                                 }
798
799                                 /* matched a right column? then keep as join column... */
800                                 if (m_name != NULL)
801                                         rlist = lappend(rlist, m_name);
802                         }
803
804                         j->usingClause = rlist;
805                 }
806
807                 /*
808                  * Now transform the join qualifications, if any.
809                  */
810                 res_colnames = NIL;
811                 res_colvars = NIL;
812
813                 if (j->usingClause)
814                 {
815                         /*
816                          * JOIN/USING (or NATURAL JOIN, as transformed above). Transform
817                          * the list into an explicit ON-condition, and generate a list of
818                          * merged result columns.
819                          */
820                         List       *ucols = j->usingClause;
821                         List       *l_usingvars = NIL;
822                         List       *r_usingvars = NIL;
823                         ListCell   *ucol;
824
825                         Assert(j->quals == NULL);       /* shouldn't have ON() too */
826
827                         foreach(ucol, ucols)
828                         {
829                                 char       *u_colname = strVal(lfirst(ucol));
830                                 ListCell   *col;
831                                 int                     ndx;
832                                 int                     l_index = -1;
833                                 int                     r_index = -1;
834                                 Var                *l_colvar,
835                                                    *r_colvar;
836
837                                 /* Check for USING(foo,foo) */
838                                 foreach(col, res_colnames)
839                                 {
840                                         char       *res_colname = strVal(lfirst(col));
841
842                                         if (strcmp(res_colname, u_colname) == 0)
843                                                 ereport(ERROR,
844                                                                 (errcode(ERRCODE_DUPLICATE_COLUMN),
845                                                                  errmsg("column name \"%s\" appears more than once in USING clause",
846                                                                                 u_colname)));
847                                 }
848
849                                 /* Find it in left input */
850                                 ndx = 0;
851                                 foreach(col, l_colnames)
852                                 {
853                                         char       *l_colname = strVal(lfirst(col));
854
855                                         if (strcmp(l_colname, u_colname) == 0)
856                                         {
857                                                 if (l_index >= 0)
858                                                         ereport(ERROR,
859                                                                         (errcode(ERRCODE_AMBIGUOUS_COLUMN),
860                                                                          errmsg("common column name \"%s\" appears more than once in left table",
861                                                                                         u_colname)));
862                                                 l_index = ndx;
863                                         }
864                                         ndx++;
865                                 }
866                                 if (l_index < 0)
867                                         ereport(ERROR,
868                                                         (errcode(ERRCODE_UNDEFINED_COLUMN),
869                                                          errmsg("column \"%s\" specified in USING clause does not exist in left table",
870                                                                         u_colname)));
871
872                                 /* Find it in right input */
873                                 ndx = 0;
874                                 foreach(col, r_colnames)
875                                 {
876                                         char       *r_colname = strVal(lfirst(col));
877
878                                         if (strcmp(r_colname, u_colname) == 0)
879                                         {
880                                                 if (r_index >= 0)
881                                                         ereport(ERROR,
882                                                                         (errcode(ERRCODE_AMBIGUOUS_COLUMN),
883                                                                          errmsg("common column name \"%s\" appears more than once in right table",
884                                                                                         u_colname)));
885                                                 r_index = ndx;
886                                         }
887                                         ndx++;
888                                 }
889                                 if (r_index < 0)
890                                         ereport(ERROR,
891                                                         (errcode(ERRCODE_UNDEFINED_COLUMN),
892                                                          errmsg("column \"%s\" specified in USING clause does not exist in right table",
893                                                                         u_colname)));
894
895                                 l_colvar = list_nth(l_colvars, l_index);
896                                 l_usingvars = lappend(l_usingvars, l_colvar);
897                                 r_colvar = list_nth(r_colvars, r_index);
898                                 r_usingvars = lappend(r_usingvars, r_colvar);
899
900                                 res_colnames = lappend(res_colnames, lfirst(ucol));
901                                 res_colvars = lappend(res_colvars,
902                                                                           buildMergedJoinVar(pstate,
903                                                                                                                  j->jointype,
904                                                                                                                  l_colvar,
905                                                                                                                  r_colvar));
906                         }
907
908                         j->quals = transformJoinUsingClause(pstate,
909                                                                                                 l_rte,
910                                                                                                 r_rte,
911                                                                                                 l_usingvars,
912                                                                                                 r_usingvars);
913                 }
914                 else if (j->quals)
915                 {
916                         /* User-written ON-condition; transform it */
917                         j->quals = transformJoinOnClause(pstate, j, my_namespace);
918                 }
919                 else
920                 {
921                         /* CROSS JOIN: no quals */
922                 }
923
924                 /* Add remaining columns from each side to the output columns */
925                 extractRemainingColumns(res_colnames,
926                                                                 l_colnames, l_colvars,
927                                                                 &l_colnames, &l_colvars);
928                 extractRemainingColumns(res_colnames,
929                                                                 r_colnames, r_colvars,
930                                                                 &r_colnames, &r_colvars);
931                 res_colnames = list_concat(res_colnames, l_colnames);
932                 res_colvars = list_concat(res_colvars, l_colvars);
933                 res_colnames = list_concat(res_colnames, r_colnames);
934                 res_colvars = list_concat(res_colvars, r_colvars);
935
936                 /*
937                  * Check alias (AS clause), if any.
938                  */
939                 if (j->alias)
940                 {
941                         if (j->alias->colnames != NIL)
942                         {
943                                 if (list_length(j->alias->colnames) > list_length(res_colnames))
944                                         ereport(ERROR,
945                                                         (errcode(ERRCODE_SYNTAX_ERROR),
946                                                          errmsg("column alias list for \"%s\" has too many entries",
947                                                                         j->alias->aliasname)));
948                         }
949                 }
950
951                 /*
952                  * Now build an RTE for the result of the join
953                  */
954                 rte = addRangeTableEntryForJoin(pstate,
955                                                                                 res_colnames,
956                                                                                 j->jointype,
957                                                                                 res_colvars,
958                                                                                 j->alias,
959                                                                                 true);
960
961                 /* assume new rte is at end */
962                 j->rtindex = list_length(pstate->p_rtable);
963                 Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
964
965                 *top_rte = rte;
966                 *top_rti = j->rtindex;
967
968                 /* make a matching link to the JoinExpr for later use */
969                 for (k = list_length(pstate->p_joinexprs) + 1; k < j->rtindex; k++)
970                         pstate->p_joinexprs = lappend(pstate->p_joinexprs, NULL);
971                 pstate->p_joinexprs = lappend(pstate->p_joinexprs, j);
972                 Assert(list_length(pstate->p_joinexprs) == j->rtindex);
973
974                 /*
975                  * Prepare returned namespace list.  If the JOIN has an alias then it
976                  * hides the contained RTEs completely; otherwise, the contained RTEs
977                  * are still visible as table names, but are not visible for
978                  * unqualified column-name access.
979                  *
980                  * Note: if there are nested alias-less JOINs, the lower-level ones
981                  * will remain in the list although they have neither p_rel_visible
982                  * nor p_cols_visible set.      We could delete such list items, but it's
983                  * unclear that it's worth expending cycles to do so.
984                  */
985                 if (j->alias != NULL)
986                         my_namespace = NIL;
987                 else
988                         setNamespaceColumnVisibility(my_namespace, false);
989
990                 /*
991                  * The join RTE itself is always made visible for unqualified column
992                  * names.  It's visible as a relation name only if it has an alias.
993                  */
994                 *namespace = lappend(my_namespace,
995                                                          makeNamespaceItem(rte,
996                                                                                            (j->alias != NULL),
997                                                                                            true,
998                                                                                            false,
999                                                                                            true));
1000
1001                 return (Node *) j;
1002         }
1003         else
1004                 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
1005         return NULL;                            /* can't get here, keep compiler quiet */
1006 }
1007
1008 /*
1009  * buildMergedJoinVar -
1010  *        generate a suitable replacement expression for a merged join column
1011  */
1012 static Node *
1013 buildMergedJoinVar(ParseState *pstate, JoinType jointype,
1014                                    Var *l_colvar, Var *r_colvar)
1015 {
1016         Oid                     outcoltype;
1017         int32           outcoltypmod;
1018         Node       *l_node,
1019                            *r_node,
1020                            *res_node;
1021
1022         /*
1023          * Choose output type if input types are dissimilar.
1024          */
1025         outcoltype = l_colvar->vartype;
1026         outcoltypmod = l_colvar->vartypmod;
1027         if (outcoltype != r_colvar->vartype)
1028         {
1029                 outcoltype = select_common_type(pstate,
1030                                                                                 list_make2(l_colvar, r_colvar),
1031                                                                                 "JOIN/USING",
1032                                                                                 NULL);
1033                 outcoltypmod = -1;              /* ie, unknown */
1034         }
1035         else if (outcoltypmod != r_colvar->vartypmod)
1036         {
1037                 /* same type, but not same typmod */
1038                 outcoltypmod = -1;              /* ie, unknown */
1039         }
1040
1041         /*
1042          * Insert coercion functions if needed.  Note that a difference in typmod
1043          * can only happen if input has typmod but outcoltypmod is -1. In that
1044          * case we insert a RelabelType to clearly mark that result's typmod is
1045          * not same as input.  We never need coerce_type_typmod.
1046          */
1047         if (l_colvar->vartype != outcoltype)
1048                 l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
1049                                                          outcoltype, outcoltypmod,
1050                                                          COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1051         else if (l_colvar->vartypmod != outcoltypmod)
1052                 l_node = (Node *) makeRelabelType((Expr *) l_colvar,
1053                                                                                   outcoltype, outcoltypmod,
1054                                                                                   InvalidOid,   /* fixed below */
1055                                                                                   COERCE_IMPLICIT_CAST);
1056         else
1057                 l_node = (Node *) l_colvar;
1058
1059         if (r_colvar->vartype != outcoltype)
1060                 r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
1061                                                          outcoltype, outcoltypmod,
1062                                                          COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1063         else if (r_colvar->vartypmod != outcoltypmod)
1064                 r_node = (Node *) makeRelabelType((Expr *) r_colvar,
1065                                                                                   outcoltype, outcoltypmod,
1066                                                                                   InvalidOid,   /* fixed below */
1067                                                                                   COERCE_IMPLICIT_CAST);
1068         else
1069                 r_node = (Node *) r_colvar;
1070
1071         /*
1072          * Choose what to emit
1073          */
1074         switch (jointype)
1075         {
1076                 case JOIN_INNER:
1077
1078                         /*
1079                          * We can use either var; prefer non-coerced one if available.
1080                          */
1081                         if (IsA(l_node, Var))
1082                                 res_node = l_node;
1083                         else if (IsA(r_node, Var))
1084                                 res_node = r_node;
1085                         else
1086                                 res_node = l_node;
1087                         break;
1088                 case JOIN_LEFT:
1089                         /* Always use left var */
1090                         res_node = l_node;
1091                         break;
1092                 case JOIN_RIGHT:
1093                         /* Always use right var */
1094                         res_node = r_node;
1095                         break;
1096                 case JOIN_FULL:
1097                         {
1098                                 /*
1099                                  * Here we must build a COALESCE expression to ensure that the
1100                                  * join output is non-null if either input is.
1101                                  */
1102                                 CoalesceExpr *c = makeNode(CoalesceExpr);
1103
1104                                 c->coalescetype = outcoltype;
1105                                 /* coalescecollid will get set below */
1106                                 c->args = list_make2(l_node, r_node);
1107                                 c->location = -1;
1108                                 res_node = (Node *) c;
1109                                 break;
1110                         }
1111                 default:
1112                         elog(ERROR, "unrecognized join type: %d", (int) jointype);
1113                         res_node = NULL;        /* keep compiler quiet */
1114                         break;
1115         }
1116
1117         /*
1118          * Apply assign_expr_collations to fix up the collation info in the
1119          * coercion and CoalesceExpr nodes, if we made any.  This must be done now
1120          * so that the join node's alias vars show correct collation info.
1121          */
1122         assign_expr_collations(pstate, res_node);
1123
1124         return res_node;
1125 }
1126
1127 /*
1128  * makeNamespaceItem -
1129  *        Convenience subroutine to construct a ParseNamespaceItem.
1130  */
1131 static ParseNamespaceItem *
1132 makeNamespaceItem(RangeTblEntry *rte, bool rel_visible, bool cols_visible,
1133                                   bool lateral_only, bool lateral_ok)
1134 {
1135         ParseNamespaceItem *nsitem;
1136
1137         nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1138         nsitem->p_rte = rte;
1139         nsitem->p_rel_visible = rel_visible;
1140         nsitem->p_cols_visible = cols_visible;
1141         nsitem->p_lateral_only = lateral_only;
1142         nsitem->p_lateral_ok = lateral_ok;
1143         return nsitem;
1144 }
1145
1146 /*
1147  * setNamespaceColumnVisibility -
1148  *        Convenience subroutine to update cols_visible flags in a namespace list.
1149  */
1150 static void
1151 setNamespaceColumnVisibility(List *namespace, bool cols_visible)
1152 {
1153         ListCell   *lc;
1154
1155         foreach(lc, namespace)
1156         {
1157                 ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1158
1159                 nsitem->p_cols_visible = cols_visible;
1160         }
1161 }
1162
1163 /*
1164  * setNamespaceLateralState -
1165  *        Convenience subroutine to update LATERAL flags in a namespace list.
1166  */
1167 static void
1168 setNamespaceLateralState(List *namespace, bool lateral_only, bool lateral_ok)
1169 {
1170         ListCell   *lc;
1171
1172         foreach(lc, namespace)
1173         {
1174                 ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1175
1176                 nsitem->p_lateral_only = lateral_only;
1177                 nsitem->p_lateral_ok = lateral_ok;
1178         }
1179 }
1180
1181
1182 /*
1183  * transformWhereClause -
1184  *        Transform the qualification and make sure it is of type boolean.
1185  *        Used for WHERE and allied clauses.
1186  *
1187  * constructName does not affect the semantics, but is used in error messages
1188  */
1189 Node *
1190 transformWhereClause(ParseState *pstate, Node *clause,
1191                                          ParseExprKind exprKind, const char *constructName)
1192 {
1193         Node       *qual;
1194
1195         if (clause == NULL)
1196                 return NULL;
1197
1198         qual = transformExpr(pstate, clause, exprKind);
1199
1200         qual = coerce_to_boolean(pstate, qual, constructName);
1201
1202         return qual;
1203 }
1204
1205
1206 /*
1207  * transformLimitClause -
1208  *        Transform the expression and make sure it is of type bigint.
1209  *        Used for LIMIT and allied clauses.
1210  *
1211  * Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
1212  * rather than int4 as before.
1213  *
1214  * constructName does not affect the semantics, but is used in error messages
1215  */
1216 Node *
1217 transformLimitClause(ParseState *pstate, Node *clause,
1218                                          ParseExprKind exprKind, const char *constructName)
1219 {
1220         Node       *qual;
1221
1222         if (clause == NULL)
1223                 return NULL;
1224
1225         qual = transformExpr(pstate, clause, exprKind);
1226
1227         qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1228
1229         /* LIMIT can't refer to any variables of the current query */
1230         checkExprIsVarFree(pstate, qual, constructName);
1231
1232         return qual;
1233 }
1234
1235 /*
1236  * checkExprIsVarFree
1237  *              Check that given expr has no Vars of the current query level
1238  *              (aggregates and window functions should have been rejected already).
1239  *
1240  * This is used to check expressions that have to have a consistent value
1241  * across all rows of the query, such as a LIMIT.  Arguably it should reject
1242  * volatile functions, too, but we don't do that --- whatever value the
1243  * function gives on first execution is what you get.
1244  *
1245  * constructName does not affect the semantics, but is used in error messages
1246  */
1247 static void
1248 checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
1249 {
1250         if (contain_vars_of_level(n, 0))
1251         {
1252                 ereport(ERROR,
1253                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1254                 /* translator: %s is name of a SQL construct, eg LIMIT */
1255                                  errmsg("argument of %s must not contain variables",
1256                                                 constructName),
1257                                  parser_errposition(pstate,
1258                                                                         locate_var_of_level(n, 0))));
1259         }
1260 }
1261
1262
1263 /*
1264  * checkTargetlistEntrySQL92 -
1265  *        Validate a targetlist entry found by findTargetlistEntrySQL92
1266  *
1267  * When we select a pre-existing tlist entry as a result of syntax such
1268  * as "GROUP BY 1", we have to make sure it is acceptable for use in the
1269  * indicated clause type; transformExpr() will have treated it as a regular
1270  * targetlist item.
1271  */
1272 static void
1273 checkTargetlistEntrySQL92(ParseState *pstate, TargetEntry *tle,
1274                                                   ParseExprKind exprKind)
1275 {
1276         switch (exprKind)
1277         {
1278                 case EXPR_KIND_GROUP_BY:
1279                         /* reject aggregates and window functions */
1280                         if (pstate->p_hasAggs &&
1281                                 contain_aggs_of_level((Node *) tle->expr, 0))
1282                                 ereport(ERROR,
1283                                                 (errcode(ERRCODE_GROUPING_ERROR),
1284                                 /* translator: %s is name of a SQL construct, eg GROUP BY */
1285                                                  errmsg("aggregate functions are not allowed in %s",
1286                                                                 ParseExprKindName(exprKind)),
1287                                                  parser_errposition(pstate,
1288                                                            locate_agg_of_level((Node *) tle->expr, 0))));
1289                         if (pstate->p_hasWindowFuncs &&
1290                                 contain_windowfuncs((Node *) tle->expr))
1291                                 ereport(ERROR,
1292                                                 (errcode(ERRCODE_WINDOWING_ERROR),
1293                                 /* translator: %s is name of a SQL construct, eg GROUP BY */
1294                                                  errmsg("window functions are not allowed in %s",
1295                                                                 ParseExprKindName(exprKind)),
1296                                                  parser_errposition(pstate,
1297                                                                         locate_windowfunc((Node *) tle->expr))));
1298                         break;
1299                 case EXPR_KIND_ORDER_BY:
1300                         /* no extra checks needed */
1301                         break;
1302                 case EXPR_KIND_DISTINCT_ON:
1303                         /* no extra checks needed */
1304                         break;
1305                 default:
1306                         elog(ERROR, "unexpected exprKind in checkTargetlistEntrySQL92");
1307                         break;
1308         }
1309 }
1310
1311 /*
1312  *      findTargetlistEntrySQL92 -
1313  *        Returns the targetlist entry matching the given (untransformed) node.
1314  *        If no matching entry exists, one is created and appended to the target
1315  *        list as a "resjunk" node.
1316  *
1317  * This function supports the old SQL92 ORDER BY interpretation, where the
1318  * expression is an output column name or number.  If we fail to find a
1319  * match of that sort, we fall through to the SQL99 rules.      For historical
1320  * reasons, Postgres also allows this interpretation for GROUP BY, though
1321  * the standard never did.      However, for GROUP BY we prefer a SQL99 match.
1322  * This function is *not* used for WINDOW definitions.
1323  *
1324  * node         the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
1325  * tlist        the target list (passed by reference so we can append to it)
1326  * exprKind identifies clause type being processed
1327  */
1328 static TargetEntry *
1329 findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist,
1330                                                  ParseExprKind exprKind)
1331 {
1332         ListCell   *tl;
1333
1334         /*----------
1335          * Handle two special cases as mandated by the SQL92 spec:
1336          *
1337          * 1. Bare ColumnName (no qualifier or subscripts)
1338          *        For a bare identifier, we search for a matching column name
1339          *        in the existing target list.  Multiple matches are an error
1340          *        unless they refer to identical values; for example,
1341          *        we allow      SELECT a, a FROM table ORDER BY a
1342          *        but not       SELECT a AS b, b FROM table ORDER BY b
1343          *        If no match is found, we fall through and treat the identifier
1344          *        as an expression.
1345          *        For GROUP BY, it is incorrect to match the grouping item against
1346          *        targetlist entries: according to SQL92, an identifier in GROUP BY
1347          *        is a reference to a column name exposed by FROM, not to a target
1348          *        list column.  However, many implementations (including pre-7.0
1349          *        PostgreSQL) accept this anyway.  So for GROUP BY, we look first
1350          *        to see if the identifier matches any FROM column name, and only
1351          *        try for a targetlist name if it doesn't.  This ensures that we
1352          *        adhere to the spec in the case where the name could be both.
1353          *        DISTINCT ON isn't in the standard, so we can do what we like there;
1354          *        we choose to make it work like ORDER BY, on the rather flimsy
1355          *        grounds that ordinary DISTINCT works on targetlist entries.
1356          *
1357          * 2. IntegerConstant
1358          *        This means to use the n'th item in the existing target list.
1359          *        Note that it would make no sense to order/group/distinct by an
1360          *        actual constant, so this does not create a conflict with SQL99.
1361          *        GROUP BY column-number is not allowed by SQL92, but since
1362          *        the standard has no other behavior defined for this syntax,
1363          *        we may as well accept this common extension.
1364          *
1365          * Note that pre-existing resjunk targets must not be used in either case,
1366          * since the user didn't write them in his SELECT list.
1367          *
1368          * If neither special case applies, fall through to treat the item as
1369          * an expression per SQL99.
1370          *----------
1371          */
1372         if (IsA(node, ColumnRef) &&
1373                 list_length(((ColumnRef *) node)->fields) == 1 &&
1374                 IsA(linitial(((ColumnRef *) node)->fields), String))
1375         {
1376                 char       *name = strVal(linitial(((ColumnRef *) node)->fields));
1377                 int                     location = ((ColumnRef *) node)->location;
1378
1379                 if (exprKind == EXPR_KIND_GROUP_BY)
1380                 {
1381                         /*
1382                          * In GROUP BY, we must prefer a match against a FROM-clause
1383                          * column to one against the targetlist.  Look to see if there is
1384                          * a matching column.  If so, fall through to use SQL99 rules.
1385                          * NOTE: if name could refer ambiguously to more than one column
1386                          * name exposed by FROM, colNameToVar will ereport(ERROR). That's
1387                          * just what we want here.
1388                          *
1389                          * Small tweak for 7.4.3: ignore matches in upper query levels.
1390                          * This effectively changes the search order for bare names to (1)
1391                          * local FROM variables, (2) local targetlist aliases, (3) outer
1392                          * FROM variables, whereas before it was (1) (3) (2). SQL92 and
1393                          * SQL99 do not allow GROUPing BY an outer reference, so this
1394                          * breaks no cases that are legal per spec, and it seems a more
1395                          * self-consistent behavior.
1396                          */
1397                         if (colNameToVar(pstate, name, true, location) != NULL)
1398                                 name = NULL;
1399                 }
1400
1401                 if (name != NULL)
1402                 {
1403                         TargetEntry *target_result = NULL;
1404
1405                         foreach(tl, *tlist)
1406                         {
1407                                 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1408
1409                                 if (!tle->resjunk &&
1410                                         strcmp(tle->resname, name) == 0)
1411                                 {
1412                                         if (target_result != NULL)
1413                                         {
1414                                                 if (!equal(target_result->expr, tle->expr))
1415                                                         ereport(ERROR,
1416                                                                         (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1417
1418                                                         /*------
1419                                                           translator: first %s is name of a SQL construct, eg ORDER BY */
1420                                                                          errmsg("%s \"%s\" is ambiguous",
1421                                                                                         ParseExprKindName(exprKind),
1422                                                                                         name),
1423                                                                          parser_errposition(pstate, location)));
1424                                         }
1425                                         else
1426                                                 target_result = tle;
1427                                         /* Stay in loop to check for ambiguity */
1428                                 }
1429                         }
1430                         if (target_result != NULL)
1431                         {
1432                                 /* return the first match, after suitable validation */
1433                                 checkTargetlistEntrySQL92(pstate, target_result, exprKind);
1434                                 return target_result;
1435                         }
1436                 }
1437         }
1438         if (IsA(node, A_Const))
1439         {
1440                 Value      *val = &((A_Const *) node)->val;
1441                 int                     location = ((A_Const *) node)->location;
1442                 int                     targetlist_pos = 0;
1443                 int                     target_pos;
1444
1445                 if (!IsA(val, Integer))
1446                         ereport(ERROR,
1447                                         (errcode(ERRCODE_SYNTAX_ERROR),
1448                         /* translator: %s is name of a SQL construct, eg ORDER BY */
1449                                          errmsg("non-integer constant in %s",
1450                                                         ParseExprKindName(exprKind)),
1451                                          parser_errposition(pstate, location)));
1452
1453                 target_pos = intVal(val);
1454                 foreach(tl, *tlist)
1455                 {
1456                         TargetEntry *tle = (TargetEntry *) lfirst(tl);
1457
1458                         if (!tle->resjunk)
1459                         {
1460                                 if (++targetlist_pos == target_pos)
1461                                 {
1462                                         /* return the unique match, after suitable validation */
1463                                         checkTargetlistEntrySQL92(pstate, tle, exprKind);
1464                                         return tle;
1465                                 }
1466                         }
1467                 }
1468                 ereport(ERROR,
1469                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1470                 /* translator: %s is name of a SQL construct, eg ORDER BY */
1471                                  errmsg("%s position %d is not in select list",
1472                                                 ParseExprKindName(exprKind), target_pos),
1473                                  parser_errposition(pstate, location)));
1474         }
1475
1476         /*
1477          * Otherwise, we have an expression, so process it per SQL99 rules.
1478          */
1479         return findTargetlistEntrySQL99(pstate, node, tlist, exprKind);
1480 }
1481
1482 /*
1483  *      findTargetlistEntrySQL99 -
1484  *        Returns the targetlist entry matching the given (untransformed) node.
1485  *        If no matching entry exists, one is created and appended to the target
1486  *        list as a "resjunk" node.
1487  *
1488  * This function supports the SQL99 interpretation, wherein the expression
1489  * is just an ordinary expression referencing input column names.
1490  *
1491  * node         the ORDER BY, GROUP BY, etc expression to be matched
1492  * tlist        the target list (passed by reference so we can append to it)
1493  * exprKind identifies clause type being processed
1494  */
1495 static TargetEntry *
1496 findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist,
1497                                                  ParseExprKind exprKind)
1498 {
1499         TargetEntry *target_result;
1500         ListCell   *tl;
1501         Node       *expr;
1502
1503         /*
1504          * Convert the untransformed node to a transformed expression, and search
1505          * for a match in the tlist.  NOTE: it doesn't really matter whether there
1506          * is more than one match.      Also, we are willing to match an existing
1507          * resjunk target here, though the SQL92 cases above must ignore resjunk
1508          * targets.
1509          */
1510         expr = transformExpr(pstate, node, exprKind);
1511
1512         foreach(tl, *tlist)
1513         {
1514                 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1515                 Node       *texpr;
1516
1517                 /*
1518                  * Ignore any implicit cast on the existing tlist expression.
1519                  *
1520                  * This essentially allows the ORDER/GROUP/etc item to adopt the same
1521                  * datatype previously selected for a textually-equivalent tlist item.
1522                  * There can't be any implicit cast at top level in an ordinary SELECT
1523                  * tlist at this stage, but the case does arise with ORDER BY in an
1524                  * aggregate function.
1525                  */
1526                 texpr = strip_implicit_coercions((Node *) tle->expr);
1527
1528                 if (equal(expr, texpr))
1529                         return tle;
1530         }
1531
1532         /*
1533          * If no matches, construct a new target entry which is appended to the
1534          * end of the target list.      This target is given resjunk = TRUE so that it
1535          * will not be projected into the final tuple.
1536          */
1537         target_result = transformTargetEntry(pstate, node, expr, exprKind,
1538                                                                                  NULL, true);
1539
1540         *tlist = lappend(*tlist, target_result);
1541
1542         return target_result;
1543 }
1544
1545 /*
1546  * transformGroupClause -
1547  *        transform a GROUP BY clause
1548  *
1549  * GROUP BY items will be added to the targetlist (as resjunk columns)
1550  * if not already present, so the targetlist must be passed by reference.
1551  *
1552  * This is also used for window PARTITION BY clauses (which act almost the
1553  * same, but are always interpreted per SQL99 rules).
1554  */
1555 List *
1556 transformGroupClause(ParseState *pstate, List *grouplist,
1557                                          List **targetlist, List *sortClause,
1558                                          ParseExprKind exprKind, bool useSQL99)
1559 {
1560         List       *result = NIL;
1561         ListCell   *gl;
1562
1563         foreach(gl, grouplist)
1564         {
1565                 Node       *gexpr = (Node *) lfirst(gl);
1566                 TargetEntry *tle;
1567                 bool            found = false;
1568
1569                 if (useSQL99)
1570                         tle = findTargetlistEntrySQL99(pstate, gexpr,
1571                                                                                    targetlist, exprKind);
1572                 else
1573                         tle = findTargetlistEntrySQL92(pstate, gexpr,
1574                                                                                    targetlist, exprKind);
1575
1576                 /* Eliminate duplicates (GROUP BY x, x) */
1577                 if (targetIsInSortList(tle, InvalidOid, result))
1578                         continue;
1579
1580                 /*
1581                  * If the GROUP BY tlist entry also appears in ORDER BY, copy operator
1582                  * info from the (first) matching ORDER BY item.  This means that if
1583                  * you write something like "GROUP BY foo ORDER BY foo USING <<<", the
1584                  * GROUP BY operation silently takes on the equality semantics implied
1585                  * by the ORDER BY.  There are two reasons to do this: it improves the
1586                  * odds that we can implement both GROUP BY and ORDER BY with a single
1587                  * sort step, and it allows the user to choose the equality semantics
1588                  * used by GROUP BY, should she be working with a datatype that has
1589                  * more than one equality operator.
1590                  */
1591                 if (tle->ressortgroupref > 0)
1592                 {
1593                         ListCell   *sl;
1594
1595                         foreach(sl, sortClause)
1596                         {
1597                                 SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
1598
1599                                 if (sc->tleSortGroupRef == tle->ressortgroupref)
1600                                 {
1601                                         result = lappend(result, copyObject(sc));
1602                                         found = true;
1603                                         break;
1604                                 }
1605                         }
1606                 }
1607
1608                 /*
1609                  * If no match in ORDER BY, just add it to the result using default
1610                  * sort/group semantics.
1611                  */
1612                 if (!found)
1613                         result = addTargetToGroupList(pstate, tle,
1614                                                                                   result, *targetlist,
1615                                                                                   exprLocation(gexpr),
1616                                                                                   true);
1617         }
1618
1619         return result;
1620 }
1621
1622 /*
1623  * transformSortClause -
1624  *        transform an ORDER BY clause
1625  *
1626  * ORDER BY items will be added to the targetlist (as resjunk columns)
1627  * if not already present, so the targetlist must be passed by reference.
1628  *
1629  * This is also used for window and aggregate ORDER BY clauses (which act
1630  * almost the same, but are always interpreted per SQL99 rules).
1631  */
1632 List *
1633 transformSortClause(ParseState *pstate,
1634                                         List *orderlist,
1635                                         List **targetlist,
1636                                         ParseExprKind exprKind,
1637                                         bool resolveUnknown,
1638                                         bool useSQL99)
1639 {
1640         List       *sortlist = NIL;
1641         ListCell   *olitem;
1642
1643         foreach(olitem, orderlist)
1644         {
1645                 SortBy     *sortby = (SortBy *) lfirst(olitem);
1646                 TargetEntry *tle;
1647
1648                 if (useSQL99)
1649                         tle = findTargetlistEntrySQL99(pstate, sortby->node,
1650                                                                                    targetlist, exprKind);
1651                 else
1652                         tle = findTargetlistEntrySQL92(pstate, sortby->node,
1653                                                                                    targetlist, exprKind);
1654
1655                 sortlist = addTargetToSortList(pstate, tle,
1656                                                                            sortlist, *targetlist, sortby,
1657                                                                            resolveUnknown);
1658         }
1659
1660         return sortlist;
1661 }
1662
1663 /*
1664  * transformWindowDefinitions -
1665  *              transform window definitions (WindowDef to WindowClause)
1666  */
1667 List *
1668 transformWindowDefinitions(ParseState *pstate,
1669                                                    List *windowdefs,
1670                                                    List **targetlist)
1671 {
1672         List       *result = NIL;
1673         Index           winref = 0;
1674         ListCell   *lc;
1675
1676         foreach(lc, windowdefs)
1677         {
1678                 WindowDef  *windef = (WindowDef *) lfirst(lc);
1679                 WindowClause *refwc = NULL;
1680                 List       *partitionClause;
1681                 List       *orderClause;
1682                 WindowClause *wc;
1683
1684                 winref++;
1685
1686                 /*
1687                  * Check for duplicate window names.
1688                  */
1689                 if (windef->name &&
1690                         findWindowClause(result, windef->name) != NULL)
1691                         ereport(ERROR,
1692                                         (errcode(ERRCODE_WINDOWING_ERROR),
1693                                          errmsg("window \"%s\" is already defined", windef->name),
1694                                          parser_errposition(pstate, windef->location)));
1695
1696                 /*
1697                  * If it references a previous window, look that up.
1698                  */
1699                 if (windef->refname)
1700                 {
1701                         refwc = findWindowClause(result, windef->refname);
1702                         if (refwc == NULL)
1703                                 ereport(ERROR,
1704                                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
1705                                                  errmsg("window \"%s\" does not exist",
1706                                                                 windef->refname),
1707                                                  parser_errposition(pstate, windef->location)));
1708                 }
1709
1710                 /*
1711                  * Transform PARTITION and ORDER specs, if any.  These are treated
1712                  * almost exactly like top-level GROUP BY and ORDER BY clauses,
1713                  * including the special handling of nondefault operator semantics.
1714                  */
1715                 orderClause = transformSortClause(pstate,
1716                                                                                   windef->orderClause,
1717                                                                                   targetlist,
1718                                                                                   EXPR_KIND_WINDOW_ORDER,
1719                                                                                   true /* fix unknowns */ ,
1720                                                                                   true /* force SQL99 rules */ );
1721                 partitionClause = transformGroupClause(pstate,
1722                                                                                            windef->partitionClause,
1723                                                                                            targetlist,
1724                                                                                            orderClause,
1725                                                                                            EXPR_KIND_WINDOW_PARTITION,
1726                                                                                            true /* force SQL99 rules */ );
1727
1728                 /*
1729                  * And prepare the new WindowClause.
1730                  */
1731                 wc = makeNode(WindowClause);
1732                 wc->name = windef->name;
1733                 wc->refname = windef->refname;
1734
1735                 /*
1736                  * Per spec, a windowdef that references a previous one copies the
1737                  * previous partition clause (and mustn't specify its own).  It can
1738                  * specify its own ordering clause, but only if the previous one had
1739                  * none.  It always specifies its own frame clause, and the previous
1740                  * one must not have a frame clause.  Yeah, it's bizarre that each of
1741                  * these cases works differently, but SQL:2008 says so; see 7.11
1742                  * <window clause> syntax rule 10 and general rule 1.  The frame
1743                  * clause rule is especially bizarre because it makes "OVER foo"
1744                  * different from "OVER (foo)", and requires the latter to throw an
1745                  * error if foo has a nondefault frame clause.  Well, ours not to
1746                  * reason why, but we do go out of our way to throw a useful error
1747                  * message for such cases.
1748                  */
1749                 if (refwc)
1750                 {
1751                         if (partitionClause)
1752                                 ereport(ERROR,
1753                                                 (errcode(ERRCODE_WINDOWING_ERROR),
1754                                 errmsg("cannot override PARTITION BY clause of window \"%s\"",
1755                                            windef->refname),
1756                                                  parser_errposition(pstate, windef->location)));
1757                         wc->partitionClause = copyObject(refwc->partitionClause);
1758                 }
1759                 else
1760                         wc->partitionClause = partitionClause;
1761                 if (refwc)
1762                 {
1763                         if (orderClause && refwc->orderClause)
1764                                 ereport(ERROR,
1765                                                 (errcode(ERRCODE_WINDOWING_ERROR),
1766                                    errmsg("cannot override ORDER BY clause of window \"%s\"",
1767                                                   windef->refname),
1768                                                  parser_errposition(pstate, windef->location)));
1769                         if (orderClause)
1770                         {
1771                                 wc->orderClause = orderClause;
1772                                 wc->copiedOrder = false;
1773                         }
1774                         else
1775                         {
1776                                 wc->orderClause = copyObject(refwc->orderClause);
1777                                 wc->copiedOrder = true;
1778                         }
1779                 }
1780                 else
1781                 {
1782                         wc->orderClause = orderClause;
1783                         wc->copiedOrder = false;
1784                 }
1785                 if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
1786                 {
1787                         /*
1788                          * Use this message if this is a WINDOW clause, or if it's an OVER
1789                          * clause that includes ORDER BY or framing clauses.  (We already
1790                          * rejected PARTITION BY above, so no need to check that.)
1791                          */
1792                         if (windef->name ||
1793                                 orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
1794                                 ereport(ERROR,
1795                                                 (errcode(ERRCODE_WINDOWING_ERROR),
1796                                                  errmsg("cannot copy window \"%s\" because it has a frame clause",
1797                                                                 windef->refname),
1798                                                  parser_errposition(pstate, windef->location)));
1799                         /* Else this clause is just OVER (foo), so say this: */
1800                         ereport(ERROR,
1801                                         (errcode(ERRCODE_WINDOWING_ERROR),
1802                         errmsg("cannot copy window \"%s\" because it has a frame clause",
1803                                    windef->refname),
1804                                          errhint("Omit the parentheses in this OVER clause."),
1805                                          parser_errposition(pstate, windef->location)));
1806                 }
1807                 wc->frameOptions = windef->frameOptions;
1808                 /* Process frame offset expressions */
1809                 wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
1810                                                                                            windef->startOffset);
1811                 wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
1812                                                                                          windef->endOffset);
1813                 wc->winref = winref;
1814
1815                 result = lappend(result, wc);
1816         }
1817
1818         return result;
1819 }
1820
1821 /*
1822  * transformDistinctClause -
1823  *        transform a DISTINCT clause
1824  *
1825  * Since we may need to add items to the query's targetlist, that list
1826  * is passed by reference.
1827  *
1828  * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
1829  * possible into the distinctClause.  This avoids a possible need to re-sort,
1830  * and allows the user to choose the equality semantics used by DISTINCT,
1831  * should she be working with a datatype that has more than one equality
1832  * operator.
1833  *
1834  * is_agg is true if we are transforming an aggregate(DISTINCT ...)
1835  * function call.  This does not affect any behavior, only the phrasing
1836  * of error messages.
1837  */
1838 List *
1839 transformDistinctClause(ParseState *pstate,
1840                                                 List **targetlist, List *sortClause, bool is_agg)
1841 {
1842         List       *result = NIL;
1843         ListCell   *slitem;
1844         ListCell   *tlitem;
1845
1846         /*
1847          * The distinctClause should consist of all ORDER BY items followed by all
1848          * other non-resjunk targetlist items.  There must not be any resjunk
1849          * ORDER BY items --- that would imply that we are sorting by a value that
1850          * isn't necessarily unique within a DISTINCT group, so the results
1851          * wouldn't be well-defined.  This construction ensures we follow the rule
1852          * that sortClause and distinctClause match; in fact the sortClause will
1853          * always be a prefix of distinctClause.
1854          *
1855          * Note a corner case: the same TLE could be in the ORDER BY list multiple
1856          * times with different sortops.  We have to include it in the
1857          * distinctClause the same way to preserve the prefix property. The net
1858          * effect will be that the TLE value will be made unique according to both
1859          * sortops.
1860          */
1861         foreach(slitem, sortClause)
1862         {
1863                 SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
1864                 TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
1865
1866                 if (tle->resjunk)
1867                         ereport(ERROR,
1868                                         (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1869                                          is_agg ?
1870                                          errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
1871                                          errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
1872                                          parser_errposition(pstate,
1873                                                                                 exprLocation((Node *) tle->expr))));
1874                 result = lappend(result, copyObject(scl));
1875         }
1876
1877         /*
1878          * Now add any remaining non-resjunk tlist items, using default sort/group
1879          * semantics for their data types.
1880          */
1881         foreach(tlitem, *targetlist)
1882         {
1883                 TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
1884
1885                 if (tle->resjunk)
1886                         continue;                       /* ignore junk */
1887                 result = addTargetToGroupList(pstate, tle,
1888                                                                           result, *targetlist,
1889                                                                           exprLocation((Node *) tle->expr),
1890                                                                           true);
1891         }
1892
1893         return result;
1894 }
1895
1896 /*
1897  * transformDistinctOnClause -
1898  *        transform a DISTINCT ON clause
1899  *
1900  * Since we may need to add items to the query's targetlist, that list
1901  * is passed by reference.
1902  *
1903  * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
1904  * possible into the distinctClause.  This avoids a possible need to re-sort,
1905  * and allows the user to choose the equality semantics used by DISTINCT,
1906  * should she be working with a datatype that has more than one equality
1907  * operator.
1908  */
1909 List *
1910 transformDistinctOnClause(ParseState *pstate, List *distinctlist,
1911                                                   List **targetlist, List *sortClause)
1912 {
1913         List       *result = NIL;
1914         List       *sortgrouprefs = NIL;
1915         bool            skipped_sortitem;
1916         ListCell   *lc;
1917         ListCell   *lc2;
1918
1919         /*
1920          * Add all the DISTINCT ON expressions to the tlist (if not already
1921          * present, they are added as resjunk items).  Assign sortgroupref numbers
1922          * to them, and make a list of these numbers.  (NB: we rely below on the
1923          * sortgrouprefs list being one-for-one with the original distinctlist.
1924          * Also notice that we could have duplicate DISTINCT ON expressions and
1925          * hence duplicate entries in sortgrouprefs.)
1926          */
1927         foreach(lc, distinctlist)
1928         {
1929                 Node       *dexpr = (Node *) lfirst(lc);
1930                 int                     sortgroupref;
1931                 TargetEntry *tle;
1932
1933                 tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
1934                                                                            EXPR_KIND_DISTINCT_ON);
1935                 sortgroupref = assignSortGroupRef(tle, *targetlist);
1936                 sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
1937         }
1938
1939         /*
1940          * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
1941          * semantics from ORDER BY items that match DISTINCT ON items, and also
1942          * adopt their column sort order.  We insist that the distinctClause and
1943          * sortClause match, so throw error if we find the need to add any more
1944          * distinctClause items after we've skipped an ORDER BY item that wasn't
1945          * in DISTINCT ON.
1946          */
1947         skipped_sortitem = false;
1948         foreach(lc, sortClause)
1949         {
1950                 SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
1951
1952                 if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
1953                 {
1954                         if (skipped_sortitem)
1955                                 ereport(ERROR,
1956                                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1957                                                  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
1958                                                  parser_errposition(pstate,
1959                                                                   get_matching_location(scl->tleSortGroupRef,
1960                                                                                                                 sortgrouprefs,
1961                                                                                                                 distinctlist))));
1962                         else
1963                                 result = lappend(result, copyObject(scl));
1964                 }
1965                 else
1966                         skipped_sortitem = true;
1967         }
1968
1969         /*
1970          * Now add any remaining DISTINCT ON items, using default sort/group
1971          * semantics for their data types.      (Note: this is pretty questionable; if
1972          * the ORDER BY list doesn't include all the DISTINCT ON items and more
1973          * besides, you certainly aren't using DISTINCT ON in the intended way,
1974          * and you probably aren't going to get consistent results.  It might be
1975          * better to throw an error or warning here.  But historically we've
1976          * allowed it, so keep doing so.)
1977          */
1978         forboth(lc, distinctlist, lc2, sortgrouprefs)
1979         {
1980                 Node       *dexpr = (Node *) lfirst(lc);
1981                 int                     sortgroupref = lfirst_int(lc2);
1982                 TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
1983
1984                 if (targetIsInSortList(tle, InvalidOid, result))
1985                         continue;                       /* already in list (with some semantics) */
1986                 if (skipped_sortitem)
1987                         ereport(ERROR,
1988                                         (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1989                                          errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
1990                                          parser_errposition(pstate, exprLocation(dexpr))));
1991                 result = addTargetToGroupList(pstate, tle,
1992                                                                           result, *targetlist,
1993                                                                           exprLocation(dexpr),
1994                                                                           true);
1995         }
1996
1997         return result;
1998 }
1999
2000 /*
2001  * get_matching_location
2002  *              Get the exprLocation of the exprs member corresponding to the
2003  *              (first) member of sortgrouprefs that equals sortgroupref.
2004  *
2005  * This is used so that we can point at a troublesome DISTINCT ON entry.
2006  * (Note that we need to use the original untransformed DISTINCT ON list
2007  * item, as whatever TLE it corresponds to will very possibly have a
2008  * parse location pointing to some matching entry in the SELECT list
2009  * or ORDER BY list.)
2010  */
2011 static int
2012 get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
2013 {
2014         ListCell   *lcs;
2015         ListCell   *lce;
2016
2017         forboth(lcs, sortgrouprefs, lce, exprs)
2018         {
2019                 if (lfirst_int(lcs) == sortgroupref)
2020                         return exprLocation((Node *) lfirst(lce));
2021         }
2022         /* if no match, caller blew it */
2023         elog(ERROR, "get_matching_location: no matching sortgroupref");
2024         return -1;                                      /* keep compiler quiet */
2025 }
2026
2027 /*
2028  * addTargetToSortList
2029  *              If the given targetlist entry isn't already in the SortGroupClause
2030  *              list, add it to the end of the list, using the given sort ordering
2031  *              info.
2032  *
2033  * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT.  If not,
2034  * do nothing (which implies the search for a sort operator will fail).
2035  * pstate should be provided if resolveUnknown is TRUE, but can be NULL
2036  * otherwise.
2037  *
2038  * Returns the updated SortGroupClause list.
2039  */
2040 static List *
2041 addTargetToSortList(ParseState *pstate, TargetEntry *tle,
2042                                         List *sortlist, List *targetlist, SortBy *sortby,
2043                                         bool resolveUnknown)
2044 {
2045         Oid                     restype = exprType((Node *) tle->expr);
2046         Oid                     sortop;
2047         Oid                     eqop;
2048         bool            hashable;
2049         bool            reverse;
2050         int                     location;
2051         ParseCallbackState pcbstate;
2052
2053         /* if tlist item is an UNKNOWN literal, change it to TEXT */
2054         if (restype == UNKNOWNOID && resolveUnknown)
2055         {
2056                 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
2057                                                                                  restype, TEXTOID, -1,
2058                                                                                  COERCION_IMPLICIT,
2059                                                                                  COERCE_IMPLICIT_CAST,
2060                                                                                  -1);
2061                 restype = TEXTOID;
2062         }
2063
2064         /*
2065          * Rather than clutter the API of get_sort_group_operators and the other
2066          * functions we're about to use, make use of error context callback to
2067          * mark any error reports with a parse position.  We point to the operator
2068          * location if present, else to the expression being sorted.  (NB: use the
2069          * original untransformed expression here; the TLE entry might well point
2070          * at a duplicate expression in the regular SELECT list.)
2071          */
2072         location = sortby->location;
2073         if (location < 0)
2074                 location = exprLocation(sortby->node);
2075         setup_parser_errposition_callback(&pcbstate, pstate, location);
2076
2077         /* determine the sortop, eqop, and directionality */
2078         switch (sortby->sortby_dir)
2079         {
2080                 case SORTBY_DEFAULT:
2081                 case SORTBY_ASC:
2082                         get_sort_group_operators(restype,
2083                                                                          true, true, false,
2084                                                                          &sortop, &eqop, NULL,
2085                                                                          &hashable);
2086                         reverse = false;
2087                         break;
2088                 case SORTBY_DESC:
2089                         get_sort_group_operators(restype,
2090                                                                          false, true, true,
2091                                                                          NULL, &eqop, &sortop,
2092                                                                          &hashable);
2093                         reverse = true;
2094                         break;
2095                 case SORTBY_USING:
2096                         Assert(sortby->useOp != NIL);
2097                         sortop = compatible_oper_opid(sortby->useOp,
2098                                                                                   restype,
2099                                                                                   restype,
2100                                                                                   false);
2101
2102                         /*
2103                          * Verify it's a valid ordering operator, fetch the corresponding
2104                          * equality operator, and determine whether to consider it like
2105                          * ASC or DESC.
2106                          */
2107                         eqop = get_equality_op_for_ordering_op(sortop, &reverse);
2108                         if (!OidIsValid(eqop))
2109                                 ereport(ERROR,
2110                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2111                                            errmsg("operator %s is not a valid ordering operator",
2112                                                           strVal(llast(sortby->useOp))),
2113                                                  errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
2114
2115                         /*
2116                          * Also see if the equality operator is hashable.
2117                          */
2118                         hashable = op_hashjoinable(eqop, restype);
2119                         break;
2120                 default:
2121                         elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
2122                         sortop = InvalidOid;    /* keep compiler quiet */
2123                         eqop = InvalidOid;
2124                         hashable = false;
2125                         reverse = false;
2126                         break;
2127         }
2128
2129         cancel_parser_errposition_callback(&pcbstate);
2130
2131         /* avoid making duplicate sortlist entries */
2132         if (!targetIsInSortList(tle, sortop, sortlist))
2133         {
2134                 SortGroupClause *sortcl = makeNode(SortGroupClause);
2135
2136                 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
2137
2138                 sortcl->eqop = eqop;
2139                 sortcl->sortop = sortop;
2140                 sortcl->hashable = hashable;
2141
2142                 switch (sortby->sortby_nulls)
2143                 {
2144                         case SORTBY_NULLS_DEFAULT:
2145                                 /* NULLS FIRST is default for DESC; other way for ASC */
2146                                 sortcl->nulls_first = reverse;
2147                                 break;
2148                         case SORTBY_NULLS_FIRST:
2149                                 sortcl->nulls_first = true;
2150                                 break;
2151                         case SORTBY_NULLS_LAST:
2152                                 sortcl->nulls_first = false;
2153                                 break;
2154                         default:
2155                                 elog(ERROR, "unrecognized sortby_nulls: %d",
2156                                          sortby->sortby_nulls);
2157                                 break;
2158                 }
2159
2160                 sortlist = lappend(sortlist, sortcl);
2161         }
2162
2163         return sortlist;
2164 }
2165
2166 /*
2167  * addTargetToGroupList
2168  *              If the given targetlist entry isn't already in the SortGroupClause
2169  *              list, add it to the end of the list, using default sort/group
2170  *              semantics.
2171  *
2172  * This is very similar to addTargetToSortList, except that we allow the
2173  * case where only a grouping (equality) operator can be found, and that
2174  * the TLE is considered "already in the list" if it appears there with any
2175  * sorting semantics.
2176  *
2177  * location is the parse location to be fingered in event of trouble.  Note
2178  * that we can't rely on exprLocation(tle->expr), because that might point
2179  * to a SELECT item that matches the GROUP BY item; it'd be pretty confusing
2180  * to report such a location.
2181  *
2182  * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT.  If not,
2183  * do nothing (which implies the search for an equality operator will fail).
2184  * pstate should be provided if resolveUnknown is TRUE, but can be NULL
2185  * otherwise.
2186  *
2187  * Returns the updated SortGroupClause list.
2188  */
2189 static List *
2190 addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
2191                                          List *grouplist, List *targetlist, int location,
2192                                          bool resolveUnknown)
2193 {
2194         Oid                     restype = exprType((Node *) tle->expr);
2195
2196         /* if tlist item is an UNKNOWN literal, change it to TEXT */
2197         if (restype == UNKNOWNOID && resolveUnknown)
2198         {
2199                 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
2200                                                                                  restype, TEXTOID, -1,
2201                                                                                  COERCION_IMPLICIT,
2202                                                                                  COERCE_IMPLICIT_CAST,
2203                                                                                  -1);
2204                 restype = TEXTOID;
2205         }
2206
2207         /* avoid making duplicate grouplist entries */
2208         if (!targetIsInSortList(tle, InvalidOid, grouplist))
2209         {
2210                 SortGroupClause *grpcl = makeNode(SortGroupClause);
2211                 Oid                     sortop;
2212                 Oid                     eqop;
2213                 bool            hashable;
2214                 ParseCallbackState pcbstate;
2215
2216                 setup_parser_errposition_callback(&pcbstate, pstate, location);
2217
2218                 /* determine the eqop and optional sortop */
2219                 get_sort_group_operators(restype,
2220                                                                  false, true, false,
2221                                                                  &sortop, &eqop, NULL,
2222                                                                  &hashable);
2223
2224                 cancel_parser_errposition_callback(&pcbstate);
2225
2226                 grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
2227                 grpcl->eqop = eqop;
2228                 grpcl->sortop = sortop;
2229                 grpcl->nulls_first = false;             /* OK with or without sortop */
2230                 grpcl->hashable = hashable;
2231
2232                 grouplist = lappend(grouplist, grpcl);
2233         }
2234
2235         return grouplist;
2236 }
2237
2238 /*
2239  * assignSortGroupRef
2240  *        Assign the targetentry an unused ressortgroupref, if it doesn't
2241  *        already have one.  Return the assigned or pre-existing refnumber.
2242  *
2243  * 'tlist' is the targetlist containing (or to contain) the given targetentry.
2244  */
2245 Index
2246 assignSortGroupRef(TargetEntry *tle, List *tlist)
2247 {
2248         Index           maxRef;
2249         ListCell   *l;
2250
2251         if (tle->ressortgroupref)       /* already has one? */
2252                 return tle->ressortgroupref;
2253
2254         /* easiest way to pick an unused refnumber: max used + 1 */
2255         maxRef = 0;
2256         foreach(l, tlist)
2257         {
2258                 Index           ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
2259
2260                 if (ref > maxRef)
2261                         maxRef = ref;
2262         }
2263         tle->ressortgroupref = maxRef + 1;
2264         return tle->ressortgroupref;
2265 }
2266
2267 /*
2268  * targetIsInSortList
2269  *              Is the given target item already in the sortlist?
2270  *              If sortop is not InvalidOid, also test for a match to the sortop.
2271  *
2272  * It is not an oversight that this function ignores the nulls_first flag.
2273  * We check sortop when determining if an ORDER BY item is redundant with
2274  * earlier ORDER BY items, because it's conceivable that "ORDER BY
2275  * foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
2276  * values that < considers equal.  We need not check nulls_first
2277  * however, because a lower-order column with the same sortop but
2278  * opposite nulls direction is redundant.  Also, we can consider
2279  * ORDER BY foo ASC, foo DESC redundant, so check for a commutator match.
2280  *
2281  * Works for both ordering and grouping lists (sortop would normally be
2282  * InvalidOid when considering grouping).  Note that the main reason we need
2283  * this routine (and not just a quick test for nonzeroness of ressortgroupref)
2284  * is that a TLE might be in only one of the lists.
2285  */
2286 bool
2287 targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
2288 {
2289         Index           ref = tle->ressortgroupref;
2290         ListCell   *l;
2291
2292         /* no need to scan list if tle has no marker */
2293         if (ref == 0)
2294                 return false;
2295
2296         foreach(l, sortList)
2297         {
2298                 SortGroupClause *scl = (SortGroupClause *) lfirst(l);
2299
2300                 if (scl->tleSortGroupRef == ref &&
2301                         (sortop == InvalidOid ||
2302                          sortop == scl->sortop ||
2303                          sortop == get_commutator(scl->sortop)))
2304                         return true;
2305         }
2306         return false;
2307 }
2308
2309 /*
2310  * findWindowClause
2311  *              Find the named WindowClause in the list, or return NULL if not there
2312  */
2313 static WindowClause *
2314 findWindowClause(List *wclist, const char *name)
2315 {
2316         ListCell   *l;
2317
2318         foreach(l, wclist)
2319         {
2320                 WindowClause *wc = (WindowClause *) lfirst(l);
2321
2322                 if (wc->name && strcmp(wc->name, name) == 0)
2323                         return wc;
2324         }
2325
2326         return NULL;
2327 }
2328
2329 /*
2330  * transformFrameOffset
2331  *              Process a window frame offset expression
2332  */
2333 static Node *
2334 transformFrameOffset(ParseState *pstate, int frameOptions, Node *clause)
2335 {
2336         const char *constructName = NULL;
2337         Node       *node;
2338
2339         /* Quick exit if no offset expression */
2340         if (clause == NULL)
2341                 return NULL;
2342
2343         if (frameOptions & FRAMEOPTION_ROWS)
2344         {
2345                 /* Transform the raw expression tree */
2346                 node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_ROWS);
2347
2348                 /*
2349                  * Like LIMIT clause, simply coerce to int8
2350                  */
2351                 constructName = "ROWS";
2352                 node = coerce_to_specific_type(pstate, node, INT8OID, constructName);
2353         }
2354         else if (frameOptions & FRAMEOPTION_RANGE)
2355         {
2356                 /* Transform the raw expression tree */
2357                 node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_RANGE);
2358
2359                 /*
2360                  * this needs a lot of thought to decide how to support in the context
2361                  * of Postgres' extensible datatype framework
2362                  */
2363                 constructName = "RANGE";
2364                 /* error was already thrown by gram.y, this is just a backstop */
2365                 elog(ERROR, "window frame with value offset is not implemented");
2366         }
2367         else
2368         {
2369                 Assert(false);
2370                 node = NULL;
2371         }
2372
2373         /* Disallow variables in frame offsets */
2374         checkExprIsVarFree(pstate, node, constructName);
2375
2376         return node;
2377 }