1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
|
/*-------------------------------------------------------------------------
*
* windowfuncs.c
* Standard window functions defined in SQL spec.
*
* Portions Copyright (c) 2000-2023, PostgreSQL Global Development Group
*
*
* IDENTIFICATION
* src/backend/utils/adt/windowfuncs.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "nodes/supportnodes.h"
#include "utils/builtins.h"
#include "windowapi.h"
/*
* ranking process information
*/
typedef struct rank_context
{
int64 rank; /* current rank */
} rank_context;
/*
* ntile process information
*/
typedef struct
{
int32 ntile; /* current result */
int64 rows_per_bucket; /* row number of current bucket */
int64 boundary; /* how many rows should be in the bucket */
int64 remainder; /* (total rows) % (bucket num) */
} ntile_context;
static bool rank_up(WindowObject winobj);
static Datum leadlag_common(FunctionCallInfo fcinfo,
bool forward, bool withoffset, bool withdefault);
/*
* utility routine for *_rank functions.
*/
static bool
rank_up(WindowObject winobj)
{
bool up = false; /* should rank increase? */
int64 curpos = WinGetCurrentPosition(winobj);
rank_context *context;
context = (rank_context *)
WinGetPartitionLocalMemory(winobj, sizeof(rank_context));
if (context->rank == 0)
{
/* first call: rank of first row is always 1 */
Assert(curpos == 0);
context->rank = 1;
}
else
{
Assert(curpos > 0);
/* do current and prior tuples match by ORDER BY clause? */
if (!WinRowsArePeers(winobj, curpos - 1, curpos))
up = true;
}
/* We can advance the mark, but only *after* access to prior row */
WinSetMarkPosition(winobj, curpos);
return up;
}
/*
* row_number
* just increment up from 1 until current partition finishes.
*/
Datum
window_row_number(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
int64 curpos = WinGetCurrentPosition(winobj);
WinSetMarkPosition(winobj, curpos);
PG_RETURN_INT64(curpos + 1);
}
/*
* window_row_number_support
* prosupport function for window_row_number()
*/
Datum
window_row_number_support(PG_FUNCTION_ARGS)
{
Node *rawreq = (Node *) PG_GETARG_POINTER(0);
if (IsA(rawreq, SupportRequestWFuncMonotonic))
{
SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq;
/* row_number() is monotonically increasing */
req->monotonic = MONOTONICFUNC_INCREASING;
PG_RETURN_POINTER(req);
}
if (IsA(rawreq, SupportRequestOptimizeWindowClause))
{
SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq;
/*
* The frame options can always become "ROWS BETWEEN UNBOUNDED
* PRECEDING AND CURRENT ROW". row_number() always just increments by
* 1 with each row in the partition. Using ROWS instead of RANGE
* saves effort checking peer rows during execution.
*/
req->frameOptions = (FRAMEOPTION_NONDEFAULT |
FRAMEOPTION_ROWS |
FRAMEOPTION_START_UNBOUNDED_PRECEDING |
FRAMEOPTION_END_CURRENT_ROW);
PG_RETURN_POINTER(req);
}
PG_RETURN_POINTER(NULL);
}
/*
* rank
* Rank changes when key columns change.
* The new rank number is the current row number.
*/
Datum
window_rank(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
rank_context *context;
bool up;
up = rank_up(winobj);
context = (rank_context *)
WinGetPartitionLocalMemory(winobj, sizeof(rank_context));
if (up)
context->rank = WinGetCurrentPosition(winobj) + 1;
PG_RETURN_INT64(context->rank);
}
/*
* window_rank_support
* prosupport function for window_rank()
*/
Datum
window_rank_support(PG_FUNCTION_ARGS)
{
Node *rawreq = (Node *) PG_GETARG_POINTER(0);
if (IsA(rawreq, SupportRequestWFuncMonotonic))
{
SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq;
/* rank() is monotonically increasing */
req->monotonic = MONOTONICFUNC_INCREASING;
PG_RETURN_POINTER(req);
}
if (IsA(rawreq, SupportRequestOptimizeWindowClause))
{
SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq;
/*
* rank() is coded in such a way that it returns "(COUNT (*) OVER
* (<opt> RANGE UNBOUNDED PRECEDING) - COUNT (*) OVER (<opt> RANGE
* CURRENT ROW) + 1)" regardless of the frame options. We'll set the
* frame options to "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW"
* so they agree with what window_row_number_support() optimized the
* frame options to be. Using ROWS instead of RANGE saves from doing
* peer row checks during execution.
*/
req->frameOptions = (FRAMEOPTION_NONDEFAULT |
FRAMEOPTION_ROWS |
FRAMEOPTION_START_UNBOUNDED_PRECEDING |
FRAMEOPTION_END_CURRENT_ROW);
PG_RETURN_POINTER(req);
}
PG_RETURN_POINTER(NULL);
}
/*
* dense_rank
* Rank increases by 1 when key columns change.
*/
Datum
window_dense_rank(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
rank_context *context;
bool up;
up = rank_up(winobj);
context = (rank_context *)
WinGetPartitionLocalMemory(winobj, sizeof(rank_context));
if (up)
context->rank++;
PG_RETURN_INT64(context->rank);
}
/*
* window_dense_rank_support
* prosupport function for window_dense_rank()
*/
Datum
window_dense_rank_support(PG_FUNCTION_ARGS)
{
Node *rawreq = (Node *) PG_GETARG_POINTER(0);
if (IsA(rawreq, SupportRequestWFuncMonotonic))
{
SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq;
/* dense_rank() is monotonically increasing */
req->monotonic = MONOTONICFUNC_INCREASING;
PG_RETURN_POINTER(req);
}
if (IsA(rawreq, SupportRequestOptimizeWindowClause))
{
SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq;
/*
* dense_rank() is unaffected by the frame options. Here we set the
* frame options to match what's done in row_number's support
* function. Using ROWS instead of RANGE (the default) saves the
* executor from having to check for peer rows.
*/
req->frameOptions = (FRAMEOPTION_NONDEFAULT |
FRAMEOPTION_ROWS |
FRAMEOPTION_START_UNBOUNDED_PRECEDING |
FRAMEOPTION_END_CURRENT_ROW);
PG_RETURN_POINTER(req);
}
PG_RETURN_POINTER(NULL);
}
/*
* percent_rank
* return fraction between 0 and 1 inclusive,
* which is described as (RK - 1) / (NR - 1), where RK is the current row's
* rank and NR is the total number of rows, per spec.
*/
Datum
window_percent_rank(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
rank_context *context;
bool up;
int64 totalrows = WinGetPartitionRowCount(winobj);
Assert(totalrows > 0);
up = rank_up(winobj);
context = (rank_context *)
WinGetPartitionLocalMemory(winobj, sizeof(rank_context));
if (up)
context->rank = WinGetCurrentPosition(winobj) + 1;
/* return zero if there's only one row, per spec */
if (totalrows <= 1)
PG_RETURN_FLOAT8(0.0);
PG_RETURN_FLOAT8((float8) (context->rank - 1) / (float8) (totalrows - 1));
}
/*
* window_percent_rank_support
* prosupport function for window_percent_rank()
*/
Datum
window_percent_rank_support(PG_FUNCTION_ARGS)
{
Node *rawreq = (Node *) PG_GETARG_POINTER(0);
if (IsA(rawreq, SupportRequestWFuncMonotonic))
{
SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq;
/* percent_rank() is monotonically increasing */
req->monotonic = MONOTONICFUNC_INCREASING;
PG_RETURN_POINTER(req);
}
if (IsA(rawreq, SupportRequestOptimizeWindowClause))
{
SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq;
/*
* percent_rank() is unaffected by the frame options. Here we set the
* frame options to match what's done in row_number's support
* function. Using ROWS instead of RANGE (the default) saves the
* executor from having to check for peer rows.
*/
req->frameOptions = (FRAMEOPTION_NONDEFAULT |
FRAMEOPTION_ROWS |
FRAMEOPTION_START_UNBOUNDED_PRECEDING |
FRAMEOPTION_END_CURRENT_ROW);
PG_RETURN_POINTER(req);
}
PG_RETURN_POINTER(NULL);
}
/*
* cume_dist
* return fraction between 0 and 1 inclusive,
* which is described as NP / NR, where NP is the number of rows preceding or
* peers to the current row, and NR is the total number of rows, per spec.
*/
Datum
window_cume_dist(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
rank_context *context;
bool up;
int64 totalrows = WinGetPartitionRowCount(winobj);
Assert(totalrows > 0);
up = rank_up(winobj);
context = (rank_context *)
WinGetPartitionLocalMemory(winobj, sizeof(rank_context));
if (up || context->rank == 1)
{
/*
* The current row is not peer to prior row or is just the first, so
* count up the number of rows that are peer to the current.
*/
int64 row;
context->rank = WinGetCurrentPosition(winobj) + 1;
/*
* start from current + 1
*/
for (row = context->rank; row < totalrows; row++)
{
if (!WinRowsArePeers(winobj, row - 1, row))
break;
context->rank++;
}
}
PG_RETURN_FLOAT8((float8) context->rank / (float8) totalrows);
}
/*
* window_cume_dist_support
* prosupport function for window_cume_dist()
*/
Datum
window_cume_dist_support(PG_FUNCTION_ARGS)
{
Node *rawreq = (Node *) PG_GETARG_POINTER(0);
if (IsA(rawreq, SupportRequestWFuncMonotonic))
{
SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq;
/* cume_dist() is monotonically increasing */
req->monotonic = MONOTONICFUNC_INCREASING;
PG_RETURN_POINTER(req);
}
if (IsA(rawreq, SupportRequestOptimizeWindowClause))
{
SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq;
/*
* cume_dist() is unaffected by the frame options. Here we set the
* frame options to match what's done in row_number's support
* function. Using ROWS instead of RANGE (the default) saves the
* executor from having to check for peer rows.
*/
req->frameOptions = (FRAMEOPTION_NONDEFAULT |
FRAMEOPTION_ROWS |
FRAMEOPTION_START_UNBOUNDED_PRECEDING |
FRAMEOPTION_END_CURRENT_ROW);
PG_RETURN_POINTER(req);
}
PG_RETURN_POINTER(NULL);
}
/*
* ntile
* compute an exact numeric value with scale 0 (zero),
* ranging from 1 (one) to n, per spec.
*/
Datum
window_ntile(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
ntile_context *context;
context = (ntile_context *)
WinGetPartitionLocalMemory(winobj, sizeof(ntile_context));
if (context->ntile == 0)
{
/* first call */
int64 total;
int32 nbuckets;
bool isnull;
total = WinGetPartitionRowCount(winobj);
nbuckets = DatumGetInt32(WinGetFuncArgCurrent(winobj, 0, &isnull));
/*
* per spec: If NT is the null value, then the result is the null
* value.
*/
if (isnull)
PG_RETURN_NULL();
/*
* per spec: If NT is less than or equal to 0 (zero), then an
* exception condition is raised.
*/
if (nbuckets <= 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_ARGUMENT_FOR_NTILE),
errmsg("argument of ntile must be greater than zero")));
context->ntile = 1;
context->rows_per_bucket = 0;
context->boundary = total / nbuckets;
if (context->boundary <= 0)
context->boundary = 1;
else
{
/*
* If the total number is not divisible, add 1 row to leading
* buckets.
*/
context->remainder = total % nbuckets;
if (context->remainder != 0)
context->boundary++;
}
}
context->rows_per_bucket++;
if (context->boundary < context->rows_per_bucket)
{
/* ntile up */
if (context->remainder != 0 && context->ntile == context->remainder)
{
context->remainder = 0;
context->boundary -= 1;
}
context->ntile += 1;
context->rows_per_bucket = 1;
}
PG_RETURN_INT32(context->ntile);
}
/*
* window_ntile_support
* prosupport function for window_ntile()
*/
Datum
window_ntile_support(PG_FUNCTION_ARGS)
{
Node *rawreq = (Node *) PG_GETARG_POINTER(0);
if (IsA(rawreq, SupportRequestWFuncMonotonic))
{
SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq;
/*
* ntile() is monotonically increasing as the number of buckets cannot
* change after the first call
*/
req->monotonic = MONOTONICFUNC_INCREASING;
PG_RETURN_POINTER(req);
}
if (IsA(rawreq, SupportRequestOptimizeWindowClause))
{
SupportRequestOptimizeWindowClause *req = (SupportRequestOptimizeWindowClause *) rawreq;
/*
* ntile() is unaffected by the frame options. Here we set the frame
* options to match what's done in row_number's support function.
* Using ROWS instead of RANGE (the default) saves the executor from
* having to check for peer rows.
*/
req->frameOptions = (FRAMEOPTION_NONDEFAULT |
FRAMEOPTION_ROWS |
FRAMEOPTION_START_UNBOUNDED_PRECEDING |
FRAMEOPTION_END_CURRENT_ROW);
PG_RETURN_POINTER(req);
}
PG_RETURN_POINTER(NULL);
}
/*
* leadlag_common
* common operation of lead() and lag()
* For lead() forward is true, whereas for lag() it is false.
* withoffset indicates we have an offset second argument.
* withdefault indicates we have a default third argument.
*/
static Datum
leadlag_common(FunctionCallInfo fcinfo,
bool forward, bool withoffset, bool withdefault)
{
WindowObject winobj = PG_WINDOW_OBJECT();
int32 offset;
bool const_offset;
Datum result;
bool isnull;
bool isout;
if (withoffset)
{
offset = DatumGetInt32(WinGetFuncArgCurrent(winobj, 1, &isnull));
if (isnull)
PG_RETURN_NULL();
const_offset = get_fn_expr_arg_stable(fcinfo->flinfo, 1);
}
else
{
offset = 1;
const_offset = true;
}
result = WinGetFuncArgInPartition(winobj, 0,
(forward ? offset : -offset),
WINDOW_SEEK_CURRENT,
const_offset,
&isnull, &isout);
if (isout)
{
/*
* target row is out of the partition; supply default value if
* provided. otherwise it'll stay NULL
*/
if (withdefault)
result = WinGetFuncArgCurrent(winobj, 2, &isnull);
}
if (isnull)
PG_RETURN_NULL();
PG_RETURN_DATUM(result);
}
/*
* lag
* returns the value of VE evaluated on a row that is 1
* row before the current row within a partition,
* per spec.
*/
Datum
window_lag(PG_FUNCTION_ARGS)
{
return leadlag_common(fcinfo, false, false, false);
}
/*
* lag_with_offset
* returns the value of VE evaluated on a row that is OFFSET
* rows before the current row within a partition,
* per spec.
*/
Datum
window_lag_with_offset(PG_FUNCTION_ARGS)
{
return leadlag_common(fcinfo, false, true, false);
}
/*
* lag_with_offset_and_default
* same as lag_with_offset but accepts default value
* as its third argument.
*/
Datum
window_lag_with_offset_and_default(PG_FUNCTION_ARGS)
{
return leadlag_common(fcinfo, false, true, true);
}
/*
* lead
* returns the value of VE evaluated on a row that is 1
* row after the current row within a partition,
* per spec.
*/
Datum
window_lead(PG_FUNCTION_ARGS)
{
return leadlag_common(fcinfo, true, false, false);
}
/*
* lead_with_offset
* returns the value of VE evaluated on a row that is OFFSET
* number of rows after the current row within a partition,
* per spec.
*/
Datum
window_lead_with_offset(PG_FUNCTION_ARGS)
{
return leadlag_common(fcinfo, true, true, false);
}
/*
* lead_with_offset_and_default
* same as lead_with_offset but accepts default value
* as its third argument.
*/
Datum
window_lead_with_offset_and_default(PG_FUNCTION_ARGS)
{
return leadlag_common(fcinfo, true, true, true);
}
/*
* first_value
* return the value of VE evaluated on the first row of the
* window frame, per spec.
*/
Datum
window_first_value(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
Datum result;
bool isnull;
result = WinGetFuncArgInFrame(winobj, 0,
0, WINDOW_SEEK_HEAD, true,
&isnull, NULL);
if (isnull)
PG_RETURN_NULL();
PG_RETURN_DATUM(result);
}
/*
* last_value
* return the value of VE evaluated on the last row of the
* window frame, per spec.
*/
Datum
window_last_value(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
Datum result;
bool isnull;
result = WinGetFuncArgInFrame(winobj, 0,
0, WINDOW_SEEK_TAIL, true,
&isnull, NULL);
if (isnull)
PG_RETURN_NULL();
PG_RETURN_DATUM(result);
}
/*
* nth_value
* return the value of VE evaluated on the n-th row from the first
* row of the window frame, per spec.
*/
Datum
window_nth_value(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
bool const_offset;
Datum result;
bool isnull;
int32 nth;
nth = DatumGetInt32(WinGetFuncArgCurrent(winobj, 1, &isnull));
if (isnull)
PG_RETURN_NULL();
const_offset = get_fn_expr_arg_stable(fcinfo->flinfo, 1);
if (nth <= 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_ARGUMENT_FOR_NTH_VALUE),
errmsg("argument of nth_value must be greater than zero")));
result = WinGetFuncArgInFrame(winobj, 0,
nth - 1, WINDOW_SEEK_HEAD, const_offset,
&isnull, NULL);
if (isnull)
PG_RETURN_NULL();
PG_RETURN_DATUM(result);
}
|