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 CASE statement when branch expression is of String Datatype. #2931

Merged

Conversation

Yvinayak07
Copy link
Contributor

@Yvinayak07 Yvinayak07 commented Sep 13, 2024

1. Issue:

Return type of overall CASE expression when branch expression is of String Datatype, is evaluated as per Postgres behaviour, which gives a different Return type as compared to TSQL.

eg. Return type of overall CASE expression is evaluated to become VARCHAR, if all branches are of NVARCHAR type because Postgres tries to coerce result of every branch expression to base type (VARCHAR), whereas in TSQL, overall return type is NVARCHAR in this example.

CREATE TABLE ALLTEXTS(
CODE nvarchar(30) NOT NULL,
LANG nvarchar(2) NOT NULL,
TEXT nvarchar(2000) NULL
)

INSERT INTO ALLTEXTS (CODE, LANG, TEXT)
VALUES ('53075', 'EN', N'Registration Card - Standard');

INSERT INTO ALLTEXTS (CODE, LANG, TEXT)
VALUES ('53075', 'AR', N'بطاقة التسجيل - قياسية');

INSERT INTO ALLTEXTS (CODE, LANG, TEXT)
VALUES ('53075', 'JP', N'登録カード–標準');

SELECT CASE WHEN 1 = 1 THEN TEXT END AS WrongValue2 FROM ALLTEXTS

Return type: varchar
expected type: nvarchar

2. Changes made to fix the issues

tsql_select_common_type_hook() is set to return common_type for all branches when branch expression is of String Datatype, that calculate according to the TSQL precedence, whereas till now it was set according to the PG precedence order (Which was different from TSQL).This hook solved our problem as it returns result according to the TSQL precedence order. We have also fixed the typmod issue, by calculating the common typmod and setting all branches of CASE statement to the calculated typmod.

Task: BABEL-5103, BABEL-4332
Authored-by: yashneet vinayak [email protected]
Signed-off-by: yashneet vinayak [email protected]

Issues Resolved

Task: BABEL-5103, BABEL-4332

Engine Side PR link: babelfish-for-postgresql/postgresql_modified_for_babelfish#452

Test Scenarios Covered

  • **Use case based -**Yes

  • Boundary conditions -

  • **Arbitrary inputs -**Yes

  • Negative test cases -

  • **Minor version upgrade tests -**Yes

  • **Major version upgrade tests -**Yes

  • Performance tests -

  • Tooling impact -

  • **Client tests -**Yes

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.

yashneet vinayak added 3 commits September 16, 2024 09:55
Signed-off-by: yashneet vinayak <[email protected]>
Signed-off-by: yashneet vinayak <[email protected]>
@coveralls
Copy link
Collaborator

coveralls commented Sep 16, 2024

Pull Request Test Coverage Report for Build 11109698756

Details

  • 36 of 36 (100.0%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.004%) to 74.474%

Files with Coverage Reduction New Missed Lines %
contrib/babelfishpg_tsql/src/pltsql_coerce.c 1 81.62%
contrib/babelfishpg_tds/src/backend/tds/tdsutils.c 1 73.8%
Totals Coverage Status
Change from base Build 11109596996: 0.004%
Covered Lines: 45121
Relevant Lines: 60586

💛 - Coveralls

@Yvinayak07 Yvinayak07 changed the title Setting the correct common_type of CASE statement. Fix CASE statement when branch expression is of NVARCHAR type. Sep 18, 2024
yashneet vinayak added 2 commits September 18, 2024 09:54
yashneet vinayak added 2 commits September 23, 2024 08:33
yashneet vinayak added 2 commits September 27, 2024 13:50
Signed-off-by: yashneet vinayak <[email protected]>
Signed-off-by: yashneet vinayak <[email protected]>
Copy link
Contributor

@rohit01010 rohit01010 left a comment

Choose a reason for hiding this comment

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

LGTM

Deepesh125
Deepesh125 previously approved these changes Sep 30, 2024
@Yvinayak07 Yvinayak07 changed the title Fix CASE statement when branch expression is of NVARCHAR type. Fix CASE statement when branch expression is of Sting Datatype. Sep 30, 2024
@Yvinayak07 Yvinayak07 changed the title Fix CASE statement when branch expression is of Sting Datatype. Fix CASE statement when branch expression is of String Datatype. Sep 30, 2024
Signed-off-by: yashneet vinayak <[email protected]>
Signed-off-by: yashneet vinayak <[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.

7 participants