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