fix for char and nchar function correct handling#3855
fix for char and nchar function correct handling#3855shardgupta merged 24 commits intobabelfish-for-postgresql:BABEL_5_X_DEVfrom
Conversation
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Pull Request Test Coverage Report for Build 15966586600Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Anikait143
left a comment
There was a problem hiding this comment.
Please add following test case:
- Tests with schema.function_call
- Test with different collation settings
- Test for multibyte character input integer value.
- Test with edge case values
- Test with datatype inputs that can be implicitly casted to
int
| END IF; | ||
| END $$; | ||
|
|
||
| create or replace function sys.cht(x in int) returns sys.varchar |
There was a problem hiding this comment.
Old function should be deprecated since we have changed function signature
| END; | ||
| $body$ | ||
| language plpgsql STABLE; | ||
| LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE; |
There was a problem hiding this comment.
We should mark all the functions as STABLE since the underlying helper function depends on encoding settings
There was a problem hiding this comment.
with stable we are not able to use the CHAR function in computed column, where as in tsql the char can be used in computed column.
Since the encoding in babelfish_db can' t be changed once the db is formed, hence we can mark the function as immutable
| END; | ||
| $body$ | ||
| language plpgsql STABLE; | ||
| LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE; |
There was a problem hiding this comment.
Have we verified that it is really PARALLEL SAFE?
There was a problem hiding this comment.
Since the test case is passing in the JDBC parallel query mode, so we can say that it's parallel safe ?
There was a problem hiding this comment.
From the pg_proc catalog too, we have verified that chr() function is parallel safe and since we are using chr() underlying sys.char(), so we can say it's parallel safe
| ); | ||
|
|
||
| create or replace function sys.CHAR(x in int)returns char | ||
| create or replace function sys.cht(x in int) returns sys.varchar |
There was a problem hiding this comment.
Return type should be sys.bpchar
There was a problem hiding this comment.
IMO varchar might be a better return function since bpchar using fixed length whereas the output will be of length = 1 always, so it might allocate extra space in case of bpchar. Else anything can be used, not an issue.
| create or replace function sys.cht(x in int) returns sys.varchar | ||
| AS | ||
| $body$ | ||
| BEGIN |
There was a problem hiding this comment.
What if input is 0? (Same for other functions as well)
There was a problem hiding this comment.
yes, this edge case need to be handled since pg function 'chr' expects the input b/w 1-255 where as the tsql expects input b/w 0-255. Thanks
| if (proc->DOUBLE_QUOTE_ID()) | ||
| stream.setText(startIndex, "\"cht\" "); | ||
| else if (proc->SQUARE_BRACKET_ID()) | ||
| stream.setText(startIndex, "[cht] "); | ||
| else | ||
| stream.setText(startIndex, " cht"); |
There was a problem hiding this comment.
Would there be any case where DOUBLE_QUOTE_ID and SQUARE_BRACKET_ID are valid for function calls?
There was a problem hiding this comment.
Yes we do support the query like :
select [char](255)
adding tests for it
| else if (proc->SQUARE_BRACKET_ID()) | ||
| stream.setText(startIndex, "[cht] "); | ||
| else | ||
| stream.setText(startIndex, " cht"); |
There was a problem hiding this comment.
It's better to have proper text replacement maybe use underscore instead of space for new function name.
| AS | ||
| $body$ | ||
| BEGIN | ||
| --- 1114111 is 0x10FFFF - max value permitted as specified by documentation |
There was a problem hiding this comment.
Let's also correct the comment here
There was a problem hiding this comment.
Should this function be dependant upon the input collation ?
| language plpgsql STABLE; | ||
| LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE; | ||
|
|
||
| CREATE OR REPLACE FUNCTION sys.nchar(IN x INTEGER) RETURNS sys.nvarchar |
There was a problem hiding this comment.
Return type should be nchar
There was a problem hiding this comment.
IMO nvarchar might be a better return function since nchar using fixed length whereas the output will be of length = 1 always, so it might allocate extra space in case of nchar. Else anything can be used, not an issue.
| AS | ||
| $body$ | ||
| BEGIN | ||
| --- 1114111 is 0x10FFFF - max value permitted as specified by documentation |
There was a problem hiding this comment.
Should this function be dependant upon the input collation ?
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
| DECLARE | ||
| exception_message text; | ||
| BEGIN | ||
| ALTER FUNCTION sys.char(x in int) RENAME TO chr_deprecated_5_2_0; |
There was a problem hiding this comment.
Deprecated version 5.3.0, please correct the name everywhere.
| DECLARE | ||
| exception_message text; | ||
| BEGIN | ||
| ALTER FUNCTION sys.nchar(In x INTEGER) RENAME TO nchar_int_deprecated_5_2_0; |
There was a problem hiding this comment.
We should also drop the deprecated functions. Refer:
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
… char & nchar function handling Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
| if (pg_strcasecmp(procNameStr.c_str(), "nchar") == 0) | ||
| { | ||
| int startIndex = proc->start->getStartIndex(); | ||
| std::string schNameStr; | ||
|
|
||
| if (schema) | ||
| schNameStr = stripQuoteFromId(schema); | ||
|
|
||
| if(schema && !schNameStr.empty() && pg_strcasecmp(schNameStr.c_str(), "sys") != 0) | ||
| return; | ||
|
|
||
| if (proc->DOUBLE_QUOTE_ID()) | ||
| stream.setText(startIndex, "\"ncht_\""); | ||
| else if (proc->SQUARE_BRACKET_ID()) | ||
| stream.setText(startIndex, "[ncht_]"); | ||
| else | ||
| stream.setText(startIndex, "ncht_"); |
There was a problem hiding this comment.
Looks like this block is duplicated 4 times. Can we create one helper function for this ?
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Signed-off-by: pranav jain <pranav23iitd@gmail.com>
36085f6
into
babelfish-for-postgresql:BABEL_5_X_DEV
…resql#3855) Function char throws error if the input integer is not in range 0-255, where as tsql returns NULL instead of error. Function nchar should throw NULL if the the input integer is not in range 0-65535 Fix for nchar() function so that if input type is coercible to int then it should execute successfully without throwing error Task: [BABEL-4810] [BABEL-4812][BABEL-5933] Signed-off-by: pranav jain <pranav23iitd@gmail.com>
Description
Function char throws error if the input integer is not in range 0-255, where as tsql returns NULL instead of error.
Function nchar should throw NULL if the the input integer is not in range 0-65535
Fix for nchar() function so that if input type is coercible to int then it should execute successfully without throwing error
Issues Resolved
[BABEL-4810] [BABEL-4812][BABEL-5933]
Signed-off-by: Pranav Jain pranavjc@amazon.com
Test Scenarios Covered
Use case based -
Boundary conditions -
Arbitrary inputs -
Negative test cases -
Minor version upgrade tests -
Major version upgrade tests -
Performance tests -
Tooling impact -
Client tests -
Check List
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.