Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with convert function when converting string to date, datetimeoffset, datetime2, datetime, smalldatetime or time #3336

Merged
2,291 changes: 1,623 additions & 668 deletions contrib/babelfishpg_tsql/sql/sys_function_helpers.sql

Large diffs are not rendered by default.

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 @@ -4713,8 +4713,8 @@ BEGIN
ELSE
-- Round datetime to fixed bins (e.g. .000, .003, .007)
IF date_arg_datatype = 'sys.datetime'::regtype THEN
date := sys.babelfish_conv_string_to_datetime('DATETIME', date::TEXT)::sys.datetime;
origin := sys.babelfish_conv_string_to_datetime('DATETIME', origin::TEXT)::sys.datetime;
date := sys.babelfish_conv_string_to_datetime_v2('DATETIME', date::TEXT)::sys.datetime;
origin := sys.babelfish_conv_string_to_datetime_v2('DATETIME', origin::TEXT)::sys.datetime;
END IF;
-- when datepart is {year, quarter, month} make use of AGE() function to find number of buckets
IF datepart IN ('year', 'quarter', 'month') THEN
Expand Down
5,288 changes: 5,288 additions & 0 deletions contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.0.0--5.1.0.sql

Large diffs are not rendered by default.

57 changes: 53 additions & 4 deletions contrib/babelfishpg_tsql/src/backend_parser/gram-tsql-epilogue.y.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,60 @@ TsqlFunctionConvert(TypeName *typename, Node *arg, Node *style, bool try, int lo
result = (Node *) makeFuncCall(TsqlSystemFuncName("babelfish_conv_helper_to_date"), args, COERCE_EXPLICIT_CALL, location);

else if (type_oid == TIMEOID)
result = (Node *) makeFuncCall(TsqlSystemFuncName("babelfish_conv_helper_to_time"), args, COERCE_EXPLICIT_CALL, location);

{
Node *helperFuncCall;
helperFuncCall = (Node *) makeFuncCall(TsqlSystemFuncName("babelfish_conv_helper_to_time"), lcons(makeIntConst(typmod, location), args), COERCE_EXPLICIT_CALL, location);

/*
* add a type cast on top of the CONVERT helper function
* so typmod can be applied
*/
result = makeTypeCast(helperFuncCall, typename, location);
}
else if (type_oid == typenameTypeId(NULL, makeTypeName("datetime")))
result = (Node *) makeFuncCall(TsqlSystemFuncName("babelfish_conv_helper_to_datetime"), args, COERCE_EXPLICIT_CALL, location);

{
Node *helperFuncCall;
helperFuncCall = (Node *) makeFuncCall(TsqlSystemFuncName("babelfish_conv_helper_to_datetime"), lcons(makeIntConst(typmod, location), args), COERCE_EXPLICIT_CALL, location);

/*
* add a type cast on top of the CONVERT helper function
* so typmod can be applied
*/
result = makeTypeCast(helperFuncCall, typename, location);
}
else if (type_oid == typenameTypeId(NULL, makeTypeName("datetime2")))
{
Node *helperFuncCall;
helperFuncCall = (Node *) makeFuncCall(TsqlSystemFuncName("babelfish_conv_helper_to_datetime2"), lcons(makeIntConst(typmod, location), args), COERCE_EXPLICIT_CALL, location);

/*
* add a type cast on top of the CONVERT helper function
* so typmod can be applied
*/
result = makeTypeCast(helperFuncCall, typename, location);
}
else if (type_oid == typenameTypeId(NULL, makeTypeName("datetimeoffset")))
{
Node *helperFuncCall;
helperFuncCall = (Node *) makeFuncCall(TsqlSystemFuncName("babelfish_conv_helper_to_datetimeoffset"), lcons(makeIntConst(typmod, location), args), COERCE_EXPLICIT_CALL, location);

/*
* add a type cast on top of the CONVERT helper function
* so typmod can be applied
*/
result = makeTypeCast(helperFuncCall, typename, location);
}
else if (type_oid == typenameTypeId(NULL, makeTypeName("smalldatetime")))
{
Node *helperFuncCall;
helperFuncCall = (Node *) makeFuncCall(TsqlSystemFuncName("babelfish_conv_helper_to_smalldatetime"), lcons(makeIntConst(typmod, location), args), COERCE_EXPLICIT_CALL, location);

/*
* add a type cast on top of the CONVERT helper function
* so typmod can be applied
*/
result = makeTypeCast(helperFuncCall, typename, location);
}
else if ((strcmp(typename_string, "varchar") == 0) || (strcmp(typename_string, "nvarchar") == 0) ||
(strcmp(typename_string, "bpchar") == 0) || (strcmp(typename_string, "nchar") == 0))
{
Expand Down
Loading