4 #include "access/gin.h"
5 #include "access/skey.h"
6 #include "access/tuptoaster.h"
8 PG_FUNCTION_INFO_V1(smlararrayextract);
9 Datum smlararrayextract(PG_FUNCTION_ARGS);
11 smlararrayextract(PG_FUNCTION_ARGS)
14 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
18 * we should guarantee that array will not be destroyed during all
21 array = PG_GETARG_ARRAYTYPE_P_COPY(0);
25 sa = Array2SimpleArrayU(NULL, array, NULL);
27 *nentries = sa->nelems;
29 if (sa->nelems == 0 && PG_NARGS() == 3)
31 switch (PG_GETARG_UINT16(2)) /* StrategyNumber */
33 case SmlarOverlapStrategy:
34 case SmlarSimilarityStrategy:
35 *nentries = -1; /* nobody can be found */
42 PG_RETURN_POINTER( sa->elems );
45 PG_FUNCTION_INFO_V1(smlarqueryarrayextract);
46 Datum smlarqueryarrayextract(PG_FUNCTION_ARGS);
48 smlarqueryarrayextract(PG_FUNCTION_ARGS)
50 PG_RETURN_DATUM(DirectFunctionCall3(smlararrayextract,
56 PG_FUNCTION_INFO_V1(smlararrayconsistent);
57 Datum smlararrayconsistent(PG_FUNCTION_ARGS);
59 smlararrayconsistent(PG_FUNCTION_ARGS)
61 bool *check = (bool *) PG_GETARG_POINTER(0);
62 StrategyNumber strategy = PG_GETARG_UINT16(1);
67 bool *recheck = (bool *) PG_GETARG_POINTER(5);
73 case SmlarOverlapStrategy:
74 /* at least one element in check[] is true, so result = true */
78 case SmlarSimilarityStrategy:
80 fcinfo->flinfo->fn_extra = SearchArrayCache(
81 fcinfo->flinfo->fn_extra,
82 fcinfo->flinfo->fn_mcxt,
83 PG_GETARG_DATUM(2), NULL, &sa, NULL );
85 for(i=0; i<sa->nelems; i++)
89 * cnt is a lower limit of elements's number in indexed array;
96 double weight = 0.0, /* exact weight of union */
97 saSum = 0.0, /* exact length of query */
98 siSum = 0.0; /* lower limit of length of indexed value */
100 if ( getTFMethod() != TF_CONST )
101 elog(ERROR,"GIN supports only smlar.tf_method = \"const\"" );
105 for(i=0; i<sa->nelems; i++)
108 * With smlar.tf_method = "const" sa->df[i] is
109 * equal to its idf, so lookup of StatElem is not needed
113 weight += sa->df[i] * sa->df[i];
114 siSum += sa->df[i] * sa->df[i];
116 saSum += sa->df[i] * sa->df[i];
119 if ( saSum > 0.0 && siSum > 0.0 && weight / sqrt(saSum * siSum ) > GetSmlarLimit() )
127 power = sqrt( ((double)(sa->nelems)) * ((double)(cnt)) );
129 if ( ((double)cnt) / power >= GetSmlarLimit() )
134 if (cnt >= GetSmlarLimit())
138 elog(ERROR,"GIN doesn't support current formula type of similarity");
142 elog(ERROR, "smlararrayconsistent: unknown strategy number: %d", strategy);