Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pRsTD-0004PA-O2 for pgsql-general@arkaria.postgresql.org; Tue, 14 Feb 2023 10:23:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pRsTC-0005xK-Ad for pgsql-general@arkaria.postgresql.org; Tue, 14 Feb 2023 10:23:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pRsTB-0005xB-QA for pgsql-general@lists.postgresql.org; Tue, 14 Feb 2023 10:23:29 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pRsT4-00065e-N3 for pgsql-general@postgresql.org; Tue, 14 Feb 2023 10:23:29 +0000 Received: by mail-ej1-x62d.google.com with SMTP id rp23so38806735ejb.7 for ; Tue, 14 Feb 2023 02:23:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=5XVp2IFyvigkFxM0hWh/8Tjl4Ohu2ytI7nT/sxZY2OA=; b=aMGqH8MGDhtaNVDmTQ8bgUIIWx0VCzT4nUZI5sYnwu604e+2L9jgsH8vLKhOLEH9ye jQb43f8tSGW0uTaiQmvw70OFupvX/3OLI+kCAq7ZABKDzWIRexHgjTOv61215elkiSSO 1qnga42PtWoSkHiLLnEUS5BJ1UynXbPS6NbKg7bm2bqEyx2RsmsWZi4eSbA6OngfbJMi N9azITAdOn5U/bou208VVFSuqWy1tOVViWQFy12LP0XRIfUZmX/aFCtAGPP3UikuitGU aaohQIXKK2TNSYHWNyEo9/V2XFvaIMT6fzV+JJ7+0AHLYcHQ4I6X6qAERNMFr7ctFipg r5Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5XVp2IFyvigkFxM0hWh/8Tjl4Ohu2ytI7nT/sxZY2OA=; b=azTHrv9kXSLcOjdED72VSnvrEQO2eEf9P2rADhOwYS8n8px1d6EHJTRsH228oScgji j0EUNnJvY79s8lWctFwLJMdjRsL8fKRJ/XsL01VB0YFWtR6v8yop4WsfzzUzdiCnJ2qV aA0eMg0Nd6lgVmPmURISgoxX0c+DpB5KoWFzB5e0D8mzb8vZjTTYLq+X/t4lM9JliGJr RFJAVWrMiC0PN3Y9dUtG7s2f9mBIlR4H62X7htLfUOswjuE7BISJHWRwpEFRitM0Jdkv AYHjOsjU/mhv7Y5In2tnfBqk6pSXtDxIyIQNhsBneU8pEUsAY82EMOzdBi5+GAXbybln BzKw== X-Gm-Message-State: AO0yUKUopUkO1vM+Rcu0trerm6ZIn5pW6fnqx+rtpPjfNm8xVn/kYgvn uNQzF3UKTdY81Vd+AUXur2/BwA== X-Google-Smtp-Source: AK7set8ixUqSkeAnOmoFWLXJQH4D5uGTvGuy3gwA64X89wOR5ZLKBszwvcGctbG9lqVR91jg4iZ9WA== X-Received: by 2002:a17:907:76b7:b0:88f:a818:8ff2 with SMTP id jw23-20020a17090776b700b0088fa8188ff2mr2388476ejc.11.1676370200501; Tue, 14 Feb 2023 02:23:20 -0800 (PST) Received: from dynamic-pd01.res.v6.highway.a1.net ([2001:871:5e:539:1b2c:c52a:475d:310c]) by smtp.gmail.com with ESMTPSA id r27-20020a170906351b00b008b132c5ea47sm383762eja.153.2023.02.14.02.23.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 02:23:20 -0800 (PST) Message-ID: <5f395b5b2e1f135f487acb96882f89d012fead13.camel@cybertec.at> Subject: Re: Losing my latin on Ordering... From: Laurenz Albe To: Dominique Devienne , pgsql-general@postgresql.org Date: Tue, 14 Feb 2023 11:23:19 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.3 (3.46.3-1.fc37) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2023-02-14 at 10:31 +0100, Dominique Devienne wrote: > Hi. Porting a unit test to PostgreSQL, we got a failure related to orderi= ng. >=20 > We've distilled it to the below. The DB is en_US.UTF-8, and the sorting w= e get > does not make sense to me. The same prefix can be sorted differently base= d on > the suffix apprently, which doesn't make any sense to me. >=20 > Surely sorting should be "constant left-to-right", no? What are we missin= g? No, it isn't. That's not how natural language collations work. They typically use different levels of comparison: first, strings are sorte= d according to base character, ignoring accents, case and punctuation. Wherever that comparison is equal, the next level is used (typically accent= s), then the next (case), and so on. > I'm already surprised (star) comes before (space), when the latter "comes > before" the former in both ASCII and UTF-8, but that the two "Foo*" and "= Foo " > prefixed pairs are not clustered after sorting is just mistifying to me. = So how come? Because they compare identical on the first three levels. Any difference i= n letters, accents or case weighs stronger, even if it occurs to the right of these substrings. > For now we can work-around this by explicitly adding the `collate "C"` on > the queries underlying that particular test, but that would be wrong in t= he > general case of international strings to sort, so I'd really like to unde= rstand > what's going on. Yes, it soulds like the "C" collation may be best for you. That is, if you= don't mind that "Z" < "a". Yours, Laurenz Albe --=20 Cybertec | https://www.cybertec-postgresql.com