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

datepart function reimplementation #1959

Merged

Conversation

ParikshitSarode
Copy link
Contributor

@ParikshitSarode ParikshitSarode commented Oct 26, 2023

Description - Low performance with function sys. datepart_internal -- re-implement in C

  1. sys. datepart_internal function takes longer time in babelfish then expected
  2. The function is reimplemented in C for faster execution.
  3. Different wrapper functions are created according to the data type of the second argument in sys. datepart_internal.
  4. Additional tests are added covering all cases since previously incorrect query outputs were written in tests and logical errors were present in code

Queries with incorrect output that were present in the JDBC tests files

In datepart, some queries in tests were giving incorrect output and included in the tests file. For example,

Example 1

SELECT DATEPART(ms,'2016-12-26 23:30:05.523456');
GO
~~START~~       
int
456
~~END~~

The correct output should have been 523 which is verified and fixed in this code. This incorrect test was present in test/JDBC/expected/babel_function.out among others

Example 2

SELECT DATEPART(ss, '12-31-9999 23:59:59.999999 +14:00')
GO
~~START~~
int
0
~~END~~

The expected result should be 59

Example 3 -

SELECT DATEPART(hh, 'Jul 18, 22 01:01:00.1234567 -7:8')
GO
~~START~~
int
8
~~END~~

Correct output should be 1 but it was written as 8 in test/JDBC/expected/datepart-vu-verify.out

When verified, the output of this query should be 1

Such outputs are corrected in the [BABEL-4302] - Low performance with function sys. datepart_internal -- re-implement in C. All queries are giving correct results as expected.

The file test/JDBC/expected/babel_function.out has the following queries that should throw an error but some output is given

datepart queries should throw an error message for the following queries instead some output is given. These queries are present in the babel_functions.out in test/JDBC/expected/babel_function.out

Example 1-
BBF results

select datepart(month, cast('12:10:30.123' as time));
GO
~START~
int
1
~END~

Expected results

select datepart(month, cast('12:10:30.123' as time));
go
Msg 9810, Level 16, State 3, Server EC2AMAZ-JA3CRFV, Line 1
The datepart month is not supported by date function datepart for data type time.

When the first parameter in datepart is date, week, month, quarter, year, such error is expected.

Example 2-
BBF results

select datepart(q, CAST('2016-12-26 23:30:05.523456+8'AS datetimeoffset));
GO
~~START~~
int
4
~~END~~

Expected results

select datepart(q, CAST('2016-12-26 23:30:05.523456+8'AS datetimeoffset));
go
Msg 241, Level 16, State 1, Server EC2AMAZ-JA3CRFV, Line 1
Conversion failed when converting date and/or time from character string.

Irrespective of the first parameter in datepart, such error is expected.
These are some few examples of incorrect behaviour

Signed-off-by: Parikshit Sarode [email protected]

Issues Resolved

Task: BABEL-4302,BABEL-979, BABEL-4366 issues related to datepart, BABEL-4582 issues related to datepart and not cast

Check List

  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@ParikshitSarode ParikshitSarode removed the request for review from forestkeeper November 9, 2023 07:47
Comment on lines 74 to 75
#define DATEPART_MAX_VALUE 2958463
#define DATEPART_MIN_VALUE -53690
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does these two number represent? add simple comment just like above

Comment on lines 41 to 43
ALTER FUNCTION sys.datepart_internal RENAME TO datepart_internal_deprecated_3_5;

CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'datepart_internal_deprecated_3_5');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have defensive code, rename only if it exists with given argument types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can take help from Rohit

Datum
datepart_internal_datetimeoffset(PG_FUNCTION_ARGS)
{
char *field = text_to_cstring(PG_GETARG_TEXT_PP(0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can directly use PG_GETARG_CSTRING, applicable to everywhere else also

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not yet resolved

Comment on lines 563 to 565
DateADT date_arg;
date_arg = PG_GETARG_DATEADT(1);
timestamp = DirectFunctionCall1(date_timestamp, date_arg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DirectFunctionCall1 expects datum argument so we can directly pass PG_GETARG(1) to DirectFunctionCall1

Comment on lines +379 to +387
if (timestamp == 0 && general_integer_datatype)
{
/* Checking for the limits for general_integer_datatype */
if(df_tz > DATEPART_MAX_VALUE || df_tz < DATEPART_MIN_VALUE)
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("Arithmetic overflow error converting expression to data type datetime.")));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test cases which will hit this error

Datum
datepart_internal_datetimeoffset(PG_FUNCTION_ARGS)
{
char *field = text_to_cstring(PG_GETARG_TEXT_PP(0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not yet resolved


ALTER FUNCTION sys.datepart_internal(PG_CATALOG.TEXT, anyelement, INTEGER) RENAME TO datepart_internal_deprecated_3_5;

CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'datepart_internal_deprecated_3_5');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be done at the end of script

SELECT DATEPART(day, CAST(-53690 AS float));
GO

SELECT DATEPART(day, CAST(-53691 AS float));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we still need to have test cases which covers min/max, NULL for all the data types and also with random field argument

Comment on lines +5878 to +5884
SELECT DATEPART(second, CAST(256 AS tinyint));
GO
~~START~~
int
~~ERROR (Code: 220)~~

~~ERROR (Message: value for domain tinyint violates check constraint "tinyint_check")~~
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same T-SQL behaviour? I dont think so. Seems like some bug with casting 256 to TINYINT. Please confirm and file a bug if needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like every domain data type has this problem, for example, smallmoney

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a issue of cast function. For the datatypes, the overflow error message is thrown from the cast function before hitting the datepart functions. Will fiile a bug

Parikshit Sarode added 2 commits January 2, 2024 07:34
@KushaalShroff KushaalShroff changed the title WIP - datepart_internal reimplemented datepart function reimplementation Jan 2, 2024
@jsudrik jsudrik merged commit 5c67087 into babelfish-for-postgresql:BABEL_3_X_DEV Jan 2, 2024
30 checks passed
ParikshitSarode added a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 2, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
rishabhtanwar29 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 8, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
rishabhtanwar29 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 8, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
rishabhtanwar29 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 8, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
rishabhtanwar29 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 8, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
rishabhtanwar29 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 8, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
rishabhtanwar29 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 8, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
rishabhtanwar29 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 8, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
rishabhtanwar29 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 8, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
rishabhtanwar29 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 8, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
shardgupta pushed a commit that referenced this pull request Jan 9, 2024
PR implements fix for low performance with function sys. datepart_internal by re-implement in C

Signed-off-by: Parikshit Sarode <[email protected]>
Co-authored-by: Parikshit Sarode <[email protected]>
@Anikait143 Anikait143 deleted the Jira-4302 branch February 27, 2024 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants