OSDN Git Service

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