]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/util/tlist.c
Refactor planner's header files.
[postgresql] / src / backend / optimizer / util / tlist.c
1 /*-------------------------------------------------------------------------
2  *
3  * tlist.c
4  *        Target list manipulation routines
5  *
6  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/optimizer/util/tlist.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "nodes/makefuncs.h"
18 #include "nodes/nodeFuncs.h"
19 #include "optimizer/cost.h"
20 #include "optimizer/optimizer.h"
21 #include "optimizer/tlist.h"
22
23
24 /* Test if an expression node represents a SRF call.  Beware multiple eval! */
25 #define IS_SRF_CALL(node) \
26         ((IsA(node, FuncExpr) && ((FuncExpr *) (node))->funcretset) || \
27          (IsA(node, OpExpr) && ((OpExpr *) (node))->opretset))
28
29 /*
30  * Data structures for split_pathtarget_at_srfs().  To preserve the identity
31  * of sortgroupref items even if they are textually equal(), what we track is
32  * not just bare expressions but expressions plus their sortgroupref indexes.
33  */
34 typedef struct
35 {
36         Node       *expr;                       /* some subexpression of a PathTarget */
37         Index           sortgroupref;   /* its sortgroupref, or 0 if none */
38 } split_pathtarget_item;
39
40 typedef struct
41 {
42         /* This is a List of bare expressions: */
43         List       *input_target_exprs; /* exprs available from input */
44         /* These are Lists of Lists of split_pathtarget_items: */
45         List       *level_srfs;         /* SRF exprs to evaluate at each level */
46         List       *level_input_vars;   /* input vars needed at each level */
47         List       *level_input_srfs;   /* input SRFs needed at each level */
48         /* These are Lists of split_pathtarget_items: */
49         List       *current_input_vars; /* vars needed in current subexpr */
50         List       *current_input_srfs; /* SRFs needed in current subexpr */
51         /* Auxiliary data for current split_pathtarget_walker traversal: */
52         int                     current_depth;  /* max SRF depth in current subexpr */
53         Index           current_sgref;  /* current subexpr's sortgroupref, or 0 */
54 } split_pathtarget_context;
55
56 static bool split_pathtarget_walker(Node *node,
57                                                 split_pathtarget_context *context);
58 static void add_sp_item_to_pathtarget(PathTarget *target,
59                                                   split_pathtarget_item *item);
60 static void add_sp_items_to_pathtarget(PathTarget *target, List *items);
61
62
63 /*****************************************************************************
64  *              Target list creation and searching utilities
65  *****************************************************************************/
66
67 /*
68  * tlist_member
69  *        Finds the (first) member of the given tlist whose expression is
70  *        equal() to the given expression.  Result is NULL if no such member.
71  */
72 TargetEntry *
73 tlist_member(Expr *node, List *targetlist)
74 {
75         ListCell   *temp;
76
77         foreach(temp, targetlist)
78         {
79                 TargetEntry *tlentry = (TargetEntry *) lfirst(temp);
80
81                 if (equal(node, tlentry->expr))
82                         return tlentry;
83         }
84         return NULL;
85 }
86
87 /*
88  * tlist_member_ignore_relabel
89  *        Same as above, except that we ignore top-level RelabelType nodes
90  *        while checking for a match.  This is needed for some scenarios
91  *        involving binary-compatible sort operations.
92  */
93 TargetEntry *
94 tlist_member_ignore_relabel(Expr *node, List *targetlist)
95 {
96         ListCell   *temp;
97
98         while (node && IsA(node, RelabelType))
99                 node = ((RelabelType *) node)->arg;
100
101         foreach(temp, targetlist)
102         {
103                 TargetEntry *tlentry = (TargetEntry *) lfirst(temp);
104                 Expr       *tlexpr = tlentry->expr;
105
106                 while (tlexpr && IsA(tlexpr, RelabelType))
107                         tlexpr = ((RelabelType *) tlexpr)->arg;
108
109                 if (equal(node, tlexpr))
110                         return tlentry;
111         }
112         return NULL;
113 }
114
115 /*
116  * tlist_member_match_var
117  *        Same as above, except that we match the provided Var on the basis
118  *        of varno/varattno/varlevelsup/vartype only, rather than full equal().
119  *
120  * This is needed in some cases where we can't be sure of an exact typmod
121  * match.  For safety, though, we insist on vartype match.
122  */
123 static TargetEntry *
124 tlist_member_match_var(Var *var, List *targetlist)
125 {
126         ListCell   *temp;
127
128         foreach(temp, targetlist)
129         {
130                 TargetEntry *tlentry = (TargetEntry *) lfirst(temp);
131                 Var                *tlvar = (Var *) tlentry->expr;
132
133                 if (!tlvar || !IsA(tlvar, Var))
134                         continue;
135                 if (var->varno == tlvar->varno &&
136                         var->varattno == tlvar->varattno &&
137                         var->varlevelsup == tlvar->varlevelsup &&
138                         var->vartype == tlvar->vartype)
139                         return tlentry;
140         }
141         return NULL;
142 }
143
144 /*
145  * add_to_flat_tlist
146  *              Add more items to a flattened tlist (if they're not already in it)
147  *
148  * 'tlist' is the flattened tlist
149  * 'exprs' is a list of expressions (usually, but not necessarily, Vars)
150  *
151  * Returns the extended tlist.
152  */
153 List *
154 add_to_flat_tlist(List *tlist, List *exprs)
155 {
156         int                     next_resno = list_length(tlist) + 1;
157         ListCell   *lc;
158
159         foreach(lc, exprs)
160         {
161                 Expr       *expr = (Expr *) lfirst(lc);
162
163                 if (!tlist_member(expr, tlist))
164                 {
165                         TargetEntry *tle;
166
167                         tle = makeTargetEntry(copyObject(expr), /* copy needed?? */
168                                                                   next_resno++,
169                                                                   NULL,
170                                                                   false);
171                         tlist = lappend(tlist, tle);
172                 }
173         }
174         return tlist;
175 }
176
177
178 /*
179  * get_tlist_exprs
180  *              Get just the expression subtrees of a tlist
181  *
182  * Resjunk columns are ignored unless includeJunk is true
183  */
184 List *
185 get_tlist_exprs(List *tlist, bool includeJunk)
186 {
187         List       *result = NIL;
188         ListCell   *l;
189
190         foreach(l, tlist)
191         {
192                 TargetEntry *tle = (TargetEntry *) lfirst(l);
193
194                 if (tle->resjunk && !includeJunk)
195                         continue;
196
197                 result = lappend(result, tle->expr);
198         }
199         return result;
200 }
201
202
203 /*
204  * count_nonjunk_tlist_entries
205  *              What it says ...
206  */
207 int
208 count_nonjunk_tlist_entries(List *tlist)
209 {
210         int                     len = 0;
211         ListCell   *l;
212
213         foreach(l, tlist)
214         {
215                 TargetEntry *tle = (TargetEntry *) lfirst(l);
216
217                 if (!tle->resjunk)
218                         len++;
219         }
220         return len;
221 }
222
223
224 /*
225  * tlist_same_exprs
226  *              Check whether two target lists contain the same expressions
227  *
228  * Note: this function is used to decide whether it's safe to jam a new tlist
229  * into a non-projection-capable plan node.  Obviously we can't do that unless
230  * the node's tlist shows it already returns the column values we want.
231  * However, we can ignore the TargetEntry attributes resname, ressortgroupref,
232  * resorigtbl, resorigcol, and resjunk, because those are only labelings that
233  * don't affect the row values computed by the node.  (Moreover, if we didn't
234  * ignore them, we'd frequently fail to make the desired optimization, since
235  * the planner tends to not bother to make resname etc. valid in intermediate
236  * plan nodes.)  Note that on success, the caller must still jam the desired
237  * tlist into the plan node, else it won't have the desired labeling fields.
238  */
239 bool
240 tlist_same_exprs(List *tlist1, List *tlist2)
241 {
242         ListCell   *lc1,
243                            *lc2;
244
245         if (list_length(tlist1) != list_length(tlist2))
246                 return false;                   /* not same length, so can't match */
247
248         forboth(lc1, tlist1, lc2, tlist2)
249         {
250                 TargetEntry *tle1 = (TargetEntry *) lfirst(lc1);
251                 TargetEntry *tle2 = (TargetEntry *) lfirst(lc2);
252
253                 if (!equal(tle1->expr, tle2->expr))
254                         return false;
255         }
256
257         return true;
258 }
259
260
261 /*
262  * Does tlist have same output datatypes as listed in colTypes?
263  *
264  * Resjunk columns are ignored if junkOK is true; otherwise presence of
265  * a resjunk column will always cause a 'false' result.
266  *
267  * Note: currently no callers care about comparing typmods.
268  */
269 bool
270 tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
271 {
272         ListCell   *l;
273         ListCell   *curColType = list_head(colTypes);
274
275         foreach(l, tlist)
276         {
277                 TargetEntry *tle = (TargetEntry *) lfirst(l);
278
279                 if (tle->resjunk)
280                 {
281                         if (!junkOK)
282                                 return false;
283                 }
284                 else
285                 {
286                         if (curColType == NULL)
287                                 return false;   /* tlist longer than colTypes */
288                         if (exprType((Node *) tle->expr) != lfirst_oid(curColType))
289                                 return false;
290                         curColType = lnext(curColType);
291                 }
292         }
293         if (curColType != NULL)
294                 return false;                   /* tlist shorter than colTypes */
295         return true;
296 }
297
298 /*
299  * Does tlist have same exposed collations as listed in colCollations?
300  *
301  * Identical logic to the above, but for collations.
302  */
303 bool
304 tlist_same_collations(List *tlist, List *colCollations, bool junkOK)
305 {
306         ListCell   *l;
307         ListCell   *curColColl = list_head(colCollations);
308
309         foreach(l, tlist)
310         {
311                 TargetEntry *tle = (TargetEntry *) lfirst(l);
312
313                 if (tle->resjunk)
314                 {
315                         if (!junkOK)
316                                 return false;
317                 }
318                 else
319                 {
320                         if (curColColl == NULL)
321                                 return false;   /* tlist longer than colCollations */
322                         if (exprCollation((Node *) tle->expr) != lfirst_oid(curColColl))
323                                 return false;
324                         curColColl = lnext(curColColl);
325                 }
326         }
327         if (curColColl != NULL)
328                 return false;                   /* tlist shorter than colCollations */
329         return true;
330 }
331
332 /*
333  * apply_tlist_labeling
334  *              Apply the TargetEntry labeling attributes of src_tlist to dest_tlist
335  *
336  * This is useful for reattaching column names etc to a plan's final output
337  * targetlist.
338  */
339 void
340 apply_tlist_labeling(List *dest_tlist, List *src_tlist)
341 {
342         ListCell   *ld,
343                            *ls;
344
345         Assert(list_length(dest_tlist) == list_length(src_tlist));
346         forboth(ld, dest_tlist, ls, src_tlist)
347         {
348                 TargetEntry *dest_tle = (TargetEntry *) lfirst(ld);
349                 TargetEntry *src_tle = (TargetEntry *) lfirst(ls);
350
351                 Assert(dest_tle->resno == src_tle->resno);
352                 dest_tle->resname = src_tle->resname;
353                 dest_tle->ressortgroupref = src_tle->ressortgroupref;
354                 dest_tle->resorigtbl = src_tle->resorigtbl;
355                 dest_tle->resorigcol = src_tle->resorigcol;
356                 dest_tle->resjunk = src_tle->resjunk;
357         }
358 }
359
360
361 /*
362  * get_sortgroupref_tle
363  *              Find the targetlist entry matching the given SortGroupRef index,
364  *              and return it.
365  */
366 TargetEntry *
367 get_sortgroupref_tle(Index sortref, List *targetList)
368 {
369         ListCell   *l;
370
371         foreach(l, targetList)
372         {
373                 TargetEntry *tle = (TargetEntry *) lfirst(l);
374
375                 if (tle->ressortgroupref == sortref)
376                         return tle;
377         }
378
379         elog(ERROR, "ORDER/GROUP BY expression not found in targetlist");
380         return NULL;                            /* keep compiler quiet */
381 }
382
383 /*
384  * get_sortgroupclause_tle
385  *              Find the targetlist entry matching the given SortGroupClause
386  *              by ressortgroupref, and return it.
387  */
388 TargetEntry *
389 get_sortgroupclause_tle(SortGroupClause *sgClause,
390                                                 List *targetList)
391 {
392         return get_sortgroupref_tle(sgClause->tleSortGroupRef, targetList);
393 }
394
395 /*
396  * get_sortgroupclause_expr
397  *              Find the targetlist entry matching the given SortGroupClause
398  *              by ressortgroupref, and return its expression.
399  */
400 Node *
401 get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
402 {
403         TargetEntry *tle = get_sortgroupclause_tle(sgClause, targetList);
404
405         return (Node *) tle->expr;
406 }
407
408 /*
409  * get_sortgrouplist_exprs
410  *              Given a list of SortGroupClauses, build a list
411  *              of the referenced targetlist expressions.
412  */
413 List *
414 get_sortgrouplist_exprs(List *sgClauses, List *targetList)
415 {
416         List       *result = NIL;
417         ListCell   *l;
418
419         foreach(l, sgClauses)
420         {
421                 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
422                 Node       *sortexpr;
423
424                 sortexpr = get_sortgroupclause_expr(sortcl, targetList);
425                 result = lappend(result, sortexpr);
426         }
427         return result;
428 }
429
430
431 /*****************************************************************************
432  *              Functions to extract data from a list of SortGroupClauses
433  *
434  * These don't really belong in tlist.c, but they are sort of related to the
435  * functions just above, and they don't seem to deserve their own file.
436  *****************************************************************************/
437
438 /*
439  * get_sortgroupref_clause
440  *              Find the SortGroupClause matching the given SortGroupRef index,
441  *              and return it.
442  */
443 SortGroupClause *
444 get_sortgroupref_clause(Index sortref, List *clauses)
445 {
446         ListCell   *l;
447
448         foreach(l, clauses)
449         {
450                 SortGroupClause *cl = (SortGroupClause *) lfirst(l);
451
452                 if (cl->tleSortGroupRef == sortref)
453                         return cl;
454         }
455
456         elog(ERROR, "ORDER/GROUP BY expression not found in list");
457         return NULL;                            /* keep compiler quiet */
458 }
459
460 /*
461  * get_sortgroupref_clause_noerr
462  *              As above, but return NULL rather than throwing an error if not found.
463  */
464 SortGroupClause *
465 get_sortgroupref_clause_noerr(Index sortref, List *clauses)
466 {
467         ListCell   *l;
468
469         foreach(l, clauses)
470         {
471                 SortGroupClause *cl = (SortGroupClause *) lfirst(l);
472
473                 if (cl->tleSortGroupRef == sortref)
474                         return cl;
475         }
476
477         return NULL;
478 }
479
480 /*
481  * extract_grouping_ops - make an array of the equality operator OIDs
482  *              for a SortGroupClause list
483  */
484 Oid *
485 extract_grouping_ops(List *groupClause)
486 {
487         int                     numCols = list_length(groupClause);
488         int                     colno = 0;
489         Oid                *groupOperators;
490         ListCell   *glitem;
491
492         groupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
493
494         foreach(glitem, groupClause)
495         {
496                 SortGroupClause *groupcl = (SortGroupClause *) lfirst(glitem);
497
498                 groupOperators[colno] = groupcl->eqop;
499                 Assert(OidIsValid(groupOperators[colno]));
500                 colno++;
501         }
502
503         return groupOperators;
504 }
505
506 /*
507  * extract_grouping_cols - make an array of the grouping column resnos
508  *              for a SortGroupClause list
509  */
510 AttrNumber *
511 extract_grouping_cols(List *groupClause, List *tlist)
512 {
513         AttrNumber *grpColIdx;
514         int                     numCols = list_length(groupClause);
515         int                     colno = 0;
516         ListCell   *glitem;
517
518         grpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
519
520         foreach(glitem, groupClause)
521         {
522                 SortGroupClause *groupcl = (SortGroupClause *) lfirst(glitem);
523                 TargetEntry *tle = get_sortgroupclause_tle(groupcl, tlist);
524
525                 grpColIdx[colno++] = tle->resno;
526         }
527
528         return grpColIdx;
529 }
530
531 /*
532  * grouping_is_sortable - is it possible to implement grouping list by sorting?
533  *
534  * This is easy since the parser will have included a sortop if one exists.
535  */
536 bool
537 grouping_is_sortable(List *groupClause)
538 {
539         ListCell   *glitem;
540
541         foreach(glitem, groupClause)
542         {
543                 SortGroupClause *groupcl = (SortGroupClause *) lfirst(glitem);
544
545                 if (!OidIsValid(groupcl->sortop))
546                         return false;
547         }
548         return true;
549 }
550
551 /*
552  * grouping_is_hashable - is it possible to implement grouping list by hashing?
553  *
554  * We rely on the parser to have set the hashable flag correctly.
555  */
556 bool
557 grouping_is_hashable(List *groupClause)
558 {
559         ListCell   *glitem;
560
561         foreach(glitem, groupClause)
562         {
563                 SortGroupClause *groupcl = (SortGroupClause *) lfirst(glitem);
564
565                 if (!groupcl->hashable)
566                         return false;
567         }
568         return true;
569 }
570
571
572 /*****************************************************************************
573  *              PathTarget manipulation functions
574  *
575  * PathTarget is a somewhat stripped-down version of a full targetlist; it
576  * omits all the TargetEntry decoration except (optionally) sortgroupref data,
577  * and it adds evaluation cost and output data width info.
578  *****************************************************************************/
579
580 /*
581  * make_pathtarget_from_tlist
582  *        Construct a PathTarget equivalent to the given targetlist.
583  *
584  * This leaves the cost and width fields as zeroes.  Most callers will want
585  * to use create_pathtarget(), so as to get those set.
586  */
587 PathTarget *
588 make_pathtarget_from_tlist(List *tlist)
589 {
590         PathTarget *target = makeNode(PathTarget);
591         int                     i;
592         ListCell   *lc;
593
594         target->sortgrouprefs = (Index *) palloc(list_length(tlist) * sizeof(Index));
595
596         i = 0;
597         foreach(lc, tlist)
598         {
599                 TargetEntry *tle = (TargetEntry *) lfirst(lc);
600
601                 target->exprs = lappend(target->exprs, tle->expr);
602                 target->sortgrouprefs[i] = tle->ressortgroupref;
603                 i++;
604         }
605
606         return target;
607 }
608
609 /*
610  * make_tlist_from_pathtarget
611  *        Construct a targetlist from a PathTarget.
612  */
613 List *
614 make_tlist_from_pathtarget(PathTarget *target)
615 {
616         List       *tlist = NIL;
617         int                     i;
618         ListCell   *lc;
619
620         i = 0;
621         foreach(lc, target->exprs)
622         {
623                 Expr       *expr = (Expr *) lfirst(lc);
624                 TargetEntry *tle;
625
626                 tle = makeTargetEntry(expr,
627                                                           i + 1,
628                                                           NULL,
629                                                           false);
630                 if (target->sortgrouprefs)
631                         tle->ressortgroupref = target->sortgrouprefs[i];
632                 tlist = lappend(tlist, tle);
633                 i++;
634         }
635
636         return tlist;
637 }
638
639 /*
640  * copy_pathtarget
641  *        Copy a PathTarget.
642  *
643  * The new PathTarget has its own List cells, but shares the underlying
644  * target expression trees with the old one.  We duplicate the List cells
645  * so that items can be added to one target without damaging the other.
646  */
647 PathTarget *
648 copy_pathtarget(PathTarget *src)
649 {
650         PathTarget *dst = makeNode(PathTarget);
651
652         /* Copy scalar fields */
653         memcpy(dst, src, sizeof(PathTarget));
654         /* Shallow-copy the expression list */
655         dst->exprs = list_copy(src->exprs);
656         /* Duplicate sortgrouprefs if any (if not, the memcpy handled this) */
657         if (src->sortgrouprefs)
658         {
659                 Size            nbytes = list_length(src->exprs) * sizeof(Index);
660
661                 dst->sortgrouprefs = (Index *) palloc(nbytes);
662                 memcpy(dst->sortgrouprefs, src->sortgrouprefs, nbytes);
663         }
664         return dst;
665 }
666
667 /*
668  * create_empty_pathtarget
669  *        Create an empty (zero columns, zero cost) PathTarget.
670  */
671 PathTarget *
672 create_empty_pathtarget(void)
673 {
674         /* This is easy, but we don't want callers to hard-wire this ... */
675         return makeNode(PathTarget);
676 }
677
678 /*
679  * add_column_to_pathtarget
680  *              Append a target column to the PathTarget.
681  *
682  * As with make_pathtarget_from_tlist, we leave it to the caller to update
683  * the cost and width fields.
684  */
685 void
686 add_column_to_pathtarget(PathTarget *target, Expr *expr, Index sortgroupref)
687 {
688         /* Updating the exprs list is easy ... */
689         target->exprs = lappend(target->exprs, expr);
690         /* ... the sortgroupref data, a bit less so */
691         if (target->sortgrouprefs)
692         {
693                 int                     nexprs = list_length(target->exprs);
694
695                 /* This might look inefficient, but actually it's usually cheap */
696                 target->sortgrouprefs = (Index *)
697                         repalloc(target->sortgrouprefs, nexprs * sizeof(Index));
698                 target->sortgrouprefs[nexprs - 1] = sortgroupref;
699         }
700         else if (sortgroupref)
701         {
702                 /* Adding sortgroupref labeling to a previously unlabeled target */
703                 int                     nexprs = list_length(target->exprs);
704
705                 target->sortgrouprefs = (Index *) palloc0(nexprs * sizeof(Index));
706                 target->sortgrouprefs[nexprs - 1] = sortgroupref;
707         }
708 }
709
710 /*
711  * add_new_column_to_pathtarget
712  *              Append a target column to the PathTarget, but only if it's not
713  *              equal() to any pre-existing target expression.
714  *
715  * The caller cannot specify a sortgroupref, since it would be unclear how
716  * to merge that with a pre-existing column.
717  *
718  * As with make_pathtarget_from_tlist, we leave it to the caller to update
719  * the cost and width fields.
720  */
721 void
722 add_new_column_to_pathtarget(PathTarget *target, Expr *expr)
723 {
724         if (!list_member(target->exprs, expr))
725                 add_column_to_pathtarget(target, expr, 0);
726 }
727
728 /*
729  * add_new_columns_to_pathtarget
730  *              Apply add_new_column_to_pathtarget() for each element of the list.
731  */
732 void
733 add_new_columns_to_pathtarget(PathTarget *target, List *exprs)
734 {
735         ListCell   *lc;
736
737         foreach(lc, exprs)
738         {
739                 Expr       *expr = (Expr *) lfirst(lc);
740
741                 add_new_column_to_pathtarget(target, expr);
742         }
743 }
744
745 /*
746  * apply_pathtarget_labeling_to_tlist
747  *              Apply any sortgrouprefs in the PathTarget to matching tlist entries
748  *
749  * Here, we do not assume that the tlist entries are one-for-one with the
750  * PathTarget.  The intended use of this function is to deal with cases
751  * where createplan.c has decided to use some other tlist and we have
752  * to identify what matches exist.
753  */
754 void
755 apply_pathtarget_labeling_to_tlist(List *tlist, PathTarget *target)
756 {
757         int                     i;
758         ListCell   *lc;
759
760         /* Nothing to do if PathTarget has no sortgrouprefs data */
761         if (target->sortgrouprefs == NULL)
762                 return;
763
764         i = 0;
765         foreach(lc, target->exprs)
766         {
767                 Expr       *expr = (Expr *) lfirst(lc);
768                 TargetEntry *tle;
769
770                 if (target->sortgrouprefs[i])
771                 {
772                         /*
773                          * For Vars, use tlist_member_match_var's weakened matching rule;
774                          * this allows us to deal with some cases where a set-returning
775                          * function has been inlined, so that we now have more knowledge
776                          * about what it returns than we did when the original Var was
777                          * created.  Otherwise, use regular equal() to find the matching
778                          * TLE.  (In current usage, only the Var case is actually needed;
779                          * but it seems best to have sane behavior here for non-Vars too.)
780                          */
781                         if (expr && IsA(expr, Var))
782                                 tle = tlist_member_match_var((Var *) expr, tlist);
783                         else
784                                 tle = tlist_member(expr, tlist);
785
786                         /*
787                          * Complain if noplace for the sortgrouprefs label, or if we'd
788                          * have to label a column twice.  (The case where it already has
789                          * the desired label probably can't happen, but we may as well
790                          * allow for it.)
791                          */
792                         if (!tle)
793                                 elog(ERROR, "ORDER/GROUP BY expression not found in targetlist");
794                         if (tle->ressortgroupref != 0 &&
795                                 tle->ressortgroupref != target->sortgrouprefs[i])
796                                 elog(ERROR, "targetlist item has multiple sortgroupref labels");
797
798                         tle->ressortgroupref = target->sortgrouprefs[i];
799                 }
800                 i++;
801         }
802 }
803
804 /*
805  * split_pathtarget_at_srfs
806  *              Split given PathTarget into multiple levels to position SRFs safely
807  *
808  * The executor can only handle set-returning functions that appear at the
809  * top level of the targetlist of a ProjectSet plan node.  If we have any SRFs
810  * that are not at top level, we need to split up the evaluation into multiple
811  * plan levels in which each level satisfies this constraint.  This function
812  * creates appropriate PathTarget(s) for each level.
813  *
814  * As an example, consider the tlist expression
815  *              x + srf1(srf2(y + z))
816  * This expression should appear as-is in the top PathTarget, but below that
817  * we must have a PathTarget containing
818  *              x, srf1(srf2(y + z))
819  * and below that, another PathTarget containing
820  *              x, srf2(y + z)
821  * and below that, another PathTarget containing
822  *              x, y, z
823  * When these tlists are processed by setrefs.c, subexpressions that match
824  * output expressions of the next lower tlist will be replaced by Vars,
825  * so that what the executor gets are tlists looking like
826  *              Var1 + Var2
827  *              Var1, srf1(Var2)
828  *              Var1, srf2(Var2 + Var3)
829  *              x, y, z
830  * which satisfy the desired property.
831  *
832  * Another example is
833  *              srf1(x), srf2(srf3(y))
834  * That must appear as-is in the top PathTarget, but below that we need
835  *              srf1(x), srf3(y)
836  * That is, each SRF must be computed at a level corresponding to the nesting
837  * depth of SRFs within its arguments.
838  *
839  * In some cases, a SRF has already been evaluated in some previous plan level
840  * and we shouldn't expand it again (that is, what we see in the target is
841  * already meant as a reference to a lower subexpression).  So, don't expand
842  * any tlist expressions that appear in input_target, if that's not NULL.
843  *
844  * It's also important that we preserve any sortgroupref annotation appearing
845  * in the given target, especially on expressions matching input_target items.
846  *
847  * The outputs of this function are two parallel lists, one a list of
848  * PathTargets and the other an integer list of bool flags indicating
849  * whether the corresponding PathTarget contains any evaluatable SRFs.
850  * The lists are given in the order they'd need to be evaluated in, with
851  * the "lowest" PathTarget first.  So the last list entry is always the
852  * originally given PathTarget, and any entries before it indicate evaluation
853  * levels that must be inserted below it.  The first list entry must not
854  * contain any SRFs (other than ones duplicating input_target entries), since
855  * it will typically be attached to a plan node that cannot evaluate SRFs.
856  *
857  * Note: using a list for the flags may seem like overkill, since there
858  * are only a few possible patterns for which levels contain SRFs.
859  * But this representation decouples callers from that knowledge.
860  */
861 void
862 split_pathtarget_at_srfs(PlannerInfo *root,
863                                                  PathTarget *target, PathTarget *input_target,
864                                                  List **targets, List **targets_contain_srfs)
865 {
866         split_pathtarget_context context;
867         int                     max_depth;
868         bool            need_extra_projection;
869         List       *prev_level_tlist;
870         int                     lci;
871         ListCell   *lc,
872                            *lc1,
873                            *lc2,
874                            *lc3;
875
876         /*
877          * It's not unusual for planner.c to pass us two physically identical
878          * targets, in which case we can conclude without further ado that all
879          * expressions are available from the input.  (The logic below would
880          * arrive at the same conclusion, but much more tediously.)
881          */
882         if (target == input_target)
883         {
884                 *targets = list_make1(target);
885                 *targets_contain_srfs = list_make1_int(false);
886                 return;
887         }
888
889         /* Pass any input_target exprs down to split_pathtarget_walker() */
890         context.input_target_exprs = input_target ? input_target->exprs : NIL;
891
892         /*
893          * Initialize with empty level-zero lists, and no levels after that.
894          * (Note: we could dispense with representing level zero explicitly, since
895          * it will never receive any SRFs, but then we'd have to special-case that
896          * level when we get to building result PathTargets.  Level zero describes
897          * the SRF-free PathTarget that will be given to the input plan node.)
898          */
899         context.level_srfs = list_make1(NIL);
900         context.level_input_vars = list_make1(NIL);
901         context.level_input_srfs = list_make1(NIL);
902
903         /* Initialize data we'll accumulate across all the target expressions */
904         context.current_input_vars = NIL;
905         context.current_input_srfs = NIL;
906         max_depth = 0;
907         need_extra_projection = false;
908
909         /* Scan each expression in the PathTarget looking for SRFs */
910         lci = 0;
911         foreach(lc, target->exprs)
912         {
913                 Node       *node = (Node *) lfirst(lc);
914
915                 /* Tell split_pathtarget_walker about this expr's sortgroupref */
916                 context.current_sgref = get_pathtarget_sortgroupref(target, lci);
917                 lci++;
918
919                 /*
920                  * Find all SRFs and Vars (and Var-like nodes) in this expression, and
921                  * enter them into appropriate lists within the context struct.
922                  */
923                 context.current_depth = 0;
924                 split_pathtarget_walker(node, &context);
925
926                 /* An expression containing no SRFs is of no further interest */
927                 if (context.current_depth == 0)
928                         continue;
929
930                 /*
931                  * Track max SRF nesting depth over the whole PathTarget.  Also, if
932                  * this expression establishes a new max depth, we no longer care
933                  * whether previous expressions contained nested SRFs; we can handle
934                  * any required projection for them in the final ProjectSet node.
935                  */
936                 if (max_depth < context.current_depth)
937                 {
938                         max_depth = context.current_depth;
939                         need_extra_projection = false;
940                 }
941
942                 /*
943                  * If any maximum-depth SRF is not at the top level of its expression,
944                  * we'll need an extra Result node to compute the top-level scalar
945                  * expression.
946                  */
947                 if (max_depth == context.current_depth && !IS_SRF_CALL(node))
948                         need_extra_projection = true;
949         }
950
951         /*
952          * If we found no SRFs needing evaluation (maybe they were all present in
953          * input_target, or maybe they were all removed by const-simplification),
954          * then no ProjectSet is needed; fall out.
955          */
956         if (max_depth == 0)
957         {
958                 *targets = list_make1(target);
959                 *targets_contain_srfs = list_make1_int(false);
960                 return;
961         }
962
963         /*
964          * The Vars and SRF outputs needed at top level can be added to the last
965          * level_input lists if we don't need an extra projection step.  If we do
966          * need one, add a SRF-free level to the lists.
967          */
968         if (need_extra_projection)
969         {
970                 context.level_srfs = lappend(context.level_srfs, NIL);
971                 context.level_input_vars = lappend(context.level_input_vars,
972                                                                                    context.current_input_vars);
973                 context.level_input_srfs = lappend(context.level_input_srfs,
974                                                                                    context.current_input_srfs);
975         }
976         else
977         {
978                 lc = list_nth_cell(context.level_input_vars, max_depth);
979                 lfirst(lc) = list_concat(lfirst(lc), context.current_input_vars);
980                 lc = list_nth_cell(context.level_input_srfs, max_depth);
981                 lfirst(lc) = list_concat(lfirst(lc), context.current_input_srfs);
982         }
983
984         /*
985          * Now construct the output PathTargets.  The original target can be used
986          * as-is for the last one, but we need to construct a new SRF-free target
987          * representing what the preceding plan node has to emit, as well as a
988          * target for each intermediate ProjectSet node.
989          */
990         *targets = *targets_contain_srfs = NIL;
991         prev_level_tlist = NIL;
992
993         forthree(lc1, context.level_srfs,
994                          lc2, context.level_input_vars,
995                          lc3, context.level_input_srfs)
996         {
997                 List       *level_srfs = (List *) lfirst(lc1);
998                 PathTarget *ntarget;
999
1000                 if (lnext(lc1) == NULL)
1001                 {
1002                         ntarget = target;
1003                 }
1004                 else
1005                 {
1006                         ntarget = create_empty_pathtarget();
1007
1008                         /*
1009                          * This target should actually evaluate any SRFs of the current
1010                          * level, and it needs to propagate forward any Vars needed by
1011                          * later levels, as well as SRFs computed earlier and needed by
1012                          * later levels.
1013                          */
1014                         add_sp_items_to_pathtarget(ntarget, level_srfs);
1015                         for_each_cell(lc, lnext(lc2))
1016                         {
1017                                 List       *input_vars = (List *) lfirst(lc);
1018
1019                                 add_sp_items_to_pathtarget(ntarget, input_vars);
1020                         }
1021                         for_each_cell(lc, lnext(lc3))
1022                         {
1023                                 List       *input_srfs = (List *) lfirst(lc);
1024                                 ListCell   *lcx;
1025
1026                                 foreach(lcx, input_srfs)
1027                                 {
1028                                         split_pathtarget_item *item = lfirst(lcx);
1029
1030                                         if (list_member(prev_level_tlist, item->expr))
1031                                                 add_sp_item_to_pathtarget(ntarget, item);
1032                                 }
1033                         }
1034                         set_pathtarget_cost_width(root, ntarget);
1035                 }
1036
1037                 /*
1038                  * Add current target and does-it-compute-SRFs flag to output lists.
1039                  */
1040                 *targets = lappend(*targets, ntarget);
1041                 *targets_contain_srfs = lappend_int(*targets_contain_srfs,
1042                                                                                         (level_srfs != NIL));
1043
1044                 /* Remember this level's output for next pass */
1045                 prev_level_tlist = ntarget->exprs;
1046         }
1047 }
1048
1049 /*
1050  * Recursively examine expressions for split_pathtarget_at_srfs.
1051  *
1052  * Note we make no effort here to prevent duplicate entries in the output
1053  * lists.  Duplicates will be gotten rid of later.
1054  */
1055 static bool
1056 split_pathtarget_walker(Node *node, split_pathtarget_context *context)
1057 {
1058         if (node == NULL)
1059                 return false;
1060
1061         /*
1062          * A subexpression that matches an expression already computed in
1063          * input_target can be treated like a Var (which indeed it will be after
1064          * setrefs.c gets done with it), even if it's actually a SRF.  Record it
1065          * as being needed for the current expression, and ignore any
1066          * substructure.  (Note in particular that this preserves the identity of
1067          * any expressions that appear as sortgrouprefs in input_target.)
1068          */
1069         if (list_member(context->input_target_exprs, node))
1070         {
1071                 split_pathtarget_item *item = palloc(sizeof(split_pathtarget_item));
1072
1073                 item->expr = node;
1074                 item->sortgroupref = context->current_sgref;
1075                 context->current_input_vars = lappend(context->current_input_vars,
1076                                                                                           item);
1077                 return false;
1078         }
1079
1080         /*
1081          * Vars and Var-like constructs are expected to be gotten from the input,
1082          * too.  We assume that these constructs cannot contain any SRFs (if one
1083          * does, there will be an executor failure from a misplaced SRF).
1084          */
1085         if (IsA(node, Var) ||
1086                 IsA(node, PlaceHolderVar) ||
1087                 IsA(node, Aggref) ||
1088                 IsA(node, GroupingFunc) ||
1089                 IsA(node, WindowFunc))
1090         {
1091                 split_pathtarget_item *item = palloc(sizeof(split_pathtarget_item));
1092
1093                 item->expr = node;
1094                 item->sortgroupref = context->current_sgref;
1095                 context->current_input_vars = lappend(context->current_input_vars,
1096                                                                                           item);
1097                 return false;
1098         }
1099
1100         /*
1101          * If it's a SRF, recursively examine its inputs, determine its level, and
1102          * make appropriate entries in the output lists.
1103          */
1104         if (IS_SRF_CALL(node))
1105         {
1106                 split_pathtarget_item *item = palloc(sizeof(split_pathtarget_item));
1107                 List       *save_input_vars = context->current_input_vars;
1108                 List       *save_input_srfs = context->current_input_srfs;
1109                 int                     save_current_depth = context->current_depth;
1110                 int                     srf_depth;
1111                 ListCell   *lc;
1112
1113                 item->expr = node;
1114                 item->sortgroupref = context->current_sgref;
1115
1116                 context->current_input_vars = NIL;
1117                 context->current_input_srfs = NIL;
1118                 context->current_depth = 0;
1119                 context->current_sgref = 0; /* subexpressions are not sortgroup items */
1120
1121                 (void) expression_tree_walker(node, split_pathtarget_walker,
1122                                                                           (void *) context);
1123
1124                 /* Depth is one more than any SRF below it */
1125                 srf_depth = context->current_depth + 1;
1126
1127                 /* If new record depth, initialize another level of output lists */
1128                 if (srf_depth >= list_length(context->level_srfs))
1129                 {
1130                         context->level_srfs = lappend(context->level_srfs, NIL);
1131                         context->level_input_vars = lappend(context->level_input_vars, NIL);
1132                         context->level_input_srfs = lappend(context->level_input_srfs, NIL);
1133                 }
1134
1135                 /* Record this SRF as needing to be evaluated at appropriate level */
1136                 lc = list_nth_cell(context->level_srfs, srf_depth);
1137                 lfirst(lc) = lappend(lfirst(lc), item);
1138
1139                 /* Record its inputs as being needed at the same level */
1140                 lc = list_nth_cell(context->level_input_vars, srf_depth);
1141                 lfirst(lc) = list_concat(lfirst(lc), context->current_input_vars);
1142                 lc = list_nth_cell(context->level_input_srfs, srf_depth);
1143                 lfirst(lc) = list_concat(lfirst(lc), context->current_input_srfs);
1144
1145                 /*
1146                  * Restore caller-level state and update it for presence of this SRF.
1147                  * Notice we report the SRF itself as being needed for evaluation of
1148                  * surrounding expression.
1149                  */
1150                 context->current_input_vars = save_input_vars;
1151                 context->current_input_srfs = lappend(save_input_srfs, item);
1152                 context->current_depth = Max(save_current_depth, srf_depth);
1153
1154                 /* We're done here */
1155                 return false;
1156         }
1157
1158         /*
1159          * Otherwise, the node is a scalar (non-set) expression, so recurse to
1160          * examine its inputs.
1161          */
1162         context->current_sgref = 0; /* subexpressions are not sortgroup items */
1163         return expression_tree_walker(node, split_pathtarget_walker,
1164                                                                   (void *) context);
1165 }
1166
1167 /*
1168  * Add a split_pathtarget_item to the PathTarget, unless a matching item is
1169  * already present.  This is like add_new_column_to_pathtarget, but allows
1170  * for sortgrouprefs to be handled.  An item having zero sortgroupref can
1171  * be merged with one that has a sortgroupref, acquiring the latter's
1172  * sortgroupref.
1173  *
1174  * Note that we don't worry about possibly adding duplicate sortgrouprefs
1175  * to the PathTarget.  That would be bad, but it should be impossible unless
1176  * the target passed to split_pathtarget_at_srfs already had duplicates.
1177  * As long as it didn't, we can have at most one split_pathtarget_item with
1178  * any particular nonzero sortgroupref.
1179  */
1180 static void
1181 add_sp_item_to_pathtarget(PathTarget *target, split_pathtarget_item *item)
1182 {
1183         int                     lci;
1184         ListCell   *lc;
1185
1186         /*
1187          * Look for a pre-existing entry that is equal() and does not have a
1188          * conflicting sortgroupref already.
1189          */
1190         lci = 0;
1191         foreach(lc, target->exprs)
1192         {
1193                 Node       *node = (Node *) lfirst(lc);
1194                 Index           sgref = get_pathtarget_sortgroupref(target, lci);
1195
1196                 if ((item->sortgroupref == sgref ||
1197                          item->sortgroupref == 0 ||
1198                          sgref == 0) &&
1199                         equal(item->expr, node))
1200                 {
1201                         /* Found a match.  Assign item's sortgroupref if it has one. */
1202                         if (item->sortgroupref)
1203                         {
1204                                 if (target->sortgrouprefs == NULL)
1205                                 {
1206                                         target->sortgrouprefs = (Index *)
1207                                                 palloc0(list_length(target->exprs) * sizeof(Index));
1208                                 }
1209                                 target->sortgrouprefs[lci] = item->sortgroupref;
1210                         }
1211                         return;
1212                 }
1213                 lci++;
1214         }
1215
1216         /*
1217          * No match, so add item to PathTarget.  Copy the expr for safety.
1218          */
1219         add_column_to_pathtarget(target, (Expr *) copyObject(item->expr),
1220                                                          item->sortgroupref);
1221 }
1222
1223 /*
1224  * Apply add_sp_item_to_pathtarget to each element of list.
1225  */
1226 static void
1227 add_sp_items_to_pathtarget(PathTarget *target, List *items)
1228 {
1229         ListCell   *lc;
1230
1231         foreach(lc, items)
1232         {
1233                 split_pathtarget_item *item = lfirst(lc);
1234
1235                 add_sp_item_to_pathtarget(target, item);
1236         }
1237 }