forked from dimitri/PostgreSQL-Temporal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemporal.c
More file actions
1369 lines (1167 loc) · 32.8 KB
/
temporal.c
File metadata and controls
1369 lines (1167 loc) · 32.8 KB
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* period.c
* Implements the PERIOD data type.
*
* Portions Copyright (c) 2007-2008, Jeff Davis <jeff@j-davis.com>
*/
#include "period.h"
PG_MODULE_MAGIC;
/* only used when HAVE_INT64_TIMESTAMP is not defined */
#define DOUBLE_INF ((double)(1.0/0.0))
/* larger than largest valid period input */
#define MAX_REPR_SIZE 100
/* support functions */
static TimestampTz period_length(period *p);
static period *period_dup(period *src);
static period *period_copy(period *src, period *dst);
static bool period_equals(period *p1, period *p2);
static bool period_is_empty(period *p);
static TimestampTz prior_timestamptz(TimestampTz ts);
static TimestampTz next_timestamptz(TimestampTz ts);
static bool period_adjacent(period *p1, period *p2);
static bool period_contains(period *p1, period *p2);
static bool period_overlaps(period *p1, period *p2);
static bool period_overleft(period *p1, period *p2);
static bool period_overright(period *p1, period *p2);
static bool period_before(period *p1, period *p2);
static period *period_empty_period(period *result);
static period *period_minus(period *p1, period *p2, period *result);
static period *period_intersect(period *p1, period *p2, period *result);
static period *period_union(period *p1, period *p2, period *result, bool greedy);
static bool gist_period_int_consistent(period *p, period *query,
StrategyNumber strategy);
static bool gist_period_leaf_consistent(period *p, period *query,
StrategyNumber strategy);
static int period_compare(period *p1, period *p2);
static bool period_lessthan_timestamptz(period *p, TimestampTz ts); /* djg */
static bool period_greaterthan_timestamptz(period *p, TimestampTz ts); /* djg */
static float period_size_approx(period *p);
static float period_penalty(period *orig, period *new);
static period *period_dup(period *src)
{
period *dst;
dst = (period*) palloc(sizeof(period));
return period_copy(src, dst);
}
static period *period_copy(period *src, period *dst)
{
dst->first = src->first;
dst->next = src->next;
return dst;
}
/*
* These states are for the parsing of
* the period input.
*/
#define STATE_INIT 0
#define STATE_STR1 1
#define STATE_STR2 2
#define STATE_DONE 3
PG_FUNCTION_INFO_V1(period_in);
Datum
period_in(PG_FUNCTION_ARGS)
{
char *str = PG_GETARG_CSTRING(0);
period *result;
char str1[MAX_REPR_SIZE];
char str2[MAX_REPR_SIZE];
int i, len1=0, len2=0;
int state=0;
bool empty=false;
bool first_inc=false, second_inc=false;
TimestampTz ts1, ts2;
result = (period*) palloc(sizeof(period));
/* parse input so that we have:
* str1 and str2, and whether each should be interpreted as
* inclusive or exclusive, based on '[' or ')', respectively.
*/
for(i=0; str[i] && i < MAX_REPR_SIZE - 1; i++) {
if(state == STATE_INIT) {
switch(str[i]) {
case ' ':
continue;
case '-':
empty = true;
state = STATE_DONE;
continue;
case '[':
state = STATE_STR1;
first_inc = true;
continue;
case '(':
state = STATE_STR1;
first_inc = false;
continue;
default:
elog(ERROR,"Invalid period input");
}
}
else if(state == STATE_STR1) {
if(str[i] == ',') {
state = STATE_STR2;
continue;
}
str1[len1++] = str[i];
continue;
}
else if(state == STATE_STR2) {
if(str[i] == ']') {
second_inc = true;
state = STATE_DONE;
break;
}
if(str[i] == ')') {
second_inc = false;
state = STATE_DONE;
break;
}
str2[len2++] = str[i];
continue;
}
else if(state == STATE_DONE)
break;
}
str1[len1] = '\0';
str2[len2] = '\0';
if(state != STATE_DONE) {
elog(ERROR,"invalid period input: parse error");
}
if(empty) {
result->first = (TimestampTz)0;
result->next = (TimestampTz)0;
PG_RETURN_POINTER(result);
}
ts1 = DatumGetTimestampTz(DirectFunctionCall3(
timestamptz_in,CStringGetDatum(str1),
ObjectIdGetDatum(InvalidOid),Int32GetDatum(-1)));
ts2 = DatumGetTimestampTz(DirectFunctionCall3(
timestamptz_in,CStringGetDatum(str2),
ObjectIdGetDatum(InvalidOid),Int32GetDatum(-1)));
/* interpret inclusive/exclusive notation */
if(first_inc)
result->first = ts1;
else
result->first = next_timestamptz(ts1);
if(second_inc)
result->next = next_timestamptz(ts2);
else
result->next = ts2;
if(result->first >= result->next)
elog(ERROR,"invalid period: first > last");
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(period_out);
Datum
period_out(PG_FUNCTION_ARGS)
{
period *p = (period*)PG_GETARG_POINTER(0);
char *result;
char *ts1,*ts2;
result = (char*) palloc(MAX_REPR_SIZE);
if(period_is_empty(p))
snprintf(result,MAX_REPR_SIZE,"-EMPTY-");
else {
ts1 = DatumGetCString(DirectFunctionCall1(timestamptz_out,
TimestampTzGetDatum(p->first)));
ts2 = DatumGetCString(DirectFunctionCall1(timestamptz_out,
TimestampTzGetDatum(p->next)));
snprintf(result,MAX_REPR_SIZE,"[%s, %s)",ts1,ts2);
}
PG_RETURN_CSTRING(result);
}
PG_FUNCTION_INFO_V1(period_oo_timestamptz_timestamptz);
Datum
period_oo_timestamptz_timestamptz(PG_FUNCTION_ARGS)
{
period *result;
result = (period*)palloc(sizeof(period));
result->first = next_timestamptz(PG_GETARG_TIMESTAMPTZ(0));
result->next = PG_GETARG_TIMESTAMPTZ(1);
if(result->first > result->next)
elog(ERROR,"invalid period: first > last");
if(result->first == result->next) {
result->first = 0;
result->next = 0;
}
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(period_oc_timestamptz_timestamptz);
Datum
period_oc_timestamptz_timestamptz(PG_FUNCTION_ARGS)
{
period *result;
result = (period*)palloc(sizeof(period));
result->first = next_timestamptz(PG_GETARG_TIMESTAMPTZ(0));
result->next = next_timestamptz(PG_GETARG_TIMESTAMPTZ(1));
if(result->first > result->next)
elog(ERROR,"invalid period: first > last");
if(result->first == result->next) {
result->first = 0;
result->next = 0;
}
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(period_co_timestamptz_timestamptz);
Datum
period_co_timestamptz_timestamptz(PG_FUNCTION_ARGS)
{
period *result;
result = (period*)palloc(sizeof(period));
result->first = PG_GETARG_TIMESTAMPTZ(0);
result->next = PG_GETARG_TIMESTAMPTZ(1);
if(result->first > result->next)
elog(ERROR,"invalid period: first > last");
if(result->first == result->next) {
result->first = 0;
result->next = 0;
}
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(period_cc_timestamptz_timestamptz);
Datum
period_cc_timestamptz_timestamptz(PG_FUNCTION_ARGS)
{
period *result;
result = (period*)palloc(sizeof(period));
result->first = PG_GETARG_TIMESTAMPTZ(0);
result->next = next_timestamptz(PG_GETARG_TIMESTAMPTZ(1));
if(result->first > result->next)
elog(ERROR,"invalid period: first > last");
if(result->first == result->next) {
result->first = 0;
result->next = 0;
}
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(period_timestamptz);
Datum
period_timestamptz(PG_FUNCTION_ARGS)
{
period *result;
result = (period*)palloc(sizeof(period));
result->first = PG_GETARG_TIMESTAMPTZ(0);
result->next = next_timestamptz(result->first);
if(result->first == result->next) {
result->first = 0;
result->next = 0;
}
PG_RETURN_POINTER(result);
}
/*
* SQL INTERVAL functions
*/
PG_FUNCTION_INFO_V1(length_period);
Datum
length_period(PG_FUNCTION_ARGS)
{
period *p = (period*)PG_GETARG_POINTER(0);
Interval *sql_interval = (Interval*)palloc(sizeof(Interval));
memset(sql_interval, 0, sizeof(Interval));
sql_interval->time = period_length(p);
PG_RETURN_INTERVAL_P(sql_interval);
}
PG_FUNCTION_INFO_V1(period_offset_period_timestamptz);
Datum
period_offset_period_timestamptz(PG_FUNCTION_ARGS)
{
period *p = (period*)PG_GETARG_POINTER(0);
TimestampTz ts = PG_GETARG_TIMESTAMPTZ(1);
Interval *sql_interval = (Interval*)palloc(sizeof(Interval));
memset(sql_interval, 0, sizeof(Interval));
if (p->first > ts || ts >= p->next)
elog(ERROR, "input time must be contained in the given period");
sql_interval->time = ts - p->first;
PG_RETURN_INTERVAL_P(sql_interval);
}
PG_FUNCTION_INFO_V1(period_offset_sec_period_timestamptz);
Datum
period_offset_sec_period_timestamptz(PG_FUNCTION_ARGS)
{
period *p = (period*)PG_GETARG_POINTER(0);
TimestampTz ts = PG_GETARG_TIMESTAMPTZ(1);
int64 sec;
if (p->first > ts || ts >= p->next)
elog(ERROR, "input time must be contained in the given period");
/* using integer division, so the time will be truncated to the second */
sec = (ts - p->first) / 1000000;
PG_RETURN_INT64(sec);
}
/*
* TimestampTz functions
*/
PG_FUNCTION_INFO_V1(first_period);
Datum
first_period(PG_FUNCTION_ARGS)
{
period *p = (period*)PG_GETARG_POINTER(0);
if(period_is_empty(p))
elog(ERROR,"Interval is empty");
PG_RETURN_TIMESTAMPTZ(p->first);
}
PG_FUNCTION_INFO_V1(last_period);
Datum
last_period(PG_FUNCTION_ARGS)
{
period *p = (period*)PG_GETARG_POINTER(0);
if(period_is_empty(p))
elog(ERROR,"Interval is empty");
PG_RETURN_TIMESTAMPTZ(prior_timestamptz(p->next));
}
PG_FUNCTION_INFO_V1(next_period);
Datum
next_period(PG_FUNCTION_ARGS)
{
period *p = (period*)PG_GETARG_POINTER(0);
if(period_is_empty(p))
elog(ERROR,"Interval is empty");
PG_RETURN_TIMESTAMPTZ(p->next);
}
PG_FUNCTION_INFO_V1(prior_period);
Datum
prior_period(PG_FUNCTION_ARGS)
{
period *p = (period*)PG_GETARG_POINTER(0);
if(period_is_empty(p))
elog(ERROR,"Interval is empty");
PG_RETURN_TIMESTAMPTZ(prior_timestamptz(p->first));
}
/*
* BOOLEAN functions
*/
PG_FUNCTION_INFO_V1(equals_period_period);
Datum
equals_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_equals(p1,p2));
}
PG_FUNCTION_INFO_V1(nequals_period_period);
Datum
nequals_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(!period_equals(p1,p2));
}
PG_FUNCTION_INFO_V1(lessthan_period_period);
Datum
lessthan_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_compare(p1, p2) < 0);
}
PG_FUNCTION_INFO_V1(lessthanequals_period_period);
Datum
lessthanequals_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_compare(p1, p2) <= 0);
}
PG_FUNCTION_INFO_V1(greaterthan_period_period);
Datum
greaterthan_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_compare(p1, p2) > 0);
}
PG_FUNCTION_INFO_V1(greaterthanequals_period_period);
Datum
greaterthanequals_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_compare(p1, p2) >= 0);
}
PG_FUNCTION_INFO_V1(lessthan_period_timestamptz);
Datum
lessthan_period_timestamptz(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
TimestampTz arg = PG_GETARG_TIMESTAMPTZ(1);
PG_RETURN_BOOL(period_lessthan_timestamptz(p1, arg));
}
PG_FUNCTION_INFO_V1(greaterthan_period_timestamptz);
Datum
greaterthan_period_timestamptz(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
TimestampTz arg = PG_GETARG_TIMESTAMPTZ(1);
PG_RETURN_BOOL(period_greaterthan_timestamptz(p1, arg));
}
PG_FUNCTION_INFO_V1(lessthan_timestamptz_period);
Datum
lessthan_timestamptz_period(PG_FUNCTION_ARGS)
{
TimestampTz arg = PG_GETARG_TIMESTAMPTZ(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_greaterthan_timestamptz(p2, arg)); /* Implement timestamp < period as period > timestamp instead */
}
PG_FUNCTION_INFO_V1(greaterthan_timestamptz_period);
Datum
greaterthan_timestamptz_period(PG_FUNCTION_ARGS)
{
TimestampTz arg = PG_GETARG_TIMESTAMPTZ(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_lessthan_timestamptz(p2, arg)); /* Implement timestamp > period as period < timestamp instead */
}
PG_FUNCTION_INFO_V1(before_period_period);
Datum
before_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_before(p1,p2));
}
PG_FUNCTION_INFO_V1(after_period_period);
Datum
after_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_before(p2,p1));
}
PG_FUNCTION_INFO_V1(adjacent_period_period);
Datum
adjacent_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_adjacent(p1,p2));
}
PG_FUNCTION_INFO_V1(contains_period_timestamptz);
Datum
contains_period_timestamptz(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
TimestampTz arg = PG_GETARG_TIMESTAMPTZ(1);
period p2;
p2.first = arg;
p2.next = next_timestamptz(arg);
PG_RETURN_BOOL(period_contains(p1,&p2));
}
PG_FUNCTION_INFO_V1(contains_period_period);
Datum
contains_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_contains(p1,p2));
}
PG_FUNCTION_INFO_V1(contained_by_timestamptz_period);
Datum
contained_by_timestamptz_period(PG_FUNCTION_ARGS)
{
TimestampTz arg = PG_GETARG_TIMESTAMPTZ(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
period p1;
p1.first = arg;
p1.next = next_timestamptz(arg);
PG_RETURN_BOOL(period_contains(p2,&p1));
}
PG_FUNCTION_INFO_V1(contained_by_period_period);
Datum contained_by_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_contains(p2,p1));
}
PG_FUNCTION_INFO_V1(overlaps_period_period);
Datum
overlaps_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_overlaps(p1,p2));
}
PG_FUNCTION_INFO_V1(overleft_period_period);
Datum
overleft_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_overleft(p1,p2));
}
PG_FUNCTION_INFO_V1(overright_period_period);
Datum
overright_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(period_overright(p1,p2));
}
PG_FUNCTION_INFO_V1(is_empty_period);
Datum
is_empty_period(PG_FUNCTION_ARGS)
{
period *p = (period*)PG_GETARG_POINTER(0);
PG_RETURN_BOOL(period_is_empty(p));
}
/*
* period functions
*/
PG_FUNCTION_INFO_V1(empty_period);
Datum
empty_period(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(period_empty_period(NULL));
}
PG_FUNCTION_INFO_V1(intersect_period_period);
Datum
intersect_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_POINTER(period_intersect(p1,p2,NULL));
}
PG_FUNCTION_INFO_V1(union_period_period);
Datum
union_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_POINTER(period_union(p1,p2,NULL,false));
}
PG_FUNCTION_INFO_V1(minus_period_period);
Datum
minus_period_period(PG_FUNCTION_ARGS)
{
period *p1 = (period*)PG_GETARG_POINTER(0);
period *p2 = (period*)PG_GETARG_POINTER(1);
PG_RETURN_POINTER(period_minus(p1,p2,NULL));
}
/***********************************************
* GiST Support Functions
***********************************************/
#if 1
#define GISTENTRYCOUNT(v) ((v)->n)
#define GISTENTRYVEC(v) ((v)->vector)
#else /* IP4R_PGVER < 8000000 */
typedef bytea GistEntryVector;
#define GISTENTRYCOUNT(v) ( (VARSIZE((v)) - VARHDRSZ) / sizeof(GISTENTRY) )
#define GISTENTRYVEC(v) ((GISTENTRY *) VARDATA(entryvec))
#endif
PG_FUNCTION_INFO_V1(gist_period_consistent);
Datum
gist_period_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY*) PG_GETARG_POINTER(0);
period *key = (period*) DatumGetPointer(entry->key);
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
period query;
period * period_query;
TimestampTz t_point_query;
switch(strategy) {
case 27: //contains(period,t_point)
case 28:
// convert t_point to a period for query
t_point_query = PG_GETARG_TIMESTAMPTZ(1);
query.first = t_point_query;
query.next = next_timestamptz(t_point_query);
break;
default:
period_query = (period*)PG_GETARG_POINTER(1);
query = *period_query;
}
if(GIST_LEAF(entry))
PG_RETURN_BOOL(gist_period_leaf_consistent(key, &query, strategy));
else
PG_RETURN_BOOL(gist_period_int_consistent(key, &query, strategy));
}
PG_FUNCTION_INFO_V1(gist_period_union);
Datum
gist_period_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entries = (GistEntryVector*) PG_GETARG_POINTER(0);
int *size = (int*) PG_GETARG_POINTER(1);
period *result = (period*) palloc(sizeof(period));
period *tmp_period = NULL;
int i;
result->first = result->next = 0;
for(i = 0; i < entries->n; i++) {
tmp_period = (period*)DatumGetPointer(entries->vector[i].key);
if(period_is_empty(result)) {
result->first = tmp_period->first;
result->next = tmp_period->next;
}
else {
result->first = (result->first < tmp_period->first) ?
result->first : tmp_period->first;
result->next = (result->next > tmp_period->next) ?
result->next : tmp_period->next;
}
}
*size = sizeof(period);
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(gist_period_compress);
Datum
gist_period_compress(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
}
PG_FUNCTION_INFO_V1(gist_period_decompress);
Datum
gist_period_decompress(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
}
PG_FUNCTION_INFO_V1(gist_period_penalty);
Datum
gist_period_penalty(PG_FUNCTION_ARGS)
{
period *orig = (period*) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
period *new = (period*) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *penalty = (float *) PG_GETARG_POINTER(2);
return period_penalty(orig, new);
PG_RETURN_POINTER(penalty);
}
float period_size_approx(period *p)
{
/* Divide values by 10ish to avoid overflow in the case of infinity */
return (p->next/10) - (p->first/10);
}
float period_penalty(period *orig, period *new)
{
period union_p;
period_union(orig, new, &union_p, true);
return period_size_approx(&union_p) - period_size_approx(orig);
}
struct period_gist_sort
{
period *key;
OffsetNumber pos;
};
/*
* This is only used for the picksplit algorithm, so it's OK to use
* approximates.
*/
static int
period_gist_sort_compare(const void *a, const void *b)
{
float sa = period_size_approx(((struct period_gist_sort *)a)->key);
float sb = period_size_approx(((struct period_gist_sort *)b)->key);
return (sa > sb) ? 1 : ((sa == sb) ? 0 : -1);
}
/*
* Code was copied from ip4r module:
* http://pgfoundry.org/projects/ip4r/
* Please see that project for details.
*/
PG_FUNCTION_INFO_V1(gist_period_picksplit);
Datum
gist_period_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
GISTENTRY *ent = GISTENTRYVEC(entryvec);
OffsetNumber i;
int nbytes;
OffsetNumber maxoff;
OffsetNumber *listL;
OffsetNumber *listR;
bool allisequal = true;
period pageunion;
period *cur;
period *unionL;
period *unionR;
int posL = 0;
int posR = 0;
posL = posR = 0;
maxoff = GISTENTRYCOUNT(entryvec) - 1;
cur = (period *) DatumGetPointer(ent[FirstOffsetNumber].key);
pageunion = *cur;
/* find MBR */
for (i = OffsetNumberNext(FirstOffsetNumber); i <= maxoff; i = OffsetNumberNext(i))
{
cur = (period *) DatumGetPointer(ent[i].key);
if (allisequal == true
&& (pageunion.first != cur->first || pageunion.next != cur->next))
allisequal = false;
if (cur->first < pageunion.first)
pageunion.first = cur->first;
if (cur->next > pageunion.next)
pageunion.next = cur->next;
}
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
listL = (OffsetNumber *) palloc(nbytes);
listR = (OffsetNumber *) palloc(nbytes);
unionL = (period *) palloc(sizeof(period));
unionR = (period *) palloc(sizeof(period));
v->spl_ldatum = PointerGetDatum(unionL);
v->spl_rdatum = PointerGetDatum(unionR);
v->spl_left = listL;
v->spl_right = listR;
if (allisequal)
{
cur = (period *) DatumGetPointer(ent[OffsetNumberNext(FirstOffsetNumber)].key);
if (period_equals(cur, &pageunion))
{
OffsetNumber split_at = FirstOffsetNumber + (maxoff - FirstOffsetNumber + 1)/2;
v->spl_nleft = v->spl_nright = 0;
*unionL = pageunion;
*unionR = pageunion;
for (i = FirstOffsetNumber; i < split_at; i = OffsetNumberNext(i))
v->spl_left[v->spl_nleft++] = i;
for (; i <= maxoff; i = OffsetNumberNext(i))
v->spl_right[v->spl_nright++] = i;
PG_RETURN_POINTER(v);
}
}
#define ADDLIST( list_, u_, pos_, num_ ) do { \
if ( pos_ ) { \
if ( (u_)->next < (cur)->next ) (u_)->next = (cur)->next; \
if ( (u_)->first > (cur)->first ) (u_)->first = (cur)->first; \
} else { \
*(u_) = *(cur); \
} \
(list_)[(pos_)++] = (num_); \
} while(0)
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
cur = (period *) DatumGetPointer(ent[i].key);
if (cur->first - pageunion.first < pageunion.next - cur->next)
ADDLIST(listL, unionL, posL, i);
else
ADDLIST(listR, unionR, posR, i);
}
/* bad disposition, sort by ascending size and resplit */
if (posR == 0 || posL == 0)
{
struct period_gist_sort *arr = (struct period_gist_sort *) palloc(sizeof(struct period_gist_sort) * (maxoff + FirstOffsetNumber));
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
arr[i].key = (period *) DatumGetPointer(ent[i].key);
arr[i].pos = i;
}
qsort(arr + FirstOffsetNumber,
maxoff - FirstOffsetNumber + 1,
sizeof(struct period_gist_sort),
period_gist_sort_compare);
posL = posR = 0;
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
cur = arr[i].key;
if (cur->first - pageunion.first < pageunion.next - cur->next)
ADDLIST(listL, unionL, posL, arr[i].pos);
else if (cur->first - pageunion.first == pageunion.next - cur->next)
{
if (posL > posR)
ADDLIST(listR, unionR, posR, arr[i].pos);
else
ADDLIST(listL, unionL, posL, arr[i].pos);
}
else
ADDLIST(listR, unionR, posR, arr[i].pos);
}
pfree(arr);
}
v->spl_nleft = posL;
v->spl_nright = posR;
PG_RETURN_POINTER(v);
#if 0
GistEntryVector *entryvec = (GistEntryVector*) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC*) PG_GETARG_POINTER(1);
OffsetNumber i, j;
period *datum_alpha, *datum_beta;
period datum_l, datum_r;
period union_dl, union_dr;
bool firsttime;
float size_alpha,
size_beta,
size_union,
size_inter;
float size_waste,
waste;
float size_l,
size_r;
int nbytes;
OffsetNumber seed_1 = 1,
seed_2 = 2;
OffsetNumber *left,
*right;
OffsetNumber maxoff;
#ifdef GIST_DEBUG
fprintf(stderr, "picksplit\n");
#endif
maxoff = entryvec->n - 2;
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
v->spl_left = (OffsetNumber *) palloc(nbytes);
v->spl_right = (OffsetNumber *) palloc(nbytes);
firsttime = true;
waste = 0.0;
for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
{
datum_alpha = (period *) DatumGetPointer(entryvec->vector[i].key);
for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
{
datum_beta = (period *) DatumGetPointer(entryvec->vector[j].key);
/* compute the wasted space by unioning these guys */
/* size_waste = size_union - size_inter; */
size_waste = period_penalty(datum_alpha, datum_beta);
/*
* are these a more promising split that what we've already seen?
*/
if (size_waste > waste || firsttime)
{
waste = size_waste;
seed_1 = i;
seed_2 = j;
firsttime = false;
}
}
}
left = v->spl_left;
v->spl_nleft = 0;
right = v->spl_right;
v->spl_nright = 0;
datum_alpha = (period *) DatumGetPointer(entryvec->vector[seed_1].key);
period_copy(datum_alpha, &datum_l);
size_l = period_size_approx(&datum_l);
datum_beta = (period *) DatumGetPointer(entryvec->vector[seed_2].key);
period_copy(datum_beta, &datum_r);
size_r = period_size_approx(&datum_r);
/*
* Now split up the regions between the two seeds. An important property
* of this split algorithm is that the split vector v has the indices of
* items to be split in order in its left and right vectors. We exploit
* this property by doing a merge in the code that actually splits the
* page.
*
* For efficiency, we also place the new index tuple in this loop. This is
* handled at the very end, when we have placed all the existing tuples
* and i == maxoff + 1.
*/
maxoff = OffsetNumberNext(maxoff);
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
/*
* If we've already decided where to place this item, just put it on
* the right list. Otherwise, we need to figure out which page needs