Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dq3NT-0000gJ-EM for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Sep 2017 20:26:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dq3NT-00063q-13 for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Sep 2017 20:26:19 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dq17T-0001Qn-Hv for pgsql-bugs@postgresql.org; Thu, 07 Sep 2017 18:01:39 +0000 Received: from mxo1.nje.dmz.twosigma.com ([208.77.214.160]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dq17K-0004Nw-Vu for pgsql-bugs@postgresql.org; Thu, 07 Sep 2017 18:01:38 +0000 Received: from localhost (localhost [127.0.0.1]) by mxo1.nje.dmz.twosigma.com (Postfix) with ESMTP id 6F6FA100052 for ; Thu, 7 Sep 2017 18:01:27 +0000 (GMT) X-Virus-Scanned: Debian amavisd-new at twosigma.com Received: from mxo1.nje.dmz.twosigma.com ([127.0.0.1]) by localhost (mxo1.nje.dmz.twosigma.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id TxQ4UsfNtNUm for ; Thu, 7 Sep 2017 18:01:27 +0000 (GMT) Received: from exmbdft8.ad.twosigma.com (exmbdft8.ad.twosigma.com [172.22.2.84]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mxo1.nje.dmz.twosigma.com (Postfix) with ESMTPS id 57F808002E for ; Thu, 7 Sep 2017 18:01:27 +0000 (GMT) Received: from exmbdft5.ad.twosigma.com (172.22.1.56) by exmbdft8.ad.twosigma.com (172.22.2.84) with Microsoft SMTP Server (TLS) id 15.0.1263.5; Thu, 7 Sep 2017 18:01:26 +0000 Received: from exmbdft5.ad.twosigma.com ([fe80::6817:c8d6:1df1:1f91]) by exmbdft5.ad.twosigma.com ([fe80::6817:c8d6:1df1:1f91%17]) with mapi id 15.00.1263.000; Thu, 7 Sep 2017 18:01:26 +0000 From: Rusty Conover To: "'pgsql-bugs@postgresql.org'" Subject: ICU collation support is incomplete for replacing citext in 10.beta4 Thread-Topic: ICU collation support is incomplete for replacing citext in 10.beta4 Thread-Index: AdMoAtF/cuW8Btv/RnezxWuvCYRIrQ== Date: Thu, 7 Sep 2017 18:01:26 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [172.20.60.15] Content-Type: multipart/alternative; boundary="_000_db385073b2f6486ba663391c38fefbcfexmbdft5adtwosigmacom_" MIME-Version: 1.0 List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --_000_db385073b2f6486ba663391c38fefbcfexmbdft5adtwosigmacom_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable The support for ICU provided collations in PostgreSQL 10.beta4 adds the abi= lity to have case insensitive collations. Which is a great thing, because c= itext can be very slow! Collations in ICU allow a number of parameters to be specified that control= their behavior, one of those parameters is called strength. Strength is explained here: http://userguide.icu-project.org/collation/concepts#TOC-Comparison-Levels To create a collation that uses a strength level of primary (one that only = compares base characters) use this: create collation test_ncs (locale =3D 'und@colStrength=3Dprimary', provider= =3D 'icu'); Sadly, the collation is successfully created but PostgreSQL doesn't respect= the result of ICU's collation functionality. This can be demonstrated by this short SQL: -- test_ncs is non case sensitive create table test (v text collate "test_ncs"); insert into test (v) values ('abc'); insert into test (v) values ('aBc'); -- Should return two rows select * from test where v =3D 'ABC'; -- doesn't return any rows. v --- (0 rows) Reviewing the implementation of the text, varchar, bpchar and char data typ= es I found some problems that are likely the cause. ## String data type implementation notes texteq(), textne() - These functions call memcmp() if the lengths of two te= xt Datums don't differ. Strings could still be considered to be equal by t= he collation even if in memory their representations differ. Calls to memcmp(): https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/varl= ena.c#L1666 https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/varl= ena.c#L1695 These functions should both be changed to call text_cmp with the current co= llation from the function invocation information. varstr_cmp(), varstrfastcmp_locale() - Both of these functions compare the = strings using strcmp() if the collation returns a value of zero. This prev= ents the output of the collation function from being respected with regards= to equality. More plainly, strcmp() is always case sensitive, the collati= on we previously defined above is not. Call to strcmp(): https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/varl= ena.c#L1601 This opens a line of inquiry, should ICU's collation function and other col= lation providers be used for string equality? It seems we should respect t= he users intent when the collation was specified for the expression or the = column. I've prepared some diffs to resolve the issues around the code not trusting= the collation's equality for the varchar, text and char data types. I will post them if requested. ## Hashing Strings Using Collations An ICU collation may say that two strings are equal when strcmp() or memcmp= () says they are not equal. The hash values for strings should incorporate = the use of the collation as well. Because if a collation considers a pair = of strings to be equal they should hash to the same value. If this is not the case strings that are equal may be in different h= ash buckets so that when traversing the contents of a hash table the equivalent string values may not be enumerated together. ICU offers an API where strings can be converted to what ICU calls Sort Key= s. Sort Key representations are then able to be passed to the hash_any() fu= nction to produce hash values that respect the collation's beliefs. ICU's description of Sort Keys: http://userguide.icu-project.org/collation/architecture#TOC-Sort-Keys By requiring a collation to be used when hashing a value, it does complicat= e the query's execution. For an example look at these statements: -- create table test1 (v text collate "C") select v from test1 except select v from test1; -- In execTuplesMatch() the equality function is called using FunctionCall2(). https://github.com/postgres/postgres/blob/master/src/backend/executor/execG= rouping.c#L115 FunctionCall2() passes InvalidOid for the collation Oid to use to perform t= he equality operation. https://github.com/postgres/postgres/blob/master/src/include/fmgr.h#L605 With my changes to the implementation of the string data types, the code ra= ises an error that it attempting to compare two strings without a valid col= lation Oid. There seems to be a need to extend the various functionality and structures= for comparing tuples, hash tables for aggregates and the SetOp nodes. Am I incorrect? Would it be reasonable to assume that a collation would be able to be speci= fied for a GROUP BY like it can be specified for an ORDER BY? It does appear that by allowing collations to be specified as an expression th= is is possible: select v collate "C" from test1 group by (v collate "C") order by (v collat= e "C") collate "POSIX"; For instance, you could group without respect to case when the original col= lation of the column is case sensitive. Before continuing efforts into resolving these collation challenges, I'd li= ke to know if this is considered a bug, or simply out of scope of PostgreSQ= L 10? There are additional difficulties using collations when it comes to = the functionality of string functions such as LIKE, position() and regular = expressions. Thank you, Rusty --_000_db385073b2f6486ba663391c38fefbcfexmbdft5adtwosigmacom_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

The support for ICU provided collations in PostgreSQ= L 10.beta4 adds the ability to have case insensitive collations. Which is a= great thing, because citext can be very slow!

 

Collations in ICU allow a number of parameters to be= specified that control their behavior, one of those parameters is called s= trength.

 

Strength is explained here:

 

http://userguide.icu-project.org/collation/concepts#= TOC-Comparison-Levels

 

To create a collation that uses a strength level of = primary (one that only compares base characters) use this:

 

create collation test_ncs (locale =3D 'und@colStreng= th=3Dprimary', provider =3D 'icu');

 

Sadly, the collation is successfully created but Pos= tgreSQL doesn't respect the result of ICU's collation functionality.

 

This can be demonstrated by this short SQL:

 

-- test_ncs is non case sensitive

create table test (v text collate "test_ncs&quo= t;);

insert into test (v) values ('abc');

insert into test (v) values ('aBc');

 

-- Should return two rows

select * from test where v =3D 'ABC';

 

-- doesn't return any rows.

v

---

(0 rows)

 

Reviewing the implementation of the text, varchar, b= pchar and char data types I found some problems that are likely the cause.<= o:p>

 

## String data type implementation notes<= /p>

 

texteq(), textne() - These functions call memcmp() i= f the lengths of two text Datums don't differ.  Strings could still be= considered to be equal by the collation even if in memory their representa= tions differ.

 

Calls to memcmp():

 

https://github.com/postgres/postgres/blob/master/src= /backend/utils/adt/varlena.c#L1666

https://github.com/postgres/postgres/blob/master/src= /backend/utils/adt/varlena.c#L1695

 

These functions should both be changed to call text_= cmp with the current collation from the function invocation information.

 

varstr_cmp(), varstrfastcmp_locale() - Both of these= functions compare the strings using strcmp() if the collation returns a va= lue of zero.  This prevents the output of the collation function from = being respected with regards to equality.  More plainly, strcmp() is always case sensitive, the collation we previous= ly defined above is not.

 

Call to strcmp():

 

https://github.com/postgres/postgres/blob/master/src= /backend/utils/adt/varlena.c#L1601

 

This opens a line of inquiry, should ICU's collation= function and other collation providers be used for string equality?  = It seems we should respect the users intent when the collation was specifie= d for the expression or the column.

 

I've prepared some diffs to resolve the issues aroun= d the code not trusting the collation's equality for the varchar, text and = char data

types.  I will post them if requested.

 

## Hashing Strings Using Collations

 

An ICU collation may say that two strings are equal = when strcmp() or memcmp() says they are not equal. The hash values for stri= ngs should incorporate the use of the collation as well.  Because if a= collation considers a pair of strings to be equal they should hash to the same

value. If this is not the case strings that are equa= l may be in different hash buckets so that when traversing the contents of = a hash

table the equivalent string values may not be enumer= ated together.

 

ICU offers an API where strings can be converted to = what ICU calls Sort Keys. Sort Key representations are then able to be pass= ed to the hash_any() function to produce hash values that respect the colla= tion's beliefs.

 

ICU's description of Sort Keys:

 

http://userguide.icu-project.org/collation/architect= ure#TOC-Sort-Keys

 

By requiring a collation to be used when hashing a v= alue, it does complicate the query's execution.  For an example look a= t these statements:

 

--

create table test1 (v text collate "C")

select v from test1 except select v from test1;=

--

 

In execTuplesMatch() the equality function is called= using FunctionCall2().

 

https://github.com/postgres/postgres/blob/master/src= /backend/executor/execGrouping.c#L115

 

FunctionCall2() passes InvalidOid for the collation = Oid to use to perform the equality operation.

 

https://github.com/postgres/postgres/blob/master/src= /include/fmgr.h#L605

 

With my changes to the implementation of the string = data types, the code raises an error that it attempting to compare two stri= ngs without a valid collation Oid.

 

There seems to be a need to extend the various funct= ionality and structures for comparing tuples, hash tables for aggregates an= d the

SetOp nodes. Am I incorrect?

 

Would it be reasonable to assume that a collation wo= uld be able to be specified for a GROUP BY like it can be specified for an = ORDER BY? It

does appear that by allowing collations to be specif= ied as an expression this is possible:

 

select v collate "C" from test1 group by (= v collate "C") order by (v collate "C") collate "P= OSIX";

 

For instance, you could group without respect to cas= e when the original collation of the column is case sensitive.

 

Before continuing efforts into resolving these colla= tion challenges, I'd like to know if this is considered a bug, or simply ou= t of scope of PostgreSQL 10?  There are additional difficulties using = collations when it comes to the functionality of string functions such as LIKE, position() and regular expressions.=

 

Thank you,

 

Rusty

--_000_db385073b2f6486ba663391c38fefbcfexmbdft5adtwosigmacom_--