]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/planner.c
Cause inheritance patch to meet minimum coding standards (no gcc
[postgresql] / src / backend / optimizer / plan / planner.c
1 /*-------------------------------------------------------------------------
2  *
3  * planner.c
4  *        The query optimizer external interface.
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.82 2000/06/09 03:17:13 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include <sys/types.h>
16
17 #include "postgres.h"
18
19 #include "access/heapam.h"
20 #include "catalog/pg_type.h"
21 #include "executor/executor.h"
22 #include "nodes/makefuncs.h"
23 #include "optimizer/clauses.h"
24 #include "optimizer/internal.h"
25 #include "optimizer/paths.h"
26 #include "optimizer/plancat.h"
27 #include "optimizer/planmain.h"
28 #include "optimizer/planner.h"
29 #include "optimizer/prep.h"
30 #include "optimizer/subselect.h"
31 #include "optimizer/tlist.h"
32 #include "optimizer/var.h"
33 #include "parser/parse_expr.h"
34 #include "utils/lsyscache.h"
35
36
37 static List *make_subplanTargetList(Query *parse, List *tlist,
38                                            AttrNumber **groupColIdx);
39 static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup,
40                            List *groupClause, AttrNumber *grpColIdx,
41                            bool is_presorted, Plan *subplan);
42 static Plan *make_sortplan(List *tlist, List *sortcls, Plan *plannode);
43
44 /*****************************************************************************
45  *
46  *         Query optimizer entry point
47  *
48  *****************************************************************************/
49 Plan *
50 planner(Query *parse)
51 {
52         Plan       *result_plan;
53
54         /* Initialize state for subselects */
55         PlannerQueryLevel = 1;
56         PlannerInitPlan = NULL;
57         PlannerParamVar = NULL;
58         PlannerPlanId = 0;
59
60         /* this should go away sometime soon */
61         transformKeySetQuery(parse);
62
63         /* primary planning entry point (may recurse for subplans) */
64         result_plan = subquery_planner(parse, -1.0 /* default case */ );
65
66         Assert(PlannerQueryLevel == 1);
67
68         /* if top-level query had subqueries, do housekeeping for them */
69         if (PlannerPlanId > 0)
70         {
71                 (void) SS_finalize_plan(result_plan);
72                 result_plan->initPlan = PlannerInitPlan;
73         }
74
75         /* executor wants to know total number of Params used overall */
76         result_plan->nParamExec = length(PlannerParamVar);
77
78         /* final cleanup of the plan */
79         set_plan_references(result_plan);
80
81         return result_plan;
82 }
83
84
85 /*--------------------
86  * subquery_planner
87  *        Invokes the planner on a subquery.  We recurse to here for each
88  *        sub-SELECT found in the query tree.
89  *
90  * parse is the querytree produced by the parser & rewriter.
91  * tuple_fraction is the fraction of tuples we expect will be retrieved.
92  * tuple_fraction is interpreted as explained for union_planner, below.
93  *
94  * Basically, this routine does the stuff that should only be done once
95  * per Query object.  It then calls union_planner, which may be called
96  * recursively on the same Query node in order to handle UNIONs and/or
97  * inheritance.  subquery_planner is called recursively from subselect.c.
98  *
99  * prepunion.c uses an unholy combination of calling union_planner when
100  * recursing on the primary Query node, or subquery_planner when recursing
101  * on a UNION'd Query node that hasn't previously been seen by
102  * subquery_planner.  That whole chunk of code needs rewritten from scratch.
103  *
104  * Returns a query plan.
105  *--------------------
106  */
107 Plan *
108 subquery_planner(Query *parse, double tuple_fraction)
109 {
110     List       *l;
111         List       *rangetable = parse->rtable;
112     RangeTblEntry *rangeTblEntry;
113
114         /*
115          * A HAVING clause without aggregates is equivalent to a WHERE clause
116          * (except it can only refer to grouped fields).  If there are no aggs
117          * anywhere in the query, then we don't want to create an Agg plan
118          * node, so merge the HAVING condition into WHERE.      (We used to
119          * consider this an error condition, but it seems to be legal SQL.)
120          */
121         if (parse->havingQual != NULL && !parse->hasAggs)
122         {
123                 if (parse->qual == NULL)
124                         parse->qual = parse->havingQual;
125                 else
126                         parse->qual = (Node *) make_andclause(lappend(lcons(parse->qual,
127                                                                                                                                 NIL),
128                                                                                                          parse->havingQual));
129                 parse->havingQual = NULL;
130         }
131
132         /*
133          * Simplify constant expressions in targetlist and quals.
134          *
135          * Note that at this point the qual has not yet been converted to
136          * implicit-AND form, so we can apply eval_const_expressions directly.
137          * Also note that we need to do this before SS_process_sublinks,
138          * because that routine inserts bogus "Const" nodes.
139          */
140         parse->targetList = (List *)
141                 eval_const_expressions((Node *) parse->targetList);
142         parse->qual = eval_const_expressions(parse->qual);
143         parse->havingQual = eval_const_expressions(parse->havingQual);
144
145     /*
146      * If the query is going to look for subclasses, but no subclasses
147      * actually exist, then we can optimise away the union that would
148      * otherwise happen and thus save some time.
149     */
150     foreach(l, rangetable)
151         {
152            rangeTblEntry  = (RangeTblEntry *)lfirst(l);
153            if (rangeTblEntry->inh && !has_subclass(rangeTblEntry->relid))
154              rangeTblEntry->inh = FALSE;
155         }
156
157         /*
158          * Canonicalize the qual, and convert it to implicit-AND format.
159          *
160          * XXX Is there any value in re-applying eval_const_expressions after
161          * canonicalize_qual?
162          */
163         parse->qual = (Node *) canonicalize_qual((Expr *) parse->qual, true);
164 #ifdef OPTIMIZER_DEBUG
165         printf("After canonicalize_qual()\n");
166         pprint(parse->qual);
167 #endif
168
169         /*
170          * Ditto for the havingQual
171          */
172         parse->havingQual = (Node *) canonicalize_qual((Expr *) parse->havingQual,
173                                                                                                    true);
174
175         /* Expand SubLinks to SubPlans */
176         if (parse->hasSubLinks)
177         {
178                 parse->targetList = (List *)
179                         SS_process_sublinks((Node *) parse->targetList);
180                 parse->qual = SS_process_sublinks(parse->qual);
181                 parse->havingQual = SS_process_sublinks(parse->havingQual);
182
183                 if (parse->groupClause != NIL)
184                 {
185
186                         /*
187                          * Check for ungrouped variables passed to subplans. Note we
188                          * do NOT do this for subplans in WHERE; it's legal there
189                          * because WHERE is evaluated pre-GROUP.
190                          *
191                          * An interesting fine point: if we reassigned a HAVING qual into
192                          * WHERE above, then we will accept references to ungrouped
193                          * vars from subplans in the HAVING qual.  This is not
194                          * entirely consistent, but it doesn't seem particularly
195                          * harmful...
196                          */
197                         check_subplans_for_ungrouped_vars((Node *) parse->targetList,
198                                                                                           parse);
199                         check_subplans_for_ungrouped_vars(parse->havingQual, parse);
200                 }
201         }
202
203         /* Replace uplevel vars with Param nodes */
204         if (PlannerQueryLevel > 1)
205         {
206                 parse->targetList = (List *)
207                         SS_replace_correlation_vars((Node *) parse->targetList);
208                 parse->qual = SS_replace_correlation_vars(parse->qual);
209                 parse->havingQual = SS_replace_correlation_vars(parse->havingQual);
210         }
211
212         /* Do the main planning (potentially recursive) */
213
214         return union_planner(parse, tuple_fraction);
215
216         /*
217          * XXX should any more of union_planner's activity be moved here?
218          *
219          * That would take careful study of the interactions with prepunion.c,
220          * but I suspect it would pay off in simplicity and avoidance of
221          * wasted cycles.
222          */
223 }
224
225
226 /*--------------------
227  * union_planner
228  *        Invokes the planner on union-type queries (both regular UNIONs and
229  *        appends produced by inheritance), recursing if necessary to get them
230  *        all, then processes normal plans.
231  *
232  * parse is the querytree produced by the parser & rewriter.
233  * tuple_fraction is the fraction of tuples we expect will be retrieved
234  *
235  * tuple_fraction is interpreted as follows:
236  *        < 0: determine fraction by inspection of query (normal case)
237  *        0: expect all tuples to be retrieved
238  *        0 < tuple_fraction < 1: expect the given fraction of tuples available
239  *              from the plan to be retrieved
240  *        tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
241  *              expected to be retrieved (ie, a LIMIT specification)
242  * The normal case is to pass -1, but some callers pass values >= 0 to
243  * override this routine's determination of the appropriate fraction.
244  *
245  * Returns a query plan.
246  *--------------------
247  */
248 Plan *
249 union_planner(Query *parse,
250                           double tuple_fraction)
251 {
252         List       *tlist = parse->targetList;
253         List       *rangetable = parse->rtable;
254         Plan       *result_plan = (Plan *) NULL;
255         AttrNumber *groupColIdx = NULL;
256         List       *current_pathkeys = NIL;
257         List       *group_pathkeys;
258         List       *sort_pathkeys;
259         Index           rt_index;
260
261         if (parse->unionClause)
262         {
263                 result_plan = (Plan *) plan_union_queries(parse);
264                 /* XXX do we need to do this? bjm 12/19/97 */
265                 tlist = preprocess_targetlist(tlist,
266                                                                           parse->commandType,
267                                                                           parse->resultRelation,
268                                                                           parse->rtable);
269
270                 /*
271                  * We leave current_pathkeys NIL indicating we do not know sort
272                  * order. Actually, for a normal UNION we have done an explicit
273                  * sort; ought to change interface to plan_union_queries to pass
274                  * that info back!
275                  */
276
277                 /*
278                  * Calculate pathkeys that represent grouping/ordering
279                  * requirements
280                  */
281                 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
282                                                                                                            tlist);
283                 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
284                                                                                                           tlist);
285         }
286         else if ((rt_index = first_inherit_rt_entry(rangetable)) != -1)
287         {
288                 List       *sub_tlist;
289
290                 /*
291                  * Generate appropriate target list for subplan; may be different
292                  * from tlist if grouping or aggregation is needed.
293                  */
294                 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
295
296                 /*
297                  * Recursively plan the subqueries needed for inheritance
298                  */
299                 result_plan = (Plan *) plan_inherit_queries(parse, sub_tlist,
300                                                                                                         rt_index);
301
302                 /*
303                  * Fix up outer target list.  NOTE: unlike the case for
304                  * non-inherited query, we pass the unfixed tlist to subplans,
305                  * which do their own fixing.  But we still want to fix the outer
306                  * target list afterwards. I *think* this is correct --- doing the
307                  * fix before recursing is definitely wrong, because
308                  * preprocess_targetlist() will do the wrong thing if invoked
309                  * twice on the same list. Maybe that is a bug? tgl 6/6/99
310                  */
311                 tlist = preprocess_targetlist(tlist,
312                                                                           parse->commandType,
313                                                                           parse->resultRelation,
314                                                                           parse->rtable);
315
316                 if (parse->rowMark != NULL)
317                         elog(ERROR, "SELECT FOR UPDATE is not supported for inherit queries");
318
319                 /*
320                  * We leave current_pathkeys NIL indicating we do not know sort
321                  * order of the Append-ed results.
322                  */
323
324                 /*
325                  * Calculate pathkeys that represent grouping/ordering
326                  * requirements
327                  */
328                 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
329                                                                                                            tlist);
330                 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
331                                                                                                           tlist);
332         }
333         else
334         {
335                 List       *sub_tlist;
336
337                 /* Preprocess targetlist in case we are inside an INSERT/UPDATE. */
338                 tlist = preprocess_targetlist(tlist,
339                                                                           parse->commandType,
340                                                                           parse->resultRelation,
341                                                                           parse->rtable);
342
343                 /*
344                  * Add row-mark targets for UPDATE (should this be done in
345                  * preprocess_targetlist?)
346                  */
347                 if (parse->rowMark != NULL)
348                 {
349                         List       *l;
350
351                         foreach(l, parse->rowMark)
352                         {
353                                 RowMark    *rowmark = (RowMark *) lfirst(l);
354                                 TargetEntry *ctid;
355                                 Resdom     *resdom;
356                                 Var                *var;
357                                 char       *resname;
358
359                                 if (!(rowmark->info & ROW_MARK_FOR_UPDATE))
360                                         continue;
361
362                                 resname = (char *) palloc(32);
363                                 sprintf(resname, "ctid%u", rowmark->rti);
364                                 resdom = makeResdom(length(tlist) + 1,
365                                                                         TIDOID,
366                                                                         -1,
367                                                                         resname,
368                                                                         0,
369                                                                         0,
370                                                                         true);
371
372                                 var = makeVar(rowmark->rti, -1, TIDOID, -1, 0);
373
374                                 ctid = makeTargetEntry(resdom, (Node *) var);
375                                 tlist = lappend(tlist, ctid);
376                         }
377                 }
378
379                 /*
380                  * Generate appropriate target list for subplan; may be different
381                  * from tlist if grouping or aggregation is needed.
382                  */
383                 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
384
385                 /*
386                  * Calculate pathkeys that represent grouping/ordering
387                  * requirements
388                  */
389                 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
390                                                                                                            tlist);
391                 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
392                                                                                                           tlist);
393
394                 /*
395                  * Figure out whether we need a sorted result from query_planner.
396                  *
397                  * If we have a GROUP BY clause, then we want a result sorted
398                  * properly for grouping.  Otherwise, if there is an ORDER BY
399                  * clause, we want to sort by the ORDER BY clause.      (Note: if we
400                  * have both, and ORDER BY is a superset of GROUP BY, it would be
401                  * tempting to request sort by ORDER BY --- but that might just
402                  * leave us failing to exploit an available sort order at all.
403                  * Needs more thought...)
404                  */
405                 if (parse->groupClause)
406                         parse->query_pathkeys = group_pathkeys;
407                 else if (parse->sortClause)
408                         parse->query_pathkeys = sort_pathkeys;
409                 else
410                         parse->query_pathkeys = NIL;
411
412                 /*
413                  * Figure out whether we expect to retrieve all the tuples that
414                  * the plan can generate, or to stop early due to a LIMIT or other
415                  * factors.  If the caller passed a value >= 0, believe that
416                  * value, else do our own examination of the query context.
417                  */
418                 if (tuple_fraction < 0.0)
419                 {
420                         /* Initial assumption is we need all the tuples */
421                         tuple_fraction = 0.0;
422
423                         /*
424                          * Check for a LIMIT clause.
425                          */
426                         if (parse->limitCount != NULL)
427                         {
428                                 if (IsA(parse->limitCount, Const))
429                                 {
430                                         Const      *limitc = (Const *) parse->limitCount;
431                                         int                     count = (int) (limitc->constvalue);
432
433                                         /*
434                                          * The constant can legally be either 0 ("ALL") or a
435                                          * positive integer.  If it is not ALL, we also need
436                                          * to consider the OFFSET part of LIMIT.
437                                          */
438                                         if (count > 0)
439                                         {
440                                                 tuple_fraction = (double) count;
441                                                 if (parse->limitOffset != NULL)
442                                                 {
443                                                         if (IsA(parse->limitOffset, Const))
444                                                         {
445                                                                 int                     offset;
446
447                                                                 limitc = (Const *) parse->limitOffset;
448                                                                 offset = (int) (limitc->constvalue);
449                                                                 if (offset > 0)
450                                                                         tuple_fraction += (double) offset;
451                                                         }
452                                                         else
453                                                         {
454                                                                 /* It's a PARAM ... punt ... */
455                                                                 tuple_fraction = 0.10;
456                                                         }
457                                                 }
458                                         }
459                                 }
460                                 else
461                                 {
462
463                                         /*
464                                          * COUNT is a PARAM ... don't know exactly what the
465                                          * limit will be, but for lack of a better idea assume
466                                          * 10% of the plan's result is wanted.
467                                          */
468                                         tuple_fraction = 0.10;
469                                 }
470                         }
471
472                         /*
473                          * Check for a retrieve-into-portal, ie DECLARE CURSOR.
474                          *
475                          * We have no real idea how many tuples the user will ultimately
476                          * FETCH from a cursor, but it seems a good bet that he
477                          * doesn't want 'em all.  Optimize for 10% retrieval (you
478                          * gotta better number?)
479                          */
480                         if (parse->isPortal)
481                                 tuple_fraction = 0.10;
482                 }
483
484                 /*
485                  * Adjust tuple_fraction if we see that we are going to apply
486                  * grouping/aggregation/etc.  This is not overridable by the
487                  * caller, since it reflects plan actions that this routine will
488                  * certainly take, not assumptions about context.
489                  */
490                 if (parse->groupClause)
491                 {
492
493                         /*
494                          * In GROUP BY mode, we have the little problem that we don't
495                          * really know how many input tuples will be needed to make a
496                          * group, so we can't translate an output LIMIT count into an
497                          * input count.  For lack of a better idea, assume 25% of the
498                          * input data will be processed if there is any output limit.
499                          * However, if the caller gave us a fraction rather than an
500                          * absolute count, we can keep using that fraction (which
501                          * amounts to assuming that all the groups are about the same
502                          * size).
503                          */
504                         if (tuple_fraction >= 1.0)
505                                 tuple_fraction = 0.25;
506
507                         /*
508                          * If both GROUP BY and ORDER BY are specified, we will need
509                          * two levels of sort --- and, therefore, certainly need to
510                          * read all the input tuples --- unless ORDER BY is a subset
511                          * of GROUP BY.  (Although we are comparing non-canonicalized
512                          * pathkeys here, it should be OK since they will both contain
513                          * only single-element sublists at this point.  See
514                          * pathkeys.c.)
515                          */
516                         if (parse->groupClause && parse->sortClause &&
517                                 !pathkeys_contained_in(sort_pathkeys, group_pathkeys))
518                                 tuple_fraction = 0.0;
519                 }
520                 else if (parse->hasAggs)
521                 {
522
523                         /*
524                          * Ungrouped aggregate will certainly want all the input
525                          * tuples.
526                          */
527                         tuple_fraction = 0.0;
528                 }
529                 else if (parse->distinctClause)
530                 {
531
532                         /*
533                          * SELECT DISTINCT, like GROUP, will absorb an unpredictable
534                          * number of input tuples per output tuple.  Handle the same
535                          * way.
536                          */
537                         if (tuple_fraction >= 1.0)
538                                 tuple_fraction = 0.25;
539                 }
540
541                 /* Generate the (sub) plan */
542                 result_plan = query_planner(parse,
543                                                                         sub_tlist,
544                                                                         (List *) parse->qual,
545                                                                         tuple_fraction);
546
547                 /*
548                  * query_planner returns actual sort order (which is not
549                  * necessarily what we requested) in query_pathkeys.
550                  */
551                 current_pathkeys = parse->query_pathkeys;
552         }
553
554         /* query_planner returns NULL if it thinks plan is bogus */
555         if (!result_plan)
556                 elog(ERROR, "union_planner: failed to create plan");
557
558         /*
559          * We couldn't canonicalize group_pathkeys and sort_pathkeys before
560          * running query_planner(), so do it now.
561          */
562         group_pathkeys = canonicalize_pathkeys(parse, group_pathkeys);
563         sort_pathkeys = canonicalize_pathkeys(parse, sort_pathkeys);
564
565         /*
566          * If we have a GROUP BY clause, insert a group node (plus the
567          * appropriate sort node, if necessary).
568          */
569         if (parse->groupClause)
570         {
571                 bool            tuplePerGroup;
572                 List       *group_tlist;
573                 bool            is_sorted;
574
575                 /*
576                  * Decide whether how many tuples per group the Group node needs
577                  * to return. (Needs only one tuple per group if no aggregate is
578                  * present. Otherwise, need every tuple from the group to do the
579                  * aggregation.)  Note tuplePerGroup is named backwards :-(
580                  */
581                 tuplePerGroup = parse->hasAggs;
582
583                 /*
584                  * If there are aggregates then the Group node should just return
585                  * the same set of vars as the subplan did (but we can exclude any
586                  * GROUP BY expressions).  If there are no aggregates then the
587                  * Group node had better compute the final tlist.
588                  */
589                 if (parse->hasAggs)
590                         group_tlist = flatten_tlist(result_plan->targetlist);
591                 else
592                         group_tlist = tlist;
593
594                 /*
595                  * Figure out whether the path result is already ordered the way
596                  * we need it --- if so, no need for an explicit sort step.
597                  */
598                 if (pathkeys_contained_in(group_pathkeys, current_pathkeys))
599                 {
600                         is_sorted = true;       /* no sort needed now */
601                         /* current_pathkeys remains unchanged */
602                 }
603                 else
604                 {
605
606                         /*
607                          * We will need to do an explicit sort by the GROUP BY clause.
608                          * make_groupplan will do the work, but set current_pathkeys
609                          * to indicate the resulting order.
610                          */
611                         is_sorted = false;
612                         current_pathkeys = group_pathkeys;
613                 }
614
615                 result_plan = make_groupplan(group_tlist,
616                                                                          tuplePerGroup,
617                                                                          parse->groupClause,
618                                                                          groupColIdx,
619                                                                          is_sorted,
620                                                                          result_plan);
621         }
622
623         /*
624          * If aggregate is present, insert the Agg node
625          *
626          * HAVING clause, if any, becomes qual of the Agg node
627          */
628         if (parse->hasAggs)
629         {
630                 result_plan = (Plan *) make_agg(tlist,
631                                                                                 (List *) parse->havingQual,
632                                                                                 result_plan);
633                 /* Note: Agg does not affect any existing sort order of the tuples */
634         }
635
636         /*
637          * If we were not able to make the plan come out in the right order,
638          * add an explicit sort step.
639          */
640         if (parse->sortClause)
641         {
642                 if (!pathkeys_contained_in(sort_pathkeys, current_pathkeys))
643                         result_plan = make_sortplan(tlist, parse->sortClause, result_plan);
644         }
645
646         /*
647          * Finally, if there is a DISTINCT clause, add the UNIQUE node.
648          */
649         if (parse->distinctClause)
650         {
651                 result_plan = (Plan *) make_unique(tlist, result_plan,
652                                                                                    parse->distinctClause);
653         }
654
655         return result_plan;
656 }
657
658 /*---------------
659  * make_subplanTargetList
660  *        Generate appropriate target list when grouping is required.
661  *
662  * When union_planner inserts Aggregate and/or Group plan nodes above
663  * the result of query_planner, we typically want to pass a different
664  * target list to query_planner than the outer plan nodes should have.
665  * This routine generates the correct target list for the subplan.
666  *
667  * The initial target list passed from the parser already contains entries
668  * for all ORDER BY and GROUP BY expressions, but it will not have entries
669  * for variables used only in HAVING clauses; so we need to add those
670  * variables to the subplan target list.  Also, if we are doing either
671  * grouping or aggregation, we flatten all expressions except GROUP BY items
672  * into their component variables; the other expressions will be computed by
673  * the inserted nodes rather than by the subplan.  For example,
674  * given a query like
675  *              SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
676  * we want to pass this targetlist to the subplan:
677  *              a,b,c,d,a+b
678  * where the a+b target will be used by the Sort/Group steps, and the
679  * other targets will be used for computing the final results.  (In the
680  * above example we could theoretically suppress the a and b targets and
681  * use only a+b, but it's not really worth the trouble.)
682  *
683  * 'parse' is the query being processed.
684  * 'tlist' is the query's target list.
685  * 'groupColIdx' receives an array of column numbers for the GROUP BY
686  * expressions (if there are any) in the subplan's target list.
687  *
688  * The result is the targetlist to be passed to the subplan.
689  *---------------
690  */
691 static List *
692 make_subplanTargetList(Query *parse,
693                                            List *tlist,
694                                            AttrNumber **groupColIdx)
695 {
696         List       *sub_tlist;
697         List       *extravars;
698         int                     numCols;
699
700         *groupColIdx = NULL;
701
702         /*
703          * If we're not grouping or aggregating, nothing to do here;
704          * query_planner should receive the unmodified target list.
705          */
706         if (!parse->hasAggs && !parse->groupClause && !parse->havingQual)
707                 return tlist;
708
709         /*
710          * Otherwise, start with a "flattened" tlist (having just the vars
711          * mentioned in the targetlist and HAVING qual --- but not upper-
712          * level Vars; they will be replaced by Params later on).
713          */
714         sub_tlist = flatten_tlist(tlist);
715         extravars = pull_var_clause(parse->havingQual, false);
716         sub_tlist = add_to_flat_tlist(sub_tlist, extravars);
717         freeList(extravars);
718
719         /*
720          * If grouping, create sub_tlist entries for all GROUP BY expressions
721          * (GROUP BY items that are simple Vars should be in the list
722          * already), and make an array showing where the group columns are in
723          * the sub_tlist.
724          */
725         numCols = length(parse->groupClause);
726         if (numCols > 0)
727         {
728                 int                     keyno = 0;
729                 AttrNumber *grpColIdx;
730                 List       *gl;
731
732                 grpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
733                 *groupColIdx = grpColIdx;
734
735                 foreach(gl, parse->groupClause)
736                 {
737                         GroupClause *grpcl = (GroupClause *) lfirst(gl);
738                         Node       *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
739                         TargetEntry *te = NULL;
740                         List       *sl;
741
742                         /* Find or make a matching sub_tlist entry */
743                         foreach(sl, sub_tlist)
744                         {
745                                 te = (TargetEntry *) lfirst(sl);
746                                 if (equal(groupexpr, te->expr))
747                                         break;
748                         }
749                         if (!sl)
750                         {
751                                 te = makeTargetEntry(makeResdom(length(sub_tlist) + 1,
752                                                                                                 exprType(groupexpr),
753                                                                                                 exprTypmod(groupexpr),
754                                                                                                 NULL,
755                                                                                                 (Index) 0,
756                                                                                                 (Oid) 0,
757                                                                                                 false),
758                                                                          groupexpr);
759                                 sub_tlist = lappend(sub_tlist, te);
760                         }
761
762                         /* and save its resno */
763                         grpColIdx[keyno++] = te->resdom->resno;
764                 }
765         }
766
767         return sub_tlist;
768 }
769
770 /*
771  * make_groupplan
772  *              Add a Group node for GROUP BY processing.
773  *              If we couldn't make the subplan produce presorted output for grouping,
774  *              first add an explicit Sort node.
775  */
776 static Plan *
777 make_groupplan(List *group_tlist,
778                            bool tuplePerGroup,
779                            List *groupClause,
780                            AttrNumber *grpColIdx,
781                            bool is_presorted,
782                            Plan *subplan)
783 {
784         int                     numCols = length(groupClause);
785
786         if (!is_presorted)
787         {
788
789                 /*
790                  * The Sort node always just takes a copy of the subplan's tlist
791                  * plus ordering information.  (This might seem inefficient if the
792                  * subplan contains complex GROUP BY expressions, but in fact Sort
793                  * does not evaluate its targetlist --- it only outputs the same
794                  * tuples in a new order.  So the expressions we might be copying
795                  * are just dummies with no extra execution cost.)
796                  */
797                 List       *sort_tlist = new_unsorted_tlist(subplan->targetlist);
798                 int                     keyno = 0;
799                 List       *gl;
800
801                 foreach(gl, groupClause)
802                 {
803                         GroupClause *grpcl = (GroupClause *) lfirst(gl);
804                         TargetEntry *te = nth(grpColIdx[keyno] - 1, sort_tlist);
805                         Resdom     *resdom = te->resdom;
806
807                         /*
808                          * Check for the possibility of duplicate group-by clauses ---
809                          * the parser should have removed 'em, but the Sort executor
810                          * will get terribly confused if any get through!
811                          */
812                         if (resdom->reskey == 0)
813                         {
814                                 /* OK, insert the ordering info needed by the executor. */
815                                 resdom->reskey = ++keyno;
816                                 resdom->reskeyop = get_opcode(grpcl->sortop);
817                         }
818                 }
819
820                 subplan = (Plan *) make_sort(sort_tlist,
821                                                                          _NONAME_RELATION_ID_,
822                                                                          subplan,
823                                                                          keyno);
824         }
825
826         return (Plan *) make_group(group_tlist, tuplePerGroup, numCols,
827                                                            grpColIdx, subplan);
828 }
829
830 /*
831  * make_sortplan
832  *        Add a Sort node to implement an explicit ORDER BY clause.
833  */
834 static Plan *
835 make_sortplan(List *tlist, List *sortcls, Plan *plannode)
836 {
837         List       *temp_tlist;
838         List       *i;
839         int                     keyno = 0;
840
841         /*
842          * First make a copy of the tlist so that we don't corrupt the
843          * original.
844          */
845
846         temp_tlist = new_unsorted_tlist(tlist);
847
848         foreach(i, sortcls)
849         {
850                 SortClause *sortcl = (SortClause *) lfirst(i);
851                 TargetEntry *tle = get_sortgroupclause_tle(sortcl, temp_tlist);
852                 Resdom     *resdom = tle->resdom;
853
854                 /*
855                  * Check for the possibility of duplicate order-by clauses --- the
856                  * parser should have removed 'em, but the executor will get
857                  * terribly confused if any get through!
858                  */
859                 if (resdom->reskey == 0)
860                 {
861                         /* OK, insert the ordering info needed by the executor. */
862                         resdom->reskey = ++keyno;
863                         resdom->reskeyop = get_opcode(sortcl->sortop);
864                 }
865         }
866
867         return (Plan *) make_sort(temp_tlist,
868                                                           _NONAME_RELATION_ID_,
869                                                           plannode,
870                                                           keyno);
871 }
872
873 /*
874  * pg_checkretval() -- check return value of a list of sql parse
875  *                                              trees.
876  *
877  * The return value of a sql function is the value returned by
878  * the final query in the function.  We do some ad-hoc define-time
879  * type checking here to be sure that the user is returning the
880  * type he claims.
881  *
882  * XXX Why is this function in this module?
883  */
884 void
885 pg_checkretval(Oid rettype, List *queryTreeList)
886 {
887         Query      *parse;
888         List       *tlist;
889         List       *rt;
890         int                     cmd;
891         Type            typ;
892         Resdom     *resnode;
893         Relation        reln;
894         Oid                     relid;
895         int                     relnatts;
896         int                     i;
897
898         /* find the final query */
899         parse = (Query *) nth(length(queryTreeList) - 1, queryTreeList);
900
901         /*
902          * test 1:      if the last query is a utility invocation, then there had
903          * better not be a return value declared.
904          */
905         if (parse->commandType == CMD_UTILITY)
906         {
907                 if (rettype == InvalidOid)
908                         return;
909                 else
910                         elog(ERROR, "return type mismatch in function decl: final query is a catalog utility");
911         }
912
913         /* okay, it's an ordinary query */
914         tlist = parse->targetList;
915         rt = parse->rtable;
916         cmd = parse->commandType;
917
918         /*
919          * test 2:      if the function is declared to return no value, then the
920          * final query had better not be a retrieve.
921          */
922         if (rettype == InvalidOid)
923         {
924                 if (cmd == CMD_SELECT)
925                         elog(ERROR,
926                                  "function declared with no return type, but final query is a retrieve");
927                 else
928                         return;
929         }
930
931         /* by here, the function is declared to return some type */
932         if ((typ = typeidType(rettype)) == NULL)
933                 elog(ERROR, "can't find return type %u for function\n", rettype);
934
935         /*
936          * test 3:      if the function is declared to return a value, then the
937          * final query had better be a retrieve.
938          */
939         if (cmd != CMD_SELECT)
940                 elog(ERROR, "function declared to return type %s, but final query is not a retrieve", typeTypeName(typ));
941
942         /*
943          * test 4:      for base type returns, the target list should have exactly
944          * one entry, and its type should agree with what the user declared.
945          */
946
947         if (typeTypeRelid(typ) == InvalidOid)
948         {
949                 if (ExecTargetListLength(tlist) > 1)
950                         elog(ERROR, "function declared to return %s returns multiple values in final retrieve", typeTypeName(typ));
951
952                 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
953                 if (resnode->restype != rettype)
954                         elog(ERROR, "return type mismatch in function: declared to return %s, returns %s", typeTypeName(typ), typeidTypeName(resnode->restype));
955
956                 /* by here, base return types match */
957                 return;
958         }
959
960         /*
961          * If the target list is of length 1, and the type of the varnode in
962          * the target list is the same as the declared return type, this is
963          * okay.  This can happen, for example, where the body of the function
964          * is 'retrieve (x = func2())', where func2 has the same return type
965          * as the function that's calling it.
966          */
967         if (ExecTargetListLength(tlist) == 1)
968         {
969                 resnode = (Resdom *) ((TargetEntry *) lfirst(tlist))->resdom;
970                 if (resnode->restype == rettype)
971                         return;
972         }
973
974         /*
975          * By here, the procedure returns a (set of) tuples.  This part of the
976          * typechecking is a hack.      We look up the relation that is the
977          * declared return type, and be sure that attributes 1 .. n in the
978          * target list match the declared types.
979          */
980         reln = heap_open(typeTypeRelid(typ), AccessShareLock);
981         relid = reln->rd_id;
982         relnatts = reln->rd_rel->relnatts;
983
984         if (ExecTargetListLength(tlist) != relnatts)
985                 elog(ERROR, "function declared to return type %s does not retrieve (%s.*)", typeTypeName(typ), typeTypeName(typ));
986
987         /* expect attributes 1 .. n in order */
988         for (i = 1; i <= relnatts; i++)
989         {
990                 TargetEntry *tle = lfirst(tlist);
991                 Node       *thenode = tle->expr;
992                 Oid                     tletype = exprType(thenode);
993
994                 if (tletype != reln->rd_att->attrs[i - 1]->atttypid)
995                         elog(ERROR, "function declared to return type %s does not retrieve (%s.all)", typeTypeName(typ), typeTypeName(typ));
996                 tlist = lnext(tlist);
997         }
998
999         heap_close(reln, AccessShareLock);
1000 }