]> granicus.if.org Git - postgresql/blob - src/backend/parser/parse_clause.c
pgindent run. Make it all clean.
[postgresql] / src / backend / parser / parse_clause.c
1 /*-------------------------------------------------------------------------
2  *
3  * parse_clause.c
4  *        handle clauses in parser
5  *
6  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.78 2001/03/22 03:59:41 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include "access/heapam.h"
19 #include "nodes/makefuncs.h"
20 #include "optimizer/tlist.h"
21 #include "optimizer/var.h"
22 #include "parser/analyze.h"
23 #include "parser/parse.h"
24 #include "parser/parsetree.h"
25 #include "parser/parse_clause.h"
26 #include "parser/parse_coerce.h"
27 #include "parser/parse_expr.h"
28 #include "parser/parse_oper.h"
29 #include "parser/parse_relation.h"
30 #include "parser/parse_target.h"
31 #include "parser/parse_type.h"
32 #include "utils/guc.h"
33
34
35 #define ORDER_CLAUSE 0
36 #define GROUP_CLAUSE 1
37 #define DISTINCT_ON_CLAUSE 2
38
39 static char *clauseText[] = {"ORDER BY", "GROUP BY", "DISTINCT ON"};
40
41 static void extractUniqueColumns(List *common_colnames,
42                                          List *src_colnames, List *src_colvars,
43                                          List **res_colnames, List **res_colvars);
44 static Node *transformJoinUsingClause(ParseState *pstate,
45                                                  List *leftVars, List *rightVars);
46 static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
47                                           List *containedRels);
48 static RangeTblRef *transformTableEntry(ParseState *pstate, RangeVar *r);
49 static RangeTblRef *transformRangeSubselect(ParseState *pstate,
50                                                 RangeSubselect *r);
51 static Node *transformFromClauseItem(ParseState *pstate, Node *n,
52                                                 List **containedRels);
53 static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
54                                         List *tlist, int clause);
55 static List *addTargetToSortList(TargetEntry *tle, List *sortlist,
56                                         List *targetlist, char *opname);
57 static bool exprIsInSortList(Node *expr, List *sortList, List *targetList);
58
59
60 /*
61  * transformFromClause -
62  *        Process the FROM clause and add items to the query's range table,
63  *        joinlist, and namespace.
64  *
65  * Note: we assume that pstate's p_rtable, p_joinlist, and p_namespace lists
66  * were initialized to NIL when the pstate was created.  We will add onto
67  * any entries already present --- this is needed for rule processing, as
68  * well as for UPDATE and DELETE.
69  *
70  * The range table may grow still further when we transform the expressions
71  * in the query's quals and target list. (This is possible because in
72  * POSTQUEL, we allowed references to relations not specified in the
73  * from-clause.  PostgreSQL keeps this extension to standard SQL.)
74  */
75 void
76 transformFromClause(ParseState *pstate, List *frmList)
77 {
78         List       *fl;
79
80         /*
81          * The grammar will have produced a list of RangeVars,
82          * RangeSubselects, and/or JoinExprs. Transform each one (possibly
83          * adding entries to the rtable), check for duplicate refnames, and
84          * then add it to the joinlist and namespace.
85          */
86         foreach(fl, frmList)
87         {
88                 Node       *n = lfirst(fl);
89                 List       *containedRels;
90
91                 n = transformFromClauseItem(pstate, n, &containedRels);
92                 checkNameSpaceConflicts(pstate, (Node *) pstate->p_namespace, n);
93                 pstate->p_joinlist = lappend(pstate->p_joinlist, n);
94                 pstate->p_namespace = lappend(pstate->p_namespace, n);
95         }
96 }
97
98 /*
99  * setTargetTable
100  *        Add the target relation of INSERT/UPDATE/DELETE to the range table,
101  *        and make the special links to it in the ParseState.
102  *
103  *        We also open the target relation and acquire a write lock on it.
104  *        This must be done before processing the FROM list, in case the target
105  *        is also mentioned as a source relation --- we want to be sure to grab
106  *        the write lock before any read lock.
107  *
108  *        If alsoSource is true, add the target to the query's joinlist and
109  *        namespace.  For INSERT, we don't want the target to be joined to;
110  *        it's a destination of tuples, not a source.   For UPDATE/DELETE,
111  *        we do need to scan or join the target.  (NOTE: we do not bother
112  *        to check for namespace conflict; we assume that the namespace was
113  *        initially empty in these cases.)
114  *
115  *        Returns the rangetable index of the target relation.
116  */
117 int
118 setTargetTable(ParseState *pstate, char *relname,
119                            bool inh, bool alsoSource)
120 {
121         RangeTblEntry *rte;
122         int                     rtindex;
123
124         /* Close old target; this could only happen for multi-action rules */
125         if (pstate->p_target_relation != NULL)
126                 heap_close(pstate->p_target_relation, NoLock);
127
128         /*
129          * Open target rel and grab suitable lock (which we will hold till end
130          * of transaction).
131          *
132          * analyze.c will eventually do the corresponding heap_close(), but *not*
133          * release the lock.
134          */
135         pstate->p_target_relation = heap_openr(relname, RowExclusiveLock);
136
137         /*
138          * Now build an RTE.
139          */
140         rte = addRangeTableEntry(pstate, relname, NULL, inh, false);
141         pstate->p_target_rangetblentry = rte;
142
143         /* assume new rte is at end */
144         rtindex = length(pstate->p_rtable);
145         Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
146
147         /*
148          * Override addRangeTableEntry's default checkForRead, and instead
149          * mark target table as requiring write access.
150          *
151          * If we find an explicit reference to the rel later during parse
152          * analysis, scanRTEForColumn will change checkForRead to 'true'
153          * again.  That can't happen for INSERT but it is possible for UPDATE
154          * and DELETE.
155          */
156         rte->checkForRead = false;
157         rte->checkForWrite = true;
158
159         /*
160          * If UPDATE/DELETE, add table to joinlist and namespace.
161          */
162         if (alsoSource)
163                 addRTEtoQuery(pstate, rte, true, true);
164
165         return rtindex;
166 }
167
168 /*
169  * Simplify InhOption (yes/no/default) into boolean yes/no.
170  *
171  * The reason we do things this way is that we don't want to examine the
172  * SQL_inheritance option flag until parse_analyze is run.      Otherwise,
173  * we'd do the wrong thing with query strings that intermix SET commands
174  * with queries.
175  */
176 bool
177 interpretInhOption(InhOption inhOpt)
178 {
179         switch (inhOpt)
180         {
181                         case INH_NO:
182                         return false;
183                 case INH_YES:
184                         return true;
185                 case INH_DEFAULT:
186                         return SQL_inheritance;
187         }
188         elog(ERROR, "Bogus InhOption value");
189         return false;                           /* keep compiler quiet */
190 }
191
192 /*
193  * Extract all not-in-common columns from column lists of a source table
194  */
195 static void
196 extractUniqueColumns(List *common_colnames,
197                                          List *src_colnames, List *src_colvars,
198                                          List **res_colnames, List **res_colvars)
199 {
200         List       *new_colnames = NIL;
201         List       *new_colvars = NIL;
202         List       *lnames,
203                            *lvars = src_colvars;
204
205         foreach(lnames, src_colnames)
206         {
207                 char       *colname = strVal(lfirst(lnames));
208                 bool            match = false;
209                 List       *cnames;
210
211                 foreach(cnames, common_colnames)
212                 {
213                         char       *ccolname = strVal(lfirst(cnames));
214
215                         if (strcmp(colname, ccolname) == 0)
216                         {
217                                 match = true;
218                                 break;
219                         }
220                 }
221
222                 if (!match)
223                 {
224                         new_colnames = lappend(new_colnames, lfirst(lnames));
225                         new_colvars = lappend(new_colvars, lfirst(lvars));
226                 }
227
228                 lvars = lnext(lvars);
229         }
230
231         *res_colnames = new_colnames;
232         *res_colvars = new_colvars;
233 }
234
235 /* transformJoinUsingClause()
236  *        Build a complete ON clause from a partially-transformed USING list.
237  *        We are given lists of nodes representing left and right match columns.
238  *        Result is a transformed qualification expression.
239  */
240 static Node *
241 transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars)
242 {
243         Node       *result = NULL;
244         List       *lvars,
245                            *rvars = rightVars;
246
247         /*
248          * We cheat a little bit here by building an untransformed operator
249          * tree whose leaves are the already-transformed Vars.  This is OK
250          * because transformExpr() won't complain about already-transformed
251          * subnodes.
252          */
253         foreach(lvars, leftVars)
254         {
255                 Node       *lvar = (Node *) lfirst(lvars);
256                 Node       *rvar = (Node *) lfirst(rvars);
257                 A_Expr     *e;
258
259                 e = makeNode(A_Expr);
260                 e->oper = OP;
261                 e->opname = "=";
262                 e->lexpr = copyObject(lvar);
263                 e->rexpr = copyObject(rvar);
264
265                 if (result == NULL)
266                         result = (Node *) e;
267                 else
268                 {
269                         A_Expr     *a = makeNode(A_Expr);
270
271                         a->oper = AND;
272                         a->opname = NULL;
273                         a->lexpr = result;
274                         a->rexpr = (Node *) e;
275                         result = (Node *) a;
276                 }
277
278                 rvars = lnext(rvars);
279         }
280
281         /*
282          * Since the references are already Vars, and are certainly from the
283          * input relations, we don't have to go through the same pushups that
284          * transformJoinOnClause() does.  Just invoke transformExpr() to fix
285          * up the operators, and we're done.
286          */
287         result = transformExpr(pstate, result, EXPR_COLUMN_FIRST);
288
289         if (exprType(result) != BOOLOID)
290         {
291
292                 /*
293                  * This could only happen if someone defines a funny version of
294                  * '='
295                  */
296                 elog(ERROR, "JOIN/USING clause must return type bool, not type %s",
297                          typeidTypeName(exprType(result)));
298         }
299
300         return result;
301 }       /* transformJoinUsingClause() */
302
303 /* transformJoinOnClause()
304  *        Transform the qual conditions for JOIN/ON.
305  *        Result is a transformed qualification expression.
306  */
307 static Node *
308 transformJoinOnClause(ParseState *pstate, JoinExpr *j,
309                                           List *containedRels)
310 {
311         Node       *result;
312         List       *save_namespace;
313         List       *clause_varnos,
314                            *l;
315
316         /*
317          * This is a tad tricky, for two reasons.  First, the namespace that
318          * the join expression should see is just the two subtrees of the JOIN
319          * plus any outer references from upper pstate levels.  So,
320          * temporarily set this pstate's namespace accordingly.  (We need not
321          * check for refname conflicts, because transformFromClauseItem()
322          * already did.) NOTE: this code is OK only because the ON clause
323          * can't legally alter the namespace by causing implicit relation refs
324          * to be added.
325          */
326         save_namespace = pstate->p_namespace;
327         pstate->p_namespace = makeList2(j->larg, j->rarg);
328
329         /* This part is just like transformWhereClause() */
330         result = transformExpr(pstate, j->quals, EXPR_COLUMN_FIRST);
331         if (exprType(result) != BOOLOID)
332         {
333                 elog(ERROR, "JOIN/ON clause must return type bool, not type %s",
334                          typeidTypeName(exprType(result)));
335         }
336
337         pstate->p_namespace = save_namespace;
338
339         /*
340          * Second, we need to check that the ON condition doesn't refer to any
341          * rels outside the input subtrees of the JOIN.  It could do that
342          * despite our hack on the namespace if it uses fully-qualified names.
343          * So, grovel through the transformed clause and make sure there are
344          * no bogus references.  (Outer references are OK, and are ignored
345          * here.)
346          */
347         clause_varnos = pull_varnos(result);
348         foreach(l, clause_varnos)
349         {
350                 int                     varno = lfirsti(l);
351
352                 if (!intMember(varno, containedRels))
353                 {
354                         elog(ERROR, "JOIN/ON clause refers to \"%s\", which is not part of JOIN",
355                                  rt_fetch(varno, pstate->p_rtable)->eref->relname);
356                 }
357         }
358         freeList(clause_varnos);
359
360         return result;
361 }
362
363 /*
364  * transformTableEntry --- transform a RangeVar (simple relation reference)
365  */
366 static RangeTblRef *
367 transformTableEntry(ParseState *pstate, RangeVar *r)
368 {
369         char       *relname = r->relname;
370         RangeTblEntry *rte;
371         RangeTblRef *rtr;
372
373         /*
374          * mark this entry to indicate it comes from the FROM clause. In SQL,
375          * the target list can only refer to range variables specified in the
376          * from clause but we follow the more powerful POSTQUEL semantics and
377          * automatically generate the range variable if not specified. However
378          * there are times we need to know whether the entries are legitimate.
379          */
380         rte = addRangeTableEntry(pstate, relname, r->name,
381                                                          interpretInhOption(r->inhOpt), true);
382
383         /*
384          * We create a RangeTblRef, but we do not add it to the joinlist or
385          * namespace; our caller must do that if appropriate.
386          */
387         rtr = makeNode(RangeTblRef);
388         /* assume new rte is at end */
389         rtr->rtindex = length(pstate->p_rtable);
390         Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
391
392         return rtr;
393 }
394
395
396 /*
397  * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
398  */
399 static RangeTblRef *
400 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
401 {
402         List       *save_namespace;
403         List       *parsetrees;
404         Query      *query;
405         RangeTblEntry *rte;
406         RangeTblRef *rtr;
407
408         /*
409          * We require user to supply an alias for a subselect, per SQL92. To
410          * relax this, we'd have to be prepared to gin up a unique alias for
411          * an unlabeled subselect.
412          */
413         if (r->name == NULL)
414                 elog(ERROR, "sub-select in FROM must have an alias");
415
416         /*
417          * Analyze and transform the subquery.  This is a bit tricky because
418          * we don't want the subquery to be able to see any FROM items already
419          * created in the current query (per SQL92, the scope of a FROM item
420          * does not include other FROM items).  But it does need to be able to
421          * see any further-up parent states, so we can't just pass a null
422          * parent pstate link.  So, temporarily make the current query level
423          * have an empty namespace.
424          */
425         save_namespace = pstate->p_namespace;
426         pstate->p_namespace = NIL;
427         parsetrees = parse_analyze(r->subquery, pstate);
428         pstate->p_namespace = save_namespace;
429
430         /*
431          * Check that we got something reasonable.      Some of these conditions
432          * are probably impossible given restrictions of the grammar, but
433          * check 'em anyway.
434          */
435         if (length(parsetrees) != 1)
436                 elog(ERROR, "Unexpected parse analysis result for subselect in FROM");
437         query = (Query *) lfirst(parsetrees);
438         if (query == NULL || !IsA(query, Query))
439                 elog(ERROR, "Unexpected parse analysis result for subselect in FROM");
440
441         if (query->commandType != CMD_SELECT)
442                 elog(ERROR, "Expected SELECT query from subselect in FROM");
443         if (query->resultRelation != 0 || query->into != NULL || query->isPortal)
444                 elog(ERROR, "Subselect in FROM may not have SELECT INTO");
445
446         /*
447          * OK, build an RTE for the subquery.
448          */
449         rte = addRangeTableEntryForSubquery(pstate, query, r->name, true);
450
451         /*
452          * We create a RangeTblRef, but we do not add it to the joinlist or
453          * namespace; our caller must do that if appropriate.
454          */
455         rtr = makeNode(RangeTblRef);
456         /* assume new rte is at end */
457         rtr->rtindex = length(pstate->p_rtable);
458         Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
459
460         return rtr;
461 }
462
463
464 /*
465  * transformFromClauseItem -
466  *        Transform a FROM-clause item, adding any required entries to the
467  *        range table list being built in the ParseState, and return the
468  *        transformed item ready to include in the joinlist and namespace.
469  *        This routine can recurse to handle SQL92 JOIN expressions.
470  *
471  *        Aside from the primary return value (the transformed joinlist item)
472  *        this routine also returns an integer list of the rangetable indexes
473  *        of all the base relations represented in the joinlist item.  This
474  *        list is needed for checking JOIN/ON conditions in higher levels.
475  */
476 static Node *
477 transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
478 {
479         if (IsA(n, RangeVar))
480         {
481                 /* Plain relation reference */
482                 RangeTblRef *rtr;
483
484                 rtr = transformTableEntry(pstate, (RangeVar *) n);
485                 *containedRels = makeListi1(rtr->rtindex);
486                 return (Node *) rtr;
487         }
488         else if (IsA(n, RangeSubselect))
489         {
490                 /* sub-SELECT is like a plain relation */
491                 RangeTblRef *rtr;
492
493                 rtr = transformRangeSubselect(pstate, (RangeSubselect *) n);
494                 *containedRels = makeListi1(rtr->rtindex);
495                 return (Node *) rtr;
496         }
497         else if (IsA(n, JoinExpr))
498         {
499                 /* A newfangled join expression */
500                 JoinExpr   *j = (JoinExpr *) n;
501                 List       *l_containedRels,
502                                    *r_containedRels,
503                                    *l_colnames,
504                                    *r_colnames,
505                                    *res_colnames,
506                                    *l_colvars,
507                                    *r_colvars,
508                                    *res_colvars;
509
510                 /*
511                  * Recursively process the left and right subtrees
512                  */
513                 j->larg = transformFromClauseItem(pstate, j->larg, &l_containedRels);
514                 j->rarg = transformFromClauseItem(pstate, j->rarg, &r_containedRels);
515
516                 /*
517                  * Generate combined list of relation indexes
518                  */
519                 *containedRels = nconc(l_containedRels, r_containedRels);
520
521                 /*
522                  * Check for conflicting refnames in left and right subtrees.
523                  * Must do this because higher levels will assume I hand back a
524                  * self- consistent namespace subtree.
525                  */
526                 checkNameSpaceConflicts(pstate, j->larg, j->rarg);
527
528                 /*
529                  * Extract column name and var lists from both subtrees
530                  */
531                 if (IsA(j->larg, JoinExpr))
532                 {
533                         /* Make a copy of the subtree's lists so we can modify! */
534                         l_colnames = copyObject(((JoinExpr *) j->larg)->colnames);
535                         l_colvars = copyObject(((JoinExpr *) j->larg)->colvars);
536                 }
537                 else
538                 {
539                         RangeTblEntry *rte;
540
541                         Assert(IsA(j->larg, RangeTblRef));
542                         rte = rt_fetch(((RangeTblRef *) j->larg)->rtindex,
543                                                    pstate->p_rtable);
544                         expandRTE(pstate, rte, &l_colnames, &l_colvars);
545                         /* expandRTE returns new lists, so no need for copyObject */
546                 }
547                 if (IsA(j->rarg, JoinExpr))
548                 {
549                         /* Make a copy of the subtree's lists so we can modify! */
550                         r_colnames = copyObject(((JoinExpr *) j->rarg)->colnames);
551                         r_colvars = copyObject(((JoinExpr *) j->rarg)->colvars);
552                 }
553                 else
554                 {
555                         RangeTblEntry *rte;
556
557                         Assert(IsA(j->rarg, RangeTblRef));
558                         rte = rt_fetch(((RangeTblRef *) j->rarg)->rtindex,
559                                                    pstate->p_rtable);
560                         expandRTE(pstate, rte, &r_colnames, &r_colvars);
561                         /* expandRTE returns new lists, so no need for copyObject */
562                 }
563
564                 /*
565                  * Natural join does not explicitly specify columns; must generate
566                  * columns to join. Need to run through the list of columns from
567                  * each table or join result and match up the column names. Use
568                  * the first table, and check every column in the second table for
569                  * a match.  (We'll check that the matches were unique later on.)
570                  * The result of this step is a list of column names just like an
571                  * explicitly-written USING list.
572                  */
573                 if (j->isNatural)
574                 {
575                         List       *rlist = NIL;
576                         List       *lx,
577                                            *rx;
578
579                         Assert(j->using == NIL);        /* shouldn't have USING() too */
580
581                         foreach(lx, l_colnames)
582                         {
583                                 char       *l_colname = strVal(lfirst(lx));
584                                 Value      *m_name = NULL;
585
586                                 foreach(rx, r_colnames)
587                                 {
588                                         char       *r_colname = strVal(lfirst(rx));
589
590                                         if (strcmp(l_colname, r_colname) == 0)
591                                         {
592                                                 m_name = makeString(l_colname);
593                                                 break;
594                                         }
595                                 }
596
597                                 /* matched a right column? then keep as join column... */
598                                 if (m_name != NULL)
599                                         rlist = lappend(rlist, m_name);
600                         }
601
602                         j->using = rlist;
603                 }
604
605                 /*
606                  * Now transform the join qualifications, if any.
607                  */
608                 res_colnames = NIL;
609                 res_colvars = NIL;
610
611                 if (j->using)
612                 {
613
614                         /*
615                          * JOIN/USING (or NATURAL JOIN, as transformed above).
616                          * Transform the list into an explicit ON-condition, and
617                          * generate a list of result columns.
618                          */
619                         List       *ucols = j->using;
620                         List       *l_usingvars = NIL;
621                         List       *r_usingvars = NIL;
622                         List       *ucol;
623
624                         Assert(j->quals == NULL);       /* shouldn't have ON() too */
625
626                         foreach(ucol, ucols)
627                         {
628                                 char       *u_colname = strVal(lfirst(ucol));
629                                 List       *col;
630                                 Node       *l_colvar,
631                                                    *r_colvar,
632                                                    *colvar;
633                                 int                     ndx;
634                                 int                     l_index = -1;
635                                 int                     r_index = -1;
636
637                                 ndx = 0;
638                                 foreach(col, l_colnames)
639                                 {
640                                         char       *l_colname = strVal(lfirst(col));
641
642                                         if (strcmp(l_colname, u_colname) == 0)
643                                         {
644                                                 if (l_index >= 0)
645                                                         elog(ERROR, "Common column name \"%s\" appears more than once in left table", u_colname);
646                                                 l_index = ndx;
647                                         }
648                                         ndx++;
649                                 }
650                                 if (l_index < 0)
651                                         elog(ERROR, "JOIN/USING column \"%s\" not found in left table",
652                                                  u_colname);
653
654                                 ndx = 0;
655                                 foreach(col, r_colnames)
656                                 {
657                                         char       *r_colname = strVal(lfirst(col));
658
659                                         if (strcmp(r_colname, u_colname) == 0)
660                                         {
661                                                 if (r_index >= 0)
662                                                         elog(ERROR, "Common column name \"%s\" appears more than once in right table", u_colname);
663                                                 r_index = ndx;
664                                         }
665                                         ndx++;
666                                 }
667                                 if (r_index < 0)
668                                         elog(ERROR, "JOIN/USING column \"%s\" not found in right table",
669                                                  u_colname);
670
671                                 l_colvar = nth(l_index, l_colvars);
672                                 l_usingvars = lappend(l_usingvars, l_colvar);
673                                 r_colvar = nth(r_index, r_colvars);
674                                 r_usingvars = lappend(r_usingvars, r_colvar);
675
676                                 res_colnames = lappend(res_colnames,
677                                                                            nth(l_index, l_colnames));
678                                 switch (j->jointype)
679                                 {
680                                         case JOIN_INNER:
681                                         case JOIN_LEFT:
682                                                 colvar = l_colvar;
683                                                 break;
684                                         case JOIN_RIGHT:
685                                                 colvar = r_colvar;
686                                                 break;
687                                         default:
688                                                 {
689                                                         /* Need COALESCE(l_colvar, r_colvar) */
690                                                         CaseExpr   *c = makeNode(CaseExpr);
691                                                         CaseWhen   *w = makeNode(CaseWhen);
692                                                         A_Expr     *a = makeNode(A_Expr);
693
694                                                         a->oper = NOTNULL;
695                                                         a->lexpr = l_colvar;
696                                                         w->expr = (Node *) a;
697                                                         w->result = l_colvar;
698                                                         c->args = makeList1(w);
699                                                         c->defresult = r_colvar;
700                                                         colvar = transformExpr(pstate, (Node *) c,
701                                                                                                    EXPR_COLUMN_FIRST);
702                                                         break;
703                                                 }
704                                 }
705                                 res_colvars = lappend(res_colvars, colvar);
706                         }
707
708                         j->quals = transformJoinUsingClause(pstate,
709                                                                                                 l_usingvars,
710                                                                                                 r_usingvars);
711                 }
712                 else if (j->quals)
713                 {
714                         /* User-written ON-condition; transform it */
715                         j->quals = transformJoinOnClause(pstate, j, *containedRels);
716                 }
717                 else
718                 {
719                         /* CROSS JOIN: no quals */
720                 }
721
722                 /* Add remaining columns from each side to the output columns */
723                 extractUniqueColumns(res_colnames,
724                                                          l_colnames, l_colvars,
725                                                          &l_colnames, &l_colvars);
726                 extractUniqueColumns(res_colnames,
727                                                          r_colnames, r_colvars,
728                                                          &r_colnames, &r_colvars);
729                 res_colnames = nconc(res_colnames, l_colnames);
730                 res_colvars = nconc(res_colvars, l_colvars);
731                 res_colnames = nconc(res_colnames, r_colnames);
732                 res_colvars = nconc(res_colvars, r_colvars);
733
734                 /*
735                  * Process alias (AS clause), if any.
736                  */
737                 if (j->alias)
738                 {
739
740                         /*
741                          * If a column alias list is specified, substitute the alias
742                          * names into my output-column list
743                          */
744                         if (j->alias->attrs != NIL)
745                         {
746                                 if (length(j->alias->attrs) != length(res_colnames))
747                                         elog(ERROR, "Column alias list for \"%s\" has wrong number of entries (need %d)",
748                                                  j->alias->relname, length(res_colnames));
749                                 res_colnames = j->alias->attrs;
750                         }
751                 }
752
753                 j->colnames = res_colnames;
754                 j->colvars = res_colvars;
755
756                 return (Node *) j;
757         }
758         else
759                 elog(ERROR, "transformFromClauseItem: unexpected node (internal error)"
760                          "\n\t%s", nodeToString(n));
761         return NULL;                            /* can't get here, just keep compiler
762                                                                  * quiet */
763 }
764
765
766 /*
767  * transformWhereClause -
768  *        transforms the qualification and make sure it is of type Boolean
769  */
770 Node *
771 transformWhereClause(ParseState *pstate, Node *clause)
772 {
773         Node       *qual;
774
775         if (clause == NULL)
776                 return NULL;
777
778         qual = transformExpr(pstate, clause, EXPR_COLUMN_FIRST);
779
780         if (exprType(qual) != BOOLOID)
781         {
782                 elog(ERROR, "WHERE clause must return type bool, not type %s",
783                          typeidTypeName(exprType(qual)));
784         }
785         return qual;
786 }
787
788
789 /*
790  *      findTargetlistEntry -
791  *        Returns the targetlist entry matching the given (untransformed) node.
792  *        If no matching entry exists, one is created and appended to the target
793  *        list as a "resjunk" node.
794  *
795  * node         the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
796  * tlist        the existing target list (NB: this will never be NIL, which is a
797  *                      good thing since we'd be unable to append to it if it were...)
798  * clause       identifies clause type being processed.
799  */
800 static TargetEntry *
801 findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
802 {
803         TargetEntry *target_result = NULL;
804         List       *tl;
805         Node       *expr;
806
807         /*----------
808          * Handle two special cases as mandated by the SQL92 spec:
809          *
810          * 1. Bare ColumnName (no qualifier or subscripts)
811          *        For a bare identifier, we search for a matching column name
812          *        in the existing target list.  Multiple matches are an error
813          *        unless they refer to identical values; for example,
814          *        we allow      SELECT a, a FROM table ORDER BY a
815          *        but not       SELECT a AS b, b FROM table ORDER BY b
816          *        If no match is found, we fall through and treat the identifier
817          *        as an expression.
818          *        For GROUP BY, it is incorrect to match the grouping item against
819          *        targetlist entries: according to SQL92, an identifier in GROUP BY
820          *        is a reference to a column name exposed by FROM, not to a target
821          *        list column.  However, many implementations (including pre-7.0
822          *        PostgreSQL) accept this anyway.  So for GROUP BY, we look first
823          *        to see if the identifier matches any FROM column name, and only
824          *        try for a targetlist name if it doesn't.  This ensures that we
825          *        adhere to the spec in the case where the name could be both.
826          *        DISTINCT ON isn't in the standard, so we can do what we like there;
827          *        we choose to make it work like ORDER BY, on the rather flimsy
828          *        grounds that ordinary DISTINCT works on targetlist entries.
829          *
830          * 2. IntegerConstant
831          *        This means to use the n'th item in the existing target list.
832          *        Note that it would make no sense to order/group/distinct by an
833          *        actual constant, so this does not create a conflict with our
834          *        extension to order/group by an expression.
835          *        GROUP BY column-number is not allowed by SQL92, but since
836          *        the standard has no other behavior defined for this syntax,
837          *        we may as well accept this common extension.
838          *
839          * Note that pre-existing resjunk targets must not be used in either case,
840          * since the user didn't write them in his SELECT list.
841          *
842          * If neither special case applies, fall through to treat the item as
843          * an expression.
844          *----------
845          */
846         if (IsA(node, Ident) &&((Ident *) node)->indirection == NIL)
847         {
848                 char       *name = ((Ident *) node)->name;
849
850                 if (clause == GROUP_CLAUSE)
851                 {
852
853                         /*
854                          * In GROUP BY, we must prefer a match against a FROM-clause
855                          * column to one against the targetlist.  Look to see if there
856                          * is a matching column.  If so, fall through to let
857                          * transformExpr() do the rest.  NOTE: if name could refer
858                          * ambiguously to more than one column name exposed by FROM,
859                          * colnameToVar will elog(ERROR).  That's just what we want
860                          * here.
861                          */
862                         if (colnameToVar(pstate, name) != NULL)
863                                 name = NULL;
864                 }
865
866                 if (name != NULL)
867                 {
868                         foreach(tl, tlist)
869                         {
870                                 TargetEntry *tle = (TargetEntry *) lfirst(tl);
871                                 Resdom     *resnode = tle->resdom;
872
873                                 if (!resnode->resjunk &&
874                                         strcmp(resnode->resname, name) == 0)
875                                 {
876                                         if (target_result != NULL)
877                                         {
878                                                 if (!equal(target_result->expr, tle->expr))
879                                                         elog(ERROR, "%s '%s' is ambiguous",
880                                                                  clauseText[clause], name);
881                                         }
882                                         else
883                                                 target_result = tle;
884                                         /* Stay in loop to check for ambiguity */
885                                 }
886                         }
887                         if (target_result != NULL)
888                                 return target_result;   /* return the first match */
889                 }
890         }
891         if (IsA(node, A_Const))
892         {
893                 Value      *val = &((A_Const *) node)->val;
894                 int                     targetlist_pos = 0;
895                 int                     target_pos;
896
897                 if (!IsA(val, Integer))
898                         elog(ERROR, "Non-integer constant in %s", clauseText[clause]);
899                 target_pos = intVal(val);
900                 foreach(tl, tlist)
901                 {
902                         TargetEntry *tle = (TargetEntry *) lfirst(tl);
903                         Resdom     *resnode = tle->resdom;
904
905                         if (!resnode->resjunk)
906                         {
907                                 if (++targetlist_pos == target_pos)
908                                         return tle; /* return the unique match */
909                         }
910                 }
911                 elog(ERROR, "%s position %d is not in target list",
912                          clauseText[clause], target_pos);
913         }
914
915         /*
916          * Otherwise, we have an expression (this is a Postgres extension not
917          * found in SQL92).  Convert the untransformed node to a transformed
918          * expression, and search for a match in the tlist. NOTE: it doesn't
919          * really matter whether there is more than one match.  Also, we are
920          * willing to match a resjunk target here, though the above cases must
921          * ignore resjunk targets.
922          */
923         expr = transformExpr(pstate, node, EXPR_COLUMN_FIRST);
924
925         foreach(tl, tlist)
926         {
927                 TargetEntry *tle = (TargetEntry *) lfirst(tl);
928
929                 if (equal(expr, tle->expr))
930                         return tle;
931         }
932
933         /*
934          * If no matches, construct a new target entry which is appended to
935          * the end of the target list.  This target is given resjunk = TRUE so
936          * that it will not be projected into the final tuple.
937          */
938         target_result = transformTargetEntry(pstate, node, expr, NULL, true);
939         lappend(tlist, target_result);
940
941         return target_result;
942 }
943
944
945 /*
946  * transformGroupClause -
947  *        transform a Group By clause
948  *
949  */
950 List *
951 transformGroupClause(ParseState *pstate, List *grouplist, List *targetlist)
952 {
953         List       *glist = NIL,
954                            *gl;
955
956         foreach(gl, grouplist)
957         {
958                 TargetEntry *tle;
959
960                 tle = findTargetlistEntry(pstate, lfirst(gl),
961                                                                   targetlist, GROUP_CLAUSE);
962
963                 /* avoid making duplicate grouplist entries */
964                 if (!exprIsInSortList(tle->expr, glist, targetlist))
965                 {
966                         GroupClause *grpcl = makeNode(GroupClause);
967
968                         grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
969
970                         grpcl->sortop = any_ordering_op(tle->resdom->restype);
971
972                         glist = lappend(glist, grpcl);
973                 }
974         }
975
976         return glist;
977 }
978
979 /*
980  * transformSortClause -
981  *        transform an ORDER BY clause
982  */
983 List *
984 transformSortClause(ParseState *pstate,
985                                         List *orderlist,
986                                         List *targetlist)
987 {
988         List       *sortlist = NIL;
989         List       *olitem;
990
991         foreach(olitem, orderlist)
992         {
993                 SortGroupBy *sortby = lfirst(olitem);
994                 TargetEntry *tle;
995
996                 tle = findTargetlistEntry(pstate, sortby->node,
997                                                                   targetlist, ORDER_CLAUSE);
998
999                 sortlist = addTargetToSortList(tle, sortlist, targetlist,
1000                                                                            sortby->useOp);
1001         }
1002
1003         return sortlist;
1004 }
1005
1006 /*
1007  * transformDistinctClause -
1008  *        transform a DISTINCT or DISTINCT ON clause
1009  *
1010  * Since we may need to add items to the query's sortClause list, that list
1011  * is passed by reference.      We might also need to add items to the query's
1012  * targetlist, but we assume that cannot be empty initially, so we can
1013  * lappend to it even though the pointer is passed by value.
1014  */
1015 List *
1016 transformDistinctClause(ParseState *pstate, List *distinctlist,
1017                                                 List *targetlist, List **sortClause)
1018 {
1019         List       *result = NIL;
1020         List       *slitem;
1021         List       *dlitem;
1022
1023         /* No work if there was no DISTINCT clause */
1024         if (distinctlist == NIL)
1025                 return NIL;
1026
1027         if (lfirst(distinctlist) == NIL)
1028         {
1029                 /* We had SELECT DISTINCT */
1030
1031                 /*
1032                  * All non-resjunk elements from target list that are not already
1033                  * in the sort list should be added to it.      (We don't really care
1034                  * what order the DISTINCT fields are checked in, so we can leave
1035                  * the user's ORDER BY spec alone, and just add additional sort
1036                  * keys to it to ensure that all targetlist items get sorted.)
1037                  */
1038                 *sortClause = addAllTargetsToSortList(*sortClause, targetlist);
1039
1040                 /*
1041                  * Now, DISTINCT list consists of all non-resjunk sortlist items.
1042                  * Actually, all the sortlist items had better be non-resjunk!
1043                  * Otherwise, user wrote SELECT DISTINCT with an ORDER BY item
1044                  * that does not appear anywhere in the SELECT targetlist, and we
1045                  * can't implement that with only one sorting pass...
1046                  */
1047                 foreach(slitem, *sortClause)
1048                 {
1049                         SortClause *scl = (SortClause *) lfirst(slitem);
1050                         TargetEntry *tle = get_sortgroupclause_tle(scl, targetlist);
1051
1052                         if (tle->resdom->resjunk)
1053                                 elog(ERROR, "For SELECT DISTINCT, ORDER BY expressions must appear in target list");
1054                         else
1055                                 result = lappend(result, copyObject(scl));
1056                 }
1057         }
1058         else
1059         {
1060                 /* We had SELECT DISTINCT ON (expr, ...) */
1061
1062                 /*
1063                  * If the user writes both DISTINCT ON and ORDER BY, then the two
1064                  * expression lists must match (until one or the other runs out).
1065                  * Otherwise the ORDER BY requires a different sort order than the
1066                  * DISTINCT does, and we can't implement that with only one sort
1067                  * pass (and if we do two passes, the results will be rather
1068                  * unpredictable). However, it's OK to have more DISTINCT ON
1069                  * expressions than ORDER BY expressions; we can just add the
1070                  * extra DISTINCT values to the sort list, much as we did above
1071                  * for ordinary DISTINCT fields.
1072                  *
1073                  * Actually, it'd be OK for the common prefixes of the two lists to
1074                  * match in any order, but implementing that check seems like more
1075                  * trouble than it's worth.
1076                  */
1077                 List       *nextsortlist = *sortClause;
1078
1079                 foreach(dlitem, distinctlist)
1080                 {
1081                         TargetEntry *tle;
1082
1083                         tle = findTargetlistEntry(pstate, lfirst(dlitem),
1084                                                                           targetlist, DISTINCT_ON_CLAUSE);
1085
1086                         if (nextsortlist != NIL)
1087                         {
1088                                 SortClause *scl = (SortClause *) lfirst(nextsortlist);
1089
1090                                 if (tle->resdom->ressortgroupref != scl->tleSortGroupRef)
1091                                         elog(ERROR, "SELECT DISTINCT ON expressions must match initial ORDER BY expressions");
1092                                 result = lappend(result, copyObject(scl));
1093                                 nextsortlist = lnext(nextsortlist);
1094                         }
1095                         else
1096                         {
1097                                 *sortClause = addTargetToSortList(tle, *sortClause,
1098                                                                                                   targetlist, NULL);
1099
1100                                 /*
1101                                  * Probably, the tle should always have been added at the
1102                                  * end of the sort list ... but search to be safe.
1103                                  */
1104                                 foreach(slitem, *sortClause)
1105                                 {
1106                                         SortClause *scl = (SortClause *) lfirst(slitem);
1107
1108                                         if (tle->resdom->ressortgroupref == scl->tleSortGroupRef)
1109                                         {
1110                                                 result = lappend(result, copyObject(scl));
1111                                                 break;
1112                                         }
1113                                 }
1114                                 if (slitem == NIL)
1115                                         elog(ERROR, "transformDistinctClause: failed to add DISTINCT ON clause to target list");
1116                         }
1117                 }
1118         }
1119
1120         return result;
1121 }
1122
1123 /*
1124  * addAllTargetsToSortList
1125  *              Make sure all non-resjunk targets in the targetlist are in the
1126  *              ORDER BY list, adding the not-yet-sorted ones to the end of the list.
1127  *              This is typically used to help implement SELECT DISTINCT.
1128  *
1129  * Returns the updated ORDER BY list.
1130  */
1131 List *
1132 addAllTargetsToSortList(List *sortlist, List *targetlist)
1133 {
1134         List       *i;
1135
1136         foreach(i, targetlist)
1137         {
1138                 TargetEntry *tle = (TargetEntry *) lfirst(i);
1139
1140                 if (!tle->resdom->resjunk)
1141                         sortlist = addTargetToSortList(tle, sortlist, targetlist, NULL);
1142         }
1143         return sortlist;
1144 }
1145
1146 /*
1147  * addTargetToSortList
1148  *              If the given targetlist entry isn't already in the ORDER BY list,
1149  *              add it to the end of the list, using the sortop with given name
1150  *              or any available sort operator if opname == NULL.
1151  *
1152  * Returns the updated ORDER BY list.
1153  */
1154 static List *
1155 addTargetToSortList(TargetEntry *tle, List *sortlist, List *targetlist,
1156                                         char *opname)
1157 {
1158         /* avoid making duplicate sortlist entries */
1159         if (!exprIsInSortList(tle->expr, sortlist, targetlist))
1160         {
1161                 SortClause *sortcl = makeNode(SortClause);
1162
1163                 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
1164
1165                 if (opname)
1166                         sortcl->sortop = compatible_oper_opid(opname,
1167                                                                                                   tle->resdom->restype,
1168                                                                                                   tle->resdom->restype,
1169                                                                                                   false);
1170                 else
1171                         sortcl->sortop = any_ordering_op(tle->resdom->restype);
1172
1173                 sortlist = lappend(sortlist, sortcl);
1174         }
1175         return sortlist;
1176 }
1177
1178 /*
1179  * assignSortGroupRef
1180  *        Assign the targetentry an unused ressortgroupref, if it doesn't
1181  *        already have one.  Return the assigned or pre-existing refnumber.
1182  *
1183  * 'tlist' is the targetlist containing (or to contain) the given targetentry.
1184  */
1185 Index
1186 assignSortGroupRef(TargetEntry *tle, List *tlist)
1187 {
1188         Index           maxRef;
1189         List       *l;
1190
1191         if (tle->resdom->ressortgroupref)       /* already has one? */
1192                 return tle->resdom->ressortgroupref;
1193
1194         /* easiest way to pick an unused refnumber: max used + 1 */
1195         maxRef = 0;
1196         foreach(l, tlist)
1197         {
1198                 Index           ref = ((TargetEntry *) lfirst(l))->resdom->ressortgroupref;
1199
1200                 if (ref > maxRef)
1201                         maxRef = ref;
1202         }
1203         tle->resdom->ressortgroupref = maxRef + 1;
1204         return tle->resdom->ressortgroupref;
1205 }
1206
1207 /*
1208  * exprIsInSortList
1209  *              Is the given expression already in the sortlist?
1210  *              Note we will say 'yes' if it is equal() to any sortlist item,
1211  *              even though that might be a different targetlist member.
1212  *
1213  * Works for both SortClause and GroupClause lists.
1214  */
1215 static bool
1216 exprIsInSortList(Node *expr, List *sortList, List *targetList)
1217 {
1218         List       *i;
1219
1220         foreach(i, sortList)
1221         {
1222                 SortClause *scl = (SortClause *) lfirst(i);
1223
1224                 if (equal(expr, get_sortgroupclause_expr(scl, targetList)))
1225                         return true;
1226         }
1227         return false;
1228 }