Header And Logo

PostgreSQL
| The world's most advanced open source database.

nodeBitmapOr.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * nodeBitmapOr.c
00004  *    routines to handle BitmapOr nodes.
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/backend/executor/nodeBitmapOr.c
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 /* INTERFACE ROUTINES
00016  *      ExecInitBitmapOr    - initialize the BitmapOr node
00017  *      MultiExecBitmapOr   - retrieve the result bitmap from the node
00018  *      ExecEndBitmapOr     - shut down the BitmapOr node
00019  *      ExecReScanBitmapOr  - rescan the BitmapOr node
00020  *
00021  *   NOTES
00022  *      BitmapOr nodes don't make use of their left and right
00023  *      subtrees, rather they maintain a list of subplans,
00024  *      much like Append nodes.  The logic is much simpler than
00025  *      Append, however, since we needn't cope with forward/backward
00026  *      execution.
00027  */
00028 
00029 #include "postgres.h"
00030 
00031 #include "executor/execdebug.h"
00032 #include "executor/nodeBitmapOr.h"
00033 #include "miscadmin.h"
00034 
00035 
00036 /* ----------------------------------------------------------------
00037  *      ExecInitBitmapOr
00038  *
00039  *      Begin all of the subscans of the BitmapOr node.
00040  * ----------------------------------------------------------------
00041  */
00042 BitmapOrState *
00043 ExecInitBitmapOr(BitmapOr *node, EState *estate, int eflags)
00044 {
00045     BitmapOrState *bitmaporstate = makeNode(BitmapOrState);
00046     PlanState **bitmapplanstates;
00047     int         nplans;
00048     int         i;
00049     ListCell   *l;
00050     Plan       *initNode;
00051 
00052     /* check for unsupported flags */
00053     Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
00054 
00055     /*
00056      * Set up empty vector of subplan states
00057      */
00058     nplans = list_length(node->bitmapplans);
00059 
00060     bitmapplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
00061 
00062     /*
00063      * create new BitmapOrState for our BitmapOr node
00064      */
00065     bitmaporstate->ps.plan = (Plan *) node;
00066     bitmaporstate->ps.state = estate;
00067     bitmaporstate->bitmapplans = bitmapplanstates;
00068     bitmaporstate->nplans = nplans;
00069 
00070     /*
00071      * Miscellaneous initialization
00072      *
00073      * BitmapOr plans don't have expression contexts because they never call
00074      * ExecQual or ExecProject.  They don't need any tuple slots either.
00075      */
00076 
00077     /*
00078      * call ExecInitNode on each of the plans to be executed and save the
00079      * results into the array "bitmapplanstates".
00080      */
00081     i = 0;
00082     foreach(l, node->bitmapplans)
00083     {
00084         initNode = (Plan *) lfirst(l);
00085         bitmapplanstates[i] = ExecInitNode(initNode, estate, eflags);
00086         i++;
00087     }
00088 
00089     return bitmaporstate;
00090 }
00091 
00092 /* ----------------------------------------------------------------
00093  *     MultiExecBitmapOr
00094  * ----------------------------------------------------------------
00095  */
00096 Node *
00097 MultiExecBitmapOr(BitmapOrState *node)
00098 {
00099     PlanState **bitmapplans;
00100     int         nplans;
00101     int         i;
00102     TIDBitmap  *result = NULL;
00103 
00104     /* must provide our own instrumentation support */
00105     if (node->ps.instrument)
00106         InstrStartNode(node->ps.instrument);
00107 
00108     /*
00109      * get information from the node
00110      */
00111     bitmapplans = node->bitmapplans;
00112     nplans = node->nplans;
00113 
00114     /*
00115      * Scan all the subplans and OR their result bitmaps
00116      */
00117     for (i = 0; i < nplans; i++)
00118     {
00119         PlanState  *subnode = bitmapplans[i];
00120         TIDBitmap  *subresult;
00121 
00122         /*
00123          * We can special-case BitmapIndexScan children to avoid an explicit
00124          * tbm_union step for each child: just pass down the current result
00125          * bitmap and let the child OR directly into it.
00126          */
00127         if (IsA(subnode, BitmapIndexScanState))
00128         {
00129             if (result == NULL) /* first subplan */
00130             {
00131                 /* XXX should we use less than work_mem for this? */
00132                 result = tbm_create(work_mem * 1024L);
00133             }
00134 
00135             ((BitmapIndexScanState *) subnode)->biss_result = result;
00136 
00137             subresult = (TIDBitmap *) MultiExecProcNode(subnode);
00138 
00139             if (subresult != result)
00140                 elog(ERROR, "unrecognized result from subplan");
00141         }
00142         else
00143         {
00144             /* standard implementation */
00145             subresult = (TIDBitmap *) MultiExecProcNode(subnode);
00146 
00147             if (!subresult || !IsA(subresult, TIDBitmap))
00148                 elog(ERROR, "unrecognized result from subplan");
00149 
00150             if (result == NULL)
00151                 result = subresult;     /* first subplan */
00152             else
00153             {
00154                 tbm_union(result, subresult);
00155                 tbm_free(subresult);
00156             }
00157         }
00158     }
00159 
00160     /* We could return an empty result set here? */
00161     if (result == NULL)
00162         elog(ERROR, "BitmapOr doesn't support zero inputs");
00163 
00164     /* must provide our own instrumentation support */
00165     if (node->ps.instrument)
00166         InstrStopNode(node->ps.instrument, 0 /* XXX */ );
00167 
00168     return (Node *) result;
00169 }
00170 
00171 /* ----------------------------------------------------------------
00172  *      ExecEndBitmapOr
00173  *
00174  *      Shuts down the subscans of the BitmapOr node.
00175  *
00176  *      Returns nothing of interest.
00177  * ----------------------------------------------------------------
00178  */
00179 void
00180 ExecEndBitmapOr(BitmapOrState *node)
00181 {
00182     PlanState **bitmapplans;
00183     int         nplans;
00184     int         i;
00185 
00186     /*
00187      * get information from the node
00188      */
00189     bitmapplans = node->bitmapplans;
00190     nplans = node->nplans;
00191 
00192     /*
00193      * shut down each of the subscans (that we've initialized)
00194      */
00195     for (i = 0; i < nplans; i++)
00196     {
00197         if (bitmapplans[i])
00198             ExecEndNode(bitmapplans[i]);
00199     }
00200 }
00201 
00202 void
00203 ExecReScanBitmapOr(BitmapOrState *node)
00204 {
00205     int         i;
00206 
00207     for (i = 0; i < node->nplans; i++)
00208     {
00209         PlanState  *subnode = node->bitmapplans[i];
00210 
00211         /*
00212          * ExecReScan doesn't know about my subplans, so I have to do
00213          * changed-parameter signaling myself.
00214          */
00215         if (node->ps.chgParam != NULL)
00216             UpdateChangedParamSet(subnode, node->ps.chgParam);
00217 
00218         /*
00219          * If chgParam of subnode is not null then plan will be re-scanned by
00220          * first ExecProcNode.
00221          */
00222         if (subnode->chgParam == NULL)
00223             ExecReScan(subnode);
00224     }
00225 }