Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwvST-0002MG-UG for pgsql-bugs@arkaria.postgresql.org; Sun, 11 Aug 2019 21:32:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hwvSS-0000fr-NY for pgsql-bugs@arkaria.postgresql.org; Sun, 11 Aug 2019 21:32:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwvSS-0000fk-Ft for pgsql-bugs@lists.postgresql.org; Sun, 11 Aug 2019 21:32:56 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwvSP-0001mu-Rt for pgsql-bugs@lists.postgresql.org; Sun, 11 Aug 2019 21:32:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x7BLWnbJ008500; Sun, 11 Aug 2019 17:32:50 -0400 From: Tom Lane To: Arthur Zakirov cc: Daniel Verite , James Inform , Peter Eisentraut , pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15892: URGENT: Using an ICU collation in a primary key column breaks ILIKE query In-reply-to: <94c64652-81f6-63b9-b86c-bfc23d5d41d9@postgrespro.ru> References: <8be770db-3fd9-428a-968e-333fc67a50db@manitou-mail.org> <94c64652-81f6-63b9-b86c-bfc23d5d41d9@postgrespro.ru> Comments: In-reply-to Arthur Zakirov message dated "Thu, 04 Jul 2019 20:57:19 +0300" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <8432.1565559090.0@sss.pgh.pa.us> Date: Sun, 11 Aug 2019 17:32:49 -0400 Message-ID: <8498.1565559169@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <8432.1565559090.1@sss.pgh.pa.us> Arthur Zakirov writes: > On 04.07.2019 19:50, Daniel Verite wrote: >> Yes. It is because of the index that the code checks if the ILIKE >> can be evaluated with an index lookup. Otherwise it doesn't. >> If you feel like recompiling with a temporary fix against v11.4 to do >> your own tests, please try the attached patch. > Also this patch works. I thinks we need to use some ICU function to > check whether a char is a letter. This topic seems to have slipped through the cracks, which is too bad because we surely could have gotten it fixed in time for last week's releases. Oh well. I think Daniel's patch leaves more on the table than it needs to, and I don't like Arthur's patch because it's assuming more than it should about whether it's okay to pass a "char" to u_isalpha(). I propose that we do things as attached, instead. (I also added a regression test case.) regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-selfuncs-ilike-v3.patch"; charset="us-ascii" Content-ID: <8432.1565559090.2@sss.pgh.pa.us> Content-Description: fix-selfuncs-ilike-v3.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/= like_support.c index 26e0634..c8fec78 100644 --- a/src/backend/utils/adt/like_support.c +++ b/src/backend/utils/adt/like_support.c @@ -1437,8 +1437,9 @@ regex_selectivity(const char *patt, int pattlen, boo= l case_insensitive, * Check whether char is a letter (and, hence, subject to case-folding) * * In multibyte character sets or with ICU, we can't use isalpha, and it = does - * not seem worth trying to convert to wchar_t to use iswalpha. Instead,= just - * assume any multibyte char is potentially case-varying. + * not seem worth trying to convert to wchar_t to use iswalpha or u_isalp= ha. + * Instead, just assume any non-ASCII char is potentially case-varying, a= nd + * hard-wire knowledge of which ASCII chars are letters. */ static int pattern_char_isalpha(char c, bool is_multibyte, @@ -1449,7 +1450,8 @@ pattern_char_isalpha(char c, bool is_multibyte, else if (is_multibyte && IS_HIGHBIT_SET(c)) return true; else if (locale && locale->provider =3D=3D COLLPROVIDER_ICU) - return IS_HIGHBIT_SET(c) ? true : false; + return IS_HIGHBIT_SET(c) || + (c >=3D 'A' && c <=3D 'Z') || (c >=3D 'a' && c <=3D 'z'); #ifdef HAVE_LOCALE_T else if (locale && locale->provider =3D=3D COLLPROVIDER_LIBC) return isalpha_l((unsigned char) c, locale->info.lt); diff --git a/src/test/regress/expected/collate.icu.utf8.out b/src/test/reg= ress/expected/collate.icu.utf8.out index 51262e0..50d1e6e 100644 --- a/src/test/regress/expected/collate.icu.utf8.out +++ b/src/test/regress/expected/collate.icu.utf8.out @@ -983,6 +983,38 @@ SELECT relname, pg_get_indexdef(oid) FROM pg_class WH= ERE relname LIKE 'collate_t collate_test1_idx4 | CREATE INDEX collate_test1_idx4 ON collate_tests.co= llate_test1 USING btree (((b || 'foo'::text)) COLLATE "POSIX") (4 rows) = +set enable_seqscan =3D off; +explain (costs off) +select * from collate_test1 where b ilike 'abc'; + QUERY PLAN = +------------------------------- + Seq Scan on collate_test1 + Filter: (b ~~* 'abc'::text) +(2 rows) + +select * from collate_test1 where b ilike 'abc'; + a | b = +---+----- + 1 | abc + 4 | ABC +(2 rows) + +explain (costs off) +select * from collate_test1 where b ilike 'ABC'; + QUERY PLAN = +------------------------------- + Seq Scan on collate_test1 + Filter: (b ~~* 'ABC'::text) +(2 rows) + +select * from collate_test1 where b ilike 'ABC'; + a | b = +---+----- + 1 | abc + 4 | ABC +(2 rows) + +reset enable_seqscan; -- schema manipulation commands CREATE ROLE regress_test_role; CREATE SCHEMA test_schema; diff --git a/src/test/regress/sql/collate.icu.utf8.sql b/src/test/regress/= sql/collate.icu.utf8.sql index 46999fb..5ed6360 100644 --- a/src/test/regress/sql/collate.icu.utf8.sql +++ b/src/test/regress/sql/collate.icu.utf8.sql @@ -341,6 +341,15 @@ CREATE INDEX collate_test1_idx6 ON collate_test1 ((a = COLLATE "C")); -- fail = SELECT relname, pg_get_indexdef(oid) FROM pg_class WHERE relname LIKE 'co= llate_test%_idx%' ORDER BY 1; = +set enable_seqscan =3D off; +explain (costs off) +select * from collate_test1 where b ilike 'abc'; +select * from collate_test1 where b ilike 'abc'; +explain (costs off) +select * from collate_test1 where b ilike 'ABC'; +select * from collate_test1 where b ilike 'ABC'; +reset enable_seqscan; + = -- schema manipulation commands = ------- =_aaaaaaaaaa0--