PostgreSQL Source Code: src/backend/utils/adt/windowfuncs.c Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
15
18#include "utils/fmgrprotos.h"
20
21
22
23
25{
28
29
30
31
32typedef struct
33{
39
42 bool forward, bool withoffset, bool withdefault);
43
44
45
46
47
48static bool
50{
51 bool up = false;
54
57
58 if (context->rank == 0)
59 {
60
62 context->rank = 1;
63 }
64 else
65 {
67
69 up = true;
70 }
71
72
74
75 return up;
76}
77
78
79
80
81
82
85{
88
91}
92
93
94
95
96
99{
101
103 {
105
106
109 }
110
112 {
114
115
116
117
118
119
120
125
127 }
128
130}
131
132
133
134
135
136
139{
142 bool up;
143
147 if (up)
149
151}
152
153
154
155
156
159{
161
163 {
165
166
169 }
170
172 {
174
175
176
177
178
179
180
181
182
183
188
190 }
191
193}
194
195
196
197
198
201{
204 bool up;
205
209 if (up)
210 context->rank++;
211
213}
214
215
216
217
218
221{
223
225 {
227
228
231 }
232
234 {
236
237
238
239
240
241
242
247
249 }
250
252}
253
254
255
256
257
258
259
262{
265 bool up;
267
268 Assert(totalrows > 0);
269
273 if (up)
275
276
277 if (totalrows <= 1)
279
281}
282
283
284
285
286
289{
291
293 {
295
296
299 }
300
302 {
304
305
306
307
308
309
310
315
317 }
318
320}
321
322
323
324
325
326
327
328
331{
334 bool up;
336
337 Assert(totalrows > 0);
338
342 if (up || context->rank == 1)
343 {
344
345
346
347
349
351
352
353
354
355 for (row = context->rank; row < totalrows; row++)
356 {
358 break;
359 context->rank++;
360 }
361 }
362
364}
365
366
367
368
369
372{
374
376 {
378
379
382 }
383
385 {
387
388
389
390
391
392
393
398
400 }
401
403}
404
405
406
407
408
409
412{
415
418
419 if (context->ntile == 0)
420 {
421
424 bool isnull;
425
428
429
430
431
432
433 if (isnull)
435
436
437
438
439
440 if (nbuckets <= 0)
442 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_NTILE),
443 errmsg("argument of ntile must be greater than zero")));
444
445 context->ntile = 1;
447 context->boundary = total / nbuckets;
450 else
451 {
452
453
454
455
456 context->remainder = total % nbuckets;
459 }
460 }
461
464 {
465
467 {
470 }
471 context->ntile += 1;
473 }
474
476}
477
478
479
480
481
484{
486
488 {
490
491
492
493
494
497 }
498
500 {
502
503
504
505
506
507
508
513
515 }
516
518}
519
520
521
522
523
524
525
526
529 bool forward, bool withoffset, bool withdefault)
530{
533 bool const_offset;
535 bool isnull;
536 bool isout;
537
538 if (withoffset)
539 {
541 if (isnull)
544 }
545 else
546 {
547 offset = 1;
548 const_offset = true;
549 }
550
552 (forward ? offset : -offset),
554 const_offset,
555 &isnull, &isout);
556
557 if (isout)
558 {
559
560
561
562
563 if (withdefault)
565 }
566
567 if (isnull)
569
571}
572
573
574
575
576
577
578
581{
583}
584
585
586
587
588
589
590
593{
595}
596
597
598
599
600
601
604{
606}
607
608
609
610
611
612
613
616{
618}
619
620
621
622
623
624
625
628{
630}
631
632
633
634
635
636
639{
641}
642
643
644
645
646
647
650{
653 bool isnull;
654
657 &isnull, NULL);
658 if (isnull)
660
662}
663
664
665
666
667
668
671{
674 bool isnull;
675
678 &isnull, NULL);
679 if (isnull)
681
683}
684
685
686
687
688
689
692{
694 bool const_offset;
696 bool isnull;
698
700 if (isnull)
703
704 if (nth <= 0)
706 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_NTH_VALUE),
707 errmsg("argument of nth_value must be greater than zero")));
708
711 &isnull, NULL);
712 if (isnull)
714
716}
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
#define PG_RETURN_FLOAT8(x)
#define PG_GETARG_POINTER(n)
#define PG_RETURN_INT64(x)
#define PG_RETURN_INT32(x)
#define PG_RETURN_DATUM(x)
#define PG_RETURN_POINTER(x)
Assert(PointerIsAligned(start, uint64))
Datum WinGetFuncArgInPartition(WindowObject winobj, int argno, int relpos, int seektype, bool set_mark, bool *isnull, bool *isout)
void * WinGetPartitionLocalMemory(WindowObject winobj, Size sz)
Datum WinGetFuncArgInFrame(WindowObject winobj, int argno, int relpos, int seektype, bool set_mark, bool *isnull, bool *isout)
int64 WinGetCurrentPosition(WindowObject winobj)
bool WinRowsArePeers(WindowObject winobj, int64 pos1, int64 pos2)
void WinSetMarkPosition(WindowObject winobj, int64 markpos)
Datum WinGetFuncArgCurrent(WindowObject winobj, int argno, bool *isnull)
int64 WinGetPartitionRowCount(WindowObject winobj)
#define IsA(nodeptr, _type_)
#define FRAMEOPTION_END_CURRENT_ROW
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
#define FRAMEOPTION_NONDEFAULT
@ MONOTONICFUNC_INCREASING
static int32 DatumGetInt32(Datum X)
MonotonicFunction monotonic
#define PG_WINDOW_OBJECT()
#define WINDOW_SEEK_CURRENT
Datum window_lag_with_offset(PG_FUNCTION_ARGS)
static bool rank_up(WindowObject winobj)
Datum window_dense_rank(PG_FUNCTION_ARGS)
Datum window_cume_dist_support(PG_FUNCTION_ARGS)
Datum window_last_value(PG_FUNCTION_ARGS)
Datum window_percent_rank(PG_FUNCTION_ARGS)
static Datum leadlag_common(FunctionCallInfo fcinfo, bool forward, bool withoffset, bool withdefault)
Datum window_ntile(PG_FUNCTION_ARGS)
Datum window_row_number(PG_FUNCTION_ARGS)
Datum window_percent_rank_support(PG_FUNCTION_ARGS)
Datum window_first_value(PG_FUNCTION_ARGS)
Datum window_lead(PG_FUNCTION_ARGS)
Datum window_lead_with_offset(PG_FUNCTION_ARGS)
Datum window_lag_with_offset_and_default(PG_FUNCTION_ARGS)
Datum window_lag(PG_FUNCTION_ARGS)
Datum window_row_number_support(PG_FUNCTION_ARGS)
Datum window_lead_with_offset_and_default(PG_FUNCTION_ARGS)
Datum window_cume_dist(PG_FUNCTION_ARGS)
Datum window_rank(PG_FUNCTION_ARGS)
Datum window_ntile_support(PG_FUNCTION_ARGS)
struct rank_context rank_context
Datum window_dense_rank_support(PG_FUNCTION_ARGS)
Datum window_nth_value(PG_FUNCTION_ARGS)
Datum window_rank_support(PG_FUNCTION_ARGS)