babelfish_extensions icon indicating copy to clipboard operation
babelfish_extensions copied to clipboard

Fix CASE statement when branch expression is of String Datatype.

Open Yvinayak07 opened this issue 1 year ago • 0 comments

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: https://github.com/babelfish-for-postgresql/postgresql_modified_for_babelfish/pull/455

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

  • [x] 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.

Yvinayak07 avatar Oct 01 '24 08:10 Yvinayak07