Fix: preserve trailing spaces for DATABASE in HAS_PERMS_BY_NAME#4128
Conversation
Previously, HAS_PERMS_BY_NAME would incorrectly strip trailing spaces
from the @object_name parameter when the securable class was 'DATABASE'.
This caused permission checks to fail when the database name contained
trailing spaces.
This change removes the rtrim() on cs_as_securable for DATABASE
securable class, preserving the exact name provided by the caller.
Verified by:
CREATE DATABASE [ blank space ];
SELECT HAS_PERMS_BY_NAME(' blank space ', 'DATABASE', 'CREATE FUNCTION');
Result: Permission check now succeeds.
This aligns Babelfish behavior with SQL Server semantics, which treat
database names with and without trailing spaces as distinct.
Signed-off-by: Derviş Ali Duman <aduman387@gmail.com>
f05370f to
a0371b7
Compare
…eflect trailing space handling Signed-off-by: Derviş Ali Duman <aduman387@gmail.com>
Pull Request Test Coverage Report for Build 18468011320Details
💛 - Coveralls |
12f8235 to
317fefb
Compare
ea34ea5 to
f876527
Compare
|
Hi @tanyagupta17, @forestkeeper , the tests are passing right now. Could you please review and approve them? Thank you in advance! |
|
Hi @pranavJ23 , @rohit01010 , the tests are passing right now. Could you please review and approve them? Thank you in advance! |
pranavJ23
left a comment
There was a problem hiding this comment.
Hi @DervisAliDuman , thanks for working on this PR, I have a follow-up question:
CREATE DATABASE [ blank space ];
Can you once confirm if T-SQL allows to make a database name ending with space/tab ?
Yes @pranavJ23, you can run these test in your Test space: DROP DATABASE IF EXISTS [ blank space ];
CREATE DATABASE [ blank space ];
SELECT HAS_PERMS_BY_NAME('[ blank space ]','DATABASE','CREATE FUNCTION');
SELECT HAS_PERMS_BY_NAME(' blank space ','DATABASE','CREATE FUNCTION');
SELECT HAS_PERMS_BY_NAME('blank space ','DATABASE','CREATE FUNCTION'); |
|
Hello @pranavJ23, is this test case legit for you? You can open mssql and run these commands too: DROP DATABASE IF EXISTS [ blank space ];
CREATE DATABASE [ blank space ];
SELECT HAS_PERMS_BY_NAME('[ blank space ]','DATABASE','CREATE FUNCTION');
SELECT HAS_PERMS_BY_NAME(' blank space ','DATABASE','CREATE FUNCTION');
SELECT HAS_PERMS_BY_NAME('blank space ','DATABASE','CREATE FUNCTION'); |
|
Hi @pranavJ23 and @rohit01010 , can you review it pls. |
|
|
||
| -- Assign each name accordingly | ||
| object_name = sys.babelfish_truncate_identifier(babelfish_remove_delimiter_pair(PG_CATALOG.rtrim(lower_object_name))); | ||
| object_name = sys.babelfish_truncate_identifier(babelfish_remove_delimiter_pair(lower_object_name)); |
There was a problem hiding this comment.
Can this change produce a regression for case when originally database name do not have spaces but we passed spaces while calling HAS_PERMS_BY_NAME function in database name, eg:
create database [hello_db]
go
SELECT HAS_PERMS_BY_NAME('hello_db ','DATABASE','CREATE FUNCTION');
There was a problem hiding this comment.
Yes, the old test and my tests combination support that test you wanted.
There was a problem hiding this comment.
I agree that tests are there but is the behaviour correct ?
Can we confirm this behaviour with t-sql behaviour once ?
There was a problem hiding this comment.
Let me clarify it again :
- Make a db without any trailing spaces:
create database [hello_db]
go
- call function as
SELECT HAS_PERMS_BY_NAME('hello_db ','DATABASE','CREATE FUNCTION');
Now we are calling the HAS_PERMS_BY_NAME with database as hello_db with trailing spaces , now this should return 1 as output as per t-sql behaviour, but since with your change you are removing the rtrim() so this could make the result 0 (instead of 1).
Note: in the tests you added contain the database name with spaces and we are comparing that, in the test I am pointing towards, database name does not have any spaces in it's name, but while calling HAS_PERMS_BY_NAME function we are adding spaces in database name
| ~~START~~ | ||
| int | ||
| 1 | ||
| 0 |
There was a problem hiding this comment.
can you explain this change ?
There was a problem hiding this comment.
Originally there were no trailing space on the right side while creating this database name and it was passing but it shouldn't beacuse it has wrong name withouth spaces. This test was passing beacuse it was rtrim()'ed the spaces. And there are no original databases that can test this bug. So I created My own tests.
|
Hi @DervisAliDuman , let me know if you have any opinion over: #4128 (comment) |
Hi @pranavJ23 , I understand your concern. I just tested the exact scenario you mentioned in SSMS connected to a native SQL Server instance (SQL Server 2022). If we create [hello_db] without spaces, and then call SELECT HAS_PERMS_BY_NAME('hello_db ', 'DATABASE', 'CREATE FUNCTION');, T-SQL actually returns 0, not 1. It strictly matches the string for the DATABASE securable class and does not implicitly trim the input. Therefore, removing rtrim() from Babelfish correctly aligns our behavior with T-SQL without introducing a regression. I have attached the SSMS screenshot for this specific case before as well. |
|
This is bit strange, when I tested above query I am also getting 1 in TSQL 2022. @DervisAliDuman Can you please provide results of following queries in your TSQL Instance |


Description
What:
Previously,
HAS_PERMS_BY_NAMEwould incorrectly strip trailing spaces from the@object_nameparameter when the securable class was'DATABASE'.This caused permission checks to fail when the database name contained trailing spaces.
With this change:
Removed the
rtrim()call oncs_as_securablefor the'DATABASE'securable class.Now Babelfish preserves the exact string provided by the caller, including trailing spaces, aligning behavior with SQL Server.
Why:
SQL Server treats database names with and without trailing spaces as distinct.
Babelfish should match that behavior for accurate permission checks and T-SQL compatibility.
Verification:
Result: Permission check now succeeds.
Issues Resolved
HAS_PERMS_BY_NAME.Test Scenarios Covered
Use case based:
Verified that
HAS_PERMS_BY_NAMEworks with database names that include trailing spaces.Boundary conditions:
Tested database names with:
Negative test cases:
Verified that permission checks still fail for users without
CREATE FUNCTIONprivilege, regardless of trailing spaces.Regression tests:
Confirmed that other securable classes (
OBJECT,SCHEMA, etc.) remain unaffected.Minor/Major upgrade tests:
Initialized Babelfish on a fresh database and verified behavior after restart.
Performance impact:
No measurable impact. The change removes unnecessary
rtrim()call for'DATABASE'securable class only.Check List
--signoffsqlcmdon Babelfish-enabled PostgreSQL 17