Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eagF1-0004su-VT for pgsql-general@arkaria.postgresql.org; Sun, 14 Jan 2018 11:14:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eagF0-0003Mr-7y for pgsql-general@arkaria.postgresql.org; Sun, 14 Jan 2018 11:14:18 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eagEz-0003Mh-TS for pgsql-general@lists.postgresql.org; Sun, 14 Jan 2018 11:14:18 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eagEv-0004RM-Q0 for pgsql-general@lists.postgresql.org; Sun, 14 Jan 2018 11:14:16 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id EC9CB157C; Sun, 14 Jan 2018 12:14:07 +0100 (CET) Date: Sun, 14 Jan 2018 12:14:07 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: String comparison problem in select - too many results Message-ID: <20180114111407.dxtp2d3ip45oc2ju@hjp.at> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="vyc6bdkgcgilwy3k" Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --vyc6bdkgcgilwy3k Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2018-01-12 11:08:39 +0100, Francisco Olarte wrote: > C collation is like sorting raw bytes, it doesn't event sort > upper/lower case correctly Now you are falling into the same trap as Durumdara, calling an unintended sort order "not correct" ;-). C collation is certainly not what a "normal user" expects. It is therefore wrong for many applications (e.g., you couldn't use it to sort a telephone book), but it might be correct for others (e.g., it you need a stable, unambiguous sort order but don't care much about the order itself). (By coincidence, I also stumbled over the fact that the en_US.UTF-8 collation ignores punctuation characters and spaces at least in the first pass - took me a minute or so to figure out why I got an "inconsistent" (read: unexpected and not obvious for me) sort order.) Lexicographers are interested in sorting single words. Therefore they aren't interested in punctuation. They may also not be very interested in accents, because most languages have few words which differ only by an accent (and it the dictionary is printed on paper, the user will easily be able to scan up and down a few centimeters and find the right entry without knowing whether "=C3=A0" is sorted before or after "=C3=A1"). Somebody preparing an address list should care about punctuation: You would probably not expect to find "Smith-Jones, Amanda" between "Smith, John" and "Smith, Julia". And you probably want to sort "23 Main Street" before "180 Main Street". Which brings us back to Durumdara's example: I don't know his application, so I don't know what "normal users" of his application would expect, but to me those values look like two numbers separated by a slash. So I would expect '23/4567' to be sorted between '18/0212' and '180/2010', but of course the C collation doesn't do that: =3D> select * from foo order by t collate "C"; =E2=95=94=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=97 =E2=95=91 t =E2=95=91 =E2=95=9F=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=95=A2 =E2=95=91 18/0113 =E2=95=91 =E2=95=91 18/0212 =E2=95=91 =E2=95=91 180/2010 =E2=95=91 =E2=95=91 23/4567 =E2=95=91 =E2=95=9A=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=9D (4 rows) It might be possible to define a custom collation for that, but in a case like this I would either split this field into two integer fields or use a function-based index. hp --=20 _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson --vyc6bdkgcgilwy3k Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAlpbO3oACgkQ8g5IURL+ KF3KGBAAjWQOnQNSrkC5BUagIr23L26kCp2Zdi37TUSkgMx24rxC7KafrcyTZNul PMtbLgxlUpEHzkxPfdGS9nQ6F62uCuZIF+gGIQT4Q5prT0MFAabgur9Qnjnoc7ej nrt25EYao0IliVnGdZBQqbZD1Onx8NXjOdkcc2iQ1RxDClm9IJ4Pwg3L3Oav/DO4 ba3XO15vVRTkeNg679HU7zlGuewoYL7/i+H3wDmp5MmKvXdvrySzkr+GLM4x1C2N b6LNIxx5vlzlRKp5jBvgjJWma9wiQ+dAyEoGuhCSniwxusNLxMSces+zrcN33AhV f2j5q99GRfXT43sG651VRrRDuBH/2RxPTJMiTakOdRS6w2ja57H+hvTPNkuEf08c HsO29O2SLqBUqbw36XacD0pXyi/OtUDVTSwwI8o9CwuM3oWFeJ1kPfo6v6HxSeao XsGk+BnV9CPfuPt5Rmu+CGe6Lm1QvnlxMxZD3tnPDNulU1qJ42DBaBPoLc1ifjg6 ka2E2ohKrj8noHYOdGX3JjedcRcU0zDa1kpbOsJze6O0f1b/Yls2SFiEw9N2W7Ff guOEErzDF/j8R4CLuo/PNn1knZ7pf8jdjMeTFzYtj8KNwUP4ih3RxNKOvy7qTSKc +EpY/Dk+j9la2LXOdhOX/hpCH9Ey7vSRk7XZFb8vnmFz2y0f0Rg= =IDgy -----END PGP SIGNATURE----- --vyc6bdkgcgilwy3k--