Version 17.6 changed how similar works compared to version 17.5

Поиск
Список
Период
Сортировка
Искать

Version 17.6 changed how similar works compared to version 17.5

От:
Stephan Springl <springl-psql@bfw-online.de>
Дата:
Hello,

version 17.6 changed how similar works compared to version 17.5.

With file f as
cat >f < 0)
  		{
-			if (pchar == '"' && charclass_depth < 1)	/* escape-double-quote? */
+			if (afterescape)
+			{
+				*r++ = '\\';
+				afterescape = false;
+			}
+			*r++ = pchar;
+
+			/*
+			 * Ignore a closing bracket at the start of a character class.
+			 * Such a bracket is taken literally rather than closing the
+			 * class.  "charclass_start" is 1 right at the beginning of a
+			 * class and 2 after an initial caret.
+			 */
+			if (pchar == ']' && charclass_start > 2)
+				charclass_depth--;
+			else if (pchar == '[')
+				charclass_depth++;
+
+			/*
+			 * If there is a caret right after the opening bracket, it negates
+			 * the character class, but a following closing bracket should
+			 * still be treated as a normal character.  That holds only for
+			 * the first caret, so only the values 1 and 2 mean that closing
+			 * brackets should be taken literally.
+			 */
+			if (pchar == '^')
+				charclass_start++;
+			else
+				charclass_start = 3;	/* definitely past the start */
+		}
+		else if (afterescape)
+		{
+			if (pchar == '"')	/* escape-double-quote? */
  			{
  				/* emit appropriate part separator, per notes above */
  				if (nquotes == 0)
@@ -956,35 +988,6 @@ similar_escape_internal(text *pat_text, text *esc_text)
  			/* SQL escape character; do not send to output */
  			afterescape = true;
  		}
-		else if (charclass_depth > 0)
-		{
-			if (pchar == '\\')
-				*r++ = '\\';
-			*r++ = pchar;
-
-			/*
-			 * Ignore a closing bracket at the start of a character class.
-			 * Such a bracket is taken literally rather than closing the
-			 * class.  "charclass_start" is 1 right at the beginning of a
-			 * class and 2 after an initial caret.
-			 */
-			if (pchar == ']' && charclass_start > 2)
-				charclass_depth--;
-			else if (pchar == '[')
-				charclass_depth++;
-
-			/*
-			 * If there is a caret right after the opening bracket, it negates
-			 * the character class, but a following closing bracket should
-			 * still be treated as a normal character.  That holds only for
-			 * the first caret, so only the values 1 and 2 mean that closing
-			 * brackets should be taken literally.
-			 */
-			if (pchar == '^')
-				charclass_start++;
-			else
-				charclass_start = 3;	/* definitely past the start */
-		}
  		else if (pchar == '[')
  		{
  			/* start of a character class */



Re: Version 17.6 changed how similar works compared to version 17.5

От:
Laurenz Albe <laurenz.albe@cybertec.at>
Дата:
On Fri, 2025-09-12 at 15:45 +0200, Stephan Springl wrote:
> version 17.6 changed how similar works compared to version 17.5.
> 
> With file f as
> cat >f < drop table t;
> create table t (p varchar (1));
> insert into t values ('_');
> select * from t;
> select * from t where p similar to '[\_]%';
> END
> 
> psql -f f
> 
> gives:
> 
> DROP TABLE
> CREATE TABLE
> INSERT 0 1
>   p 
> ---
>   _
> (1 row)
> 
>   p 
> ---
> (0 rows)
> 
> The expression with similar does not find the row. With version 17.5, the row
> was found, as wanted.
> 
> Reverting commit e3ffc3e91d04579240fb54a96f9059b246488dce
> "Fix conversion of SIMILAR TO regexes for character classes"
> brings back the previous behavior.  The patch does not take account of the
> first character in a character class being escaped.  In this case it skips
> the closing ']' of the caracter class.  "[_]%" as similar expression gets
> translated to "^(?:[\_]%)$" as a regular expression.  Version 17.5
> generates "^(?:[\_].*)$" as regular expression.

You are right, that is a bug.  It was reported independently in [1].

Your analysis is correct, but I don't think that your fix is the right one.
Could you try my patch in [2] and see if that works for you?

Yours,
Laurenz Albe


 [1]: https://postgr.es/m/flat/CAFCRh-8NwJd0jq6P%3DR3qhHyqU7hw0BTor3W0SvUcii24et%2BzAw%40mail.gmail.com
 [2]: https://postgr.es/m/2c58dcdba889f5b28df5ad9d21b5ea2d0ac63a9a.camel%40cybertec.at


Re: Version 17.6 changed how similar works compared to version 17.5

От:
Laurenz Albe <laurenz.albe@cybertec.at>
Дата:
On Fri, 2025-09-12 at 15:45 +0200, Stephan Springl wrote:
> version 17.6 changed how similar works compared to version 17.5.
> 
> With file f as
> cat >f < drop table t;
> create table t (p varchar (1));
> insert into t values ('_');
> select * from t;
> select * from t where p similar to '[\_]%';
> END
> 
> psql -f f
> 
> gives:
> 
> DROP TABLE
> CREATE TABLE
> INSERT 0 1
>   p 
> ---
>   _
> (1 row)
> 
>   p 
> ---
> (0 rows)
> 
> The expression with similar does not find the row. With version 17.5, the row
> was found, as wanted.

This bug was fixed in commit e09adb5b9f [1], and the fix will be shipped with
the next minor releases.

Yours,
Laurenz Albe


 [1]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=e09adb5b9fc9dec27c7ff9e9592723d7d8098b03


FAQ