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 behaviour of DATEDIFF() and DATEDIFF_BIG() functions for week and quarter dateparts #3357

Merged

Conversation

Anikait143
Copy link
Contributor

Description

1. Issues

  • DATEDIFF() and DATEDIFF_BIG() functions return incorrect results when using 'week' and 'quarter' as datepart unit.
  • DATEDIFF_BIG() function gives wrong error message in case of overflow.

2. Changes made to fix the issues

  • Fix calculation logic for considering a week if a Saturday-Sunday boundaries crossed.
  • Fix calculation logic for considering a quarter if a quarter boundary crossed.
    1. March-April
    2. June-July
    3. September-October
    4. December-January
  • Fix error message for DATEDIFF_BIG() function in case of overflow.
# Input
1> declare @runForDate datetime = '01/22/2012';
2> declare @evaluationDate datetime = DATEADD(DAY,-1,@runForDate);
3> select DATEDIFF(wk,7,@evaluationDate)
4> go
# previous output (wrong output)               
datediff   
-----------
       5846
# current output (correct output)
datediff   
-----------
       5845                                                                                                                                                                                                                                                      

(1 rows affected)
# previous output (wrong output)
1> select DATEDIFF(quarter, '1999-12-31', '2000-01-01') AS Quarters
2> go
Quarters   
-----------
          0  
# current output (correct output)
1> select DATEDIFF(quarter, '1999-12-31', '2000-01-01') AS Quarters
2> go
Quarters   
-----------
          1
  • Added Test Cases for these issues

Task: BABEL- 5461, BABEL-5503
Authored-by: Anikait Agrawal [email protected]
Signed-off-by: Anikait Agrawal [email protected]

Test Scenarios Covered

  • Use case based - YES

  • Boundary conditions - YES

  • Arbitrary inputs - YES

  • Negative test cases - YES

  • Minor version upgrade tests -

  • Major version upgrade tests -

  • Performance tests -

  • Tooling impact -

  • Memory tests -

  • Client tests -

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.

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 12629578069

Details

  • 43 of 43 (100.0%) changed or added relevant lines in 1 file are covered.
  • 3 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.007%) to 74.874%

Files with Coverage Reduction New Missed Lines %
contrib/babelfishpg_tds/src/backend/tds/tdsutils.c 1 73.66%
contrib/babelfishpg_tds/src/backend/tds/tdscomm.c 2 76.03%
Totals Coverage Status
Change from base Build 12596818602: 0.007%
Covered Lines: 46626
Relevant Lines: 62273

💛 - Coveralls

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 jsudrik merged commit 2639702 into babelfish-for-postgresql:BABEL_5_X_DEV Jan 7, 2025
45 checks passed
@Anikait143 Anikait143 deleted the babel-5461-5x branch January 7, 2025 05:21
jsudrik pushed a commit that referenced this pull request Jan 7, 2025
… quarter dateparts (#3364)

1. Issues
DATEDIFF() and DATEDIFF_BIG() functions return incorrect results when using 'week' and 'quarter' as datepart unit.
DATEDIFF_BIG() function gives wrong error message in case of overflow.
2. Changes made to fix the issues
Fix calculation logic for considering a week if a Saturday-Sunday boundaries crossed.
Fix calculation logic for considering a quarter if a quarter boundary crossed.
1. March-April
2. June-July
3. September-October
4. December-January
Fix error message for DATEDIFF_BIG() function in case of overflow.

cherry-picked: #3357
Task: BABEL- 5461, BABEL-5503
Authored-by: Anikait Agrawal [email protected]
Signed-off-by: Anikait Agrawal [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