6 #include "access/gin.h"
7 #include "access/skey.h"
8 #include "access/heaptoast.h"
10 PG_FUNCTION_INFO_V1(smlararrayextract);
11 Datum smlararrayextract(PG_FUNCTION_ARGS);
13 smlararrayextract(PG_FUNCTION_ARGS)
16 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
20 * we should guarantee that array will not be destroyed during all
23 array = PG_GETARG_ARRAYTYPE_P_COPY(0);
27 sa = Array2SimpleArrayU(NULL, array, NULL);
29 *nentries = sa->nelems;
31 if (sa->nelems == 0 && PG_NARGS() == 3)
33 switch (PG_GETARG_UINT16(2)) /* StrategyNumber */
35 case SmlarOverlapStrategy:
36 case SmlarSimilarityStrategy:
37 *nentries = -1; /* nobody can be found */
44 PG_RETURN_POINTER( sa->elems );
47 PG_FUNCTION_INFO_V1(smlarqueryarrayextract);
48 Datum smlarqueryarrayextract(PG_FUNCTION_ARGS);
50 smlarqueryarrayextract(PG_FUNCTION_ARGS)
52 PG_RETURN_DATUM(DirectFunctionCall3(smlararrayextract,
58 PG_FUNCTION_INFO_V1(smlararrayconsistent);
59 Datum smlararrayconsistent(PG_FUNCTION_ARGS);
61 smlararrayconsistent(PG_FUNCTION_ARGS)
63 bool *check = (bool *) PG_GETARG_POINTER(0);
64 StrategyNumber strategy = PG_GETARG_UINT16(1);
69 bool *recheck = (bool *) PG_GETARG_POINTER(5);
75 case SmlarOverlapStrategy:
76 /* at least one element in check[] is true, so result = true */
80 case SmlarSimilarityStrategy:
82 fcinfo->flinfo->fn_extra = SearchArrayCache(
83 fcinfo->flinfo->fn_extra,
84 fcinfo->flinfo->fn_mcxt,
85 PG_GETARG_DATUM(2), NULL, &sa, NULL );
87 for(i=0; i<sa->nelems; i++)
91 * cnt is a lower limit of elements's number in indexed array;
98 double weight = 0.0, /* exact weight of union */
99 saSum = 0.0, /* exact length of query */
100 siSum = 0.0; /* lower limit of length of indexed value */
102 if ( getTFMethod() != TF_CONST )
103 elog(ERROR,"GIN supports only smlar.tf_method = \"const\"" );
107 for(i=0; i<sa->nelems; i++)
110 * With smlar.tf_method = "const" sa->df[i] is
111 * equal to its idf, so lookup of StatElem is not needed
115 weight += sa->df[i] * sa->df[i];
116 siSum += sa->df[i] * sa->df[i];
118 saSum += sa->df[i] * sa->df[i];
121 if ( saSum > 0.0 && siSum > 0.0 && weight / sqrt(saSum * siSum ) > GetSmlarLimit() )
129 power = sqrt( ((double)(sa->nelems)) * ((double)(cnt)) );
131 if ( ((double)cnt) / power >= GetSmlarLimit() )
136 if (cnt >= GetSmlarLimit())
140 elog(ERROR,"GIN doesn't support current formula type of similarity");
144 elog(ERROR, "smlararrayconsistent: unknown strategy number: %d", strategy);