Skip to content

Commit 7bfb490

Browse files
author
Vladlen Popolitov
committed
gin triconsistent class support function added
1 parent ebf7f14 commit 7bfb490

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

anyarray--1.0.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,11 @@ CREATE OR REPLACE FUNCTION ginanyarray_consistent(internal, internal, anyarray)
571571
AS 'MODULE_PATHNAME'
572572
LANGUAGE C IMMUTABLE;
573573

574+
CREATE OR REPLACE FUNCTION ginanyarray_triconsistent(internal, internal, anyarray,internal,internal,internal,internal,internal)
575+
RETURNS internal
576+
AS 'MODULE_PATHNAME'
577+
LANGUAGE C IMMUTABLE;
578+
574579
--gin opclasses
575580

576581
CREATE OPERATOR CLASS _int2_aa_ops
@@ -612,6 +617,37 @@ AS
612617
FUNCTION 2 ginanyarray_extract(anyarray, internal),
613618
FUNCTION 3 ginanyarray_queryextract(anyarray, internal, internal),
614619
FUNCTION 4 ginanyarray_consistent(internal, internal, anyarray),
620+
--FUNCTION 6 ginanyarray_triconsistent(internal, internal, anyarray,internal,internal,internal,internal,internal),
621+
STORAGE int8;
622+
623+
CREATE OPERATOR CLASS _int8_aa_ops_beta
624+
FOR TYPE _int8 USING gin
625+
AS
626+
OPERATOR 3 && (anyarray, anyarray),
627+
OPERATOR 6 = (anyarray, anyarray),
628+
OPERATOR 7 @> (anyarray, anyarray),
629+
OPERATOR 8 <@ (anyarray, anyarray),
630+
OPERATOR 16 % (anyarray, anyarray),
631+
FUNCTION 1 btint8cmp(int8,int8),
632+
FUNCTION 2 ginanyarray_extract(anyarray, internal),
633+
FUNCTION 3 ginanyarray_queryextract(anyarray, internal, internal),
634+
-- FUNCTION 4 ginanyarray_consistent(internal, internal, anyarray),
635+
FUNCTION 6 ginanyarray_triconsistent(internal, internal, anyarray,internal,internal,internal,internal,internal),
636+
STORAGE int8;
637+
638+
CREATE OPERATOR CLASS _int8_aa_ops_beta2
639+
FOR TYPE _int8 USING gin
640+
AS
641+
OPERATOR 3 && (anyarray, anyarray),
642+
OPERATOR 6 = (anyarray, anyarray),
643+
OPERATOR 7 @> (anyarray, anyarray),
644+
OPERATOR 8 <@ (anyarray, anyarray),
645+
OPERATOR 16 % (anyarray, anyarray),
646+
FUNCTION 1 btint8cmp(int8,int8),
647+
FUNCTION 2 ginanyarray_extract(anyarray, internal),
648+
FUNCTION 3 ginanyarray_queryextract(anyarray, internal, internal),
649+
FUNCTION 4 ginanyarray_consistent(internal, internal, anyarray),
650+
FUNCTION 6 ginanyarray_triconsistent(internal, internal, anyarray,internal,internal,internal,internal,internal),
615651
STORAGE int8;
616652

617653
CREATE OPERATOR CLASS _float4_aa_ops

anyarray_gin.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,128 @@ ginanyarray_consistent(PG_FUNCTION_ARGS)
194194

195195
PG_RETURN_BOOL(res);
196196
}
197+
198+
PG_FUNCTION_INFO_V1(ginanyarray_triconsistent);
199+
Datum
200+
ginanyarray_triconsistent(PG_FUNCTION_ARGS)
201+
{
202+
GinTernaryValue *check = (GinTernaryValue *)PG_GETARG_POINTER(0);
203+
StrategyNumber strategy = PG_GETARG_UINT16(1);
204+
int32 nkeys = PG_GETARG_INT32(3);
205+
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
206+
GinTernaryValue res = GIN_MAYBE;
207+
int32 i;
208+
209+
switch (strategy)
210+
{
211+
case RTOverlapStrategyNumber:
212+
/* if at least one element in check[] is GIN_TRUE, so result = GIN_TRUE
213+
* otherwise if at least one element in check[] is GIN_MAYBE, so result = GIN_MAYBE
214+
* otherwise result = GIN_FALSE
215+
*/
216+
res = GIN_FALSE;
217+
for (i = 0; i < nkeys; i++)
218+
{
219+
if (check[i] == GIN_TRUE)
220+
{
221+
res = GIN_TRUE;
222+
break;
223+
} else if (check[i] == GIN_MAYBE)
224+
{
225+
res = GIN_MAYBE;
226+
}
227+
}
228+
break;
229+
case RTContainedByStrategyNumber:
230+
/* at least one element in check[] is GIN_TRUE or GIN_MAYBE, so result = GIN_MAYBE (we will need recheck in any case) */
231+
res = GIN_MAYBE;
232+
break;
233+
case RTSameStrategyNumber:
234+
/* if at least one element in check[] is GIN_FALSE, so result = GIN_FALSE
235+
* otherwise result = GIN_MAYBE
236+
*/
237+
res = GIN_MAYBE;
238+
for (i = 0; i < nkeys; i++)
239+
{
240+
if (check[i] == GIN_FALSE)
241+
{
242+
res = GIN_FALSE;
243+
break;
244+
}
245+
}
246+
break;
247+
case RTContainsStrategyNumber:
248+
/* if at least one element in check[] is GIN_FALSE, so result = GIN_FALSE
249+
* otherwise if at least one element in check[] is GIN_MAYBE, so result = GIN_MAYBE
250+
* otherwise result = GIN_TRUE
251+
*/
252+
res = GIN_TRUE;
253+
for (i = 0; i < nkeys; i++)
254+
{
255+
if (check[i] == GIN_FALSE)
256+
{
257+
res = GIN_FALSE;
258+
break;
259+
} else if (check[i] == GIN_MAYBE)
260+
{
261+
res = GIN_MAYBE;
262+
}
263+
}
264+
break;
265+
case AnyAarraySimilarityStrategy:
266+
{
267+
int32 nIntersectionMin = 0;
268+
int32 nIntersectionMax = 0;
269+
270+
res = GIN_FALSE;
271+
for (i = 0; i < nkeys; i++)
272+
{
273+
if (check[i] == GIN_TRUE)
274+
{
275+
nIntersectionMin++;
276+
nIntersectionMax++;
277+
} else if (check[i] == GIN_MAYBE)
278+
{
279+
nIntersectionMax++;
280+
res = GIN_MAYBE;
281+
}
282+
}
283+
284+
switch(SmlType)
285+
{
286+
case AA_Cosine:
287+
/* nIntersection / sqrt(nkeys * nIntersection) */
288+
if(sqrt(((double)nIntersectionMax) / (double)nkeys) < SmlLimit){
289+
res = GIN_FALSE;
290+
} else {
291+
res = GIN_MAYBE;
292+
}
293+
break;
294+
case AA_Jaccard:
295+
if((((double)nIntersectionMax) / (double)nkeys) < SmlLimit){
296+
res = GIN_FALSE;
297+
} else {
298+
res = GIN_MAYBE;
299+
}
300+
break;
301+
case AA_Overlap:
302+
/* if nIntersection >= SmlLimit, so result = GIN_TRUE
303+
* otherwise if at least one element in check[] is GIN_MAYBE, so result = GIN_MAYBE
304+
* otherwise result = GIN_FALSE
305+
*/
306+
if(((double)nIntersectionMin) >= SmlLimit)
307+
{
308+
res = GIN_TRUE;
309+
}
310+
break;
311+
default:
312+
elog(ERROR, "unknown similarity type");
313+
}
314+
}
315+
break;
316+
default:
317+
elog(ERROR, "ginanyarray_consistent: unknown strategy number: %d",
318+
strategy);
319+
}
320+
PG_RETURN_GIN_TERNARY_VALUE(res);
321+
}

0 commit comments

Comments
 (0)