Check nsl library for gethostbyname_r() on all platforms (HP-UX uses it
[PostgreSQL.git] / contrib / intarray / _int_gin.c
blob1fba4cb9d3463bad8ec998a5fc1a6d049da280b4
1 /*
2 * $PostgreSQL:$
3 */
4 #include "postgres.h"
6 #include "access/gist.h"
7 #include "access/skey.h"
9 #include "_int.h"
11 PG_FUNCTION_INFO_V1(ginint4_queryextract);
12 Datum ginint4_queryextract(PG_FUNCTION_ARGS);
14 Datum
15 ginint4_queryextract(PG_FUNCTION_ARGS)
17 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
18 StrategyNumber strategy = PG_GETARG_UINT16(2);
19 Datum *res = NULL;
21 *nentries = 0;
23 if (strategy == BooleanSearchStrategy)
25 QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM_COPY(PG_GETARG_POINTER(0));
26 ITEM *items = GETQUERY(query);
27 int i;
29 if (query->size == 0)
30 PG_RETURN_POINTER(NULL);
32 if (shorterquery(items, query->size) == 0)
33 elog(ERROR, "Query requires full scan, GIN doesn't support it");
35 pfree(query);
37 query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_POINTER(0));
38 items = GETQUERY(query);
40 res = (Datum *) palloc(sizeof(Datum) * query->size);
41 *nentries = 0;
43 for (i = 0; i < query->size; i++)
44 if (items[i].type == VAL)
46 res[*nentries] = Int32GetDatum(items[i].val);
47 (*nentries)++;
50 else
52 ArrayType *query = PG_GETARG_ARRAYTYPE_P(0);
53 int4 *arr;
54 uint32 i;
56 CHECKARRVALID(query);
57 *nentries = ARRNELEMS(query);
58 if (*nentries > 0)
60 res = (Datum *) palloc(sizeof(Datum) * (*nentries));
62 arr = ARRPTR(query);
63 for (i = 0; i < *nentries; i++)
64 res[i] = Int32GetDatum(arr[i]);
68 if (nentries == 0)
70 switch (strategy)
72 case BooleanSearchStrategy:
73 case RTOverlapStrategyNumber:
74 *nentries = -1; /* nobody can be found */
75 break;
76 default: /* require fullscan: GIN can't find void
77 * arrays */
78 break;
82 PG_RETURN_POINTER(res);
85 PG_FUNCTION_INFO_V1(ginint4_consistent);
86 Datum ginint4_consistent(PG_FUNCTION_ARGS);
88 Datum
89 ginint4_consistent(PG_FUNCTION_ARGS)
91 bool *check = (bool *) PG_GETARG_POINTER(0);
92 StrategyNumber strategy = PG_GETARG_UINT16(1);
93 bool *recheck = (bool *) PG_GETARG_POINTER(3);
94 bool res = FALSE;
97 * we need not check array carefully, it's done by previous
98 * ginarrayextract call
101 switch (strategy)
103 case RTOverlapStrategyNumber:
104 /* result is not lossy */
105 *recheck = false;
106 /* at least one element in check[] is true, so result = true */
107 res = TRUE;
108 break;
109 case RTContainedByStrategyNumber:
110 case RTOldContainedByStrategyNumber:
111 /* we will need recheck */
112 *recheck = true;
113 /* at least one element in check[] is true, so result = true */
114 res = TRUE;
115 break;
116 case RTSameStrategyNumber:
118 ArrayType *query = PG_GETARG_ARRAYTYPE_P(2);
119 int i,
120 nentries = ARRNELEMS(query);
122 /* we will need recheck */
123 *recheck = true;
124 res = TRUE;
125 for (i = 0; i < nentries; i++)
126 if (!check[i])
128 res = FALSE;
129 break;
132 break;
133 case RTContainsStrategyNumber:
134 case RTOldContainsStrategyNumber:
136 ArrayType *query = PG_GETARG_ARRAYTYPE_P(2);
137 int i,
138 nentries = ARRNELEMS(query);
140 /* result is not lossy */
141 *recheck = false;
142 res = TRUE;
143 for (i = 0; i < nentries; i++)
144 if (!check[i])
146 res = FALSE;
147 break;
150 break;
151 case BooleanSearchStrategy:
153 QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_POINTER(2));
155 /* result is not lossy */
156 *recheck = false;
157 res = ginconsistent(query, check);
159 break;
160 default:
161 elog(ERROR, "ginint4_consistent: unknown strategy number: %d",
162 strategy);
165 PG_RETURN_BOOL(res);