summaryrefslogtreecommitdiff
path: root/contrib/intarray/_int_gin.c
blob: 0edb686df2ec83b9a0dc1ea1ebaf1ef9d39cdc95 (plain)
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
/*
 * $PostgreSQL: pgsql/contrib/intarray/_int_gin.c,v 1.8 2008/05/17 01:28:19 adunstan Exp $ 
 */
#include "postgres.h"

#include "access/gist.h"
#include "access/skey.h"

#include "_int.h"

PG_FUNCTION_INFO_V1(ginint4_queryextract);
Datum		ginint4_queryextract(PG_FUNCTION_ARGS);

Datum
ginint4_queryextract(PG_FUNCTION_ARGS)
{
	int32	   *nentries = (int32 *) PG_GETARG_POINTER(1);
	StrategyNumber strategy = PG_GETARG_UINT16(2);
	Datum	   *res = NULL;

	*nentries = 0;

	if (strategy == BooleanSearchStrategy)
	{
		QUERYTYPE  *query = (QUERYTYPE *) PG_DETOAST_DATUM_COPY(PG_GETARG_POINTER(0));
		ITEM	   *items = GETQUERY(query);
		int			i;

		if (query->size == 0)
			PG_RETURN_POINTER(NULL);

		if (shorterquery(items, query->size) == 0)
			elog(ERROR, "Query requires full scan, GIN doesn't support it");

		pfree(query);

		query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_POINTER(0));
		items = GETQUERY(query);

		res = (Datum *) palloc(sizeof(Datum) * query->size);
		*nentries = 0;

		for (i = 0; i < query->size; i++)
			if (items[i].type == VAL)
			{
				res[*nentries] = Int32GetDatum(items[i].val);
				(*nentries)++;
			}
	}
	else
	{
		ArrayType  *query = PG_GETARG_ARRAYTYPE_P(0);
		int4	   *arr;
		uint32		i;

		CHECKARRVALID(query);
		*nentries = ARRNELEMS(query);
		if (*nentries > 0)
		{
			res = (Datum *) palloc(sizeof(Datum) * (*nentries));

			arr = ARRPTR(query);
			for (i = 0; i < *nentries; i++)
				res[i] = Int32GetDatum(arr[i]);
		}
	}

	if (nentries == 0)
	{
		switch (strategy)
		{
			case BooleanSearchStrategy:
			case RTOverlapStrategyNumber:
				*nentries = -1; /* nobody can be found */
				break;
			default:			/* require fullscan: GIN can't find void
								 * arrays */
				break;
		}
	}

	PG_RETURN_POINTER(res);
}

PG_FUNCTION_INFO_V1(ginint4_consistent);
Datum		ginint4_consistent(PG_FUNCTION_ARGS);

Datum
ginint4_consistent(PG_FUNCTION_ARGS)
{
	bool	   *check = (bool *) PG_GETARG_POINTER(0);
	StrategyNumber strategy = PG_GETARG_UINT16(1);
	bool	   *recheck = (bool *) PG_GETARG_POINTER(3);
	bool		res = FALSE;

	/*
	 * we need not check array carefully, it's done by previous
	 * ginarrayextract call
	 */

	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:
		case RTOldContainedByStrategyNumber:
			/* we will need recheck */
			*recheck = true;
			/* at least one element in check[] is true, so result = true */
			res = TRUE;
			break;
		case RTSameStrategyNumber:
			{
				ArrayType  *query = PG_GETARG_ARRAYTYPE_P(2);
				int			i,
							nentries = ARRNELEMS(query);

				/* we will need recheck */
				*recheck = true;
				res = TRUE;
				for (i = 0; i < nentries; i++)
					if (!check[i])
					{
						res = FALSE;
						break;
					}
			}
			break;
		case RTContainsStrategyNumber:
		case RTOldContainsStrategyNumber:
			{
				ArrayType  *query = PG_GETARG_ARRAYTYPE_P(2);
				int			i,
							nentries = ARRNELEMS(query);

				/* result is not lossy */
				*recheck = false;
				res = TRUE;
				for (i = 0; i < nentries; i++)
					if (!check[i])
					{
						res = FALSE;
						break;
					}
			}
			break;
		case BooleanSearchStrategy:
			{
				QUERYTYPE  *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_POINTER(2));

				/* result is not lossy */
				*recheck = false;
				res = ginconsistent(query, check);
			}
			break;
		default:
			elog(ERROR, "ginint4_consistent: unknown strategy number: %d",
				 strategy);
	}

	PG_RETURN_BOOL(res);
}