Skip to content

Commit

Permalink
Uninitialized variable fix for ISNUMERIC function with varchar and nv…
Browse files Browse the repository at this point in the history
…archar arguments (#3001)

Uninitialized variable fix for ISNUMERIC function with varchar and nvarchar arguments (#2945)
* Fix uninitialised variable issue related to varchar2numeric function
* Test cases for isnumeric function with varchar and nvarchar variables
* removing unintended merged commits
Fixing pr comments, added test name to jdbc latest schedule
* Added null test case
* Added boundary test cases
Removed unintended changes in jdbc latest schedule from conflict resolution

Task: BABEL-5129

Signed-off-by: manisha-deshpande <[email protected]>
Co-authored-by: “manisha-deshpande” <“[email protected]">
  • Loading branch information
manisha-deshpande and “manisha-deshpande” authored Oct 2, 2024
1 parent 1b0a48a commit 58dd341
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 1 deletion.
5 changes: 4 additions & 1 deletion contrib/babelfishpg_common/src/varchar.c
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,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);
}
Expand Down
2 changes: 2 additions & 0 deletions test/JDBC/expected/BABEL-5129-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE babel_5129
GO
28 changes: 28 additions & 0 deletions test/JDBC/expected/BABEL-5129-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -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~~

217 changes: 217 additions & 0 deletions test/JDBC/expected/BABEL-5129-vu-verify.out
Original file line number Diff line number Diff line change
@@ -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#!#<NULL>#!#<NULL>
~~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#!#<NULL>#!#<NULL>
~~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~~

2 changes: 2 additions & 0 deletions test/JDBC/input/BABEL-5129-vu-cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE babel_5129
GO
26 changes: 26 additions & 0 deletions test/JDBC/input/BABEL-5129-vu-prepare.sql
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 58dd341

Please sign in to comment.