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