Re: SIMILAR TO expressions translate wildcards where they shouldn't

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: James Moore <james(dot)moore(at)g5labs(dot)ai>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: SIMILAR TO expressions translate wildcards where they shouldn't
Date: 2026-11-30 23:55:04
Message-ID: 2bbde1e52c82d906412bd3e1e58e01758cfb4221.camel@cybertec.at
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Mon, 2026-11-30 at 12:37 -0500, James Moore wrote:
> The bracket handling added for this report (e3ffc3e91 in 17.6, and its
> follow-ups in 17.7, as far as we can tell) covers the single-byte path of
> similar_escape_internal(), but not the path taken when the ESCAPE character
> is multibyte. In that path charclass_pos is not updated, so a multibyte
> character directly after "[" leaves the following "]" treated as a literal
> inside the bracket, and the rest of the pattern is not translated.
>
> UTF8 database, reproduced on 17.11, 18.7 and 19.0:
>
>   SELECT 'éx' SIMILAR TO '[é]%' ESCAPE 'ü';   -- f   (expected t)
>   SELECT 'éx' SIMILAR TO '[é]%';              -- t
>   SELECT 'éx' SIMILAR TO '[é]%' ESCAPE '!';   -- t
>   SELECT similar_to_escape('[é]%', 'ü');      -- ^(?:[é]%)$
>                                               --   expected ^(?:[é].*)$
>
> An ASCII character after "[" is not affected:
>
>   SELECT 'ax' SIMILAR TO '[a]%' ESCAPE 'ü';   -- t
>
> The code in question is the multibyte branch of similar_escape_internal()
> in src/backend/utils/adt/regexp.c (around lines 825-860 on 17.11).
>
> We found this by comparing PostgreSQL's results against a formal model of
> SQL pattern matching.

The never-ending story...

Here is a patch that is hopefully the final fix.

Yours,
Laurenz Albe

Attachment Content-Type Size
v1-0001-Further-fix-SIMILAR-TO-regexes-with-character-cla.patch text/x-patch 3.8 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message shihao zhong 2026-11-30 23:57:39 Re: BUG #19872: JSON_TABLE: an UNUSED PASSING argument's COLLATE leaks into the output columns (wrong results, persi
Previous Message shihao zhong 2026-11-30 23:54:04 Re: BUG #19913: Parent ALTER TABLE during concurrent DETACH leaves pending partition unusable (42804)