From d3561e1d09a651850afef809ee9499b43185dbb2 Mon Sep 17 00:00:00 2001 From: Manisha Malhar Rao Deshpande <125510312+manisha-deshpande@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:00:13 -0700 Subject: [PATCH] Uninitialized variable fix for ISNUMERIC function with varchar and nvarchar arguments (#2945) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix uninitialised variable issue related to varchar2numeric function Task: BABEL-5129 Signed-off-by: manisha-deshpande * Test cases for isnumeric function with varchar and nvarchar variables Task: BABEL-5129 Signed-off-by: “manisha-deshpande” <“dmanisha.work@gmail.com”> * removing unintended merged commits Fixing pr comments, added test name to jdbc latest schedule Task: BABEL-5129 Signed-off-by: “manisha-deshpande” <“dmanisha.work@gmail.com”> Fixed issue in SP_TABLES() when @table_name parameter has square brackets around underscore (#2940) Currently, SP_TABLES procedure does not support wild card [], which matches to any character that is specified in square bracket. Due to this SP_TABLES procedure returns different output when @table_name parameter value is table_1 and table[_]1. This issue exists because previously we were using helper function sp_tables_internal whose body language was plpgsql hence we were using PostgresSQL Like operator for comparison, which does not support square bracket wild card expression. Fixed the issue by removing this helper function and migrating this function logic inside the SP_TABLES procedure whose language is pltsql and Like operator in pltsql supports square bracket wild card. Task: BABEL-4128 Signed-off-by: Rohit Bhagat Fix date functions to take into account the timezone setting (#2947) Issues - When the timezone parameter group setting is set to something other than the default UTC, Babelfish date related functions like SYSDATETIME(), SYSDATETIMEOFFSET(), GETDATE() and CURRENT_TIMESTAMP don't take it into account, while they should. The same issue occurs with a session-level timezone parameter. SYSDATETIMEOFFSET() function returns datetimeoffset which does not store timezone due to incorrect casting function. Changes made to fix the issues - Added appropriate CAST from TIMESTAMP to respective return types of date functions to ensure proper accounting for timezone setting. Added timestamptz_datetimeoffset function for converting timestamp with time zone to datetimeoffset. It ensures to store timezone property similar to timestamptz_datetime2 and timestamptz_datetime functions. Task: BABEL-5069 Signed-off-by: Anikait Agrawal refactoring (#2951) Reduce the number of arguments passed to gen_createdb_subcmds & gen_dropdb_subcmds. Create a helper functions to generate RoleSpec & AccessPriv nodes from cstring. Create a new function for inserting fixed roles/users to bbf_authid_user_ext catalog during create database. Remove unused declaration update_RevokeRoleStmt Issues Resolved: [BABEL-5274] Signed-off-by: Tanzeel Khan tzlkhan@amazon.com Support Cross-database references in views (#2899) Support execution of views which references objects (tables/views/functions) from across the databases. Here we are talking about Babelfish logical database (T-SQL database) which is different from a physical Postgres database. To support this, perform permission checks for cross database objects using session user (login) instead of current_user (user of current T-SQL database). The reason login can be used for permission check is since login is member of all it’s users, so it inherits all their permissions so it will be able execute any cross database objects owned by its users. This commit handles functions and tables/views separately for cross database permission checks. For functions/procedures, a new hook `ExecFuncProc_AclCheck_hook` and for tables/views existing `ExecutorStart_hook` will be used to decide whether to use session user or current_user for permission check depending upon whether the object is from same or different database. We will be using `is_schema_from_db` function to identify if the object is from different database which performs a lookup into `babelfish_namespace_ext` catalog table which can be expensive as will be doing it pretty frequently. So, added this table into SYSCACHE for better performance. Tables/views permissions are handled slightly different than functions as we do not blindly want to check the permissions against session user (current login) since permissions of RTEs inside a view are checked against that view's owner which can very well be a user of some different database. So if we blindly check permission against session user instead of view's owner then it would break view's ownership chaining. Instead, we will replace `checkAsUser` with it's corresponding mapped login if present and only in cases where `checkAsUser` is not set, we will replace it with session user (login). We are using login to allow cross database queries since login can access all its objects across the databases. Getting mapped login to a user require lookup into sys.babelfish_authid_user_ext catalog table using its primary key column (rolname) so added this table is also into SYSCACHE. Additionally, remove previous code to globally set current user to session user since newer logic takes care of the permission check now. Task: BABEL-5206 Signed-off-by: Rishabh Tanwar Engine PR: https://github.com/babelfish-for-postgresql/postgresql_modified_for_babelfish/pull/434 Revert unintended rebase commits "Support Cross-database references in views (#2899)" This reverts commit 90a0b1fad4b49ce5e0523acad6ccbd0639e4f29a. Revert unintended changes "refactoring (#2951)" This reverts commit 37ce1309191dda4580d47ddc4f0aa43c0caa5ed0. Revert unintended rebase commit "Fix date functions to take into account the timezone setting (#2947)" This reverts commit 3df9fa08e8816c6cff6c7c5734950a5ea2e73142. Revert unintended rebase commit "Fixed issue in SP_TABLES() when @table_name parameter has square brackets around underscore (#2940)" This reverts commit 230c433b2ef9f20da8ab5fef226350e71a2461e1. * Added null test case Task: BABEL-5129 Signed-off-by: “manisha-deshpande” <“dmanisha.work@gmail.com”> * Added boundary test cases Signed-off-by: “manisha-deshpande” <“dmanisha.work@gmail.com”> --------- Signed-off-by: manisha-deshpande Signed-off-by: “manisha-deshpande” <“dmanisha.work@gmail.com”> Co-authored-by: “manisha-deshpande” <“dmanisha.work@gmail.com”> --- contrib/babelfishpg_common/src/varchar.c | 5 +- test/JDBC/expected/BABEL-5129-vu-cleanup.out | 2 + test/JDBC/expected/BABEL-5129-vu-prepare.out | 28 +++ test/JDBC/expected/BABEL-5129-vu-verify.out | 217 +++++++++++++++++++ test/JDBC/input/BABEL-5129-vu-cleanup.sql | 2 + test/JDBC/input/BABEL-5129-vu-prepare.sql | 26 +++ test/JDBC/input/BABEL-5129-vu-verify.sql | 112 ++++++++++ test/JDBC/upgrade/latest/schedule | 1 + 8 files changed, 392 insertions(+), 1 deletion(-) create mode 100644 test/JDBC/expected/BABEL-5129-vu-cleanup.out create mode 100644 test/JDBC/expected/BABEL-5129-vu-prepare.out create mode 100644 test/JDBC/expected/BABEL-5129-vu-verify.out create mode 100644 test/JDBC/input/BABEL-5129-vu-cleanup.sql create mode 100644 test/JDBC/input/BABEL-5129-vu-prepare.sql create mode 100644 test/JDBC/input/BABEL-5129-vu-verify.sql diff --git a/contrib/babelfishpg_common/src/varchar.c b/contrib/babelfishpg_common/src/varchar.c index b6408656de3..d6ce302a7b3 100644 --- a/contrib/babelfishpg_common/src/varchar.c +++ b/contrib/babelfishpg_common/src/varchar.c @@ -922,7 +922,10 @@ varchar2numeric(PG_FUNCTION_ARGS) char *str; str = varchar2cstring(source); - result = DatumGetNumeric(DirectFunctionCall1(numeric_in, CStringGetDatum(str))); + result = DatumGetNumeric(DirectFunctionCall3(numeric_in, + CStringGetDatum(str), + ObjectIdGetDatum(InvalidOid), + Int32GetDatum(-1))); pfree(str); PG_RETURN_NUMERIC(result); } diff --git a/test/JDBC/expected/BABEL-5129-vu-cleanup.out b/test/JDBC/expected/BABEL-5129-vu-cleanup.out new file mode 100644 index 00000000000..cee389b28f9 --- /dev/null +++ b/test/JDBC/expected/BABEL-5129-vu-cleanup.out @@ -0,0 +1,2 @@ +DROP TABLE babel_5129 +GO diff --git a/test/JDBC/expected/BABEL-5129-vu-prepare.out b/test/JDBC/expected/BABEL-5129-vu-prepare.out new file mode 100644 index 00000000000..b77dbc007b0 --- /dev/null +++ b/test/JDBC/expected/BABEL-5129-vu-prepare.out @@ -0,0 +1,28 @@ + +-- Tests for ISNUMERIC function with varchar and nvarchar variables +CREATE TABLE babel_5129 ( + int_type int, + numeric_type numeric(10,5), + money_type money, + varchar_type varchar(20), + nvarchar_type nvarchar(20) +) +GO + +INSERT INTO babel_5129 ( + int_type, + numeric_type, + money_type, + varchar_type, + nvarchar_type +) +VALUES ( + 45000, + 12345.12, + 237891.22, + '12.3420000000', + '12.3420000000' +) +GO +~~ROW COUNT: 1~~ + diff --git a/test/JDBC/expected/BABEL-5129-vu-verify.out b/test/JDBC/expected/BABEL-5129-vu-verify.out new file mode 100644 index 00000000000..41a860d252e --- /dev/null +++ b/test/JDBC/expected/BABEL-5129-vu-verify.out @@ -0,0 +1,217 @@ + +-- Tests for ISNUMERIC function with varchar and nvarchar variables +SELECT * FROM babel_5129 +GO +~~START~~ +int#!#numeric#!#money#!#varchar#!#nvarchar +45000#!#12345.12000#!#237891.2200#!#12.3420000000#!#12.3420000000 +~~END~~ + +-- Test int +SELECT ISNUMERIC(int_type) +FROM babel_5129 +GO +~~START~~ +int +1 +~~END~~ + +-- Test numeric +SELECT ISNUMERIC(numeric_type) +FROM babel_5129 +GO +~~START~~ +int +1 +~~END~~ + +-- Test money +SELECT ISNUMERIC(money_type) +FROM babel_5129 +GO +~~START~~ +int +1 +~~END~~ + +-- Test varchar +SELECT ISNUMERIC(varchar_type) +FROM babel_5129 +GO +~~START~~ +int +1 +~~END~~ + +-- Test nvarchar +SELECT ISNUMERIC(nvarchar_type) +FROM babel_5129 +GO +~~START~~ +int +1 +~~END~~ + + +-- Test numeric variable +DECLARE @a numeric(24,6); +SELECT @a = 12.3420000000; +SELECT ISNUMERIC(@a), LEN(@a), DATALENGTH(@a) +GO +~~START~~ +int#!#int#!#int +1#!#9#!#6 +~~END~~ + + +-- Test varchar variable +DECLARE @v varchar(20); +SELECT @v = '12.3420000000'; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO +~~START~~ +int#!#int#!#int +1#!#13#!#13 +~~END~~ + + +-- Test nvarchar variable +DECLARE @nv nvarchar(10); +SELECT @nv = '12.3420000000'; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO +~~START~~ +int#!#int#!#int +1#!#10#!#10 +~~END~~ + + +-- Test NULL varchar variable +DECLARE @v varchar(20); +SELECT @v = NULL; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO +~~START~~ +int#!#int#!#int +0#!##!# +~~END~~ + + +-- Test NULL nvarchar variable +DECLARE @nv nvarchar(10); +SELECT @nv = null; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO +~~START~~ +int#!#int#!#int +0#!##!# +~~END~~ + + +-- Test empty varchar variable +DECLARE @v varchar(20); +SELECT @v = ''; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO +~~START~~ +int#!#int#!#int +0#!#0#!#0 +~~END~~ + + +-- Test empty nvarchar variable +DECLARE @nv nvarchar(10); +SELECT @nv = ''; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO +~~START~~ +int#!#int#!#int +0#!#0#!#0 +~~END~~ + + +-- Test varchar with number argument that exceeds range of bigint. +DECLARE @v varchar(20); +SELECT @v = '9223372036854775807'; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO +~~START~~ +int#!#int#!#int +1#!#19#!#19 +~~END~~ + + +DECLARE @v varchar(20); +SELECT @v = '-9223372036854775808'; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO +~~START~~ +int#!#int#!#int +1#!#20#!#20 +~~END~~ + + +-- Test nvarchar with number argument that exceeds range of bigint. +DECLARE @nv nvarchar(20); +SELECT @nv = '9223372036854775807'; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO +~~START~~ +int#!#int#!#int +1#!#19#!#19 +~~END~~ + + +DECLARE @nv nvarchar(20); +SELECT @nv = '-9223372036854775808'; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO +~~START~~ +int#!#int#!#int +1#!#20#!#20 +~~END~~ + + +-- Test varchar with lengthy numeric value +DECLARE @v varchar; +SELECT @v = '12345678901234567890123456789012345'; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO +~~START~~ +int#!#int#!#int +1#!#1#!#1 +~~END~~ + + +-- Test nvarchar with lengthy numeric value +DECLARE @nv nvarchar; +SELECT @nv = '12345678901234567890123456789012345'; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO +~~START~~ +int#!#int#!#int +1#!#1#!#1 +~~END~~ + + +-- Test varchar variable with invalid numeric +DECLARE @v varchar(20); +SELECT @v = '12.34.20000000'; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO +~~START~~ +int#!#int#!#int +0#!#14#!#14 +~~END~~ + + +-- Test nvarchar variable with invalid numeric +DECLARE @nv nvarchar(10); +SELECT @nv = '12.34.20000000'; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO +~~START~~ +int#!#int#!#int +0#!#10#!#10 +~~END~~ + diff --git a/test/JDBC/input/BABEL-5129-vu-cleanup.sql b/test/JDBC/input/BABEL-5129-vu-cleanup.sql new file mode 100644 index 00000000000..2fb355d10ab --- /dev/null +++ b/test/JDBC/input/BABEL-5129-vu-cleanup.sql @@ -0,0 +1,2 @@ +DROP TABLE babel_5129 +GO \ No newline at end of file diff --git a/test/JDBC/input/BABEL-5129-vu-prepare.sql b/test/JDBC/input/BABEL-5129-vu-prepare.sql new file mode 100644 index 00000000000..06e10855753 --- /dev/null +++ b/test/JDBC/input/BABEL-5129-vu-prepare.sql @@ -0,0 +1,26 @@ +-- Tests for ISNUMERIC function with varchar and nvarchar variables + +CREATE TABLE babel_5129 ( + int_type int, + numeric_type numeric(10,5), + money_type money, + varchar_type varchar(20), + nvarchar_type nvarchar(20) +) +GO + +INSERT INTO babel_5129 ( + int_type, + numeric_type, + money_type, + varchar_type, + nvarchar_type +) +VALUES ( + 45000, + 12345.12, + 237891.22, + '12.3420000000', + '12.3420000000' +) +GO \ No newline at end of file diff --git a/test/JDBC/input/BABEL-5129-vu-verify.sql b/test/JDBC/input/BABEL-5129-vu-verify.sql new file mode 100644 index 00000000000..0ef888bbccf --- /dev/null +++ b/test/JDBC/input/BABEL-5129-vu-verify.sql @@ -0,0 +1,112 @@ +-- Tests for ISNUMERIC function with varchar and nvarchar variables + +SELECT * FROM babel_5129 +GO +-- Test int +SELECT ISNUMERIC(int_type) +FROM babel_5129 +GO +-- Test numeric +SELECT ISNUMERIC(numeric_type) +FROM babel_5129 +GO +-- Test money +SELECT ISNUMERIC(money_type) +FROM babel_5129 +GO +-- Test varchar +SELECT ISNUMERIC(varchar_type) +FROM babel_5129 +GO +-- Test nvarchar +SELECT ISNUMERIC(nvarchar_type) +FROM babel_5129 +GO + +-- Test numeric variable +DECLARE @a numeric(24,6); +SELECT @a = 12.3420000000; +SELECT ISNUMERIC(@a), LEN(@a), DATALENGTH(@a) +GO + +-- Test varchar variable +DECLARE @v varchar(20); +SELECT @v = '12.3420000000'; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO + +-- Test nvarchar variable +DECLARE @nv nvarchar(10); +SELECT @nv = '12.3420000000'; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO + +-- Test NULL varchar variable +DECLARE @v varchar(20); +SELECT @v = NULL; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO + +-- Test NULL nvarchar variable +DECLARE @nv nvarchar(10); +SELECT @nv = null; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO + +-- Test empty varchar variable +DECLARE @v varchar(20); +SELECT @v = ''; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO + +-- Test empty nvarchar variable +DECLARE @nv nvarchar(10); +SELECT @nv = ''; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO + +-- Test varchar with number argument that exceeds range of bigint. +DECLARE @v varchar(20); +SELECT @v = '9223372036854775807'; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO + +DECLARE @v varchar(20); +SELECT @v = '-9223372036854775808'; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO + +-- Test nvarchar with number argument that exceeds range of bigint. +DECLARE @nv nvarchar(20); +SELECT @nv = '9223372036854775807'; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO + +DECLARE @nv nvarchar(20); +SELECT @nv = '-9223372036854775808'; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO + +-- Test varchar with lengthy numeric value +DECLARE @v varchar; +SELECT @v = '12345678901234567890123456789012345'; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO + +-- Test nvarchar with lengthy numeric value +DECLARE @nv nvarchar; +SELECT @nv = '12345678901234567890123456789012345'; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO + +-- Test varchar variable with invalid numeric +DECLARE @v varchar(20); +SELECT @v = '12.34.20000000'; +SELECT ISNUMERIC(@v), LEN(@v), DATALENGTH(@v) +GO + +-- Test nvarchar variable with invalid numeric +DECLARE @nv nvarchar(10); +SELECT @nv = '12.34.20000000'; +SELECT ISNUMERIC(@nv), LEN(@nv), DATALENGTH(@nv) +GO \ No newline at end of file diff --git a/test/JDBC/upgrade/latest/schedule b/test/JDBC/upgrade/latest/schedule index 552a7d98117..1c0825603e6 100644 --- a/test/JDBC/upgrade/latest/schedule +++ b/test/JDBC/upgrade/latest/schedule @@ -571,4 +571,5 @@ sys_dm_os_sys_info cast-varchar-to-time test_db_collation BABEL-5119 +BABEL-5129