LCOV - code coverage report
Current view: top level - src/backend/executor - nodeWindowAgg.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 92.8 % 1442 1338
Test Date: 2026-04-22 22:16:24 Functions: 100.0 % 38 38
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * nodeWindowAgg.c
       4              :  *    routines to handle WindowAgg nodes.
       5              :  *
       6              :  * A WindowAgg node evaluates "window functions" across suitable partitions
       7              :  * of the input tuple set.  Any one WindowAgg works for just a single window
       8              :  * specification, though it can evaluate multiple window functions sharing
       9              :  * identical window specifications.  The input tuples are required to be
      10              :  * delivered in sorted order, with the PARTITION BY columns (if any) as
      11              :  * major sort keys and the ORDER BY columns (if any) as minor sort keys.
      12              :  * (The planner generates a stack of WindowAggs with intervening Sort nodes
      13              :  * as needed, if a query involves more than one window specification.)
      14              :  *
      15              :  * Since window functions can require access to any or all of the rows in
      16              :  * the current partition, we accumulate rows of the partition into a
      17              :  * tuplestore.  The window functions are called using the WindowObject API
      18              :  * so that they can access those rows as needed.
      19              :  *
      20              :  * We also support using plain aggregate functions as window functions.
      21              :  * For these, the regular Agg-node environment is emulated for each partition.
      22              :  * As required by the SQL spec, the output represents the value of the
      23              :  * aggregate function over all rows in the current row's window frame.
      24              :  *
      25              :  *
      26              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      27              :  * Portions Copyright (c) 1994, Regents of the University of California
      28              :  *
      29              :  * IDENTIFICATION
      30              :  *    src/backend/executor/nodeWindowAgg.c
      31              :  *
      32              :  *-------------------------------------------------------------------------
      33              :  */
      34              : #include "postgres.h"
      35              : 
      36              : #include "access/htup_details.h"
      37              : #include "catalog/objectaccess.h"
      38              : #include "catalog/pg_aggregate.h"
      39              : #include "catalog/pg_proc.h"
      40              : #include "common/int.h"
      41              : #include "executor/executor.h"
      42              : #include "executor/instrument.h"
      43              : #include "executor/nodeWindowAgg.h"
      44              : #include "miscadmin.h"
      45              : #include "nodes/nodeFuncs.h"
      46              : #include "optimizer/clauses.h"
      47              : #include "optimizer/optimizer.h"
      48              : #include "parser/parse_agg.h"
      49              : #include "parser/parse_coerce.h"
      50              : #include "utils/acl.h"
      51              : #include "utils/builtins.h"
      52              : #include "utils/datum.h"
      53              : #include "utils/expandeddatum.h"
      54              : #include "utils/lsyscache.h"
      55              : #include "utils/memutils.h"
      56              : #include "utils/regproc.h"
      57              : #include "utils/syscache.h"
      58              : #include "utils/tuplestore.h"
      59              : #include "windowapi.h"
      60              : 
      61              : /*
      62              :  * All the window function APIs are called with this object, which is passed
      63              :  * to window functions as fcinfo->context.
      64              :  */
      65              : typedef struct WindowObjectData
      66              : {
      67              :     NodeTag     type;
      68              :     WindowAggState *winstate;   /* parent WindowAggState */
      69              :     List       *argstates;      /* ExprState trees for fn's arguments */
      70              :     void       *localmem;       /* WinGetPartitionLocalMemory's chunk */
      71              :     int         markptr;        /* tuplestore mark pointer for this fn */
      72              :     int         readptr;        /* tuplestore read pointer for this fn */
      73              :     int64       markpos;        /* row that markptr is positioned on */
      74              :     int64       seekpos;        /* row that readptr is positioned on */
      75              :     uint8     **notnull_info;   /* not null info for each func args */
      76              :     int64      *num_notnull_info;   /* track size (number of tuples in
      77              :                                      * partition) of the notnull_info array
      78              :                                      * for each func args */
      79              : 
      80              :     /*
      81              :      * Null treatment options. One of: NO_NULLTREATMENT, PARSER_IGNORE_NULLS,
      82              :      * PARSER_RESPECT_NULLS or IGNORE_NULLS.
      83              :      */
      84              :     int         ignore_nulls;
      85              : } WindowObjectData;
      86              : 
      87              : /*
      88              :  * We have one WindowStatePerFunc struct for each window function and
      89              :  * window aggregate handled by this node.
      90              :  */
      91              : typedef struct WindowStatePerFuncData
      92              : {
      93              :     /* Links to WindowFunc expr and state nodes this working state is for */
      94              :     WindowFuncExprState *wfuncstate;
      95              :     WindowFunc *wfunc;
      96              : 
      97              :     int         numArguments;   /* number of arguments */
      98              : 
      99              :     FmgrInfo    flinfo;         /* fmgr lookup data for window function */
     100              : 
     101              :     Oid         winCollation;   /* collation derived for window function */
     102              : 
     103              :     /*
     104              :      * We need the len and byval info for the result of each function in order
     105              :      * to know how to copy/delete values.
     106              :      */
     107              :     int16       resulttypeLen;
     108              :     bool        resulttypeByVal;
     109              : 
     110              :     bool        plain_agg;      /* is it just a plain aggregate function? */
     111              :     int         aggno;          /* if so, index of its WindowStatePerAggData */
     112              :     uint8       ignore_nulls;   /* ignore nulls */
     113              : 
     114              :     WindowObject winobj;        /* object used in window function API */
     115              : } WindowStatePerFuncData;
     116              : 
     117              : /*
     118              :  * For plain aggregate window functions, we also have one of these.
     119              :  */
     120              : typedef struct WindowStatePerAggData
     121              : {
     122              :     /* Oids of transition functions */
     123              :     Oid         transfn_oid;
     124              :     Oid         invtransfn_oid; /* may be InvalidOid */
     125              :     Oid         finalfn_oid;    /* may be InvalidOid */
     126              : 
     127              :     /*
     128              :      * fmgr lookup data for transition functions --- only valid when
     129              :      * corresponding oid is not InvalidOid.  Note in particular that fn_strict
     130              :      * flags are kept here.
     131              :      */
     132              :     FmgrInfo    transfn;
     133              :     FmgrInfo    invtransfn;
     134              :     FmgrInfo    finalfn;
     135              : 
     136              :     int         numFinalArgs;   /* number of arguments to pass to finalfn */
     137              : 
     138              :     /*
     139              :      * initial value from pg_aggregate entry
     140              :      */
     141              :     Datum       initValue;
     142              :     bool        initValueIsNull;
     143              : 
     144              :     /*
     145              :      * cached value for current frame boundaries
     146              :      */
     147              :     Datum       resultValue;
     148              :     bool        resultValueIsNull;
     149              : 
     150              :     /*
     151              :      * We need the len and byval info for the agg's input, result, and
     152              :      * transition data types in order to know how to copy/delete values.
     153              :      */
     154              :     int16       inputtypeLen,
     155              :                 resulttypeLen,
     156              :                 transtypeLen;
     157              :     bool        inputtypeByVal,
     158              :                 resulttypeByVal,
     159              :                 transtypeByVal;
     160              : 
     161              :     int         wfuncno;        /* index of associated WindowStatePerFuncData */
     162              : 
     163              :     /* Context holding transition value and possibly other subsidiary data */
     164              :     MemoryContext aggcontext;   /* may be private, or winstate->aggcontext */
     165              : 
     166              :     /* Current transition value */
     167              :     Datum       transValue;     /* current transition value */
     168              :     bool        transValueIsNull;
     169              : 
     170              :     int64       transValueCount;    /* number of currently-aggregated rows */
     171              : 
     172              :     /* Data local to eval_windowaggregates() */
     173              :     bool        restart;        /* need to restart this agg in this cycle? */
     174              : } WindowStatePerAggData;
     175              : 
     176              : static void initialize_windowaggregate(WindowAggState *winstate,
     177              :                                        WindowStatePerFunc perfuncstate,
     178              :                                        WindowStatePerAgg peraggstate);
     179              : static void advance_windowaggregate(WindowAggState *winstate,
     180              :                                     WindowStatePerFunc perfuncstate,
     181              :                                     WindowStatePerAgg peraggstate);
     182              : static bool advance_windowaggregate_base(WindowAggState *winstate,
     183              :                                          WindowStatePerFunc perfuncstate,
     184              :                                          WindowStatePerAgg peraggstate);
     185              : static void finalize_windowaggregate(WindowAggState *winstate,
     186              :                                      WindowStatePerFunc perfuncstate,
     187              :                                      WindowStatePerAgg peraggstate,
     188              :                                      Datum *result, bool *isnull);
     189              : 
     190              : static void eval_windowaggregates(WindowAggState *winstate);
     191              : static void eval_windowfunction(WindowAggState *winstate,
     192              :                                 WindowStatePerFunc perfuncstate,
     193              :                                 Datum *result, bool *isnull);
     194              : 
     195              : static void begin_partition(WindowAggState *winstate);
     196              : static void spool_tuples(WindowAggState *winstate, int64 pos);
     197              : static void release_partition(WindowAggState *winstate);
     198              : 
     199              : static int  row_is_in_frame(WindowObject winobj, int64 pos,
     200              :                             TupleTableSlot *slot, bool fetch_tuple);
     201              : static void update_frameheadpos(WindowAggState *winstate);
     202              : static void update_frametailpos(WindowAggState *winstate);
     203              : static void update_grouptailpos(WindowAggState *winstate);
     204              : 
     205              : static WindowStatePerAggData *initialize_peragg(WindowAggState *winstate,
     206              :                                                 WindowFunc *wfunc,
     207              :                                                 WindowStatePerAgg peraggstate);
     208              : static Datum GetAggInitVal(Datum textInitVal, Oid transtype);
     209              : 
     210              : static bool are_peers(WindowAggState *winstate, TupleTableSlot *slot1,
     211              :                       TupleTableSlot *slot2);
     212              : static bool window_gettupleslot(WindowObject winobj, int64 pos,
     213              :                                 TupleTableSlot *slot);
     214              : 
     215              : static Datum ignorenulls_getfuncarginframe(WindowObject winobj, int argno,
     216              :                                            int relpos, int seektype,
     217              :                                            bool set_mark, bool *isnull,
     218              :                                            bool *isout);
     219              : static Datum gettuple_eval_partition(WindowObject winobj, int argno,
     220              :                                      int64 abs_pos, bool *isnull,
     221              :                                      bool *isout);
     222              : static void init_notnull_info(WindowObject winobj,
     223              :                               WindowStatePerFunc perfuncstate);
     224              : static void grow_notnull_info(WindowObject winobj,
     225              :                               int64 pos, int argno);
     226              : static uint8 get_notnull_info(WindowObject winobj,
     227              :                               int64 pos, int argno);
     228              : static void put_notnull_info(WindowObject winobj,
     229              :                              int64 pos, int argno, bool isnull);
     230              : 
     231              : /*
     232              :  * Not null info bit array consists of 2-bit items
     233              :  */
     234              : #define NN_UNKNOWN  0x00        /* value not calculated yet */
     235              : #define NN_NULL     0x01        /* NULL */
     236              : #define NN_NOTNULL  0x02        /* NOT NULL */
     237              : #define NN_MASK     0x03        /* mask for NOT NULL MAP */
     238              : #define NN_BITS_PER_MEMBER  2   /* number of bits in not null map */
     239              : /* number of items per variable */
     240              : #define NN_ITEM_PER_VAR (BITS_PER_BYTE / NN_BITS_PER_MEMBER)
     241              : /* convert map position to byte offset */
     242              : #define NN_POS_TO_BYTES(pos)    ((pos) / NN_ITEM_PER_VAR)
     243              : /* bytes offset to map position */
     244              : #define NN_BYTES_TO_POS(bytes)  ((bytes) * NN_ITEM_PER_VAR)
     245              : /* calculate shift bits */
     246              : #define NN_SHIFT(pos)   ((pos) % NN_ITEM_PER_VAR) * NN_BITS_PER_MEMBER
     247              : 
     248              : /*
     249              :  * initialize_windowaggregate
     250              :  * parallel to initialize_aggregates in nodeAgg.c
     251              :  */
     252              : static void
     253         2838 : initialize_windowaggregate(WindowAggState *winstate,
     254              :                            WindowStatePerFunc perfuncstate,
     255              :                            WindowStatePerAgg peraggstate)
     256              : {
     257              :     MemoryContext oldContext;
     258              : 
     259              :     /*
     260              :      * If we're using a private aggcontext, we may reset it here.  But if the
     261              :      * context is shared, we don't know which other aggregates may still need
     262              :      * it, so we must leave it to the caller to reset at an appropriate time.
     263              :      */
     264         2838 :     if (peraggstate->aggcontext != winstate->aggcontext)
     265         2075 :         MemoryContextReset(peraggstate->aggcontext);
     266              : 
     267         2838 :     if (peraggstate->initValueIsNull)
     268         1052 :         peraggstate->transValue = peraggstate->initValue;
     269              :     else
     270              :     {
     271         1786 :         oldContext = MemoryContextSwitchTo(peraggstate->aggcontext);
     272         3572 :         peraggstate->transValue = datumCopy(peraggstate->initValue,
     273         1786 :                                             peraggstate->transtypeByVal,
     274         1786 :                                             peraggstate->transtypeLen);
     275         1786 :         MemoryContextSwitchTo(oldContext);
     276              :     }
     277         2838 :     peraggstate->transValueIsNull = peraggstate->initValueIsNull;
     278         2838 :     peraggstate->transValueCount = 0;
     279         2838 :     peraggstate->resultValue = (Datum) 0;
     280         2838 :     peraggstate->resultValueIsNull = true;
     281         2838 : }
     282              : 
     283              : /*
     284              :  * advance_windowaggregate
     285              :  * parallel to advance_aggregates in nodeAgg.c
     286              :  */
     287              : static void
     288       119377 : advance_windowaggregate(WindowAggState *winstate,
     289              :                         WindowStatePerFunc perfuncstate,
     290              :                         WindowStatePerAgg peraggstate)
     291              : {
     292       119377 :     LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
     293       119377 :     WindowFuncExprState *wfuncstate = perfuncstate->wfuncstate;
     294       119377 :     int         numArguments = perfuncstate->numArguments;
     295              :     Datum       newVal;
     296              :     ListCell   *arg;
     297              :     int         i;
     298              :     MemoryContext oldContext;
     299       119377 :     ExprContext *econtext = winstate->tmpcontext;
     300       119377 :     ExprState  *filter = wfuncstate->aggfilter;
     301              : 
     302       119377 :     oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
     303              : 
     304              :     /* Skip anything FILTERed out */
     305       119377 :     if (filter)
     306              :     {
     307              :         bool        isnull;
     308          228 :         Datum       res = ExecEvalExpr(filter, econtext, &isnull);
     309              : 
     310          228 :         if (isnull || !DatumGetBool(res))
     311              :         {
     312          108 :             MemoryContextSwitchTo(oldContext);
     313          108 :             return;
     314              :         }
     315              :     }
     316              : 
     317              :     /* We start from 1, since the 0th arg will be the transition value */
     318       119269 :     i = 1;
     319       198318 :     foreach(arg, wfuncstate->args)
     320              :     {
     321        79049 :         ExprState  *argstate = (ExprState *) lfirst(arg);
     322              : 
     323        79049 :         fcinfo->args[i].value = ExecEvalExpr(argstate, econtext,
     324              :                                              &fcinfo->args[i].isnull);
     325        79049 :         i++;
     326              :     }
     327              : 
     328       119269 :     if (peraggstate->transfn.fn_strict)
     329              :     {
     330              :         /*
     331              :          * For a strict transfn, nothing happens when there's a NULL input; we
     332              :          * just keep the prior transValue.  Note transValueCount doesn't
     333              :          * change either.
     334              :          */
     335        70114 :         for (i = 1; i <= numArguments; i++)
     336              :         {
     337        14949 :             if (fcinfo->args[i].isnull)
     338              :             {
     339          132 :                 MemoryContextSwitchTo(oldContext);
     340          132 :                 return;
     341              :             }
     342              :         }
     343              : 
     344              :         /*
     345              :          * For strict transition functions with initial value NULL we use the
     346              :          * first non-NULL input as the initial state.  (We already checked
     347              :          * that the agg's input type is binary-compatible with its transtype,
     348              :          * so straight copy here is OK.)
     349              :          *
     350              :          * We must copy the datum into aggcontext if it is pass-by-ref.  We do
     351              :          * not need to pfree the old transValue, since it's NULL.
     352              :          */
     353        55165 :         if (peraggstate->transValueCount == 0 && peraggstate->transValueIsNull)
     354              :         {
     355          312 :             MemoryContextSwitchTo(peraggstate->aggcontext);
     356          624 :             peraggstate->transValue = datumCopy(fcinfo->args[1].value,
     357          312 :                                                 peraggstate->transtypeByVal,
     358          312 :                                                 peraggstate->transtypeLen);
     359          312 :             peraggstate->transValueIsNull = false;
     360          312 :             peraggstate->transValueCount = 1;
     361          312 :             MemoryContextSwitchTo(oldContext);
     362          312 :             return;
     363              :         }
     364              : 
     365        54853 :         if (peraggstate->transValueIsNull)
     366              :         {
     367              :             /*
     368              :              * Don't call a strict function with NULL inputs.  Note it is
     369              :              * possible to get here despite the above tests, if the transfn is
     370              :              * strict *and* returned a NULL on a prior cycle.  If that happens
     371              :              * we will propagate the NULL all the way to the end.  That can
     372              :              * only happen if there's no inverse transition function, though,
     373              :              * since we disallow transitions back to NULL when there is one.
     374              :              */
     375            0 :             MemoryContextSwitchTo(oldContext);
     376              :             Assert(!OidIsValid(peraggstate->invtransfn_oid));
     377            0 :             return;
     378              :         }
     379              :     }
     380              : 
     381              :     /*
     382              :      * OK to call the transition function.  Set winstate->curaggcontext while
     383              :      * calling it, for possible use by AggCheckCallContext.
     384              :      */
     385       118825 :     InitFunctionCallInfoData(*fcinfo, &(peraggstate->transfn),
     386              :                              numArguments + 1,
     387              :                              perfuncstate->winCollation,
     388              :                              (Node *) winstate, NULL);
     389       118825 :     fcinfo->args[0].value = peraggstate->transValue;
     390       118825 :     fcinfo->args[0].isnull = peraggstate->transValueIsNull;
     391       118825 :     winstate->curaggcontext = peraggstate->aggcontext;
     392       118825 :     newVal = FunctionCallInvoke(fcinfo);
     393       118817 :     winstate->curaggcontext = NULL;
     394              : 
     395              :     /*
     396              :      * Moving-aggregate transition functions must not return null, see
     397              :      * advance_windowaggregate_base().
     398              :      */
     399       118817 :     if (fcinfo->isnull && OidIsValid(peraggstate->invtransfn_oid))
     400            0 :         ereport(ERROR,
     401              :                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
     402              :                  errmsg("moving-aggregate transition function must not return null")));
     403              : 
     404              :     /*
     405              :      * We must track the number of rows included in transValue, since to
     406              :      * remove the last input, advance_windowaggregate_base() mustn't call the
     407              :      * inverse transition function, but simply reset transValue back to its
     408              :      * initial value.
     409              :      */
     410       118817 :     peraggstate->transValueCount++;
     411              : 
     412              :     /*
     413              :      * If pass-by-ref datatype, must copy the new value into aggcontext and
     414              :      * free the prior transValue.  But if transfn returned a pointer to its
     415              :      * first input, we don't need to do anything.  Also, if transfn returned a
     416              :      * pointer to a R/W expanded object that is already a child of the
     417              :      * aggcontext, assume we can adopt that value without copying it.  (See
     418              :      * comments for ExecAggCopyTransValue, which this code duplicates.)
     419              :      */
     420       124609 :     if (!peraggstate->transtypeByVal &&
     421         5792 :         DatumGetPointer(newVal) != DatumGetPointer(peraggstate->transValue))
     422              :     {
     423          640 :         if (!fcinfo->isnull)
     424              :         {
     425          640 :             MemoryContextSwitchTo(peraggstate->aggcontext);
     426          640 :             if (DatumIsReadWriteExpandedObject(newVal,
     427              :                                                false,
     428          644 :                                                peraggstate->transtypeLen) &&
     429            4 :                 MemoryContextGetParent(DatumGetEOHP(newVal)->eoh_context) == CurrentMemoryContext)
     430              :                  /* do nothing */ ;
     431              :             else
     432          636 :                 newVal = datumCopy(newVal,
     433          636 :                                    peraggstate->transtypeByVal,
     434          636 :                                    peraggstate->transtypeLen);
     435              :         }
     436          640 :         if (!peraggstate->transValueIsNull)
     437              :         {
     438          600 :             if (DatumIsReadWriteExpandedObject(peraggstate->transValue,
     439              :                                                false,
     440              :                                                peraggstate->transtypeLen))
     441            0 :                 DeleteExpandedObject(peraggstate->transValue);
     442              :             else
     443          600 :                 pfree(DatumGetPointer(peraggstate->transValue));
     444              :         }
     445              :     }
     446              : 
     447       118817 :     MemoryContextSwitchTo(oldContext);
     448       118817 :     peraggstate->transValue = newVal;
     449       118817 :     peraggstate->transValueIsNull = fcinfo->isnull;
     450              : }
     451              : 
     452              : /*
     453              :  * advance_windowaggregate_base
     454              :  * Remove the oldest tuple from an aggregation.
     455              :  *
     456              :  * This is very much like advance_windowaggregate, except that we will call
     457              :  * the inverse transition function (which caller must have checked is
     458              :  * available).
     459              :  *
     460              :  * Returns true if we successfully removed the current row from this
     461              :  * aggregate, false if not (in the latter case, caller is responsible
     462              :  * for cleaning up by restarting the aggregation).
     463              :  */
     464              : static bool
     465         3116 : advance_windowaggregate_base(WindowAggState *winstate,
     466              :                              WindowStatePerFunc perfuncstate,
     467              :                              WindowStatePerAgg peraggstate)
     468              : {
     469         3116 :     LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
     470         3116 :     WindowFuncExprState *wfuncstate = perfuncstate->wfuncstate;
     471         3116 :     int         numArguments = perfuncstate->numArguments;
     472              :     Datum       newVal;
     473              :     ListCell   *arg;
     474              :     int         i;
     475              :     MemoryContext oldContext;
     476         3116 :     ExprContext *econtext = winstate->tmpcontext;
     477         3116 :     ExprState  *filter = wfuncstate->aggfilter;
     478              : 
     479         3116 :     oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
     480              : 
     481              :     /* Skip anything FILTERed out */
     482         3116 :     if (filter)
     483              :     {
     484              :         bool        isnull;
     485           68 :         Datum       res = ExecEvalExpr(filter, econtext, &isnull);
     486              : 
     487           68 :         if (isnull || !DatumGetBool(res))
     488              :         {
     489           32 :             MemoryContextSwitchTo(oldContext);
     490           32 :             return true;
     491              :         }
     492              :     }
     493              : 
     494              :     /* We start from 1, since the 0th arg will be the transition value */
     495         3084 :     i = 1;
     496         6156 :     foreach(arg, wfuncstate->args)
     497              :     {
     498         3072 :         ExprState  *argstate = (ExprState *) lfirst(arg);
     499              : 
     500         3072 :         fcinfo->args[i].value = ExecEvalExpr(argstate, econtext,
     501              :                                              &fcinfo->args[i].isnull);
     502         3072 :         i++;
     503              :     }
     504              : 
     505         3084 :     if (peraggstate->invtransfn.fn_strict)
     506              :     {
     507              :         /*
     508              :          * For a strict (inv)transfn, nothing happens when there's a NULL
     509              :          * input; we just keep the prior transValue.  Note transValueCount
     510              :          * doesn't change either.
     511              :          */
     512         3800 :         for (i = 1; i <= numArguments; i++)
     513              :         {
     514         1920 :             if (fcinfo->args[i].isnull)
     515              :             {
     516           52 :                 MemoryContextSwitchTo(oldContext);
     517           52 :                 return true;
     518              :             }
     519              :         }
     520              :     }
     521              : 
     522              :     /* There should still be an added but not yet removed value */
     523              :     Assert(peraggstate->transValueCount > 0);
     524              : 
     525              :     /*
     526              :      * In moving-aggregate mode, the state must never be NULL, except possibly
     527              :      * before any rows have been aggregated (which is surely not the case at
     528              :      * this point).  This restriction allows us to interpret a NULL result
     529              :      * from the inverse function as meaning "sorry, can't do an inverse
     530              :      * transition in this case".  We already checked this in
     531              :      * advance_windowaggregate, but just for safety, check again.
     532              :      */
     533         3032 :     if (peraggstate->transValueIsNull)
     534            0 :         elog(ERROR, "aggregate transition value is NULL before inverse transition");
     535              : 
     536              :     /*
     537              :      * We mustn't use the inverse transition function to remove the last
     538              :      * input.  Doing so would yield a non-NULL state, whereas we should be in
     539              :      * the initial state afterwards which may very well be NULL.  So instead,
     540              :      * we simply re-initialize the aggregate in this case.
     541              :      */
     542         3032 :     if (peraggstate->transValueCount == 1)
     543              :     {
     544           60 :         MemoryContextSwitchTo(oldContext);
     545           60 :         initialize_windowaggregate(winstate,
     546           60 :                                    &winstate->perfunc[peraggstate->wfuncno],
     547              :                                    peraggstate);
     548           60 :         return true;
     549              :     }
     550              : 
     551              :     /*
     552              :      * OK to call the inverse transition function.  Set
     553              :      * winstate->curaggcontext while calling it, for possible use by
     554              :      * AggCheckCallContext.
     555              :      */
     556         2972 :     InitFunctionCallInfoData(*fcinfo, &(peraggstate->invtransfn),
     557              :                              numArguments + 1,
     558              :                              perfuncstate->winCollation,
     559              :                              (Node *) winstate, NULL);
     560         2972 :     fcinfo->args[0].value = peraggstate->transValue;
     561         2972 :     fcinfo->args[0].isnull = peraggstate->transValueIsNull;
     562         2972 :     winstate->curaggcontext = peraggstate->aggcontext;
     563         2972 :     newVal = FunctionCallInvoke(fcinfo);
     564         2972 :     winstate->curaggcontext = NULL;
     565              : 
     566              :     /*
     567              :      * If the function returns NULL, report failure, forcing a restart.
     568              :      */
     569         2972 :     if (fcinfo->isnull)
     570              :     {
     571          171 :         MemoryContextSwitchTo(oldContext);
     572          171 :         return false;
     573              :     }
     574              : 
     575              :     /* Update number of rows included in transValue */
     576         2801 :     peraggstate->transValueCount--;
     577              : 
     578              :     /*
     579              :      * If pass-by-ref datatype, must copy the new value into aggcontext and
     580              :      * free the prior transValue.  But if invtransfn returned a pointer to its
     581              :      * first input, we don't need to do anything.  Also, if invtransfn
     582              :      * returned a pointer to a R/W expanded object that is already a child of
     583              :      * the aggcontext, assume we can adopt that value without copying it. (See
     584              :      * comments for ExecAggCopyTransValue, which this code duplicates.)
     585              :      *
     586              :      * Note: the checks for null values here will never fire, but it seems
     587              :      * best to have this stanza look just like advance_windowaggregate.
     588              :      */
     589         4253 :     if (!peraggstate->transtypeByVal &&
     590         1452 :         DatumGetPointer(newVal) != DatumGetPointer(peraggstate->transValue))
     591              :     {
     592          444 :         if (!fcinfo->isnull)
     593              :         {
     594          444 :             MemoryContextSwitchTo(peraggstate->aggcontext);
     595          444 :             if (DatumIsReadWriteExpandedObject(newVal,
     596              :                                                false,
     597          444 :                                                peraggstate->transtypeLen) &&
     598            0 :                 MemoryContextGetParent(DatumGetEOHP(newVal)->eoh_context) == CurrentMemoryContext)
     599              :                  /* do nothing */ ;
     600              :             else
     601          444 :                 newVal = datumCopy(newVal,
     602          444 :                                    peraggstate->transtypeByVal,
     603          444 :                                    peraggstate->transtypeLen);
     604              :         }
     605          444 :         if (!peraggstate->transValueIsNull)
     606              :         {
     607          444 :             if (DatumIsReadWriteExpandedObject(peraggstate->transValue,
     608              :                                                false,
     609              :                                                peraggstate->transtypeLen))
     610            0 :                 DeleteExpandedObject(peraggstate->transValue);
     611              :             else
     612          444 :                 pfree(DatumGetPointer(peraggstate->transValue));
     613              :         }
     614              :     }
     615              : 
     616         2801 :     MemoryContextSwitchTo(oldContext);
     617         2801 :     peraggstate->transValue = newVal;
     618         2801 :     peraggstate->transValueIsNull = fcinfo->isnull;
     619              : 
     620         2801 :     return true;
     621              : }
     622              : 
     623              : /*
     624              :  * finalize_windowaggregate
     625              :  * parallel to finalize_aggregate in nodeAgg.c
     626              :  */
     627              : static void
     628         7323 : finalize_windowaggregate(WindowAggState *winstate,
     629              :                          WindowStatePerFunc perfuncstate,
     630              :                          WindowStatePerAgg peraggstate,
     631              :                          Datum *result, bool *isnull)
     632              : {
     633              :     MemoryContext oldContext;
     634              : 
     635         7323 :     oldContext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
     636              : 
     637              :     /*
     638              :      * Apply the agg's finalfn if one is provided, else return transValue.
     639              :      */
     640         7323 :     if (OidIsValid(peraggstate->finalfn_oid))
     641              :     {
     642         4156 :         LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
     643         4156 :         int         numFinalArgs = peraggstate->numFinalArgs;
     644              :         bool        anynull;
     645              :         int         i;
     646              : 
     647         4156 :         InitFunctionCallInfoData(fcinfodata.fcinfo, &(peraggstate->finalfn),
     648              :                                  numFinalArgs,
     649              :                                  perfuncstate->winCollation,
     650              :                                  (Node *) winstate, NULL);
     651         4156 :         fcinfo->args[0].value =
     652         4156 :             MakeExpandedObjectReadOnly(peraggstate->transValue,
     653              :                                        peraggstate->transValueIsNull,
     654              :                                        peraggstate->transtypeLen);
     655         4156 :         fcinfo->args[0].isnull = peraggstate->transValueIsNull;
     656         4156 :         anynull = peraggstate->transValueIsNull;
     657              : 
     658              :         /* Fill any remaining argument positions with nulls */
     659         4216 :         for (i = 1; i < numFinalArgs; i++)
     660              :         {
     661           60 :             fcinfo->args[i].value = (Datum) 0;
     662           60 :             fcinfo->args[i].isnull = true;
     663           60 :             anynull = true;
     664              :         }
     665              : 
     666         4156 :         if (fcinfo->flinfo->fn_strict && anynull)
     667              :         {
     668              :             /* don't call a strict function with NULL inputs */
     669            0 :             *result = (Datum) 0;
     670            0 :             *isnull = true;
     671              :         }
     672              :         else
     673              :         {
     674              :             Datum       res;
     675              : 
     676         4156 :             winstate->curaggcontext = peraggstate->aggcontext;
     677         4156 :             res = FunctionCallInvoke(fcinfo);
     678         4148 :             winstate->curaggcontext = NULL;
     679         4148 :             *isnull = fcinfo->isnull;
     680         4148 :             *result = MakeExpandedObjectReadOnly(res,
     681              :                                                  fcinfo->isnull,
     682              :                                                  peraggstate->resulttypeLen);
     683              :         }
     684              :     }
     685              :     else
     686              :     {
     687         3167 :         *result =
     688         3167 :             MakeExpandedObjectReadOnly(peraggstate->transValue,
     689              :                                        peraggstate->transValueIsNull,
     690              :                                        peraggstate->transtypeLen);
     691         3167 :         *isnull = peraggstate->transValueIsNull;
     692              :     }
     693              : 
     694         7315 :     MemoryContextSwitchTo(oldContext);
     695         7315 : }
     696              : 
     697              : /*
     698              :  * eval_windowaggregates
     699              :  * evaluate plain aggregates being used as window functions
     700              :  *
     701              :  * This differs from nodeAgg.c in two ways.  First, if the window's frame
     702              :  * start position moves, we use the inverse transition function (if it exists)
     703              :  * to remove rows from the transition value.  And second, we expect to be
     704              :  * able to call aggregate final functions repeatedly after aggregating more
     705              :  * data onto the same transition value.  This is not a behavior required by
     706              :  * nodeAgg.c.
     707              :  */
     708              : static void
     709       107065 : eval_windowaggregates(WindowAggState *winstate)
     710              : {
     711              :     WindowStatePerAgg peraggstate;
     712              :     int         wfuncno,
     713              :                 numaggs,
     714              :                 numaggs_restart,
     715              :                 i;
     716              :     int64       aggregatedupto_nonrestarted;
     717              :     MemoryContext oldContext;
     718              :     ExprContext *econtext;
     719              :     WindowObject agg_winobj;
     720              :     TupleTableSlot *agg_row_slot;
     721              :     TupleTableSlot *temp_slot;
     722              : 
     723       107065 :     numaggs = winstate->numaggs;
     724       107065 :     if (numaggs == 0)
     725            0 :         return;                 /* nothing to do */
     726              : 
     727              :     /* final output execution is in ps_ExprContext */
     728       107065 :     econtext = winstate->ss.ps.ps_ExprContext;
     729       107065 :     agg_winobj = winstate->agg_winobj;
     730       107065 :     agg_row_slot = winstate->agg_row_slot;
     731       107065 :     temp_slot = winstate->temp_slot_1;
     732              : 
     733              :     /*
     734              :      * If the window's frame start clause is UNBOUNDED_PRECEDING and no
     735              :      * exclusion clause is specified, then the window frame consists of a
     736              :      * contiguous group of rows extending forward from the start of the
     737              :      * partition, and rows only enter the frame, never exit it, as the current
     738              :      * row advances forward.  This makes it possible to use an incremental
     739              :      * strategy for evaluating aggregates: we run the transition function for
     740              :      * each row added to the frame, and run the final function whenever we
     741              :      * need the current aggregate value.  This is considerably more efficient
     742              :      * than the naive approach of re-running the entire aggregate calculation
     743              :      * for each current row.  It does assume that the final function doesn't
     744              :      * damage the running transition value, but we have the same assumption in
     745              :      * nodeAgg.c too (when it rescans an existing hash table).
     746              :      *
     747              :      * If the frame start does sometimes move, we can still optimize as above
     748              :      * whenever successive rows share the same frame head, but if the frame
     749              :      * head moves beyond the previous head we try to remove those rows using
     750              :      * the aggregate's inverse transition function.  This function restores
     751              :      * the aggregate's current state to what it would be if the removed row
     752              :      * had never been aggregated in the first place.  Inverse transition
     753              :      * functions may optionally return NULL, indicating that the function was
     754              :      * unable to remove the tuple from aggregation.  If this happens, or if
     755              :      * the aggregate doesn't have an inverse transition function at all, we
     756              :      * must perform the aggregation all over again for all tuples within the
     757              :      * new frame boundaries.
     758              :      *
     759              :      * If there's any exclusion clause, then we may have to aggregate over a
     760              :      * non-contiguous set of rows, so we punt and recalculate for every row.
     761              :      * (For some frame end choices, it might be that the frame is always
     762              :      * contiguous anyway, but that's an optimization to investigate later.)
     763              :      *
     764              :      * In many common cases, multiple rows share the same frame and hence the
     765              :      * same aggregate value. (In particular, if there's no ORDER BY in a RANGE
     766              :      * window, then all rows are peers and so they all have window frame equal
     767              :      * to the whole partition.)  We optimize such cases by calculating the
     768              :      * aggregate value once when we reach the first row of a peer group, and
     769              :      * then returning the saved value for all subsequent rows.
     770              :      *
     771              :      * 'aggregatedupto' keeps track of the first row that has not yet been
     772              :      * accumulated into the aggregate transition values.  Whenever we start a
     773              :      * new peer group, we accumulate forward to the end of the peer group.
     774              :      */
     775              : 
     776              :     /*
     777              :      * First, update the frame head position.
     778              :      *
     779              :      * The frame head should never move backwards, and the code below wouldn't
     780              :      * cope if it did, so for safety we complain if it does.
     781              :      */
     782       107065 :     update_frameheadpos(winstate);
     783       107061 :     if (winstate->frameheadpos < winstate->aggregatedbase)
     784            0 :         elog(ERROR, "window frame head moved backward");
     785              : 
     786              :     /*
     787              :      * If the frame didn't change compared to the previous row, we can re-use
     788              :      * the result values that were previously saved at the bottom of this
     789              :      * function.  Since we don't know the current frame's end yet, this is not
     790              :      * possible to check for fully.  But if the frame end mode is UNBOUNDED
     791              :      * FOLLOWING or CURRENT ROW, no exclusion clause is specified, and the
     792              :      * current row lies within the previous row's frame, then the two frames'
     793              :      * ends must coincide.  Note that on the first row aggregatedbase ==
     794              :      * aggregatedupto, meaning this test must fail, so we don't need to check
     795              :      * the "there was no previous row" case explicitly here.
     796              :      */
     797       107061 :     if (winstate->aggregatedbase == winstate->frameheadpos &&
     798       104489 :         (winstate->frameOptions & (FRAMEOPTION_END_UNBOUNDED_FOLLOWING |
     799       103141 :                                    FRAMEOPTION_END_CURRENT_ROW)) &&
     800       103141 :         !(winstate->frameOptions & FRAMEOPTION_EXCLUSION) &&
     801       103021 :         winstate->aggregatedbase <= winstate->currentpos &&
     802       102961 :         winstate->aggregatedupto > winstate->currentpos)
     803              :     {
     804       201828 :         for (i = 0; i < numaggs; i++)
     805              :         {
     806       100918 :             peraggstate = &winstate->peragg[i];
     807       100918 :             wfuncno = peraggstate->wfuncno;
     808       100918 :             econtext->ecxt_aggvalues[wfuncno] = peraggstate->resultValue;
     809       100918 :             econtext->ecxt_aggnulls[wfuncno] = peraggstate->resultValueIsNull;
     810              :         }
     811       100910 :         return;
     812              :     }
     813              : 
     814              :     /*----------
     815              :      * Initialize restart flags.
     816              :      *
     817              :      * We restart the aggregation:
     818              :      *   - if we're processing the first row in the partition, or
     819              :      *   - if the frame's head moved and we cannot use an inverse
     820              :      *     transition function, or
     821              :      *   - we have an EXCLUSION clause, or
     822              :      *   - if the new frame doesn't overlap the old one
     823              :      *
     824              :      * Note that we don't strictly need to restart in the last case, but if
     825              :      * we're going to remove all rows from the aggregation anyway, a restart
     826              :      * surely is faster.
     827              :      *----------
     828              :      */
     829         6151 :     numaggs_restart = 0;
     830        13490 :     for (i = 0; i < numaggs; i++)
     831              :     {
     832         7339 :         peraggstate = &winstate->peragg[i];
     833         7339 :         if (winstate->currentpos == 0 ||
     834         5944 :             (winstate->aggregatedbase != winstate->frameheadpos &&
     835         3516 :              !OidIsValid(peraggstate->invtransfn_oid)) ||
     836         5896 :             (winstate->frameOptions & FRAMEOPTION_EXCLUSION) ||
     837         5116 :             winstate->aggregatedupto <= winstate->frameheadpos)
     838              :         {
     839         2607 :             peraggstate->restart = true;
     840         2607 :             numaggs_restart++;
     841              :         }
     842              :         else
     843         4732 :             peraggstate->restart = false;
     844              :     }
     845              : 
     846              :     /*
     847              :      * If we have any possibly-moving aggregates, attempt to advance
     848              :      * aggregatedbase to match the frame's head by removing input rows that
     849              :      * fell off the top of the frame from the aggregations.  This can fail,
     850              :      * i.e. advance_windowaggregate_base() can return false, in which case
     851              :      * we'll restart that aggregate below.
     852              :      */
     853         8295 :     while (numaggs_restart < numaggs &&
     854         5824 :            winstate->aggregatedbase < winstate->frameheadpos)
     855              :     {
     856              :         /*
     857              :          * Fetch the next tuple of those being removed. This should never fail
     858              :          * as we should have been here before.
     859              :          */
     860         2144 :         if (!window_gettupleslot(agg_winobj, winstate->aggregatedbase,
     861              :                                  temp_slot))
     862            0 :             elog(ERROR, "could not re-fetch previously fetched frame row");
     863              : 
     864              :         /* Set tuple context for evaluation of aggregate arguments */
     865         2144 :         winstate->tmpcontext->ecxt_outertuple = temp_slot;
     866              : 
     867              :         /*
     868              :          * Perform the inverse transition for each aggregate function in the
     869              :          * window, unless it has already been marked as needing a restart.
     870              :          */
     871         5268 :         for (i = 0; i < numaggs; i++)
     872              :         {
     873              :             bool        ok;
     874              : 
     875         3124 :             peraggstate = &winstate->peragg[i];
     876         3124 :             if (peraggstate->restart)
     877            8 :                 continue;
     878              : 
     879         3116 :             wfuncno = peraggstate->wfuncno;
     880         3116 :             ok = advance_windowaggregate_base(winstate,
     881         3116 :                                               &winstate->perfunc[wfuncno],
     882              :                                               peraggstate);
     883         3116 :             if (!ok)
     884              :             {
     885              :                 /* Inverse transition function has failed, must restart */
     886          171 :                 peraggstate->restart = true;
     887          171 :                 numaggs_restart++;
     888              :             }
     889              :         }
     890              : 
     891              :         /* Reset per-input-tuple context after each tuple */
     892         2144 :         ResetExprContext(winstate->tmpcontext);
     893              : 
     894              :         /* And advance the aggregated-row state */
     895         2144 :         winstate->aggregatedbase++;
     896         2144 :         ExecClearTuple(temp_slot);
     897              :     }
     898              : 
     899              :     /*
     900              :      * If we successfully advanced the base rows of all the aggregates,
     901              :      * aggregatedbase now equals frameheadpos; but if we failed for any, we
     902              :      * must forcibly update aggregatedbase.
     903              :      */
     904         6151 :     winstate->aggregatedbase = winstate->frameheadpos;
     905              : 
     906              :     /*
     907              :      * If we created a mark pointer for aggregates, keep it pushed up to frame
     908              :      * head, so that tuplestore can discard unnecessary rows.
     909              :      */
     910         6151 :     if (agg_winobj->markptr >= 0)
     911         4306 :         WinSetMarkPosition(agg_winobj, winstate->frameheadpos);
     912              : 
     913              :     /*
     914              :      * Now restart the aggregates that require it.
     915              :      *
     916              :      * We assume that aggregates using the shared context always restart if
     917              :      * *any* aggregate restarts, and we may thus clean up the shared
     918              :      * aggcontext if that is the case.  Private aggcontexts are reset by
     919              :      * initialize_windowaggregate() if their owning aggregate restarts. If we
     920              :      * aren't restarting an aggregate, we need to free any previously saved
     921              :      * result for it, else we'll leak memory.
     922              :      */
     923         6151 :     if (numaggs_restart > 0)
     924         2633 :         MemoryContextReset(winstate->aggcontext);
     925        13490 :     for (i = 0; i < numaggs; i++)
     926              :     {
     927         7339 :         peraggstate = &winstate->peragg[i];
     928              : 
     929              :         /* Aggregates using the shared ctx must restart if *any* agg does */
     930              :         Assert(peraggstate->aggcontext != winstate->aggcontext ||
     931              :                numaggs_restart == 0 ||
     932              :                peraggstate->restart);
     933              : 
     934         7339 :         if (peraggstate->restart)
     935              :         {
     936         2778 :             wfuncno = peraggstate->wfuncno;
     937         2778 :             initialize_windowaggregate(winstate,
     938         2778 :                                        &winstate->perfunc[wfuncno],
     939              :                                        peraggstate);
     940              :         }
     941         4561 :         else if (!peraggstate->resultValueIsNull)
     942              :         {
     943         4405 :             if (!peraggstate->resulttypeByVal)
     944         1504 :                 pfree(DatumGetPointer(peraggstate->resultValue));
     945         4405 :             peraggstate->resultValue = (Datum) 0;
     946         4405 :             peraggstate->resultValueIsNull = true;
     947              :         }
     948              :     }
     949              : 
     950              :     /*
     951              :      * Non-restarted aggregates now contain the rows between aggregatedbase
     952              :      * (i.e., frameheadpos) and aggregatedupto, while restarted aggregates
     953              :      * contain no rows.  If there are any restarted aggregates, we must thus
     954              :      * begin aggregating anew at frameheadpos, otherwise we may simply
     955              :      * continue at aggregatedupto.  We must remember the old value of
     956              :      * aggregatedupto to know how long to skip advancing non-restarted
     957              :      * aggregates.  If we modify aggregatedupto, we must also clear
     958              :      * agg_row_slot, per the loop invariant below.
     959              :      */
     960         6151 :     aggregatedupto_nonrestarted = winstate->aggregatedupto;
     961         6151 :     if (numaggs_restart > 0 &&
     962         2633 :         winstate->aggregatedupto != winstate->frameheadpos)
     963              :     {
     964          994 :         winstate->aggregatedupto = winstate->frameheadpos;
     965          994 :         ExecClearTuple(agg_row_slot);
     966              :     }
     967              : 
     968              :     /*
     969              :      * Advance until we reach a row not in frame (or end of partition).
     970              :      *
     971              :      * Note the loop invariant: agg_row_slot is either empty or holds the row
     972              :      * at position aggregatedupto.  We advance aggregatedupto after processing
     973              :      * a row.
     974              :      */
     975              :     for (;;)
     976       118678 :     {
     977              :         int         ret;
     978              : 
     979              :         /* Fetch next row if we didn't already */
     980       124829 :         if (TupIsNull(agg_row_slot))
     981              :         {
     982       122185 :             if (!window_gettupleslot(agg_winobj, winstate->aggregatedupto,
     983              :                                      agg_row_slot))
     984         2931 :                 break;          /* must be end of partition */
     985              :         }
     986              : 
     987              :         /*
     988              :          * Exit loop if no more rows can be in frame.  Skip aggregation if
     989              :          * current row is not in frame but there might be more in the frame.
     990              :          */
     991       121898 :         ret = row_is_in_frame(agg_winobj, winstate->aggregatedupto,
     992              :                               agg_row_slot, false);
     993       121890 :         if (ret < 0)
     994         3204 :             break;
     995       118686 :         if (ret == 0)
     996         1304 :             goto next_tuple;
     997              : 
     998              :         /* Set tuple context for evaluation of aggregate arguments */
     999       117382 :         winstate->tmpcontext->ecxt_outertuple = agg_row_slot;
    1000              : 
    1001              :         /* Accumulate row into the aggregates */
    1002       251186 :         for (i = 0; i < numaggs; i++)
    1003              :         {
    1004       133812 :             peraggstate = &winstate->peragg[i];
    1005              : 
    1006              :             /* Non-restarted aggs skip until aggregatedupto_nonrestarted */
    1007       133812 :             if (!peraggstate->restart &&
    1008        80939 :                 winstate->aggregatedupto < aggregatedupto_nonrestarted)
    1009        14435 :                 continue;
    1010              : 
    1011       119377 :             wfuncno = peraggstate->wfuncno;
    1012       119377 :             advance_windowaggregate(winstate,
    1013       119377 :                                     &winstate->perfunc[wfuncno],
    1014              :                                     peraggstate);
    1015              :         }
    1016              : 
    1017       117374 : next_tuple:
    1018              :         /* Reset per-input-tuple context after each tuple */
    1019       118678 :         ResetExprContext(winstate->tmpcontext);
    1020              : 
    1021              :         /* And advance the aggregated-row state */
    1022       118678 :         winstate->aggregatedupto++;
    1023       118678 :         ExecClearTuple(agg_row_slot);
    1024              :     }
    1025              : 
    1026              :     /* The frame's end is not supposed to move backwards, ever */
    1027              :     Assert(aggregatedupto_nonrestarted <= winstate->aggregatedupto);
    1028              : 
    1029              :     /*
    1030              :      * finalize aggregates and fill result/isnull fields.
    1031              :      */
    1032        13450 :     for (i = 0; i < numaggs; i++)
    1033              :     {
    1034              :         Datum      *result;
    1035              :         bool       *isnull;
    1036              : 
    1037         7323 :         peraggstate = &winstate->peragg[i];
    1038         7323 :         wfuncno = peraggstate->wfuncno;
    1039         7323 :         result = &econtext->ecxt_aggvalues[wfuncno];
    1040         7323 :         isnull = &econtext->ecxt_aggnulls[wfuncno];
    1041         7323 :         finalize_windowaggregate(winstate,
    1042         7323 :                                  &winstate->perfunc[wfuncno],
    1043              :                                  peraggstate,
    1044              :                                  result, isnull);
    1045              : 
    1046              :         /*
    1047              :          * save the result in case next row shares the same frame.
    1048              :          *
    1049              :          * XXX in some framing modes, eg ROWS/END_CURRENT_ROW, we can know in
    1050              :          * advance that the next row can't possibly share the same frame. Is
    1051              :          * it worth detecting that and skipping this code?
    1052              :          */
    1053         7315 :         if (!peraggstate->resulttypeByVal && !*isnull)
    1054              :         {
    1055         1908 :             oldContext = MemoryContextSwitchTo(peraggstate->aggcontext);
    1056         1908 :             peraggstate->resultValue =
    1057         1908 :                 datumCopy(*result,
    1058         1908 :                           peraggstate->resulttypeByVal,
    1059         1908 :                           peraggstate->resulttypeLen);
    1060         1908 :             MemoryContextSwitchTo(oldContext);
    1061              :         }
    1062              :         else
    1063              :         {
    1064         5407 :             peraggstate->resultValue = *result;
    1065              :         }
    1066         7315 :         peraggstate->resultValueIsNull = *isnull;
    1067              :     }
    1068              : }
    1069              : 
    1070              : /*
    1071              :  * eval_windowfunction
    1072              :  *
    1073              :  * Arguments of window functions are not evaluated here, because a window
    1074              :  * function can need random access to arbitrary rows in the partition.
    1075              :  * The window function uses the special WinGetFuncArgInPartition and
    1076              :  * WinGetFuncArgInFrame functions to evaluate the arguments for the rows
    1077              :  * it wants.
    1078              :  */
    1079              : static void
    1080       580655 : eval_windowfunction(WindowAggState *winstate, WindowStatePerFunc perfuncstate,
    1081              :                     Datum *result, bool *isnull)
    1082              : {
    1083       580655 :     LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
    1084              :     MemoryContext oldContext;
    1085              : 
    1086       580655 :     oldContext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
    1087              : 
    1088              :     /*
    1089              :      * We don't pass any normal arguments to a window function, but we do pass
    1090              :      * it the number of arguments, in order to permit window function
    1091              :      * implementations to support varying numbers of arguments.  The real info
    1092              :      * goes through the WindowObject, which is passed via fcinfo->context.
    1093              :      */
    1094       580655 :     InitFunctionCallInfoData(*fcinfo, &(perfuncstate->flinfo),
    1095              :                              perfuncstate->numArguments,
    1096              :                              perfuncstate->winCollation,
    1097              :                              (Node *) perfuncstate->winobj, NULL);
    1098              :     /* Just in case, make all the regular argument slots be null */
    1099       746899 :     for (int argno = 0; argno < perfuncstate->numArguments; argno++)
    1100       166244 :         fcinfo->args[argno].isnull = true;
    1101              :     /* Window functions don't have a current aggregate context, either */
    1102       580655 :     winstate->curaggcontext = NULL;
    1103              : 
    1104       580655 :     *result = FunctionCallInvoke(fcinfo);
    1105       580547 :     *isnull = fcinfo->isnull;
    1106              : 
    1107              :     /*
    1108              :      * The window function might have returned a pass-by-ref result that's
    1109              :      * just a pointer into one of the WindowObject's temporary slots.  That's
    1110              :      * not a problem if it's the only window function using the WindowObject;
    1111              :      * but if there's more than one function, we'd better copy the result to
    1112              :      * ensure it's not clobbered by later window functions.
    1113              :      */
    1114       580547 :     if (!perfuncstate->resulttypeByVal && !fcinfo->isnull &&
    1115          680 :         winstate->numfuncs > 1)
    1116           72 :         *result = datumCopy(*result,
    1117           72 :                             perfuncstate->resulttypeByVal,
    1118           72 :                             perfuncstate->resulttypeLen);
    1119              : 
    1120       580547 :     MemoryContextSwitchTo(oldContext);
    1121       580547 : }
    1122              : 
    1123              : /*
    1124              :  * prepare_tuplestore
    1125              :  *      Prepare the tuplestore and all of the required read pointers for the
    1126              :  *      WindowAggState's frameOptions.
    1127              :  *
    1128              :  * Note: We use pg_noinline to avoid bloating the calling function with code
    1129              :  * which is only called once.
    1130              :  */
    1131              : static pg_noinline void
    1132         1553 : prepare_tuplestore(WindowAggState *winstate)
    1133              : {
    1134         1553 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
    1135         1553 :     int         frameOptions = winstate->frameOptions;
    1136         1553 :     int         numfuncs = winstate->numfuncs;
    1137              : 
    1138              :     /* we shouldn't be called if this was done already */
    1139              :     Assert(winstate->buffer == NULL);
    1140              : 
    1141              :     /* Create new tuplestore */
    1142         1553 :     winstate->buffer = tuplestore_begin_heap(false, false, work_mem);
    1143              : 
    1144              :     /*
    1145              :      * Set up read pointers for the tuplestore.  The current pointer doesn't
    1146              :      * need BACKWARD capability, but the per-window-function read pointers do,
    1147              :      * and the aggregate pointer does if we might need to restart aggregation.
    1148              :      */
    1149         1553 :     winstate->current_ptr = 0;   /* read pointer 0 is pre-allocated */
    1150              : 
    1151              :     /* reset default REWIND capability bit for current ptr */
    1152         1553 :     tuplestore_set_eflags(winstate->buffer, 0);
    1153              : 
    1154              :     /* create read pointers for aggregates, if needed */
    1155         1553 :     if (winstate->numaggs > 0)
    1156              :     {
    1157          775 :         WindowObject agg_winobj = winstate->agg_winobj;
    1158          775 :         int         readptr_flags = 0;
    1159              : 
    1160              :         /*
    1161              :          * If the frame head is potentially movable, or we have an EXCLUSION
    1162              :          * clause, we might need to restart aggregation ...
    1163              :          */
    1164          775 :         if (!(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING) ||
    1165          270 :             (frameOptions & FRAMEOPTION_EXCLUSION))
    1166              :         {
    1167              :             /* ... so create a mark pointer to track the frame head */
    1168          517 :             agg_winobj->markptr = tuplestore_alloc_read_pointer(winstate->buffer, 0);
    1169              :             /* and the read pointer will need BACKWARD capability */
    1170          517 :             readptr_flags |= EXEC_FLAG_BACKWARD;
    1171              :         }
    1172              : 
    1173          775 :         agg_winobj->readptr = tuplestore_alloc_read_pointer(winstate->buffer,
    1174              :                                                             readptr_flags);
    1175              :     }
    1176              : 
    1177              :     /* create mark and read pointers for each real window function */
    1178         3614 :     for (int i = 0; i < numfuncs; i++)
    1179              :     {
    1180         2061 :         WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
    1181              : 
    1182         2061 :         if (!perfuncstate->plain_agg)
    1183              :         {
    1184         1222 :             WindowObject winobj = perfuncstate->winobj;
    1185              : 
    1186         1222 :             winobj->markptr = tuplestore_alloc_read_pointer(winstate->buffer,
    1187              :                                                             0);
    1188         1222 :             winobj->readptr = tuplestore_alloc_read_pointer(winstate->buffer,
    1189              :                                                             EXEC_FLAG_BACKWARD);
    1190              :         }
    1191              :     }
    1192              : 
    1193              :     /*
    1194              :      * If we are in RANGE or GROUPS mode, then determining frame boundaries
    1195              :      * requires physical access to the frame endpoint rows, except in certain
    1196              :      * degenerate cases.  We create read pointers to point to those rows, to
    1197              :      * simplify access and ensure that the tuplestore doesn't discard the
    1198              :      * endpoint rows prematurely.  (Must create pointers in exactly the same
    1199              :      * cases that update_frameheadpos and update_frametailpos need them.)
    1200              :      */
    1201         1553 :     winstate->framehead_ptr = winstate->frametail_ptr = -1; /* if not used */
    1202              : 
    1203         1553 :     if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
    1204              :     {
    1205          859 :         if (((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
    1206           49 :              node->ordNumCols != 0) ||
    1207          810 :             (frameOptions & FRAMEOPTION_START_OFFSET))
    1208          489 :             winstate->framehead_ptr =
    1209          489 :                 tuplestore_alloc_read_pointer(winstate->buffer, 0);
    1210          859 :         if (((frameOptions & FRAMEOPTION_END_CURRENT_ROW) &&
    1211          338 :              node->ordNumCols != 0) ||
    1212          634 :             (frameOptions & FRAMEOPTION_END_OFFSET))
    1213          705 :             winstate->frametail_ptr =
    1214          705 :                 tuplestore_alloc_read_pointer(winstate->buffer, 0);
    1215              :     }
    1216              : 
    1217              :     /*
    1218              :      * If we have an exclusion clause that requires knowing the boundaries of
    1219              :      * the current row's peer group, we create a read pointer to track the
    1220              :      * tail position of the peer group (i.e., first row of the next peer
    1221              :      * group).  The head position does not require its own pointer because we
    1222              :      * maintain that as a side effect of advancing the current row.
    1223              :      */
    1224         1553 :     winstate->grouptail_ptr = -1;
    1225              : 
    1226         1553 :     if ((frameOptions & (FRAMEOPTION_EXCLUDE_GROUP |
    1227          120 :                          FRAMEOPTION_EXCLUDE_TIES)) &&
    1228          120 :         node->ordNumCols != 0)
    1229              :     {
    1230          112 :         winstate->grouptail_ptr =
    1231          112 :             tuplestore_alloc_read_pointer(winstate->buffer, 0);
    1232              :     }
    1233         1553 : }
    1234              : 
    1235              : /*
    1236              :  * begin_partition
    1237              :  * Start buffering rows of the next partition.
    1238              :  */
    1239              : static void
    1240         2431 : begin_partition(WindowAggState *winstate)
    1241              : {
    1242         2431 :     PlanState  *outerPlan = outerPlanState(winstate);
    1243         2431 :     int         numfuncs = winstate->numfuncs;
    1244              : 
    1245         2431 :     winstate->partition_spooled = false;
    1246         2431 :     winstate->framehead_valid = false;
    1247         2431 :     winstate->frametail_valid = false;
    1248         2431 :     winstate->grouptail_valid = false;
    1249         2431 :     winstate->spooled_rows = 0;
    1250         2431 :     winstate->currentpos = 0;
    1251         2431 :     winstate->frameheadpos = 0;
    1252         2431 :     winstate->frametailpos = 0;
    1253         2431 :     winstate->currentgroup = 0;
    1254         2431 :     winstate->frameheadgroup = 0;
    1255         2431 :     winstate->frametailgroup = 0;
    1256         2431 :     winstate->groupheadpos = 0;
    1257         2431 :     winstate->grouptailpos = -1; /* see update_grouptailpos */
    1258         2431 :     ExecClearTuple(winstate->agg_row_slot);
    1259         2431 :     if (winstate->framehead_slot)
    1260          690 :         ExecClearTuple(winstate->framehead_slot);
    1261         2431 :     if (winstate->frametail_slot)
    1262         1154 :         ExecClearTuple(winstate->frametail_slot);
    1263              : 
    1264              :     /*
    1265              :      * If this is the very first partition, we need to fetch the first input
    1266              :      * row to store in first_part_slot.
    1267              :      */
    1268         2431 :     if (TupIsNull(winstate->first_part_slot))
    1269              :     {
    1270         1605 :         TupleTableSlot *outerslot = ExecProcNode(outerPlan);
    1271              : 
    1272         1605 :         if (!TupIsNull(outerslot))
    1273         1593 :             ExecCopySlot(winstate->first_part_slot, outerslot);
    1274              :         else
    1275              :         {
    1276              :             /* outer plan is empty, so we have nothing to do */
    1277           12 :             winstate->partition_spooled = true;
    1278           12 :             winstate->more_partitions = false;
    1279           12 :             return;
    1280              :         }
    1281              :     }
    1282              : 
    1283              :     /* Create new tuplestore if not done already. */
    1284         2419 :     if (unlikely(winstate->buffer == NULL))
    1285         1553 :         prepare_tuplestore(winstate);
    1286              : 
    1287         2419 :     winstate->next_partition = false;
    1288              : 
    1289         2419 :     if (winstate->numaggs > 0)
    1290              :     {
    1291         1267 :         WindowObject agg_winobj = winstate->agg_winobj;
    1292              : 
    1293              :         /* reset mark and see positions for aggregate functions */
    1294         1267 :         agg_winobj->markpos = -1;
    1295         1267 :         agg_winobj->seekpos = -1;
    1296              : 
    1297              :         /* Also reset the row counters for aggregates */
    1298         1267 :         winstate->aggregatedbase = 0;
    1299         1267 :         winstate->aggregatedupto = 0;
    1300              :     }
    1301              : 
    1302              :     /* reset mark and seek positions for each real window function */
    1303         5550 :     for (int i = 0; i < numfuncs; i++)
    1304              :     {
    1305         3131 :         WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
    1306              : 
    1307         3131 :         if (!perfuncstate->plain_agg)
    1308              :         {
    1309         1732 :             WindowObject winobj = perfuncstate->winobj;
    1310              : 
    1311         1732 :             winobj->markpos = -1;
    1312         1732 :             winobj->seekpos = -1;
    1313              : 
    1314              :             /* reset null map */
    1315         1732 :             if (winobj->ignore_nulls == IGNORE_NULLS ||
    1316         1712 :                 winobj->ignore_nulls == PARSER_IGNORE_NULLS)
    1317              :             {
    1318          144 :                 int         numargs = perfuncstate->numArguments;
    1319              : 
    1320          332 :                 for (int j = 0; j < numargs; j++)
    1321              :                 {
    1322          188 :                     int         n = winobj->num_notnull_info[j];
    1323              : 
    1324          188 :                     if (n > 0)
    1325           20 :                         memset(winobj->notnull_info[j], 0,
    1326           20 :                                NN_POS_TO_BYTES(n));
    1327              :                 }
    1328              :             }
    1329              :         }
    1330              :     }
    1331              : 
    1332              :     /*
    1333              :      * Store the first tuple into the tuplestore (it's always available now;
    1334              :      * we either read it above, or saved it at the end of previous partition)
    1335              :      */
    1336         2419 :     tuplestore_puttupleslot(winstate->buffer, winstate->first_part_slot);
    1337         2419 :     winstate->spooled_rows++;
    1338              : }
    1339              : 
    1340              : /*
    1341              :  * Read tuples from the outer node, up to and including position 'pos', and
    1342              :  * store them into the tuplestore. If pos is -1, reads the whole partition.
    1343              :  */
    1344              : static void
    1345      1241922 : spool_tuples(WindowAggState *winstate, int64 pos)
    1346              : {
    1347      1241922 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
    1348              :     PlanState  *outerPlan;
    1349              :     TupleTableSlot *outerslot;
    1350              :     MemoryContext oldcontext;
    1351              : 
    1352      1241922 :     if (!winstate->buffer)
    1353            4 :         return;                 /* just a safety check */
    1354      1241918 :     if (winstate->partition_spooled)
    1355        84152 :         return;                 /* whole partition done already */
    1356              : 
    1357              :     /*
    1358              :      * When in pass-through mode we can just exhaust all tuples in the current
    1359              :      * partition.  We don't need these tuples for any further window function
    1360              :      * evaluation, however, we do need to keep them around if we're not the
    1361              :      * top-level window as another WindowAgg node above must see these.
    1362              :      */
    1363      1157766 :     if (winstate->status != WINDOWAGG_RUN)
    1364              :     {
    1365              :         Assert(winstate->status == WINDOWAGG_PASSTHROUGH ||
    1366              :                winstate->status == WINDOWAGG_PASSTHROUGH_STRICT);
    1367              : 
    1368           20 :         pos = -1;
    1369              :     }
    1370              : 
    1371              :     /*
    1372              :      * If the tuplestore has spilled to disk, alternate reading and writing
    1373              :      * becomes quite expensive due to frequent buffer flushes.  It's cheaper
    1374              :      * to force the entire partition to get spooled in one go.
    1375              :      *
    1376              :      * XXX this is a horrid kluge --- it'd be better to fix the performance
    1377              :      * problem inside tuplestore.  FIXME
    1378              :      */
    1379      1157746 :     else if (!tuplestore_in_memory(winstate->buffer))
    1380            8 :         pos = -1;
    1381              : 
    1382      1157766 :     outerPlan = outerPlanState(winstate);
    1383              : 
    1384              :     /* Must be in query context to call outerplan */
    1385      1157766 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
    1386              : 
    1387      2997037 :     while (winstate->spooled_rows <= pos || pos == -1)
    1388              :     {
    1389       683792 :         outerslot = ExecProcNode(outerPlan);
    1390       683792 :         if (TupIsNull(outerslot))
    1391              :         {
    1392              :             /* reached the end of the last partition */
    1393         1461 :             winstate->partition_spooled = true;
    1394         1461 :             winstate->more_partitions = false;
    1395         1461 :             break;
    1396              :         }
    1397              : 
    1398       682331 :         if (node->partNumCols > 0)
    1399              :         {
    1400        92455 :             ExprContext *econtext = winstate->tmpcontext;
    1401              : 
    1402        92455 :             econtext->ecxt_innertuple = winstate->first_part_slot;
    1403        92455 :             econtext->ecxt_outertuple = outerslot;
    1404              : 
    1405              :             /* Check if this tuple still belongs to the current partition */
    1406        92455 :             if (!ExecQualAndReset(winstate->partEqfunction, econtext))
    1407              :             {
    1408              :                 /*
    1409              :                  * end of partition; copy the tuple for the next cycle.
    1410              :                  */
    1411          826 :                 ExecCopySlot(winstate->first_part_slot, outerslot);
    1412          826 :                 winstate->partition_spooled = true;
    1413          826 :                 winstate->more_partitions = true;
    1414          826 :                 break;
    1415              :             }
    1416              :         }
    1417              : 
    1418              :         /*
    1419              :          * Remember the tuple unless we're the top-level window and we're in
    1420              :          * pass-through mode.
    1421              :          */
    1422       681505 :         if (winstate->status != WINDOWAGG_PASSTHROUGH_STRICT)
    1423              :         {
    1424              :             /* Still in partition, so save it into the tuplestore */
    1425       681497 :             tuplestore_puttupleslot(winstate->buffer, outerslot);
    1426       681497 :             winstate->spooled_rows++;
    1427              :         }
    1428              :     }
    1429              : 
    1430      1157766 :     MemoryContextSwitchTo(oldcontext);
    1431              : }
    1432              : 
    1433              : /*
    1434              :  * release_partition
    1435              :  * clear information kept within a partition, including
    1436              :  * tuplestore and aggregate results.
    1437              :  */
    1438              : static void
    1439         4089 : release_partition(WindowAggState *winstate)
    1440              : {
    1441              :     int         i;
    1442              : 
    1443         9342 :     for (i = 0; i < winstate->numfuncs; i++)
    1444              :     {
    1445         5253 :         WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
    1446              : 
    1447              :         /* Release any partition-local state of this window function */
    1448         5253 :         if (perfuncstate->winobj)
    1449         2755 :             perfuncstate->winobj->localmem = NULL;
    1450              :     }
    1451              : 
    1452              :     /*
    1453              :      * Release all partition-local memory (in particular, any partition-local
    1454              :      * state that we might have trashed our pointers to in the above loop, and
    1455              :      * any aggregate temp data).  We don't rely on retail pfree because some
    1456              :      * aggregates might have allocated data we don't have direct pointers to.
    1457              :      */
    1458         4089 :     MemoryContextReset(winstate->partcontext);
    1459         4089 :     MemoryContextReset(winstate->aggcontext);
    1460         6587 :     for (i = 0; i < winstate->numaggs; i++)
    1461              :     {
    1462         2498 :         if (winstate->peragg[i].aggcontext != winstate->aggcontext)
    1463         1320 :             MemoryContextReset(winstate->peragg[i].aggcontext);
    1464              :     }
    1465              : 
    1466         4089 :     if (winstate->buffer)
    1467         2307 :         tuplestore_clear(winstate->buffer);
    1468         4089 :     winstate->partition_spooled = false;
    1469         4089 :     winstate->next_partition = true;
    1470         4089 : }
    1471              : 
    1472              : /*
    1473              :  * row_is_in_frame
    1474              :  * Determine whether a row is in the current row's window frame according
    1475              :  * to our window framing rule
    1476              :  *
    1477              :  * The caller must have already determined that the row is in the partition
    1478              :  * and fetched it into a slot if fetch_tuple is false.
    1479              :  * This function just encapsulates the framing rules.
    1480              :  *
    1481              :  * Returns:
    1482              :  * -1, if the row is out of frame and no succeeding rows can be in frame
    1483              :  * 0, if the row is out of frame but succeeding rows might be in frame
    1484              :  * 1, if the row is in frame
    1485              :  *
    1486              :  * May clobber winstate->temp_slot_2.
    1487              :  */
    1488              : static int
    1489       129110 : row_is_in_frame(WindowObject winobj, int64 pos, TupleTableSlot *slot,
    1490              :                 bool fetch_tuple)
    1491              : {
    1492       129110 :     WindowAggState *winstate = winobj->winstate;
    1493       129110 :     int         frameOptions = winstate->frameOptions;
    1494              : 
    1495              :     Assert(pos >= 0);            /* else caller error */
    1496              : 
    1497              :     /*
    1498              :      * First, check frame starting conditions.  We might as well delegate this
    1499              :      * to update_frameheadpos always; it doesn't add any notable cost.
    1500              :      */
    1501       129110 :     update_frameheadpos(winstate);
    1502       129110 :     if (pos < winstate->frameheadpos)
    1503           96 :         return 0;
    1504              : 
    1505              :     /*
    1506              :      * Okay so far, now check frame ending conditions.  Here, we avoid calling
    1507              :      * update_frametailpos in simple cases, so as not to spool tuples further
    1508              :      * ahead than necessary.
    1509              :      */
    1510       129014 :     if (frameOptions & FRAMEOPTION_END_CURRENT_ROW)
    1511              :     {
    1512       105563 :         if (frameOptions & FRAMEOPTION_ROWS)
    1513              :         {
    1514              :             /* rows after current row are out of frame */
    1515         1472 :             if (pos > winstate->currentpos)
    1516          648 :                 return -1;
    1517              :         }
    1518       104091 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
    1519              :         {
    1520              :             /* following row that is not peer is out of frame */
    1521       104091 :             if (pos > winstate->currentpos)
    1522              :             {
    1523       101766 :                 if (fetch_tuple)    /* need to fetch tuple? */
    1524            0 :                     if (!window_gettupleslot(winobj, pos, slot))
    1525            0 :                         return -1;
    1526       101766 :                 if (!are_peers(winstate, slot, winstate->ss.ss_ScanTupleSlot))
    1527          912 :                     return -1;
    1528              :             }
    1529              :         }
    1530              :         else
    1531              :             Assert(false);
    1532              :     }
    1533        23451 :     else if (frameOptions & FRAMEOPTION_END_OFFSET)
    1534              :     {
    1535        13572 :         if (frameOptions & FRAMEOPTION_ROWS)
    1536              :         {
    1537         4208 :             int64       offset = DatumGetInt64(winstate->endOffsetValue);
    1538         4208 :             int64       frameendpos = 0;
    1539              : 
    1540              :             /* rows after current row + offset are out of frame */
    1541         4208 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
    1542           76 :                 offset = -offset;
    1543              : 
    1544              :             /*
    1545              :              * If we have an overflow, it means the frame end is beyond the
    1546              :              * range of int64.  Since currentpos >= 0, this can only be a
    1547              :              * positive overflow.  We treat this as meaning that the frame
    1548              :              * extends to end of partition.
    1549              :              */
    1550         4208 :             if (!pg_add_s64_overflow(winstate->currentpos, offset,
    1551         3996 :                                      &frameendpos) &&
    1552         3996 :                 pos > frameendpos)
    1553          804 :                 return -1;
    1554              :         }
    1555         9364 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
    1556              :         {
    1557              :             /* hard cases, so delegate to update_frametailpos */
    1558         9364 :             update_frametailpos(winstate);
    1559         9336 :             if (pos >= winstate->frametailpos)
    1560          980 :                 return -1;
    1561              :         }
    1562              :         else
    1563              :             Assert(false);
    1564              :     }
    1565              : 
    1566              :     /* Check exclusion clause */
    1567       125642 :     if (frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW)
    1568              :     {
    1569         2220 :         if (pos == winstate->currentpos)
    1570          372 :             return 0;
    1571              :     }
    1572       123422 :     else if ((frameOptions & FRAMEOPTION_EXCLUDE_GROUP) ||
    1573       121514 :              ((frameOptions & FRAMEOPTION_EXCLUDE_TIES) &&
    1574         1980 :               pos != winstate->currentpos))
    1575              :     {
    1576         3528 :         WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
    1577              : 
    1578              :         /* If no ORDER BY, all rows are peers with each other */
    1579         3528 :         if (node->ordNumCols == 0)
    1580          312 :             return 0;
    1581              :         /* Otherwise, check the group boundaries */
    1582         3216 :         if (pos >= winstate->groupheadpos)
    1583              :         {
    1584         1728 :             update_grouptailpos(winstate);
    1585         1728 :             if (pos < winstate->grouptailpos)
    1586          672 :                 return 0;
    1587              :         }
    1588              :     }
    1589              : 
    1590              :     /* If we get here, it's in frame */
    1591       124286 :     return 1;
    1592              : }
    1593              : 
    1594              : /*
    1595              :  * update_frameheadpos
    1596              :  * make frameheadpos valid for the current row
    1597              :  *
    1598              :  * Note that frameheadpos is computed without regard for any window exclusion
    1599              :  * clause; the current row and/or its peers are considered part of the frame
    1600              :  * for this purpose even if they must be excluded later.
    1601              :  *
    1602              :  * May clobber winstate->temp_slot_2.
    1603              :  */
    1604              : static void
    1605       244173 : update_frameheadpos(WindowAggState *winstate)
    1606              : {
    1607       244173 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
    1608       244173 :     int         frameOptions = winstate->frameOptions;
    1609              :     MemoryContext oldcontext;
    1610              : 
    1611       244173 :     if (winstate->framehead_valid)
    1612       133512 :         return;                 /* already known for current row */
    1613              : 
    1614              :     /* We may be called in a short-lived context */
    1615       110661 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
    1616              : 
    1617       110661 :     if (frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
    1618              :     {
    1619              :         /* In UNBOUNDED PRECEDING mode, frame head is always row 0 */
    1620       103331 :         winstate->frameheadpos = 0;
    1621       103331 :         winstate->framehead_valid = true;
    1622              :     }
    1623         7330 :     else if (frameOptions & FRAMEOPTION_START_CURRENT_ROW)
    1624              :     {
    1625         1986 :         if (frameOptions & FRAMEOPTION_ROWS)
    1626              :         {
    1627              :             /* In ROWS mode, frame head is the same as current */
    1628         1664 :             winstate->frameheadpos = winstate->currentpos;
    1629         1664 :             winstate->framehead_valid = true;
    1630              :         }
    1631          322 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
    1632              :         {
    1633              :             /* If no ORDER BY, all rows are peers with each other */
    1634          322 :             if (node->ordNumCols == 0)
    1635              :             {
    1636            0 :                 winstate->frameheadpos = 0;
    1637            0 :                 winstate->framehead_valid = true;
    1638            0 :                 MemoryContextSwitchTo(oldcontext);
    1639            0 :                 return;
    1640              :             }
    1641              : 
    1642              :             /*
    1643              :              * In RANGE or GROUPS START_CURRENT_ROW mode, frame head is the
    1644              :              * first row that is a peer of current row.  We keep a copy of the
    1645              :              * last-known frame head row in framehead_slot, and advance as
    1646              :              * necessary.  Note that if we reach end of partition, we will
    1647              :              * leave frameheadpos = end+1 and framehead_slot empty.
    1648              :              */
    1649          322 :             tuplestore_select_read_pointer(winstate->buffer,
    1650              :                                            winstate->framehead_ptr);
    1651          322 :             if (winstate->frameheadpos == 0 &&
    1652          156 :                 TupIsNull(winstate->framehead_slot))
    1653              :             {
    1654              :                 /* fetch first row into framehead_slot, if we didn't already */
    1655           58 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    1656              :                                              winstate->framehead_slot))
    1657            0 :                     elog(ERROR, "unexpected end of tuplestore");
    1658              :             }
    1659              : 
    1660          562 :             while (!TupIsNull(winstate->framehead_slot))
    1661              :             {
    1662          562 :                 if (are_peers(winstate, winstate->framehead_slot,
    1663              :                               winstate->ss.ss_ScanTupleSlot))
    1664          322 :                     break;      /* this row is the correct frame head */
    1665              :                 /* Note we advance frameheadpos even if the fetch fails */
    1666          240 :                 winstate->frameheadpos++;
    1667          240 :                 spool_tuples(winstate, winstate->frameheadpos);
    1668          240 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    1669              :                                              winstate->framehead_slot))
    1670            0 :                     break;      /* end of partition */
    1671              :             }
    1672          322 :             winstate->framehead_valid = true;
    1673              :         }
    1674              :         else
    1675              :             Assert(false);
    1676              :     }
    1677         5344 :     else if (frameOptions & FRAMEOPTION_START_OFFSET)
    1678              :     {
    1679         5344 :         if (frameOptions & FRAMEOPTION_ROWS)
    1680              :         {
    1681              :             /* In ROWS mode, bound is physically n before/after current */
    1682         1368 :             int64       offset = DatumGetInt64(winstate->startOffsetValue);
    1683              : 
    1684         1368 :             if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
    1685         1288 :                 offset = -offset;
    1686              : 
    1687              :             /*
    1688              :              * If we have an overflow, it means the frame head is beyond the
    1689              :              * range of int64.  Since currentpos >= 0, this can only be a
    1690              :              * positive overflow.  We treat this as being beyond end of
    1691              :              * partition.
    1692              :              */
    1693         1368 :             if (pg_add_s64_overflow(winstate->currentpos, offset,
    1694              :                                     &winstate->frameheadpos))
    1695           36 :                 winstate->frameheadpos = PG_INT64_MAX;
    1696              : 
    1697              :             /* frame head can't go before first row */
    1698         1368 :             if (winstate->frameheadpos < 0)
    1699          224 :                 winstate->frameheadpos = 0;
    1700         1144 :             else if (winstate->frameheadpos > winstate->currentpos + 1)
    1701              :             {
    1702              :                 /* make sure frameheadpos is not past end of partition */
    1703           40 :                 spool_tuples(winstate, winstate->frameheadpos - 1);
    1704           40 :                 if (winstate->frameheadpos > winstate->spooled_rows)
    1705           40 :                     winstate->frameheadpos = winstate->spooled_rows;
    1706              :             }
    1707         1368 :             winstate->framehead_valid = true;
    1708              :         }
    1709         3976 :         else if (frameOptions & FRAMEOPTION_RANGE)
    1710              :         {
    1711              :             /*
    1712              :              * In RANGE START_OFFSET mode, frame head is the first row that
    1713              :              * satisfies the in_range constraint relative to the current row.
    1714              :              * We keep a copy of the last-known frame head row in
    1715              :              * framehead_slot, and advance as necessary.  Note that if we
    1716              :              * reach end of partition, we will leave frameheadpos = end+1 and
    1717              :              * framehead_slot empty.
    1718              :              */
    1719         3016 :             int         sortCol = node->ordColIdx[0];
    1720              :             bool        sub,
    1721              :                         less;
    1722              : 
    1723              :             /* We must have an ordering column */
    1724              :             Assert(node->ordNumCols == 1);
    1725              : 
    1726              :             /* Precompute flags for in_range checks */
    1727         3016 :             if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
    1728         2468 :                 sub = true;     /* subtract startOffset from current row */
    1729              :             else
    1730          548 :                 sub = false;    /* add it */
    1731         3016 :             less = false;       /* normally, we want frame head >= sum */
    1732              :             /* If sort order is descending, flip both flags */
    1733         3016 :             if (!winstate->inRangeAsc)
    1734              :             {
    1735          436 :                 sub = !sub;
    1736          436 :                 less = true;
    1737              :             }
    1738              : 
    1739         3016 :             tuplestore_select_read_pointer(winstate->buffer,
    1740              :                                            winstate->framehead_ptr);
    1741         3016 :             if (winstate->frameheadpos == 0 &&
    1742         1668 :                 TupIsNull(winstate->framehead_slot))
    1743              :             {
    1744              :                 /* fetch first row into framehead_slot, if we didn't already */
    1745          380 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    1746              :                                              winstate->framehead_slot))
    1747            0 :                     elog(ERROR, "unexpected end of tuplestore");
    1748              :             }
    1749              : 
    1750         4844 :             while (!TupIsNull(winstate->framehead_slot))
    1751              :             {
    1752              :                 Datum       headval,
    1753              :                             currval;
    1754              :                 bool        headisnull,
    1755              :                             currisnull;
    1756              : 
    1757         4708 :                 headval = slot_getattr(winstate->framehead_slot, sortCol,
    1758              :                                        &headisnull);
    1759         4708 :                 currval = slot_getattr(winstate->ss.ss_ScanTupleSlot, sortCol,
    1760              :                                        &currisnull);
    1761         4708 :                 if (headisnull || currisnull)
    1762              :                 {
    1763              :                     /* order of the rows depends only on nulls_first */
    1764           72 :                     if (winstate->inRangeNullsFirst)
    1765              :                     {
    1766              :                         /* advance head if head is null and curr is not */
    1767           32 :                         if (!headisnull || currisnull)
    1768              :                             break;
    1769              :                     }
    1770              :                     else
    1771              :                     {
    1772              :                         /* advance head if head is not null and curr is null */
    1773           40 :                         if (headisnull || !currisnull)
    1774              :                             break;
    1775              :                     }
    1776              :                 }
    1777              :                 else
    1778              :                 {
    1779         4636 :                     if (DatumGetBool(FunctionCall5Coll(&winstate->startInRangeFunc,
    1780              :                                                        winstate->inRangeColl,
    1781              :                                                        headval,
    1782              :                                                        currval,
    1783              :                                                        winstate->startOffsetValue,
    1784              :                                                        BoolGetDatum(sub),
    1785              :                                                        BoolGetDatum(less))))
    1786         2780 :                         break;  /* this row is the correct frame head */
    1787              :                 }
    1788              :                 /* Note we advance frameheadpos even if the fetch fails */
    1789         1864 :                 winstate->frameheadpos++;
    1790         1864 :                 spool_tuples(winstate, winstate->frameheadpos);
    1791         1864 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    1792              :                                              winstate->framehead_slot))
    1793           36 :                     break;      /* end of partition */
    1794              :             }
    1795         2984 :             winstate->framehead_valid = true;
    1796              :         }
    1797          960 :         else if (frameOptions & FRAMEOPTION_GROUPS)
    1798              :         {
    1799              :             /*
    1800              :              * In GROUPS START_OFFSET mode, frame head is the first row of the
    1801              :              * first peer group whose number satisfies the offset constraint.
    1802              :              * We keep a copy of the last-known frame head row in
    1803              :              * framehead_slot, and advance as necessary.  Note that if we
    1804              :              * reach end of partition, we will leave frameheadpos = end+1 and
    1805              :              * framehead_slot empty.
    1806              :              */
    1807          960 :             int64       offset = DatumGetInt64(winstate->startOffsetValue);
    1808          960 :             int64       minheadgroup = 0;
    1809              : 
    1810          960 :             if (frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
    1811          752 :                 minheadgroup = winstate->currentgroup - offset;
    1812              :             else
    1813              :             {
    1814              :                 /*
    1815              :                  * If we have an overflow, it means the target group is beyond
    1816              :                  * the range of int64.  We treat this as "infinity", which
    1817              :                  * ensures the loop below advances to end of partition.
    1818              :                  */
    1819          208 :                 if (pg_add_s64_overflow(winstate->currentgroup, offset,
    1820              :                                         &minheadgroup))
    1821           28 :                     minheadgroup = PG_INT64_MAX;
    1822              :             }
    1823              : 
    1824          960 :             tuplestore_select_read_pointer(winstate->buffer,
    1825              :                                            winstate->framehead_ptr);
    1826          960 :             if (winstate->frameheadpos == 0 &&
    1827          504 :                 TupIsNull(winstate->framehead_slot))
    1828              :             {
    1829              :                 /* fetch first row into framehead_slot, if we didn't already */
    1830          252 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    1831              :                                              winstate->framehead_slot))
    1832            0 :                     elog(ERROR, "unexpected end of tuplestore");
    1833              :             }
    1834              : 
    1835         2464 :             while (!TupIsNull(winstate->framehead_slot))
    1836              :             {
    1837         1452 :                 if (winstate->frameheadgroup >= minheadgroup)
    1838          880 :                     break;      /* this row is the correct frame head */
    1839          572 :                 ExecCopySlot(winstate->temp_slot_2, winstate->framehead_slot);
    1840              :                 /* Note we advance frameheadpos even if the fetch fails */
    1841          572 :                 winstate->frameheadpos++;
    1842          572 :                 spool_tuples(winstate, winstate->frameheadpos);
    1843          572 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    1844              :                                              winstate->framehead_slot))
    1845           28 :                     break;      /* end of partition */
    1846          544 :                 if (!are_peers(winstate, winstate->temp_slot_2,
    1847              :                                winstate->framehead_slot))
    1848          360 :                     winstate->frameheadgroup++;
    1849              :             }
    1850          960 :             ExecClearTuple(winstate->temp_slot_2);
    1851          960 :             winstate->framehead_valid = true;
    1852              :         }
    1853              :         else
    1854              :             Assert(false);
    1855              :     }
    1856              :     else
    1857              :         Assert(false);
    1858              : 
    1859       110629 :     MemoryContextSwitchTo(oldcontext);
    1860              : }
    1861              : 
    1862              : /*
    1863              :  * update_frametailpos
    1864              :  * make frametailpos valid for the current row
    1865              :  *
    1866              :  * Note that frametailpos is computed without regard for any window exclusion
    1867              :  * clause; the current row and/or its peers are considered part of the frame
    1868              :  * for this purpose even if they must be excluded later.
    1869              :  *
    1870              :  * May clobber winstate->temp_slot_2.
    1871              :  */
    1872              : static void
    1873       135358 : update_frametailpos(WindowAggState *winstate)
    1874              : {
    1875       135358 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
    1876       135358 :     int         frameOptions = winstate->frameOptions;
    1877              :     MemoryContext oldcontext;
    1878              : 
    1879       135358 :     if (winstate->frametail_valid)
    1880        12000 :         return;                 /* already known for current row */
    1881              : 
    1882              :     /* We may be called in a short-lived context */
    1883       123358 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
    1884              : 
    1885       123358 :     if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
    1886              :     {
    1887              :         /* In UNBOUNDED FOLLOWING mode, all partition rows are in frame */
    1888          160 :         spool_tuples(winstate, -1);
    1889          160 :         winstate->frametailpos = winstate->spooled_rows;
    1890          160 :         winstate->frametail_valid = true;
    1891              :     }
    1892       123198 :     else if (frameOptions & FRAMEOPTION_END_CURRENT_ROW)
    1893              :     {
    1894       118702 :         if (frameOptions & FRAMEOPTION_ROWS)
    1895              :         {
    1896              :             /* In ROWS mode, exactly the rows up to current are in frame */
    1897           80 :             winstate->frametailpos = winstate->currentpos + 1;
    1898           80 :             winstate->frametail_valid = true;
    1899              :         }
    1900       118622 :         else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
    1901              :         {
    1902              :             /* If no ORDER BY, all rows are peers with each other */
    1903       118622 :             if (node->ordNumCols == 0)
    1904              :             {
    1905           40 :                 spool_tuples(winstate, -1);
    1906           40 :                 winstate->frametailpos = winstate->spooled_rows;
    1907           40 :                 winstate->frametail_valid = true;
    1908           40 :                 MemoryContextSwitchTo(oldcontext);
    1909           40 :                 return;
    1910              :             }
    1911              : 
    1912              :             /*
    1913              :              * In RANGE or GROUPS END_CURRENT_ROW mode, frame end is the last
    1914              :              * row that is a peer of current row, frame tail is the row after
    1915              :              * that (if any).  We keep a copy of the last-known frame tail row
    1916              :              * in frametail_slot, and advance as necessary.  Note that if we
    1917              :              * reach end of partition, we will leave frametailpos = end+1 and
    1918              :              * frametail_slot empty.
    1919              :              */
    1920       118582 :             tuplestore_select_read_pointer(winstate->buffer,
    1921              :                                            winstate->frametail_ptr);
    1922       118582 :             if (winstate->frametailpos == 0 &&
    1923          470 :                 TupIsNull(winstate->frametail_slot))
    1924              :             {
    1925              :                 /* fetch first row into frametail_slot, if we didn't already */
    1926          470 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    1927              :                                              winstate->frametail_slot))
    1928            0 :                     elog(ERROR, "unexpected end of tuplestore");
    1929              :             }
    1930              : 
    1931       236702 :             while (!TupIsNull(winstate->frametail_slot))
    1932              :             {
    1933       220622 :                 if (winstate->frametailpos > winstate->currentpos &&
    1934       182280 :                     !are_peers(winstate, winstate->frametail_slot,
    1935              :                                winstate->ss.ss_ScanTupleSlot))
    1936       102040 :                     break;      /* this row is the frame tail */
    1937              :                 /* Note we advance frametailpos even if the fetch fails */
    1938       118582 :                 winstate->frametailpos++;
    1939       118582 :                 spool_tuples(winstate, winstate->frametailpos);
    1940       118582 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    1941              :                                              winstate->frametail_slot))
    1942          462 :                     break;      /* end of partition */
    1943              :             }
    1944       118582 :             winstate->frametail_valid = true;
    1945              :         }
    1946              :         else
    1947              :             Assert(false);
    1948              :     }
    1949         4496 :     else if (frameOptions & FRAMEOPTION_END_OFFSET)
    1950              :     {
    1951         4496 :         if (frameOptions & FRAMEOPTION_ROWS)
    1952              :         {
    1953              :             /* In ROWS mode, bound is physically n before/after current */
    1954          320 :             int64       offset = DatumGetInt64(winstate->endOffsetValue);
    1955              : 
    1956          320 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
    1957            0 :                 offset = -offset;
    1958              : 
    1959              :             /*
    1960              :              * If we have an overflow, it means the frame tail is beyond the
    1961              :              * range of int64.  Since currentpos >= 0, this can only be a
    1962              :              * positive overflow.  We treat this as being beyond end of
    1963              :              * partition.
    1964              :              */
    1965          320 :             if (pg_add_s64_overflow(winstate->currentpos, offset,
    1966          284 :                                     &winstate->frametailpos) ||
    1967          284 :                 pg_add_s64_overflow(winstate->frametailpos, 1,
    1968              :                                     &winstate->frametailpos))
    1969           40 :                 winstate->frametailpos = PG_INT64_MAX;
    1970              : 
    1971              :             /* smallest allowable value of frametailpos is 0 */
    1972          320 :             if (winstate->frametailpos < 0)
    1973            0 :                 winstate->frametailpos = 0;
    1974          320 :             else if (winstate->frametailpos > winstate->currentpos + 1)
    1975              :             {
    1976              :                 /* make sure frametailpos is not past end of partition */
    1977          320 :                 spool_tuples(winstate, winstate->frametailpos - 1);
    1978          320 :                 if (winstate->frametailpos > winstate->spooled_rows)
    1979          104 :                     winstate->frametailpos = winstate->spooled_rows;
    1980              :             }
    1981          320 :             winstate->frametail_valid = true;
    1982              :         }
    1983         4176 :         else if (frameOptions & FRAMEOPTION_RANGE)
    1984              :         {
    1985              :             /*
    1986              :              * In RANGE END_OFFSET mode, frame end is the last row that
    1987              :              * satisfies the in_range constraint relative to the current row,
    1988              :              * frame tail is the row after that (if any).  We keep a copy of
    1989              :              * the last-known frame tail row in frametail_slot, and advance as
    1990              :              * necessary.  Note that if we reach end of partition, we will
    1991              :              * leave frametailpos = end+1 and frametail_slot empty.
    1992              :              */
    1993         3256 :             int         sortCol = node->ordColIdx[0];
    1994              :             bool        sub,
    1995              :                         less;
    1996              : 
    1997              :             /* We must have an ordering column */
    1998              :             Assert(node->ordNumCols == 1);
    1999              : 
    2000              :             /* Precompute flags for in_range checks */
    2001         3256 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
    2002          608 :                 sub = true;     /* subtract endOffset from current row */
    2003              :             else
    2004         2648 :                 sub = false;    /* add it */
    2005         3256 :             less = true;        /* normally, we want frame tail <= sum */
    2006              :             /* If sort order is descending, flip both flags */
    2007         3256 :             if (!winstate->inRangeAsc)
    2008              :             {
    2009          460 :                 sub = !sub;
    2010          460 :                 less = false;
    2011              :             }
    2012              : 
    2013         3256 :             tuplestore_select_read_pointer(winstate->buffer,
    2014              :                                            winstate->frametail_ptr);
    2015         3256 :             if (winstate->frametailpos == 0 &&
    2016          548 :                 TupIsNull(winstate->frametail_slot))
    2017              :             {
    2018              :                 /* fetch first row into frametail_slot, if we didn't already */
    2019          392 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    2020              :                                              winstate->frametail_slot))
    2021            0 :                     elog(ERROR, "unexpected end of tuplestore");
    2022              :             }
    2023              : 
    2024         6004 :             while (!TupIsNull(winstate->frametail_slot))
    2025              :             {
    2026              :                 Datum       tailval,
    2027              :                             currval;
    2028              :                 bool        tailisnull,
    2029              :                             currisnull;
    2030              : 
    2031         4960 :                 tailval = slot_getattr(winstate->frametail_slot, sortCol,
    2032              :                                        &tailisnull);
    2033         4960 :                 currval = slot_getattr(winstate->ss.ss_ScanTupleSlot, sortCol,
    2034              :                                        &currisnull);
    2035         4960 :                 if (tailisnull || currisnull)
    2036              :                 {
    2037              :                     /* order of the rows depends only on nulls_first */
    2038           72 :                     if (winstate->inRangeNullsFirst)
    2039              :                     {
    2040              :                         /* advance tail if tail is null or curr is not */
    2041           32 :                         if (!tailisnull)
    2042         2180 :                             break;
    2043              :                     }
    2044              :                     else
    2045              :                     {
    2046              :                         /* advance tail if tail is not null or curr is null */
    2047           40 :                         if (!currisnull)
    2048           24 :                             break;
    2049              :                     }
    2050              :                 }
    2051              :                 else
    2052              :                 {
    2053         4888 :                     if (!DatumGetBool(FunctionCall5Coll(&winstate->endInRangeFunc,
    2054              :                                                         winstate->inRangeColl,
    2055              :                                                         tailval,
    2056              :                                                         currval,
    2057              :                                                         winstate->endOffsetValue,
    2058              :                                                         BoolGetDatum(sub),
    2059              :                                                         BoolGetDatum(less))))
    2060         1820 :                         break;  /* this row is the correct frame tail */
    2061              :                 }
    2062              :                 /* Note we advance frametailpos even if the fetch fails */
    2063         3068 :                 winstate->frametailpos++;
    2064         3068 :                 spool_tuples(winstate, winstate->frametailpos);
    2065         3068 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    2066              :                                              winstate->frametail_slot))
    2067          320 :                     break;      /* end of partition */
    2068              :             }
    2069         3224 :             winstate->frametail_valid = true;
    2070              :         }
    2071          920 :         else if (frameOptions & FRAMEOPTION_GROUPS)
    2072              :         {
    2073              :             /*
    2074              :              * In GROUPS END_OFFSET mode, frame end is the last row of the
    2075              :              * last peer group whose number satisfies the offset constraint,
    2076              :              * and frame tail is the row after that (if any).  We keep a copy
    2077              :              * of the last-known frame tail row in frametail_slot, and advance
    2078              :              * as necessary.  Note that if we reach end of partition, we will
    2079              :              * leave frametailpos = end+1 and frametail_slot empty.
    2080              :              */
    2081          920 :             int64       offset = DatumGetInt64(winstate->endOffsetValue);
    2082          920 :             int64       maxtailgroup = 0;
    2083              : 
    2084          920 :             if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
    2085           48 :                 maxtailgroup = winstate->currentgroup - offset;
    2086              :             else
    2087              :             {
    2088              :                 /*
    2089              :                  * If we have an overflow, it means the target group is beyond
    2090              :                  * the range of int64.  We treat this as "infinity", which
    2091              :                  * ensures the loop below advances to end of partition.
    2092              :                  */
    2093          872 :                 if (pg_add_s64_overflow(winstate->currentgroup, offset,
    2094              :                                         &maxtailgroup))
    2095           28 :                     maxtailgroup = PG_INT64_MAX;
    2096              :             }
    2097              : 
    2098          920 :             tuplestore_select_read_pointer(winstate->buffer,
    2099              :                                            winstate->frametail_ptr);
    2100          920 :             if (winstate->frametailpos == 0 &&
    2101          260 :                 TupIsNull(winstate->frametail_slot))
    2102              :             {
    2103              :                 /* fetch first row into frametail_slot, if we didn't already */
    2104          248 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    2105              :                                              winstate->frametail_slot))
    2106            0 :                     elog(ERROR, "unexpected end of tuplestore");
    2107              :             }
    2108              : 
    2109         2508 :             while (!TupIsNull(winstate->frametail_slot))
    2110              :             {
    2111         1400 :                 if (winstate->frametailgroup > maxtailgroup)
    2112          496 :                     break;      /* this row is the correct frame tail */
    2113          904 :                 ExecCopySlot(winstate->temp_slot_2, winstate->frametail_slot);
    2114              :                 /* Note we advance frametailpos even if the fetch fails */
    2115          904 :                 winstate->frametailpos++;
    2116          904 :                 spool_tuples(winstate, winstate->frametailpos);
    2117          904 :                 if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    2118              :                                              winstate->frametail_slot))
    2119          236 :                     break;      /* end of partition */
    2120          668 :                 if (!are_peers(winstate, winstate->temp_slot_2,
    2121              :                                winstate->frametail_slot))
    2122          412 :                     winstate->frametailgroup++;
    2123              :             }
    2124          920 :             ExecClearTuple(winstate->temp_slot_2);
    2125          920 :             winstate->frametail_valid = true;
    2126              :         }
    2127              :         else
    2128              :             Assert(false);
    2129              :     }
    2130              :     else
    2131              :         Assert(false);
    2132              : 
    2133       123286 :     MemoryContextSwitchTo(oldcontext);
    2134              : }
    2135              : 
    2136              : /*
    2137              :  * update_grouptailpos
    2138              :  * make grouptailpos valid for the current row
    2139              :  *
    2140              :  * May clobber winstate->temp_slot_2.
    2141              :  */
    2142              : static void
    2143         3248 : update_grouptailpos(WindowAggState *winstate)
    2144              : {
    2145         3248 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
    2146              :     MemoryContext oldcontext;
    2147              : 
    2148         3248 :     if (winstate->grouptail_valid)
    2149         2636 :         return;                 /* already known for current row */
    2150              : 
    2151              :     /* We may be called in a short-lived context */
    2152          612 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
    2153              : 
    2154              :     /* If no ORDER BY, all rows are peers with each other */
    2155          612 :     if (node->ordNumCols == 0)
    2156              :     {
    2157            0 :         spool_tuples(winstate, -1);
    2158            0 :         winstate->grouptailpos = winstate->spooled_rows;
    2159            0 :         winstate->grouptail_valid = true;
    2160            0 :         MemoryContextSwitchTo(oldcontext);
    2161            0 :         return;
    2162              :     }
    2163              : 
    2164              :     /*
    2165              :      * Because grouptail_valid is reset only when current row advances into a
    2166              :      * new peer group, we always reach here knowing that grouptailpos needs to
    2167              :      * be advanced by at least one row.  Hence, unlike the otherwise similar
    2168              :      * case for frame tail tracking, we do not need persistent storage of the
    2169              :      * group tail row.
    2170              :      */
    2171              :     Assert(winstate->grouptailpos <= winstate->currentpos);
    2172          612 :     tuplestore_select_read_pointer(winstate->buffer,
    2173              :                                    winstate->grouptail_ptr);
    2174              :     for (;;)
    2175              :     {
    2176              :         /* Note we advance grouptailpos even if the fetch fails */
    2177         1172 :         winstate->grouptailpos++;
    2178         1172 :         spool_tuples(winstate, winstate->grouptailpos);
    2179         1172 :         if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    2180              :                                      winstate->temp_slot_2))
    2181          172 :             break;              /* end of partition */
    2182         1000 :         if (winstate->grouptailpos > winstate->currentpos &&
    2183          828 :             !are_peers(winstate, winstate->temp_slot_2,
    2184              :                        winstate->ss.ss_ScanTupleSlot))
    2185          440 :             break;              /* this row is the group tail */
    2186              :     }
    2187          612 :     ExecClearTuple(winstate->temp_slot_2);
    2188          612 :     winstate->grouptail_valid = true;
    2189              : 
    2190          612 :     MemoryContextSwitchTo(oldcontext);
    2191              : }
    2192              : 
    2193              : /*
    2194              :  * calculate_frame_offsets
    2195              :  *      Determine the startOffsetValue and endOffsetValue values for the
    2196              :  *      WindowAgg's frame options.
    2197              :  */
    2198              : static pg_noinline void
    2199         1605 : calculate_frame_offsets(PlanState *pstate)
    2200              : {
    2201         1605 :     WindowAggState *winstate = castNode(WindowAggState, pstate);
    2202              :     ExprContext *econtext;
    2203         1605 :     int         frameOptions = winstate->frameOptions;
    2204              :     Datum       value;
    2205              :     bool        isnull;
    2206              :     int16       len;
    2207              :     bool        byval;
    2208              : 
    2209              :     /* Ensure we've not been called before for this scan */
    2210              :     Assert(winstate->all_first);
    2211              : 
    2212         1605 :     econtext = winstate->ss.ps.ps_ExprContext;
    2213              : 
    2214         1605 :     if (frameOptions & FRAMEOPTION_START_OFFSET)
    2215              :     {
    2216              :         Assert(winstate->startOffset != NULL);
    2217          584 :         value = ExecEvalExprSwitchContext(winstate->startOffset,
    2218              :                                           econtext,
    2219              :                                           &isnull);
    2220          584 :         if (isnull)
    2221            0 :             ereport(ERROR,
    2222              :                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
    2223              :                      errmsg("frame starting offset must not be null")));
    2224              :         /* copy value into query-lifespan context */
    2225          584 :         get_typlenbyval(exprType((Node *) winstate->startOffset->expr),
    2226              :                         &len,
    2227              :                         &byval);
    2228          584 :         winstate->startOffsetValue = datumCopy(value, byval, len);
    2229          584 :         if (frameOptions & (FRAMEOPTION_ROWS | FRAMEOPTION_GROUPS))
    2230              :         {
    2231              :             /* value is known to be int8 */
    2232          240 :             int64       offset = DatumGetInt64(value);
    2233              : 
    2234          240 :             if (offset < 0)
    2235            0 :                 ereport(ERROR,
    2236              :                         (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
    2237              :                          errmsg("frame starting offset must not be negative")));
    2238              :         }
    2239              :     }
    2240              : 
    2241         1605 :     if (frameOptions & FRAMEOPTION_END_OFFSET)
    2242              :     {
    2243              :         Assert(winstate->endOffset != NULL);
    2244          656 :         value = ExecEvalExprSwitchContext(winstate->endOffset,
    2245              :                                           econtext,
    2246              :                                           &isnull);
    2247          656 :         if (isnull)
    2248            0 :             ereport(ERROR,
    2249              :                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
    2250              :                      errmsg("frame ending offset must not be null")));
    2251              :         /* copy value into query-lifespan context */
    2252          656 :         get_typlenbyval(exprType((Node *) winstate->endOffset->expr),
    2253              :                         &len,
    2254              :                         &byval);
    2255          656 :         winstate->endOffsetValue = datumCopy(value, byval, len);
    2256          656 :         if (frameOptions & (FRAMEOPTION_ROWS | FRAMEOPTION_GROUPS))
    2257              :         {
    2258              :             /* value is known to be int8 */
    2259          268 :             int64       offset = DatumGetInt64(value);
    2260              : 
    2261          268 :             if (offset < 0)
    2262            0 :                 ereport(ERROR,
    2263              :                         (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
    2264              :                          errmsg("frame ending offset must not be negative")));
    2265              :         }
    2266              :     }
    2267         1605 :     winstate->all_first = false;
    2268         1605 : }
    2269              : 
    2270              : /* -----------------
    2271              :  * ExecWindowAgg
    2272              :  *
    2273              :  *  ExecWindowAgg receives tuples from its outer subplan and
    2274              :  *  stores them into a tuplestore, then processes window functions.
    2275              :  *  This node doesn't reduce nor qualify any row so the number of
    2276              :  *  returned rows is exactly the same as its outer subplan's result.
    2277              :  * -----------------
    2278              :  */
    2279              : static TupleTableSlot *
    2280       605057 : ExecWindowAgg(PlanState *pstate)
    2281              : {
    2282       605057 :     WindowAggState *winstate = castNode(WindowAggState, pstate);
    2283              :     TupleTableSlot *slot;
    2284              :     ExprContext *econtext;
    2285              :     int         i;
    2286              :     int         numfuncs;
    2287              : 
    2288       605057 :     CHECK_FOR_INTERRUPTS();
    2289              : 
    2290       605057 :     if (winstate->status == WINDOWAGG_DONE)
    2291            0 :         return NULL;
    2292              : 
    2293              :     /*
    2294              :      * Compute frame offset values, if any, during first call (or after a
    2295              :      * rescan).  These are assumed to hold constant throughout the scan; if
    2296              :      * user gives us a volatile expression, we'll only use its initial value.
    2297              :      */
    2298       605057 :     if (unlikely(winstate->all_first))
    2299         1605 :         calculate_frame_offsets(pstate);
    2300              : 
    2301              :     /* We need to loop as the runCondition or qual may filter out tuples */
    2302              :     for (;;)
    2303              :     {
    2304       605145 :         if (winstate->next_partition)
    2305              :         {
    2306              :             /* Initialize for first partition and set current row = 0 */
    2307         1605 :             begin_partition(winstate);
    2308              :             /* If there are no input rows, we'll detect that and exit below */
    2309              :         }
    2310              :         else
    2311              :         {
    2312              :             /* Advance current row within partition */
    2313       603540 :             winstate->currentpos++;
    2314              :             /* This might mean that the frame moves, too */
    2315       603540 :             winstate->framehead_valid = false;
    2316       603540 :             winstate->frametail_valid = false;
    2317              :             /* we don't need to invalidate grouptail here; see below */
    2318              :         }
    2319              : 
    2320              :         /*
    2321              :          * Spool all tuples up to and including the current row, if we haven't
    2322              :          * already
    2323              :          */
    2324       605145 :         spool_tuples(winstate, winstate->currentpos);
    2325              : 
    2326              :         /* Move to the next partition if we reached the end of this partition */
    2327       605145 :         if (winstate->partition_spooled &&
    2328        42213 :             winstate->currentpos >= winstate->spooled_rows)
    2329              :         {
    2330         2263 :             release_partition(winstate);
    2331              : 
    2332         2263 :             if (winstate->more_partitions)
    2333              :             {
    2334          826 :                 begin_partition(winstate);
    2335              :                 Assert(winstate->spooled_rows > 0);
    2336              : 
    2337              :                 /* Come out of pass-through mode when changing partition */
    2338          826 :                 winstate->status = WINDOWAGG_RUN;
    2339              :             }
    2340              :             else
    2341              :             {
    2342              :                 /* No further partitions?  We're done */
    2343         1437 :                 winstate->status = WINDOWAGG_DONE;
    2344         1437 :                 return NULL;
    2345              :             }
    2346              :         }
    2347              : 
    2348              :         /* final output execution is in ps_ExprContext */
    2349       603708 :         econtext = winstate->ss.ps.ps_ExprContext;
    2350              : 
    2351              :         /* Clear the per-output-tuple context for current row */
    2352       603708 :         ResetExprContext(econtext);
    2353              : 
    2354              :         /*
    2355              :          * Read the current row from the tuplestore, and save in
    2356              :          * ScanTupleSlot. (We can't rely on the outerplan's output slot
    2357              :          * because we may have to read beyond the current row.  Also, we have
    2358              :          * to actually copy the row out of the tuplestore, since window
    2359              :          * function evaluation might cause the tuplestore to dump its state to
    2360              :          * disk.)
    2361              :          *
    2362              :          * In GROUPS mode, or when tracking a group-oriented exclusion clause,
    2363              :          * we must also detect entering a new peer group and update associated
    2364              :          * state when that happens.  We use temp_slot_2 to temporarily hold
    2365              :          * the previous row for this purpose.
    2366              :          *
    2367              :          * Current row must be in the tuplestore, since we spooled it above.
    2368              :          */
    2369       603708 :         tuplestore_select_read_pointer(winstate->buffer, winstate->current_ptr);
    2370       603708 :         if ((winstate->frameOptions & (FRAMEOPTION_GROUPS |
    2371              :                                        FRAMEOPTION_EXCLUDE_GROUP |
    2372         2012 :                                        FRAMEOPTION_EXCLUDE_TIES)) &&
    2373         2012 :             winstate->currentpos > 0)
    2374              :         {
    2375         1644 :             ExecCopySlot(winstate->temp_slot_2, winstate->ss.ss_ScanTupleSlot);
    2376         1644 :             if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    2377              :                                          winstate->ss.ss_ScanTupleSlot))
    2378            0 :                 elog(ERROR, "unexpected end of tuplestore");
    2379         1644 :             if (!are_peers(winstate, winstate->temp_slot_2,
    2380              :                            winstate->ss.ss_ScanTupleSlot))
    2381              :             {
    2382          852 :                 winstate->currentgroup++;
    2383          852 :                 winstate->groupheadpos = winstate->currentpos;
    2384          852 :                 winstate->grouptail_valid = false;
    2385              :             }
    2386         1644 :             ExecClearTuple(winstate->temp_slot_2);
    2387              :         }
    2388              :         else
    2389              :         {
    2390       602064 :             if (!tuplestore_gettupleslot(winstate->buffer, true, true,
    2391              :                                          winstate->ss.ss_ScanTupleSlot))
    2392            0 :                 elog(ERROR, "unexpected end of tuplestore");
    2393              :         }
    2394              : 
    2395              :         /* don't evaluate the window functions when we're in pass-through mode */
    2396       603708 :         if (winstate->status == WINDOWAGG_RUN)
    2397              :         {
    2398              :             /*
    2399              :              * Evaluate true window functions
    2400              :              */
    2401       603664 :             numfuncs = winstate->numfuncs;
    2402      1292472 :             for (i = 0; i < numfuncs; i++)
    2403              :             {
    2404       688916 :                 WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
    2405              : 
    2406       688916 :                 if (perfuncstate->plain_agg)
    2407       108261 :                     continue;
    2408       580655 :                 eval_windowfunction(winstate, perfuncstate,
    2409       580655 :                                     &(econtext->ecxt_aggvalues[perfuncstate->wfuncstate->wfuncno]),
    2410       580655 :                                     &(econtext->ecxt_aggnulls[perfuncstate->wfuncstate->wfuncno]));
    2411              :             }
    2412              : 
    2413              :             /*
    2414              :              * Evaluate aggregates
    2415              :              */
    2416       603556 :             if (winstate->numaggs > 0)
    2417       107065 :                 eval_windowaggregates(winstate);
    2418              :         }
    2419              : 
    2420              :         /*
    2421              :          * If we have created auxiliary read pointers for the frame or group
    2422              :          * boundaries, force them to be kept up-to-date, because we don't know
    2423              :          * whether the window function(s) will do anything that requires that.
    2424              :          * Failing to advance the pointers would result in being unable to
    2425              :          * trim data from the tuplestore, which is bad.  (If we could know in
    2426              :          * advance whether the window functions will use frame boundary info,
    2427              :          * we could skip creating these pointers in the first place ... but
    2428              :          * unfortunately the window function API doesn't require that.)
    2429              :          */
    2430       603572 :         if (winstate->framehead_ptr >= 0)
    2431         4234 :             update_frameheadpos(winstate);
    2432       603572 :         if (winstate->frametail_ptr >= 0)
    2433       122726 :             update_frametailpos(winstate);
    2434       603572 :         if (winstate->grouptail_ptr >= 0)
    2435         1000 :             update_grouptailpos(winstate);
    2436              : 
    2437              :         /*
    2438              :          * Truncate any no-longer-needed rows from the tuplestore.
    2439              :          */
    2440       603572 :         tuplestore_trim(winstate->buffer);
    2441              : 
    2442              :         /*
    2443              :          * Form and return a projection tuple using the windowfunc results and
    2444              :          * the current row.  Setting ecxt_outertuple arranges that any Vars
    2445              :          * will be evaluated with respect to that row.
    2446              :          */
    2447       603572 :         econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
    2448              : 
    2449       603572 :         slot = ExecProject(winstate->ss.ps.ps_ProjInfo);
    2450              : 
    2451       603572 :         if (winstate->status == WINDOWAGG_RUN)
    2452              :         {
    2453       603528 :             econtext->ecxt_scantuple = slot;
    2454              : 
    2455              :             /*
    2456              :              * Now evaluate the run condition to see if we need to go into
    2457              :              * pass-through mode, or maybe stop completely.
    2458              :              */
    2459       603528 :             if (!ExecQual(winstate->runcondition, econtext))
    2460              :             {
    2461              :                 /*
    2462              :                  * Determine which mode to move into.  If there is no
    2463              :                  * PARTITION BY clause and we're the top-level WindowAgg then
    2464              :                  * we're done.  This tuple and any future tuples cannot
    2465              :                  * possibly match the runcondition.  However, when there is a
    2466              :                  * PARTITION BY clause or we're not the top-level window we
    2467              :                  * can't just stop as we need to either process other
    2468              :                  * partitions or ensure WindowAgg nodes above us receive all
    2469              :                  * of the tuples they need to process their WindowFuncs.
    2470              :                  */
    2471           88 :                 if (winstate->use_pass_through)
    2472              :                 {
    2473              :                     /*
    2474              :                      * When switching into a pass-through mode, we'd better
    2475              :                      * NULLify the aggregate results as these are no longer
    2476              :                      * updated and NULLifying them avoids the old stale
    2477              :                      * results lingering.  Some of these might be byref types
    2478              :                      * so we can't have them pointing to free'd memory.  The
    2479              :                      * planner insisted that quals used in the runcondition
    2480              :                      * are strict, so the top-level WindowAgg will always
    2481              :                      * filter these NULLs out in the filter clause.
    2482              :                      */
    2483           60 :                     numfuncs = winstate->numfuncs;
    2484          176 :                     for (i = 0; i < numfuncs; i++)
    2485              :                     {
    2486          116 :                         econtext->ecxt_aggvalues[i] = (Datum) 0;
    2487          116 :                         econtext->ecxt_aggnulls[i] = true;
    2488              :                     }
    2489              : 
    2490              :                     /*
    2491              :                      * STRICT pass-through mode is required for the top window
    2492              :                      * when there is a PARTITION BY clause.  Otherwise we must
    2493              :                      * ensure we store tuples that don't match the
    2494              :                      * runcondition so they're available to WindowAggs above.
    2495              :                      */
    2496           60 :                     if (winstate->top_window)
    2497              :                     {
    2498           48 :                         winstate->status = WINDOWAGG_PASSTHROUGH_STRICT;
    2499           48 :                         continue;
    2500              :                     }
    2501              :                     else
    2502              :                     {
    2503           12 :                         winstate->status = WINDOWAGG_PASSTHROUGH;
    2504              :                     }
    2505              :                 }
    2506              :                 else
    2507              :                 {
    2508              :                     /*
    2509              :                      * Pass-through not required.  We can just return NULL.
    2510              :                      * Nothing else will match the runcondition.
    2511              :                      */
    2512           28 :                     winstate->status = WINDOWAGG_DONE;
    2513           28 :                     return NULL;
    2514              :                 }
    2515              :             }
    2516              : 
    2517              :             /*
    2518              :              * Filter out any tuples we don't need in the top-level WindowAgg.
    2519              :              */
    2520       603452 :             if (!ExecQual(winstate->ss.ps.qual, econtext))
    2521              :             {
    2522           12 :                 InstrCountFiltered1(winstate, 1);
    2523           12 :                 continue;
    2524              :             }
    2525              : 
    2526       603440 :             break;
    2527              :         }
    2528              : 
    2529              :         /*
    2530              :          * When not in WINDOWAGG_RUN mode, we must still return this tuple if
    2531              :          * we're anything apart from the top window.
    2532              :          */
    2533           44 :         else if (!winstate->top_window)
    2534           16 :             break;
    2535              :     }
    2536              : 
    2537       603456 :     return slot;
    2538              : }
    2539              : 
    2540              : /* -----------------
    2541              :  * ExecInitWindowAgg
    2542              :  *
    2543              :  *  Creates the run-time information for the WindowAgg node produced by the
    2544              :  *  planner and initializes its outer subtree
    2545              :  * -----------------
    2546              :  */
    2547              : WindowAggState *
    2548         1910 : ExecInitWindowAgg(WindowAgg *node, EState *estate, int eflags)
    2549              : {
    2550              :     WindowAggState *winstate;
    2551              :     Plan       *outerPlan;
    2552              :     ExprContext *econtext;
    2553              :     ExprContext *tmpcontext;
    2554              :     WindowStatePerFunc perfunc;
    2555              :     WindowStatePerAgg peragg;
    2556         1910 :     int         frameOptions = node->frameOptions;
    2557              :     int         numfuncs,
    2558              :                 wfuncno,
    2559              :                 numaggs,
    2560              :                 aggno;
    2561              :     TupleDesc   scanDesc;
    2562              :     ListCell   *l;
    2563              : 
    2564              :     /* check for unsupported flags */
    2565              :     Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
    2566              : 
    2567              :     /*
    2568              :      * create state structure
    2569              :      */
    2570         1910 :     winstate = makeNode(WindowAggState);
    2571         1910 :     winstate->ss.ps.plan = (Plan *) node;
    2572         1910 :     winstate->ss.ps.state = estate;
    2573         1910 :     winstate->ss.ps.ExecProcNode = ExecWindowAgg;
    2574              : 
    2575              :     /* copy frame options to state node for easy access */
    2576         1910 :     winstate->frameOptions = frameOptions;
    2577              : 
    2578              :     /*
    2579              :      * Create expression contexts.  We need two, one for per-input-tuple
    2580              :      * processing and one for per-output-tuple processing.  We cheat a little
    2581              :      * by using ExecAssignExprContext() to build both.
    2582              :      */
    2583         1910 :     ExecAssignExprContext(estate, &winstate->ss.ps);
    2584         1910 :     tmpcontext = winstate->ss.ps.ps_ExprContext;
    2585         1910 :     winstate->tmpcontext = tmpcontext;
    2586         1910 :     ExecAssignExprContext(estate, &winstate->ss.ps);
    2587              : 
    2588              :     /* Create long-lived context for storage of partition-local memory etc */
    2589         1910 :     winstate->partcontext =
    2590         1910 :         AllocSetContextCreate(CurrentMemoryContext,
    2591              :                               "WindowAgg Partition",
    2592              :                               ALLOCSET_DEFAULT_SIZES);
    2593              : 
    2594              :     /*
    2595              :      * Create mid-lived context for aggregate trans values etc.
    2596              :      *
    2597              :      * Note that moving aggregates each use their own private context, not
    2598              :      * this one.
    2599              :      */
    2600         1910 :     winstate->aggcontext =
    2601         1910 :         AllocSetContextCreate(CurrentMemoryContext,
    2602              :                               "WindowAgg Aggregates",
    2603              :                               ALLOCSET_DEFAULT_SIZES);
    2604              : 
    2605              :     /* Only the top-level WindowAgg may have a qual */
    2606              :     Assert(node->plan.qual == NIL || node->topWindow);
    2607              : 
    2608              :     /* Initialize the qual */
    2609         1910 :     winstate->ss.ps.qual = ExecInitQual(node->plan.qual,
    2610              :                                         (PlanState *) winstate);
    2611              : 
    2612              :     /*
    2613              :      * Setup the run condition, if we received one from the query planner.
    2614              :      * When set, this may allow us to move into pass-through mode so that we
    2615              :      * don't have to perform any further evaluation of WindowFuncs in the
    2616              :      * current partition or possibly stop returning tuples altogether when all
    2617              :      * tuples are in the same partition.
    2618              :      */
    2619         1910 :     winstate->runcondition = ExecInitQual(node->runCondition,
    2620              :                                           (PlanState *) winstate);
    2621              : 
    2622              :     /*
    2623              :      * When we're not the top-level WindowAgg node or we are but have a
    2624              :      * PARTITION BY clause we must move into one of the WINDOWAGG_PASSTHROUGH*
    2625              :      * modes when the runCondition becomes false.
    2626              :      */
    2627         1910 :     winstate->use_pass_through = !node->topWindow || node->partNumCols > 0;
    2628              : 
    2629              :     /* remember if we're the top-window or we are below the top-window */
    2630         1910 :     winstate->top_window = node->topWindow;
    2631              : 
    2632              :     /*
    2633              :      * initialize child nodes
    2634              :      */
    2635         1910 :     outerPlan = outerPlan(node);
    2636         1910 :     outerPlanState(winstate) = ExecInitNode(outerPlan, estate, eflags);
    2637              : 
    2638              :     /*
    2639              :      * initialize source tuple type (which is also the tuple type that we'll
    2640              :      * store in the tuplestore and use in all our working slots).
    2641              :      */
    2642         1910 :     ExecCreateScanSlotFromOuterPlan(estate, &winstate->ss, &TTSOpsMinimalTuple);
    2643         1910 :     scanDesc = winstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
    2644              : 
    2645              :     /* the outer tuple isn't the child's tuple, but always a minimal tuple */
    2646         1910 :     winstate->ss.ps.outeropsset = true;
    2647         1910 :     winstate->ss.ps.outerops = &TTSOpsMinimalTuple;
    2648         1910 :     winstate->ss.ps.outeropsfixed = true;
    2649              : 
    2650              :     /*
    2651              :      * tuple table initialization
    2652              :      */
    2653         1910 :     winstate->first_part_slot = ExecInitExtraTupleSlot(estate, scanDesc,
    2654              :                                                        &TTSOpsMinimalTuple);
    2655         1910 :     winstate->agg_row_slot = ExecInitExtraTupleSlot(estate, scanDesc,
    2656              :                                                     &TTSOpsMinimalTuple);
    2657         1910 :     winstate->temp_slot_1 = ExecInitExtraTupleSlot(estate, scanDesc,
    2658              :                                                    &TTSOpsMinimalTuple);
    2659         1910 :     winstate->temp_slot_2 = ExecInitExtraTupleSlot(estate, scanDesc,
    2660              :                                                    &TTSOpsMinimalTuple);
    2661              : 
    2662              :     /*
    2663              :      * create frame head and tail slots only if needed (must create slots in
    2664              :      * exactly the same cases that update_frameheadpos and update_frametailpos
    2665              :      * need them)
    2666              :      */
    2667         1910 :     winstate->framehead_slot = winstate->frametail_slot = NULL;
    2668              : 
    2669         1910 :     if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS))
    2670              :     {
    2671         1099 :         if (((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
    2672           54 :              node->ordNumCols != 0) ||
    2673         1045 :             (frameOptions & FRAMEOPTION_START_OFFSET))
    2674          502 :             winstate->framehead_slot = ExecInitExtraTupleSlot(estate, scanDesc,
    2675              :                                                               &TTSOpsMinimalTuple);
    2676         1099 :         if (((frameOptions & FRAMEOPTION_END_CURRENT_ROW) &&
    2677          569 :              node->ordNumCols != 0) ||
    2678          769 :             (frameOptions & FRAMEOPTION_END_OFFSET))
    2679          818 :             winstate->frametail_slot = ExecInitExtraTupleSlot(estate, scanDesc,
    2680              :                                                               &TTSOpsMinimalTuple);
    2681              :     }
    2682              : 
    2683              :     /*
    2684              :      * Initialize result slot, type and projection.
    2685              :      */
    2686         1910 :     ExecInitResultTupleSlotTL(&winstate->ss.ps, &TTSOpsVirtual);
    2687         1910 :     ExecAssignProjectionInfo(&winstate->ss.ps, NULL);
    2688              : 
    2689              :     /* Set up data for comparing tuples */
    2690         1910 :     if (node->partNumCols > 0)
    2691          452 :         winstate->partEqfunction =
    2692          452 :             execTuplesMatchPrepare(scanDesc,
    2693              :                                    node->partNumCols,
    2694          452 :                                    node->partColIdx,
    2695          452 :                                    node->partOperators,
    2696          452 :                                    node->partCollations,
    2697              :                                    &winstate->ss.ps);
    2698              : 
    2699         1910 :     if (node->ordNumCols > 0)
    2700         1481 :         winstate->ordEqfunction =
    2701         1481 :             execTuplesMatchPrepare(scanDesc,
    2702              :                                    node->ordNumCols,
    2703         1481 :                                    node->ordColIdx,
    2704         1481 :                                    node->ordOperators,
    2705         1481 :                                    node->ordCollations,
    2706              :                                    &winstate->ss.ps);
    2707              : 
    2708              :     /*
    2709              :      * WindowAgg nodes use aggvalues and aggnulls as well as Agg nodes.
    2710              :      */
    2711         1910 :     numfuncs = winstate->numfuncs;
    2712         1910 :     numaggs = winstate->numaggs;
    2713         1910 :     econtext = winstate->ss.ps.ps_ExprContext;
    2714         1910 :     econtext->ecxt_aggvalues = palloc0_array(Datum, numfuncs);
    2715         1910 :     econtext->ecxt_aggnulls = palloc0_array(bool, numfuncs);
    2716              : 
    2717              :     /*
    2718              :      * allocate per-wfunc/per-agg state information.
    2719              :      */
    2720         1910 :     perfunc = palloc0_array(WindowStatePerFuncData, numfuncs);
    2721         1910 :     peragg = palloc0_array(WindowStatePerAggData, numaggs);
    2722         1910 :     winstate->perfunc = perfunc;
    2723         1910 :     winstate->peragg = peragg;
    2724              : 
    2725         1910 :     wfuncno = -1;
    2726         1910 :     aggno = -1;
    2727         4384 :     foreach(l, winstate->funcs)
    2728              :     {
    2729         2474 :         WindowFuncExprState *wfuncstate = (WindowFuncExprState *) lfirst(l);
    2730         2474 :         WindowFunc *wfunc = wfuncstate->wfunc;
    2731              :         WindowStatePerFunc perfuncstate;
    2732              :         AclResult   aclresult;
    2733              :         int         i;
    2734              : 
    2735         2474 :         if (wfunc->winref != node->winref)    /* planner screwed up? */
    2736            0 :             elog(ERROR, "WindowFunc with winref %u assigned to WindowAgg with winref %u",
    2737              :                  wfunc->winref, node->winref);
    2738              : 
    2739              :         /*
    2740              :          * Look for a previous duplicate window function, which needs the same
    2741              :          * ignore_nulls value
    2742              :          */
    2743         3210 :         for (i = 0; i <= wfuncno; i++)
    2744              :         {
    2745          744 :             if (equal(wfunc, perfunc[i].wfunc) &&
    2746            8 :                 !contain_volatile_functions((Node *) wfunc))
    2747            8 :                 break;
    2748              :         }
    2749         2474 :         if (i <= wfuncno && wfunc->ignore_nulls == perfunc[i].ignore_nulls)
    2750              :         {
    2751              :             /* Found a match to an existing entry, so just mark it */
    2752            8 :             wfuncstate->wfuncno = i;
    2753            8 :             continue;
    2754              :         }
    2755              : 
    2756              :         /* Nope, so assign a new PerAgg record */
    2757         2466 :         perfuncstate = &perfunc[++wfuncno];
    2758              : 
    2759              :         /* Mark WindowFunc state node with assigned index in the result array */
    2760         2466 :         wfuncstate->wfuncno = wfuncno;
    2761              : 
    2762              :         /* Check permission to call window function */
    2763         2466 :         aclresult = object_aclcheck(ProcedureRelationId, wfunc->winfnoid, GetUserId(),
    2764              :                                     ACL_EXECUTE);
    2765         2466 :         if (aclresult != ACLCHECK_OK)
    2766            0 :             aclcheck_error(aclresult, OBJECT_FUNCTION,
    2767            0 :                            get_func_name(wfunc->winfnoid));
    2768         2466 :         InvokeFunctionExecuteHook(wfunc->winfnoid);
    2769              : 
    2770              :         /* Fill in the perfuncstate data */
    2771         2466 :         perfuncstate->wfuncstate = wfuncstate;
    2772         2466 :         perfuncstate->wfunc = wfunc;
    2773         2466 :         perfuncstate->numArguments = list_length(wfuncstate->args);
    2774         2466 :         perfuncstate->winCollation = wfunc->inputcollid;
    2775              : 
    2776         2466 :         get_typlenbyval(wfunc->wintype,
    2777              :                         &perfuncstate->resulttypeLen,
    2778              :                         &perfuncstate->resulttypeByVal);
    2779              : 
    2780              :         /*
    2781              :          * If it's really just a plain aggregate function, we'll emulate the
    2782              :          * Agg environment for it.
    2783              :          */
    2784         2466 :         perfuncstate->plain_agg = wfunc->winagg;
    2785         2466 :         if (wfunc->winagg)
    2786              :         {
    2787              :             WindowStatePerAgg peraggstate;
    2788              : 
    2789         1103 :             perfuncstate->aggno = ++aggno;
    2790         1103 :             peraggstate = &winstate->peragg[aggno];
    2791         1103 :             initialize_peragg(winstate, wfunc, peraggstate);
    2792         1103 :             peraggstate->wfuncno = wfuncno;
    2793              :         }
    2794              :         else
    2795              :         {
    2796         1363 :             WindowObject winobj = makeNode(WindowObjectData);
    2797              : 
    2798         1363 :             winobj->winstate = winstate;
    2799         1363 :             winobj->argstates = wfuncstate->args;
    2800         1363 :             winobj->localmem = NULL;
    2801         1363 :             perfuncstate->winobj = winobj;
    2802         1363 :             winobj->ignore_nulls = wfunc->ignore_nulls;
    2803         1363 :             init_notnull_info(winobj, perfuncstate);
    2804              : 
    2805              :             /* It's a real window function, so set up to call it. */
    2806         1363 :             fmgr_info_cxt(wfunc->winfnoid, &perfuncstate->flinfo,
    2807              :                           econtext->ecxt_per_query_memory);
    2808         1363 :             fmgr_info_set_expr((Node *) wfunc, &perfuncstate->flinfo);
    2809              :         }
    2810              :     }
    2811              : 
    2812              :     /* Update numfuncs, numaggs to match number of unique functions found */
    2813         1910 :     winstate->numfuncs = wfuncno + 1;
    2814         1910 :     winstate->numaggs = aggno + 1;
    2815              : 
    2816              :     /* Set up WindowObject for aggregates, if needed */
    2817         1910 :     if (winstate->numaggs > 0)
    2818              :     {
    2819         1027 :         WindowObject agg_winobj = makeNode(WindowObjectData);
    2820              : 
    2821         1027 :         agg_winobj->winstate = winstate;
    2822         1027 :         agg_winobj->argstates = NIL;
    2823         1027 :         agg_winobj->localmem = NULL;
    2824              :         /* make sure markptr = -1 to invalidate. It may not get used */
    2825         1027 :         agg_winobj->markptr = -1;
    2826         1027 :         agg_winobj->readptr = -1;
    2827         1027 :         winstate->agg_winobj = agg_winobj;
    2828              :     }
    2829              : 
    2830              :     /* Set the status to running */
    2831         1910 :     winstate->status = WINDOWAGG_RUN;
    2832              : 
    2833              :     /* initialize frame bound offset expressions */
    2834         1910 :     winstate->startOffset = ExecInitExpr((Expr *) node->startOffset,
    2835              :                                          (PlanState *) winstate);
    2836         1910 :     winstate->endOffset = ExecInitExpr((Expr *) node->endOffset,
    2837              :                                        (PlanState *) winstate);
    2838              : 
    2839              :     /* Lookup in_range support functions if needed */
    2840         1910 :     if (OidIsValid(node->startInRangeFunc))
    2841          348 :         fmgr_info(node->startInRangeFunc, &winstate->startInRangeFunc);
    2842         1910 :     if (OidIsValid(node->endInRangeFunc))
    2843          392 :         fmgr_info(node->endInRangeFunc, &winstate->endInRangeFunc);
    2844         1910 :     winstate->inRangeColl = node->inRangeColl;
    2845         1910 :     winstate->inRangeAsc = node->inRangeAsc;
    2846         1910 :     winstate->inRangeNullsFirst = node->inRangeNullsFirst;
    2847              : 
    2848         1910 :     winstate->all_first = true;
    2849         1910 :     winstate->partition_spooled = false;
    2850         1910 :     winstate->more_partitions = false;
    2851         1910 :     winstate->next_partition = true;
    2852              : 
    2853         1910 :     return winstate;
    2854              : }
    2855              : 
    2856              : /* -----------------
    2857              :  * ExecEndWindowAgg
    2858              :  * -----------------
    2859              :  */
    2860              : void
    2861         1774 : ExecEndWindowAgg(WindowAggState *node)
    2862              : {
    2863              :     PlanState  *outerPlan;
    2864              :     int         i;
    2865              : 
    2866         1774 :     if (node->buffer != NULL)
    2867              :     {
    2868         1417 :         tuplestore_end(node->buffer);
    2869              : 
    2870              :         /* nullify so that release_partition skips the tuplestore_clear() */
    2871         1417 :         node->buffer = NULL;
    2872              :     }
    2873              : 
    2874         1774 :     release_partition(node);
    2875              : 
    2876         2849 :     for (i = 0; i < node->numaggs; i++)
    2877              :     {
    2878         1075 :         if (node->peragg[i].aggcontext != node->aggcontext)
    2879          540 :             MemoryContextDelete(node->peragg[i].aggcontext);
    2880              :     }
    2881         1774 :     MemoryContextDelete(node->partcontext);
    2882         1774 :     MemoryContextDelete(node->aggcontext);
    2883              : 
    2884         1774 :     pfree(node->perfunc);
    2885         1774 :     pfree(node->peragg);
    2886              : 
    2887         1774 :     outerPlan = outerPlanState(node);
    2888         1774 :     ExecEndNode(outerPlan);
    2889         1774 : }
    2890              : 
    2891              : /* -----------------
    2892              :  * ExecReScanWindowAgg
    2893              :  * -----------------
    2894              :  */
    2895              : void
    2896           52 : ExecReScanWindowAgg(WindowAggState *node)
    2897              : {
    2898           52 :     PlanState  *outerPlan = outerPlanState(node);
    2899           52 :     ExprContext *econtext = node->ss.ps.ps_ExprContext;
    2900              : 
    2901           52 :     node->status = WINDOWAGG_RUN;
    2902           52 :     node->all_first = true;
    2903              : 
    2904              :     /* release tuplestore et al */
    2905           52 :     release_partition(node);
    2906              : 
    2907              :     /* release all temp tuples, but especially first_part_slot */
    2908           52 :     ExecClearTuple(node->ss.ss_ScanTupleSlot);
    2909           52 :     ExecClearTuple(node->first_part_slot);
    2910           52 :     ExecClearTuple(node->agg_row_slot);
    2911           52 :     ExecClearTuple(node->temp_slot_1);
    2912           52 :     ExecClearTuple(node->temp_slot_2);
    2913           52 :     if (node->framehead_slot)
    2914            0 :         ExecClearTuple(node->framehead_slot);
    2915           52 :     if (node->frametail_slot)
    2916            4 :         ExecClearTuple(node->frametail_slot);
    2917              : 
    2918              :     /* Forget current wfunc values */
    2919          104 :     MemSet(econtext->ecxt_aggvalues, 0, sizeof(Datum) * node->numfuncs);
    2920           52 :     MemSet(econtext->ecxt_aggnulls, 0, sizeof(bool) * node->numfuncs);
    2921              : 
    2922              :     /*
    2923              :      * if chgParam of subnode is not null then plan will be re-scanned by
    2924              :      * first ExecProcNode.
    2925              :      */
    2926           52 :     if (outerPlan->chgParam == NULL)
    2927            4 :         ExecReScan(outerPlan);
    2928           52 : }
    2929              : 
    2930              : /*
    2931              :  * initialize_peragg
    2932              :  *
    2933              :  * Almost same as in nodeAgg.c, except we don't support DISTINCT currently.
    2934              :  */
    2935              : static WindowStatePerAggData *
    2936         1103 : initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc,
    2937              :                   WindowStatePerAgg peraggstate)
    2938              : {
    2939              :     Oid         inputTypes[FUNC_MAX_ARGS];
    2940              :     int         numArguments;
    2941              :     HeapTuple   aggTuple;
    2942              :     Form_pg_aggregate aggform;
    2943              :     Oid         aggtranstype;
    2944              :     AttrNumber  initvalAttNo;
    2945              :     AclResult   aclresult;
    2946              :     bool        use_ma_code;
    2947              :     Oid         transfn_oid,
    2948              :                 invtransfn_oid,
    2949              :                 finalfn_oid;
    2950              :     bool        finalextra;
    2951              :     char        finalmodify;
    2952              :     Expr       *transfnexpr,
    2953              :                *invtransfnexpr,
    2954              :                *finalfnexpr;
    2955              :     Datum       textInitVal;
    2956              :     int         i;
    2957              :     ListCell   *lc;
    2958              : 
    2959         1103 :     numArguments = list_length(wfunc->args);
    2960              : 
    2961         1103 :     i = 0;
    2962         2130 :     foreach(lc, wfunc->args)
    2963              :     {
    2964         1027 :         inputTypes[i++] = exprType((Node *) lfirst(lc));
    2965              :     }
    2966              : 
    2967         1103 :     aggTuple = SearchSysCache1(AGGFNOID, ObjectIdGetDatum(wfunc->winfnoid));
    2968         1103 :     if (!HeapTupleIsValid(aggTuple))
    2969            0 :         elog(ERROR, "cache lookup failed for aggregate %u",
    2970              :              wfunc->winfnoid);
    2971         1103 :     aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
    2972              : 
    2973              :     /*
    2974              :      * Figure out whether we want to use the moving-aggregate implementation,
    2975              :      * and collect the right set of fields from the pg_aggregate entry.
    2976              :      *
    2977              :      * It's possible that an aggregate would supply a safe moving-aggregate
    2978              :      * implementation and an unsafe normal one, in which case our hand is
    2979              :      * forced.  Otherwise, if the frame head can't move, we don't need
    2980              :      * moving-aggregate code.  Even if we'd like to use it, don't do so if the
    2981              :      * aggregate's arguments (and FILTER clause if any) contain any calls to
    2982              :      * volatile functions.  Otherwise, the difference between restarting and
    2983              :      * not restarting the aggregation would be user-visible.
    2984              :      *
    2985              :      * We also don't risk using moving aggregates when there are subplans in
    2986              :      * the arguments or FILTER clause.  This is partly because
    2987              :      * contain_volatile_functions() doesn't look inside subplans; but there
    2988              :      * are other reasons why a subplan's output might be volatile.  For
    2989              :      * example, syncscan mode can render the results nonrepeatable.
    2990              :      */
    2991         1103 :     if (!OidIsValid(aggform->aggminvtransfn))
    2992          189 :         use_ma_code = false;    /* sine qua non */
    2993          914 :     else if (aggform->aggmfinalmodify == AGGMODIFY_READ_ONLY &&
    2994          914 :              aggform->aggfinalmodify != AGGMODIFY_READ_ONLY)
    2995            0 :         use_ma_code = true;     /* decision forced by safety */
    2996          914 :     else if (winstate->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
    2997          358 :         use_ma_code = false;    /* non-moving frame head */
    2998          556 :     else if (contain_volatile_functions((Node *) wfunc))
    2999            8 :         use_ma_code = false;    /* avoid possible behavioral change */
    3000          548 :     else if (contain_subplans((Node *) wfunc))
    3001            0 :         use_ma_code = false;    /* subplans might contain volatile functions */
    3002              :     else
    3003          548 :         use_ma_code = true;     /* yes, let's use it */
    3004         1103 :     if (use_ma_code)
    3005              :     {
    3006          548 :         peraggstate->transfn_oid = transfn_oid = aggform->aggmtransfn;
    3007          548 :         peraggstate->invtransfn_oid = invtransfn_oid = aggform->aggminvtransfn;
    3008          548 :         peraggstate->finalfn_oid = finalfn_oid = aggform->aggmfinalfn;
    3009          548 :         finalextra = aggform->aggmfinalextra;
    3010          548 :         finalmodify = aggform->aggmfinalmodify;
    3011          548 :         aggtranstype = aggform->aggmtranstype;
    3012          548 :         initvalAttNo = Anum_pg_aggregate_aggminitval;
    3013              :     }
    3014              :     else
    3015              :     {
    3016          555 :         peraggstate->transfn_oid = transfn_oid = aggform->aggtransfn;
    3017          555 :         peraggstate->invtransfn_oid = invtransfn_oid = InvalidOid;
    3018          555 :         peraggstate->finalfn_oid = finalfn_oid = aggform->aggfinalfn;
    3019          555 :         finalextra = aggform->aggfinalextra;
    3020          555 :         finalmodify = aggform->aggfinalmodify;
    3021          555 :         aggtranstype = aggform->aggtranstype;
    3022          555 :         initvalAttNo = Anum_pg_aggregate_agginitval;
    3023              :     }
    3024              : 
    3025              :     /*
    3026              :      * ExecInitWindowAgg already checked permission to call aggregate function
    3027              :      * ... but we still need to check the component functions
    3028              :      */
    3029              : 
    3030              :     /* Check that aggregate owner has permission to call component fns */
    3031              :     {
    3032              :         HeapTuple   procTuple;
    3033              :         Oid         aggOwner;
    3034              : 
    3035         1103 :         procTuple = SearchSysCache1(PROCOID,
    3036              :                                     ObjectIdGetDatum(wfunc->winfnoid));
    3037         1103 :         if (!HeapTupleIsValid(procTuple))
    3038            0 :             elog(ERROR, "cache lookup failed for function %u",
    3039              :                  wfunc->winfnoid);
    3040         1103 :         aggOwner = ((Form_pg_proc) GETSTRUCT(procTuple))->proowner;
    3041         1103 :         ReleaseSysCache(procTuple);
    3042              : 
    3043         1103 :         aclresult = object_aclcheck(ProcedureRelationId, transfn_oid, aggOwner,
    3044              :                                     ACL_EXECUTE);
    3045         1103 :         if (aclresult != ACLCHECK_OK)
    3046            0 :             aclcheck_error(aclresult, OBJECT_FUNCTION,
    3047            0 :                            get_func_name(transfn_oid));
    3048         1103 :         InvokeFunctionExecuteHook(transfn_oid);
    3049              : 
    3050         1103 :         if (OidIsValid(invtransfn_oid))
    3051              :         {
    3052          548 :             aclresult = object_aclcheck(ProcedureRelationId, invtransfn_oid, aggOwner,
    3053              :                                         ACL_EXECUTE);
    3054          548 :             if (aclresult != ACLCHECK_OK)
    3055            0 :                 aclcheck_error(aclresult, OBJECT_FUNCTION,
    3056            0 :                                get_func_name(invtransfn_oid));
    3057          548 :             InvokeFunctionExecuteHook(invtransfn_oid);
    3058              :         }
    3059              : 
    3060         1103 :         if (OidIsValid(finalfn_oid))
    3061              :         {
    3062          584 :             aclresult = object_aclcheck(ProcedureRelationId, finalfn_oid, aggOwner,
    3063              :                                         ACL_EXECUTE);
    3064          584 :             if (aclresult != ACLCHECK_OK)
    3065            0 :                 aclcheck_error(aclresult, OBJECT_FUNCTION,
    3066            0 :                                get_func_name(finalfn_oid));
    3067          584 :             InvokeFunctionExecuteHook(finalfn_oid);
    3068              :         }
    3069              :     }
    3070              : 
    3071              :     /*
    3072              :      * If the selected finalfn isn't read-only, we can't run this aggregate as
    3073              :      * a window function.  This is a user-facing error, so we take a bit more
    3074              :      * care with the error message than elsewhere in this function.
    3075              :      */
    3076         1103 :     if (finalmodify != AGGMODIFY_READ_ONLY)
    3077            0 :         ereport(ERROR,
    3078              :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3079              :                  errmsg("aggregate function %s does not support use as a window function",
    3080              :                         format_procedure(wfunc->winfnoid))));
    3081              : 
    3082              :     /* Detect how many arguments to pass to the finalfn */
    3083         1103 :     if (finalextra)
    3084           16 :         peraggstate->numFinalArgs = numArguments + 1;
    3085              :     else
    3086         1087 :         peraggstate->numFinalArgs = 1;
    3087              : 
    3088              :     /* resolve actual type of transition state, if polymorphic */
    3089         1103 :     aggtranstype = resolve_aggregate_transtype(wfunc->winfnoid,
    3090              :                                                aggtranstype,
    3091              :                                                inputTypes,
    3092              :                                                numArguments);
    3093              : 
    3094              :     /* build expression trees using actual argument & result types */
    3095         1103 :     build_aggregate_transfn_expr(inputTypes,
    3096              :                                  numArguments,
    3097              :                                  0, /* no ordered-set window functions yet */
    3098              :                                  false, /* no variadic window functions yet */
    3099              :                                  aggtranstype,
    3100              :                                  wfunc->inputcollid,
    3101              :                                  transfn_oid,
    3102              :                                  invtransfn_oid,
    3103              :                                  &transfnexpr,
    3104              :                                  &invtransfnexpr);
    3105              : 
    3106              :     /* set up infrastructure for calling the transfn(s) and finalfn */
    3107         1103 :     fmgr_info(transfn_oid, &peraggstate->transfn);
    3108         1103 :     fmgr_info_set_expr((Node *) transfnexpr, &peraggstate->transfn);
    3109              : 
    3110         1103 :     if (OidIsValid(invtransfn_oid))
    3111              :     {
    3112          548 :         fmgr_info(invtransfn_oid, &peraggstate->invtransfn);
    3113          548 :         fmgr_info_set_expr((Node *) invtransfnexpr, &peraggstate->invtransfn);
    3114              :     }
    3115              : 
    3116         1103 :     if (OidIsValid(finalfn_oid))
    3117              :     {
    3118          584 :         build_aggregate_finalfn_expr(inputTypes,
    3119              :                                      peraggstate->numFinalArgs,
    3120              :                                      aggtranstype,
    3121              :                                      wfunc->wintype,
    3122              :                                      wfunc->inputcollid,
    3123              :                                      finalfn_oid,
    3124              :                                      &finalfnexpr);
    3125          584 :         fmgr_info(finalfn_oid, &peraggstate->finalfn);
    3126          584 :         fmgr_info_set_expr((Node *) finalfnexpr, &peraggstate->finalfn);
    3127              :     }
    3128              : 
    3129              :     /* get info about relevant datatypes */
    3130         1103 :     get_typlenbyval(wfunc->wintype,
    3131              :                     &peraggstate->resulttypeLen,
    3132              :                     &peraggstate->resulttypeByVal);
    3133         1103 :     get_typlenbyval(aggtranstype,
    3134              :                     &peraggstate->transtypeLen,
    3135              :                     &peraggstate->transtypeByVal);
    3136              : 
    3137              :     /*
    3138              :      * initval is potentially null, so don't try to access it as a struct
    3139              :      * field. Must do it the hard way with SysCacheGetAttr.
    3140              :      */
    3141         1103 :     textInitVal = SysCacheGetAttr(AGGFNOID, aggTuple, initvalAttNo,
    3142              :                                   &peraggstate->initValueIsNull);
    3143              : 
    3144         1103 :     if (peraggstate->initValueIsNull)
    3145          617 :         peraggstate->initValue = (Datum) 0;
    3146              :     else
    3147          486 :         peraggstate->initValue = GetAggInitVal(textInitVal,
    3148              :                                                aggtranstype);
    3149              : 
    3150              :     /*
    3151              :      * If the transfn is strict and the initval is NULL, make sure input type
    3152              :      * and transtype are the same (or at least binary-compatible), so that
    3153              :      * it's OK to use the first input value as the initial transValue.  This
    3154              :      * should have been checked at agg definition time, but we must check
    3155              :      * again in case the transfn's strictness property has been changed.
    3156              :      */
    3157         1103 :     if (peraggstate->transfn.fn_strict && peraggstate->initValueIsNull)
    3158              :     {
    3159          161 :         if (numArguments < 1 ||
    3160          161 :             !IsBinaryCoercible(inputTypes[0], aggtranstype))
    3161            0 :             ereport(ERROR,
    3162              :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
    3163              :                      errmsg("aggregate %u needs to have compatible input type and transition type",
    3164              :                             wfunc->winfnoid)));
    3165              :     }
    3166              : 
    3167              :     /*
    3168              :      * Insist that forward and inverse transition functions have the same
    3169              :      * strictness setting.  Allowing them to differ would require handling
    3170              :      * more special cases in advance_windowaggregate and
    3171              :      * advance_windowaggregate_base, for no discernible benefit.  This should
    3172              :      * have been checked at agg definition time, but we must check again in
    3173              :      * case either function's strictness property has been changed.
    3174              :      */
    3175         1103 :     if (OidIsValid(invtransfn_oid) &&
    3176          548 :         peraggstate->transfn.fn_strict != peraggstate->invtransfn.fn_strict)
    3177            0 :         ereport(ERROR,
    3178              :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
    3179              :                  errmsg("strictness of aggregate's forward and inverse transition functions must match")));
    3180              : 
    3181              :     /*
    3182              :      * Moving aggregates use their own aggcontext.
    3183              :      *
    3184              :      * This is necessary because they might restart at different times, so we
    3185              :      * might never be able to reset the shared context otherwise.  We can't
    3186              :      * make it the aggregates' responsibility to clean up after themselves,
    3187              :      * because strict aggregates must be restarted whenever we remove their
    3188              :      * last non-NULL input, which the aggregate won't be aware is happening.
    3189              :      * Also, just pfree()ing the transValue upon restarting wouldn't help,
    3190              :      * since we'd miss any indirectly referenced data.  We could, in theory,
    3191              :      * make the memory allocation rules for moving aggregates different than
    3192              :      * they have historically been for plain aggregates, but that seems grotty
    3193              :      * and likely to lead to memory leaks.
    3194              :      */
    3195         1103 :     if (OidIsValid(invtransfn_oid))
    3196          548 :         peraggstate->aggcontext =
    3197          548 :             AllocSetContextCreate(CurrentMemoryContext,
    3198              :                                   "WindowAgg Per Aggregate",
    3199              :                                   ALLOCSET_DEFAULT_SIZES);
    3200              :     else
    3201          555 :         peraggstate->aggcontext = winstate->aggcontext;
    3202              : 
    3203         1103 :     ReleaseSysCache(aggTuple);
    3204              : 
    3205         1103 :     return peraggstate;
    3206              : }
    3207              : 
    3208              : static Datum
    3209          486 : GetAggInitVal(Datum textInitVal, Oid transtype)
    3210              : {
    3211              :     Oid         typinput,
    3212              :                 typioparam;
    3213              :     char       *strInitVal;
    3214              :     Datum       initVal;
    3215              : 
    3216          486 :     getTypeInputInfo(transtype, &typinput, &typioparam);
    3217          486 :     strInitVal = TextDatumGetCString(textInitVal);
    3218          486 :     initVal = OidInputFunctionCall(typinput, strInitVal,
    3219              :                                    typioparam, -1);
    3220          486 :     pfree(strInitVal);
    3221          486 :     return initVal;
    3222              : }
    3223              : 
    3224              : /*
    3225              :  * are_peers
    3226              :  * compare two rows to see if they are equal according to the ORDER BY clause
    3227              :  *
    3228              :  * NB: this does not consider the window frame mode.
    3229              :  */
    3230              : static bool
    3231       398487 : are_peers(WindowAggState *winstate, TupleTableSlot *slot1,
    3232              :           TupleTableSlot *slot2)
    3233              : {
    3234       398487 :     WindowAgg  *node = (WindowAgg *) winstate->ss.ps.plan;
    3235       398487 :     ExprContext *econtext = winstate->tmpcontext;
    3236              : 
    3237              :     /* If no ORDER BY, all rows are peers with each other */
    3238       398487 :     if (node->ordNumCols == 0)
    3239        20702 :         return true;
    3240              : 
    3241       377785 :     econtext->ecxt_outertuple = slot1;
    3242       377785 :     econtext->ecxt_innertuple = slot2;
    3243       377785 :     return ExecQualAndReset(winstate->ordEqfunction, econtext);
    3244              : }
    3245              : 
    3246              : /*
    3247              :  * window_gettupleslot
    3248              :  *  Fetch the pos'th tuple of the current partition into the slot,
    3249              :  *  using the winobj's read pointer
    3250              :  *
    3251              :  * Returns true if successful, false if no such row
    3252              :  */
    3253              : static bool
    3254       509879 : window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
    3255              : {
    3256       509879 :     WindowAggState *winstate = winobj->winstate;
    3257              :     MemoryContext oldcontext;
    3258              : 
    3259              :     /* often called repeatedly in a row */
    3260       509879 :     CHECK_FOR_INTERRUPTS();
    3261              : 
    3262              :     /* Don't allow passing -1 to spool_tuples here */
    3263       509879 :     if (pos < 0)
    3264          272 :         return false;
    3265              : 
    3266              :     /* If necessary, fetch the tuple into the spool */
    3267       509607 :     spool_tuples(winstate, pos);
    3268              : 
    3269       509607 :     if (pos >= winstate->spooled_rows)
    3270         3331 :         return false;
    3271              : 
    3272       506276 :     if (pos < winobj->markpos)
    3273            0 :         elog(ERROR, "cannot fetch row before WindowObject's mark position");
    3274              : 
    3275       506276 :     oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
    3276              : 
    3277       506276 :     tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
    3278              : 
    3279              :     /*
    3280              :      * Advance or rewind until we are within one tuple of the one we want.
    3281              :      */
    3282       506276 :     if (winobj->seekpos < pos - 1)
    3283              :     {
    3284         1664 :         if (!tuplestore_skiptuples(winstate->buffer,
    3285         1664 :                                    pos - 1 - winobj->seekpos,
    3286              :                                    true))
    3287            0 :             elog(ERROR, "unexpected end of tuplestore");
    3288         1664 :         winobj->seekpos = pos - 1;
    3289              :     }
    3290       504612 :     else if (winobj->seekpos > pos + 1)
    3291              :     {
    3292         1884 :         if (!tuplestore_skiptuples(winstate->buffer,
    3293         1884 :                                    winobj->seekpos - (pos + 1),
    3294              :                                    false))
    3295            0 :             elog(ERROR, "unexpected end of tuplestore");
    3296         1884 :         winobj->seekpos = pos + 1;
    3297              :     }
    3298       502728 :     else if (winobj->seekpos == pos)
    3299              :     {
    3300              :         /*
    3301              :          * There's no API to refetch the tuple at the current position.  We
    3302              :          * have to move one tuple forward, and then one backward.  (We don't
    3303              :          * do it the other way because we might try to fetch the row before
    3304              :          * our mark, which isn't allowed.)  XXX this case could stand to be
    3305              :          * optimized.
    3306              :          */
    3307       115463 :         tuplestore_advance(winstate->buffer, true);
    3308       115463 :         winobj->seekpos++;
    3309              :     }
    3310              : 
    3311              :     /*
    3312              :      * Now we should be on the tuple immediately before or after the one we
    3313              :      * want, so just fetch forwards or backwards as appropriate.
    3314              :      *
    3315              :      * Notice that we tell tuplestore_gettupleslot to make a physical copy of
    3316              :      * the fetched tuple.  This ensures that the slot's contents remain valid
    3317              :      * through manipulations of the tuplestore, which some callers depend on.
    3318              :      */
    3319       506276 :     if (winobj->seekpos > pos)
    3320              :     {
    3321       117535 :         if (!tuplestore_gettupleslot(winstate->buffer, false, true, slot))
    3322            0 :             elog(ERROR, "unexpected end of tuplestore");
    3323       117535 :         winobj->seekpos--;
    3324              :     }
    3325              :     else
    3326              :     {
    3327       388741 :         if (!tuplestore_gettupleslot(winstate->buffer, true, true, slot))
    3328            0 :             elog(ERROR, "unexpected end of tuplestore");
    3329       388741 :         winobj->seekpos++;
    3330              :     }
    3331              : 
    3332              :     Assert(winobj->seekpos == pos);
    3333              : 
    3334       506276 :     MemoryContextSwitchTo(oldcontext);
    3335              : 
    3336       506276 :     return true;
    3337              : }
    3338              : 
    3339              : /* gettuple_eval_partition
    3340              :  * get tuple in a partition and evaluate the window function's argument
    3341              :  * expression on it.
    3342              :  */
    3343              : static Datum
    3344       158360 : gettuple_eval_partition(WindowObject winobj, int argno,
    3345              :                         int64 abs_pos, bool *isnull, bool *isout)
    3346              : {
    3347              :     WindowAggState *winstate;
    3348              :     ExprContext *econtext;
    3349              :     TupleTableSlot *slot;
    3350              : 
    3351       158360 :     winstate = winobj->winstate;
    3352       158360 :     slot = winstate->temp_slot_1;
    3353       158360 :     if (!window_gettupleslot(winobj, abs_pos, slot))
    3354              :     {
    3355              :         /* out of partition */
    3356          388 :         if (isout)
    3357          388 :             *isout = true;
    3358          388 :         *isnull = true;
    3359          388 :         return (Datum) 0;
    3360              :     }
    3361              : 
    3362       157972 :     if (isout)
    3363       157972 :         *isout = false;
    3364       157972 :     econtext = winstate->ss.ps.ps_ExprContext;
    3365       157972 :     econtext->ecxt_outertuple = slot;
    3366       157972 :     return ExecEvalExpr((ExprState *) list_nth
    3367       157972 :                         (winobj->argstates, argno),
    3368              :                         econtext, isnull);
    3369              : }
    3370              : 
    3371              : /*
    3372              :  * ignorenulls_getfuncarginframe
    3373              :  * For IGNORE NULLS, get the next nonnull value in the frame, moving forward
    3374              :  * or backward until we find a value or reach the frame's end.
    3375              :  */
    3376              : static Datum
    3377          640 : ignorenulls_getfuncarginframe(WindowObject winobj, int argno,
    3378              :                               int relpos, int seektype, bool set_mark,
    3379              :                               bool *isnull, bool *isout)
    3380              : {
    3381              :     WindowAggState *winstate;
    3382              :     ExprContext *econtext;
    3383              :     TupleTableSlot *slot;
    3384              :     Datum       datum;
    3385              :     int64       abs_pos;
    3386              :     int64       mark_pos;
    3387              :     int         notnull_offset;
    3388              :     int         notnull_relpos;
    3389              :     int         forward;
    3390              : 
    3391              :     Assert(WindowObjectIsValid(winobj));
    3392          640 :     winstate = winobj->winstate;
    3393          640 :     econtext = winstate->ss.ps.ps_ExprContext;
    3394          640 :     slot = winstate->temp_slot_1;
    3395          640 :     datum = (Datum) 0;
    3396          640 :     notnull_offset = 0;
    3397          640 :     notnull_relpos = abs(relpos);
    3398              : 
    3399          640 :     switch (seektype)
    3400              :     {
    3401            0 :         case WINDOW_SEEK_CURRENT:
    3402            0 :             elog(ERROR, "WINDOW_SEEK_CURRENT is not supported for WinGetFuncArgInFrame");
    3403              :             abs_pos = mark_pos = 0; /* keep compiler quiet */
    3404              :             break;
    3405          440 :         case WINDOW_SEEK_HEAD:
    3406              :             /* rejecting relpos < 0 is easy and simplifies code below */
    3407          440 :             if (relpos < 0)
    3408            0 :                 goto out_of_frame;
    3409          440 :             update_frameheadpos(winstate);
    3410          440 :             abs_pos = winstate->frameheadpos;
    3411          440 :             mark_pos = winstate->frameheadpos;
    3412          440 :             forward = 1;
    3413          440 :             break;
    3414          200 :         case WINDOW_SEEK_TAIL:
    3415              :             /* rejecting relpos > 0 is easy and simplifies code below */
    3416          200 :             if (relpos > 0)
    3417            0 :                 goto out_of_frame;
    3418          200 :             update_frametailpos(winstate);
    3419          200 :             abs_pos = winstate->frametailpos - 1;
    3420          200 :             mark_pos = 0;       /* keep compiler quiet */
    3421          200 :             forward = -1;
    3422          200 :             break;
    3423            0 :         default:
    3424            0 :             elog(ERROR, "unrecognized window seek type: %d", seektype);
    3425              :             abs_pos = mark_pos = 0; /* keep compiler quiet */
    3426              :             break;
    3427              :     }
    3428              : 
    3429              :     /*
    3430              :      * Get the next nonnull value in the frame, moving forward or backward
    3431              :      * until we find a value or reach the frame's end.
    3432              :      */
    3433              :     do
    3434              :     {
    3435              :         int         inframe;
    3436              :         int         v;
    3437              : 
    3438              :         /*
    3439              :          * Check apparent out of frame case.  We need to do this because we
    3440              :          * may not call window_gettupleslot before row_is_in_frame, which
    3441              :          * supposes abs_pos is never negative.
    3442              :          */
    3443         1528 :         if (abs_pos < 0)
    3444            8 :             goto out_of_frame;
    3445              : 
    3446              :         /* check whether row is in frame */
    3447         1520 :         inframe = row_is_in_frame(winobj, abs_pos, slot, true);
    3448         1520 :         if (inframe == -1)
    3449           36 :             goto out_of_frame;
    3450         1484 :         else if (inframe == 0)
    3451           52 :             goto advance;
    3452              : 
    3453         1432 :         if (isout)
    3454            0 :             *isout = false;
    3455              : 
    3456         1432 :         v = get_notnull_info(winobj, abs_pos, argno);
    3457         1432 :         if (v == NN_NULL)       /* this row is known to be NULL */
    3458          400 :             goto advance;
    3459              : 
    3460         1032 :         else if (v == NN_UNKNOWN)   /* need to check NULL or not */
    3461              :         {
    3462          524 :             if (!window_gettupleslot(winobj, abs_pos, slot))
    3463           20 :                 goto out_of_frame;
    3464              : 
    3465          504 :             econtext->ecxt_outertuple = slot;
    3466          504 :             datum = ExecEvalExpr(
    3467          504 :                                  (ExprState *) list_nth(winobj->argstates,
    3468              :                                                         argno), econtext,
    3469              :                                  isnull);
    3470          504 :             if (!*isnull)
    3471          300 :                 notnull_offset++;
    3472              : 
    3473              :             /* record the row status */
    3474          504 :             put_notnull_info(winobj, abs_pos, argno, *isnull);
    3475              :         }
    3476              :         else                    /* this row is known to be NOT NULL */
    3477              :         {
    3478          508 :             notnull_offset++;
    3479          508 :             if (notnull_offset > notnull_relpos)
    3480              :             {
    3481              :                 /* to prepare exiting this loop, datum needs to be set */
    3482          320 :                 if (!window_gettupleslot(winobj, abs_pos, slot))
    3483            0 :                     goto out_of_frame;
    3484              : 
    3485          320 :                 econtext->ecxt_outertuple = slot;
    3486          320 :                 datum = ExecEvalExpr(
    3487          320 :                                      (ExprState *) list_nth
    3488          320 :                                      (winobj->argstates, argno),
    3489              :                                      econtext, isnull);
    3490              :             }
    3491              :         }
    3492          188 : advance:
    3493         1464 :         abs_pos += forward;
    3494         1464 :     } while (notnull_offset <= notnull_relpos);
    3495              : 
    3496          576 :     if (set_mark)
    3497          576 :         WinSetMarkPosition(winobj, mark_pos);
    3498              : 
    3499          576 :     return datum;
    3500              : 
    3501           64 : out_of_frame:
    3502           64 :     if (isout)
    3503            0 :         *isout = true;
    3504           64 :     *isnull = true;
    3505           64 :     return (Datum) 0;
    3506              : }
    3507              : 
    3508              : 
    3509              : /*
    3510              :  * init_notnull_info
    3511              :  * Initialize non null map.
    3512              :  */
    3513              : static void
    3514         1363 : init_notnull_info(WindowObject winobj, WindowStatePerFunc perfuncstate)
    3515              : {
    3516         1363 :     int         numargs = perfuncstate->numArguments;
    3517              : 
    3518         1363 :     if (winobj->ignore_nulls == PARSER_IGNORE_NULLS)
    3519              :     {
    3520          124 :         winobj->notnull_info = palloc0_array(uint8 *, numargs);
    3521          124 :         winobj->num_notnull_info = palloc0_array(int64, numargs);
    3522              :     }
    3523         1363 : }
    3524              : 
    3525              : /*
    3526              :  * grow_notnull_info
    3527              :  * expand notnull_info if necessary.
    3528              :  * pos: not null info position
    3529              :  * argno: argument number
    3530              : */
    3531              : static void
    3532         2632 : grow_notnull_info(WindowObject winobj, int64 pos, int argno)
    3533              : {
    3534              : /* initial number of notnull info members */
    3535              : #define INIT_NOT_NULL_INFO_NUM  128
    3536              : 
    3537         2632 :     if (pos >= winobj->num_notnull_info[argno])
    3538              :     {
    3539              :         /* We may be called in a short-lived context */
    3540          100 :         MemoryContext oldcontext = MemoryContextSwitchTo
    3541          100 :             (winobj->winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
    3542              : 
    3543              :         for (;;)
    3544            0 :         {
    3545          100 :             Size        oldsize = NN_POS_TO_BYTES
    3546              :                 (winobj->num_notnull_info[argno]);
    3547              :             Size        newsize;
    3548              : 
    3549          100 :             if (oldsize == 0)   /* memory has not been allocated yet for this
    3550              :                                  * arg */
    3551              :             {
    3552          100 :                 newsize = NN_POS_TO_BYTES(INIT_NOT_NULL_INFO_NUM);
    3553          100 :                 winobj->notnull_info[argno] = palloc0(newsize);
    3554              :             }
    3555              :             else
    3556              :             {
    3557            0 :                 newsize = oldsize * 2;
    3558            0 :                 winobj->notnull_info[argno] =
    3559            0 :                     repalloc0(winobj->notnull_info[argno], oldsize, newsize);
    3560              :             }
    3561          100 :             winobj->num_notnull_info[argno] = NN_BYTES_TO_POS(newsize);
    3562          100 :             if (winobj->num_notnull_info[argno] > pos)
    3563          100 :                 break;
    3564              :         }
    3565          100 :         MemoryContextSwitchTo(oldcontext);
    3566              :     }
    3567         2632 : }
    3568              : 
    3569              : /*
    3570              :  * get_notnull_info
    3571              :  * retrieve a map
    3572              :  * pos: map position
    3573              :  * argno: argument number
    3574              :  */
    3575              : static uint8
    3576         1848 : get_notnull_info(WindowObject winobj, int64 pos, int argno)
    3577              : {
    3578              :     uint8      *mbp;
    3579              :     uint8       mb;
    3580              :     int64       bpos;
    3581              : 
    3582         1848 :     grow_notnull_info(winobj, pos, argno);
    3583         1848 :     bpos = NN_POS_TO_BYTES(pos);
    3584         1848 :     mbp = winobj->notnull_info[argno];
    3585         1848 :     mb = mbp[bpos];
    3586         1848 :     return (mb >> (NN_SHIFT(pos))) & NN_MASK;
    3587              : }
    3588              : 
    3589              : /*
    3590              :  * put_notnull_info
    3591              :  * update map
    3592              :  * pos: map position
    3593              :  * argno: argument number
    3594              :  * isnull: indicate NULL or NOT
    3595              :  */
    3596              : static void
    3597          784 : put_notnull_info(WindowObject winobj, int64 pos, int argno, bool isnull)
    3598              : {
    3599              :     uint8      *mbp;
    3600              :     uint8       mb;
    3601              :     int64       bpos;
    3602          784 :     uint8       val = isnull ? NN_NULL : NN_NOTNULL;
    3603              :     int         shift;
    3604              : 
    3605          784 :     grow_notnull_info(winobj, pos, argno);
    3606          784 :     bpos = NN_POS_TO_BYTES(pos);
    3607          784 :     mbp = winobj->notnull_info[argno];
    3608          784 :     mb = mbp[bpos];
    3609          784 :     shift = NN_SHIFT(pos);
    3610          784 :     mb &= ~(NN_MASK << shift);    /* clear map */
    3611          784 :     mb |= (val << shift);     /* update map */
    3612          784 :     mbp[bpos] = mb;
    3613          784 : }
    3614              : 
    3615              : /***********************************************************************
    3616              :  * API exposed to window functions
    3617              :  ***********************************************************************/
    3618              : 
    3619              : 
    3620              : /*
    3621              :  * WinCheckAndInitializeNullTreatment
    3622              :  *      Check null treatment clause and sets ignore_nulls
    3623              :  *
    3624              :  * Window functions should call this to check if they are being called with
    3625              :  * a null treatment clause when they don't allow it, or to set ignore_nulls.
    3626              :  */
    3627              : void
    3628       580655 : WinCheckAndInitializeNullTreatment(WindowObject winobj,
    3629              :                                    bool allowNullTreatment,
    3630              :                                    FunctionCallInfo fcinfo)
    3631              : {
    3632              :     Assert(WindowObjectIsValid(winobj));
    3633       580655 :     if (winobj->ignore_nulls != NO_NULLTREATMENT && !allowNullTreatment)
    3634              :     {
    3635           48 :         const char *funcname = get_func_name(fcinfo->flinfo->fn_oid);
    3636              : 
    3637           48 :         if (!funcname)
    3638            0 :             elog(ERROR, "could not get function name");
    3639           48 :         ereport(ERROR,
    3640              :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3641              :                  errmsg("function %s does not allow RESPECT/IGNORE NULLS",
    3642              :                         funcname)));
    3643              :     }
    3644       580607 :     else if (winobj->ignore_nulls == PARSER_IGNORE_NULLS)
    3645          100 :         winobj->ignore_nulls = IGNORE_NULLS;
    3646       580607 : }
    3647              : 
    3648              : /*
    3649              :  * WinGetPartitionLocalMemory
    3650              :  *      Get working memory that lives till end of partition processing
    3651              :  *
    3652              :  * On first call within a given partition, this allocates and zeroes the
    3653              :  * requested amount of space.  Subsequent calls just return the same chunk.
    3654              :  *
    3655              :  * Memory obtained this way is normally used to hold state that should be
    3656              :  * automatically reset for each new partition.  If a window function wants
    3657              :  * to hold state across the whole query, fcinfo->fn_extra can be used in the
    3658              :  * usual way for that.
    3659              :  */
    3660              : void *
    3661       221280 : WinGetPartitionLocalMemory(WindowObject winobj, Size sz)
    3662              : {
    3663              :     Assert(WindowObjectIsValid(winobj));
    3664       221280 :     if (winobj->localmem == NULL)
    3665          295 :         winobj->localmem =
    3666          295 :             MemoryContextAllocZero(winobj->winstate->partcontext, sz);
    3667       221280 :     return winobj->localmem;
    3668              : }
    3669              : 
    3670              : /*
    3671              :  * WinGetCurrentPosition
    3672              :  *      Return the current row's position (counting from 0) within the current
    3673              :  *      partition.
    3674              :  */
    3675              : int64
    3676       501546 : WinGetCurrentPosition(WindowObject winobj)
    3677              : {
    3678              :     Assert(WindowObjectIsValid(winobj));
    3679       501546 :     return winobj->winstate->currentpos;
    3680              : }
    3681              : 
    3682              : /*
    3683              :  * WinGetPartitionRowCount
    3684              :  *      Return total number of rows contained in the current partition.
    3685              :  *
    3686              :  * Note: this is a relatively expensive operation because it forces the
    3687              :  * whole partition to be "spooled" into the tuplestore at once.  Once
    3688              :  * executed, however, additional calls within the same partition are cheap.
    3689              :  */
    3690              : int64
    3691          208 : WinGetPartitionRowCount(WindowObject winobj)
    3692              : {
    3693              :     Assert(WindowObjectIsValid(winobj));
    3694          208 :     spool_tuples(winobj->winstate, -1);
    3695          208 :     return winobj->winstate->spooled_rows;
    3696              : }
    3697              : 
    3698              : /*
    3699              :  * WinSetMarkPosition
    3700              :  *      Set the "mark" position for the window object, which is the oldest row
    3701              :  *      number (counting from 0) it is allowed to fetch during all subsequent
    3702              :  *      operations within the current partition.
    3703              :  *
    3704              :  * Window functions do not have to call this, but are encouraged to move the
    3705              :  * mark forward when possible to keep the tuplestore size down and prevent
    3706              :  * having to spill rows to disk.
    3707              :  */
    3708              : void
    3709       583689 : WinSetMarkPosition(WindowObject winobj, int64 markpos)
    3710              : {
    3711              :     WindowAggState *winstate;
    3712              : 
    3713              :     Assert(WindowObjectIsValid(winobj));
    3714       583689 :     winstate = winobj->winstate;
    3715              : 
    3716       583689 :     if (markpos < winobj->markpos)
    3717            0 :         elog(ERROR, "cannot move WindowObject's mark position backward");
    3718       583689 :     tuplestore_select_read_pointer(winstate->buffer, winobj->markptr);
    3719       583689 :     if (markpos > winobj->markpos)
    3720              :     {
    3721       579381 :         tuplestore_skiptuples(winstate->buffer,
    3722       579381 :                               markpos - winobj->markpos,
    3723              :                               true);
    3724       579381 :         winobj->markpos = markpos;
    3725              :     }
    3726       583689 :     tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
    3727       583689 :     if (markpos > winobj->seekpos)
    3728              :     {
    3729       308334 :         tuplestore_skiptuples(winstate->buffer,
    3730       308334 :                               markpos - winobj->seekpos,
    3731              :                               true);
    3732       308334 :         winobj->seekpos = markpos;
    3733              :     }
    3734       583689 : }
    3735              : 
    3736              : /*
    3737              :  * WinRowsArePeers
    3738              :  *      Compare two rows (specified by absolute position in partition) to see
    3739              :  *      if they are equal according to the ORDER BY clause.
    3740              :  *
    3741              :  * NB: this does not consider the window frame mode.
    3742              :  */
    3743              : bool
    3744       110375 : WinRowsArePeers(WindowObject winobj, int64 pos1, int64 pos2)
    3745              : {
    3746              :     WindowAggState *winstate;
    3747              :     WindowAgg  *node;
    3748              :     TupleTableSlot *slot1;
    3749              :     TupleTableSlot *slot2;
    3750              :     bool        res;
    3751              : 
    3752              :     Assert(WindowObjectIsValid(winobj));
    3753       110375 :     winstate = winobj->winstate;
    3754       110375 :     node = (WindowAgg *) winstate->ss.ps.plan;
    3755              : 
    3756              :     /* If no ORDER BY, all rows are peers; don't bother to fetch them */
    3757       110375 :     if (node->ordNumCols == 0)
    3758          180 :         return true;
    3759              : 
    3760              :     /*
    3761              :      * Note: OK to use temp_slot_2 here because we aren't calling any
    3762              :      * frame-related functions (those tend to clobber temp_slot_2).
    3763              :      */
    3764       110195 :     slot1 = winstate->temp_slot_1;
    3765       110195 :     slot2 = winstate->temp_slot_2;
    3766              : 
    3767       110195 :     if (!window_gettupleslot(winobj, pos1, slot1))
    3768            0 :         elog(ERROR, "specified position is out of window: " INT64_FORMAT,
    3769              :              pos1);
    3770       110195 :     if (!window_gettupleslot(winobj, pos2, slot2))
    3771            0 :         elog(ERROR, "specified position is out of window: " INT64_FORMAT,
    3772              :              pos2);
    3773              : 
    3774       110195 :     res = are_peers(winstate, slot1, slot2);
    3775              : 
    3776       110195 :     ExecClearTuple(slot1);
    3777       110195 :     ExecClearTuple(slot2);
    3778              : 
    3779       110195 :     return res;
    3780              : }
    3781              : 
    3782              : /*
    3783              :  * WinGetFuncArgInPartition
    3784              :  *      Evaluate a window function's argument expression on a specified
    3785              :  *      row of the partition.  The row is identified in lseek(2) style,
    3786              :  *      i.e. relative to the current, first, or last row.
    3787              :  *
    3788              :  * argno: argument number to evaluate (counted from 0)
    3789              :  * relpos: signed rowcount offset from the seek position
    3790              :  * seektype: WINDOW_SEEK_CURRENT, WINDOW_SEEK_HEAD, or WINDOW_SEEK_TAIL
    3791              :  * set_mark: If the row is found and set_mark is true, the mark is moved to
    3792              :  *      the row as a side-effect.
    3793              :  * isnull: output argument, receives isnull status of result
    3794              :  * isout: output argument, set to indicate whether target row position
    3795              :  *      is out of partition (can pass NULL if caller doesn't care about this)
    3796              :  *
    3797              :  * Specifying a nonexistent row is not an error, it just causes a null result
    3798              :  * (plus setting *isout true, if isout isn't NULL).
    3799              :  */
    3800              : Datum
    3801       158040 : WinGetFuncArgInPartition(WindowObject winobj, int argno,
    3802              :                          int relpos, int seektype, bool set_mark,
    3803              :                          bool *isnull, bool *isout)
    3804              : {
    3805              :     WindowAggState *winstate;
    3806              :     int64       abs_pos;
    3807              :     int64       mark_pos;
    3808              :     Datum       datum;
    3809              :     bool        null_treatment;
    3810              :     int         notnull_offset;
    3811              :     int         notnull_relpos;
    3812              :     int         forward;
    3813              :     bool        myisout;
    3814              : 
    3815              :     Assert(WindowObjectIsValid(winobj));
    3816       158040 :     winstate = winobj->winstate;
    3817              : 
    3818       158040 :     null_treatment = (winobj->ignore_nulls == IGNORE_NULLS && relpos != 0);
    3819              : 
    3820       158040 :     switch (seektype)
    3821              :     {
    3822       158040 :         case WINDOW_SEEK_CURRENT:
    3823       158040 :             if (null_treatment)
    3824          320 :                 abs_pos = winstate->currentpos;
    3825              :             else
    3826       157720 :                 abs_pos = winstate->currentpos + relpos;
    3827       158040 :             break;
    3828            0 :         case WINDOW_SEEK_HEAD:
    3829            0 :             if (null_treatment)
    3830            0 :                 abs_pos = 0;
    3831              :             else
    3832            0 :                 abs_pos = relpos;
    3833            0 :             break;
    3834            0 :         case WINDOW_SEEK_TAIL:
    3835            0 :             spool_tuples(winstate, -1);
    3836            0 :             abs_pos = winstate->spooled_rows - 1 + relpos;
    3837            0 :             break;
    3838            0 :         default:
    3839            0 :             elog(ERROR, "unrecognized window seek type: %d", seektype);
    3840              :             abs_pos = 0;        /* keep compiler quiet */
    3841              :             break;
    3842              :     }
    3843              : 
    3844              :     /* Easy case if IGNORE NULLS is not specified */
    3845       158040 :     if (!null_treatment)
    3846              :     {
    3847              :         /* get tuple and evaluate in partition */
    3848       157720 :         datum = gettuple_eval_partition(winobj, argno,
    3849              :                                         abs_pos, isnull, &myisout);
    3850       157720 :         if (!myisout && set_mark)
    3851       157360 :             WinSetMarkPosition(winobj, abs_pos);
    3852       157720 :         if (isout)
    3853       157720 :             *isout = myisout;
    3854       157720 :         return datum;
    3855              :     }
    3856              : 
    3857              :     /* Prepare for loop */
    3858          320 :     notnull_offset = 0;
    3859          320 :     notnull_relpos = abs(relpos);
    3860          320 :     forward = relpos > 0 ? 1 : -1;
    3861          320 :     myisout = false;
    3862          320 :     datum = 0;
    3863              : 
    3864              :     /*
    3865              :      * IGNORE NULLS + WINDOW_SEEK_CURRENT + relpos > 0 case, we would fetch
    3866              :      * beyond the current row + relpos to find out the target row. If we mark
    3867              :      * at abs_pos, next call to WinGetFuncArgInPartition or
    3868              :      * WinGetFuncArgInFrame (in case when a window function have multiple
    3869              :      * args) could fail with "cannot fetch row before WindowObject's mark
    3870              :      * position". So keep the mark position at currentpos.
    3871              :      */
    3872          320 :     if (seektype == WINDOW_SEEK_CURRENT && relpos > 0)
    3873          160 :         mark_pos = winstate->currentpos;
    3874              :     else
    3875              :     {
    3876              :         /*
    3877              :          * For other cases we have no idea what position of row callers would
    3878              :          * fetch next time. Also for relpos < 0 case (we go backward), we
    3879              :          * cannot set mark either. For those cases we always set mark at 0.
    3880              :          */
    3881          160 :         mark_pos = 0;
    3882              :     }
    3883              : 
    3884              :     /*
    3885              :      * Get the next nonnull value in the partition, moving forward or backward
    3886              :      * until we find a value or reach the partition's end.  We cache the
    3887              :      * nullness status because we may repeat this process many times.
    3888              :      */
    3889              :     do
    3890              :     {
    3891              :         int         nn_info;    /* NOT NULL status */
    3892              : 
    3893          468 :         abs_pos += forward;
    3894          468 :         if (abs_pos < 0)     /* clearly out of partition */
    3895           52 :             break;
    3896              : 
    3897              :         /* check NOT NULL cached info */
    3898          416 :         nn_info = get_notnull_info(winobj, abs_pos, argno);
    3899          416 :         if (nn_info == NN_NOTNULL)  /* this row is known to be NOT NULL */
    3900           60 :             notnull_offset++;
    3901          356 :         else if (nn_info == NN_NULL)    /* this row is known to be NULL */
    3902           36 :             continue;           /* keep on moving forward or backward */
    3903              :         else                    /* need to check NULL or not */
    3904              :         {
    3905              :             /*
    3906              :              * NOT NULL info does not exist yet.  Get tuple and evaluate func
    3907              :              * arg in partition. We ignore the return value from
    3908              :              * gettuple_eval_partition because we are just interested in
    3909              :              * whether we are inside or outside of partition, NULL or NOT
    3910              :              * NULL.
    3911              :              */
    3912          320 :             (void) gettuple_eval_partition(winobj, argno,
    3913              :                                            abs_pos, isnull, &myisout);
    3914          320 :             if (myisout)        /* out of partition? */
    3915           40 :                 break;
    3916          280 :             if (!*isnull)
    3917          168 :                 notnull_offset++;
    3918              :             /* record the row status */
    3919          280 :             put_notnull_info(winobj, abs_pos, argno, *isnull);
    3920              :         }
    3921          376 :     } while (notnull_offset < notnull_relpos);
    3922              : 
    3923              :     /* get tuple and evaluate func arg in partition */
    3924          320 :     datum = gettuple_eval_partition(winobj, argno,
    3925              :                                     abs_pos, isnull, &myisout);
    3926          320 :     if (!myisout && set_mark)
    3927          228 :         WinSetMarkPosition(winobj, mark_pos);
    3928          320 :     if (isout)
    3929          320 :         *isout = myisout;
    3930              : 
    3931          320 :     return datum;
    3932              : }
    3933              : 
    3934              : /*
    3935              :  * WinGetFuncArgInFrame
    3936              :  *      Evaluate a window function's argument expression on a specified
    3937              :  *      row of the window frame.  The row is identified in lseek(2) style,
    3938              :  *      i.e. relative to the first or last row of the frame.  (We do not
    3939              :  *      support WINDOW_SEEK_CURRENT here, because it's not very clear what
    3940              :  *      that should mean if the current row isn't part of the frame.)
    3941              :  *
    3942              :  * argno: argument number to evaluate (counted from 0)
    3943              :  * relpos: signed rowcount offset from the seek position
    3944              :  * seektype: WINDOW_SEEK_HEAD or WINDOW_SEEK_TAIL
    3945              :  * set_mark: If the row is found/in frame and set_mark is true, the mark is
    3946              :  *      moved to the row as a side-effect.
    3947              :  * isnull: output argument, receives isnull status of result
    3948              :  * isout: output argument, set to indicate whether target row position
    3949              :  *      is out of frame (can pass NULL if caller doesn't care about this)
    3950              :  *
    3951              :  * Specifying a nonexistent or not-in-frame row is not an error, it just
    3952              :  * causes a null result (plus setting *isout true, if isout isn't NULL).
    3953              :  *
    3954              :  * Note that some exclusion-clause options lead to situations where the
    3955              :  * rows that are in-frame are not consecutive in the partition.  But we
    3956              :  * count only in-frame rows when measuring relpos.
    3957              :  *
    3958              :  * The set_mark flag is interpreted as meaning that the caller will specify
    3959              :  * a constant (or, perhaps, monotonically increasing) relpos in successive
    3960              :  * calls, so that *if there is no exclusion clause* there will be no need
    3961              :  * to fetch a row before the previously fetched row.  But we do not expect
    3962              :  * the caller to know how to account for exclusion clauses.  Therefore,
    3963              :  * if there is an exclusion clause we take responsibility for adjusting the
    3964              :  * mark request to something that will be safe given the above assumption
    3965              :  * about relpos.
    3966              :  */
    3967              : Datum
    3968         6672 : WinGetFuncArgInFrame(WindowObject winobj, int argno,
    3969              :                      int relpos, int seektype, bool set_mark,
    3970              :                      bool *isnull, bool *isout)
    3971              : {
    3972              :     WindowAggState *winstate;
    3973              :     ExprContext *econtext;
    3974              :     TupleTableSlot *slot;
    3975              :     int64       abs_pos;
    3976              :     int64       mark_pos;
    3977              : 
    3978              :     Assert(WindowObjectIsValid(winobj));
    3979         6672 :     winstate = winobj->winstate;
    3980         6672 :     econtext = winstate->ss.ps.ps_ExprContext;
    3981         6672 :     slot = winstate->temp_slot_1;
    3982              : 
    3983         6672 :     if (winobj->ignore_nulls == IGNORE_NULLS)
    3984          640 :         return ignorenulls_getfuncarginframe(winobj, argno, relpos, seektype,
    3985              :                                              set_mark, isnull, isout);
    3986              : 
    3987         6032 :     switch (seektype)
    3988              :     {
    3989            0 :         case WINDOW_SEEK_CURRENT:
    3990            0 :             elog(ERROR, "WINDOW_SEEK_CURRENT is not supported for WinGetFuncArgInFrame");
    3991              :             abs_pos = mark_pos = 0; /* keep compiler quiet */
    3992              :             break;
    3993         2964 :         case WINDOW_SEEK_HEAD:
    3994              :             /* rejecting relpos < 0 is easy and simplifies code below */
    3995         2964 :             if (relpos < 0)
    3996            0 :                 goto out_of_frame;
    3997         2964 :             update_frameheadpos(winstate);
    3998         2936 :             abs_pos = winstate->frameheadpos + relpos;
    3999         2936 :             mark_pos = abs_pos;
    4000              : 
    4001              :             /*
    4002              :              * Account for exclusion option if one is active, but advance only
    4003              :              * abs_pos not mark_pos.  This prevents changes of the current
    4004              :              * row's peer group from resulting in trying to fetch a row before
    4005              :              * some previous mark position.
    4006              :              *
    4007              :              * Note that in some corner cases such as current row being
    4008              :              * outside frame, these calculations are theoretically too simple,
    4009              :              * but it doesn't matter because we'll end up deciding the row is
    4010              :              * out of frame.  We do not attempt to avoid fetching rows past
    4011              :              * end of frame; that would happen in some cases anyway.
    4012              :              */
    4013         2936 :             switch (winstate->frameOptions & FRAMEOPTION_EXCLUSION)
    4014              :             {
    4015         2496 :                 case 0:
    4016              :                     /* no adjustment needed */
    4017         2496 :                     break;
    4018          160 :                 case FRAMEOPTION_EXCLUDE_CURRENT_ROW:
    4019          160 :                     if (abs_pos >= winstate->currentpos &&
    4020          124 :                         winstate->currentpos >= winstate->frameheadpos)
    4021           44 :                         abs_pos++;
    4022          160 :                     break;
    4023           80 :                 case FRAMEOPTION_EXCLUDE_GROUP:
    4024           80 :                     update_grouptailpos(winstate);
    4025           80 :                     if (abs_pos >= winstate->groupheadpos &&
    4026           48 :                         winstate->grouptailpos > winstate->frameheadpos)
    4027              :                     {
    4028           48 :                         int64       overlapstart = Max(winstate->groupheadpos,
    4029              :                                                        winstate->frameheadpos);
    4030              : 
    4031           48 :                         abs_pos += winstate->grouptailpos - overlapstart;
    4032              :                     }
    4033           80 :                     break;
    4034          200 :                 case FRAMEOPTION_EXCLUDE_TIES:
    4035          200 :                     update_grouptailpos(winstate);
    4036          200 :                     if (abs_pos >= winstate->groupheadpos &&
    4037          136 :                         winstate->grouptailpos > winstate->frameheadpos)
    4038              :                     {
    4039           56 :                         int64       overlapstart = Max(winstate->groupheadpos,
    4040              :                                                        winstate->frameheadpos);
    4041              : 
    4042           56 :                         if (abs_pos == overlapstart)
    4043           56 :                             abs_pos = winstate->currentpos;
    4044              :                         else
    4045            0 :                             abs_pos += winstate->grouptailpos - overlapstart - 1;
    4046              :                     }
    4047          200 :                     break;
    4048            0 :                 default:
    4049            0 :                     elog(ERROR, "unrecognized frame option state: 0x%x",
    4050              :                          winstate->frameOptions);
    4051              :                     break;
    4052              :             }
    4053         2936 :             break;
    4054         3068 :         case WINDOW_SEEK_TAIL:
    4055              :             /* rejecting relpos > 0 is easy and simplifies code below */
    4056         3068 :             if (relpos > 0)
    4057            0 :                 goto out_of_frame;
    4058         3068 :             update_frametailpos(winstate);
    4059         3064 :             abs_pos = winstate->frametailpos - 1 + relpos;
    4060              : 
    4061              :             /*
    4062              :              * Account for exclusion option if one is active.  If there is no
    4063              :              * exclusion, we can safely set the mark at the accessed row.  But
    4064              :              * if there is, we can only mark the frame start, because we can't
    4065              :              * be sure how far back in the frame the exclusion might cause us
    4066              :              * to fetch in future.  Furthermore, we have to actually check
    4067              :              * against frameheadpos here, since it's unsafe to try to fetch a
    4068              :              * row before frame start if the mark might be there already.
    4069              :              */
    4070         3064 :             switch (winstate->frameOptions & FRAMEOPTION_EXCLUSION)
    4071              :             {
    4072         2704 :                 case 0:
    4073              :                     /* no adjustment needed */
    4074         2704 :                     mark_pos = abs_pos;
    4075         2704 :                     break;
    4076          120 :                 case FRAMEOPTION_EXCLUDE_CURRENT_ROW:
    4077          120 :                     if (abs_pos <= winstate->currentpos &&
    4078           12 :                         winstate->currentpos < winstate->frametailpos)
    4079           12 :                         abs_pos--;
    4080          120 :                     update_frameheadpos(winstate);
    4081          120 :                     if (abs_pos < winstate->frameheadpos)
    4082            8 :                         goto out_of_frame;
    4083          112 :                     mark_pos = winstate->frameheadpos;
    4084          112 :                     break;
    4085          160 :                 case FRAMEOPTION_EXCLUDE_GROUP:
    4086          160 :                     update_grouptailpos(winstate);
    4087          160 :                     if (abs_pos < winstate->grouptailpos &&
    4088           36 :                         winstate->groupheadpos < winstate->frametailpos)
    4089              :                     {
    4090           36 :                         int64       overlapend = Min(winstate->grouptailpos,
    4091              :                                                      winstate->frametailpos);
    4092              : 
    4093           36 :                         abs_pos -= overlapend - winstate->groupheadpos;
    4094              :                     }
    4095          160 :                     update_frameheadpos(winstate);
    4096          160 :                     if (abs_pos < winstate->frameheadpos)
    4097           36 :                         goto out_of_frame;
    4098          124 :                     mark_pos = winstate->frameheadpos;
    4099          124 :                     break;
    4100           80 :                 case FRAMEOPTION_EXCLUDE_TIES:
    4101           80 :                     update_grouptailpos(winstate);
    4102           80 :                     if (abs_pos < winstate->grouptailpos &&
    4103           24 :                         winstate->groupheadpos < winstate->frametailpos)
    4104              :                     {
    4105           24 :                         int64       overlapend = Min(winstate->grouptailpos,
    4106              :                                                      winstate->frametailpos);
    4107              : 
    4108           24 :                         if (abs_pos == overlapend - 1)
    4109           24 :                             abs_pos = winstate->currentpos;
    4110              :                         else
    4111            0 :                             abs_pos -= overlapend - 1 - winstate->groupheadpos;
    4112              :                     }
    4113           80 :                     update_frameheadpos(winstate);
    4114           80 :                     if (abs_pos < winstate->frameheadpos)
    4115            0 :                         goto out_of_frame;
    4116           80 :                     mark_pos = winstate->frameheadpos;
    4117           80 :                     break;
    4118            0 :                 default:
    4119            0 :                     elog(ERROR, "unrecognized frame option state: 0x%x",
    4120              :                          winstate->frameOptions);
    4121              :                     mark_pos = 0;   /* keep compiler quiet */
    4122              :                     break;
    4123              :             }
    4124         3020 :             break;
    4125            0 :         default:
    4126            0 :             elog(ERROR, "unrecognized window seek type: %d", seektype);
    4127              :             abs_pos = mark_pos = 0; /* keep compiler quiet */
    4128              :             break;
    4129              :     }
    4130              : 
    4131         5956 :     if (!window_gettupleslot(winobj, abs_pos, slot))
    4132          264 :         goto out_of_frame;
    4133              : 
    4134              :     /* The code above does not detect all out-of-frame cases, so check */
    4135         5692 :     if (row_is_in_frame(winobj, abs_pos, slot, false) <= 0)
    4136          200 :         goto out_of_frame;
    4137              : 
    4138         5472 :     if (isout)
    4139            0 :         *isout = false;
    4140         5472 :     if (set_mark)
    4141         5444 :         WinSetMarkPosition(winobj, mark_pos);
    4142         5472 :     econtext->ecxt_outertuple = slot;
    4143         5472 :     return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
    4144              :                         econtext, isnull);
    4145              : 
    4146          508 : out_of_frame:
    4147          508 :     if (isout)
    4148            0 :         *isout = true;
    4149          508 :     *isnull = true;
    4150          508 :     return (Datum) 0;
    4151              : }
    4152              : 
    4153              : /*
    4154              :  * WinGetFuncArgCurrent
    4155              :  *      Evaluate a window function's argument expression on the current row.
    4156              :  *
    4157              :  * argno: argument number to evaluate (counted from 0)
    4158              :  * isnull: output argument, receives isnull status of result
    4159              :  *
    4160              :  * Note: this isn't quite equivalent to WinGetFuncArgInPartition or
    4161              :  * WinGetFuncArgInFrame targeting the current row, because it will succeed
    4162              :  * even if the WindowObject's mark has been set beyond the current row.
    4163              :  * This should generally be used for "ordinary" arguments of a window
    4164              :  * function, such as the offset argument of lead() or lag().
    4165              :  */
    4166              : Datum
    4167         1340 : WinGetFuncArgCurrent(WindowObject winobj, int argno, bool *isnull)
    4168              : {
    4169              :     WindowAggState *winstate;
    4170              :     ExprContext *econtext;
    4171              : 
    4172              :     Assert(WindowObjectIsValid(winobj));
    4173         1340 :     winstate = winobj->winstate;
    4174              : 
    4175         1340 :     econtext = winstate->ss.ps.ps_ExprContext;
    4176              : 
    4177         1340 :     econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
    4178         1340 :     return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
    4179              :                         econtext, isnull);
    4180              : }
        

Generated by: LCOV version 2.0-1