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