mirror of
https://github.com/postgres/postgres.git
synced 2025-12-21 05:21:08 +03:00
Support C.UTF-8 locale in the new builtin collation provider.
The builtin C.UTF-8 locale has similar semantics to the libc locale of the same name. That is, code point sort order (fast, memcmp-based) combined with Unicode semantics for character operations such as pattern matching, regular expressions, and LOWER()/INITCAP()/UPPER(). The character semantics are based on Unicode simple case mappings. The builtin provider's C.UTF-8 offers several important advantages over libc: * faster sorting -- benefits from additional optimizations such as abbreviated keys and varstrfastcmp_c * faster case conversion, e.g. LOWER(), at least compared with some libc implementations * available on all platforms with identical semantics, and the semantics are stable, testable, and documentable within a given Postgres major version Being based on memcmp, the builtin C.UTF-8 locale does not offer natural language sort order. But it is an improvement for most use cases that might otherwise use libc's "C.UTF-8" locale, as well as many use cases that use libc's "C" locale. Discussion: https://postgr.es/m/ff4c2f2f9c8fc7ca27c1c24ae37ecaeaeaff6b53.camel%40j-davis.com Reviewed-by: Daniel Vérité, Peter Eisentraut, Jeremy Schneider
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
*/
|
||||
|
||||
#include "catalog/pg_collation.h"
|
||||
#include "common/unicode_case.h"
|
||||
#include "common/unicode_category.h"
|
||||
#include "utils/pg_locale.h"
|
||||
|
||||
/*
|
||||
@@ -64,6 +66,7 @@
|
||||
typedef enum
|
||||
{
|
||||
PG_REGEX_LOCALE_C, /* C locale (encoding independent) */
|
||||
PG_REGEX_BUILTIN, /* built-in Unicode semantics */
|
||||
PG_REGEX_LOCALE_WIDE, /* Use <wctype.h> functions */
|
||||
PG_REGEX_LOCALE_1BYTE, /* Use <ctype.h> functions */
|
||||
PG_REGEX_LOCALE_WIDE_L, /* Use locale_t <wctype.h> functions */
|
||||
@@ -266,7 +269,12 @@ pg_set_regex_collation(Oid collation)
|
||||
if (GetDatabaseEncoding() == PG_UTF8)
|
||||
{
|
||||
if (pg_regex_locale)
|
||||
pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L;
|
||||
{
|
||||
if (pg_regex_locale->provider == COLLPROVIDER_BUILTIN)
|
||||
pg_regex_strategy = PG_REGEX_BUILTIN;
|
||||
else
|
||||
pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L;
|
||||
}
|
||||
else
|
||||
pg_regex_strategy = PG_REGEX_LOCALE_WIDE;
|
||||
}
|
||||
@@ -290,6 +298,8 @@ pg_wc_isdigit(pg_wchar c)
|
||||
case PG_REGEX_LOCALE_C:
|
||||
return (c <= (pg_wchar) 127 &&
|
||||
(pg_char_properties[c] & PG_ISDIGIT));
|
||||
case PG_REGEX_BUILTIN:
|
||||
return pg_u_isdigit(c, true);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
|
||||
return iswdigit((wint_t) c);
|
||||
@@ -322,6 +332,8 @@ pg_wc_isalpha(pg_wchar c)
|
||||
case PG_REGEX_LOCALE_C:
|
||||
return (c <= (pg_wchar) 127 &&
|
||||
(pg_char_properties[c] & PG_ISALPHA));
|
||||
case PG_REGEX_BUILTIN:
|
||||
return pg_u_isalpha(c);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
|
||||
return iswalpha((wint_t) c);
|
||||
@@ -354,6 +366,8 @@ pg_wc_isalnum(pg_wchar c)
|
||||
case PG_REGEX_LOCALE_C:
|
||||
return (c <= (pg_wchar) 127 &&
|
||||
(pg_char_properties[c] & PG_ISALNUM));
|
||||
case PG_REGEX_BUILTIN:
|
||||
return pg_u_isalnum(c, true);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
|
||||
return iswalnum((wint_t) c);
|
||||
@@ -395,6 +409,8 @@ pg_wc_isupper(pg_wchar c)
|
||||
case PG_REGEX_LOCALE_C:
|
||||
return (c <= (pg_wchar) 127 &&
|
||||
(pg_char_properties[c] & PG_ISUPPER));
|
||||
case PG_REGEX_BUILTIN:
|
||||
return pg_u_isupper(c);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
|
||||
return iswupper((wint_t) c);
|
||||
@@ -427,6 +443,8 @@ pg_wc_islower(pg_wchar c)
|
||||
case PG_REGEX_LOCALE_C:
|
||||
return (c <= (pg_wchar) 127 &&
|
||||
(pg_char_properties[c] & PG_ISLOWER));
|
||||
case PG_REGEX_BUILTIN:
|
||||
return pg_u_islower(c);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
|
||||
return iswlower((wint_t) c);
|
||||
@@ -459,6 +477,8 @@ pg_wc_isgraph(pg_wchar c)
|
||||
case PG_REGEX_LOCALE_C:
|
||||
return (c <= (pg_wchar) 127 &&
|
||||
(pg_char_properties[c] & PG_ISGRAPH));
|
||||
case PG_REGEX_BUILTIN:
|
||||
return pg_u_isgraph(c);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
|
||||
return iswgraph((wint_t) c);
|
||||
@@ -491,6 +511,8 @@ pg_wc_isprint(pg_wchar c)
|
||||
case PG_REGEX_LOCALE_C:
|
||||
return (c <= (pg_wchar) 127 &&
|
||||
(pg_char_properties[c] & PG_ISPRINT));
|
||||
case PG_REGEX_BUILTIN:
|
||||
return pg_u_isprint(c);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
|
||||
return iswprint((wint_t) c);
|
||||
@@ -523,6 +545,8 @@ pg_wc_ispunct(pg_wchar c)
|
||||
case PG_REGEX_LOCALE_C:
|
||||
return (c <= (pg_wchar) 127 &&
|
||||
(pg_char_properties[c] & PG_ISPUNCT));
|
||||
case PG_REGEX_BUILTIN:
|
||||
return pg_u_ispunct(c, true);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
|
||||
return iswpunct((wint_t) c);
|
||||
@@ -555,6 +579,8 @@ pg_wc_isspace(pg_wchar c)
|
||||
case PG_REGEX_LOCALE_C:
|
||||
return (c <= (pg_wchar) 127 &&
|
||||
(pg_char_properties[c] & PG_ISSPACE));
|
||||
case PG_REGEX_BUILTIN:
|
||||
return pg_u_isspace(c);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
|
||||
return iswspace((wint_t) c);
|
||||
@@ -588,6 +614,8 @@ pg_wc_toupper(pg_wchar c)
|
||||
if (c <= (pg_wchar) 127)
|
||||
return pg_ascii_toupper((unsigned char) c);
|
||||
return c;
|
||||
case PG_REGEX_BUILTIN:
|
||||
return unicode_uppercase_simple(c);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
/* force C behavior for ASCII characters, per comments above */
|
||||
if (c <= (pg_wchar) 127)
|
||||
@@ -628,6 +656,8 @@ pg_wc_tolower(pg_wchar c)
|
||||
if (c <= (pg_wchar) 127)
|
||||
return pg_ascii_tolower((unsigned char) c);
|
||||
return c;
|
||||
case PG_REGEX_BUILTIN:
|
||||
return unicode_lowercase_simple(c);
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
/* force C behavior for ASCII characters, per comments above */
|
||||
if (c <= (pg_wchar) 127)
|
||||
@@ -792,6 +822,9 @@ pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
|
||||
max_chr = (pg_wchar) MAX_SIMPLE_CHR;
|
||||
#endif
|
||||
break;
|
||||
case PG_REGEX_BUILTIN:
|
||||
max_chr = (pg_wchar) MAX_SIMPLE_CHR;
|
||||
break;
|
||||
case PG_REGEX_LOCALE_WIDE:
|
||||
case PG_REGEX_LOCALE_WIDE_L:
|
||||
max_chr = (pg_wchar) MAX_SIMPLE_CHR;
|
||||
@@ -809,6 +842,7 @@ pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
|
||||
max_chr = (pg_wchar) MAX_SIMPLE_CHR;
|
||||
break;
|
||||
default:
|
||||
Assert(false);
|
||||
max_chr = 0; /* can't get here, but keep compiler quiet */
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -77,6 +77,8 @@
|
||||
|
||||
#include "catalog/pg_collation.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "common/unicode_case.h"
|
||||
#include "common/unicode_category.h"
|
||||
#include "mb/pg_wchar.h"
|
||||
#include "nodes/miscnodes.h"
|
||||
#include "parser/scansup.h"
|
||||
@@ -1679,6 +1681,34 @@ str_tolower(const char *buff, size_t nbytes, Oid collid)
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN)
|
||||
{
|
||||
const char *src = buff;
|
||||
size_t srclen = nbytes;
|
||||
size_t dstsize;
|
||||
char *dst;
|
||||
size_t needed;
|
||||
|
||||
Assert(GetDatabaseEncoding() == PG_UTF8);
|
||||
|
||||
/* first try buffer of equal size plus terminating NUL */
|
||||
dstsize = srclen + 1;
|
||||
dst = palloc(dstsize);
|
||||
|
||||
needed = unicode_strlower(dst, dstsize, src, srclen);
|
||||
if (needed + 1 > dstsize)
|
||||
{
|
||||
/* grow buffer if needed and retry */
|
||||
dstsize = needed + 1;
|
||||
dst = repalloc(dst, dstsize);
|
||||
needed = unicode_strlower(dst, dstsize, src, srclen);
|
||||
Assert(needed + 1 == dstsize);
|
||||
}
|
||||
|
||||
Assert(dst[needed] == '\0');
|
||||
result = dst;
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC);
|
||||
|
||||
@@ -1799,6 +1829,34 @@ str_toupper(const char *buff, size_t nbytes, Oid collid)
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN)
|
||||
{
|
||||
const char *src = buff;
|
||||
size_t srclen = nbytes;
|
||||
size_t dstsize;
|
||||
char *dst;
|
||||
size_t needed;
|
||||
|
||||
Assert(GetDatabaseEncoding() == PG_UTF8);
|
||||
|
||||
/* first try buffer of equal size plus terminating NUL */
|
||||
dstsize = srclen + 1;
|
||||
dst = palloc(dstsize);
|
||||
|
||||
needed = unicode_strupper(dst, dstsize, src, srclen);
|
||||
if (needed + 1 > dstsize)
|
||||
{
|
||||
/* grow buffer if needed and retry */
|
||||
dstsize = needed + 1;
|
||||
dst = repalloc(dst, dstsize);
|
||||
needed = unicode_strupper(dst, dstsize, src, srclen);
|
||||
Assert(needed + 1 == dstsize);
|
||||
}
|
||||
|
||||
Assert(dst[needed] == '\0');
|
||||
result = dst;
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC);
|
||||
|
||||
@@ -1920,6 +1978,60 @@ str_initcap(const char *buff, size_t nbytes, Oid collid)
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN)
|
||||
{
|
||||
const unsigned char *src = (unsigned char *) buff;
|
||||
size_t srclen = nbytes;
|
||||
unsigned char *dst;
|
||||
size_t dstsize;
|
||||
int srcoff = 0;
|
||||
int dstoff = 0;
|
||||
|
||||
Assert(GetDatabaseEncoding() == PG_UTF8);
|
||||
|
||||
/* overflow paranoia */
|
||||
if ((srclen + 1) > (INT_MAX / MAX_MULTIBYTE_CHAR_LEN))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_OUT_OF_MEMORY),
|
||||
errmsg("out of memory")));
|
||||
|
||||
/* result is at most srclen codepoints plus terminating NUL */
|
||||
dstsize = srclen * MAX_MULTIBYTE_CHAR_LEN + 1;
|
||||
dst = (unsigned char *) palloc(dstsize);
|
||||
|
||||
while (srcoff < nbytes)
|
||||
{
|
||||
pg_wchar u1 = utf8_to_unicode(src + srcoff);
|
||||
pg_wchar u2;
|
||||
int u1len = unicode_utf8len(u1);
|
||||
int u2len;
|
||||
|
||||
if (wasalnum)
|
||||
u2 = unicode_lowercase_simple(u1);
|
||||
else
|
||||
u2 = unicode_uppercase_simple(u1);
|
||||
|
||||
u2len = unicode_utf8len(u2);
|
||||
|
||||
Assert(dstoff + u2len + 1 <= dstsize);
|
||||
|
||||
wasalnum = pg_u_isalnum(u2, true);
|
||||
|
||||
unicode_to_utf8(u2, dst + dstoff);
|
||||
srcoff += u1len;
|
||||
dstoff += u2len;
|
||||
}
|
||||
|
||||
Assert(dstoff + 1 <= dstsize);
|
||||
*(dst + dstoff) = '\0';
|
||||
dstoff++;
|
||||
|
||||
/* allocate result buffer of the right size and free workspace */
|
||||
result = palloc(dstoff);
|
||||
memcpy(result, dst, dstoff);
|
||||
pfree(dst);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC);
|
||||
|
||||
|
||||
@@ -1270,8 +1270,14 @@ lookup_collation_cache(Oid collation, bool set_flags)
|
||||
|
||||
if (collform->collprovider == COLLPROVIDER_BUILTIN)
|
||||
{
|
||||
Datum datum;
|
||||
const char *colllocale;
|
||||
|
||||
datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale);
|
||||
colllocale = TextDatumGetCString(datum);
|
||||
|
||||
cache_entry->collate_is_c = true;
|
||||
cache_entry->ctype_is_c = true;
|
||||
cache_entry->ctype_is_c = (strcmp(colllocale, "C") == 0);
|
||||
}
|
||||
else if (collform->collprovider == COLLPROVIDER_LIBC)
|
||||
{
|
||||
@@ -1670,7 +1676,6 @@ pg_newlocale_from_collation(Oid collid)
|
||||
|
||||
collversionstr = TextDatumGetCString(datum);
|
||||
|
||||
Assert(collform->collprovider != COLLPROVIDER_BUILTIN);
|
||||
if (collform->collprovider == COLLPROVIDER_LIBC)
|
||||
datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate);
|
||||
else
|
||||
@@ -1725,7 +1730,13 @@ get_collation_actual_version(char collprovider, const char *collcollate)
|
||||
{
|
||||
char *collversion = NULL;
|
||||
|
||||
/* the builtin collation provider is not versioned */
|
||||
/*
|
||||
* The only two supported locales (C and C.UTF-8) are both based on memcmp
|
||||
* and are not expected to change.
|
||||
*
|
||||
* Note that the character semantics may change for some locales, but the
|
||||
* collation version only tracks changes to sort order.
|
||||
*/
|
||||
if (collprovider == COLLPROVIDER_BUILTIN)
|
||||
return NULL;
|
||||
|
||||
@@ -2505,13 +2516,17 @@ pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src,
|
||||
int
|
||||
builtin_locale_encoding(const char *locale)
|
||||
{
|
||||
if (strcmp(locale, "C") != 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("invalid locale name \"%s\" for builtin provider",
|
||||
locale)));
|
||||
if (strcmp(locale, "C") == 0)
|
||||
return -1;
|
||||
if (strcmp(locale, "C.UTF-8") == 0)
|
||||
return PG_UTF8;
|
||||
|
||||
return -1;
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("invalid locale name \"%s\" for builtin provider",
|
||||
locale)));
|
||||
|
||||
return 0; /* keep compiler quiet */
|
||||
}
|
||||
|
||||
|
||||
@@ -2525,13 +2540,28 @@ builtin_locale_encoding(const char *locale)
|
||||
const char *
|
||||
builtin_validate_locale(int encoding, const char *locale)
|
||||
{
|
||||
if (strcmp(locale, "C") != 0)
|
||||
const char *canonical_name = NULL;
|
||||
int required_encoding;
|
||||
|
||||
if (strcmp(locale, "C") == 0)
|
||||
canonical_name = "C";
|
||||
else if (strcmp(locale, "C.UTF-8") == 0 || strcmp(locale, "C.UTF8") == 0)
|
||||
canonical_name = "C.UTF-8";
|
||||
|
||||
if (!canonical_name)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("invalid locale name \"%s\" for builtin provider",
|
||||
locale)));
|
||||
|
||||
return "C";
|
||||
required_encoding = builtin_locale_encoding(canonical_name);
|
||||
if (required_encoding >= 0 && encoding != required_encoding)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("encoding \"%s\" does not match locale \"%s\"",
|
||||
pg_encoding_to_char(encoding), locale)));
|
||||
|
||||
return canonical_name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user