Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset cross-db flag for each statement #3237

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions contrib/babelfishpg_tsql/src/tsqlIface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,10 @@ class tsqlBuilder : public tsqlCommonMutator

// record whether stmt is cross-db
if (is_cross_db)
{
stmt->is_cross_db = true;
is_cross_db = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are also setting this flag to true in makeExecuteProcedure but not resetting it back. What would happen if we exec a batch like

EXEC db.dbo.p
SELECT * FROM t

Copy link
Contributor

@tanscorpio7 tanscorpio7 Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we reset is_cross_db in enterDMLStatement ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also seems like we can just remove use of this flag from makeExecuteProcedure

Copy link
Contributor Author

@HarshLunagariya HarshLunagariya Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also seems like we can just remove use of this flag from makeExecuteProcedure

Yes, I noticed it too. On top of that we can cleanup some logic from pl_exec as well. We should track it separately because I assume there we will need more test coverage, can't just rely on existing tests to conclude that given logic is redundant.

can we reset is_cross_db in enterDMLStatement ?

Done.

}
// record that the stmt is dml
stmt->is_dml = true;
// record if a function call
Expand Down
366 changes: 366 additions & 0 deletions test/JDBC/expected/BABEL-CROSS-DB-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,372 @@ int
~~END~~


-- tsql
-- Cross-db testcases for statements in batch/block
USE master;
GO

-- Batches having system objects
BEGIN; SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases; BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; END; END;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Let's also add test where we call sys objects as well like db1.sys.databases etc.
  2. Create temp tables after executing cross-db stmt as DDL permissions work differently.

Copy link
Contributor

@anju15bharti anju15bharti Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also let's include below cases:

  1. tests for statements in batch inside a transaction.
  2. some cases where login/user has some default db and schemas with it and it's executing one cross-db query with other queries in a batch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Let's also add test where we call sys objects as well like db1.sys.databases etc.
  • Create temp tables after executing cross-db stmt as DDL permissions work differently.

Done.

Also let's include below cases:

  1. tests for statements in batch inside a transaction.
  2. some cases where login/user has some default db and schemas with it and it's executing one cross-db query with other queries in a batch.

Regarding, 2nd point - not adding anything for default db setting since it doesn't affect anything while query execution. It is only utilised while setting up a new db session.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regarding default_schema, It is being tracked by BABEL-5050, needs separate fix.

GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; BEGIN SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases; END; END;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


BEGIN; SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases; END; BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; END;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; END; BEGIN SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases; END;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases;
SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases;
SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases;
SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


-- Batches having user created objects
BEGIN; SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1; BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; END; END;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; BEGIN SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1; END; END;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


BEGIN; SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1; END; BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; END;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; END; BEGIN SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1; END;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1;
SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1;
SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1;
SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1;
GO
~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~

~~START~~
int
1
~~END~~


-- Batches having different types of DMLs
BEGIN; INSERT INTO my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 VALUES (1); BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; END; END;
GO
~~ROW COUNT: 1~~

~~START~~
int
1
~~END~~


BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; BEGIN; INSERT INTO my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 VALUES (1); END; END;
GO
~~START~~
int
1
~~END~~

~~ROW COUNT: 1~~


BEGIN; UPDATE my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 SET a = 2; BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; END; END;
GO
~~ROW COUNT: 10~~

~~START~~
int
1
~~END~~


BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; BEGIN; UPDATE my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 SET a = 2; END; END;
GO
~~START~~
int
1
~~END~~

~~ROW COUNT: 10~~


BEGIN; DELETE FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 WHERE a = 2; BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; END; END;
GO
~~ROW COUNT: 10~~

~~START~~
int
1
~~END~~


BEGIN; SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1; BEGIN; DELETE FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 WHERE a = 2; END; END;
GO
~~START~~
int
1
~~END~~


-- Batches with subqueries
SELECT DISTINCT (SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 t2 WHERE t2.a = t1.a) FROM babel_cross_db_vu_prepare_master_t1 t1;
SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
GO
~~START~~
int
<NULL>
~~END~~

~~START~~
int
1
~~END~~


SELECT DISTINCT 1 FROM babel_cross_db_vu_prepare_master_t1;
SELECT DISTINCT (SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.babel_cross_db_vu_prepare_db1_t1 t2 WHERE t2.a = t1.a) FROM babel_cross_db_vu_prepare_master_t1 t1;
GO
~~START~~
int
1
~~END~~

~~START~~
int
<NULL>
~~END~~


-- Batches with DDL and DML combination
BEGIN; SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases; BEGIN CREATE TABLE babel_cross_db_vu_verify_tempt(a int); SELECT DISTINCT 1 FROM babel_cross_db_vu_verify_tempt; END; END;
GO
~~START~~
int
1
~~END~~

~~START~~
int
~~END~~


DROP TABLE babel_cross_db_vu_verify_tempt;
GO

BEGIN; CREATE TABLE babel_cross_db_vu_verify_tempt(a int); SELECT DISTINCT 1 FROM babel_cross_db_vu_verify_tempt; BEGIN; SELECT DISTINCT 1 FROM my_babel_cross_db_vu_prepare_db1.dbo.sysdatabases; END; END;
GO
~~START~~
int
~~END~~

~~START~~
int
1
~~END~~


DROP TABLE babel_cross_db_vu_verify_tempt;
GO

-- tsql
USE master
GO
Loading
Loading