LCOV - code coverage report
Current view: top level - src/backend/utils/adt - like.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 116 134 86.6 %
Date: 2025-01-18 04:15:08 Functions: 15 16 93.8 %
Legend: Lines: hit not hit

          Line data    Source code
       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-2025, 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             : 
      22             : #include "catalog/pg_collation.h"
      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             : 
      35             : static int  SB_MatchText(const char *t, int tlen, const char *p, int plen,
      36             :                          pg_locale_t locale);
      37             : static text *SB_do_like_escape(text *pat, text *esc);
      38             : 
      39             : static int  MB_MatchText(const char *t, int tlen, const char *p, int plen,
      40             :                          pg_locale_t locale);
      41             : static text *MB_do_like_escape(text *pat, text *esc);
      42             : 
      43             : static int  UTF8_MatchText(const char *t, int tlen, const char *p, int plen,
      44             :                            pg_locale_t locale);
      45             : 
      46             : static int  SB_IMatchText(const char *t, int tlen, const char *p, int plen,
      47             :                           pg_locale_t locale);
      48             : 
      49             : static int  GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation);
      50             : static 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             :  */
      57             : static inline int
      58         924 : wchareq(const char *p1, const char *p2)
      59             : {
      60             :     int         p1_len;
      61             : 
      62             :     /* Optimization:  quickly compare the first byte. */
      63         924 :     if (*p1 != *p2)
      64         696 :         return 0;
      65             : 
      66         228 :     p1_len = pg_mblen(p1);
      67         228 :     if (pg_mblen(p2) != p1_len)
      68           0 :         return 0;
      69             : 
      70             :     /* They are the same length */
      71         456 :     while (p1_len--)
      72             :     {
      73         228 :         if (*p1++ != *p2++)
      74           0 :             return 0;
      75             :     }
      76         228 :     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             : 
      89             : /*
      90             :  * We do handle case-insensitive matching for single-byte encodings using
      91             :  * fold-on-the-fly processing, however.
      92             :  */
      93             : static char
      94           0 : SB_lower_char(unsigned char c, pg_locale_t locale)
      95             : {
      96           0 :     if (locale->ctype_is_c)
      97           0 :         return pg_ascii_tolower(c);
      98           0 :     else if (locale->is_default)
      99           0 :         return pg_tolower(c);
     100             :     else
     101           0 :         return tolower_l(c, locale->info.lt);
     102             : }
     103             : 
     104             : 
     105             : #define NextByte(p, plen)   ((p)++, (plen)--)
     106             : 
     107             : /* Set up to compile like_match.c for multibyte characters */
     108             : #define CHAREQ(p1, p2) wchareq((p1), (p2))
     109             : #define NextChar(p, plen) \
     110             :     do { int __l = pg_mblen(p); (p) +=__l; (plen) -=__l; } while (0)
     111             : #define CopyAdvChar(dst, src, srclen) \
     112             :     do { int __l = pg_mblen(src); \
     113             :          (srclen) -= __l; \
     114             :          while (__l-- > 0) \
     115             :              *(dst)++ = *(src)++; \
     116             :        } while (0)
     117             : 
     118             : #define MatchText   MB_MatchText
     119             : #define do_like_escape  MB_do_like_escape
     120             : 
     121             : #include "like_match.c"
     122             : 
     123             : /* Set up to compile like_match.c for single-byte characters */
     124             : #define CHAREQ(p1, p2) (*(p1) == *(p2))
     125             : #define NextChar(p, plen) NextByte((p), (plen))
     126             : #define CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
     127             : 
     128             : #define MatchText   SB_MatchText
     129             : #define do_like_escape  SB_do_like_escape
     130             : 
     131             : #include "like_match.c"
     132             : 
     133             : /* setup to compile like_match.c for single byte case insensitive matches */
     134             : #define MATCH_LOWER(t, locale) SB_lower_char((unsigned char) (t), locale)
     135             : #define NextChar(p, plen) NextByte((p), (plen))
     136             : #define MatchText SB_IMatchText
     137             : 
     138             : #include "like_match.c"
     139             : 
     140             : /* setup to compile like_match.c for UTF8 encoding, using fast NextChar */
     141             : 
     142             : #define NextChar(p, plen) \
     143             :     do { (p)++; (plen)--; } while ((plen) > 0 && (*(p) & 0xC0) == 0x80 )
     144             : #define MatchText   UTF8_MatchText
     145             : 
     146             : #include "like_match.c"
     147             : 
     148             : /* Generic for all cases not requiring inline case-folding */
     149             : static inline int
     150     1010788 : GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation)
     151             : {
     152             :     pg_locale_t locale;
     153             : 
     154     1010788 :     if (!OidIsValid(collation))
     155             :     {
     156             :         /*
     157             :          * This typically means that the parser could not resolve a conflict
     158             :          * of implicit collations, so report it that way.
     159             :          */
     160           0 :         ereport(ERROR,
     161             :                 (errcode(ERRCODE_INDETERMINATE_COLLATION),
     162             :                  errmsg("could not determine which collation to use for LIKE"),
     163             :                  errhint("Use the COLLATE clause to set the collation explicitly.")));
     164             :     }
     165             : 
     166     1010788 :     locale = pg_newlocale_from_collation(collation);
     167             : 
     168     1010788 :     if (pg_database_encoding_max_length() == 1)
     169       80136 :         return SB_MatchText(s, slen, p, plen, locale);
     170      930652 :     else if (GetDatabaseEncoding() == PG_UTF8)
     171      930652 :         return UTF8_MatchText(s, slen, p, plen, locale);
     172             :     else
     173           0 :         return MB_MatchText(s, slen, p, plen, locale);
     174             : }
     175             : 
     176             : static inline int
     177       84898 : Generic_Text_IC_like(text *str, text *pat, Oid collation)
     178             : {
     179             :     char       *s,
     180             :                *p;
     181             :     int         slen,
     182             :                 plen;
     183             :     pg_locale_t locale;
     184             : 
     185       84898 :     if (!OidIsValid(collation))
     186             :     {
     187             :         /*
     188             :          * This typically means that the parser could not resolve a conflict
     189             :          * of implicit collations, so report it that way.
     190             :          */
     191           0 :         ereport(ERROR,
     192             :                 (errcode(ERRCODE_INDETERMINATE_COLLATION),
     193             :                  errmsg("could not determine which collation to use for ILIKE"),
     194             :                  errhint("Use the COLLATE clause to set the collation explicitly.")));
     195             :     }
     196             : 
     197       84898 :     locale = pg_newlocale_from_collation(collation);
     198             : 
     199       84898 :     if (!locale->deterministic)
     200          12 :         ereport(ERROR,
     201             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     202             :                  errmsg("nondeterministic collations are not supported for ILIKE")));
     203             : 
     204             :     /*
     205             :      * For efficiency reasons, in the single byte case we don't call lower()
     206             :      * on the pattern and text, but instead call SB_lower_char on each
     207             :      * character.  In the multi-byte case we don't have much choice :-(. Also,
     208             :      * ICU does not support single-character case folding, so we go the long
     209             :      * way.
     210             :      */
     211             : 
     212       84886 :     if (pg_database_encoding_max_length() > 1 || (locale->provider == COLLPROVIDER_ICU))
     213             :     {
     214       84886 :         pat = DatumGetTextPP(DirectFunctionCall1Coll(lower, collation,
     215             :                                                      PointerGetDatum(pat)));
     216       84886 :         p = VARDATA_ANY(pat);
     217       84886 :         plen = VARSIZE_ANY_EXHDR(pat);
     218       84886 :         str = DatumGetTextPP(DirectFunctionCall1Coll(lower, collation,
     219             :                                                      PointerGetDatum(str)));
     220       84886 :         s = VARDATA_ANY(str);
     221       84886 :         slen = VARSIZE_ANY_EXHDR(str);
     222       84886 :         if (GetDatabaseEncoding() == PG_UTF8)
     223       84886 :             return UTF8_MatchText(s, slen, p, plen, 0);
     224             :         else
     225           0 :             return MB_MatchText(s, slen, p, plen, 0);
     226             :     }
     227             :     else
     228             :     {
     229           0 :         p = VARDATA_ANY(pat);
     230           0 :         plen = VARSIZE_ANY_EXHDR(pat);
     231           0 :         s = VARDATA_ANY(str);
     232           0 :         slen = VARSIZE_ANY_EXHDR(str);
     233           0 :         return SB_IMatchText(s, slen, p, plen, locale);
     234             :     }
     235             : }
     236             : 
     237             : /*
     238             :  *  interface routines called by the function manager
     239             :  */
     240             : 
     241             : Datum
     242      160556 : namelike(PG_FUNCTION_ARGS)
     243             : {
     244      160556 :     Name        str = PG_GETARG_NAME(0);
     245      160556 :     text       *pat = PG_GETARG_TEXT_PP(1);
     246             :     bool        result;
     247             :     char       *s,
     248             :                *p;
     249             :     int         slen,
     250             :                 plen;
     251             : 
     252      160556 :     s = NameStr(*str);
     253      160556 :     slen = strlen(s);
     254      160556 :     p = VARDATA_ANY(pat);
     255      160556 :     plen = VARSIZE_ANY_EXHDR(pat);
     256             : 
     257      160556 :     result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) == LIKE_TRUE);
     258             : 
     259      160556 :     PG_RETURN_BOOL(result);
     260             : }
     261             : 
     262             : Datum
     263        5374 : namenlike(PG_FUNCTION_ARGS)
     264             : {
     265        5374 :     Name        str = PG_GETARG_NAME(0);
     266        5374 :     text       *pat = PG_GETARG_TEXT_PP(1);
     267             :     bool        result;
     268             :     char       *s,
     269             :                *p;
     270             :     int         slen,
     271             :                 plen;
     272             : 
     273        5374 :     s = NameStr(*str);
     274        5374 :     slen = strlen(s);
     275        5374 :     p = VARDATA_ANY(pat);
     276        5374 :     plen = VARSIZE_ANY_EXHDR(pat);
     277             : 
     278        5374 :     result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) != LIKE_TRUE);
     279             : 
     280        5374 :     PG_RETURN_BOOL(result);
     281             : }
     282             : 
     283             : Datum
     284      526936 : textlike(PG_FUNCTION_ARGS)
     285             : {
     286      526936 :     text       *str = PG_GETARG_TEXT_PP(0);
     287      526936 :     text       *pat = PG_GETARG_TEXT_PP(1);
     288             :     bool        result;
     289             :     char       *s,
     290             :                *p;
     291             :     int         slen,
     292             :                 plen;
     293             : 
     294      526936 :     s = VARDATA_ANY(str);
     295      526936 :     slen = VARSIZE_ANY_EXHDR(str);
     296      526936 :     p = VARDATA_ANY(pat);
     297      526936 :     plen = VARSIZE_ANY_EXHDR(pat);
     298             : 
     299      526936 :     result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) == LIKE_TRUE);
     300             : 
     301      526930 :     PG_RETURN_BOOL(result);
     302             : }
     303             : 
     304             : Datum
     305      317922 : textnlike(PG_FUNCTION_ARGS)
     306             : {
     307      317922 :     text       *str = PG_GETARG_TEXT_PP(0);
     308      317922 :     text       *pat = PG_GETARG_TEXT_PP(1);
     309             :     bool        result;
     310             :     char       *s,
     311             :                *p;
     312             :     int         slen,
     313             :                 plen;
     314             : 
     315      317922 :     s = VARDATA_ANY(str);
     316      317922 :     slen = VARSIZE_ANY_EXHDR(str);
     317      317922 :     p = VARDATA_ANY(pat);
     318      317922 :     plen = VARSIZE_ANY_EXHDR(pat);
     319             : 
     320      317922 :     result = (GenericMatchText(s, slen, p, plen, PG_GET_COLLATION()) != LIKE_TRUE);
     321             : 
     322      317922 :     PG_RETURN_BOOL(result);
     323             : }
     324             : 
     325             : Datum
     326          12 : bytealike(PG_FUNCTION_ARGS)
     327             : {
     328          12 :     bytea      *str = PG_GETARG_BYTEA_PP(0);
     329          12 :     bytea      *pat = PG_GETARG_BYTEA_PP(1);
     330             :     bool        result;
     331             :     char       *s,
     332             :                *p;
     333             :     int         slen,
     334             :                 plen;
     335             : 
     336          12 :     s = VARDATA_ANY(str);
     337          12 :     slen = VARSIZE_ANY_EXHDR(str);
     338          12 :     p = VARDATA_ANY(pat);
     339          12 :     plen = VARSIZE_ANY_EXHDR(pat);
     340             : 
     341          12 :     result = (SB_MatchText(s, slen, p, plen, 0) == LIKE_TRUE);
     342             : 
     343          12 :     PG_RETURN_BOOL(result);
     344             : }
     345             : 
     346             : Datum
     347          12 : byteanlike(PG_FUNCTION_ARGS)
     348             : {
     349          12 :     bytea      *str = PG_GETARG_BYTEA_PP(0);
     350          12 :     bytea      *pat = PG_GETARG_BYTEA_PP(1);
     351             :     bool        result;
     352             :     char       *s,
     353             :                *p;
     354             :     int         slen,
     355             :                 plen;
     356             : 
     357          12 :     s = VARDATA_ANY(str);
     358          12 :     slen = VARSIZE_ANY_EXHDR(str);
     359          12 :     p = VARDATA_ANY(pat);
     360          12 :     plen = VARSIZE_ANY_EXHDR(pat);
     361             : 
     362          12 :     result = (SB_MatchText(s, slen, p, plen, 0) != LIKE_TRUE);
     363             : 
     364          12 :     PG_RETURN_BOOL(result);
     365             : }
     366             : 
     367             : /*
     368             :  * Case-insensitive versions
     369             :  */
     370             : 
     371             : Datum
     372       16376 : nameiclike(PG_FUNCTION_ARGS)
     373             : {
     374       16376 :     Name        str = PG_GETARG_NAME(0);
     375       16376 :     text       *pat = PG_GETARG_TEXT_PP(1);
     376             :     bool        result;
     377             :     text       *strtext;
     378             : 
     379       16376 :     strtext = DatumGetTextPP(DirectFunctionCall1(name_text,
     380             :                                                  NameGetDatum(str)));
     381       16376 :     result = (Generic_Text_IC_like(strtext, pat, PG_GET_COLLATION()) == LIKE_TRUE);
     382             : 
     383       16376 :     PG_RETURN_BOOL(result);
     384             : }
     385             : 
     386             : Datum
     387           6 : nameicnlike(PG_FUNCTION_ARGS)
     388             : {
     389           6 :     Name        str = PG_GETARG_NAME(0);
     390           6 :     text       *pat = PG_GETARG_TEXT_PP(1);
     391             :     bool        result;
     392             :     text       *strtext;
     393             : 
     394           6 :     strtext = DatumGetTextPP(DirectFunctionCall1(name_text,
     395             :                                                  NameGetDatum(str)));
     396           6 :     result = (Generic_Text_IC_like(strtext, pat, PG_GET_COLLATION()) != LIKE_TRUE);
     397             : 
     398           6 :     PG_RETURN_BOOL(result);
     399             : }
     400             : 
     401             : Datum
     402       68460 : texticlike(PG_FUNCTION_ARGS)
     403             : {
     404       68460 :     text       *str = PG_GETARG_TEXT_PP(0);
     405       68460 :     text       *pat = PG_GETARG_TEXT_PP(1);
     406             :     bool        result;
     407             : 
     408       68460 :     result = (Generic_Text_IC_like(str, pat, PG_GET_COLLATION()) == LIKE_TRUE);
     409             : 
     410       68448 :     PG_RETURN_BOOL(result);
     411             : }
     412             : 
     413             : Datum
     414          56 : texticnlike(PG_FUNCTION_ARGS)
     415             : {
     416          56 :     text       *str = PG_GETARG_TEXT_PP(0);
     417          56 :     text       *pat = PG_GETARG_TEXT_PP(1);
     418             :     bool        result;
     419             : 
     420          56 :     result = (Generic_Text_IC_like(str, pat, PG_GET_COLLATION()) != LIKE_TRUE);
     421             : 
     422          56 :     PG_RETURN_BOOL(result);
     423             : }
     424             : 
     425             : /*
     426             :  * like_escape() --- given a pattern and an ESCAPE string,
     427             :  * convert the pattern to use Postgres' standard backslash escape convention.
     428             :  */
     429             : Datum
     430         212 : like_escape(PG_FUNCTION_ARGS)
     431             : {
     432         212 :     text       *pat = PG_GETARG_TEXT_PP(0);
     433         212 :     text       *esc = PG_GETARG_TEXT_PP(1);
     434             :     text       *result;
     435             : 
     436         212 :     if (pg_database_encoding_max_length() == 1)
     437           0 :         result = SB_do_like_escape(pat, esc);
     438             :     else
     439         212 :         result = MB_do_like_escape(pat, esc);
     440             : 
     441         212 :     PG_RETURN_TEXT_P(result);
     442             : }
     443             : 
     444             : /*
     445             :  * like_escape_bytea() --- given a pattern and an ESCAPE string,
     446             :  * convert the pattern to use Postgres' standard backslash escape convention.
     447             :  */
     448             : Datum
     449          12 : like_escape_bytea(PG_FUNCTION_ARGS)
     450             : {
     451          12 :     bytea      *pat = PG_GETARG_BYTEA_PP(0);
     452          12 :     bytea      *esc = PG_GETARG_BYTEA_PP(1);
     453          12 :     bytea      *result = SB_do_like_escape((text *) pat, (text *) esc);
     454             : 
     455          12 :     PG_RETURN_BYTEA_P((bytea *) result);
     456             : }

Generated by: LCOV version 1.14