From b7a74abb08cb0fcf41ab9a780e24abe189492fb6 Mon Sep 17 00:00:00 2001 From: Rock Date: Fri, 23 Aug 2024 15:51:48 +0800 Subject: [PATCH] add nls_initcap --- src/common/backend/catalog/builtin_funcs.ini | 6 +- src/common/backend/utils/adt/a_compat.cpp | 35 ++++++++++++ src/common/backend/utils/init/globals.cpp | 2 +- .../rollback-post_catalog_maindb_93_021.sql | 1 + .../rollback-post_catalog_otherdb_93_021.sql | 1 + .../upgrade-post_catalog_maindb_93_021.sql | 7 +++ .../upgrade-post_catalog_otherdb_93_021.sql | 8 +++ src/include/utils/builtins.h | 1 + src/test/regress/expected/function.out | 56 +++++++++++++++++++ src/test/regress/sql/function.sql | 10 +++- 10 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 src/include/catalog/upgrade_sql/rollback_catalog_maindb/rollback-post_catalog_maindb_93_021.sql create mode 100644 src/include/catalog/upgrade_sql/rollback_catalog_otherdb/rollback-post_catalog_otherdb_93_021.sql create mode 100644 src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade-post_catalog_maindb_93_021.sql create mode 100644 src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade-post_catalog_otherdb_93_021.sql diff --git a/src/common/backend/catalog/builtin_funcs.ini b/src/common/backend/catalog/builtin_funcs.ini index 16f5edd33..19f9591f0 100644 --- a/src/common/backend/catalog/builtin_funcs.ini +++ b/src/common/backend/catalog/builtin_funcs.ini @@ -7377,6 +7377,10 @@ "nlssort", 1, AddBuiltinFunc(_0(1849), _1("nlssort"), _2(2), _3(false), _4(false), _5(nlssort), _6(25), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(0), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('i'), _19(0), _20(2, 25, 25), _21(NULL), _22(NULL), _23(NULL), _24(NULL), _25("nlssort"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(true), _32(false), _33(NULL), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0)) ), + AddFuncGroup( + "nls_initcap", 1, + AddBuiltinFunc(_0(9537), _1("nls_initcap"), _2(2), _3(false), _4(false), _5(nls_initcap), _6(25), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(0), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('i'), _19(1), _20(2, 25, 25), _21(NULL), _22(2,'i','i'), _23(2,"str","nlsparam"), _24("({CONST :consttype 25 :consttypmod -1 :constcollid 100 :constlen -1 :constbyval false :constisnull true :ismaxvalue false :location 68 :constvalue <> :cursor_data :row_count 0 :cur_dno -1 :is_open false :found false :not_found false :null_open false :null_fetch false})"), _25("nls_initcap"), _26(NULL), _27(NULL), _28(NULL), _29(1,1), _30(false), _31(NULL), _32(false), _33(NULL), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0)) + ), AddFuncGroup( "node_oid_name", 1, AddBuiltinFunc(_0(3950), _1("node_oid_name"), _2(1), _3(true), _4(false), _5(node_oid_name), _6(2275), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(0), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('i'), _19(0), _20(1, 26), _21(NULL), _22(NULL), _23(NULL), _24(NULL), _25("node_oid_name"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(NULL), _32(false), _33(NULL), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0)) @@ -13503,4 +13507,4 @@ AddFuncGroup( AddFuncGroup( "sparsevec_to_vector", 1, AddBuiltinFunc(_0(8230), _1("sparsevec_to_vector"), _2(3), _3(true), _4(false), _5(sparsevec_to_vector), _6(8305), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(0), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('s'), _19(0), _20(3, 8307, 23, 16), _21(NULL), _22(NULL), _23(NULL), _24(NULL), _25("sparsevec_to_vector"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(NULL), _32(false), _33("NULL"), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0)) - ), \ No newline at end of file + ), diff --git a/src/common/backend/utils/adt/a_compat.cpp b/src/common/backend/utils/adt/a_compat.cpp index ca5f2bef5..a878bb1e1 100644 --- a/src/common/backend/utils/adt/a_compat.cpp +++ b/src/common/backend/utils/adt/a_compat.cpp @@ -112,6 +112,41 @@ Datum initcap(PG_FUNCTION_ARGS) PG_RETURN_TEXT_P(result); } +/******************************************************************** + * + * nls_initcap + * + * Syntax: + * + * text nls_initcap(text string) + * + * Purpose: + * + * Returns string, with first letter of each word in uppercase, all + * other letters in lowercase. A word is defined as a sequence of + * alphanumeric characters, delimited by non-alphanumeric + * characters. + * + ********************************************************************/ + +Datum nls_initcap(PG_FUNCTION_ARGS) +{ + text* in_string = PG_GETARG_TEXT_PP(0); + if (!PG_ARGISNULL(1)) { + ereport(NOTICE, + (errcode(ERRCODE_OPERATE_INVALID_PARAM), + errmsg("The second parameter of this function will not take effect."))); + } + char* out_string = NULL; + text* result = NULL; + FUNC_CHECK_HUGE_POINTER(false, in_string, "nls_initcap()"); + + out_string = str_initcap(VARDATA_ANY(in_string), VARSIZE_ANY_EXHDR(in_string), PG_GET_COLLATION()); + result = cstring_to_text(out_string); + pfree_ext(out_string); + + PG_RETURN_TEXT_P(result); +} /******************************************************************** * * lpad diff --git a/src/common/backend/utils/init/globals.cpp b/src/common/backend/utils/init/globals.cpp index 47e6b3e4b..184ffd51e 100644 --- a/src/common/backend/utils/init/globals.cpp +++ b/src/common/backend/utils/init/globals.cpp @@ -77,7 +77,7 @@ bool will_shutdown = false; * ********************************************/ -const uint32 GRAND_VERSION_NUM = 93020; +const uint32 GRAND_VERSION_NUM = 93021; /******************************************** * 2.VERSION NUM FOR EACH FEATURE diff --git a/src/include/catalog/upgrade_sql/rollback_catalog_maindb/rollback-post_catalog_maindb_93_021.sql b/src/include/catalog/upgrade_sql/rollback_catalog_maindb/rollback-post_catalog_maindb_93_021.sql new file mode 100644 index 000000000..3c253bf58 --- /dev/null +++ b/src/include/catalog/upgrade_sql/rollback_catalog_maindb/rollback-post_catalog_maindb_93_021.sql @@ -0,0 +1 @@ +DROP FUNCTION IF EXISTS pg_catalog.nls_initcap(text, text) CASCADE; \ No newline at end of file diff --git a/src/include/catalog/upgrade_sql/rollback_catalog_otherdb/rollback-post_catalog_otherdb_93_021.sql b/src/include/catalog/upgrade_sql/rollback_catalog_otherdb/rollback-post_catalog_otherdb_93_021.sql new file mode 100644 index 000000000..3c253bf58 --- /dev/null +++ b/src/include/catalog/upgrade_sql/rollback_catalog_otherdb/rollback-post_catalog_otherdb_93_021.sql @@ -0,0 +1 @@ +DROP FUNCTION IF EXISTS pg_catalog.nls_initcap(text, text) CASCADE; \ No newline at end of file diff --git a/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade-post_catalog_maindb_93_021.sql b/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade-post_catalog_maindb_93_021.sql new file mode 100644 index 000000000..1eebc4192 --- /dev/null +++ b/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade-post_catalog_maindb_93_021.sql @@ -0,0 +1,7 @@ +DROP FUNCTION IF EXISTS pg_catalog.nls_initcap(text, text) CASCADE; +SET LOCAL inplace_upgrade_next_system_object_oids=IUO_PROC, 9537; +CREATE OR REPLACE FUNCTION pg_catalog.nls_initcap(str text, nlsparam text DEFAULT NULL::text) + RETURNS text + LANGUAGE internal + IMMUTABLE NOT FENCED NOT SHIPPABLE +AS $function$nls_initcap$function$; diff --git a/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade-post_catalog_otherdb_93_021.sql b/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade-post_catalog_otherdb_93_021.sql new file mode 100644 index 000000000..1034c6004 --- /dev/null +++ b/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade-post_catalog_otherdb_93_021.sql @@ -0,0 +1,8 @@ +DROP FUNCTION IF EXISTS pg_catalog.nls_initcap(text, text) CASCADE; +SET LOCAL inplace_upgrade_next_system_object_oids=IUO_PROC, 9537; +CREATE OR REPLACE FUNCTION pg_catalog.nls_initcap(str text, nlsparam text DEFAULT NULL::text) + RETURNS text + LANGUAGE internal + IMMUTABLE NOT FENCED NOT SHIPPABLE +AS $function$nls_initcap$function$; + diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b2150b30d..13e1f9072 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -1746,6 +1746,7 @@ extern Datum pg_lsn_in(PG_FUNCTION_ARGS); /* nlssort.cpp */ extern Datum nlssort(PG_FUNCTION_ARGS); +extern Datum nls_initcap(PG_FUNCTION_ARGS); extern char *remove_trailing_spaces(const char *src_str); // template function implementation diff --git a/src/test/regress/expected/function.out b/src/test/regress/expected/function.out index 596861c67..e4ee28bb9 100644 --- a/src/test/regress/expected/function.out +++ b/src/test/regress/expected/function.out @@ -2,6 +2,62 @@ create database pl_test_funcion DBCOMPATIBILITY 'pg'; \c pl_test_funcion; create schema distribute_function; set current_schema = distribute_function; +select nls_initcap('hello word'); + nls_initcap +------------- + Hello Word +(1 row) + +select nls_initcap('журнал журнал');--俄语 + nls_initcap +--------------- + Журнал Журнал +(1 row) + +SELECT nls_initcap('el gato negro');--西班牙语 + nls_initcap +--------------- + El Gato Negro +(1 row) + +SELECT nls_initcap('o gato preto');--葡萄牙语 + nls_initcap +-------------- + O Gato Preto +(1 row) + +SELECT nls_initcap('HELLO ß world', 'NLS_SORT = XGERMAN'); --第二个参数不支持。 +NOTICE: The second parameter of this function will not take effect. +CONTEXT: referenced column: nls_initcap + nls_initcap +--------------- + Hello ß World +(1 row) + +select nls_initcap('hello word'); --3个空格 + nls_initcap +-------------- + Hello Word +(1 row) + +select nls_initcap('hello word'); --2个tab键等作为分隔符 + nls_initcap +------------------- + Hello Word +(1 row) + +select nls_initcap('hello *** &&& ()()() 你好啦 word'); --中间有其他非英文字母,或者中文等 + nls_initcap +---------------------------------------- + Hello *** &&& ()()() 你好啦 Word +(1 row) + +SELECT nls_initcap('o gato preto','o gato preto','o gato preto');--报错 +ERROR: function nls_initcap(unknown, unknown, unknown) does not exist +LINE 1: SELECT nls_initcap('o gato preto','o gato preto','o gato pre... + ^ +HINT: No function matches the given name and argument types. You might need to add explicit type casts. +CONTEXT: referenced column: nls_initcap create table function_table_01(f1 int, f2 float, f3 text); insert into function_table_01 values(1,2.0,'abcde'),(2,4.0,'abcde'),(3,5.0,'affde'); insert into function_table_01 values(4,7.0,'aeede'),(5,1.0,'facde'),(6,3.0,'affde'); diff --git a/src/test/regress/sql/function.sql b/src/test/regress/sql/function.sql index e6728d1d8..9f343884a 100644 --- a/src/test/regress/sql/function.sql +++ b/src/test/regress/sql/function.sql @@ -2,7 +2,15 @@ create database pl_test_funcion DBCOMPATIBILITY 'pg'; \c pl_test_funcion; create schema distribute_function; set current_schema = distribute_function; - +select nls_initcap('hello word'); +select nls_initcap('журнал журнал');--俄语 +SELECT nls_initcap('el gato negro');--西班牙语 +SELECT nls_initcap('o gato preto');--葡萄牙语 +SELECT nls_initcap('HELLO ß world', 'NLS_SORT = XGERMAN'); --第二个参数不支持。 +select nls_initcap('hello word'); --3个空格 +select nls_initcap('hello word'); --2个tab键等作为分隔符 +select nls_initcap('hello *** &&& ()()() 你好啦 word'); --中间有其他非英文字母,或者中文等 +SELECT nls_initcap('o gato preto','o gato preto','o gato preto');--报错 create table function_table_01(f1 int, f2 float, f3 text); insert into function_table_01 values(1,2.0,'abcde'),(2,4.0,'abcde'),(3,5.0,'affde'); insert into function_table_01 values(4,7.0,'aeede'),(5,1.0,'facde'),(6,3.0,'affde');