Refactor to introduce pg_locale_deterministic().
authorJeff Davis
Thu, 23 Feb 2023 19:17:41 +0000 (11:17 -0800)
committerJeff Davis
Thu, 23 Feb 2023 19:17:41 +0000 (11:17 -0800)
Avoids the need of callers to test for NULL, and also avoids the need
to access the pg_locale_t structure directly.

Reviewed-by: Peter Eisentraut, Peter Geoghegan
Discussion: https://postgr.es/m/a581136455c940d7bd0ff482d3a2bd51af25a94f.camel%40j-davis.com

src/backend/access/hash/hashfunc.c
src/backend/regex/regc_pg_locale.c
src/backend/utils/adt/like.c
src/backend/utils/adt/pg_locale.c
src/backend/utils/adt/varchar.c
src/backend/utils/adt/varlena.c
src/include/utils/pg_locale.h

index 2deba44abd3b568ae1fd6782e502e93adeee31f6..2e2fd5566e70d0864e60500c4f54921d5423ca7f 100644 (file)
@@ -282,7 +282,7 @@ hashtext(PG_FUNCTION_ARGS)
    if (!lc_collate_is_c(collid))
        mylocale = pg_newlocale_from_collation(collid);
 
-   if (!mylocale || mylocale->deterministic)
+   if (pg_locale_deterministic(mylocale))
    {
        result = hash_any((unsigned char *) VARDATA_ANY(key),
                          VARSIZE_ANY_EXHDR(key));
@@ -342,7 +342,7 @@ hashtextextended(PG_FUNCTION_ARGS)
    if (!lc_collate_is_c(collid))
        mylocale = pg_newlocale_from_collation(collid);
 
-   if (!mylocale || mylocale->deterministic)
+   if (pg_locale_deterministic(mylocale))
    {
        result = hash_any_extended((unsigned char *) VARDATA_ANY(key),
                                   VARSIZE_ANY_EXHDR(key),
index 323f00516d48d67642ea6ebe80d15eab9576b88b..00ce735fdd871dc4220bf8419e4a4e8e44b5877b 100644 (file)
@@ -259,7 +259,7 @@ pg_set_regex_collation(Oid collation)
         */
        pg_regex_locale = pg_newlocale_from_collation(collation);
 
-       if (pg_regex_locale && !pg_regex_locale->deterministic)
+       if (!pg_locale_deterministic(pg_regex_locale))
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("nondeterministic collations are not supported for regular expressions")));
index fc6cb7f5b75cc6eb2a599e5bb3e685721a40a3ce..33a2f46aab06999c704133e6aa29ea04fb65f22b 100644 (file)
@@ -155,7 +155,7 @@ GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation
    {
        pg_locale_t locale = pg_newlocale_from_collation(collation);
 
-       if (locale && !locale->deterministic)
+       if (!pg_locale_deterministic(locale))
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("nondeterministic collations are not supported for LIKE")));
@@ -196,7 +196,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation)
    else
        locale = pg_newlocale_from_collation(collation);
 
-   if (locale && !locale->deterministic)
+   if (!pg_locale_deterministic(locale))
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("nondeterministic collations are not supported for ILIKE")));
index ef9efb4a7c901502e593094a41e1ea87a135a115..274b8b9ccd7c62d4f7f56249089ea2f48da40b7d 100644 (file)
@@ -1481,6 +1481,15 @@ report_newlocale_failure(const char *localename)
 }
 #endif                         /* HAVE_LOCALE_T */
 
+bool
+pg_locale_deterministic(pg_locale_t locale)
+{
+   /* default locale must always be deterministic */
+   if (locale == NULL)
+       return true;
+   else
+       return locale->deterministic;
+}
 
 /*
  * Create a locale_t from a collation OID.  Results are cached for the
index 9ff3bcbdb75c7c9c02e6d08e9f446179c224f912..99a4ca7cd00f6f46fc8169f10ddf1622cead135f 100644 (file)
@@ -762,7 +762,7 @@ bpchareq(PG_FUNCTION_ARGS)
    else
        mylocale = pg_newlocale_from_collation(collid);
 
-   if (locale_is_c || !mylocale || mylocale->deterministic)
+   if (locale_is_c || pg_locale_deterministic(mylocale))
    {
        /*
         * Since we only care about equality or not-equality, we can avoid all
@@ -807,7 +807,7 @@ bpcharne(PG_FUNCTION_ARGS)
    else
        mylocale = pg_newlocale_from_collation(collid);
 
-   if (locale_is_c || !mylocale || mylocale->deterministic)
+   if (locale_is_c || pg_locale_deterministic(mylocale))
    {
        /*
         * Since we only care about equality or not-equality, we can avoid all
@@ -1015,7 +1015,7 @@ hashbpchar(PG_FUNCTION_ARGS)
    if (!lc_collate_is_c(collid))
        mylocale = pg_newlocale_from_collation(collid);
 
-   if (!mylocale || mylocale->deterministic)
+   if (pg_locale_deterministic(mylocale))
    {
        result = hash_any((unsigned char *) keydata, keylen);
    }
@@ -1077,7 +1077,7 @@ hashbpcharextended(PG_FUNCTION_ARGS)
    if (!lc_collate_is_c(collid))
        mylocale = pg_newlocale_from_collation(collid);
 
-   if (!mylocale || mylocale->deterministic)
+   if (pg_locale_deterministic(mylocale))
    {
        result = hash_any_extended((unsigned char *) keydata, keylen,
                                   PG_GETARG_INT64(1));
index 4ca823ca7b136735e06124c95ce9e7a5b987c5d1..5778e3f0efdfd984ea1b328f9877dfbfa532c075 100644 (file)
@@ -1221,7 +1221,7 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
    if (!lc_collate_is_c(collid))
        mylocale = pg_newlocale_from_collation(collid);
 
-   if (mylocale && !mylocale->deterministic)
+   if (!pg_locale_deterministic(mylocale))
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("nondeterministic collations are not supported for substring searches")));
@@ -1572,8 +1572,7 @@ varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
        result = pg_strncoll(arg1, len1, arg2, len2, mylocale);
 
        /* Break tie if necessary. */
-       if (result == 0 &&
-           (!mylocale || mylocale->deterministic))
+       if (result == 0 && pg_locale_deterministic(mylocale))
        {
            result = memcmp(arg1, arg2, Min(len1, len2));
            if ((result == 0) && (len1 != len2))
@@ -1628,7 +1627,7 @@ texteq(PG_FUNCTION_ARGS)
    else
        mylocale = pg_newlocale_from_collation(collid);
 
-   if (locale_is_c || !mylocale || mylocale->deterministic)
+   if (locale_is_c || pg_locale_deterministic(mylocale))
    {
        Datum       arg1 = PG_GETARG_DATUM(0);
        Datum       arg2 = PG_GETARG_DATUM(1);
@@ -1687,7 +1686,7 @@ textne(PG_FUNCTION_ARGS)
    else
        mylocale = pg_newlocale_from_collation(collid);
 
-   if (locale_is_c || !mylocale || mylocale->deterministic)
+   if (locale_is_c || pg_locale_deterministic(mylocale))
    {
        Datum       arg1 = PG_GETARG_DATUM(0);
        Datum       arg2 = PG_GETARG_DATUM(1);
@@ -1801,7 +1800,7 @@ text_starts_with(PG_FUNCTION_ARGS)
    if (!lc_collate_is_c(collid))
        mylocale = pg_newlocale_from_collation(collid);
 
-   if (mylocale && !mylocale->deterministic)
+   if (!pg_locale_deterministic(mylocale))
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("nondeterministic collations are not supported for substring searches")));
@@ -2217,8 +2216,7 @@ varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
    result = pg_strcoll(sss->buf1, sss->buf2, sss->locale);
 
    /* Break tie if necessary. */
-   if (result == 0 &&
-       (!sss->locale || sss->locale->deterministic))
+   if (result == 0 && pg_locale_deterministic(sss->locale))
        result = strcmp(sss->buf1, sss->buf2);
 
    /* Cache result, perhaps saving an expensive strcoll() call next time */
index def2b55f94177f88bd2dec1e387d6c66be9d9506..b8f22875a89ee8548d3d41c40eec3937e05bbe55 100644 (file)
@@ -97,6 +97,7 @@ extern PGDLLIMPORT struct pg_locale_struct default_locale;
 extern void make_icu_collator(const char *iculocstr,
                              struct pg_locale_struct *resultp);
 
+extern bool pg_locale_deterministic(pg_locale_t locale);
 extern pg_locale_t pg_newlocale_from_collation(Oid collid);
 
 extern char *get_collation_actual_version(char collprovider, const char *collcollate);