Amend recent fix for SIMILAR TO regex conversion.
authorTom Lane <[email protected]>
Sat, 13 Sep 2025 20:55:51 +0000 (16:55 -0400)
committerTom Lane <[email protected]>
Sat, 13 Sep 2025 20:55:51 +0000 (16:55 -0400)
commite09adb5b9fc9dec27c7ff9e9592723d7d8098b03
treea058a91f97bc620c92e61d9ddeb232fd99e2b4a2
parenta220e40d11063cf2e43bdfb5e1ca872fe201f37d
Amend recent fix for SIMILAR TO regex conversion.

Commit e3ffc3e91 fixed the translation of character classes in
SIMILAR TO regular expressions.  Unfortunately the fix broke a corner
case: if there is an escape character right after the opening bracket
(for example in "[\q]"), a closing bracket right after the escape
sequence would not be seen as closing the character class.

There were two more oversights: a backslash or a nested opening bracket
right at the beginning of a character class should remove the special
meaning from any following caret or closing bracket.

This bug suggests that this code needs to be more readable, so also
rename the variables "charclass_depth" and "charclass_start" to
something more meaningful, rewrite an "if" cascade to be more
consistent, and improve the commentary.

Reported-by: Dominique Devienne <[email protected]>
Reported-by: Stephan Springl <[email protected]>
Author: Laurenz Albe <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/CAFCRh-8NwJd0jq6P=R3qhHyqU7hw0BTor3W0SvUcii24et+zAw@mail.gmail.com
Backpatch-through: 13
src/backend/utils/adt/regexp.c
src/test/regress/expected/strings.out
src/test/regress/sql/strings.sql