-
Notifications
You must be signed in to change notification settings - Fork 0
/
anyarray_gin.c
196 lines (171 loc) · 4.45 KB
/
anyarray_gin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*-------------------------------------------------------------------------
*
* anyarray_gin.c
* GIN support functions for anyarray
*
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
*
* Copyright (c) 2023, Postgres Professional
*
* Author: Teodor Sigaev <[email protected]>,
* Oleg Bartunov <[email protected]>
*
* IDENTIFICATION
* contrib/anyarray/anyarray_gin.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/gin.h"
#include "access/gist.h"
#include "access/skey.h"
#include "anyarray.h"
PG_FUNCTION_INFO_V1(ginanyarray_extract);
Datum
ginanyarray_extract(PG_FUNCTION_ARGS)
{
ArrayType *array;
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
SimpleArray *sa;
AnyArrayTypeInfo *info;
array = PG_GETARG_ARRAYTYPE_P_COPY(0);
CHECKARRVALID(array);
info = getAnyArrayTypeInfoCached(fcinfo, ARR_ELEMTYPE(array));
sa = Array2SimpleArray(info, array);
sortSimpleArray(sa, 1);
uniqSimpleArray(sa, false);
*nentries = sa->nelems;
PG_RETURN_POINTER(sa->elems);
}
PG_FUNCTION_INFO_V1(ginanyarray_queryextract);
Datum
ginanyarray_queryextract(PG_FUNCTION_ARGS)
{
int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
StrategyNumber strategy = PG_GETARG_UINT16(2);
SimpleArray *sa;
AnyArrayTypeInfo *info;
ArrayType *array;
array = PG_GETARG_ARRAYTYPE_P_COPY(0);
CHECKARRVALID(array);
info = getAnyArrayTypeInfoCached(fcinfo, ARR_ELEMTYPE(array));
sa = Array2SimpleArray(info, array);
sortSimpleArray(sa, 1);
uniqSimpleArray(sa, false);
*nentries = sa->nelems;
switch (strategy)
{
case AnyAarraySimilarityStrategy:
case RTOverlapStrategyNumber:
*searchMode = GIN_SEARCH_MODE_DEFAULT;
break;
case RTContainedByStrategyNumber:
/* empty set is contained in everything */
*searchMode = GIN_SEARCH_MODE_INCLUDE_EMPTY;
break;
case RTSameStrategyNumber:
if (*nentries > 0)
*searchMode = GIN_SEARCH_MODE_DEFAULT;
else
*searchMode = GIN_SEARCH_MODE_INCLUDE_EMPTY;
break;
case RTContainsStrategyNumber:
if (*nentries > 0)
*searchMode = GIN_SEARCH_MODE_DEFAULT;
else /* everything contains the empty set */
*searchMode = GIN_SEARCH_MODE_ALL;
break;
default:
elog(ERROR, "ginanyarray_queryextract: unknown strategy number: %d",
strategy);
}
PG_RETURN_POINTER(sa->elems);
}
PG_FUNCTION_INFO_V1(ginanyarray_consistent);
Datum
ginanyarray_consistent(PG_FUNCTION_ARGS)
{
bool *check = (bool *) PG_GETARG_POINTER(0);
StrategyNumber strategy = PG_GETARG_UINT16(1);
int32 nkeys = PG_GETARG_INT32(3);
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
bool *recheck = (bool *) PG_GETARG_POINTER(5);
bool res = true;
int32 i;
switch (strategy)
{
case RTOverlapStrategyNumber:
/* result is not lossy */
*recheck = false;
/* at least one element in check[] is true, so result = true */
res = true;
break;
case RTContainedByStrategyNumber:
/* we will need recheck */
*recheck = true;
/* at least one element in check[] is true, so result = true */
res = true;
break;
case RTSameStrategyNumber:
/* we will need recheck */
*recheck = true;
/* Must have all elements in check[] true */
res = true;
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
break;
case RTContainsStrategyNumber:
/* result is not lossy */
*recheck = false;
/* Must have all elements in check[] true */
res = true;
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
break;
case AnyAarraySimilarityStrategy:
{
int32 nIntersection = 0;
for (i = 0; i < nkeys; i++)
{
if (check[i])
nIntersection++;
}
switch(SmlType)
{
case AA_Cosine:
*recheck = true;
/* nIntersection / sqrt(nkeys * nIntersection) */
res = (sqrt(((double)nIntersection) / (double)nkeys) >= SmlLimit);
break;
case AA_Jaccard:
*recheck = true;
res = ((((double)nIntersection) / (double)nkeys) >= SmlLimit);
break;
case AA_Overlap:
*recheck = false;
res = (((double)nIntersection) >= SmlLimit);
break;
default:
elog(ERROR, "unknown similarity type");
}
}
break;
default:
elog(ERROR, "ginanyarray_consistent: unknown strategy number: %d",
strategy);
}
PG_RETURN_BOOL(res);
}