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