4 #include "btree_gist.h"
5 #include "btree_utils_num.h"
6 #include "utils/date.h"
17 PG_FUNCTION_INFO_V1(gbt_date_compress
);
18 PG_FUNCTION_INFO_V1(gbt_date_union
);
19 PG_FUNCTION_INFO_V1(gbt_date_picksplit
);
20 PG_FUNCTION_INFO_V1(gbt_date_consistent
);
21 PG_FUNCTION_INFO_V1(gbt_date_penalty
);
22 PG_FUNCTION_INFO_V1(gbt_date_same
);
24 Datum
gbt_date_compress(PG_FUNCTION_ARGS
);
25 Datum
gbt_date_union(PG_FUNCTION_ARGS
);
26 Datum
gbt_date_picksplit(PG_FUNCTION_ARGS
);
27 Datum
gbt_date_consistent(PG_FUNCTION_ARGS
);
28 Datum
gbt_date_penalty(PG_FUNCTION_ARGS
);
29 Datum
gbt_date_same(PG_FUNCTION_ARGS
);
32 gbt_dategt(const void *a
, const void *b
)
35 DirectFunctionCall2(date_gt
, DateADTGetDatum(*((DateADT
*) a
)), DateADTGetDatum(*((DateADT
*) b
)))
40 gbt_datege(const void *a
, const void *b
)
43 DirectFunctionCall2(date_ge
, DateADTGetDatum(*((DateADT
*) a
)), DateADTGetDatum(*((DateADT
*) b
)))
48 gbt_dateeq(const void *a
, const void *b
)
51 DirectFunctionCall2(date_eq
, DateADTGetDatum(*((DateADT
*) a
)), DateADTGetDatum(*((DateADT
*) b
)))
56 gbt_datele(const void *a
, const void *b
)
59 DirectFunctionCall2(date_le
, DateADTGetDatum(*((DateADT
*) a
)), DateADTGetDatum(*((DateADT
*) b
)))
64 gbt_datelt(const void *a
, const void *b
)
67 DirectFunctionCall2(date_lt
, DateADTGetDatum(*((DateADT
*) a
)), DateADTGetDatum(*((DateADT
*) b
)))
74 gbt_datekey_cmp(const void *a
, const void *b
)
76 if (gbt_dategt((void *) &(((Nsrt
*) a
)->t
[0]), (void *) &(((Nsrt
*) b
)->t
[0])))
78 else if (gbt_datelt((void *) &(((Nsrt
*) a
)->t
[0]), (void *) &(((Nsrt
*) b
)->t
[0])))
84 static const gbtree_ninfo tinfo
=
97 /**************************************************
99 **************************************************/
104 gbt_date_compress(PG_FUNCTION_ARGS
)
106 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
107 GISTENTRY
*retval
= NULL
;
109 PG_RETURN_POINTER(gbt_num_compress(retval
, entry
, &tinfo
));
115 gbt_date_consistent(PG_FUNCTION_ARGS
)
117 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
118 DateADT query
= PG_GETARG_DATEADT(1);
119 StrategyNumber strategy
= (StrategyNumber
) PG_GETARG_UINT16(2);
120 /* Oid subtype = PG_GETARG_OID(3); */
121 bool *recheck
= (bool *) PG_GETARG_POINTER(4);
122 dateKEY
*kkk
= (dateKEY
*) DatumGetPointer(entry
->key
);
125 /* All cases served by this function are exact */
128 key
.lower
= (GBT_NUMKEY
*) & kkk
->lower
;
129 key
.upper
= (GBT_NUMKEY
*) & kkk
->upper
;
132 gbt_num_consistent(&key
, (void *) &query
, &strategy
, GIST_LEAF(entry
), &tinfo
)
138 gbt_date_union(PG_FUNCTION_ARGS
)
140 GistEntryVector
*entryvec
= (GistEntryVector
*) PG_GETARG_POINTER(0);
141 void *out
= palloc(sizeof(dateKEY
));
143 *(int *) PG_GETARG_POINTER(1) = sizeof(dateKEY
);
144 PG_RETURN_POINTER(gbt_num_union((void *) out
, entryvec
, &tinfo
));
149 gbt_date_penalty(PG_FUNCTION_ARGS
)
151 dateKEY
*origentry
= (dateKEY
*) DatumGetPointer(((GISTENTRY
*) PG_GETARG_POINTER(0))->key
);
152 dateKEY
*newentry
= (dateKEY
*) DatumGetPointer(((GISTENTRY
*) PG_GETARG_POINTER(1))->key
);
153 float *result
= (float *) PG_GETARG_POINTER(2);
157 diff
= DatumGetInt32(DirectFunctionCall2(
159 DateADTGetDatum(newentry
->upper
),
160 DateADTGetDatum(origentry
->upper
)));
164 diff
= DatumGetInt32(DirectFunctionCall2(
166 DateADTGetDatum(origentry
->lower
),
167 DateADTGetDatum(newentry
->lower
)));
175 diff
= DatumGetInt32(DirectFunctionCall2(
177 DateADTGetDatum(origentry
->upper
),
178 DateADTGetDatum(origentry
->lower
)));
180 *result
+= (float) (res
/ ((double) (res
+ diff
)));
181 *result
*= (FLT_MAX
/ (((GISTENTRY
*) PG_GETARG_POINTER(0))->rel
->rd_att
->natts
+ 1));
184 PG_RETURN_POINTER(result
);
189 gbt_date_picksplit(PG_FUNCTION_ARGS
)
191 PG_RETURN_POINTER(gbt_num_picksplit(
192 (GistEntryVector
*) PG_GETARG_POINTER(0),
193 (GIST_SPLITVEC
*) PG_GETARG_POINTER(1),
199 gbt_date_same(PG_FUNCTION_ARGS
)
201 dateKEY
*b1
= (dateKEY
*) PG_GETARG_POINTER(0);
202 dateKEY
*b2
= (dateKEY
*) PG_GETARG_POINTER(1);
203 bool *result
= (bool *) PG_GETARG_POINTER(2);
205 *result
= gbt_num_same((void *) b1
, (void *) b2
, &tinfo
);
206 PG_RETURN_POINTER(result
);