6 #include "access/gin.h"
7 #include "access/skey.h"
8 #if PG_VERSION_NUM < 130000
9 #include "access/tuptoaster.h"
11 #include "access/heaptoast.h"
14 PG_FUNCTION_INFO_V1(smlararrayextract);
15 Datum smlararrayextract(PG_FUNCTION_ARGS);
17 smlararrayextract(PG_FUNCTION_ARGS)
20 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
24 * we should guarantee that array will not be destroyed during all
27 array = PG_GETARG_ARRAYTYPE_P_COPY(0);
31 sa = Array2SimpleArrayU(NULL, array, NULL);
33 *nentries = sa->nelems;
35 if (sa->nelems == 0 && PG_NARGS() == 3)
37 switch (PG_GETARG_UINT16(2)) /* StrategyNumber */
39 case SmlarOverlapStrategy:
40 case SmlarSimilarityStrategy:
41 *nentries = -1; /* nobody can be found */
48 PG_RETURN_POINTER( sa->elems );
51 PG_FUNCTION_INFO_V1(smlarqueryarrayextract);
52 Datum smlarqueryarrayextract(PG_FUNCTION_ARGS);
54 smlarqueryarrayextract(PG_FUNCTION_ARGS)
56 PG_RETURN_DATUM(DirectFunctionCall3(smlararrayextract,
62 PG_FUNCTION_INFO_V1(smlararrayconsistent);
63 Datum smlararrayconsistent(PG_FUNCTION_ARGS);
65 smlararrayconsistent(PG_FUNCTION_ARGS)
67 bool *check = (bool *) PG_GETARG_POINTER(0);
68 StrategyNumber strategy = PG_GETARG_UINT16(1);
73 bool *recheck = (bool *) PG_GETARG_POINTER(5);
79 case SmlarOverlapStrategy:
80 /* at least one element in check[] is true, so result = true */
84 case SmlarSimilarityStrategy:
86 fcinfo->flinfo->fn_extra = SearchArrayCache(
87 fcinfo->flinfo->fn_extra,
88 fcinfo->flinfo->fn_mcxt,
89 PG_GETARG_DATUM(2), NULL, &sa, NULL );
91 for(i=0; i<sa->nelems; i++)
95 * cnt is a lower limit of elements's number in indexed array;
102 double weight = 0.0, /* exact weight of union */
103 saSum = 0.0, /* exact length of query */
104 siSum = 0.0; /* lower limit of length of indexed value */
106 if ( getTFMethod() != TF_CONST )
107 elog(ERROR,"GIN supports only smlar.tf_method = \"const\"" );
111 for(i=0; i<sa->nelems; i++)
114 * With smlar.tf_method = "const" sa->df[i] is
115 * equal to its idf, so lookup of StatElem is not needed
119 weight += sa->df[i] * sa->df[i];
120 siSum += sa->df[i] * sa->df[i];
122 saSum += sa->df[i] * sa->df[i];
125 if ( saSum > 0.0 && siSum > 0.0 && weight / sqrt(saSum * siSum ) > GetSmlarLimit() )
133 power = sqrt( ((double)(sa->nelems)) * ((double)(cnt)) );
135 if ( ((double)cnt) / power >= GetSmlarLimit() )
140 if (cnt >= GetSmlarLimit())
144 elog(ERROR,"GIN doesn't support current formula type of similarity");
148 elog(ERROR, "smlararrayconsistent: unknown strategy number: %d", strategy);