Skip to content

Commit

Permalink
Fix Convert() to return correct date format with char and nchar (#2448)
Browse files Browse the repository at this point in the history
Earlier when we were converting from DATE/TIME types to CHAR or NCHAR type using CONVERT function the output
style was not getting into account as we have not handled the case for CHAR and NCHAR type in TsqlFunctionConvert
function correctly. This commit add changes to handle the case for CHAR and NCHAR correctly.

Task: BABEL-4641
Signed-off-by: Rohit Bhagat <[email protected]>
  • Loading branch information
rohit01010 authored Apr 12, 2024
1 parent ed88bd1 commit fdb469e
Show file tree
Hide file tree
Showing 48 changed files with 3,114 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ TsqlFunctionConvert(TypeName *typename, Node *arg, Node *style, bool try, int lo
else if (type_oid == typenameTypeId(NULL, makeTypeName("datetime")))
result = (Node *) makeFuncCall(TsqlSystemFuncName("babelfish_conv_helper_to_datetime"), args, COERCE_EXPLICIT_CALL, location);

else if ((strcmp(typename_string, "varchar") == 0) || (strcmp(typename_string, "nvarchar") == 0))
else if ((strcmp(typename_string, "varchar") == 0) || (strcmp(typename_string, "nvarchar") == 0) ||
(strcmp(typename_string, "bpchar") == 0) || (strcmp(typename_string, "nchar") == 0))
{
Node *helperFuncCall;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ create table sys_columns_dep_vu_prepare_t1(a int, b float, c bigint, d numeric,
go

create procedure sys_columns_dep_vu_prepare_p1 as
select name, column_id, precision, scale from sys.columns where object_id=OBJECT_ID('sys_columns_dep_vu_prepare_t1');
select name, column_id, precision, scale from sys.columns where object_id=OBJECT_ID('sys_columns_dep_vu_prepare_t1') order by name;
go

create view sys_columns_dep_vu_prepare_v1 as
select name, column_id, precision, scale from sys.columns where object_id=OBJECT_ID('sys_columns_dep_vu_prepare_t1');
select name, column_id, precision, scale from sys.columns where object_id=OBJECT_ID('sys_columns_dep_vu_prepare_t1') order by name;
go

create function sys_columns_dep_vu_prepare_f1()
Expand Down
2 changes: 1 addition & 1 deletion test/JDBC/expected/BABEL-1994-CHAR-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ select
go
~~START~~
char#!#char#!#char#!#char#!#char
2021-08-15 00:00:00 #!#2021-08-15 00:00:00 #!#2021-08-15 00:00:00 #!#decimal #!#a
Aug 15 2021 12:00AM #!#2021-08-15 00:00:00 #!#2021-08-15 00:00:00 #!#decimal #!#a
~~END~~


Expand Down
2 changes: 1 addition & 1 deletion test/JDBC/expected/BABEL-1994-CHAR.out
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ select
go
~~START~~
char#!#char#!#char#!#char#!#char
2021-08-15 00:00:00 #!#2021-08-15 00:00:00 #!#2021-08-15 00:00:00 #!#decimal #!#a
Aug 15 2021 12:00AM #!#2021-08-15 00:00:00 #!#2021-08-15 00:00:00 #!#decimal #!#a
~~END~~


Expand Down
12 changes: 8 additions & 4 deletions test/JDBC/expected/BABEL-2998.out
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,48 @@ GO
SELECT CAST('1' AS CHAR(10)) AS Col1
UNION
SELECT CAST(NULL AS CHAR(10)) AS Col1
ORDER BY Col1
GO
~~START~~
char
1
<NULL>
1
~~END~~


SELECT CAST('1' AS CHAR(10)) AS Col1
UNION ALL
SELECT CONVERT(CHAR(10), NULL) AS Col1
ORDER BY Col1
GO
~~START~~
char
1
<NULL>
1
~~END~~


SELECT CAST('1' AS CHAR(10)) AS Col1
UNION ALL
SELECT CAST(NULL AS CHAR(10)) AS Col1
ORDER BY Col1
GO
~~START~~
char
1
<NULL>
1
~~END~~


SELECT CAST('1' AS CHAR(10)) AS Col1
UNION
SELECT CONVERT(CHAR(10), NULL) AS Col1
ORDER BY Col1
GO
~~START~~
char
1
<NULL>
1
~~END~~


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DROP TABLE BABEL_3147_before_16_3_or_15_7_vu_prepare_t_1
DROP TABLE BABEL_3147_before_16_3_or_15_7_vu_prepare_t_2
DROP TABLE BABEL_3147_before_16_3_or_15_7_vu_prepare_t_3
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CREATE TABLE BABEL_3147_before_16_3_or_15_7_vu_prepare_t_1 (
c [int] NULL,
c_comp AS ISNULL(CONVERT(CHAR(1), 'A'), 'B')
)
GO

CREATE TABLE BABEL_3147_before_16_3_or_15_7_vu_prepare_t_2 (
a numeric(6, 4),
b numeric(6, 3)
)
GO

INSERT INTO BABEL_3147_before_16_3_or_15_7_vu_prepare_t_2 VALUES (10.1234, 10.123);
INSERT INTO BABEL_3147_before_16_3_or_15_7_vu_prepare_t_2 VALUES (NULL, 101.123);
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~


CREATE TABLE BABEL_3147_before_16_3_or_15_7_vu_prepare_t_3 (
a decimal(6, 4),
b decimal(6, 3)
)
GO

INSERT INTO BABEL_3147_before_16_3_or_15_7_vu_prepare_t_3 VALUES (10.1234, 10.123);
INSERT INTO BABEL_3147_before_16_3_or_15_7_vu_prepare_t_3 VALUES (NULL, 101.123);
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
INSERT INTO BABEL_3147_before_16_3_or_15_7_vu_prepare_t_1 VALUES(1)
GO
~~ROW COUNT: 1~~


SELECT c_comp FROM BABEL_3147_before_16_3_or_15_7_vu_prepare_t_1
GO
~~START~~
char
A
~~END~~


-- Test ISNULL with numeric columns
SELECT ISNULL(a, b) FROM BABEL_3147_before_16_3_or_15_7_vu_prepare_t_2
GO
~~START~~
numeric
10.1234
101.1230
~~END~~


-- Test ISNULL with decimal columns
SELECT ISNULL(a, b) FROM BABEL_3147_before_16_3_or_15_7_vu_prepare_t_3
GO
~~START~~
numeric
10.1234
101.1230
~~END~~

5 changes: 5 additions & 0 deletions test/JDBC/expected/BABEL-3147-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
-- error should not have been thrown, should be fixed under BABEL-1710
DROP TABLE BABEL_3147_vu_prepare_t_1
DROP TABLE BABEL_3147_vu_prepare_t_2
DROP TABLE BABEL_3147_vu_prepare_t_3
GO
~~ERROR (Code: 3701)~~

~~ERROR (Message: table "babel_3147_vu_prepare_t_1" does not exist)~~

5 changes: 5 additions & 0 deletions test/JDBC/expected/BABEL-3147-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
-- error should not have been thrown, should be fixed under BABEL-1710
CREATE TABLE BABEL_3147_vu_prepare_t_1 (
c [int] NULL,
c_comp AS ISNULL(CONVERT(CHAR(1), 'A'), 'B')
)
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: generation expression is not immutable)~~


CREATE TABLE BABEL_3147_vu_prepare_t_2 (
a numeric(6, 4),
Expand Down
14 changes: 11 additions & 3 deletions test/JDBC/expected/BABEL-3147-vu-verify.out
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
INSERT INTO BABEL_3147_before_14_5_vu_prepare_t_1 VALUES(1)
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: relation "babel_3147_before_14_5_vu_prepare_t_1" does not exist)~~


-- error should not have been thrown, should be fixed under BABEL-1710
SELECT c_comp FROM BABEL_3147_vu_prepare_t_1
GO
~~START~~
char
~~END~~
~~ERROR (Code: 33557097)~~

~~ERROR (Message: relation "babel_3147_vu_prepare_t_1" does not exist)~~


-- Test ISNULL with numeric columns
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
-- CHAR
DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view1
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view11
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view12
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view2
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view22
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view3
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view31
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view32
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_char_vu_prepare_proc1
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_char_vu_prepare_proc11
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_char_vu_prepare_proc12
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_char_vu_prepare_proc2
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_char_vu_prepare_proc22
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_char_vu_prepare_proc3
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_char_vu_prepare_proc31
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_char_vu_prepare_proc32
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_char_vu_prepare_func1
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_char_vu_prepare_func11
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_char_vu_prepare_func12
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_char_vu_prepare_func2
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_char_vu_prepare_func22
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_char_vu_prepare_func3
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_char_vu_prepare_func31
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_char_vu_prepare_func32
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view4
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view5
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view6
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view7
GO

DROP VIEW babel_4461_before_16_3_or_15_7_char_vu_prepare_view8
GO

-- NCHAR
DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view1
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view11
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view12
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view2
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view22
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view3
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view31
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view32
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_nchar_vu_prepare_proc1
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_nchar_vu_prepare_proc11
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_nchar_vu_prepare_proc12
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_nchar_vu_prepare_proc2
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_nchar_vu_prepare_proc22
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_nchar_vu_prepare_proc3
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_nchar_vu_prepare_proc31
GO

DROP PROCEDURE babel_4461_before_16_3_or_15_7_nchar_vu_prepare_proc32
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_nchar_vu_prepare_func1
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_nchar_vu_prepare_func11
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_nchar_vu_prepare_func12
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_nchar_vu_prepare_func2
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_nchar_vu_prepare_func22
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_nchar_vu_prepare_func3
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_nchar_vu_prepare_func31
GO

DROP FUNCTION babel_4461_before_16_3_or_15_7_nchar_vu_prepare_func32
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view4
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view5
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view6
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view7
GO

DROP VIEW babel_4461_before_16_3_or_15_7_nchar_vu_prepare_view8
GO
Loading

0 comments on commit fdb469e

Please sign in to comment.