diff --git a/contrib/babelfishpg_tsql/src/tsqlIface.cpp b/contrib/babelfishpg_tsql/src/tsqlIface.cpp index c1ec79ddc6..cc556693ae 100644 --- a/contrib/babelfishpg_tsql/src/tsqlIface.cpp +++ b/contrib/babelfishpg_tsql/src/tsqlIface.cpp @@ -1918,7 +1918,10 @@ class tsqlBuilder : public tsqlCommonMutator // record whether stmt is cross-db if (is_cross_db) + { stmt->is_cross_db = true; + is_cross_db = false; + } // record that the stmt is dml stmt->is_dml = true; // record if a function call diff --git a/test/JDBC/expected/BABEL-CROSS-DB-vu-verify.out b/test/JDBC/expected/BABEL-CROSS-DB-vu-verify.out index 1d42bf13e4..a998a2107c 100644 --- a/test/JDBC/expected/BABEL-CROSS-DB-vu-verify.out +++ b/test/JDBC/expected/BABEL-CROSS-DB-vu-verify.out @@ -1452,8 +1452,371 @@ int DROP TABLE #babel_cross_db_vu_verify_tempt; GO +-- [TESTCASE GROUP START] Batches with proc execution and DMLs +USE master; +GO + +EXEC babel_cross_db_vu_prepare_master_p1 +SELECT * FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 +GO +~~START~~ +int +10 +10 +10 +30 +20 +~~END~~ + +~~START~~ +int +~~END~~ + + +EXEC my_babel_cross_db_vu_prepare_db1.dbo.my_babel_cross_db_vu_prepare_db1_p1 +SELECT * FROM babel_cross_db_vu_prepare_master_t1 +GO +~~START~~ +int +10 +~~END~~ + +~~START~~ +int#!#int +2#!#10 +3#!#10 +4#!#10 +5#!#30 +6#!#20 +~~END~~ + + +USE my_babel_cross_db_vu_prepare_db1; +GO + +EXEC my_babel_cross_db_vu_prepare_db1_p1 +SELECT * FROM master.dbo.babel_cross_db_vu_prepare_master_t1 +GO +~~START~~ +int +10 +~~END~~ + +~~START~~ +int#!#int +2#!#10 +3#!#10 +4#!#10 +5#!#30 +6#!#20 +~~END~~ + + +EXEC master.dbo.babel_cross_db_vu_prepare_master_p1 +SELECT * FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 +GO +~~START~~ +int +10 +10 +10 +30 +20 +~~END~~ + +~~START~~ +int +~~END~~ + + + +-- [TESTCASE GROUP END] Batches with proc execution and DMLs +-- [TESTCASE GROUP START] Cross-db SELECT INTO +-- TODO: related to BABEL-4934 - Cross-db SELECT INTO statement is not currently supported correctly +USE master; +GO + +-- New table gets created in my_babel_cross_db_vu_prepare_db1.dbo schema +SELECT 1 INTO new_table1 FROM my_babel_cross_db_vu_prepare_db1.sys.sysdatabases; +GO + +SELECT * FROM new_table1; +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: relation "new_table1" does not exist)~~ + + +USE my_babel_cross_db_vu_prepare_db1; +GO + +DROP TABLE new_table1; +GO + +USE master; +GO + +SELECT 1 INTO new_table2 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1; +GO + +SELECT * FROM new_table2; +GO +~~START~~ +int +~~END~~ + + +DROP TABLE new_table2; +GO + + +-- [TESTCASE GROUP END] Cross-db SELECT INTO +-- [TESTCASE GROUP START] INSERT/UPDATE/SELECT INTO statements +-- Prepare: Populate some rows in test tables +-- CAUTION: Here we are dropping all the rows and then again inserting some rows for ease of validation +USE master; +GO + +DELETE FROM dbo.babel_cross_db_vu_prepare_master_t1; +GO +~~ROW COUNT: 5~~ + + +DELETE FROM dbo.babel_cross_db_vu_prepare_master_t2; +GO +~~ROW COUNT: 1~~ + + +DELETE FROM babel_cross_db_vu_prepare_s1.babel_cross_db_vu_prepare_t1; +GO +~~ROW COUNT: 1~~ + + +USE my_babel_cross_db_vu_prepare_db1; +GO + +DELETE FROM dbo.babel_cross_db_vu_prepare_db1_t1; +GO + +DELETE FROM dbo.my_babel_cross_db_vu_prepare_db1_t2; +GO +~~ROW COUNT: 1~~ + + +DELETE FROM babel_cross_db_vu_prepare_s2.babel_cross_db_vu_prepare_t2; +GO +~~ROW COUNT: 1~~ + + +USE master; +GO + +INSERT INTO dbo.babel_cross_db_vu_prepare_master_t1 (a) VALUES (1), (2), (3), (5); +GO +~~ROW COUNT: 4~~ + + +INSERT INTO dbo.babel_cross_db_vu_prepare_master_t2 (b) VALUES (10), (20), (30), (50); +GO +~~ROW COUNT: 4~~ + + +INSERT INTO babel_cross_db_vu_prepare_s1.babel_cross_db_vu_prepare_t1 (name) VALUES ('Alice'), ('Bob'), ('Charlie'), ('Same'); +GO +~~ROW COUNT: 4~~ + + +USE my_babel_cross_db_vu_prepare_db1; +GO + +INSERT INTO dbo.babel_cross_db_vu_prepare_db1_t1 (a) VALUES (5), (7), (8), (9); +GO +~~ROW COUNT: 4~~ + + +INSERT INTO dbo.my_babel_cross_db_vu_prepare_db1_t2 (a) VALUES (50), (70), (80), (90); +GO +~~ROW COUNT: 4~~ + + +INSERT INTO babel_cross_db_vu_prepare_s2.babel_cross_db_vu_prepare_t2 (name) VALUES ('David'), ('Eva'), ('Frank'), ('Same'); +GO +~~ROW COUNT: 4~~ + + +USE master; +GO + +-- INSERT INTO - Insert data from master database table into my_babel_cross_db_vu_prepare_db1 database table +BEGIN TRAN; +GO + +INSERT INTO my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 (a) +SELECT a +FROM master.dbo.babel_cross_db_vu_prepare_master_t1; +GO +~~ROW COUNT: 4~~ + + +SELECT * FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1; +GO +~~START~~ +int +5 +7 +8 +9 +1 +2 +3 +5 +~~END~~ + + +ROLLBACK; +GO + +-- UPDATE - Update data in my_babel_cross_db_vu_prepare_db1 database table based on data from master database table +BEGIN TRAN; +GO + +UPDATE my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 +SET a = m.a * 2 +FROM master.dbo.babel_cross_db_vu_prepare_master_t1 m +WHERE my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1.a = m.a; +GO +~~ROW COUNT: 1~~ + + +SELECT * FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1; +GO +~~START~~ +int +7 +8 +9 +10 +~~END~~ + + +ROLLBACK; +GO + +-- CROSSDB INSERT WITH JOIN - Insert data into my_babel_cross_db_vu_prepare_db1 table using data from both databases +USE my_babel_cross_db_vu_prepare_db1; +GO + +BEGIN TRAN; +GO + +INSERT INTO dbo.my_babel_cross_db_vu_prepare_db1_t2 (a) +SELECT t1.a +FROM master.dbo.babel_cross_db_vu_prepare_master_t1 t1 +JOIN dbo.babel_cross_db_vu_prepare_db1_t1 t2 ON t1.a = t2.a; +GO +~~ROW COUNT: 1~~ + + +SELECT * FROM dbo.my_babel_cross_db_vu_prepare_db1_t2; +GO +~~START~~ +int +50 +70 +80 +90 +5 +~~END~~ + + +ROLLBACK; +GO + +-- CROSSDB UPDATE USING SUBQUERY - Update data in master database table using a subquery from my_babel_cross_db_vu_prepare_db1 database +USE master; +GO + +BEGIN TRAN; +GO + +UPDATE dbo.babel_cross_db_vu_prepare_master_t2 +SET b = (SELECT MAX(a) FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1) +WHERE b IS NULL; +GO + +ROLLBACK; +GO + +-- CROSSDB INSERT INTO TABLE IN CUSTOM SCHEMA - Insert data from master database into a table in a custom schema in my_babel_cross_db_vu_prepare_db1 database +BEGIN TRAN; +GO + +INSERT INTO my_babel_cross_db_vu_prepare_db1.babel_cross_db_vu_prepare_s2.babel_cross_db_vu_prepare_t2 (name) +SELECT name +FROM babel_cross_db_vu_prepare_s1.babel_cross_db_vu_prepare_t1; +GO +~~ROW COUNT: 4~~ + + +ROLLBACK; +GO + +-- CROSSDB UPDATE USING VIEW - Update data in my_babel_cross_db_vu_prepare_db1 database using a view from master database +BEGIN TRAN; +GO + +UPDATE t +SET t.name = v.t1_name +FROM my_babel_cross_db_vu_prepare_db1.babel_cross_db_vu_prepare_s2.babel_cross_db_vu_prepare_t2 t +JOIN babel_cross_db_vu_prepare_s1.babel_cross_db_vu_prepare_v1 v ON t.id = v.id; +GO +~~ROW COUNT: 4~~ + + +ROLLBACK; +GO + +-- cleanup populated data +USE master; +GO + +DELETE FROM dbo.babel_cross_db_vu_prepare_master_t1; +GO +~~ROW COUNT: 4~~ + + +DELETE FROM dbo.babel_cross_db_vu_prepare_master_t2; +GO +~~ROW COUNT: 4~~ + + +DELETE FROM babel_cross_db_vu_prepare_s1.babel_cross_db_vu_prepare_t1; +GO +~~ROW COUNT: 4~~ + + +USE my_babel_cross_db_vu_prepare_db1; +GO + +DELETE FROM dbo.babel_cross_db_vu_prepare_db1_t1; +GO +~~ROW COUNT: 4~~ + + +DELETE FROM dbo.my_babel_cross_db_vu_prepare_db1_t2; +GO +~~ROW COUNT: 4~~ + + +DELETE FROM babel_cross_db_vu_prepare_s2.babel_cross_db_vu_prepare_t2; +GO +~~ROW COUNT: 4~~ + + + -- tsql --- Batches for testing default_database and default_schema setting +-- [TESTCASE GROUP END] INSERT/UPDATE/SELECT INTO statements +-- [TESTCASE GROUP START] Batches for testing default_database and default_schema setting USE master; GO @@ -1484,7 +1847,6 @@ SELECT * FROM babel_cross_db_vu_prepare_t1; SELECT * FROM my_babel_cross_db_vu_p GO ~~START~~ int#!#varchar -1#!#def ~~END~~ ~~ERROR (Code: 33557097)~~ @@ -1496,12 +1858,10 @@ SELECT * FROM babel_cross_db_vu_prepare_t1; SELECT * FROM my_babel_cross_db_vu_p GO ~~START~~ int#!#varchar -1#!#def ~~END~~ ~~START~~ int#!#varchar -1#!#abc ~~END~~ @@ -1513,7 +1873,6 @@ SELECT * FROM babel_cross_db_vu_prepare_t2; SELECT * FROM master..babel_cross_db GO ~~START~~ int#!#varchar -1#!#abc ~~END~~ ~~ERROR (Code: 33557097)~~ @@ -1525,12 +1884,10 @@ SELECT * FROM babel_cross_db_vu_prepare_t2; SELECT * FROM master.babel_cross_db_ GO ~~START~~ int#!#varchar -1#!#abc ~~END~~ ~~START~~ int#!#varchar -1#!#def ~~END~~ @@ -1559,6 +1916,8 @@ GO DROP LOGIN babel_cross_db_vu_verify_lgn1; GO + -- tsql +-- [TESTCASE GROUP END] Batches for testing default_database and default_schema setting USE master GO diff --git a/test/JDBC/input/ownership/BABEL-CROSS-DB-vu-verify.mix b/test/JDBC/input/ownership/BABEL-CROSS-DB-vu-verify.mix index 715cd65a26..69466f0830 100644 --- a/test/JDBC/input/ownership/BABEL-CROSS-DB-vu-verify.mix +++ b/test/JDBC/input/ownership/BABEL-CROSS-DB-vu-verify.mix @@ -624,7 +624,235 @@ GO DROP TABLE #babel_cross_db_vu_verify_tempt; GO --- Batches for testing default_database and default_schema setting +-- [TESTCASE GROUP START] Batches with proc execution and DMLs +USE master; +GO + +EXEC babel_cross_db_vu_prepare_master_p1 +SELECT * FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 +GO + +EXEC my_babel_cross_db_vu_prepare_db1.dbo.my_babel_cross_db_vu_prepare_db1_p1 +SELECT * FROM babel_cross_db_vu_prepare_master_t1 +GO + +USE my_babel_cross_db_vu_prepare_db1; +GO + +EXEC my_babel_cross_db_vu_prepare_db1_p1 +SELECT * FROM master.dbo.babel_cross_db_vu_prepare_master_t1 +GO + +EXEC master.dbo.babel_cross_db_vu_prepare_master_p1 +SELECT * FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 +GO + +-- [TESTCASE GROUP END] Batches with proc execution and DMLs + +-- [TESTCASE GROUP START] Cross-db SELECT INTO +-- TODO: related to BABEL-4934 - Cross-db SELECT INTO statement is not currently supported correctly +USE master; +GO + +-- New table gets created in my_babel_cross_db_vu_prepare_db1.dbo schema +SELECT 1 INTO new_table1 FROM my_babel_cross_db_vu_prepare_db1.sys.sysdatabases; +GO + +SELECT * FROM new_table1; +GO + +USE my_babel_cross_db_vu_prepare_db1; +GO + +DROP TABLE new_table1; +GO + +USE master; +GO + +SELECT 1 INTO new_table2 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1; +GO + +SELECT * FROM new_table2; +GO + +DROP TABLE new_table2; +GO + +-- [TESTCASE GROUP END] Cross-db SELECT INTO + +-- [TESTCASE GROUP START] INSERT/UPDATE/SELECT INTO statements +-- Prepare: Populate some rows in test tables +-- CAUTION: Here we are dropping all the rows and then again inserting some rows for ease of validation +USE master; +GO + +DELETE FROM dbo.babel_cross_db_vu_prepare_master_t1; +GO + +DELETE FROM dbo.babel_cross_db_vu_prepare_master_t2; +GO + +DELETE FROM babel_cross_db_vu_prepare_s1.babel_cross_db_vu_prepare_t1; +GO + +USE my_babel_cross_db_vu_prepare_db1; +GO + +DELETE FROM dbo.babel_cross_db_vu_prepare_db1_t1; +GO + +DELETE FROM dbo.my_babel_cross_db_vu_prepare_db1_t2; +GO + +DELETE FROM babel_cross_db_vu_prepare_s2.babel_cross_db_vu_prepare_t2; +GO + +USE master; +GO + +INSERT INTO dbo.babel_cross_db_vu_prepare_master_t1 (a) VALUES (1), (2), (3), (5); +GO + +INSERT INTO dbo.babel_cross_db_vu_prepare_master_t2 (b) VALUES (10), (20), (30), (50); +GO + +INSERT INTO babel_cross_db_vu_prepare_s1.babel_cross_db_vu_prepare_t1 (name) VALUES ('Alice'), ('Bob'), ('Charlie'), ('Same'); +GO + +USE my_babel_cross_db_vu_prepare_db1; +GO + +INSERT INTO dbo.babel_cross_db_vu_prepare_db1_t1 (a) VALUES (5), (7), (8), (9); +GO + +INSERT INTO dbo.my_babel_cross_db_vu_prepare_db1_t2 (a) VALUES (50), (70), (80), (90); +GO + +INSERT INTO babel_cross_db_vu_prepare_s2.babel_cross_db_vu_prepare_t2 (name) VALUES ('David'), ('Eva'), ('Frank'), ('Same'); +GO + +USE master; +GO + +-- INSERT INTO - Insert data from master database table into my_babel_cross_db_vu_prepare_db1 database table +BEGIN TRAN; +GO + +INSERT INTO my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 (a) +SELECT a +FROM master.dbo.babel_cross_db_vu_prepare_master_t1; +GO + +SELECT * FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1; +GO + +ROLLBACK; +GO + +-- UPDATE - Update data in my_babel_cross_db_vu_prepare_db1 database table based on data from master database table +BEGIN TRAN; +GO + +UPDATE my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 +SET a = m.a * 2 +FROM master.dbo.babel_cross_db_vu_prepare_master_t1 m +WHERE my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1.a = m.a; +GO + +SELECT * FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1; +GO + +ROLLBACK; +GO + +-- CROSSDB INSERT WITH JOIN - Insert data into my_babel_cross_db_vu_prepare_db1 table using data from both databases +USE my_babel_cross_db_vu_prepare_db1; +GO + +BEGIN TRAN; +GO + +INSERT INTO dbo.my_babel_cross_db_vu_prepare_db1_t2 (a) +SELECT t1.a +FROM master.dbo.babel_cross_db_vu_prepare_master_t1 t1 +JOIN dbo.babel_cross_db_vu_prepare_db1_t1 t2 ON t1.a = t2.a; +GO + +SELECT * FROM dbo.my_babel_cross_db_vu_prepare_db1_t2; +GO + +ROLLBACK; +GO + +-- CROSSDB UPDATE USING SUBQUERY - Update data in master database table using a subquery from my_babel_cross_db_vu_prepare_db1 database +USE master; +GO + +BEGIN TRAN; +GO + +UPDATE dbo.babel_cross_db_vu_prepare_master_t2 +SET b = (SELECT MAX(a) FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1) +WHERE b IS NULL; +GO + +ROLLBACK; +GO + +-- CROSSDB INSERT INTO TABLE IN CUSTOM SCHEMA - Insert data from master database into a table in a custom schema in my_babel_cross_db_vu_prepare_db1 database +BEGIN TRAN; +GO + +INSERT INTO my_babel_cross_db_vu_prepare_db1.babel_cross_db_vu_prepare_s2.babel_cross_db_vu_prepare_t2 (name) +SELECT name +FROM babel_cross_db_vu_prepare_s1.babel_cross_db_vu_prepare_t1; +GO + +ROLLBACK; +GO + +-- CROSSDB UPDATE USING VIEW - Update data in my_babel_cross_db_vu_prepare_db1 database using a view from master database +BEGIN TRAN; +GO + +UPDATE t +SET t.name = v.t1_name +FROM my_babel_cross_db_vu_prepare_db1.babel_cross_db_vu_prepare_s2.babel_cross_db_vu_prepare_t2 t +JOIN babel_cross_db_vu_prepare_s1.babel_cross_db_vu_prepare_v1 v ON t.id = v.id; +GO + +ROLLBACK; +GO + +-- cleanup populated data +USE master; +GO + +DELETE FROM dbo.babel_cross_db_vu_prepare_master_t1; +GO + +DELETE FROM dbo.babel_cross_db_vu_prepare_master_t2; +GO + +DELETE FROM babel_cross_db_vu_prepare_s1.babel_cross_db_vu_prepare_t1; +GO + +USE my_babel_cross_db_vu_prepare_db1; +GO + +DELETE FROM dbo.babel_cross_db_vu_prepare_db1_t1; +GO + +DELETE FROM dbo.my_babel_cross_db_vu_prepare_db1_t2; +GO + +DELETE FROM babel_cross_db_vu_prepare_s2.babel_cross_db_vu_prepare_t2; +GO + +-- [TESTCASE GROUP END] INSERT/UPDATE/SELECT INTO statements + +-- [TESTCASE GROUP START] Batches for testing default_database and default_schema setting -- tsql USE master; GO @@ -693,6 +921,8 @@ GO DROP LOGIN babel_cross_db_vu_verify_lgn1; GO +-- [TESTCASE GROUP END] Batches for testing default_database and default_schema setting + -- tsql USE master GO