From ba6df879e166b9d2ec4f03113a3b09b17d9b13d0 Mon Sep 17 00:00:00 2001 From: Roshan Kanwar Date: Fri, 27 Sep 2024 16:28:36 +0530 Subject: [PATCH] Fix issue with sp_tables failing for cross-database table qualifiers (#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 --- .../babelfishpg_tsql/sql/babelfishpg_tsql.sql | 7 +- .../babelfishpg_tsql--4.3.0--4.4.0.sql | 7 +- .../SP_TABLES-cross-db-vu-cleanup.out | 20 ++++++ .../SP_TABLES-cross-db-vu-prepare.out | 17 +++++ .../expected/SP_TABLES-cross-db-vu-verify.out | 68 +++++++++++++++++++ .../input/SP_TABLES-cross-db-vu-cleanup.sql | 20 ++++++ .../input/SP_TABLES-cross-db-vu-prepare.sql | 17 +++++ .../input/SP_TABLES-cross-db-vu-verify.sql | 36 ++++++++++ test/JDBC/upgrade/latest/schedule | 1 + 9 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 test/JDBC/expected/SP_TABLES-cross-db-vu-cleanup.out create mode 100644 test/JDBC/expected/SP_TABLES-cross-db-vu-prepare.out create mode 100644 test/JDBC/expected/SP_TABLES-cross-db-vu-verify.out create mode 100644 test/JDBC/input/SP_TABLES-cross-db-vu-cleanup.sql create mode 100644 test/JDBC/input/SP_TABLES-cross-db-vu-prepare.sql create mode 100644 test/JDBC/input/SP_TABLES-cross-db-vu-verify.sql diff --git a/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql b/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql index 8a0721961b..93bef048cb 100644 --- a/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql +++ b/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql @@ -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 @@ -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 diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.3.0--4.4.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.3.0--4.4.0.sql index 00a8c48fee..2b96ab75ba 100644 --- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.3.0--4.4.0.sql +++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.3.0--4.4.0.sql @@ -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 @@ -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 diff --git a/test/JDBC/expected/SP_TABLES-cross-db-vu-cleanup.out b/test/JDBC/expected/SP_TABLES-cross-db-vu-cleanup.out new file mode 100644 index 0000000000..f1e150bbcf --- /dev/null +++ b/test/JDBC/expected/SP_TABLES-cross-db-vu-cleanup.out @@ -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 diff --git a/test/JDBC/expected/SP_TABLES-cross-db-vu-prepare.out b/test/JDBC/expected/SP_TABLES-cross-db-vu-prepare.out new file mode 100644 index 0000000000..6f870c8475 --- /dev/null +++ b/test/JDBC/expected/SP_TABLES-cross-db-vu-prepare.out @@ -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 diff --git a/test/JDBC/expected/SP_TABLES-cross-db-vu-verify.out b/test/JDBC/expected/SP_TABLES-cross-db-vu-verify.out new file mode 100644 index 0000000000..c59b1ff8c9 --- /dev/null +++ b/test/JDBC/expected/SP_TABLES-cross-db-vu-verify.out @@ -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#!# +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!# +babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!# +~~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#!# +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!# +babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!# +~~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#!# +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!# +babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!# +~~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#!# +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!# +babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!# +~~END~~ + diff --git a/test/JDBC/input/SP_TABLES-cross-db-vu-cleanup.sql b/test/JDBC/input/SP_TABLES-cross-db-vu-cleanup.sql new file mode 100644 index 0000000000..f1e150bbcf --- /dev/null +++ b/test/JDBC/input/SP_TABLES-cross-db-vu-cleanup.sql @@ -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 diff --git a/test/JDBC/input/SP_TABLES-cross-db-vu-prepare.sql b/test/JDBC/input/SP_TABLES-cross-db-vu-prepare.sql new file mode 100644 index 0000000000..6f870c8475 --- /dev/null +++ b/test/JDBC/input/SP_TABLES-cross-db-vu-prepare.sql @@ -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 diff --git a/test/JDBC/input/SP_TABLES-cross-db-vu-verify.sql b/test/JDBC/input/SP_TABLES-cross-db-vu-verify.sql new file mode 100644 index 0000000000..468b130e0d --- /dev/null +++ b/test/JDBC/input/SP_TABLES-cross-db-vu-verify.sql @@ -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 diff --git a/test/JDBC/upgrade/latest/schedule b/test/JDBC/upgrade/latest/schedule index 1c0825603e..3fe22cbe62 100644 --- a/test/JDBC/upgrade/latest/schedule +++ b/test/JDBC/upgrade/latest/schedule @@ -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