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

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: James Moore <james(dot)moore(at)g5labs(dot)ai>, 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-12-01 04:56:28
Message-ID: aw5TfH5MM8eTsAR9@paquier.xyz
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tue, Dec 01, 2026 at 12:55:04AM +0100, Laurenz Albe wrote:
> The never-ending story...

That sounds like the plot of a movie, a rather old one.

> memcpy(r, p, mblen);
> r += mblen;
> afterescape = false;
> +
> + /*
> + * If we encounter an escaped character in a character class,
> + * we are no longer at the beginning.
> + */
> + charclass_pos = 3;
> }
> else if (e && elen == mblen && memcmp(e, p, mblen) == 0)
> {
> @@ -907,6 +913,13 @@ similar_escape_internal(text *pat_text, text *esc_text)
> */
> memcpy(r, p, mblen);
> r += mblen;
> +
> + /*
> + * If we encounter a multi-byte (non-escape) character in a
> + * character class, we are no longer at the beginning.
> + */
> + if (bracket_depth > 0)
> + charclass_pos = 3;

Hmm. The bracket_depth check is not actually necessary if I am
following correctly, but I can see that you have added it to match
with the fast path where we have a (bracket_depth > 0), and the
comment around the top of similar_escape_internal() mentions the
relationship between the two. Why not.

Saying that, these two additions look OK here.

> +-- Multi-byte escape char and multi-byte character at the beginning of
> +-- a character class
> +EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO U&'[\00E9].' ESCAPE U&'\00FC';
> + QUERY PLAN
> +--------------------------------------
> + Seq Scan on text_tbl
> + Filter: (f1 ~ '^(?:[é]\.)$'::text)
> +(2 rows)

This won't work without some filtering to only run the test in an
UTF-8 database. encodings.sql would be one better location, because
we make its run conditional. The output of your patch is actually
plain wrong.

There is nothing unsurmontable. I'll go handle things. Thanks for
the report and the patch.
--
Michael

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Ishjaap Singh 2026-12-01 05:19:57 Re: BUG #19957: jsonpath `last` returns the wrong index after a filter containing `exists($x[0])`
Previous Message Raja Sai pranav 2026-12-01 04:50:50 Re: BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance