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 1hj2kK-0008Hb-46 for pgsql-bugs@arkaria.postgresql.org; Thu, 04 Jul 2019 14:30:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hj2kI-0004HX-Ut for pgsql-bugs@arkaria.postgresql.org; Thu, 04 Jul 2019 14:29:58 +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 1hj1zk-0003pr-QV for pgsql-bugs@lists.postgresql.org; Thu, 04 Jul 2019 13:41:52 +0000 Received: from mout.kundenserver.de ([212.227.17.10]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hj1zh-0006Wu-NG for pgsql-bugs@lists.postgresql.org; Thu, 04 Jul 2019 13:41:52 +0000 Received: from oxbsltgw04.schlund.de ([172.19.249.21]) by mrelayeu.kundenserver.de (mreue107 [213.165.67.113]) with ESMTPSA (Nemesis) id 1MAP77-1hpOio2txL-00Bs7S for ; Thu, 04 Jul 2019 15:41:45 +0200 Date: Thu, 4 Jul 2019 15:41:45 +0200 (CEST) From: James Inform Reply-To: James Inform To: pgsql-bugs@lists.postgresql.org Message-ID: <1797229370.392447.1562247705371@email.ionos.de> In-Reply-To: References: <15892-e5d2bea3e8a04a1b@postgresql.org> Subject: Re: BUG #15892: URGENT: Using an ICU collation in a primary key column breaks ILIKE query MIME-Version: 1.0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Priority: 3 Importance: Medium X-Mailer: Open-Xchange Mailer v7.8.4-Rev58 X-Originating-Client: open-xchange-appsuite X-Provags-ID: V03:K1:rw03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mAIgyHaGHwGlvtwxgCMJFw5z 03N5WDv List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk

The weird thing is, that this behavior only occures when us= ed on a field field a primary key:

If you do: 

create data= base testdb;

\c testdb;

-- Just create a simple table with one = column
create table icutest(data text not null collate "de-x-icu"= ; primary key, data2 text collate "de-x-icu");

-- Insert a re= cord with uppercase string
insert into icutest values ('MYTEST',= 'MYTEST');

-- This is not giving a match
select * from icu= test where data ilike 'mytest';

-- BUT THIS GIVES A MATCH:

select * from icutest where data2 ilike 'mytest';

-- So i= t seems to be especially related to the scenario where a primary key / inde= x exists.

On July 4, 2019 at 12:36 PM Daniel V= erite <daniel@manitou-mail.o= rg> wrote:


PG Bug reporting form wrote:
-- Just create a simple table with one column
create table ic= utest(data text not null collate "de-x-icu" primary key);

-= - Insert a record with uppercase string
insert into icutest values (= 9;MYTEST');

-- This is not giving a match
select * from icu= test where data ilike 'mytest';

This also happens o= n v10 and on the master branch.

The bug seems to come from a mistake= in like_support.c:


/* * Check whether char is a letter (and, h= ence, subject to case-folding)
* * In multibyte character sets or with= ICU, we can't use isalpha, and it does * not seem worth trying to conv= ert to wchar_t to use iswalpha. Instead,
just * assume any multibyte cha= r is potentially case-varying.
*/
static int
pattern_char_isalph= a(char c, bool is_multibyte,
pg_locale_t locale, bool locale_is_c)
{=
if (locale_is_c)
return (c >=3D 'A' && c <= =3D 'Z') || (c >=3D 'a' && c <=3D 'z= 9;);
else if (is_multibyte && IS_HIGHBIT_SET(c))
return tru= e;
else if (locale && locale->provider =3D=3D COLLPROVIDER_= ICU)
return IS_HIGHBIT_SET(c) ? true : false;


With an ICU lo= cale, this returns false for all characters in 'mytest'.

I t= hink this eventually leads the caller to incorrectly believe that it
can= optimize the test into an exact match (data=3D'mytest'), given
= there are otherwise no wildcards in the pattern.

On fixing the bug, = if we make this function returns true for all
characters under an ICU lo= cale, it appears to work, but we're loosing an
opportunity to optimi= ze for some patterns.
If OTOH we wanted to use an ICU call like u_isalph= a(), to be closer
to what's done with libc, we'd need to pass a = UChar32 argument,
not a char, and since we're in a char-oriented con= text, I don't see how
to do that.


Best regards,
-- Daniel Vérité
PostgreSQL-powered mailer: http://www.manitou-m= ail.org
Twitter: @DanielVerite
=20