Skip to content

Fix behavioural differences due to return type and handle constant string literal inputs differently for T-SQL COALESCE function #349

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

Conversation

basasairohan
Copy link
Contributor

Description

Behavioural differences mentioned below have been found out better PG Coalesce and T-SQL Coalesce function.

  1. Consistency in return datatype i.e., for the same set of input datatypes T-SQL should return consistent return datatype as shown in below example
-- T-SQL
select coalesce(cast(8 as smallint), cast(1 as bit))
go
~~START~~
smallint
8
~~END~~
select coalesce(cast(1 as bit), cast(8 as smallint))
go
~~START~~
smallint
1
~~END~~

-- BBF

select coalesce(cast(8 as smallint), cast(1 as bit))
go
~~START~~
smallint
8
~~END~~
select coalesce(cast(1 as bit), cast(8 as smallint))
go
~~START~~
bit
1
~~END~~
  1. When the input is constant string literal i.e., T-SQL should take short circuit evaluation meaning unnecessary arguments should not be evaluated
-- T-SQL
1> SELECT COALESCE(NULL, 1, 2, 'I am a string')
2> go
           
-----------
          1

(1 rows affected)

-- BBF
1> SELECT COALESCE(NULL, 1, 2, 'I am a string')
2> go
Msg 22, Level 16, State 1, Server BABEL, Line 1
invalid input syntax for integer: "I am a string"

This commit contains the following changes :

  1. Whenever the caller is T-SQL COALESCE, then call the select_common_type_hook with 'TSQL_COALESCE' context.
  2. If the input is a constant string literal, first we convert the UNKNOWN node to VARCHAR node and then coerce it to the common type. This change also handles empty or white space strings according to the way T-SQL handles.

Issues Resolved

[BABEL-726]

Extension PR : babelfish-for-postgresql/babelfish_extensions#2507

Signed-off-by: Sai Rohan Basa [email protected]

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 PostgreSQL license, 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.

…ring literal inputs differently for T-SQL COALESCE function (babelfish-for-postgresql#345)

This commit contains the following changes :

    Whenever the caller is T-SQL COALESCE, then call the select_common_type_hook with 'TSQL_COALESCE' context.
    If the input is a constant string literal, first we convert the UNKNOWN node to VARCHAR node and then coerce it to the common type. This change also handles empty or white space strings according to the way T-SQL handles.

Issues Resolved

[BABEL-726]

Extension PR : babelfish-for-postgresql/babelfish_extensions#2507

Signed-off-by: Sai Rohan Basa [email protected]
Copy link

@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 based on the other review.

@jsudrik jsudrik merged commit 9353138 into babelfish-for-postgresql:BABEL_3_X_DEV__PG_15_X Apr 25, 2024
staticlibs pushed a commit to wiltondb/postgresql_modified_for_babelfish that referenced this pull request Jun 23, 2024
…ring literal inputs differently for T-SQL COALESCE function (babelfish-for-postgresql#345) (babelfish-for-postgresql#349)

This commit contains the following changes :

    Whenever the caller is T-SQL COALESCE, then call the select_common_type_hook with 'TSQL_COALESCE' context.
    If the input is a constant string literal, first we convert the UNKNOWN node to VARCHAR node and then coerce it to the common type. This change also handles empty or white space strings according to the way T-SQL handles.

Issues Resolved

[BABEL-726]

Extension PR : babelfish-for-postgresql/babelfish_extensions#2507

Signed-off-by: Sai Rohan Basa [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.

3 participants