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

support DATETRUNC() function #1846

Conversation

skumawat2025
Copy link
Contributor

@skumawat2025 skumawat2025 commented Sep 18, 2023

Description

With this pull request babelfish now supports function DATETRUNC().

The DATETRUNC function returns an input date truncated to a specified datepart.

For the implementation of the 'DATETRUNC' function, the PostgreSQL 'date_trunc' function was utilized with certain custom modifications to meet specific requirements.

Example -

SELECT DATETRUNC(year, CAST('2021-12-08 11:30:15.1234567' as datetime2))
2021-01-01 00:00:00.0000000

SELECT DATETRUNC(month, '1998-03-04');
1998-03-01 00:00:00.0000000

For detailed explanation -
Reference - https://github.com/MicrosoftDocs/sql-docs/blob/live/docs/t-sql/functions/datetrunc-transact-sql.md

Limitation

There are some limitation of current implementation of DATETRUNC() function, when a specific scale provided during casting to time, datetime2, datetimeoffset datatypes.

  1. The output should match the fractional time scale same as the input data type. But currently BBF output will always be till 7 fractional digits not matter what is input data type fractional time-scale.
Example.
SELECT DATETRUNC(MILLISECOND, CAST('09:23:32.23456' as time(4)));
EXPECTED:- 09:23:32.2340
BBF:- 09:23:32.2340000

SELECT DATETRUNC(Month, CAST('2012-02-02 09:23:32.23456' as datetime2(1)));
EXPECTED:- 2012-02-01 00:00:00.0
BBF:- 2012-02-01 00:00:00.0000000
  1. When the fractional time-scale of input data type is not sufficient for provided datepart, function DATETRUNC should throw an error message, but currently BBF's definition of DATETRUNC will give output by assuming required fractional seconds digits as 0.
Example.
SELECT DATETRUNC(MILLISECOND, CAST('2012-02-02 09:23:32.23456' as datetime2(2)));
EXPECTED:- The datepart millisecond is not supported by date function datetrunc for data type datetime2
BBF:-2012-02-02 09:23:32.2300000

SELECT DATETRUNC(MILLISECOND, CAST('2012-02-02 09:23:32.23456 +02:12' as datetimeoffset(2)));
EXPECTED:- The datepart millisecond is not supported by date function datetrunc for data type datetimeoffset
BBF:- 2012-02-02 09:23:32.2300000 +02:12

Signed-off-by: Sandeep Kumawat [email protected]

Issues Resolved

BABEL-3953

Test Scenarios Covered

  • Use case based - Added

  • Boundary conditions - Added

  • Arbitrary inputs - Added

  • Negative test cases - Added

  • Minor version upgrade tests- Added

  • Major version upgrade tests - Added

  • Performance tests - NA

  • Tooling impact - NA

  • Client tests - NA

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.

Copy link
Contributor

@hash-16 hash-16 left a comment

Choose a reason for hiding this comment

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

You can add limitation of this function in PR description with some examples.

@skumawat2025 skumawat2025 requested a review from hash-16 October 3, 2023 14:55
Copy link
Contributor

@hash-16 hash-16 left a comment

Choose a reason for hiding this comment

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

Please resolve the merge conflicts.

Copy link
Contributor

@hash-16 hash-16 left a comment

Choose a reason for hiding this comment

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

Lets add all testcases from the doc and verify it against standard TSQL.

Copy link
Contributor

@jsudrik jsudrik left a comment

Choose a reason for hiding this comment

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

Approved.

@jsudrik
Copy link
Contributor

jsudrik commented Oct 16, 2023

With this change, babelfish now supports function DATETRUNC().

The DATETRUNC function returns an input date truncated to a specified datepart.

@jsudrik jsudrik merged commit f40bde1 into babelfish-for-postgresql:BABEL_3_X_DEV Oct 16, 2023
27 checks passed
ParikshitSarode pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Oct 17, 2023
* support DATETRUNC() function

* test failures

* code refectoring and some jdbc tests

* test failure

* Revert "test failure"

This reverts commit bfcac2e.

* address comments

* address review comments
Signed-off-by: Sandeep Kumawat <[email protected]>

---------

Co-authored-by: Sandeep Kumawat <[email protected]>
ahmed-shameem pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Oct 25, 2023
* support DATETRUNC() function

* test failures

* code refectoring and some jdbc tests

* test failure

* Revert "test failure"

This reverts commit bfcac2e.

* address comments

* address review comments
Signed-off-by: Sandeep Kumawat <[email protected]>

---------

Co-authored-by: Sandeep Kumawat <[email protected]>
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