4 #include "btree_gist.h"
9 #include "btree_utils_var.h"
10 #include "utils/builtins.h"
11 #include "utils/numeric.h"
12 #include "utils/rel.h"
17 PG_FUNCTION_INFO_V1(gbt_numeric_compress
);
18 PG_FUNCTION_INFO_V1(gbt_numeric_union
);
19 PG_FUNCTION_INFO_V1(gbt_numeric_picksplit
);
20 PG_FUNCTION_INFO_V1(gbt_numeric_consistent
);
21 PG_FUNCTION_INFO_V1(gbt_numeric_penalty
);
22 PG_FUNCTION_INFO_V1(gbt_numeric_same
);
24 Datum
gbt_numeric_compress(PG_FUNCTION_ARGS
);
25 Datum
gbt_numeric_union(PG_FUNCTION_ARGS
);
26 Datum
gbt_numeric_picksplit(PG_FUNCTION_ARGS
);
27 Datum
gbt_numeric_consistent(PG_FUNCTION_ARGS
);
28 Datum
gbt_numeric_penalty(PG_FUNCTION_ARGS
);
29 Datum
gbt_numeric_same(PG_FUNCTION_ARGS
);
32 /* define for comparison */
35 gbt_numeric_gt(const void *a
, const void *b
)
37 return (DatumGetBool(DirectFunctionCall2(numeric_gt
, PointerGetDatum(a
), PointerGetDatum(b
))));
41 gbt_numeric_ge(const void *a
, const void *b
)
43 return (DatumGetBool(DirectFunctionCall2(numeric_ge
, PointerGetDatum(a
), PointerGetDatum(b
))));
47 gbt_numeric_eq(const void *a
, const void *b
)
49 return (DatumGetBool(DirectFunctionCall2(numeric_eq
, PointerGetDatum(a
), PointerGetDatum(b
))));
53 gbt_numeric_le(const void *a
, const void *b
)
55 return (DatumGetBool(DirectFunctionCall2(numeric_le
, PointerGetDatum(a
), PointerGetDatum(b
))));
59 gbt_numeric_lt(const void *a
, const void *b
)
61 return (DatumGetBool(DirectFunctionCall2(numeric_lt
, PointerGetDatum(a
), PointerGetDatum(b
))));
66 gbt_numeric_cmp(const bytea
*a
, const bytea
*b
)
69 (DatumGetInt32(DirectFunctionCall2(numeric_cmp
, PointerGetDatum(a
), PointerGetDatum(b
))));
73 static const gbtree_vinfo tinfo
=
88 /**************************************************
90 **************************************************/
94 gbt_numeric_compress(PG_FUNCTION_ARGS
)
96 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
98 PG_RETURN_POINTER(gbt_var_compress(entry
, &tinfo
));
104 gbt_numeric_consistent(PG_FUNCTION_ARGS
)
106 GISTENTRY
*entry
= (GISTENTRY
*) PG_GETARG_POINTER(0);
107 void *query
= (void *) DatumGetNumeric(PG_GETARG_DATUM(1));
108 StrategyNumber strategy
= (StrategyNumber
) PG_GETARG_UINT16(2);
109 /* Oid subtype = PG_GETARG_OID(3); */
110 bool *recheck
= (bool *) PG_GETARG_POINTER(4);
112 GBT_VARKEY
*key
= (GBT_VARKEY
*) DatumGetPointer(entry
->key
);
113 GBT_VARKEY_R r
= gbt_var_key_readable(key
);
115 /* All cases served by this function are exact */
118 retval
= gbt_var_consistent(&r
, query
, &strategy
, GIST_LEAF(entry
), &tinfo
);
119 PG_RETURN_BOOL(retval
);
125 gbt_numeric_union(PG_FUNCTION_ARGS
)
127 GistEntryVector
*entryvec
= (GistEntryVector
*) PG_GETARG_POINTER(0);
128 int32
*size
= (int *) PG_GETARG_POINTER(1);
130 PG_RETURN_POINTER(gbt_var_union(entryvec
, size
, &tinfo
));
135 gbt_numeric_same(PG_FUNCTION_ARGS
)
137 Datum d1
= PG_GETARG_DATUM(0);
138 Datum d2
= PG_GETARG_DATUM(1);
139 bool *result
= (bool *) PG_GETARG_POINTER(2);
141 PG_RETURN_POINTER(gbt_var_same(result
, d1
, d2
, &tinfo
));
146 gbt_numeric_penalty(PG_FUNCTION_ARGS
)
148 GISTENTRY
*o
= (GISTENTRY
*) PG_GETARG_POINTER(0);
149 GISTENTRY
*n
= (GISTENTRY
*) PG_GETARG_POINTER(1);
150 float *result
= (float *) PG_GETARG_POINTER(2);
156 GBT_VARKEY
*org
= (GBT_VARKEY
*) DatumGetPointer(o
->key
);
157 GBT_VARKEY
*newe
= (GBT_VARKEY
*) DatumGetPointer(n
->key
);
163 rk
= gbt_var_key_readable(org
);
164 uni
= PointerGetDatum(gbt_var_key_copy(&rk
, TRUE
));
165 gbt_var_bin_union(&uni
, newe
, &tinfo
);
166 ok
= gbt_var_key_readable(org
);
167 uk
= gbt_var_key_readable((GBT_VARKEY
*) DatumGetPointer(uni
));
169 us
= DatumGetNumeric(DirectFunctionCall2(
171 PointerGetDatum(uk
.upper
),
172 PointerGetDatum(uk
.lower
)
175 os
= DatumGetNumeric(DirectFunctionCall2(
177 PointerGetDatum(ok
.upper
),
178 PointerGetDatum(ok
.lower
)
181 ds
= DatumGetNumeric(DirectFunctionCall2(
187 if (NUMERIC_IS_NAN(us
))
189 if (NUMERIC_IS_NAN(os
))
196 Numeric nul
= DatumGetNumeric(DirectFunctionCall1(int4_numeric
, Int32GetDatum(0)));
200 if (DirectFunctionCall2(numeric_gt
, NumericGetDatum(ds
), NumericGetDatum(nul
)))
203 os
= DatumGetNumeric(DirectFunctionCall2(
208 *result
+= (float4
) DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow
, NumericGetDatum(os
)));
213 *result
*= (FLT_MAX
/ (((GISTENTRY
*) PG_GETARG_POINTER(0))->rel
->rd_att
->natts
+ 1));
215 PG_RETURN_POINTER(result
);
221 gbt_numeric_picksplit(PG_FUNCTION_ARGS
)
223 GistEntryVector
*entryvec
= (GistEntryVector
*) PG_GETARG_POINTER(0);
224 GIST_SPLITVEC
*v
= (GIST_SPLITVEC
*) PG_GETARG_POINTER(1);
226 gbt_var_picksplit(entryvec
, v
, &tinfo
);
227 PG_RETURN_POINTER(v
);