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