pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
 BUG #15805: Problem with lower function for greek sigma (Σ) letter
9+ messages / 5 participants
[nested] [flat]

*  BUG #15805: Problem with lower function for greek sigma (Σ) letter
@ 2019-05-15 07:57  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 2 replies; 9+ messages in thread

From: PG Bug reporting form @ 2019-05-15 07:57 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: iksss.88@gmail.com

The following bug has been logged on the website:

Bug reference:      15805
Logged by:          Sergey kuznetsov
Email address:      iksss.88@gmail.com
PostgreSQL version: 9.6.10
Operating system:   linux
Description:        

I see unexpected behaviour of lower function for greek sigma letter Σ.
According to wikipedia (https://en.wikipedia.org/wiki/Sigma) if this letter
is in final word position, it should be ς in lowercase, and not σ. But
PotgreSQL lower function returns σ, for example lower('ΔΗΜΟΤΕΣ ΦΑΙΣΤΟΥ') =
"δημοτεσ φαιστου" instead of "δημοτες φαιστου". So if I try to pass this
lower string from another part of a system (java code, for example) Postgre
will not return this row cause it differs from java-generated one.

Thanks, 
Sergey Kuznetsov



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #15805: Problem with lower function for greek sigma (Σ) letter
@ 2019-05-15 09:53  Daniel Gustafsson <daniel@yesql.se>
  parent: PG Bug reporting form <noreply@postgresql.org>
  1 sibling, 1 reply; 9+ messages in thread

From: Daniel Gustafsson @ 2019-05-15 09:53 UTC (permalink / raw)
  To: iksss.88@gmail.com, PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

> On 15 May 2019, at 09:57, PG Bug reporting form <noreply@postgresql.org> wrote:
> 
> The following bug has been logged on the website:
> 
> Bug reference:      15805
> Logged by:          Sergey kuznetsov
> Email address:      iksss.88@gmail.com
> PostgreSQL version: 9.6.10
> Operating system:   linux
> Description:        
> 
> I see unexpected behaviour of lower function for greek sigma letter Σ.
> According to wikipedia (https://en.wikipedia.org/wiki/Sigma) if this letter
> is in final word position, it should be ς in lowercase, and not σ. But
> PotgreSQL lower function returns σ, for example lower('ΔΗΜΟΤΕΣ ΦΑΙΣΤΟΥ') =
> "δημοτεσ φαιστου" instead of "δημοτες φαιστου". So if I try to pass this
> lower string from another part of a system (java code, for example) Postgre
> will not return this row cause it differs from java-generated one.

This is indeed a bug, and a rare occurrence since AFAICT from ISO 30112 and
googling there is only a single case of word-final lowercasing which is this
sigma. The attached patch takes a stab at fixing this.

cheers ./daniel

Attachments:

sigma-wordfinal.patch (application/octet-stream, 2.7K) download From 337b2049fae0ac614ae3ca0c5f93f6cfcbf8dbbf Mon Sep 17 00:00:00 2001 From: Daniel Gustafsson <daniel@yesql.se> Date: Wed, 15 May 2019 11:47:56 +0200 Subject: [PATCH] Handle lowercase sigma in word-final position According to ISO/IEC 30112 WD10, when GREEK SMALL LETTER SIGMA is at word-final position it should instead become GREEK SMALL LETTER FINAL SIGMA. Since towlower only sees a single wchar and the context we need to handle word-final positions separately. Reference: http://www.open-std.org/JTC1/SC35/WG5/docs/30112d10.pdf --- src/backend/tsearch/ts_locale.c | 8 ++++++++ src/backend/utils/adt/formatting.c | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/backend/tsearch/ts_locale.c b/src/backend/tsearch/ts_locale.c index 309f7c0459..c49b980843 100644 --- a/src/backend/tsearch/ts_locale.c +++ b/src/backend/tsearch/ts_locale.c @@ -283,6 +283,14 @@ lowerstr_with_len(const char *str, int len) while (*wptr) { *wptr = towlower((wint_t) *wptr); + /* + * According to ISO/IEC 30112 WD10, when GREEK SMALL LETTER SIGMA + * is at word- final position it should instead become GREEK SMALL + * LETTER FINAL SIGMA. Since towlower only sees a single wchar and + * the context we need to handle word-final positions separately. + */ + if (*wptr == 0x03c3 && (!*(wptr + 1) || *(wptr + 1) < 0x0041)) + *wptr = (wint_t) 0x03c2; wptr++; } diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 69a691f18e..4cbac9561b 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -962,6 +962,18 @@ typedef struct NUMProc *L_currency_symbol; } NUMProc; +/* + * According to ISO/IEC 30112 WD10, when GREEK SMALL LETTER SIGMA is at word- + * final position it should instead become GREEK SMALL LETTER FINAL SIGMA. + * Since towlower only sees a single wchar and the context we need to handle + * word-final positions separately. + */ +#define ADJUST_WORDFINAL \ +do { \ + if ((workspace[curr_char] == 0x03c3) && \ + (!workspace[curr_char + 1] || workspace[curr_char + 1] < 0x0041)) \ + workspace[curr_char] = (wchar_t) 0x03c2; \ +} while(0) /* ---------- * Functions @@ -1615,6 +1627,8 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) else #endif workspace[curr_char] = towlower(workspace[curr_char]); + + ADJUST_WORDFINAL; } /* @@ -1869,7 +1883,10 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) #endif { if (wasalnum) + { workspace[curr_char] = towlower(workspace[curr_char]); + ADJUST_WORDFINAL; + } else workspace[curr_char] = towupper(workspace[curr_char]); wasalnum = iswalnum(workspace[curr_char]); -- 2.14.1.145.gb3622a4ee =
^ permalink raw reply [nested|flat] 9+ messages in thread

* Re: BUG #15805: Problem with lower function for greek sigma (Σ) letter
@ 2019-05-15 12:20  Alvaro Herrera <alvherre@2ndquadrant.com>
  parent: Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 2 replies; 9+ messages in thread

From: Alvaro Herrera @ 2019-05-15 12:20 UTC (permalink / raw)
  To: Daniel Gustafsson <daniel@yesql.se>; +Cc: iksss.88@gmail.com, PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

On 2019-May-15, Daniel Gustafsson wrote:

> > I see unexpected behaviour of lower function for greek sigma letter Σ.
> > According to wikipedia (https://en.wikipedia.org/wiki/Sigma) if this letter
> > is in final word position, it should be ς in lowercase, and not σ. But
> > PotgreSQL lower function returns σ, for example lower('ΔΗΜΟΤΕΣ ΦΑΙΣΤΟΥ') =
> > "δημοτεσ φαιστου" instead of "δημοτες φαιστου". So if I try to pass this
> > lower string from another part of a system (java code, for example) Postgre
> > will not return this row cause it differs from java-generated one.
> 
> This is indeed a bug, and a rare occurrence since AFAICT from ISO 30112 and
> googling there is only a single case of word-final lowercasing which is this
> sigma. The attached patch takes a stab at fixing this.

Ummm ... isn't this a counterexample?
https://hebrew4christians.com/Grammar/Unit_One/Final_Forms/final_forms.html

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #15805: Problem with lower function for greek sigma (Σ) letter
@ 2019-05-15 13:44  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Alvaro Herrera <alvherre@2ndquadrant.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Tom Lane @ 2019-05-15 13:44 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Daniel Gustafsson <daniel@yesql.se>; iksss.88@gmail.com, PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

Alvaro Herrera <alvherre@2ndquadrant.com> writes:
> On 2019-May-15, Daniel Gustafsson wrote:
>> This is indeed a bug, and a rare occurrence since AFAICT from ISO 30112 and
>> googling there is only a single case of word-final lowercasing which is this
>> sigma. The attached patch takes a stab at fixing this.

> Ummm ... isn't this a counterexample?
> https://hebrew4christians.com/Grammar/Unit_One/Final_Forms/final_forms.html

I do not think the patch as given is acceptable in any case:

1. assumes without any evidence whatsoever that the system's wide-character
representation is Unicode code points;

2. assumes without checking that the locale is one that would allow this
conversion (counterexample: C locale);

3. unreasonable hard-coded assumption about what the "not a word character"
condition is.

It's possible that 1 and 2 could be finessed by checking both that the
original character is Σ and the new one is σ (in Unicode).  We'd still
theoretically be taking a risk of the wrong substitution if the wchar
representation is not Unicode, but the odds seem fairly small.  As for
point 3, why aren't you using iswalpha() on the next character?

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #15805: Problem with lower function for greek sigma (Σ) letter
@ 2019-05-15 15:31  Daniel Verite <daniel@manitou-mail.org>
  parent: PG Bug reporting form <noreply@postgresql.org>
  1 sibling, 1 reply; 9+ messages in thread

From: Daniel Verite @ 2019-05-15 15:31 UTC (permalink / raw)
  To: iksss.88@gmail.com; pgsql-bugs@lists.postgresql.org

	PG Bug reporting form wrote:

> lower('ΔΗΜΟΤΕΣ ΦΑΙΣΤΟΥ') =
> "δημοτεσ φαιστου" instead of "δημοτες φαιστου"

With PostgreSQL version 10 or newer, you could use an ICU
locale. lower() would produce the expected result:

psql (11.3 (Debian 11.3-1.pgdg90+1))

=> select lower('ΔΗΜΟΤΕΣ ΦΑΙΣΤΟΥ' collate "und-x-icu");
      lower	 
-----------------
 δημοτες φαιστου
(1 row)

This case looks comparable to the case of the german ß (sharp s),
which should be upcased into 'SS', but the locales backed by libc
don't do that:

=> select upper(''Ich muß');
  upper  
---------
 ICH MUß

For that exemple as well, an ICU locale produces a correct
result with regard to linguistic rules:

=> select upper('Ich muß' collate "und-x-icu");
  upper   
----------
 ICH MUSS

The libc library provides an API with character-by-character
case conversions (tolower/toupper), which is too limited
to deal with the above cases, and PostgreSQL basically
just uses this API.


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





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #15805: Problem with lower function for greek sigma (Σ) letter
@ 2019-05-15 15:46  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Daniel Verite <daniel@manitou-mail.org>
  0 siblings, 1 reply; 9+ messages in thread

From: Tom Lane @ 2019-05-15 15:46 UTC (permalink / raw)
  To: Daniel Verite <daniel@manitou-mail.org>; +Cc: iksss.88@gmail.com; pgsql-bugs@lists.postgresql.org

"Daniel Verite" <daniel@manitou-mail.org> writes:
> 	PG Bug reporting form wrote:
>> lower('ΔΗΜΟΤΕΣ ΦΑΙΣΤΟΥ') =
>> "δημοτεσ φαιστου" instead of "δημοτες φαιστου"

> With PostgreSQL version 10 or newer, you could use an ICU
> locale. lower() would produce the expected result:

Oh, if using ICU already fixes this, I think we might as well just
say that you have to use ICU if you want the right behavior for such
cases.

> The libc library provides an API with character-by-character
> case conversions (tolower/toupper), which is too limited
> to deal with the above cases, and PostgreSQL basically
> just uses this API.

Right.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #15805: Problem with lower function for greek sigma (Σ) letter
@ 2019-05-15 16:23  Daniel Gustafsson <daniel@yesql.se>
  parent: Alvaro Herrera <alvherre@2ndquadrant.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Daniel Gustafsson @ 2019-05-15 16:23 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: iksss.88@gmail.com, PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

> On 15 May 2019, at 14:20, Alvaro Herrera <alvherre@2ndquadrant.com> wrote:
> 
> On 2019-May-15, Daniel Gustafsson wrote:
> 
>>> I see unexpected behaviour of lower function for greek sigma letter Σ.
>>> According to wikipedia (https://en.wikipedia.org/wiki/Sigma) if this letter
>>> is in final word position, it should be ς in lowercase, and not σ. But
>>> PotgreSQL lower function returns σ, for example lower('ΔΗΜΟΤΕΣ ΦΑΙΣΤΟΥ') =
>>> "δημοτεσ φαιστου" instead of "δημοτες φαιστου". So if I try to pass this
>>> lower string from another part of a system (java code, for example) Postgre
>>> will not return this row cause it differs from java-generated one.
>> 
>> This is indeed a bug, and a rare occurrence since AFAICT from ISO 30112 and
>> googling there is only a single case of word-final lowercasing which is this
>> sigma. The attached patch takes a stab at fixing this.
> 
> Ummm ... isn't this a counterexample?
> https://hebrew4christians.com/Grammar/Unit_One/Final_Forms/final_forms.html

Hebrew doesn’t have case, so it doesn’t apply in this case.

cheers ./daniel




^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #15805: Problem with lower function for greek sigma (Σ) letter
@ 2019-05-15 17:04  Alvaro Herrera <alvherre@2ndquadrant.com>
  parent: Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 0 replies; 9+ messages in thread

From: Alvaro Herrera @ 2019-05-15 17:04 UTC (permalink / raw)
  To: Daniel Gustafsson <daniel@yesql.se>; +Cc: iksss.88@gmail.com, PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

On 2019-May-15, Daniel Gustafsson wrote:

> > On 15 May 2019, at 14:20, Alvaro Herrera <alvherre@2ndquadrant.com> wrote:

> > Ummm ... isn't this a counterexample?
> > https://hebrew4christians.com/Grammar/Unit_One/Final_Forms/final_forms.html
> 
> Hebrew doesn’t have case, so it doesn’t apply in this case.

Yeah, I realized that afterwards.  Sorry for the noise.

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #15805: Problem with lower function for greek sigma (Σ) letter
@ 2019-05-15 19:43  Daniel Gustafsson <daniel@yesql.se>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 9+ messages in thread

From: Daniel Gustafsson @ 2019-05-15 19:43 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Daniel Verite <daniel@manitou-mail.org>; iksss.88@gmail.com; pgsql-bugs@lists.postgresql.org

> On 15 May 2019, at 17:46, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> 
> "Daniel Verite" <daniel@manitou-mail.org> writes:
>> 	PG Bug reporting form wrote:
>>> lower('ΔΗΜΟΤΕΣ ΦΑΙΣΤΟΥ') =
>>> "δημοτεσ φαιστου" instead of "δημοτες φαιστου"
> 
>> With PostgreSQL version 10 or newer, you could use an ICU
>> locale. lower() would produce the expected result:
> 
> Oh, if using ICU already fixes this, I think we might as well just
> say that you have to use ICU if you want the right behavior for such
> cases.

Seems reasonable.  Maybe it warrants a mention in the docs on the string
function page since it may suprise users?

cheers ./daniel

Attachments:

casefolding.diff (application/octet-stream, 541B) download diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index bc2275c8fe..b2f5a49b29 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1682,6 +1682,10 @@ </tgroup> </table> + <para> + The database must be compiled with ICU in order for contextual aware case-folding, + such as word-final characters. + </para> <para> Additional string manipulation functions are available and are listed in <xref linkend="functions-string-other"/>. Some of them are used internally to implement the
^ permalink raw reply [nested|flat] 9+ messages in thread


end of thread, other threads:[~2019-05-15 19:43 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-05-15 07:57  BUG #15805: Problem with lower function for greek sigma (Σ) letter PG Bug reporting form <noreply@postgresql.org>
2019-05-15 09:53 ` Daniel Gustafsson <daniel@yesql.se>
2019-05-15 12:20   ` Alvaro Herrera <alvherre@2ndquadrant.com>
2019-05-15 13:44     ` Tom Lane <tgl@sss.pgh.pa.us>
2019-05-15 16:23     ` Daniel Gustafsson <daniel@yesql.se>
2019-05-15 17:04       ` Alvaro Herrera <alvherre@2ndquadrant.com>
2019-05-15 15:31 ` Daniel Verite <daniel@manitou-mail.org>
2019-05-15 15:46   ` Tom Lane <tgl@sss.pgh.pa.us>
2019-05-15 19:43     ` Daniel Gustafsson <daniel@yesql.se>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox