Skip to content

Commit

Permalink
Add first batch of tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tanzeel Khan <[email protected]>
  • Loading branch information
tanscorpio7 committed Feb 5, 2025
1 parent fd40915 commit 0bcd5b0
Show file tree
Hide file tree
Showing 9 changed files with 1,693 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/src/iterative_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -2225,7 +2225,7 @@ set_search_path_for_pltsql_stmt(PLtsql_stmt *stmt)
pfree(physical_schema);
pfree(dbo_schema);
}
else if (execsqlstmt->is_ddl && current_db_search_path)
else if (execsqlstmt->is_ddl)
{
new_search_path = pstrdup(current_db_search_path);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- tsql
DROP DATABASE babel_5448_db
GO

DROP PROC dbo.babel_5448_p, dbo.babel_5448_p1
GO
DROP PROC babel_5448_user_default_schema.babel_5448_p, babel_5448_user_default_schema.babel_5448_p1
GO
DROP PROC babel_5448_s1.babel_5448_p, babel_5448_s1.babel_5448_p1
GO

DROP FUNCTION dbo.babel_5448_f
GO
DROP FUNCTION babel_5448_user_default_schema.babel_5448_f
GO
DROP FUNCTION babel_5448_s1.babel_5448_f
GO

DROP TABLE dbo.babel_5448_t
GO
DROP TABLE babel_5448_user_default_schema.babel_5448_t
GO
DROP TABLE babel_5448_s1.babel_5448_t
GO

DROP VIEW dbo.babel_5448_v
GO
DROP VIEW babel_5448_user_default_schema.babel_5448_v
GO
DROP VIEW babel_5448_s1.babel_5448_v
GO

DROP SCHEMA babel_5448_user_default_schema
GO
DROP SCHEMA babel_5448_s1
GO
283 changes: 283 additions & 0 deletions test/JDBC/expected/BABEL_OBJECT_RESOLUTION_IN_ROUTINES-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@









-- tsql
-- In this test file we will test babelfish object resolution inside procedure functions and triggers
-- At high level there are four cases to tests
-- 1. CROSS DB routine calls
-- 2. Same DB routine calls
-- 3. CROSS DB non default user schema
-- 4. Same DB non default user schema
-- Then each case will be tested against subcases like
-- 1. DMLs
-- 2. DDLs
-- 3. Cross databases objects references
-- 4. EXEC procedure call statements
-- 5. Other tsql statements
-- Each tests will need to be repeated thrice since search path will have two schemas and on the third attemp we should get object does not exists errors
-- 1. Object exists in the first schema
-- 2. Object does not exists in the first schema but does exists in second schema
-- 3. We should error out now, if object is not a schema sys object and not schema qualified
-- Another important case to test is scope handling
-- This means that search path and current logical database value should be set and reset correctly
-- and we do not leak any of these values in any case
-- For example a search path which was set inside the procedure should not be used for queries after the procedure has ended
-- So we need tests cases like EXEC s1.p; SELECT * FROM t; and check if t is resolved correctly.
/***************************************/
/*************** SAME DB ***************/
/***************************************/
/******** SETUP ********/
-- We will work with 3 schemas "dbo" <user_default_schema> & <some_other_schema>
USE master
GO
CREATE SCHEMA babel_5448_user_default_schema
GO
CREATE SCHEMA babel_5448_s1
GO

-- Now create objects in all three schemas
CREATE TABLE dbo.babel_5448_t (id NVARCHAR(max))
GO
CREATE TABLE babel_5448_user_default_schema.babel_5448_t (id NVARCHAR(max))
GO
CREATE TABLE babel_5448_s1.babel_5448_t (id NVARCHAR(max))
GO

INSERT INTO dbo.babel_5448_t VALUES ('master.dbo')
INSERT INTO babel_5448_user_default_schema.babel_5448_t VALUES ('master.babel_5448_user_default_schema')
INSERT INTO babel_5448_s1.babel_5448_t VALUES ('master.babel_5448_s1')
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~


CREATE PROCEDURE dbo.babel_5448_p AS SELECT 'master.dbo' AS col1
GO
CREATE PROCEDURE babel_5448_user_default_schema.babel_5448_p AS SELECT 'master.babel_5448_user_default_schema' AS col1
GO
CREATE PROCEDURE babel_5448_s1.babel_5448_p AS SELECT 'master.babel_5448_s1' AS col1
GO

CREATE FUNCTION dbo.babel_5448_f() RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN 'master.dbo';
END
GO

CREATE FUNCTION babel_5448_user_default_schema.babel_5448_f() RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN 'master.babel_5448_user_default_schema';
END
GO

CREATE FUNCTION babel_5448_s1.babel_5448_f() RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN 'master.babel_5448_s1';
END
GO

CREATE VIEW dbo.babel_5448_v AS SELECT 'master.dbo' AS col1
GO
CREATE VIEW babel_5448_user_default_schema.babel_5448_v AS SELECT 'master.babel_5448_user_default_schema' AS col1
GO
CREATE VIEW babel_5448_s1.babel_5448_v AS SELECT 'master.babel_5448_s1' AS col1
GO

CREATE TRIGGER dbo.babel_5448_trigger
ON dbo.babel_5448_t
AFTER INSERT AS
SELECT '=========== START OF TRIGGER EXECUTION ========== master.dbo'
SELECT dbo.babel_5448_f();
SELECT * FROM babel_5448_t;
SELECT '=========== END OF TRIGGER EXECUTION ========== master.dbo'
GO

CREATE TRIGGER babel_5448_user_default_schema.babel_5448_trigger
ON babel_5448_user_default_schema.babel_5448_t
AFTER INSERT AS
SELECT '=========== START OF TRIGGER EXECUTION ========== master.babel_5448_user_default_schema'
SELECT babel_5448_user_default_schema.babel_5448_f();
SELECT * FROM babel_5448_t;
SELECT '=========== END OF TRIGGER EXECUTION ========== master.babel_5448_user_default_schema'
GO

CREATE TRIGGER babel_5448_s1.babel_5448_trigger
ON babel_5448_s1.babel_5448_t
AFTER INSERT AS
SELECT '=========== START OF TRIGGER EXECUTION ========== master.babel_5448_s1'
SELECT babel_5448_s1.babel_5448_f();
SELECT * FROM babel_5448_t;
SELECT '=========== END OF TRIGGER EXECUTION ========== master.babel_5448_s1'
GO

CREATE PROCEDURE dbo.babel_5448_p1
AS
SELECT babel_5448_f(), * FROM babel_5448_t;
-- EXEC call should still resolve to dbo.babel_5448_p
EXEC babel_5448_p;
INSERT INTO babel_5448_t VALUES ('temp inserted row to check DML and trigger executiong');
DELETE FROM babel_5448_t WHERE length(id) > 50;
-- CHECK IF ROW WAS DELETED FROM THE RIGHT TABLE
SELECT * FROM babel_5448_t;
GO

CREATE PROCEDURE babel_5448_user_default_schema.babel_5448_p1
AS
SELECT babel_5448_f(), * FROM babel_5448_t;
-- EXEC call should still resolve to dbo.babel_5448_p
EXEC babel_5448_p;
INSERT INTO babel_5448_t VALUES ('temp inserted row to check DML and trigger executiong');
DELETE FROM babel_5448_t WHERE length(id) > 50;
-- CHECK IF ROW WAS DELETED FROM THE RIGHT TABLE
SELECT * FROM babel_5448_t;
GO

CREATE PROCEDURE babel_5448_s1.babel_5448_p1
AS
SELECT babel_5448_f(), * FROM babel_5448_t;
-- EXEC call should still resolve to dbo.babel_5448_p
EXEC babel_5448_p;
INSERT INTO babel_5448_t VALUES ('temp inserted row to check DML and trigger executiong');
DELETE FROM babel_5448_t WHERE length(id) > 50;
-- CHECK IF ROW WAS DELETED FROM THE RIGHT TABLE
SELECT * FROM babel_5448_t;
GO


/***************************************/
/************** CROSS DB ***************/
/***************************************/
CREATE DATABASE babel_5448_db
GO
USE babel_5448_db
GO
CREATE SCHEMA babel_5448_user_default_schema
GO
CREATE SCHEMA babel_5448_s1
GO

-- Now create objects in all three schemas
CREATE TABLE dbo.babel_5448_t (id NVARCHAR(max))
GO
CREATE TABLE babel_5448_user_default_schema.babel_5448_t (id NVARCHAR(max))
GO
CREATE TABLE babel_5448_s1.babel_5448_t (id NVARCHAR(max))
GO

INSERT INTO dbo.babel_5448_t VALUES ('babel_5448_db.dbo')
INSERT INTO babel_5448_user_default_schema.babel_5448_t VALUES ('babel_5448_db.babel_5448_user_default_schema')
INSERT INTO babel_5448_s1.babel_5448_t VALUES ('babel_5448_db.babel_5448_s1')
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~


CREATE PROCEDURE dbo.babel_5448_p AS SELECT 'babel_5448_db.dbo' AS col1
GO
CREATE PROCEDURE babel_5448_user_default_schema.babel_5448_p AS SELECT 'babel_5448_db.babel_5448_user_default_schema' AS col1
GO
CREATE PROCEDURE babel_5448_s1.babel_5448_p AS SELECT 'babel_5448_db.babel_5448_s1' AS col1
GO

CREATE FUNCTION dbo.babel_5448_f() RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN 'babel_5448_db.dbo';
END
GO

CREATE FUNCTION babel_5448_user_default_schema.babel_5448_f() RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN 'babel_5448_db.babel_5448_user_default_schema';
END
GO

CREATE FUNCTION babel_5448_s1.babel_5448_f() RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN 'babel_5448_db.babel_5448_s1';
END
GO

CREATE VIEW dbo.babel_5448_v AS SELECT 'babel_5448_db.dbo' AS col1
GO
CREATE VIEW babel_5448_user_default_schema.babel_5448_v AS SELECT 'babel_5448_db.babel_5448_user_default_schema' AS col1
GO
CREATE VIEW babel_5448_s1.babel_5448_v AS SELECT 'babel_5448_db.babel_5448_s1' AS col1
GO

CREATE TRIGGER dbo.babel_5448_trigger
ON dbo.babel_5448_t
AFTER INSERT AS
SELECT '=========== START OF TRIGGER EXECUTION ========== babel_5448_db.dbo'
SELECT dbo.babel_5448_f();
SELECT * FROM babel_5448_t;
SELECT '=========== END OF TRIGGER EXECUTION ========== babel_5448_db.dbo'
GO

CREATE TRIGGER babel_5448_user_default_schema.babel_5448_trigger
ON babel_5448_user_default_schema.babel_5448_t
AFTER INSERT AS
SELECT '=========== START OF TRIGGER EXECUTION ========== babel_5448_db.babel_5448_user_default_schema'
SELECT babel_5448_user_default_schema.babel_5448_f();
SELECT * FROM babel_5448_t;
SELECT '=========== END OF TRIGGER EXECUTION ========== babel_5448_db.babel_5448_user_default_schema'
GO

CREATE TRIGGER babel_5448_s1.babel_5448_trigger
ON babel_5448_s1.babel_5448_t
AFTER INSERT AS
SELECT '=========== START OF TRIGGER EXECUTION ========== babel_5448_db.babel_5448_s1'
SELECT babel_5448_s1.babel_5448_f();
SELECT * FROM babel_5448_t;
SELECT '=========== END OF TRIGGER EXECUTION ========== babel_5448_db.babel_5448_s1'
GO

CREATE PROCEDURE dbo.babel_5448_p1
AS
SELECT babel_5448_f(), * FROM babel_5448_t;
-- EXEC call should still resolve to dbo.babel_5448_p
EXEC babel_5448_p;
INSERT INTO babel_5448_t VALUES ('temp inserted row to check DML and trigger executiong');
DELETE FROM babel_5448_t WHERE length(id) > 50;
-- CHECK IF ROW WAS DELETED FROM THE RIGHT TABLE
SELECT * FROM babel_5448_t;
GO

CREATE PROCEDURE babel_5448_user_default_schema.babel_5448_p1
AS
SELECT babel_5448_f(), * FROM babel_5448_t;
-- EXEC call should still resolve to dbo.babel_5448_p
EXEC babel_5448_p;
INSERT INTO babel_5448_t VALUES ('temp inserted row to check DML and trigger executiong');
DELETE FROM babel_5448_t WHERE length(id) > 50;
-- CHECK IF ROW WAS DELETED FROM THE RIGHT TABLE
SELECT * FROM babel_5448_t;
GO

CREATE PROCEDURE babel_5448_s1.babel_5448_p1
AS
SELECT babel_5448_f(), * FROM babel_5448_t;
-- EXEC call should still resolve to dbo.babel_5448_p
EXEC babel_5448_p;
INSERT INTO babel_5448_t VALUES ('temp inserted row to check DML and trigger executiong');
DELETE FROM babel_5448_t WHERE length(id) > 50;
-- CHECK IF ROW WAS DELETED FROM THE RIGHT TABLE
SELECT * FROM babel_5448_t;
GO
Loading

0 comments on commit 0bcd5b0

Please sign in to comment.