]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeSubplan.c
Update copyrights in source tree to 2008.
[postgresql] / src / backend / executor / nodeSubplan.c
1 /*-------------------------------------------------------------------------
2  *
3  * nodeSubplan.c
4  *        routines to support subselects
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  *        $PostgreSQL: pgsql/src/backend/executor/nodeSubplan.c,v 1.92 2008/01/01 19:45:49 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 /*
15  *       INTERFACE ROUTINES
16  *              ExecSubPlan  - process a subselect
17  *              ExecInitSubPlan - initialize a subselect
18  */
19 #include "postgres.h"
20
21 #include <math.h>
22
23 #include "access/heapam.h"
24 #include "executor/executor.h"
25 #include "executor/nodeSubplan.h"
26 #include "nodes/makefuncs.h"
27 #include "optimizer/clauses.h"
28 #include "utils/array.h"
29 #include "utils/lsyscache.h"
30 #include "utils/memutils.h"
31
32
33 static Datum ExecHashSubPlan(SubPlanState *node,
34                                 ExprContext *econtext,
35                                 bool *isNull);
36 static Datum ExecScanSubPlan(SubPlanState *node,
37                                 ExprContext *econtext,
38                                 bool *isNull);
39 static void buildSubPlanHash(SubPlanState *node, ExprContext *econtext);
40 static bool findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot);
41 static bool slotAllNulls(TupleTableSlot *slot);
42 static bool slotNoNulls(TupleTableSlot *slot);
43
44
45 /* ----------------------------------------------------------------
46  *              ExecSubPlan
47  * ----------------------------------------------------------------
48  */
49 Datum
50 ExecSubPlan(SubPlanState *node,
51                         ExprContext *econtext,
52                         bool *isNull,
53                         ExprDoneCond *isDone)
54 {
55         SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
56
57         /* Set default values for result flags: non-null, not a set result */
58         *isNull = false;
59         if (isDone)
60                 *isDone = ExprSingleResult;
61
62         if (subplan->setParam != NIL)
63                 elog(ERROR, "cannot set parent params from subquery");
64
65         if (subplan->useHashTable)
66                 return ExecHashSubPlan(node, econtext, isNull);
67         else
68                 return ExecScanSubPlan(node, econtext, isNull);
69 }
70
71 /*
72  * ExecHashSubPlan: store subselect result in an in-memory hash table
73  */
74 static Datum
75 ExecHashSubPlan(SubPlanState *node,
76                                 ExprContext *econtext,
77                                 bool *isNull)
78 {
79         SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
80         PlanState  *planstate = node->planstate;
81         ExprContext *innerecontext = node->innerecontext;
82         TupleTableSlot *slot;
83
84         /* Shouldn't have any direct correlation Vars */
85         if (subplan->parParam != NIL || node->args != NIL)
86                 elog(ERROR, "hashed subplan with direct correlation not supported");
87
88         /*
89          * If first time through or we need to rescan the subplan, build the hash
90          * table.
91          */
92         if (node->hashtable == NULL || planstate->chgParam != NULL)
93                 buildSubPlanHash(node, econtext);
94
95         /*
96          * The result for an empty subplan is always FALSE; no need to evaluate
97          * lefthand side.
98          */
99         *isNull = false;
100         if (!node->havehashrows && !node->havenullrows)
101                 return BoolGetDatum(false);
102
103         /*
104          * Evaluate lefthand expressions and form a projection tuple. First we
105          * have to set the econtext to use (hack alert!).
106          */
107         node->projLeft->pi_exprContext = econtext;
108         slot = ExecProject(node->projLeft, NULL);
109
110         /*
111          * Note: because we are typically called in a per-tuple context, we have
112          * to explicitly clear the projected tuple before returning. Otherwise,
113          * we'll have a double-free situation: the per-tuple context will probably
114          * be reset before we're called again, and then the tuple slot will think
115          * it still needs to free the tuple.
116          */
117
118         /*
119          * Since the hashtable routines will use innerecontext's per-tuple memory
120          * as working memory, be sure to reset it for each tuple.
121          */
122         ResetExprContext(innerecontext);
123
124         /*
125          * If the LHS is all non-null, probe for an exact match in the main hash
126          * table.  If we find one, the result is TRUE. Otherwise, scan the
127          * partly-null table to see if there are any rows that aren't provably
128          * unequal to the LHS; if so, the result is UNKNOWN.  (We skip that part
129          * if we don't care about UNKNOWN.) Otherwise, the result is FALSE.
130          *
131          * Note: the reason we can avoid a full scan of the main hash table is
132          * that the combining operators are assumed never to yield NULL when both
133          * inputs are non-null.  If they were to do so, we might need to produce
134          * UNKNOWN instead of FALSE because of an UNKNOWN result in comparing the
135          * LHS to some main-table entry --- which is a comparison we will not even
136          * make, unless there's a chance match of hash keys.
137          */
138         if (slotNoNulls(slot))
139         {
140                 if (node->havehashrows &&
141                         FindTupleHashEntry(node->hashtable,
142                                                            slot,
143                                                            node->cur_eq_funcs,
144                                                            node->lhs_hash_funcs) != NULL)
145                 {
146                         ExecClearTuple(slot);
147                         return BoolGetDatum(true);
148                 }
149                 if (node->havenullrows &&
150                         findPartialMatch(node->hashnulls, slot))
151                 {
152                         ExecClearTuple(slot);
153                         *isNull = true;
154                         return BoolGetDatum(false);
155                 }
156                 ExecClearTuple(slot);
157                 return BoolGetDatum(false);
158         }
159
160         /*
161          * When the LHS is partly or wholly NULL, we can never return TRUE. If we
162          * don't care about UNKNOWN, just return FALSE.  Otherwise, if the LHS is
163          * wholly NULL, immediately return UNKNOWN.  (Since the combining
164          * operators are strict, the result could only be FALSE if the sub-select
165          * were empty, but we already handled that case.) Otherwise, we must scan
166          * both the main and partly-null tables to see if there are any rows that
167          * aren't provably unequal to the LHS; if so, the result is UNKNOWN.
168          * Otherwise, the result is FALSE.
169          */
170         if (node->hashnulls == NULL)
171         {
172                 ExecClearTuple(slot);
173                 return BoolGetDatum(false);
174         }
175         if (slotAllNulls(slot))
176         {
177                 ExecClearTuple(slot);
178                 *isNull = true;
179                 return BoolGetDatum(false);
180         }
181         /* Scan partly-null table first, since more likely to get a match */
182         if (node->havenullrows &&
183                 findPartialMatch(node->hashnulls, slot))
184         {
185                 ExecClearTuple(slot);
186                 *isNull = true;
187                 return BoolGetDatum(false);
188         }
189         if (node->havehashrows &&
190                 findPartialMatch(node->hashtable, slot))
191         {
192                 ExecClearTuple(slot);
193                 *isNull = true;
194                 return BoolGetDatum(false);
195         }
196         ExecClearTuple(slot);
197         return BoolGetDatum(false);
198 }
199
200 /*
201  * ExecScanSubPlan: default case where we have to rescan subplan each time
202  */
203 static Datum
204 ExecScanSubPlan(SubPlanState *node,
205                                 ExprContext *econtext,
206                                 bool *isNull)
207 {
208         SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
209         PlanState  *planstate = node->planstate;
210         SubLinkType subLinkType = subplan->subLinkType;
211         MemoryContext oldcontext;
212         TupleTableSlot *slot;
213         Datum           result;
214         bool            found = false;  /* TRUE if got at least one subplan tuple */
215         ListCell   *pvar;
216         ListCell   *l;
217         ArrayBuildState *astate = NULL;
218
219         /*
220          * We are probably in a short-lived expression-evaluation context. Switch
221          * to the per-query context for manipulating the child plan's chgParam,
222          * calling ExecProcNode on it, etc.
223          */
224         oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
225
226         /*
227          * Set Params of this plan from parent plan correlation values. (Any
228          * calculation we have to do is done in the parent econtext, since the
229          * Param values don't need to have per-query lifetime.)
230          */
231         Assert(list_length(subplan->parParam) == list_length(node->args));
232
233         forboth(l, subplan->parParam, pvar, node->args)
234         {
235                 int                     paramid = lfirst_int(l);
236                 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
237
238                 prm->value = ExecEvalExprSwitchContext((ExprState *) lfirst(pvar),
239                                                                                            econtext,
240                                                                                            &(prm->isnull),
241                                                                                            NULL);
242                 planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
243         }
244
245         /*
246          * Now that we've set up its parameters, we can reset the subplan.
247          */
248         ExecReScan(planstate, NULL);
249
250         /*
251          * For all sublink types except EXPR_SUBLINK and ARRAY_SUBLINK, the result
252          * is boolean as are the results of the combining operators. We combine
253          * results across tuples (if the subplan produces more than one) using OR
254          * semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK.
255          * (ROWCOMPARE_SUBLINK doesn't allow multiple tuples from the subplan.)
256          * NULL results from the combining operators are handled according to the
257          * usual SQL semantics for OR and AND.  The result for no input tuples is
258          * FALSE for ANY_SUBLINK, TRUE for ALL_SUBLINK, NULL for
259          * ROWCOMPARE_SUBLINK.
260          *
261          * For EXPR_SUBLINK we require the subplan to produce no more than one
262          * tuple, else an error is raised.      If zero tuples are produced, we return
263          * NULL.  Assuming we get a tuple, we just use its first column (there can
264          * be only one non-junk column in this case).
265          *
266          * For ARRAY_SUBLINK we allow the subplan to produce any number of tuples,
267          * and form an array of the first column's values.  Note in particular
268          * that we produce a zero-element array if no tuples are produced (this is
269          * a change from pre-8.3 behavior of returning NULL).
270          */
271         result = BoolGetDatum(subLinkType == ALL_SUBLINK);
272         *isNull = false;
273
274         for (slot = ExecProcNode(planstate);
275                  !TupIsNull(slot);
276                  slot = ExecProcNode(planstate))
277         {
278                 TupleDesc       tdesc = slot->tts_tupleDescriptor;
279                 Datum           rowresult;
280                 bool            rownull;
281                 int                     col;
282                 ListCell   *plst;
283
284                 if (subLinkType == EXISTS_SUBLINK)
285                 {
286                         found = true;
287                         result = BoolGetDatum(true);
288                         break;
289                 }
290
291                 if (subLinkType == EXPR_SUBLINK)
292                 {
293                         /* cannot allow multiple input tuples for EXPR sublink */
294                         if (found)
295                                 ereport(ERROR,
296                                                 (errcode(ERRCODE_CARDINALITY_VIOLATION),
297                                                  errmsg("more than one row returned by a subquery used as an expression")));
298                         found = true;
299
300                         /*
301                          * We need to copy the subplan's tuple in case the result is of
302                          * pass-by-ref type --- our return value will point into this
303                          * copied tuple!  Can't use the subplan's instance of the tuple
304                          * since it won't still be valid after next ExecProcNode() call.
305                          * node->curTuple keeps track of the copied tuple for eventual
306                          * freeing.
307                          */
308                         if (node->curTuple)
309                                 heap_freetuple(node->curTuple);
310                         node->curTuple = ExecCopySlotTuple(slot);
311
312                         result = heap_getattr(node->curTuple, 1, tdesc, isNull);
313                         /* keep scanning subplan to make sure there's only one tuple */
314                         continue;
315                 }
316
317                 if (subLinkType == ARRAY_SUBLINK)
318                 {
319                         Datum           dvalue;
320                         bool            disnull;
321
322                         found = true;
323                         /* stash away current value */
324                         Assert(subplan->firstColType == tdesc->attrs[0]->atttypid);
325                         dvalue = slot_getattr(slot, 1, &disnull);
326                         astate = accumArrayResult(astate, dvalue, disnull,
327                                                                           subplan->firstColType, oldcontext);
328                         /* keep scanning subplan to collect all values */
329                         continue;
330                 }
331
332                 /* cannot allow multiple input tuples for ROWCOMPARE sublink either */
333                 if (subLinkType == ROWCOMPARE_SUBLINK && found)
334                         ereport(ERROR,
335                                         (errcode(ERRCODE_CARDINALITY_VIOLATION),
336                                          errmsg("more than one row returned by a subquery used as an expression")));
337
338                 found = true;
339
340                 /*
341                  * For ALL, ANY, and ROWCOMPARE sublinks, load up the Params
342                  * representing the columns of the sub-select, and then evaluate the
343                  * combining expression.
344                  */
345                 col = 1;
346                 foreach(plst, subplan->paramIds)
347                 {
348                         int                     paramid = lfirst_int(plst);
349                         ParamExecData *prmdata;
350
351                         prmdata = &(econtext->ecxt_param_exec_vals[paramid]);
352                         Assert(prmdata->execPlan == NULL);
353                         prmdata->value = slot_getattr(slot, col, &(prmdata->isnull));
354                         col++;
355                 }
356
357                 rowresult = ExecEvalExprSwitchContext(node->testexpr, econtext,
358                                                                                           &rownull, NULL);
359
360                 if (subLinkType == ANY_SUBLINK)
361                 {
362                         /* combine across rows per OR semantics */
363                         if (rownull)
364                                 *isNull = true;
365                         else if (DatumGetBool(rowresult))
366                         {
367                                 result = BoolGetDatum(true);
368                                 *isNull = false;
369                                 break;                  /* needn't look at any more rows */
370                         }
371                 }
372                 else if (subLinkType == ALL_SUBLINK)
373                 {
374                         /* combine across rows per AND semantics */
375                         if (rownull)
376                                 *isNull = true;
377                         else if (!DatumGetBool(rowresult))
378                         {
379                                 result = BoolGetDatum(false);
380                                 *isNull = false;
381                                 break;                  /* needn't look at any more rows */
382                         }
383                 }
384                 else
385                 {
386                         /* must be ROWCOMPARE_SUBLINK */
387                         result = rowresult;
388                         *isNull = rownull;
389                 }
390         }
391
392         MemoryContextSwitchTo(oldcontext);
393
394         if (subLinkType == ARRAY_SUBLINK)
395         {
396                 /* We return the result in the caller's context */
397                 if (astate != NULL)
398                         result = makeArrayResult(astate, oldcontext);
399                 else
400                         result = PointerGetDatum(construct_empty_array(subplan->firstColType));
401         }
402         else if (!found)
403         {
404                 /*
405                  * deal with empty subplan result.      result/isNull were previously
406                  * initialized correctly for all sublink types except EXPR and
407                  * ROWCOMPARE; for those, return NULL.
408                  */
409                 if (subLinkType == EXPR_SUBLINK ||
410                         subLinkType == ROWCOMPARE_SUBLINK)
411                 {
412                         result = (Datum) 0;
413                         *isNull = true;
414                 }
415         }
416
417         return result;
418 }
419
420 /*
421  * buildSubPlanHash: load hash table by scanning subplan output.
422  */
423 static void
424 buildSubPlanHash(SubPlanState *node, ExprContext *econtext)
425 {
426         SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
427         PlanState  *planstate = node->planstate;
428         int                     ncols = list_length(subplan->paramIds);
429         ExprContext *innerecontext = node->innerecontext;
430         MemoryContext tempcxt = innerecontext->ecxt_per_tuple_memory;
431         MemoryContext oldcontext;
432         int                     nbuckets;
433         TupleTableSlot *slot;
434
435         Assert(subplan->subLinkType == ANY_SUBLINK);
436
437         /*
438          * If we already had any hash tables, destroy 'em; then create empty hash
439          * table(s).
440          *
441          * If we need to distinguish accurately between FALSE and UNKNOWN (i.e.,
442          * NULL) results of the IN operation, then we have to store subplan output
443          * rows that are partly or wholly NULL.  We store such rows in a separate
444          * hash table that we expect will be much smaller than the main table. (We
445          * can use hashing to eliminate partly-null rows that are not distinct. We
446          * keep them separate to minimize the cost of the inevitable full-table
447          * searches; see findPartialMatch.)
448          *
449          * If it's not necessary to distinguish FALSE and UNKNOWN, then we don't
450          * need to store subplan output rows that contain NULL.
451          */
452         MemoryContextReset(node->tablecxt);
453         node->hashtable = NULL;
454         node->hashnulls = NULL;
455         node->havehashrows = false;
456         node->havenullrows = false;
457
458         nbuckets = (int) ceil(planstate->plan->plan_rows);
459         if (nbuckets < 1)
460                 nbuckets = 1;
461
462         node->hashtable = BuildTupleHashTable(ncols,
463                                                                                   node->keyColIdx,
464                                                                                   node->tab_eq_funcs,
465                                                                                   node->tab_hash_funcs,
466                                                                                   nbuckets,
467                                                                                   sizeof(TupleHashEntryData),
468                                                                                   node->tablecxt,
469                                                                                   tempcxt);
470
471         if (!subplan->unknownEqFalse)
472         {
473                 if (ncols == 1)
474                         nbuckets = 1;           /* there can only be one entry */
475                 else
476                 {
477                         nbuckets /= 16;
478                         if (nbuckets < 1)
479                                 nbuckets = 1;
480                 }
481                 node->hashnulls = BuildTupleHashTable(ncols,
482                                                                                           node->keyColIdx,
483                                                                                           node->tab_eq_funcs,
484                                                                                           node->tab_hash_funcs,
485                                                                                           nbuckets,
486                                                                                           sizeof(TupleHashEntryData),
487                                                                                           node->tablecxt,
488                                                                                           tempcxt);
489         }
490
491         /*
492          * We are probably in a short-lived expression-evaluation context. Switch
493          * to the per-query context for manipulating the child plan.
494          */
495         oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
496
497         /*
498          * Reset subplan to start.
499          */
500         ExecReScan(planstate, NULL);
501
502         /*
503          * Scan the subplan and load the hash table(s).  Note that when there are
504          * duplicate rows coming out of the sub-select, only one copy is stored.
505          */
506         for (slot = ExecProcNode(planstate);
507                  !TupIsNull(slot);
508                  slot = ExecProcNode(planstate))
509         {
510                 int                     col = 1;
511                 ListCell   *plst;
512                 bool            isnew;
513
514                 /*
515                  * Load up the Params representing the raw sub-select outputs, then
516                  * form the projection tuple to store in the hashtable.
517                  */
518                 foreach(plst, subplan->paramIds)
519                 {
520                         int                     paramid = lfirst_int(plst);
521                         ParamExecData *prmdata;
522
523                         prmdata = &(innerecontext->ecxt_param_exec_vals[paramid]);
524                         Assert(prmdata->execPlan == NULL);
525                         prmdata->value = slot_getattr(slot, col,
526                                                                                   &(prmdata->isnull));
527                         col++;
528                 }
529                 slot = ExecProject(node->projRight, NULL);
530
531                 /*
532                  * If result contains any nulls, store separately or not at all.
533                  */
534                 if (slotNoNulls(slot))
535                 {
536                         (void) LookupTupleHashEntry(node->hashtable, slot, &isnew);
537                         node->havehashrows = true;
538                 }
539                 else if (node->hashnulls)
540                 {
541                         (void) LookupTupleHashEntry(node->hashnulls, slot, &isnew);
542                         node->havenullrows = true;
543                 }
544
545                 /*
546                  * Reset innerecontext after each inner tuple to free any memory used
547                  * in hash computation or comparison routines.
548                  */
549                 ResetExprContext(innerecontext);
550         }
551
552         /*
553          * Since the projected tuples are in the sub-query's context and not the
554          * main context, we'd better clear the tuple slot before there's any
555          * chance of a reset of the sub-query's context.  Else we will have the
556          * potential for a double free attempt.  (XXX possibly no longer needed,
557          * but can't hurt.)
558          */
559         ExecClearTuple(node->projRight->pi_slot);
560
561         MemoryContextSwitchTo(oldcontext);
562 }
563
564 /*
565  * findPartialMatch: does the hashtable contain an entry that is not
566  * provably distinct from the tuple?
567  *
568  * We have to scan the whole hashtable; we can't usefully use hashkeys
569  * to guide probing, since we might get partial matches on tuples with
570  * hashkeys quite unrelated to what we'd get from the given tuple.
571  */
572 static bool
573 findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot)
574 {
575         int                     numCols = hashtable->numCols;
576         AttrNumber *keyColIdx = hashtable->keyColIdx;
577         TupleHashIterator hashiter;
578         TupleHashEntry entry;
579
580         InitTupleHashIterator(hashtable, &hashiter);
581         while ((entry = ScanTupleHashTable(&hashiter)) != NULL)
582         {
583                 ExecStoreMinimalTuple(entry->firstTuple, hashtable->tableslot, false);
584                 if (!execTuplesUnequal(slot, hashtable->tableslot,
585                                                            numCols, keyColIdx,
586                                                            hashtable->cur_eq_funcs,
587                                                            hashtable->tempcxt))
588                 {
589                         TermTupleHashIterator(&hashiter);
590                         return true;
591                 }
592         }
593         /* No TermTupleHashIterator call needed here */
594         return false;
595 }
596
597 /*
598  * slotAllNulls: is the slot completely NULL?
599  *
600  * This does not test for dropped columns, which is OK because we only
601  * use it on projected tuples.
602  */
603 static bool
604 slotAllNulls(TupleTableSlot *slot)
605 {
606         int                     ncols = slot->tts_tupleDescriptor->natts;
607         int                     i;
608
609         for (i = 1; i <= ncols; i++)
610         {
611                 if (!slot_attisnull(slot, i))
612                         return false;
613         }
614         return true;
615 }
616
617 /*
618  * slotNoNulls: is the slot entirely not NULL?
619  *
620  * This does not test for dropped columns, which is OK because we only
621  * use it on projected tuples.
622  */
623 static bool
624 slotNoNulls(TupleTableSlot *slot)
625 {
626         int                     ncols = slot->tts_tupleDescriptor->natts;
627         int                     i;
628
629         for (i = 1; i <= ncols; i++)
630         {
631                 if (slot_attisnull(slot, i))
632                         return false;
633         }
634         return true;
635 }
636
637 /* ----------------------------------------------------------------
638  *              ExecInitSubPlan
639  *
640  * Create a SubPlanState for a SubPlan; this is the SubPlan-specific part
641  * of ExecInitExpr().  We split it out so that it can be used for InitPlans
642  * as well as regular SubPlans.  Note that we don't link the SubPlan into
643  * the parent's subPlan list, because that shouldn't happen for InitPlans.
644  * Instead, ExecInitExpr() does that one part.
645  * ----------------------------------------------------------------
646  */
647 SubPlanState *
648 ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
649 {
650         SubPlanState *sstate = makeNode(SubPlanState);
651         EState     *estate = parent->state;
652
653         sstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecSubPlan;
654         sstate->xprstate.expr = (Expr *) subplan;
655
656         /* Link the SubPlanState to already-initialized subplan */
657         sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
658                                                                                            subplan->plan_id - 1);
659
660         /* Initialize subexpressions */
661         sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
662         sstate->args = (List *) ExecInitExpr((Expr *) subplan->args, parent);
663
664         /*
665          * initialize my state
666          */
667         sstate->curTuple = NULL;
668         sstate->projLeft = NULL;
669         sstate->projRight = NULL;
670         sstate->hashtable = NULL;
671         sstate->hashnulls = NULL;
672         sstate->tablecxt = NULL;
673         sstate->innerecontext = NULL;
674         sstate->keyColIdx = NULL;
675         sstate->tab_hash_funcs = NULL;
676         sstate->tab_eq_funcs = NULL;
677         sstate->lhs_hash_funcs = NULL;
678         sstate->cur_eq_funcs = NULL;
679
680         /*
681          * If this plan is un-correlated or undirect correlated one and want to
682          * set params for parent plan then mark parameters as needing evaluation.
683          *
684          * Note that in the case of un-correlated subqueries we don't care about
685          * setting parent->chgParam here: indices take care about it, for others -
686          * it doesn't matter...
687          */
688         if (subplan->setParam != NIL)
689         {
690                 ListCell   *lst;
691
692                 foreach(lst, subplan->setParam)
693                 {
694                         int                     paramid = lfirst_int(lst);
695                         ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
696
697                         prm->execPlan = sstate;
698                 }
699         }
700
701         /*
702          * If we are going to hash the subquery output, initialize relevant stuff.
703          * (We don't create the hashtable until needed, though.)
704          */
705         if (subplan->useHashTable)
706         {
707                 int                     ncols,
708                                         i;
709                 TupleDesc       tupDesc;
710                 TupleTable      tupTable;
711                 TupleTableSlot *slot;
712                 List       *oplist,
713                                    *lefttlist,
714                                    *righttlist,
715                                    *leftptlist,
716                                    *rightptlist;
717                 ListCell   *l;
718
719                 /* We need a memory context to hold the hash table(s) */
720                 sstate->tablecxt =
721                         AllocSetContextCreate(CurrentMemoryContext,
722                                                                   "Subplan HashTable Context",
723                                                                   ALLOCSET_DEFAULT_MINSIZE,
724                                                                   ALLOCSET_DEFAULT_INITSIZE,
725                                                                   ALLOCSET_DEFAULT_MAXSIZE);
726                 /* and a short-lived exprcontext for function evaluation */
727                 sstate->innerecontext = CreateExprContext(estate);
728                 /* Silly little array of column numbers 1..n */
729                 ncols = list_length(subplan->paramIds);
730                 sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
731                 for (i = 0; i < ncols; i++)
732                         sstate->keyColIdx[i] = i + 1;
733
734                 /*
735                  * We use ExecProject to evaluate the lefthand and righthand
736                  * expression lists and form tuples.  (You might think that we could
737                  * use the sub-select's output tuples directly, but that is not the
738                  * case if we had to insert any run-time coercions of the sub-select's
739                  * output datatypes; anyway this avoids storing any resjunk columns
740                  * that might be in the sub-select's output.) Run through the
741                  * combining expressions to build tlists for the lefthand and
742                  * righthand sides.  We need both the ExprState list (for ExecProject)
743                  * and the underlying parse Exprs (for ExecTypeFromTL).
744                  *
745                  * We also extract the combining operators themselves to initialize
746                  * the equality and hashing functions for the hash tables.
747                  */
748                 if (IsA(sstate->testexpr->expr, OpExpr))
749                 {
750                         /* single combining operator */
751                         oplist = list_make1(sstate->testexpr);
752                 }
753                 else if (and_clause((Node *) sstate->testexpr->expr))
754                 {
755                         /* multiple combining operators */
756                         Assert(IsA(sstate->testexpr, BoolExprState));
757                         oplist = ((BoolExprState *) sstate->testexpr)->args;
758                 }
759                 else
760                 {
761                         /* shouldn't see anything else in a hashable subplan */
762                         elog(ERROR, "unrecognized testexpr type: %d",
763                                  (int) nodeTag(sstate->testexpr->expr));
764                         oplist = NIL;           /* keep compiler quiet */
765                 }
766                 Assert(list_length(oplist) == ncols);
767
768                 lefttlist = righttlist = NIL;
769                 leftptlist = rightptlist = NIL;
770                 sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
771                 sstate->tab_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
772                 sstate->lhs_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
773                 sstate->cur_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
774                 i = 1;
775                 foreach(l, oplist)
776                 {
777                         FuncExprState *fstate = (FuncExprState *) lfirst(l);
778                         OpExpr     *opexpr = (OpExpr *) fstate->xprstate.expr;
779                         ExprState  *exstate;
780                         Expr       *expr;
781                         TargetEntry *tle;
782                         GenericExprState *tlestate;
783                         Oid                     rhs_eq_oper;
784                         Oid                     left_hashfn;
785                         Oid                     right_hashfn;
786
787                         Assert(IsA(fstate, FuncExprState));
788                         Assert(IsA(opexpr, OpExpr));
789                         Assert(list_length(fstate->args) == 2);
790
791                         /* Process lefthand argument */
792                         exstate = (ExprState *) linitial(fstate->args);
793                         expr = exstate->expr;
794                         tle = makeTargetEntry(expr,
795                                                                   i,
796                                                                   NULL,
797                                                                   false);
798                         tlestate = makeNode(GenericExprState);
799                         tlestate->xprstate.expr = (Expr *) tle;
800                         tlestate->xprstate.evalfunc = NULL;
801                         tlestate->arg = exstate;
802                         lefttlist = lappend(lefttlist, tlestate);
803                         leftptlist = lappend(leftptlist, tle);
804
805                         /* Process righthand argument */
806                         exstate = (ExprState *) lsecond(fstate->args);
807                         expr = exstate->expr;
808                         tle = makeTargetEntry(expr,
809                                                                   i,
810                                                                   NULL,
811                                                                   false);
812                         tlestate = makeNode(GenericExprState);
813                         tlestate->xprstate.expr = (Expr *) tle;
814                         tlestate->xprstate.evalfunc = NULL;
815                         tlestate->arg = exstate;
816                         righttlist = lappend(righttlist, tlestate);
817                         rightptlist = lappend(rightptlist, tle);
818
819                         /* Lookup the equality function (potentially cross-type) */
820                         fmgr_info(opexpr->opfuncid, &sstate->cur_eq_funcs[i - 1]);
821                         sstate->cur_eq_funcs[i - 1].fn_expr = (Node *) opexpr;
822
823                         /* Look up the equality function for the RHS type */
824                         if (!get_compatible_hash_operators(opexpr->opno,
825                                                                                            NULL, &rhs_eq_oper))
826                                 elog(ERROR, "could not find compatible hash operator for operator %u",
827                                          opexpr->opno);
828                         fmgr_info(get_opcode(rhs_eq_oper), &sstate->tab_eq_funcs[i - 1]);
829
830                         /* Lookup the associated hash functions */
831                         if (!get_op_hash_functions(opexpr->opno,
832                                                                            &left_hashfn, &right_hashfn))
833                                 elog(ERROR, "could not find hash function for hash operator %u",
834                                          opexpr->opno);
835                         fmgr_info(left_hashfn, &sstate->lhs_hash_funcs[i - 1]);
836                         fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]);
837
838                         i++;
839                 }
840
841                 /*
842                  * Create a tupletable to hold these tuples.  (Note: we never bother
843                  * to free the tupletable explicitly; that's okay because it will
844                  * never store raw disk tuples that might have associated buffer pins.
845                  * The only resource involved is memory, which will be cleaned up by
846                  * freeing the query context.)
847                  */
848                 tupTable = ExecCreateTupleTable(2);
849
850                 /*
851                  * Construct tupdescs, slots and projection nodes for left and right
852                  * sides.  The lefthand expressions will be evaluated in the parent
853                  * plan node's exprcontext, which we don't have access to here.
854                  * Fortunately we can just pass NULL for now and fill it in later
855                  * (hack alert!).  The righthand expressions will be evaluated in our
856                  * own innerecontext.
857                  */
858                 tupDesc = ExecTypeFromTL(leftptlist, false);
859                 slot = ExecAllocTableSlot(tupTable);
860                 ExecSetSlotDescriptor(slot, tupDesc);
861                 sstate->projLeft = ExecBuildProjectionInfo(lefttlist,
862                                                                                                    NULL,
863                                                                                                    slot,
864                                                                                                    NULL);
865
866                 tupDesc = ExecTypeFromTL(rightptlist, false);
867                 slot = ExecAllocTableSlot(tupTable);
868                 ExecSetSlotDescriptor(slot, tupDesc);
869                 sstate->projRight = ExecBuildProjectionInfo(righttlist,
870                                                                                                         sstate->innerecontext,
871                                                                                                         slot,
872                                                                                                         NULL);
873         }
874
875         return sstate;
876 }
877
878 /* ----------------------------------------------------------------
879  *              ExecSetParamPlan
880  *
881  *              Executes an InitPlan subplan and sets its output parameters.
882  *
883  * This is called from ExecEvalParam() when the value of a PARAM_EXEC
884  * parameter is requested and the param's execPlan field is set (indicating
885  * that the param has not yet been evaluated).  This allows lazy evaluation
886  * of initplans: we don't run the subplan until/unless we need its output.
887  * Note that this routine MUST clear the execPlan fields of the plan's
888  * output parameters after evaluating them!
889  * ----------------------------------------------------------------
890  */
891 void
892 ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
893 {
894         SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
895         PlanState  *planstate = node->planstate;
896         SubLinkType subLinkType = subplan->subLinkType;
897         MemoryContext oldcontext;
898         TupleTableSlot *slot;
899         ListCell   *l;
900         bool            found = false;
901         ArrayBuildState *astate = NULL;
902
903         /*
904          * Must switch to per-query memory context.
905          */
906         oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
907
908         if (subLinkType == ANY_SUBLINK ||
909                 subLinkType == ALL_SUBLINK)
910                 elog(ERROR, "ANY/ALL subselect unsupported as initplan");
911
912         /*
913          * By definition, an initplan has no parameters from our query level, but
914          * it could have some from an outer level.      Rescan it if needed.
915          */
916         if (planstate->chgParam != NULL)
917                 ExecReScan(planstate, NULL);
918
919         for (slot = ExecProcNode(planstate);
920                  !TupIsNull(slot);
921                  slot = ExecProcNode(planstate))
922         {
923                 TupleDesc       tdesc = slot->tts_tupleDescriptor;
924                 int                     i = 1;
925
926                 if (subLinkType == EXISTS_SUBLINK)
927                 {
928                         /* There can be only one param... */
929                         int                     paramid = linitial_int(subplan->setParam);
930                         ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
931
932                         prm->execPlan = NULL;
933                         prm->value = BoolGetDatum(true);
934                         prm->isnull = false;
935                         found = true;
936                         break;
937                 }
938
939                 if (subLinkType == ARRAY_SUBLINK)
940                 {
941                         Datum           dvalue;
942                         bool            disnull;
943
944                         found = true;
945                         /* stash away current value */
946                         Assert(subplan->firstColType == tdesc->attrs[0]->atttypid);
947                         dvalue = slot_getattr(slot, 1, &disnull);
948                         astate = accumArrayResult(astate, dvalue, disnull,
949                                                                           subplan->firstColType, oldcontext);
950                         /* keep scanning subplan to collect all values */
951                         continue;
952                 }
953
954                 if (found &&
955                         (subLinkType == EXPR_SUBLINK ||
956                          subLinkType == ROWCOMPARE_SUBLINK))
957                         ereport(ERROR,
958                                         (errcode(ERRCODE_CARDINALITY_VIOLATION),
959                                          errmsg("more than one row returned by a subquery used as an expression")));
960
961                 found = true;
962
963                 /*
964                  * We need to copy the subplan's tuple into our own context, in case
965                  * any of the params are pass-by-ref type --- the pointers stored in
966                  * the param structs will point at this copied tuple! node->curTuple
967                  * keeps track of the copied tuple for eventual freeing.
968                  */
969                 if (node->curTuple)
970                         heap_freetuple(node->curTuple);
971                 node->curTuple = ExecCopySlotTuple(slot);
972
973                 /*
974                  * Now set all the setParam params from the columns of the tuple
975                  */
976                 foreach(l, subplan->setParam)
977                 {
978                         int                     paramid = lfirst_int(l);
979                         ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
980
981                         prm->execPlan = NULL;
982                         prm->value = heap_getattr(node->curTuple, i, tdesc,
983                                                                           &(prm->isnull));
984                         i++;
985                 }
986         }
987
988         if (subLinkType == ARRAY_SUBLINK)
989         {
990                 /* There can be only one param... */
991                 int                     paramid = linitial_int(subplan->setParam);
992                 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
993
994                 prm->execPlan = NULL;
995                 /* We build the result in query context so it won't disappear */
996                 if (astate != NULL)
997                         prm->value = makeArrayResult(astate,
998                                                                                  econtext->ecxt_per_query_memory);
999                 else
1000                 {
1001                         MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1002                         prm->value = PointerGetDatum(construct_empty_array(subplan->firstColType));
1003                 }
1004                 prm->isnull = false;
1005         }
1006         else if (!found)
1007         {
1008                 if (subLinkType == EXISTS_SUBLINK)
1009                 {
1010                         /* There can be only one param... */
1011                         int                     paramid = linitial_int(subplan->setParam);
1012                         ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1013
1014                         prm->execPlan = NULL;
1015                         prm->value = BoolGetDatum(false);
1016                         prm->isnull = false;
1017                 }
1018                 else
1019                 {
1020                         foreach(l, subplan->setParam)
1021                         {
1022                                 int                     paramid = lfirst_int(l);
1023                                 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1024
1025                                 prm->execPlan = NULL;
1026                                 prm->value = (Datum) 0;
1027                                 prm->isnull = true;
1028                         }
1029                 }
1030         }
1031
1032         MemoryContextSwitchTo(oldcontext);
1033 }
1034
1035 /*
1036  * Mark an initplan as needing recalculation
1037  */
1038 void
1039 ExecReScanSetParamPlan(SubPlanState *node, PlanState *parent)
1040 {
1041         PlanState  *planstate = node->planstate;
1042         SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
1043         EState     *estate = parent->state;
1044         ListCell   *l;
1045
1046         /* sanity checks */
1047         if (subplan->parParam != NIL)
1048                 elog(ERROR, "direct correlated subquery unsupported as initplan");
1049         if (subplan->setParam == NIL)
1050                 elog(ERROR, "setParam list of initplan is empty");
1051         if (bms_is_empty(planstate->plan->extParam))
1052                 elog(ERROR, "extParam set of initplan is empty");
1053
1054         /*
1055          * Don't actually re-scan: ExecSetParamPlan does it if needed.
1056          */
1057
1058         /*
1059          * Mark this subplan's output parameters as needing recalculation
1060          */
1061         foreach(l, subplan->setParam)
1062         {
1063                 int                     paramid = lfirst_int(l);
1064                 ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1065
1066                 prm->execPlan = node;
1067                 parent->chgParam = bms_add_member(parent->chgParam, paramid);
1068         }
1069 }