PostgreSQL Source Code git master
Loading...
Searching...
No Matches
like.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * like.c
4 * like expression handling code.
5 *
6 * NOTES
7 * A big hack of the regexp.c code!! Contributed by
8 * Keith Parks <emkxp01@mtcc.demon.co.uk> (7/95).
9 *
10 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
12 *
13 * IDENTIFICATION
14 * src/backend/utils/adt/like.c
15 *
16 *-------------------------------------------------------------------------
17 */
18#include "postgres.h"
19
20#include <ctype.h>
21
23#include "mb/pg_wchar.h"
24#include "miscadmin.h"
25#include "utils/fmgrprotos.h"
26#include "utils/pg_locale.h"
27#include "varatt.h"
28
29
30#define LIKE_TRUE 1
31#define LIKE_FALSE 0
32#define LIKE_ABORT (-1)
33
34
35static int SB_MatchText(const char *t, int tlen, const char *p, int plen,
36 pg_locale_t locale);
38
39static int MB_MatchText(const char *t, int tlen, const char *p, int plen,
40 pg_locale_t locale);
42
43static int UTF8_MatchText(const char *t, int tlen, const char *p, int plen,
44 pg_locale_t locale);
45
46static int C_IMatchText(const char *t, int tlen, const char *p, int plen,
47 pg_locale_t locale);
48
49static int GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation);
50static int Generic_Text_IC_like(text *str, text *pat, Oid collation);
51
52/*--------------------
53 * Support routine for MatchText. Compares given multibyte streams
54 * as wide characters. If they match, returns 1 otherwise returns 0.
55 *--------------------
56 */
57static inline int
58wchareq(const char *p1, int p1len, const char *p2, int p2len)
59{
60 int p1clen;
61
62 /* Optimization: quickly compare the first byte. */
63 if (*p1 != *p2)
64 return 0;
65
68 return 0;
69
70 /* They are the same length */
71 while (p1clen--)
72 {
73 if (*p1++ != *p2++)
74 return 0;
75 }
76 return 1;
77}
78
79/*
80 * Formerly we had a routine iwchareq() here that tried to do case-insensitive
81 * comparison of multibyte characters. It did not work at all, however,
82 * because it relied on tolower() which has a single-byte API ... and
83 * towlower() wouldn't be much better since we have no suitably cheap way
84 * of getting a single character transformed to the system's wchar_t format.
85 * So now, we just downcase the strings using lower() and apply regular LIKE
86 * comparison. This should be revisited when we install better locale support.
87 *
88 * We do handle case-insensitive matching for the C locale using
89 * fold-on-the-fly processing, however.
90 */
91
92
93#define NextByte(p, plen) ((p)++, (plen)--)
94
95/* Set up to compile like_match.c for multibyte characters */
96#define CHAREQ(p1, p1len, p2, p2len) wchareq((p1), (p1len), (p2), (p2len))
97#define NextChar(p, plen) \
98 do { int __l = pg_mblen_with_len((p), (plen)); (p) +=__l; (plen) -=__l; } while (0)
99#define CopyAdvChar(dst, src, srclen) \
100 do { int __l = pg_mblen_with_len((src), (srclen)); \
101 (srclen) -= __l; \
102 while (__l-- > 0) \
103 *(dst)++ = *(src)++; \
104 } while (0)
105
106#define MatchText MB_MatchText
107#define do_like_escape MB_do_like_escape
108
109#include "like_match.c"
110
111/* Set up to compile like_match.c for single-byte characters */
112#define CHAREQ(p1, p1len, p2, p2len) (*(p1) == *(p2))
113#define NextChar(p, plen) NextByte((p), (plen))
114#define CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
115
116#define MatchText SB_MatchText
117#define do_like_escape SB_do_like_escape
118
119#include "like_match.c"
120
121/* setup to compile like_match.c for case-insensitive matches in C locale */
122#define MATCH_LOWER
123#define NextChar(p, plen) NextByte((p), (plen))
124#define MatchText C_IMatchText
125
126#include "like_match.c"
127
128/* setup to compile like_match.c for UTF8 encoding, using fast NextChar */
129
130#define NextChar(p, plen) \
131 do { (p)++; (plen)--; } while ((plen) > 0 && (*(p) & 0xC0) == 0x80 )
132#define MatchText UTF8_MatchText
133
134#include "like_match.c"
135
136/* Generic for all cases not requiring inline case-folding */
137static inline int
138GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation)
139{
140 pg_locale_t locale;
141
142 if (!OidIsValid(collation))
143 {
144 /*
145 * This typically means that the parser could not resolve a conflict
146 * of implicit collations, so report it that way.
147 */
150 errmsg("could not determine which collation to use for LIKE"),
151 errhint("Use the COLLATE clause to set the collation explicitly.")));
152 }
153
154 locale = pg_newlocale_from_collation(collation);
155
157 return SB_MatchText(s, slen, p, plen, locale);
158 else if (GetDatabaseEncoding() == PG_UTF8)
159 return UTF8_MatchText(s, slen, p, plen, locale);
160 else
161 return MB_MatchText(s, slen, p, plen, locale);
162}
163
164static inline int
166{
167 char *s,
168 *p;
169 int slen,
170 plen;
171 pg_locale_t locale;
172
173 if (!OidIsValid(collation))
174 {
175 /*
176 * This typically means that the parser could not resolve a conflict
177 * of implicit collations, so report it that way.
178 */
181 errmsg("could not determine which collation to use for ILIKE"),
182 errhint("Use the COLLATE clause to set the collation explicitly.")));
183 }
184
185 locale = pg_newlocale_from_collation(collation);
186
187 if (!locale->deterministic)
190 errmsg("nondeterministic collations are not supported for ILIKE")));
191
192 /*
193 * For efficiency reasons, in the C locale we don't call lower() on the
194 * pattern and text, but instead lowercase each character lazily. This
195 * only works for single-byte encodings, otherwise "_" may incorrectly
196 * match an incomplete byte sequence.
197 *
198 * XXX: use casefolding instead?
199 */
200
201 if (locale->ctype_is_c && pg_database_encoding_max_length() == 1)
202 {
203 p = VARDATA_ANY(pat);
204 plen = VARSIZE_ANY_EXHDR(pat);
205 s = VARDATA_ANY(str);
207 return C_IMatchText(s, slen, p, plen, locale);
208 }
209 else
210 {
213 p = VARDATA_ANY(pat);
214 plen = VARSIZE_ANY_EXHDR(pat);
217 s = VARDATA_ANY(str);
219
221 return UTF8_MatchText(s, slen, p, plen, 0);
222 else if (pg_database_encoding_max_length() > 1)
223 return MB_MatchText(s, slen, p, plen, 0);
224 else
225 return SB_MatchText(s, slen, p, plen, 0);
226 }
227}
228
229/*
230 * interface routines called by the function manager
231 */
232
233Datum
235{
238 bool result;
239 char *s,
240 *p;
241 int slen,
242 plen;
243
244 s = NameStr(*str);
245 slen = strlen(s);
246 p = VARDATA_ANY(pat);
247 plen = VARSIZE_ANY_EXHDR(pat);
248
250
252}
253
254Datum
256{
259 bool result;
260 char *s,
261 *p;
262 int slen,
263 plen;
264
265 s = NameStr(*str);
266 slen = strlen(s);
267 p = VARDATA_ANY(pat);
268 plen = VARSIZE_ANY_EXHDR(pat);
269
271
273}
274
275Datum
277{
280 bool result;
281 char *s,
282 *p;
283 int slen,
284 plen;
285
286 s = VARDATA_ANY(str);
288 p = VARDATA_ANY(pat);
289 plen = VARSIZE_ANY_EXHDR(pat);
290
292
294}
295
296Datum
298{
301 bool result;
302 char *s,
303 *p;
304 int slen,
305 plen;
306
307 s = VARDATA_ANY(str);
309 p = VARDATA_ANY(pat);
310 plen = VARSIZE_ANY_EXHDR(pat);
311
313
315}
316
317Datum
319{
322 bool result;
323 char *s,
324 *p;
325 int slen,
326 plen;
327
328 s = VARDATA_ANY(str);
330 p = VARDATA_ANY(pat);
331 plen = VARSIZE_ANY_EXHDR(pat);
332
333 result = (SB_MatchText(s, slen, p, plen, 0) == LIKE_TRUE);
334
336}
337
338Datum
340{
343 bool result;
344 char *s,
345 *p;
346 int slen,
347 plen;
348
349 s = VARDATA_ANY(str);
351 p = VARDATA_ANY(pat);
352 plen = VARSIZE_ANY_EXHDR(pat);
353
354 result = (SB_MatchText(s, slen, p, plen, 0) != LIKE_TRUE);
355
357}
358
359/*
360 * Case-insensitive versions
361 */
362
363Datum
377
378Datum
392
393Datum
404
405Datum
416
417/*
418 * like_escape() --- given a pattern and an ESCAPE string,
419 * convert the pattern to use Postgres' standard backslash escape convention.
420 */
421Datum
435
436/*
437 * like_escape_bytea() --- given a pattern and an ESCAPE string,
438 * convert the pattern to use Postgres' standard backslash escape convention.
439 */
440Datum
#define NameStr(name)
Definition c.h:894
#define OidIsValid(objectId)
Definition c.h:917
uint32 result
int errcode(int sqlerrcode)
Definition elog.c:875
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
Datum DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
Definition fmgr.c:794
#define PG_GETARG_BYTEA_PP(n)
Definition fmgr.h:309
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
#define DatumGetTextPP(X)
Definition fmgr.h:293
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:688
#define PG_GETARG_NAME(n)
Definition fmgr.h:279
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
#define PG_GET_COLLATION()
Definition fmgr.h:198
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
const char * str
static int UTF8_MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
static int MB_MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
static text * MB_do_like_escape(text *pat, text *esc)
#define LIKE_TRUE
Definition like.c:30
Datum texticnlike(PG_FUNCTION_ARGS)
Definition like.c:406
Datum textlike(PG_FUNCTION_ARGS)
Definition like.c:276
Datum namelike(PG_FUNCTION_ARGS)
Definition like.c:234
Datum nameiclike(PG_FUNCTION_ARGS)
Definition like.c:364
Datum byteanlike(PG_FUNCTION_ARGS)
Definition like.c:339
static int C_IMatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
Datum like_escape(PG_FUNCTION_ARGS)
Definition like.c:422
Datum namenlike(PG_FUNCTION_ARGS)
Definition like.c:255
Datum textnlike(PG_FUNCTION_ARGS)
Definition like.c:297
Datum nameicnlike(PG_FUNCTION_ARGS)
Definition like.c:379
Datum like_escape_bytea(PG_FUNCTION_ARGS)
Definition like.c:441
static text * SB_do_like_escape(text *pat, text *esc)
Datum texticlike(PG_FUNCTION_ARGS)
Definition like.c:394
static int SB_MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
static int Generic_Text_IC_like(text *str, text *pat, Oid collation)
Definition like.c:165
Datum bytealike(PG_FUNCTION_ARGS)
Definition like.c:318
static int wchareq(const char *p1, int p1len, const char *p2, int p2len)
Definition like.c:58
static int GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation)
Definition like.c:138
#define PG_UTF8
Definition mbprint.c:43
int GetDatabaseEncoding(void)
Definition mbutils.c:1389
int pg_mblen_with_len(const char *mbstr, int limit)
Definition mbutils.c:1108
int pg_database_encoding_max_length(void)
Definition mbutils.c:1673
static char * errmsg
Datum lower(PG_FUNCTION_ARGS)
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition pg_locale.c:1189
static Datum NameGetDatum(const NameData *X)
Definition postgres.h:406
uint64_t Datum
Definition postgres.h:70
#define PointerGetDatum(X)
Definition postgres.h:354
unsigned int Oid
static int fb(int x)
Definition c.h:889
Definition c.h:835
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
Datum name_text(PG_FUNCTION_ARGS)
Definition varlena.c:2705