2 * contrib/btree_gist/btree_float4.c
6 #include "btree_gist.h"
7 #include "btree_utils_num.h"
8 #include "utils/float.h"
10 typedef struct float4key
19 PG_FUNCTION_INFO_V1(gbt_float4_compress
);
20 PG_FUNCTION_INFO_V1(gbt_float4_fetch
);
21 PG_FUNCTION_INFO_V1(gbt_float4_union
);
22 PG_FUNCTION_INFO_V1(gbt_float4_picksplit
);
23 PG_FUNCTION_INFO_V1(gbt_float4_consistent
);
24 PG_FUNCTION_INFO_V1(gbt_float4_distance
);
25 PG_FUNCTION_INFO_V1(gbt_float4_penalty
);
26 PG_FUNCTION_INFO_V1(gbt_float4_same
);
29 gbt_float4gt(const void *a
, const void *b
, FmgrInfo
*flinfo
)
31 return (*((const float4
*) a
) > *((const float4
*) b
));
34 gbt_float4ge(const void *a
, const void *b
, FmgrInfo
*flinfo
)
36 return (*((const float4
*) a
) >= *((const float4
*) b
));
39 gbt_float4eq(const void *a
, const void *b
, FmgrInfo
*flinfo
)
41 return (*((const float4
*) a
) == *((const float4
*) b
));
44 gbt_float4le(const void *a
, const void *b
, FmgrInfo
*flinfo
)
46 return (*((const float4
*) a
) <= *((const float4
*) b
));
49 gbt_float4lt(const void *a
, const void *b
, FmgrInfo
*flinfo
)
51 return (*((const float4
*) a
) < *((const float4
*) b
));
55 gbt_float4key_cmp(const void *a
, const void *b
, FmgrInfo
*flinfo
)
57 float4KEY
*ia
= (float4KEY
*) (((const Nsrt
*) a
)->t
);
58 float4KEY
*ib
= (float4KEY
*) (((const Nsrt
*) b
)->t
);
60 if (ia
->lower
== ib
->lower
)
62 if (ia
->upper
== ib
->upper
)
65 return (ia
->upper
> ib
->upper
) ? 1 : -1;
68 return (ia
->lower
> ib
->lower
) ? 1 : -1;
72 gbt_float4_dist(const void *a
, const void *b
, FmgrInfo
*flinfo
)
74 return GET_FLOAT_DISTANCE(float4
, a
, b
);
78 static const gbtree_ninfo tinfo
=
82 8, /* sizeof(gbtreekey8) */
93 PG_FUNCTION_INFO_V1(float4_dist
);
95 float4_dist(PG_FUNCTION_ARGS
)
97 float4 a
= PG_GETARG_FLOAT4(0);
98 float4 b
= PG_GETARG_FLOAT4(1);
102 if (unlikely(isinf(r
)) && !isinf(a
) && !isinf(b
))
103 float_overflow_error();
105 PG_RETURN_FLOAT4(fabsf(r
));
109 /**************************************************
111 **************************************************/
115 gbt_float4_compress(PG_FUNCTION_ARGS
)
117 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
119 PG_RETURN_POINTER(gbt_num_compress(entry
, &tinfo
));
123 gbt_float4_fetch(PG_FUNCTION_ARGS
)
125 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
127 PG_RETURN_POINTER(gbt_num_fetch(entry
, &tinfo
));
131 gbt_float4_consistent(PG_FUNCTION_ARGS
)
133 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
134 float4 query
= PG_GETARG_FLOAT4(1);
135 StrategyNumber strategy
= (StrategyNumber
) PG_GETARG_UINT16(2);
137 /* Oid subtype = PG_GETARG_OID(3); */
138 bool *recheck
= (bool *) PG_GETARG_POINTER(4);
139 float4KEY
*kkk
= (float4KEY
*) DatumGetPointer(entry
->key
);
142 /* All cases served by this function are exact */
145 key
.lower
= (GBT_NUMKEY
*) &kkk
->lower
;
146 key
.upper
= (GBT_NUMKEY
*) &kkk
->upper
;
148 PG_RETURN_BOOL(gbt_num_consistent(&key
, &query
, &strategy
,
149 GIST_LEAF(entry
), &tinfo
,
155 gbt_float4_distance(PG_FUNCTION_ARGS
)
157 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
158 float4 query
= PG_GETARG_FLOAT4(1);
160 /* Oid subtype = PG_GETARG_OID(3); */
161 float4KEY
*kkk
= (float4KEY
*) DatumGetPointer(entry
->key
);
164 key
.lower
= (GBT_NUMKEY
*) &kkk
->lower
;
165 key
.upper
= (GBT_NUMKEY
*) &kkk
->upper
;
167 PG_RETURN_FLOAT8(gbt_num_distance(&key
, &query
, GIST_LEAF(entry
),
168 &tinfo
, fcinfo
->flinfo
));
173 gbt_float4_union(PG_FUNCTION_ARGS
)
175 GistEntryVector
*entryvec
= (GistEntryVector
*) PG_GETARG_POINTER(0);
176 void *out
= palloc(sizeof(float4KEY
));
178 *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY
);
179 PG_RETURN_POINTER(gbt_num_union(out
, entryvec
, &tinfo
, fcinfo
->flinfo
));
184 gbt_float4_penalty(PG_FUNCTION_ARGS
)
186 float4KEY
*origentry
= (float4KEY
*) DatumGetPointer(((GISTENTRY
*) PG_GETARG_POINTER(0))->key
);
187 float4KEY
*newentry
= (float4KEY
*) DatumGetPointer(((GISTENTRY
*) PG_GETARG_POINTER(1))->key
);
188 float *result
= (float *) PG_GETARG_POINTER(2);
190 penalty_num(result
, origentry
->lower
, origentry
->upper
, newentry
->lower
, newentry
->upper
);
192 PG_RETURN_POINTER(result
);
196 gbt_float4_picksplit(PG_FUNCTION_ARGS
)
198 PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector
*) PG_GETARG_POINTER(0),
199 (GIST_SPLITVEC
*) PG_GETARG_POINTER(1),
200 &tinfo
, fcinfo
->flinfo
));
204 gbt_float4_same(PG_FUNCTION_ARGS
)
206 float4KEY
*b1
= (float4KEY
*) PG_GETARG_POINTER(0);
207 float4KEY
*b2
= (float4KEY
*) PG_GETARG_POINTER(1);
208 bool *result
= (bool *) PG_GETARG_POINTER(2);
210 *result
= gbt_num_same((void *) b1
, (void *) b2
, &tinfo
, fcinfo
->flinfo
);
211 PG_RETURN_POINTER(result
);