1 /*-------------------------------------------------------------------------
4 * routines to handle WindowAgg nodes.
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.)
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.
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.
26 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
27 * Portions Copyright (c) 1994, Regents of the University of California
30 * $PostgreSQL: pgsql/src/backend/executor/nodeWindowAgg.c,v 1.2 2008/12/31 00:08:35 tgl Exp $
32 *-------------------------------------------------------------------------
36 #include "catalog/pg_aggregate.h"
37 #include "catalog/pg_proc.h"
38 #include "catalog/pg_type.h"
39 #include "executor/executor.h"
40 #include "executor/nodeWindowAgg.h"
41 #include "miscadmin.h"
42 #include "nodes/nodeFuncs.h"
43 #include "optimizer/clauses.h"
44 #include "parser/parse_agg.h"
45 #include "parser/parse_coerce.h"
46 #include "utils/acl.h"
47 #include "utils/builtins.h"
48 #include "utils/datum.h"
49 #include "utils/lsyscache.h"
50 #include "utils/memutils.h"
51 #include "utils/syscache.h"
52 #include "windowapi.h"
55 * All the window function APIs are called with this object, which is passed
56 * to window functions as fcinfo->context.
58 typedef struct WindowObjectData
61 WindowAggState *winstate; /* parent WindowAggState */
62 List *argstates; /* ExprState trees for fn's arguments */
63 void *localmem; /* WinGetPartitionLocalMemory's chunk */
64 int markptr; /* tuplestore mark pointer for this fn */
65 int readptr; /* tuplestore read pointer for this fn */
66 int64 markpos; /* row that markptr is positioned on */
67 int64 seekpos; /* row that readptr is positioned on */
71 * We have one WindowStatePerFunc struct for each window function and
72 * window aggregate handled by this node.
74 typedef struct WindowStatePerFuncData
76 /* Links to WindowFunc expr and state nodes this working state is for */
77 WindowFuncExprState *wfuncstate;
80 int numArguments; /* number of arguments */
82 FmgrInfo flinfo; /* fmgr lookup data for window function */
85 * We need the len and byval info for the result of each function
86 * in order to know how to copy/delete values.
91 bool plain_agg; /* is it just a plain aggregate function? */
92 int aggno; /* if so, index of its PerAggData */
94 WindowObject winobj; /* object used in window function API */
95 } WindowStatePerFuncData;
98 * For plain aggregate window functions, we also have one of these.
100 typedef struct WindowStatePerAggData
102 /* Oids of transfer functions */
104 Oid finalfn_oid; /* may be InvalidOid */
107 * fmgr lookup data for transfer functions --- only valid when
108 * corresponding oid is not InvalidOid. Note in particular that fn_strict
109 * flags are kept here.
115 * initial value from pg_aggregate entry
118 bool initValueIsNull;
121 * cached value for current frame boundaries
124 bool resultValueIsNull;
127 * We need the len and byval info for the agg's input, result, and
128 * transition data types in order to know how to copy/delete values.
137 int wfuncno; /* index of associated PerFuncData */
139 /* Current transition value */
140 Datum transValue; /* current transition value */
141 bool transValueIsNull;
143 bool noTransValue; /* true if transValue not set yet */
144 } WindowStatePerAggData;
146 static void initialize_windowaggregate(WindowAggState *winstate,
147 WindowStatePerFunc perfuncstate,
148 WindowStatePerAgg peraggstate);
149 static void advance_windowaggregate(WindowAggState *winstate,
150 WindowStatePerFunc perfuncstate,
151 WindowStatePerAgg peraggstate);
152 static void finalize_windowaggregate(WindowAggState *winstate,
153 WindowStatePerFunc perfuncstate,
154 WindowStatePerAgg peraggstate,
155 Datum *result, bool *isnull);
157 static void eval_windowaggregates(WindowAggState *winstate);
158 static void eval_windowfunction(WindowAggState *winstate,
159 WindowStatePerFunc perfuncstate,
160 Datum *result, bool *isnull);
162 static void begin_partition(WindowAggState *winstate);
163 static void spool_tuples(WindowAggState *winstate, int64 pos);
164 static void release_partition(WindowAggState *winstate);
166 static bool row_is_in_frame(WindowAggState *winstate, int64 pos,
167 TupleTableSlot *slot);
168 static void update_frametailpos(WindowObject winobj, TupleTableSlot *slot);
170 static WindowStatePerAggData *initialize_peragg(WindowAggState *winstate,
172 WindowStatePerAgg peraggstate);
173 static Datum GetAggInitVal(Datum textInitVal, Oid transtype);
175 static bool are_peers(WindowAggState *winstate, TupleTableSlot *slot1,
176 TupleTableSlot *slot2);
177 static bool window_gettupleslot(WindowObject winobj, int64 pos,
178 TupleTableSlot *slot);
182 * initialize_windowaggregate
183 * parallel to initialize_aggregate in nodeAgg.c
186 initialize_windowaggregate(WindowAggState *winstate,
187 WindowStatePerFunc perfuncstate,
188 WindowStatePerAgg peraggstate)
190 MemoryContext oldContext;
192 if (peraggstate->initValueIsNull)
193 peraggstate->transValue = peraggstate->initValue;
196 oldContext = MemoryContextSwitchTo(winstate->wincontext);
197 peraggstate->transValue = datumCopy(peraggstate->initValue,
198 peraggstate->transtypeByVal,
199 peraggstate->transtypeLen);
200 MemoryContextSwitchTo(oldContext);
202 peraggstate->transValueIsNull = peraggstate->initValueIsNull;
203 peraggstate->noTransValue = peraggstate->initValueIsNull;
204 peraggstate->resultValueIsNull = true;
208 * advance_windowaggregate
209 * parallel to advance_aggregate in nodeAgg.c
212 advance_windowaggregate(WindowAggState *winstate,
213 WindowStatePerFunc perfuncstate,
214 WindowStatePerAgg peraggstate)
216 WindowFuncExprState *wfuncstate = perfuncstate->wfuncstate;
217 int numArguments = perfuncstate->numArguments;
218 FunctionCallInfoData fcinfodata;
219 FunctionCallInfo fcinfo = &fcinfodata;
223 MemoryContext oldContext;
224 ExprContext *econtext = winstate->tmpcontext;
226 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
228 /* We start from 1, since the 0th arg will be the transition value */
230 foreach(arg, wfuncstate->args)
232 ExprState *argstate = (ExprState *) lfirst(arg);
234 fcinfo->arg[i] = ExecEvalExpr(argstate, econtext,
235 &fcinfo->argnull[i], NULL);
239 if (peraggstate->transfn.fn_strict)
242 * For a strict transfn, nothing happens when there's a NULL input; we
243 * just keep the prior transValue.
245 for (i = 1; i <= numArguments; i++)
247 if (fcinfo->argnull[i])
249 MemoryContextSwitchTo(oldContext);
253 if (peraggstate->noTransValue)
256 * transValue has not been initialized. This is the first non-NULL
257 * input value. We use it as the initial value for transValue. (We
258 * already checked that the agg's input type is binary-compatible
259 * with its transtype, so straight copy here is OK.)
261 * We must copy the datum into wincontext if it is pass-by-ref. We
262 * do not need to pfree the old transValue, since it's NULL.
264 MemoryContextSwitchTo(winstate->wincontext);
265 peraggstate->transValue = datumCopy(fcinfo->arg[1],
266 peraggstate->transtypeByVal,
267 peraggstate->transtypeLen);
268 peraggstate->transValueIsNull = false;
269 peraggstate->noTransValue = false;
270 MemoryContextSwitchTo(oldContext);
273 if (peraggstate->transValueIsNull)
276 * Don't call a strict function with NULL inputs. Note it is
277 * possible to get here despite the above tests, if the transfn is
278 * strict *and* returned a NULL on a prior cycle. If that happens
279 * we will propagate the NULL all the way to the end.
281 MemoryContextSwitchTo(oldContext);
287 * OK to call the transition function
289 InitFunctionCallInfoData(*fcinfo, &(peraggstate->transfn),
291 (void *) winstate, NULL);
292 fcinfo->arg[0] = peraggstate->transValue;
293 fcinfo->argnull[0] = peraggstate->transValueIsNull;
294 newVal = FunctionCallInvoke(fcinfo);
297 * If pass-by-ref datatype, must copy the new value into wincontext and
298 * pfree the prior transValue. But if transfn returned a pointer to its
299 * first input, we don't need to do anything.
301 if (!peraggstate->transtypeByVal &&
302 DatumGetPointer(newVal) != DatumGetPointer(peraggstate->transValue))
306 MemoryContextSwitchTo(winstate->wincontext);
307 newVal = datumCopy(newVal,
308 peraggstate->transtypeByVal,
309 peraggstate->transtypeLen);
311 if (!peraggstate->transValueIsNull)
312 pfree(DatumGetPointer(peraggstate->transValue));
315 MemoryContextSwitchTo(oldContext);
316 peraggstate->transValue = newVal;
317 peraggstate->transValueIsNull = fcinfo->isnull;
321 * finalize_windowaggregate
322 * parallel to finalize_aggregate in nodeAgg.c
325 finalize_windowaggregate(WindowAggState *winstate,
326 WindowStatePerFunc perfuncstate,
327 WindowStatePerAgg peraggstate,
328 Datum *result, bool *isnull)
330 MemoryContext oldContext;
332 oldContext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
335 * Apply the agg's finalfn if one is provided, else return transValue.
337 if (OidIsValid(peraggstate->finalfn_oid))
339 FunctionCallInfoData fcinfo;
341 InitFunctionCallInfoData(fcinfo, &(peraggstate->finalfn), 1,
342 (void *) winstate, NULL);
343 fcinfo.arg[0] = peraggstate->transValue;
344 fcinfo.argnull[0] = peraggstate->transValueIsNull;
345 if (fcinfo.flinfo->fn_strict && peraggstate->transValueIsNull)
347 /* don't call a strict function with NULL inputs */
353 *result = FunctionCallInvoke(&fcinfo);
354 *isnull = fcinfo.isnull;
359 *result = peraggstate->transValue;
360 *isnull = peraggstate->transValueIsNull;
364 * If result is pass-by-ref, make sure it is in the right context.
366 if (!peraggstate->resulttypeByVal && !*isnull &&
367 !MemoryContextContains(CurrentMemoryContext,
368 DatumGetPointer(*result)))
369 *result = datumCopy(*result,
370 peraggstate->resulttypeByVal,
371 peraggstate->resulttypeLen);
372 MemoryContextSwitchTo(oldContext);
376 * eval_windowaggregates
377 * evaluate plain aggregates being used as window functions
379 * Much of this is duplicated from nodeAgg.c. But NOTE that we expect to be
380 * able to call aggregate final functions repeatedly after aggregating more
381 * data onto the same transition value. This is not a behavior required by
385 eval_windowaggregates(WindowAggState *winstate)
387 WindowStatePerAgg peraggstate;
388 int wfuncno, numaggs;
390 MemoryContext oldContext;
391 ExprContext *econtext;
392 TupleTableSlot *agg_row_slot;
394 numaggs = winstate->numaggs;
396 return; /* nothing to do */
398 /* final output execution is in ps_ExprContext */
399 econtext = winstate->ss.ps.ps_ExprContext;
402 * Currently, we support only a subset of the SQL-standard window framing
403 * rules. In all the supported cases, the window frame always consists
404 * of a contiguous group of rows extending forward from the start of the
405 * partition, and rows only enter the frame, never exit it, as the
406 * current row advances forward. This makes it possible to use an
407 * incremental strategy for evaluating aggregates: we run the transition
408 * function for each row added to the frame, and run the final function
409 * whenever we need the current aggregate value. This is considerably
410 * more efficient than the naive approach of re-running the entire
411 * aggregate calculation for each current row. It does assume that the
412 * final function doesn't damage the running transition value. (Some
413 * C-coded aggregates do that for efficiency's sake --- but they are
414 * supposed to do so only when their fcinfo->context is an AggState, not
417 * In many common cases, multiple rows share the same frame and hence
418 * the same aggregate value. (In particular, if there's no ORDER BY in
419 * a RANGE window, then all rows are peers and so they all have window
420 * frame equal to the whole partition.) We optimize such cases by
421 * calculating the aggregate value once when we reach the first row of a
422 * peer group, and then returning the saved value for all subsequent rows.
424 * 'aggregatedupto' keeps track of the first row that has not yet been
425 * accumulated into the aggregate transition values. Whenever we start a
426 * new peer group, we accumulate forward to the end of the peer group.
428 * TODO: In the future, we should implement the full SQL-standard set
429 * of framing rules. We could implement the other cases by recalculating
430 * the aggregates whenever a row exits the frame. That would be pretty
431 * slow, though. For aggregates like SUM and COUNT we could implement a
432 * "negative transition function" that would be called for each row as it
433 * exits the frame. We'd have to think about avoiding recalculation of
434 * volatile arguments of aggregate functions, too.
438 * If we've already aggregated up through current row, reuse the
439 * saved result values. NOTE: this test works for the currently
440 * supported framing rules, but will need fixing when more are added.
442 if (winstate->aggregatedupto > winstate->currentpos)
444 for (i = 0; i < numaggs; i++)
446 peraggstate = &winstate->peragg[i];
447 wfuncno = peraggstate->wfuncno;
448 econtext->ecxt_aggvalues[wfuncno] = peraggstate->resultValue;
449 econtext->ecxt_aggnulls[wfuncno] = peraggstate->resultValueIsNull;
454 /* Initialize aggregates on first call for partition */
455 if (winstate->currentpos == 0)
457 for (i = 0; i < numaggs; i++)
459 peraggstate = &winstate->peragg[i];
460 wfuncno = peraggstate->wfuncno;
461 initialize_windowaggregate(winstate,
462 &winstate->perfunc[wfuncno],
468 * Advance until we reach a row not in frame (or end of partition).
470 * Note the loop invariant: agg_row_slot is either empty or holds the
471 * row at position aggregatedupto. The agg_ptr read pointer must always
472 * point to the next row to read into agg_row_slot.
474 agg_row_slot = winstate->agg_row_slot;
477 /* Fetch next row if we didn't already */
478 if (TupIsNull(agg_row_slot))
480 spool_tuples(winstate, winstate->aggregatedupto);
481 tuplestore_select_read_pointer(winstate->buffer,
483 if (!tuplestore_gettupleslot(winstate->buffer, true, agg_row_slot))
484 break; /* must be end of partition */
487 /* Exit loop (for now) if not in frame */
488 if (!row_is_in_frame(winstate, winstate->aggregatedupto, agg_row_slot))
491 /* Set tuple context for evaluation of aggregate arguments */
492 winstate->tmpcontext->ecxt_outertuple = agg_row_slot;
494 /* Accumulate row into the aggregates */
495 for (i = 0; i < numaggs; i++)
497 peraggstate = &winstate->peragg[i];
498 wfuncno = peraggstate->wfuncno;
499 advance_windowaggregate(winstate,
500 &winstate->perfunc[wfuncno],
504 /* Reset per-input-tuple context after each tuple */
505 ResetExprContext(winstate->tmpcontext);
507 /* And advance the aggregated-row state */
508 winstate->aggregatedupto++;
509 ExecClearTuple(agg_row_slot);
513 * finalize aggregates and fill result/isnull fields.
515 for (i = 0; i < numaggs; i++)
520 peraggstate = &winstate->peragg[i];
521 wfuncno = peraggstate->wfuncno;
522 result = &econtext->ecxt_aggvalues[wfuncno];
523 isnull = &econtext->ecxt_aggnulls[wfuncno];
524 finalize_windowaggregate(winstate,
525 &winstate->perfunc[wfuncno],
530 * save the result in case next row shares the same frame.
532 * XXX in some framing modes, eg ROWS/END_CURRENT_ROW, we can know
533 * in advance that the next row can't possibly share the same frame.
534 * Is it worth detecting that and skipping this code?
536 if (!peraggstate->resulttypeByVal)
539 * clear old resultValue in order not to leak memory. (Note:
540 * the new result can't possibly be the same datum as old
541 * resultValue, because we never passed it to the trans function.)
543 if (!peraggstate->resultValueIsNull)
544 pfree(DatumGetPointer(peraggstate->resultValue));
547 * If pass-by-ref, copy it into our global context.
551 oldContext = MemoryContextSwitchTo(winstate->wincontext);
552 peraggstate->resultValue =
554 peraggstate->resulttypeByVal,
555 peraggstate->resulttypeLen);
556 MemoryContextSwitchTo(oldContext);
561 peraggstate->resultValue = *result;
563 peraggstate->resultValueIsNull = *isnull;
568 * eval_windowfunction
570 * Arguments of window functions are not evaluated here, because a window
571 * function can need random access to arbitrary rows in the partition.
572 * The window function uses the special WinGetFuncArgInPartition and
573 * WinGetFuncArgInFrame functions to evaluate the arguments for the rows
577 eval_windowfunction(WindowAggState *winstate, WindowStatePerFunc perfuncstate,
578 Datum *result, bool *isnull)
580 FunctionCallInfoData fcinfo;
581 MemoryContext oldContext;
583 oldContext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
586 * We don't pass any normal arguments to a window function, but we do
587 * pass it the number of arguments, in order to permit window function
588 * implementations to support varying numbers of arguments. The real
589 * info goes through the WindowObject, which is passed via fcinfo->context.
591 InitFunctionCallInfoData(fcinfo, &(perfuncstate->flinfo),
592 perfuncstate->numArguments,
593 (void *) perfuncstate->winobj, NULL);
594 /* Just in case, make all the regular argument slots be null */
595 memset(fcinfo.argnull, true, perfuncstate->numArguments);
597 *result = FunctionCallInvoke(&fcinfo);
598 *isnull = fcinfo.isnull;
601 * Make sure pass-by-ref data is allocated in the appropriate context.
602 * (We need this in case the function returns a pointer into some
603 * short-lived tuple, as is entirely possible.)
605 if (!perfuncstate->resulttypeByVal && !fcinfo.isnull &&
606 !MemoryContextContains(CurrentMemoryContext,
607 DatumGetPointer(*result)))
608 *result = datumCopy(*result,
609 perfuncstate->resulttypeByVal,
610 perfuncstate->resulttypeLen);
612 MemoryContextSwitchTo(oldContext);
617 * Start buffering rows of the next partition.
620 begin_partition(WindowAggState *winstate)
622 PlanState *outerPlan = outerPlanState(winstate);
623 int numfuncs = winstate->numfuncs;
626 winstate->partition_spooled = false;
627 winstate->frametail_valid = false;
628 winstate->spooled_rows = 0;
629 winstate->currentpos = 0;
630 winstate->frametailpos = -1;
631 winstate->aggregatedupto = 0;
632 ExecClearTuple(winstate->agg_row_slot);
635 * If this is the very first partition, we need to fetch the first
636 * input row to store in first_part_slot.
638 if (TupIsNull(winstate->first_part_slot))
640 TupleTableSlot *outerslot = ExecProcNode(outerPlan);
642 if (!TupIsNull(outerslot))
643 ExecCopySlot(winstate->first_part_slot, outerslot);
646 /* outer plan is empty, so we have nothing to do */
647 winstate->partition_spooled = true;
648 winstate->more_partitions = false;
653 /* Create new tuplestore for this partition */
654 winstate->buffer = tuplestore_begin_heap(false, false, work_mem);
657 * Set up read pointers for the tuplestore. The current and agg pointers
658 * don't need BACKWARD capability, but the per-window-function read
661 winstate->current_ptr = 0; /* read pointer 0 is pre-allocated */
663 /* reset default REWIND capability bit for current ptr */
664 tuplestore_set_eflags(winstate->buffer, 0);
666 /* create a read pointer for aggregates, if needed */
667 if (winstate->numaggs > 0)
668 winstate->agg_ptr = tuplestore_alloc_read_pointer(winstate->buffer, 0);
670 /* create mark and read pointers for each real window function */
671 for (i = 0; i < numfuncs; i++)
673 WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
675 if (!perfuncstate->plain_agg)
677 WindowObject winobj = perfuncstate->winobj;
679 winobj->markptr = tuplestore_alloc_read_pointer(winstate->buffer,
681 winobj->readptr = tuplestore_alloc_read_pointer(winstate->buffer,
683 winobj->markpos = -1;
684 winobj->seekpos = -1;
689 * Store the first tuple into the tuplestore (it's always available now;
690 * we either read it above, or saved it at the end of previous partition)
692 tuplestore_puttupleslot(winstate->buffer, winstate->first_part_slot);
693 winstate->spooled_rows++;
697 * Read tuples from the outer node, up to position 'pos', and store them
698 * into the tuplestore. If pos is -1, reads the whole partition.
701 spool_tuples(WindowAggState *winstate, int64 pos)
703 WindowAgg *node = (WindowAgg *) winstate->ss.ps.plan;
704 PlanState *outerPlan;
705 TupleTableSlot *outerslot;
706 MemoryContext oldcontext;
708 if (!winstate->buffer)
709 return; /* just a safety check */
710 if (winstate->partition_spooled)
711 return; /* whole partition done already */
714 * If the tuplestore has spilled to disk, alternate reading and writing
715 * becomes quite expensive due to frequent buffer flushes. It's cheaper
716 * to force the entire partition to get spooled in one go.
718 * XXX this is a horrid kluge --- it'd be better to fix the performance
719 * problem inside tuplestore. FIXME
721 if (!tuplestore_in_memory(winstate->buffer))
724 outerPlan = outerPlanState(winstate);
726 /* Must be in query context to call outerplan or touch tuplestore */
727 oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
729 while (winstate->spooled_rows <= pos || pos == -1)
731 outerslot = ExecProcNode(outerPlan);
732 if (TupIsNull(outerslot))
734 /* reached the end of the last partition */
735 winstate->partition_spooled = true;
736 winstate->more_partitions = false;
740 if (node->partNumCols > 0)
742 /* Check if this tuple still belongs to the current partition */
743 if (!execTuplesMatch(winstate->first_part_slot,
745 node->partNumCols, node->partColIdx,
746 winstate->partEqfunctions,
747 winstate->tmpcontext->ecxt_per_tuple_memory))
750 * end of partition; copy the tuple for the next cycle.
752 ExecCopySlot(winstate->first_part_slot, outerslot);
753 winstate->partition_spooled = true;
754 winstate->more_partitions = true;
759 /* Still in partition, so save it into the tuplestore */
760 tuplestore_puttupleslot(winstate->buffer, outerslot);
761 winstate->spooled_rows++;
764 MemoryContextSwitchTo(oldcontext);
769 * clear information kept within a partition, including
770 * tuplestore and aggregate results.
773 release_partition(WindowAggState *winstate)
777 for (i = 0; i < winstate->numfuncs; i++)
779 WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
781 /* Release any partition-local state of this window function */
782 if (perfuncstate->winobj)
783 perfuncstate->winobj->localmem = NULL;
787 * Release all partition-local memory (in particular, any partition-local
788 * state that we might have trashed our pointers to in the above loop, and
789 * any aggregate temp data). We don't rely on retail pfree because some
790 * aggregates might have allocated data we don't have direct pointers to.
792 MemoryContextResetAndDeleteChildren(winstate->wincontext);
794 if (winstate->buffer)
795 tuplestore_end(winstate->buffer);
796 winstate->buffer = NULL;
797 winstate->partition_spooled = false;
802 * Determine whether a row is in the current row's window frame according
803 * to our window framing rule
805 * The caller must have already determined that the row is in the partition
806 * and fetched it into a slot. This function just encapsulates the framing
810 row_is_in_frame(WindowAggState *winstate, int64 pos, TupleTableSlot *slot)
812 WindowAgg *node = (WindowAgg *) winstate->ss.ps.plan;
813 int frameOptions = node->frameOptions;
815 Assert(pos >= 0); /* else caller error */
817 /* We only support frame start mode UNBOUNDED PRECEDING for now */
818 Assert(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING);
820 /* In UNBOUNDED FOLLOWING mode, all partition rows are in frame */
821 if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
824 /* Else frame tail mode must be CURRENT ROW */
825 Assert(frameOptions & FRAMEOPTION_END_CURRENT_ROW);
827 /* if row is current row or a predecessor, it must be in frame */
828 if (pos <= winstate->currentpos)
831 /* In ROWS mode, *only* such rows are in frame */
832 if (frameOptions & FRAMEOPTION_ROWS)
835 /* Else must be RANGE mode */
836 Assert(frameOptions & FRAMEOPTION_RANGE);
838 /* In frame iff it's a peer of current row */
839 return are_peers(winstate, slot, winstate->ss.ss_ScanTupleSlot);
843 * update_frametailpos
844 * make frametailpos valid for the current row
846 * Uses the winobj's read pointer for any required fetches; the winobj's
847 * mark must not be past the currently known frame tail. Also uses the
848 * specified slot for any required fetches.
851 update_frametailpos(WindowObject winobj, TupleTableSlot *slot)
853 WindowAggState *winstate = winobj->winstate;
854 WindowAgg *node = (WindowAgg *) winstate->ss.ps.plan;
855 int frameOptions = node->frameOptions;
858 if (winstate->frametail_valid)
859 return; /* already known for current row */
861 /* We only support frame start mode UNBOUNDED PRECEDING for now */
862 Assert(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING);
864 /* In UNBOUNDED FOLLOWING mode, all partition rows are in frame */
865 if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
867 spool_tuples(winstate, -1);
868 winstate->frametailpos = winstate->spooled_rows - 1;
869 winstate->frametail_valid = true;
873 /* Else frame tail mode must be CURRENT ROW */
874 Assert(frameOptions & FRAMEOPTION_END_CURRENT_ROW);
876 /* In ROWS mode, exactly the rows up to current are in frame */
877 if (frameOptions & FRAMEOPTION_ROWS)
879 winstate->frametailpos = winstate->currentpos;
880 winstate->frametail_valid = true;
884 /* Else must be RANGE mode */
885 Assert(frameOptions & FRAMEOPTION_RANGE);
887 /* If no ORDER BY, all rows are peers with each other */
888 if (node->ordNumCols == 0)
890 spool_tuples(winstate, -1);
891 winstate->frametailpos = winstate->spooled_rows - 1;
892 winstate->frametail_valid = true;
897 * Else we have to search for the first non-peer of the current row.
898 * We assume the current value of frametailpos is a lower bound on the
899 * possible frame tail location, ie, frame tail never goes backward, and
900 * that currentpos is also a lower bound, ie, current row is always in
903 ftnext = Max(winstate->frametailpos, winstate->currentpos) + 1;
906 if (!window_gettupleslot(winobj, ftnext, slot))
907 break; /* end of partition */
908 if (!are_peers(winstate, slot, winstate->ss.ss_ScanTupleSlot))
909 break; /* not peer of current row */
912 winstate->frametailpos = ftnext - 1;
913 winstate->frametail_valid = true;
920 * ExecWindowAgg receives tuples from its outer subplan and
921 * stores them into a tuplestore, then processes window functions.
922 * This node doesn't reduce nor qualify any row so the number of
923 * returned rows is exactly the same as its outer subplan's result
924 * (ignoring the case of SRFs in the targetlist, that is).
928 ExecWindowAgg(WindowAggState *winstate)
930 TupleTableSlot *result;
932 ExprContext *econtext;
936 if (winstate->all_done)
940 * Check to see if we're still projecting out tuples from a previous output
941 * tuple (because there is a function-returning-set in the projection
942 * expressions). If so, try to project another one.
944 if (winstate->ss.ps.ps_TupFromTlist)
946 TupleTableSlot *result;
949 result = ExecProject(winstate->ss.ps.ps_ProjInfo, &isDone);
950 if (isDone == ExprMultipleResult)
952 /* Done with that source tuple... */
953 winstate->ss.ps.ps_TupFromTlist = false;
957 if (winstate->buffer == NULL)
959 /* Initialize for first partition and set current row = 0 */
960 begin_partition(winstate);
961 /* If there are no input rows, we'll detect that and exit below */
965 /* Advance current row within partition */
966 winstate->currentpos++;
967 /* This might mean that the frame tail moves, too */
968 winstate->frametail_valid = false;
972 * Spool all tuples up to and including the current row, if we haven't
975 spool_tuples(winstate, winstate->currentpos);
977 /* Move to the next partition if we reached the end of this partition */
978 if (winstate->partition_spooled &&
979 winstate->currentpos >= winstate->spooled_rows)
981 release_partition(winstate);
983 if (winstate->more_partitions)
985 begin_partition(winstate);
986 Assert(winstate->spooled_rows > 0);
990 winstate->all_done = true;
995 /* final output execution is in ps_ExprContext */
996 econtext = winstate->ss.ps.ps_ExprContext;
998 /* Clear the per-output-tuple context for current row */
999 ResetExprContext(econtext);
1002 * Read the current row from the tuplestore, and save in ScanTupleSlot.
1003 * (We can't rely on the outerplan's output slot because we may have to
1004 * read beyond the current row.)
1006 * Current row must be in the tuplestore, since we spooled it above.
1008 tuplestore_select_read_pointer(winstate->buffer, winstate->current_ptr);
1009 if (!tuplestore_gettupleslot(winstate->buffer, true,
1010 winstate->ss.ss_ScanTupleSlot))
1011 elog(ERROR, "unexpected end of tuplestore");
1014 * Evaluate true window functions
1016 numfuncs = winstate->numfuncs;
1017 for (i = 0; i < numfuncs; i++)
1019 WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
1021 if (perfuncstate->plain_agg)
1023 eval_windowfunction(winstate, perfuncstate,
1024 &(econtext->ecxt_aggvalues[perfuncstate->wfuncstate->wfuncno]),
1025 &(econtext->ecxt_aggnulls[perfuncstate->wfuncstate->wfuncno]));
1029 * Evaluate aggregates
1031 if (winstate->numaggs > 0)
1032 eval_windowaggregates(winstate);
1035 * Truncate any no-longer-needed rows from the tuplestore.
1037 tuplestore_trim(winstate->buffer);
1040 * Form and return a projection tuple using the windowfunc results
1041 * and the current row. Setting ecxt_outertuple arranges that any
1042 * Vars will be evaluated with respect to that row.
1044 econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
1045 result = ExecProject(winstate->ss.ps.ps_ProjInfo, &isDone);
1047 if (isDone == ExprEndResult)
1049 /* SRF in tlist returned no rows, so advance to next input tuple */
1053 winstate->ss.ps.ps_TupFromTlist =
1054 (isDone == ExprMultipleResult);
1058 /* -----------------
1061 * Creates the run-time information for the WindowAgg node produced by the
1062 * planner and initializes its outer subtree
1066 ExecInitWindowAgg(WindowAgg *node, EState *estate, int eflags)
1068 WindowAggState *winstate;
1070 ExprContext *econtext;
1071 ExprContext *tmpcontext;
1072 WindowStatePerFunc perfunc;
1073 WindowStatePerAgg peragg;
1080 /* check for unsupported flags */
1081 Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1084 * create state structure
1086 winstate = makeNode(WindowAggState);
1087 winstate->ss.ps.plan = (Plan *) node;
1088 winstate->ss.ps.state = estate;
1091 * Create expression contexts. We need two, one for per-input-tuple
1092 * processing and one for per-output-tuple processing. We cheat a little
1093 * by using ExecAssignExprContext() to build both.
1095 ExecAssignExprContext(estate, &winstate->ss.ps);
1096 tmpcontext = winstate->ss.ps.ps_ExprContext;
1097 winstate->tmpcontext = tmpcontext;
1098 ExecAssignExprContext(estate, &winstate->ss.ps);
1100 /* Create long-lived context for storage of aggregate transvalues etc */
1101 winstate->wincontext =
1102 AllocSetContextCreate(CurrentMemoryContext,
1104 ALLOCSET_DEFAULT_MINSIZE,
1105 ALLOCSET_DEFAULT_INITSIZE,
1106 ALLOCSET_DEFAULT_MAXSIZE);
1108 #define WINDOWAGG_NSLOTS 6
1111 * tuple table initialization
1113 ExecInitScanTupleSlot(estate, &winstate->ss);
1114 ExecInitResultTupleSlot(estate, &winstate->ss.ps);
1115 winstate->first_part_slot = ExecInitExtraTupleSlot(estate);
1116 winstate->agg_row_slot = ExecInitExtraTupleSlot(estate);
1117 winstate->temp_slot_1 = ExecInitExtraTupleSlot(estate);
1118 winstate->temp_slot_2 = ExecInitExtraTupleSlot(estate);
1120 winstate->ss.ps.targetlist = (List *)
1121 ExecInitExpr((Expr *) node->plan.targetlist,
1122 (PlanState *) winstate);
1125 * WindowAgg nodes never have quals, since they can only occur at the
1126 * logical top level of a query (ie, after any WHERE or HAVING filters)
1128 Assert(node->plan.qual == NIL);
1129 winstate->ss.ps.qual = NIL;
1132 * initialize child nodes
1134 outerPlan = outerPlan(node);
1135 outerPlanState(winstate) = ExecInitNode(outerPlan, estate, eflags);
1138 * initialize source tuple type (which is also the tuple type that we'll
1139 * store in the tuplestore and use in all our working slots).
1141 ExecAssignScanTypeFromOuterPlan(&winstate->ss);
1143 ExecSetSlotDescriptor(winstate->first_part_slot,
1144 winstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor);
1145 ExecSetSlotDescriptor(winstate->agg_row_slot,
1146 winstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor);
1147 ExecSetSlotDescriptor(winstate->temp_slot_1,
1148 winstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor);
1149 ExecSetSlotDescriptor(winstate->temp_slot_2,
1150 winstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor);
1153 * Initialize result tuple type and projection info.
1155 ExecAssignResultTypeFromTL(&winstate->ss.ps);
1156 ExecAssignProjectionInfo(&winstate->ss.ps, NULL);
1158 winstate->ss.ps.ps_TupFromTlist = false;
1160 /* Set up data for comparing tuples */
1161 if (node->partNumCols > 0)
1162 winstate->partEqfunctions = execTuplesMatchPrepare(node->partNumCols,
1163 node->partOperators);
1164 if (node->ordNumCols > 0)
1165 winstate->ordEqfunctions = execTuplesMatchPrepare(node->ordNumCols,
1166 node->ordOperators);
1169 * WindowAgg nodes use aggvalues and aggnulls as well as Agg nodes.
1171 numfuncs = winstate->numfuncs;
1172 numaggs = winstate->numaggs;
1173 econtext = winstate->ss.ps.ps_ExprContext;
1174 econtext->ecxt_aggvalues = (Datum *) palloc0(sizeof(Datum) * numfuncs);
1175 econtext->ecxt_aggnulls = (bool *) palloc0(sizeof(bool) * numfuncs);
1178 * allocate per-wfunc/per-agg state information.
1180 perfunc = (WindowStatePerFunc) palloc0(sizeof(WindowStatePerFuncData) * numfuncs);
1181 peragg = (WindowStatePerAgg) palloc0(sizeof(WindowStatePerAggData) * numaggs);
1182 winstate->perfunc = perfunc;
1183 winstate->peragg = peragg;
1187 foreach(l, winstate->funcs)
1189 WindowFuncExprState *wfuncstate = (WindowFuncExprState *) lfirst(l);
1190 WindowFunc *wfunc = (WindowFunc *) wfuncstate->xprstate.expr;
1191 WindowStatePerFunc perfuncstate;
1192 AclResult aclresult;
1195 if (wfunc->winref != node->winref) /* planner screwed up? */
1196 elog(ERROR, "WindowFunc with winref %u assigned to WindowAgg with winref %u",
1197 wfunc->winref, node->winref);
1199 /* Look for a previous duplicate window function */
1200 for (i = 0; i <= wfuncno; i++)
1202 if (equal(wfunc, perfunc[i].wfunc) &&
1203 !contain_volatile_functions((Node *) wfunc))
1208 /* Found a match to an existing entry, so just mark it */
1209 wfuncstate->wfuncno = i;
1213 /* Nope, so assign a new PerAgg record */
1214 perfuncstate = &perfunc[++wfuncno];
1216 /* Mark WindowFunc state node with assigned index in the result array */
1217 wfuncstate->wfuncno = wfuncno;
1219 /* Check permission to call window function */
1220 aclresult = pg_proc_aclcheck(wfunc->winfnoid, GetUserId(),
1222 if (aclresult != ACLCHECK_OK)
1223 aclcheck_error(aclresult, ACL_KIND_PROC,
1224 get_func_name(wfunc->winfnoid));
1226 /* Fill in the perfuncstate data */
1227 perfuncstate->wfuncstate = wfuncstate;
1228 perfuncstate->wfunc = wfunc;
1229 perfuncstate->numArguments = list_length(wfuncstate->args);
1231 fmgr_info_cxt(wfunc->winfnoid, &perfuncstate->flinfo,
1232 tmpcontext->ecxt_per_query_memory);
1233 perfuncstate->flinfo.fn_expr = (Node *) wfunc;
1234 get_typlenbyval(wfunc->wintype,
1235 &perfuncstate->resulttypeLen,
1236 &perfuncstate->resulttypeByVal);
1239 * If it's really just a plain aggregate function,
1240 * we'll emulate the Agg environment for it.
1242 perfuncstate->plain_agg = wfunc->winagg;
1245 WindowStatePerAgg peraggstate;
1247 perfuncstate->aggno = ++aggno;
1248 peraggstate = &winstate->peragg[aggno];
1249 initialize_peragg(winstate, wfunc, peraggstate);
1250 peraggstate->wfuncno = wfuncno;
1254 WindowObject winobj = makeNode(WindowObjectData);
1256 winobj->winstate = winstate;
1257 winobj->argstates = wfuncstate->args;
1258 winobj->localmem = NULL;
1259 perfuncstate->winobj = winobj;
1263 /* Update numfuncs, numaggs to match number of unique functions found */
1264 winstate->numfuncs = wfuncno + 1;
1265 winstate->numaggs = aggno + 1;
1267 winstate->partition_spooled = false;
1268 winstate->more_partitions = false;
1273 /* -----------------
1274 * ExecCountSlotsWindowAgg
1278 ExecCountSlotsWindowAgg(WindowAgg *node)
1280 return ExecCountSlotsNode(outerPlan(node)) +
1281 ExecCountSlotsNode(innerPlan(node)) +
1285 /* -----------------
1290 ExecEndWindowAgg(WindowAggState *node)
1292 PlanState *outerPlan;
1294 release_partition(node);
1296 pfree(node->perfunc);
1297 pfree(node->peragg);
1299 ExecClearTuple(node->ss.ss_ScanTupleSlot);
1300 ExecClearTuple(node->first_part_slot);
1301 ExecClearTuple(node->agg_row_slot);
1302 ExecClearTuple(node->temp_slot_1);
1303 ExecClearTuple(node->temp_slot_2);
1306 * Free both the expr contexts.
1308 ExecFreeExprContext(&node->ss.ps);
1309 node->ss.ps.ps_ExprContext = node->tmpcontext;
1310 ExecFreeExprContext(&node->ss.ps);
1312 MemoryContextDelete(node->wincontext);
1314 outerPlan = outerPlanState(node);
1315 ExecEndNode(outerPlan);
1318 /* -----------------
1319 * ExecRescanWindowAgg
1323 ExecReScanWindowAgg(WindowAggState *node, ExprContext *exprCtxt)
1325 ExprContext *econtext = node->ss.ps.ps_ExprContext;
1327 node->all_done = false;
1329 node->ss.ps.ps_TupFromTlist = false;
1331 /* release tuplestore et al */
1332 release_partition(node);
1334 /* release all temp tuples, but especially first_part_slot */
1335 ExecClearTuple(node->ss.ss_ScanTupleSlot);
1336 ExecClearTuple(node->first_part_slot);
1337 ExecClearTuple(node->agg_row_slot);
1338 ExecClearTuple(node->temp_slot_1);
1339 ExecClearTuple(node->temp_slot_2);
1341 /* Forget current wfunc values */
1342 MemSet(econtext->ecxt_aggvalues, 0, sizeof(Datum) * node->numfuncs);
1343 MemSet(econtext->ecxt_aggnulls, 0, sizeof(bool) * node->numfuncs);
1346 * if chgParam of subnode is not null then plan will be re-scanned by
1347 * first ExecProcNode.
1349 if (((PlanState *) node)->lefttree->chgParam == NULL)
1350 ExecReScan(((PlanState *) node)->lefttree, exprCtxt);
1356 * Almost same as in nodeAgg.c, except we don't support DISTINCT currently.
1358 static WindowStatePerAggData *
1359 initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc,
1360 WindowStatePerAgg peraggstate)
1362 Oid inputTypes[FUNC_MAX_ARGS];
1365 Form_pg_aggregate aggform;
1367 AclResult aclresult;
1376 numArguments = list_length(wfunc->args);
1379 foreach(lc, wfunc->args)
1381 inputTypes[i++] = exprType((Node *) lfirst(lc));
1384 aggTuple = SearchSysCache(AGGFNOID,
1385 ObjectIdGetDatum(wfunc->winfnoid),
1387 if (!HeapTupleIsValid(aggTuple))
1388 elog(ERROR, "cache lookup failed for aggregate %u",
1390 aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
1393 * ExecInitWindowAgg already checked permission to call aggregate function
1394 * ... but we still need to check the component functions
1397 peraggstate->transfn_oid = transfn_oid = aggform->aggtransfn;
1398 peraggstate->finalfn_oid = finalfn_oid = aggform->aggfinalfn;
1400 /* Check that aggregate owner has permission to call component fns */
1402 HeapTuple procTuple;
1405 procTuple = SearchSysCache(PROCOID,
1406 ObjectIdGetDatum(wfunc->winfnoid),
1408 if (!HeapTupleIsValid(procTuple))
1409 elog(ERROR, "cache lookup failed for function %u",
1411 aggOwner = ((Form_pg_proc) GETSTRUCT(procTuple))->proowner;
1412 ReleaseSysCache(procTuple);
1414 aclresult = pg_proc_aclcheck(transfn_oid, aggOwner,
1416 if (aclresult != ACLCHECK_OK)
1417 aclcheck_error(aclresult, ACL_KIND_PROC,
1418 get_func_name(transfn_oid));
1419 if (OidIsValid(finalfn_oid))
1421 aclresult = pg_proc_aclcheck(finalfn_oid, aggOwner,
1423 if (aclresult != ACLCHECK_OK)
1424 aclcheck_error(aclresult, ACL_KIND_PROC,
1425 get_func_name(finalfn_oid));
1429 /* resolve actual type of transition state, if polymorphic */
1430 aggtranstype = aggform->aggtranstype;
1431 if (IsPolymorphicType(aggtranstype))
1433 /* have to fetch the agg's declared input types... */
1434 Oid *declaredArgTypes;
1437 get_func_signature(wfunc->winfnoid,
1438 &declaredArgTypes, &agg_nargs);
1439 Assert(agg_nargs == numArguments);
1440 aggtranstype = enforce_generic_type_consistency(inputTypes,
1445 pfree(declaredArgTypes);
1448 /* build expression trees using actual argument & result types */
1449 build_aggregate_fnexprs(inputTypes,
1458 fmgr_info(transfn_oid, &peraggstate->transfn);
1459 peraggstate->transfn.fn_expr = (Node *) transfnexpr;
1461 if (OidIsValid(finalfn_oid))
1463 fmgr_info(finalfn_oid, &peraggstate->finalfn);
1464 peraggstate->finalfn.fn_expr = (Node *) finalfnexpr;
1467 get_typlenbyval(wfunc->wintype,
1468 &peraggstate->resulttypeLen,
1469 &peraggstate->resulttypeByVal);
1470 get_typlenbyval(aggtranstype,
1471 &peraggstate->transtypeLen,
1472 &peraggstate->transtypeByVal);
1475 * initval is potentially null, so don't try to access it as a struct
1476 * field. Must do it the hard way with SysCacheGetAttr.
1478 textInitVal = SysCacheGetAttr(AGGFNOID, aggTuple,
1479 Anum_pg_aggregate_agginitval,
1480 &peraggstate->initValueIsNull);
1482 if (peraggstate->initValueIsNull)
1483 peraggstate->initValue = (Datum) 0;
1485 peraggstate->initValue = GetAggInitVal(textInitVal,
1489 * If the transfn is strict and the initval is NULL, make sure input
1490 * type and transtype are the same (or at least binary-compatible), so
1491 * that it's OK to use the first input value as the initial
1492 * transValue. This should have been checked at agg definition time,
1493 * but just in case...
1495 if (peraggstate->transfn.fn_strict && peraggstate->initValueIsNull)
1497 if (numArguments < 1 ||
1498 !IsBinaryCoercible(inputTypes[0], aggtranstype))
1500 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1501 errmsg("aggregate %u needs to have compatible input type and transition type",
1505 ReleaseSysCache(aggTuple);
1511 GetAggInitVal(Datum textInitVal, Oid transtype)
1518 getTypeInputInfo(transtype, &typinput, &typioparam);
1519 strInitVal = TextDatumGetCString(textInitVal);
1520 initVal = OidInputFunctionCall(typinput, strInitVal,
1528 * compare two rows to see if they are equal according to the ORDER BY clause
1530 * NB: this does not consider the window frame mode.
1533 are_peers(WindowAggState *winstate, TupleTableSlot *slot1,
1534 TupleTableSlot *slot2)
1536 WindowAgg *node = (WindowAgg *) winstate->ss.ps.plan;
1538 /* If no ORDER BY, all rows are peers with each other */
1539 if (node->ordNumCols == 0)
1542 return execTuplesMatch(slot1, slot2,
1543 node->ordNumCols, node->ordColIdx,
1544 winstate->ordEqfunctions,
1545 winstate->tmpcontext->ecxt_per_tuple_memory);
1549 * window_gettupleslot
1550 * Fetch the pos'th tuple of the current partition into the slot,
1551 * using the winobj's read pointer
1553 * Returns true if successful, false if no such row
1556 window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
1558 WindowAggState *winstate = winobj->winstate;
1559 MemoryContext oldcontext;
1561 /* Don't allow passing -1 to spool_tuples here */
1565 /* If necessary, fetch the tuple into the spool */
1566 spool_tuples(winstate, pos);
1568 if (pos >= winstate->spooled_rows)
1571 if (pos < winobj->markpos)
1572 elog(ERROR, "cannot fetch row before WindowObject's mark position");
1574 oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
1576 tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
1579 * There's no API to refetch the tuple at the current position. We
1580 * have to move one tuple forward, and then one backward. (We don't
1581 * do it the other way because we might try to fetch the row before
1582 * our mark, which isn't allowed.)
1584 if (winobj->seekpos == pos)
1586 tuplestore_advance(winstate->buffer, true);
1590 while (winobj->seekpos > pos)
1592 if (!tuplestore_gettupleslot(winstate->buffer, false, slot))
1593 elog(ERROR, "unexpected end of tuplestore");
1597 while (winobj->seekpos < pos)
1599 if (!tuplestore_gettupleslot(winstate->buffer, true, slot))
1600 elog(ERROR, "unexpected end of tuplestore");
1604 MemoryContextSwitchTo(oldcontext);
1610 /***********************************************************************
1611 * API exposed to window functions
1612 ***********************************************************************/
1616 * WinGetPartitionLocalMemory
1617 * Get working memory that lives till end of partition processing
1619 * On first call within a given partition, this allocates and zeroes the
1620 * requested amount of space. Subsequent calls just return the same chunk.
1622 * Memory obtained this way is normally used to hold state that should be
1623 * automatically reset for each new partition. If a window function wants
1624 * to hold state across the whole query, fcinfo->fn_extra can be used in the
1625 * usual way for that.
1628 WinGetPartitionLocalMemory(WindowObject winobj, Size sz)
1630 Assert(WindowObjectIsValid(winobj));
1631 if (winobj->localmem == NULL)
1632 winobj->localmem = MemoryContextAllocZero(winobj->winstate->wincontext,
1634 return winobj->localmem;
1638 * WinGetCurrentPosition
1639 * Return the current row's position (counting from 0) within the current
1643 WinGetCurrentPosition(WindowObject winobj)
1645 Assert(WindowObjectIsValid(winobj));
1646 return winobj->winstate->currentpos;
1650 * WinGetPartitionRowCount
1651 * Return total number of rows contained in the current partition.
1653 * Note: this is a relatively expensive operation because it forces the
1654 * whole partition to be "spooled" into the tuplestore at once. Once
1655 * executed, however, additional calls within the same partition are cheap.
1658 WinGetPartitionRowCount(WindowObject winobj)
1660 Assert(WindowObjectIsValid(winobj));
1661 spool_tuples(winobj->winstate, -1);
1662 return winobj->winstate->spooled_rows;
1666 * WinSetMarkPosition
1667 * Set the "mark" position for the window object, which is the oldest row
1668 * number (counting from 0) it is allowed to fetch during all subsequent
1669 * operations within the current partition.
1671 * Window functions do not have to call this, but are encouraged to move the
1672 * mark forward when possible to keep the tuplestore size down and prevent
1673 * having to spill rows to disk.
1676 WinSetMarkPosition(WindowObject winobj, int64 markpos)
1678 WindowAggState *winstate;
1680 Assert(WindowObjectIsValid(winobj));
1681 winstate = winobj->winstate;
1683 if (markpos < winobj->markpos)
1684 elog(ERROR, "cannot move WindowObject's mark position backward");
1685 tuplestore_select_read_pointer(winstate->buffer, winobj->markptr);
1686 while (markpos > winobj->markpos)
1688 tuplestore_advance(winstate->buffer, true);
1691 tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
1692 while (markpos > winobj->seekpos)
1694 tuplestore_advance(winstate->buffer, true);
1701 * Compare two rows (specified by absolute position in window) to see
1702 * if they are equal according to the ORDER BY clause.
1704 * NB: this does not consider the window frame mode.
1707 WinRowsArePeers(WindowObject winobj, int64 pos1, int64 pos2)
1709 WindowAggState *winstate;
1711 TupleTableSlot *slot1;
1712 TupleTableSlot *slot2;
1715 Assert(WindowObjectIsValid(winobj));
1716 winstate = winobj->winstate;
1717 node = (WindowAgg *) winstate->ss.ps.plan;
1719 /* If no ORDER BY, all rows are peers; don't bother to fetch them */
1720 if (node->ordNumCols == 0)
1723 slot1 = winstate->temp_slot_1;
1724 slot2 = winstate->temp_slot_2;
1726 if (!window_gettupleslot(winobj, pos1, slot1))
1727 elog(ERROR, "specified position is out of window: " INT64_FORMAT,
1729 if (!window_gettupleslot(winobj, pos2, slot2))
1730 elog(ERROR, "specified position is out of window: " INT64_FORMAT,
1733 res = are_peers(winstate, slot1, slot2);
1735 ExecClearTuple(slot1);
1736 ExecClearTuple(slot2);
1742 * WinGetFuncArgInPartition
1743 * Evaluate a window function's argument expression on a specified
1744 * row of the partition. The row is identified in lseek(2) style,
1745 * i.e. relative to the current, first, or last row.
1747 * argno: argument number to evaluate (counted from 0)
1748 * relpos: signed rowcount offset from the seek position
1749 * seektype: WINDOW_SEEK_CURRENT, WINDOW_SEEK_HEAD, or WINDOW_SEEK_TAIL
1750 * set_mark: If the row is found and set_mark is true, the mark is moved to
1751 * the row as a side-effect.
1752 * isnull: output argument, receives isnull status of result
1753 * isout: output argument, set to indicate whether target row position
1754 * is out of partition (can pass NULL if caller doesn't care about this)
1756 * Specifying a nonexistent row is not an error, it just causes a null result
1757 * (plus setting *isout true, if isout isn't NULL).
1760 WinGetFuncArgInPartition(WindowObject winobj, int argno,
1761 int relpos, int seektype, bool set_mark,
1762 bool *isnull, bool *isout)
1764 WindowAggState *winstate;
1765 ExprContext *econtext;
1766 TupleTableSlot *slot;
1770 Assert(WindowObjectIsValid(winobj));
1771 winstate = winobj->winstate;
1772 econtext = winstate->ss.ps.ps_ExprContext;
1773 slot = winstate->temp_slot_1;
1777 case WINDOW_SEEK_CURRENT:
1778 abs_pos = winstate->currentpos + relpos;
1780 case WINDOW_SEEK_HEAD:
1783 case WINDOW_SEEK_TAIL:
1784 spool_tuples(winstate, -1);
1785 abs_pos = winstate->spooled_rows - 1 + relpos;
1788 elog(ERROR, "unrecognized window seek type: %d", seektype);
1789 abs_pos = 0; /* keep compiler quiet */
1793 gottuple = window_gettupleslot(winobj, abs_pos, slot);
1807 WinSetMarkPosition(winobj, abs_pos);
1808 econtext->ecxt_outertuple = slot;
1809 return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
1810 econtext, isnull, NULL);
1815 * WinGetFuncArgInFrame
1816 * Evaluate a window function's argument expression on a specified
1817 * row of the window frame. The row is identified in lseek(2) style,
1818 * i.e. relative to the current, first, or last row.
1820 * argno: argument number to evaluate (counted from 0)
1821 * relpos: signed rowcount offset from the seek position
1822 * seektype: WINDOW_SEEK_CURRENT, WINDOW_SEEK_HEAD, or WINDOW_SEEK_TAIL
1823 * set_mark: If the row is found and set_mark is true, the mark is moved to
1824 * the row as a side-effect.
1825 * isnull: output argument, receives isnull status of result
1826 * isout: output argument, set to indicate whether target row position
1827 * is out of frame (can pass NULL if caller doesn't care about this)
1829 * Specifying a nonexistent row is not an error, it just causes a null result
1830 * (plus setting *isout true, if isout isn't NULL).
1833 WinGetFuncArgInFrame(WindowObject winobj, int argno,
1834 int relpos, int seektype, bool set_mark,
1835 bool *isnull, bool *isout)
1837 WindowAggState *winstate;
1838 ExprContext *econtext;
1839 TupleTableSlot *slot;
1843 Assert(WindowObjectIsValid(winobj));
1844 winstate = winobj->winstate;
1845 econtext = winstate->ss.ps.ps_ExprContext;
1846 slot = winstate->temp_slot_1;
1850 case WINDOW_SEEK_CURRENT:
1851 abs_pos = winstate->currentpos + relpos;
1853 case WINDOW_SEEK_HEAD:
1856 case WINDOW_SEEK_TAIL:
1857 update_frametailpos(winobj, slot);
1858 abs_pos = winstate->frametailpos + relpos;
1861 elog(ERROR, "unrecognized window seek type: %d", seektype);
1862 abs_pos = 0; /* keep compiler quiet */
1866 gottuple = window_gettupleslot(winobj, abs_pos, slot);
1868 gottuple = row_is_in_frame(winstate, abs_pos, slot);
1882 WinSetMarkPosition(winobj, abs_pos);
1883 econtext->ecxt_outertuple = slot;
1884 return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
1885 econtext, isnull, NULL);
1890 * WinGetFuncArgCurrent
1891 * Evaluate a window function's argument expression on the current row.
1893 * argno: argument number to evaluate (counted from 0)
1894 * isnull: output argument, receives isnull status of result
1896 * Note: this isn't quite equivalent to WinGetFuncArgInPartition or
1897 * WinGetFuncArgInFrame targeting the current row, because it will succeed
1898 * even if the WindowObject's mark has been set beyond the current row.
1899 * This should generally be used for "ordinary" arguments of a window
1900 * function, such as the offset argument of lead() or lag().
1903 WinGetFuncArgCurrent(WindowObject winobj, int argno, bool *isnull)
1905 WindowAggState *winstate;
1906 ExprContext *econtext;
1908 Assert(WindowObjectIsValid(winobj));
1909 winstate = winobj->winstate;
1911 econtext = winstate->ss.ps.ps_ExprContext;
1913 econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
1914 return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
1915 econtext, isnull, NULL);