Skip to content

Commit

Permalink
tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Parikshit Sarode committed Jan 2, 2024
1 parent c589883 commit cb24877
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 55 deletions.
52 changes: 0 additions & 52 deletions contrib/babelfishpg_tsql/runtime/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ PG_FUNCTION_INFO_V1(getdate_internal);
PG_FUNCTION_INFO_V1(sysdatetime);
PG_FUNCTION_INFO_V1(sysdatetimeoffset);
PG_FUNCTION_INFO_V1(datepart_internal_int);
PG_FUNCTION_INFO_V1(datepart_internal_smallint);
PG_FUNCTION_INFO_V1(datepart_internal_tinyint);
PG_FUNCTION_INFO_V1(datepart_internal_date);
PG_FUNCTION_INFO_V1(datepart_internal_datetime);
PG_FUNCTION_INFO_V1(datepart_internal_datetimeoffset);
Expand Down Expand Up @@ -601,56 +599,6 @@ datepart_internal_int(PG_FUNCTION_ARGS)

}

/*
* datepart_internal_smallint takes int and converts it to
* timestamp and calls datepart_internal and checks limits
*/

Datum
datepart_internal_smallint(PG_FUNCTION_ARGS)
{
char *field = text_to_cstring(PG_GETARG_TEXT_PP(0));
int num = PG_GETARG_INT16(1);

if(num > SHRT_MAX || num < SHRT_MIN)
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("Arithmetic overflow error for data type smallint, value = %d",num)));
}

/*
* Setting the timestamp in datepart_internal as 0 and passing num in third argument
* as there is no need of df_tz
*/
return datepart_internal(field, 0, num, true);
}

/*
* datepart_internal_tinyint takes int and converts it to
* timestamp and calls datepart_internal and checks limits
*/

Datum
datepart_internal_tinyint(PG_FUNCTION_ARGS)
{
char *field = text_to_cstring(PG_GETARG_TEXT_PP(0));
int num = PG_GETARG_INT16(1);

if(num > UCHAR_MAX || num < 0)
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("Arithmetic overflow error for data type tinyint, value = %d",num)));
}

/*
* Setting the timestamp in datepart_internal as 0 and passing num in third argument
* as there is no need of df_tz
*/
return datepart_internal(field, 0, num, true);
}

/*
* datepart_internal_money takes money and converts it to
* timestamp and calls datepart_internal
Expand Down
4 changes: 2 additions & 2 deletions contrib/babelfishpg_tsql/sql/sys_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.datepart_internal(field text, datapart_date sys.TINYINT ,df_tz INTEGER DEFAULT 0)
RETURNS INTEGER
AS 'babelfishpg_tsql', 'datepart_internal_tinyint'
AS 'babelfishpg_tsql', 'datepart_internal_int'
LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.datepart_internal(field text, datapart_date SMALLINT ,df_tz INTEGER DEFAULT 0)
RETURNS INTEGER
AS 'babelfishpg_tsql', 'datepart_internal_smallint'
AS 'babelfishpg_tsql', 'datepart_internal_int'
LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.datepart_internal(field text, datapart_date sys.MONEY ,df_tz INTEGER DEFAULT 0)
Expand Down
125 changes: 125 additions & 0 deletions test/JDBC/expected/BABEL_4302.out
Original file line number Diff line number Diff line change
Expand Up @@ -6177,3 +6177,128 @@ int
<NULL>
~~END~~



-- datepart without cast
SELECT DATEPART(day, 2147483647);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(qq, -2147483648);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(month, 9223372036854775807);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(year, -9223372036854775808);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(yy, 214748.3647);
GO
~~START~~
int
2487
~~END~~


SELECT DATEPART(day, -214748.3648);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(yy, 922337203685477.5807);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(yyyy, -922337203685477.5808);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(day, 1.79E+308);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(hour, -1.79E+308);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(second, 3.40E+38);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(minute, -3.40E+38);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(day, 0);
GO
~~START~~
int
1
~~END~~


SELECT DATEPART(month, 2958463);
GO
~~START~~
int
12
~~END~~


SELECT DATEPART(hour, 2958464);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~


SELECT DATEPART(year, -53690);
GO
~~START~~
int
1753
~~END~~


SELECT DATEPART(microsecond, -53691);
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Arithmetic overflow error converting expression to data type datetime.)~~

55 changes: 54 additions & 1 deletion test/JDBC/input/BABEL_4302.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2443,4 +2443,57 @@ SELECT DATEPART(nanosecond, NULL);
GO

SELECT DATEPART(tzoffset, NULL);
GO
GO

-- datepart without cast

SELECT DATEPART(day, 2147483647);
GO

SELECT DATEPART(qq, -2147483648);
GO

SELECT DATEPART(month, 9223372036854775807);
GO

SELECT DATEPART(year, -9223372036854775808);
GO

SELECT DATEPART(yy, 214748.3647);
GO

SELECT DATEPART(day, -214748.3648);
GO

SELECT DATEPART(yy, 922337203685477.5807);
GO

SELECT DATEPART(yyyy, -922337203685477.5808);
GO

SELECT DATEPART(day, 1.79E+308);
GO

SELECT DATEPART(hour, -1.79E+308);
GO

SELECT DATEPART(second, 3.40E+38);
GO

SELECT DATEPART(minute, -3.40E+38);
GO

SELECT DATEPART(day, 0);
GO

SELECT DATEPART(month, 2958463);
GO

SELECT DATEPART(hour, 2958464);
GO

SELECT DATEPART(year, -53690);
GO

SELECT DATEPART(microsecond, -53691);
GO

0 comments on commit cb24877

Please sign in to comment.