|
7 | 7 |
|
8 | 8 | #include "access/gin.h"
|
9 | 9 | #include "access/itup.h"
|
| 10 | +#include "access/skey.h" |
10 | 11 | #include "access/tuptoaster.h"
|
11 | 12 | #include "storage/bufpage.h"
|
12 | 13 | #include "utils/array.h"
|
|
16 | 17 | PG_FUNCTION_INFO_V1(gin_extract_trgm);
|
17 | 18 | Datum gin_extract_trgm(PG_FUNCTION_ARGS);
|
18 | 19 |
|
| 20 | +PG_FUNCTION_INFO_V1(gin_extract_value_trgm); |
| 21 | +Datum gin_extract_value_trgm(PG_FUNCTION_ARGS); |
| 22 | + |
| 23 | +PG_FUNCTION_INFO_V1(gin_extract_query_trgm); |
| 24 | +Datum gin_extract_query_trgm(PG_FUNCTION_ARGS); |
| 25 | + |
19 | 26 | PG_FUNCTION_INFO_V1(gin_trgm_consistent);
|
20 | 27 | Datum gin_trgm_consistent(PG_FUNCTION_ARGS);
|
21 | 28 |
|
22 | 29 | /*
|
23 |
| - * This function is used as both extractValue and extractQuery |
| 30 | + * This function can only be called if a pre-9.1 version of the GIN operator |
| 31 | + * class definition is present in the catalogs (probably as a consequence |
| 32 | + * of upgrade-in-place). Complain. |
24 | 33 | */
|
25 | 34 | Datum
|
26 | 35 | gin_extract_trgm(PG_FUNCTION_ARGS)
|
| 36 | +{ |
| 37 | + ereport(ERROR, |
| 38 | + (errmsg("GIN operator class for pg_trgm is out of date"), |
| 39 | + errhint("Please drop and re-create the pg_trgm catalog entries."))); |
| 40 | + PG_RETURN_NULL(); |
| 41 | +} |
| 42 | + |
| 43 | +Datum |
| 44 | +gin_extract_value_trgm(PG_FUNCTION_ARGS) |
27 | 45 | {
|
28 | 46 | text *val = (text *) PG_GETARG_TEXT_P(0);
|
29 | 47 | int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
@@ -57,34 +75,124 @@ gin_extract_trgm(PG_FUNCTION_ARGS)
|
57 | 75 | PG_RETURN_POINTER(entries);
|
58 | 76 | }
|
59 | 77 |
|
| 78 | +Datum |
| 79 | +gin_extract_query_trgm(PG_FUNCTION_ARGS) |
| 80 | +{ |
| 81 | + text *val = (text *) PG_GETARG_TEXT_P(0); |
| 82 | + int32 *nentries = (int32 *) PG_GETARG_POINTER(1); |
| 83 | + StrategyNumber strategy = PG_GETARG_UINT16(2); |
| 84 | + /* bool **pmatch = (bool **) PG_GETARG_POINTER(3); */ |
| 85 | + /* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */ |
| 86 | + /* bool **nullFlags = (bool **) PG_GETARG_POINTER(5); */ |
| 87 | + int32 *searchMode = (int32 *) PG_GETARG_POINTER(6); |
| 88 | + Datum *entries = NULL; |
| 89 | + TRGM *trg; |
| 90 | + int32 trglen; |
| 91 | + trgm *ptr; |
| 92 | + int32 i; |
| 93 | + |
| 94 | + switch (strategy) |
| 95 | + { |
| 96 | + case SimilarityStrategyNumber: |
| 97 | + trg = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ); |
| 98 | + break; |
| 99 | + case ILikeStrategyNumber: |
| 100 | +#ifndef IGNORECASE |
| 101 | + elog(ERROR, "cannot handle ~~* with case-sensitive trigrams"); |
| 102 | +#endif |
| 103 | + /* FALL THRU */ |
| 104 | + case LikeStrategyNumber: |
| 105 | + /* |
| 106 | + * For wildcard search we extract all the trigrams that every |
| 107 | + * potentially-matching string must include. |
| 108 | + */ |
| 109 | + trg = generate_wildcard_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ); |
| 110 | + break; |
| 111 | + default: |
| 112 | + elog(ERROR, "unrecognized strategy number: %d", strategy); |
| 113 | + trg = NULL; /* keep compiler quiet */ |
| 114 | + break; |
| 115 | + } |
| 116 | + |
| 117 | + trglen = ARRNELEM(trg); |
| 118 | + *nentries = trglen; |
| 119 | + |
| 120 | + if (trglen > 0) |
| 121 | + { |
| 122 | + entries = (Datum *) palloc(sizeof(Datum) * trglen); |
| 123 | + ptr = GETARR(trg); |
| 124 | + for (i = 0; i < trglen; i++) |
| 125 | + { |
| 126 | + int32 item = trgm2int(ptr); |
| 127 | + |
| 128 | + entries[i] = Int32GetDatum(item); |
| 129 | + ptr++; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /* |
| 134 | + * If no trigram was extracted then we have to scan all the index. |
| 135 | + */ |
| 136 | + if (trglen == 0) |
| 137 | + *searchMode = GIN_SEARCH_MODE_ALL; |
| 138 | + |
| 139 | + PG_RETURN_POINTER(entries); |
| 140 | +} |
| 141 | + |
60 | 142 | Datum
|
61 | 143 | gin_trgm_consistent(PG_FUNCTION_ARGS)
|
62 | 144 | {
|
63 | 145 | bool *check = (bool *) PG_GETARG_POINTER(0);
|
64 |
| - /* StrategyNumber strategy = PG_GETARG_UINT16(1); */ |
| 146 | + StrategyNumber strategy = PG_GETARG_UINT16(1); |
65 | 147 | /* text *query = PG_GETARG_TEXT_P(2); */
|
66 | 148 | int32 nkeys = PG_GETARG_INT32(3);
|
67 | 149 | /* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
|
68 | 150 | bool *recheck = (bool *) PG_GETARG_POINTER(5);
|
69 |
| - bool res = FALSE; |
| 151 | + bool res; |
70 | 152 | int32 i,
|
71 |
| - ntrue = 0; |
| 153 | + ntrue; |
72 | 154 |
|
73 | 155 | /* All cases served by this function are inexact */
|
74 | 156 | *recheck = true;
|
75 | 157 |
|
76 |
| - /* Count the matches */ |
77 |
| - for (i = 0; i < nkeys; i++) |
| 158 | + switch (strategy) |
78 | 159 | {
|
79 |
| - if (check[i]) |
80 |
| - ntrue++; |
81 |
| - } |
82 |
| - |
| 160 | + case SimilarityStrategyNumber: |
| 161 | + /* Count the matches */ |
| 162 | + ntrue = 0; |
| 163 | + for (i = 0; i < nkeys; i++) |
| 164 | + { |
| 165 | + if (check[i]) |
| 166 | + ntrue++; |
| 167 | + } |
83 | 168 | #ifdef DIVUNION
|
84 |
| - res = (nkeys == ntrue) ? true : ((((((float4) ntrue) / ((float4) (nkeys - ntrue)))) >= trgm_limit) ? true : false); |
| 169 | + res = (nkeys == ntrue) ? true : ((((((float4) ntrue) / ((float4) (nkeys - ntrue)))) >= trgm_limit) ? true : false); |
85 | 170 | #else
|
86 |
| - res = (nkeys == 0) ? false : ((((((float4) ntrue) / ((float4) nkeys))) >= trgm_limit) ? true : false); |
| 171 | + res = (nkeys == 0) ? false : ((((((float4) ntrue) / ((float4) nkeys))) >= trgm_limit) ? true : false); |
87 | 172 | #endif
|
| 173 | + break; |
| 174 | + case ILikeStrategyNumber: |
| 175 | +#ifndef IGNORECASE |
| 176 | + elog(ERROR, "cannot handle ~~* with case-sensitive trigrams"); |
| 177 | +#endif |
| 178 | + /* FALL THRU */ |
| 179 | + case LikeStrategyNumber: |
| 180 | + /* Check if all extracted trigrams are presented. */ |
| 181 | + res = true; |
| 182 | + for (i = 0; i < nkeys; i++) |
| 183 | + { |
| 184 | + if (!check[i]) |
| 185 | + { |
| 186 | + res = false; |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + break; |
| 191 | + default: |
| 192 | + elog(ERROR, "unrecognized strategy number: %d", strategy); |
| 193 | + res = false; /* keep compiler quiet */ |
| 194 | + break; |
| 195 | + } |
88 | 196 |
|
89 | 197 | PG_RETURN_BOOL(res);
|
90 | 198 | }
|
0 commit comments