-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaterange_inclusive.c
58 lines (46 loc) · 1.36 KB
/
daterange_inclusive.c
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
#include "postgres.h"
#include "utils/rangetypes.h"
#include "utils/date.h"
#include "utils/fmgrprotos.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(daterange_inclusive_canonical);
Datum
daterange_inclusive_canonical(PG_FUNCTION_ARGS)
{
RangeType *r = PG_GETARG_RANGE_P(0);
Node *escontext = fcinfo->context;
TypeCacheEntry *typcache;
RangeBound lower;
RangeBound upper;
bool empty;
typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
range_deserialize(typcache, r, &lower, &upper, &empty);
if (empty)
PG_RETURN_RANGE_P(r);
if (!lower.infinite && !DATE_NOT_FINITE(DatumGetDateADT(lower.val)) &&
!lower.inclusive)
{
DateADT bnd = DatumGetDateADT(lower.val);
bnd++;
if (unlikely(!IS_VALID_DATE(bnd)))
ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("date out of range")));
lower.val = DateADTGetDatum(bnd);
lower.inclusive = true;
}
if (!upper.infinite && !DATE_NOT_FINITE(DatumGetDateADT(upper.val)) &&
!upper.inclusive)
{
DateADT bnd = DatumGetDateADT(upper.val);
bnd--;
if (unlikely(!IS_VALID_DATE(bnd)))
ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("date out of range")));
upper.val = DateADTGetDatum(bnd);
upper.inclusive = true;
}
PG_RETURN_RANGE_P(range_serialize(typcache, &lower, &upper,
false, escontext));
}