Skip to content

Commit

Permalink
Fix issue with sp_tables failing for cross-database table qualifiers (b…
Browse files Browse the repository at this point in the history
…abelfish-for-postgresql#2961)

This change fixes an issue with the sp_tables stored procedure, which incorrectly handled three-part object names across databases, throwing an error regarding database context. The root cause was sys.db_name() returning the wrong database name based on the object qualifier.

The issue affected the usability of linked servers in Babelfish, as it prevented accurate metadata retrieval, limiting compatibility with SQL Server.

The fix replaced sys.db_name() with a direct SELECT sys.db_name() to fetch the correct database name before comparing it against the table_qualifier.

Task: BABEL-5263

Signed-off-by: Roshan Kanwar <[email protected]>
  • Loading branch information
roshan0708 authored Sep 27, 2024
1 parent 8d0cc8a commit ba6df87
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 2 deletions.
7 changes: 6 additions & 1 deletion contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,9 @@ CREATE OR REPLACE PROCEDURE sys.sp_tables (
AS $$
BEGIN
-- Temporary variable to hold the current database name
DECLARE @current_db_name sys.sysname;
-- Handle special case: Enumerate all databases when name and owner are blank but qualifier is '%'
IF (@table_qualifier = '%' AND @table_owner = '' AND @table_name = '')
BEGIN
Expand All @@ -935,7 +938,9 @@ BEGIN
RETURN;
END;
IF (@table_qualifier != '' AND LOWER(@table_qualifier) != LOWER(sys.db_name()))
SELECT @current_db_name = sys.db_name();
IF (@table_qualifier != '' AND LOWER(@table_qualifier) != LOWER(@current_db_name))
BEGIN
THROW 33557097, N'The database name component of the object qualifier must be the name of the current database.', 1;
END
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,9 @@ CREATE OR REPLACE PROCEDURE sys.sp_tables (
AS $$
BEGIN
-- Temporary variable to hold the current database name
DECLARE @current_db_name sys.sysname;
-- Handle special case: Enumerate all databases when name and owner are blank but qualifier is '%'
IF (@table_qualifier = '%' AND @table_owner = '' AND @table_name = '')
BEGIN
Expand All @@ -1878,7 +1881,9 @@ BEGIN
RETURN;
END;
IF (@table_qualifier != '' AND LOWER(@table_qualifier) != LOWER(sys.db_name()))
SELECT @current_db_name = sys.db_name();
IF (@table_qualifier != '' AND LOWER(@table_qualifier) != LOWER(@current_db_name))
BEGIN
THROW 33557097, N'The database name component of the object qualifier must be the name of the current database.', 1;
END
Expand Down
20 changes: 20 additions & 0 deletions test/JDBC/expected/SP_TABLES-cross-db-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
USE babel_5263_vu_prepare_db1;
GO

DROP FUNCTION IF EXISTS babel_5263_vu_prepare_f1;
GO

DROP PROCEDURE IF EXISTS babel_5263_vu_prepare_p1;
GO

DROP VIEW IF EXISTS babel_5263_vu_prepare_v1;
GO

DROP TABLE IF EXISTS babel_5263_vu_prepare_t1;
GO

USE master;
GO

DROP DATABASE IF EXISTS babel_5263_vu_prepare_db1;
GO
17 changes: 17 additions & 0 deletions test/JDBC/expected/SP_TABLES-cross-db-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE DATABASE babel_5263_vu_prepare_db1;
GO

USE babel_5263_vu_prepare_db1;
GO

CREATE TABLE babel_5263_vu_prepare_t1 (a INT);
GO

CREATE PROCEDURE babel_5263_vu_prepare_p1 AS SELECT 1;
GO

CREATE FUNCTION babel_5263_vu_prepare_f1() RETURNS INT AS BEGIN RETURN 1 END;
GO

CREATE VIEW babel_5263_vu_prepare_v1 AS SELECT 1;
GO
68 changes: 68 additions & 0 deletions test/JDBC/expected/SP_TABLES-cross-db-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
-- Case 1: Correct Database Context
USE babel_5263_vu_prepare_db1;
GO

-- Enumerate matching objects
EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1;
GO
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_t1#!#TABLE#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!#<NULL>
~~END~~


-- Case 2: Mismatched Database Context
USE master;
GO

-- Enumerate matching objects
EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1;
GO
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_t1#!#TABLE#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!#<NULL>
~~END~~


-- Case 3: No Table Qualifier - Current Database Assumed
USE babel_5263_vu_prepare_db1;
GO

EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, NULL, NULL, 1;
GO
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_t1#!#TABLE#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!#<NULL>
~~END~~


-- Case 4: Cross-database Access - Mismatch
USE babel_5263_vu_prepare_db1;
GO

EXEC master.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1;
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: The database name component of the object qualifier must be the name of the current database.)~~


-- Case 5: Case Sensitivity in Table Qualifier
USE master;
GO

EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_VU_prepARe_db1', NULL, 1;
GO
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_t1#!#TABLE#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!#<NULL>
~~END~~

20 changes: 20 additions & 0 deletions test/JDBC/input/SP_TABLES-cross-db-vu-cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
USE babel_5263_vu_prepare_db1;
GO

DROP FUNCTION IF EXISTS babel_5263_vu_prepare_f1;
GO

DROP PROCEDURE IF EXISTS babel_5263_vu_prepare_p1;
GO

DROP VIEW IF EXISTS babel_5263_vu_prepare_v1;
GO

DROP TABLE IF EXISTS babel_5263_vu_prepare_t1;
GO

USE master;
GO

DROP DATABASE IF EXISTS babel_5263_vu_prepare_db1;
GO
17 changes: 17 additions & 0 deletions test/JDBC/input/SP_TABLES-cross-db-vu-prepare.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE DATABASE babel_5263_vu_prepare_db1;
GO

USE babel_5263_vu_prepare_db1;
GO

CREATE TABLE babel_5263_vu_prepare_t1 (a INT);
GO

CREATE PROCEDURE babel_5263_vu_prepare_p1 AS SELECT 1;
GO

CREATE FUNCTION babel_5263_vu_prepare_f1() RETURNS INT AS BEGIN RETURN 1 END;
GO

CREATE VIEW babel_5263_vu_prepare_v1 AS SELECT 1;
GO
36 changes: 36 additions & 0 deletions test/JDBC/input/SP_TABLES-cross-db-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Case 1: Correct Database Context
USE babel_5263_vu_prepare_db1;
GO

-- Enumerate matching objects
EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1;
GO

-- Case 2: Mismatched Database Context
USE master;
GO

-- Enumerate matching objects
EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1;
GO

-- Case 3: No Table Qualifier - Current Database Assumed
USE babel_5263_vu_prepare_db1;
GO

EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, NULL, NULL, 1;
GO

-- Case 4: Cross-database Access - Mismatch
USE babel_5263_vu_prepare_db1;
GO

EXEC master.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1;
GO

-- Case 5: Case Sensitivity in Table Qualifier
USE master;
GO

EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_VU_prepARe_db1', NULL, 1;
GO
1 change: 1 addition & 0 deletions test/JDBC/upgrade/latest/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ BABEL-SP_STORED_PROCEDURES-dep
BABEL-SP_TABLE_PRIVILIGES
BABEL-SP_TABLES
SP_TABLES-dep
SP_TABLES-cross-db
babel_sqlvariant_cast_compare
BABEL-SQUARE
BABEL-TABLEOPTIONS
Expand Down

0 comments on commit ba6df87

Please sign in to comment.