Debian Codegraph @

src/backend/executor/nodeWindowAgg.c

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