From 65623547f6c507746b34da022f0eb7269b7a4fc5 Mon Sep 17 00:00:00 2001 From: Kristian Lejao Date: Tue, 30 Apr 2024 20:52:11 +0000 Subject: [PATCH] Make NEWID() and NEWSEQUENTIALID() functions VOLATILE Altering a table to add a column with DEFAULT NEWID() makes the values all the same value which is not the correct bheaviour. This is because NEWID and NEWSEQUENTIALID are both STABLE when then should be VOLATILE. Task: BABEL-4923 Signed-off-by: Kristian Lejao --- contrib/babelfishpg_common/Version.config | 2 +- .../sql/uniqueidentifier.sql | 4 +- .../babelfishpg_common--2.6.0--2.7.0.sql | 10 + test/JDBC/expected/BABEL-1858.out | 158 +++++++++++- ...efore_14_12_or_15_7_or_16_3-vu-cleanup.out | 9 + ...efore_14_12_or_15_7_or_16_3-vu-prepare.out | 182 ++++++++++++++ ...before_14_12_or_15_7_or_16_3-vu-verify.out | 92 +++++++ .../newid_before_14_7_or_15_2-vu-cleanup.out | 9 + .../newid_before_14_7_or_15_2-vu-prepare.out | 193 +++++++++++++++ .../newid_before_14_7_or_15_2-vu-verify.out | 98 ++++++++ .../expected/parallel_query/BABEL-1858.out | 228 ++++++++++++++++++ test/JDBC/input/BABEL-1858.mix | 133 ++++++++++ test/JDBC/input/BABEL-1858.sql | 40 --- ...efore_14_12_or_15_7_or_16_3-vu-cleanup.sql | 9 + ...efore_14_12_or_15_7_or_16_3-vu-prepare.mix | 115 +++++++++ ...before_14_12_or_15_7_or_16_3-vu-verify.mix | 48 ++++ .../newid_before_14_7_or_15_2-vu-cleanup.sql | 9 + .../newid_before_14_7_or_15_2-vu-prepare.mix | 124 ++++++++++ .../newid_before_14_7_or_15_2-vu-verify.mix | 50 ++++ test/JDBC/jdbc_schedule | 8 + test/JDBC/upgrade/14_10/schedule | 1 + test/JDBC/upgrade/14_11/schedule | 1 + test/JDBC/upgrade/14_6/schedule | 1 + test/JDBC/upgrade/14_8/schedule | 1 + test/JDBC/upgrade/14_9/schedule | 1 + 25 files changed, 1481 insertions(+), 45 deletions(-) create mode 100644 contrib/babelfishpg_common/sql/upgrades/babelfishpg_common--2.6.0--2.7.0.sql create mode 100644 test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-cleanup.out create mode 100644 test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-prepare.out create mode 100644 test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-verify.out create mode 100644 test/JDBC/expected/newid_before_14_7_or_15_2-vu-cleanup.out create mode 100644 test/JDBC/expected/newid_before_14_7_or_15_2-vu-prepare.out create mode 100644 test/JDBC/expected/newid_before_14_7_or_15_2-vu-verify.out create mode 100644 test/JDBC/expected/parallel_query/BABEL-1858.out create mode 100644 test/JDBC/input/BABEL-1858.mix delete mode 100644 test/JDBC/input/BABEL-1858.sql create mode 100644 test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-cleanup.sql create mode 100644 test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-prepare.mix create mode 100644 test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-verify.mix create mode 100644 test/JDBC/input/newid_before_14_7_or_15_2-vu-cleanup.sql create mode 100644 test/JDBC/input/newid_before_14_7_or_15_2-vu-prepare.mix create mode 100644 test/JDBC/input/newid_before_14_7_or_15_2-vu-verify.mix diff --git a/contrib/babelfishpg_common/Version.config b/contrib/babelfishpg_common/Version.config index 0616e3dd88..83973a9a02 100644 --- a/contrib/babelfishpg_common/Version.config +++ b/contrib/babelfishpg_common/Version.config @@ -1,4 +1,4 @@ BBFPGCMN_MAJOR_VERSION=2 -BBFPGCMN_MINOR_VERSION=6 +BBFPGCMN_MINOR_VERSION=7 BBFPGCMN_MICRO_VERSION=0 diff --git a/contrib/babelfishpg_common/sql/uniqueidentifier.sql b/contrib/babelfishpg_common/sql/uniqueidentifier.sql index 482688e831..004899c479 100644 --- a/contrib/babelfishpg_common/sql/uniqueidentifier.sql +++ b/contrib/babelfishpg_common/sql/uniqueidentifier.sql @@ -36,7 +36,7 @@ CREATE TYPE sys.UNIQUEIDENTIFIER ( CREATE OR REPLACE FUNCTION sys.newid() RETURNS sys.UNIQUEIDENTIFIER AS 'uuid-ossp', 'uuid_generate_v4' -- uuid-ossp was added as dependency -LANGUAGE C STABLE STRICT PARALLEL SAFE; +LANGUAGE C VOLATILE STRICT PARALLEL SAFE; /* * in tsql, NEWSEQUENTIALID() produces a new unique value @@ -47,7 +47,7 @@ LANGUAGE C STABLE STRICT PARALLEL SAFE; CREATE OR REPLACE FUNCTION sys.NEWSEQUENTIALID() RETURNS sys.UNIQUEIDENTIFIER AS 'uuid-ossp', 'uuid_generate_v4' -LANGUAGE C STABLE STRICT PARALLEL SAFE; +LANGUAGE C VOLATILE STRICT PARALLEL SAFE; CREATE FUNCTION sys.uniqueidentifiereq(sys.UNIQUEIDENTIFIER, sys.UNIQUEIDENTIFIER) RETURNS bool diff --git a/contrib/babelfishpg_common/sql/upgrades/babelfishpg_common--2.6.0--2.7.0.sql b/contrib/babelfishpg_common/sql/upgrades/babelfishpg_common--2.6.0--2.7.0.sql new file mode 100644 index 0000000000..40f14de2fd --- /dev/null +++ b/contrib/babelfishpg_common/sql/upgrades/babelfishpg_common--2.6.0--2.7.0.sql @@ -0,0 +1,10 @@ +-- complain if script is sourced in psql, rather than via ALTER EXTENSION +\echo Use "ALTER EXTENSION ""babelfishpg_common"" UPDATE TO '2.7.0'" to load this file. \quit + +SELECT set_config('search_path', 'sys, '||current_setting('search_path'), false); + +ALTER FUNCTION sys.newid() VOLATILE; +ALTER FUNCTION sys.NEWSEQUENTIALID() VOLATILE; + +-- Reset search_path to not affect any subsequent scripts +SELECT set_config('search_path', trim(leading 'sys, ' from current_setting('search_path')), false); diff --git a/test/JDBC/expected/BABEL-1858.out b/test/JDBC/expected/BABEL-1858.out index 03d0ea4402..282d585c39 100644 --- a/test/JDBC/expected/BABEL-1858.out +++ b/test/JDBC/expected/BABEL-1858.out @@ -1,3 +1,5 @@ +-- tsql + USE master; GO @@ -31,18 +33,50 @@ int ~~END~~ +TRUNCATE TABLE newid_volatile +GO + + +DECLARE @i INT = 1 +DECLARE @num_iter INT = 5 +WHILE @i <= @num_iter +BEGIN + INSERT INTO newid_volatile VALUES (NEWSEQUENTIALID()) + SET @i = @i + 1 +END +-- should be equal to @num_iter +select count(distinct u) from newid_volatile +go +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +5 +~~END~~ + + -- test volatility of function in procedure -- should see different id values with each iteration CREATE PROC p_newid AS DECLARE @num_iter INT = 5 DECLARE @i INT = 1 -CREATE TABLE newid_volatile_proc (u uniqueidentifier) +CREATE TABLE newid_volatile_proc (u uniqueidentifier, v uniqueidentifier) WHILE @i <= @num_iter BEGIN - INSERT INTO newid_volatile_proc VALUES (NEWID()) + INSERT INTO newid_volatile_proc VALUES (NEWID(), NEWSEQUENTIALID()) SET @i = @i + 1 END go + + EXEC p_newid -- should be equal to @num_iter select count(distinct u) from newid_volatile_proc @@ -63,8 +97,128 @@ int ~~END~~ +select count(distinct v) from newid_volatile_proc +GO +~~START~~ +int +5 +~~END~~ + DROP TABLE newid_volatile DROP TABLE newid_volatile_proc DROP PROCEDURE p_newid GO + +-- Add alter table coverage +CREATE TABLE TestNewId( + id INT, + name VARCHAR(32) +) +GO + +insert into TestNewId values(1, 'aaa') +insert into TestNewId values(2, 'bbb') +insert into TestNewId values(3, 'ccc') +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + + +-- should be successful +ALTER TABLE TestNewId ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +INSERT INTO TestNewId SELECT generate_series(1, 1000, 1), 'hello', NEWID() +GO +~~ROW COUNT: 1000~~ + + +-- should be 1000+3 +SELECT COUNT(DISTINCT uuid) FROM TestNewId +GO +~~START~~ +int +1003 +~~END~~ + + +-- psql +ANALYZE master_dbo.TestNewId +GO + +-- tsql + +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO +~~START~~ +text +off +~~END~~ + + +-- cannot use indexscan with volatile anymore +SET BABELFISH_SHOWPLAN_ALL ON +SELECT * FROM TestNewId WHERE uuid=NEWID() +GO +~~START~~ +text +Query Text: SELECT * FROM TestNewId WHERE uuid=NEWID() +Seq Scan on testnewid + Filter: (uuid = newid()) +~~END~~ + + +SET BABELFISH_SHOWPLAN_ALL OFF +GO + +-- Repeat with NEWSEQUENTIALID instead of NEWID +ALTER TABLE TestNewId DROP COLUMN uuid +GO + +ALTER TABLE TestNewId ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO + +-- psql +ANALYZE master_dbo.TestNewId +GO + + +-- tsql +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO +~~START~~ +text +off +~~END~~ + + +-- cannot use indexscan with volatile anymore +SET BABELFISH_SHOWPLAN_ALL ON +SELECT * FROM TestNewId WHERE uuid=NEWID() +GO +~~START~~ +text +Query Text: SELECT * FROM TestNewId WHERE uuid=NEWID() +Seq Scan on testnewid + Filter: (uuid = newid()) +~~END~~ + + +SET BABELFISH_SHOWPLAN_ALL OFF +GO + +-- should still be 1003 +SELECT COUNT(DISTINCT uuid) FROM TestNewId +GO +~~START~~ +int +1003 +~~END~~ + + +DROP TABLE TestNewId +GO diff --git a/test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-cleanup.out b/test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-cleanup.out new file mode 100644 index 0000000000..2607b2c241 --- /dev/null +++ b/test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-cleanup.out @@ -0,0 +1,9 @@ + +DROP PROC babel_4923_newid_proc +DROP PROC babel_4923_newid_proc_2 +GO + +DROP TABLE babel_4923_newid_tab2 +DROP TABLE babel_4923_newid_tab3 +DROP TABLE babel_4923_newid_tab4 +GO diff --git a/test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-prepare.out b/test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-prepare.out new file mode 100644 index 0000000000..a25d6b65dd --- /dev/null +++ b/test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-prepare.out @@ -0,0 +1,182 @@ +-- tsql + + + + + +CREATE PROC babel_4923_newid_proc +AS + DECLARE @num_iter INT = 5 + DECLARE @i INT = 1 + CREATE TABLE babel_4923_newid_tab1 (u uniqueidentifier) + WHILE @i <= @num_iter + BEGIN + INSERT INTO babel_4923_newid_tab1 VALUES (NEWID()) + SET @i = @i + 1 + END + ALTER TABLE babel_4923_newid_tab1 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() + -- should be 1 before the fix + SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab1 + DROP TABLE babel_4923_newid_tab1 +GO + +EXEC babel_4923_newid_proc +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +1 +~~END~~ + + + + + + +CREATE PROC babel_4923_newid_proc_2 +AS + DECLARE @num_iter INT = 5 + DECLARE @i INT = 1 + CREATE TABLE babel_4923_newid_tab1 (u uniqueidentifier) + WHILE @i <= @num_iter + BEGIN + INSERT INTO babel_4923_newid_tab1 VALUES (NEWSEQUENTIALID()) + SET @i = @i + 1 + END + ALTER TABLE babel_4923_newid_tab1 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() + -- should be 1 before the fix + SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab1 + DROP TABLE babel_4923_newid_tab1 +GO + +EXEC babel_4923_newid_proc_2 +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +1 +~~END~~ + + + + +-- Also create outside a proc showing we are not able to create primary key +-- before upgrade but will be able to after the upgrade +CREATE TABLE babel_4923_newid_tab2( + id INT, + name CHAR(32) +) +GO + +insert into babel_4923_newid_tab2 values(1, 'aaa') +insert into babel_4923_newid_tab2 values(2, 'bbb') +insert into babel_4923_newid_tab2 values(3, 'ccc') +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + + +-- should fail before 14.12, 15.7, 16.3, should succeed after upgrade +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO +~~ERROR (Code: 1505)~~ + +~~ERROR (Message: could not create unique index "babel_4923_newid_tab2_pkey")~~ + + +-- should fail before 14.12, 15.7, 16.3, should succeed after upgrade +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO +~~ERROR (Code: 1505)~~ + +~~ERROR (Message: could not create unique index "babel_4923_newid_tab2_pkey")~~ + + +CREATE TABLE babel_4923_newid_tab3( + id INT, + name CHAR(32), + uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY +) +GO + +INSERT INTO babel_4923_newid_tab3(id, name) SELECT generate_series(1, 10000, 1), 'hello' +GO +~~ROW COUNT: 10000~~ + + +CREATE TABLE babel_4923_newid_tab4( + id INT, + name CHAR(32), + uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY +) +GO + +INSERT INTO babel_4923_newid_tab4(id, name) SELECT generate_series(1, 10000, 1), 'hello' +GO +~~ROW COUNT: 10000~~ + + +-- psql +ANALYZE master_dbo.babel_4923_newid_tab3 +GO + +ANALYZE master_dbo.babel_4923_newid_tab4 +GO + +-- tsql +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO +~~START~~ +text +off +~~END~~ + + + + +SET BABELFISH_SHOWPLAN_ALL ON +-- should show index scan before 14.12, 15.7, 16.3 +-- but it should show seqscan after upgrade +SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +GO +~~START~~ +text +Query Text: SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +Index Scan using babel_4923_newid_tab3_pkey on babel_4923_newid_tab3 + Index Cond: (uuid = newid()) +~~END~~ + + +SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWSEQUENTIALID() +GO +~~START~~ +text +Query Text: SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWSEQUENTIALID() +Index Scan using babel_4923_newid_tab4_pkey on babel_4923_newid_tab4 + Index Cond: (uuid = newsequentialid()) +~~END~~ + + +SET BABELFISH_SHOWPLAN_ALL OFF +GO diff --git a/test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-verify.out b/test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-verify.out new file mode 100644 index 0000000000..5e9037ae20 --- /dev/null +++ b/test/JDBC/expected/newid_before_14_12_or_15_7_or_16_3-vu-verify.out @@ -0,0 +1,92 @@ +-- tsql + +-- should show 5 unique instead of 1 prior to upgrade +EXEC babel_4923_newid_proc +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +5 +~~END~~ + + +-- should succeed after upgrade +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +-- to verify success, this should return 3 +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO +~~START~~ +int +3 +~~END~~ + + +ALTER TABLE babel_4923_newid_tab3 DROP COLUMN uuid +GO + +-- should succeed after upgrade +ALTER TABLE babel_4923_newid_tab3 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO + +-- to verify success, this should return 3 +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO +~~START~~ +int +3 +~~END~~ + + +-- psql +ANALYZE master_dbo.babel_4923_newid_tab3 +GO + +ANALYZE master_dbo.babel_4923_newid_tab4 +GO + +-- tsql + +-- should show index scan before 14.12, 15.7, 16.3 +-- but it should show seqscan after upgrade because volatile function cant be use during index scan. +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO +~~START~~ +text +off +~~END~~ + + +SET BABELFISH_SHOWPLAN_ALL ON +SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +GO +~~START~~ +text +Query Text: SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +Seq Scan on babel_4923_newid_tab3 + Filter: (uuid = newid()) +~~END~~ + + +SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWSEQUENTIALID() +GO +~~START~~ +text +Query Text: SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWSEQUENTIALID() +Seq Scan on babel_4923_newid_tab4 + Filter: (uuid = newsequentialid()) +~~END~~ + + +SET BABELFISH_SHOWPLAN_ALL OFF +GO diff --git a/test/JDBC/expected/newid_before_14_7_or_15_2-vu-cleanup.out b/test/JDBC/expected/newid_before_14_7_or_15_2-vu-cleanup.out new file mode 100644 index 0000000000..2607b2c241 --- /dev/null +++ b/test/JDBC/expected/newid_before_14_7_or_15_2-vu-cleanup.out @@ -0,0 +1,9 @@ + +DROP PROC babel_4923_newid_proc +DROP PROC babel_4923_newid_proc_2 +GO + +DROP TABLE babel_4923_newid_tab2 +DROP TABLE babel_4923_newid_tab3 +DROP TABLE babel_4923_newid_tab4 +GO diff --git a/test/JDBC/expected/newid_before_14_7_or_15_2-vu-prepare.out b/test/JDBC/expected/newid_before_14_7_or_15_2-vu-prepare.out new file mode 100644 index 0000000000..00de4000e3 --- /dev/null +++ b/test/JDBC/expected/newid_before_14_7_or_15_2-vu-prepare.out @@ -0,0 +1,193 @@ +-- tsql + + + + + +CREATE PROC babel_4923_newid_proc +AS + DECLARE @num_iter INT = 5 + DECLARE @i INT = 1 + CREATE TABLE babel_4923_newid_tab1 (u uniqueidentifier) + WHILE @i <= @num_iter + BEGIN + INSERT INTO babel_4923_newid_tab1 VALUES (NEWID()) + SET @i = @i + 1 + END + ALTER TABLE babel_4923_newid_tab1 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() + -- should be 5 + SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab1 + DROP TABLE babel_4923_newid_tab1 +GO + +EXEC babel_4923_newid_proc +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +5 +~~END~~ + + + + + + +CREATE PROC babel_4923_newid_proc_2 +AS + DECLARE @num_iter INT = 5 + DECLARE @i INT = 1 + CREATE TABLE babel_4923_newid_tab1 (u uniqueidentifier) + WHILE @i <= @num_iter + BEGIN + INSERT INTO babel_4923_newid_tab1 VALUES (NEWSEQUENTIALID()) + SET @i = @i + 1 + END + ALTER TABLE babel_4923_newid_tab1 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() + -- should be 5 + SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab1 + DROP TABLE babel_4923_newid_tab1 +GO + +EXEC babel_4923_newid_proc_2 +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +5 +~~END~~ + + + +-- Also create outside a proc showing we are not able to create primary key +-- before upgrade but will be able to after the upgrade +CREATE TABLE babel_4923_newid_tab2( + id INT, + name CHAR(32) +) +GO + +insert into babel_4923_newid_tab2 values(1, 'aaa') +insert into babel_4923_newid_tab2 values(2, 'bbb') +insert into babel_4923_newid_tab2 values(3, 'ccc') +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + + +-- should succeed before 14.7 / 15.2 +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO +~~START~~ +int +3 +~~END~~ + + +ALTER TABLE babel_4923_newid_tab2 DROP COLUMN uuid +GO + +-- should succeed before 14.7 / 15.2 +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO + +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO +~~START~~ +int +3 +~~END~~ + + +CREATE TABLE babel_4923_newid_tab3( + id INT, + name CHAR(32), + uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY +) +GO + +INSERT INTO babel_4923_newid_tab3(id, name) SELECT generate_series(1, 10000, 1), 'hello' +GO +~~ROW COUNT: 10000~~ + + +CREATE TABLE babel_4923_newid_tab4( + id INT, + name CHAR(32), + uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY +) +GO + +INSERT INTO babel_4923_newid_tab4(id, name) SELECT generate_series(1, 10000, 1), 'hello' +GO +~~ROW COUNT: 10000~~ + + + +-- psql +ANALYZE master_dbo.babel_4923_newid_tab3 +GO + +ANALYZE master_dbo.babel_4923_newid_tab4 +GO + +-- tsql + + +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO +~~START~~ +text +off +~~END~~ + + + +SET BABELFISH_SHOWPLAN_ALL ON +-- should both show seqscan before and after upgrade +SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +GO +~~START~~ +text +Query Text: SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +Seq Scan on babel_4923_newid_tab3 + Filter: (uuid = newid()) +~~END~~ + + +SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWSEQUENTIALID() +GO +~~START~~ +text +Query Text: SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWSEQUENTIALID() +Seq Scan on babel_4923_newid_tab4 + Filter: (uuid = newsequentialid()) +~~END~~ + + +SET BABELFISH_SHOWPLAN_ALL OFF +GO diff --git a/test/JDBC/expected/newid_before_14_7_or_15_2-vu-verify.out b/test/JDBC/expected/newid_before_14_7_or_15_2-vu-verify.out new file mode 100644 index 0000000000..bd3906e12a --- /dev/null +++ b/test/JDBC/expected/newid_before_14_7_or_15_2-vu-verify.out @@ -0,0 +1,98 @@ +-- tsql +-- should still show 5 +EXEC babel_4923_newid_proc +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +5 +~~END~~ + + +-- should fail because primary key already exists before upgrade +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: column "uuid" of relation "babel_4923_newid_tab2" already exists)~~ + + +ALTER TABLE babel_4923_newid_tab2 DROP COLUMN uuid +GO + +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO +~~START~~ +int +3 +~~END~~ + + +ALTER TABLE babel_4923_newid_tab2 DROP COLUMN uuid +GO + +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO + +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO +~~START~~ +int +3 +~~END~~ + + +-- psql +ANALYZE master_dbo.babel_4923_newid_tab3 +GO + +ANALYZE master_dbo.babel_4923_newid_tab4 +GO + +-- tsql + +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO +~~START~~ +text +off +~~END~~ + + + +SET BABELFISH_SHOWPLAN_ALL ON +-- should still show seqscan after upgrade otherwise something changed +SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +GO +~~START~~ +text +Query Text: SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +Seq Scan on babel_4923_newid_tab3 + Filter: (uuid = newid()) +~~END~~ + + +SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWID() +GO +~~START~~ +text +Query Text: SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWID() +Seq Scan on babel_4923_newid_tab4 + Filter: (uuid = newid()) +~~END~~ + + +SET BABELFISH_SHOWPLAN_ALL OFF +GO diff --git a/test/JDBC/expected/parallel_query/BABEL-1858.out b/test/JDBC/expected/parallel_query/BABEL-1858.out new file mode 100644 index 0000000000..06cbdd3d04 --- /dev/null +++ b/test/JDBC/expected/parallel_query/BABEL-1858.out @@ -0,0 +1,228 @@ +-- tsql + +USE master; +GO + + +-- test volatility of function in batch +-- should see different id values with each iteration +DECLARE @i INT = 1 +DECLARE @num_iter INT = 5 +CREATE TABLE newid_volatile (u uniqueidentifier) +WHILE @i <= @num_iter +BEGIN + INSERT INTO newid_volatile VALUES (NEWID()) + SET @i = @i + 1 +END +-- should be equal to @num_iter +select count(distinct u) from newid_volatile +go +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +5 +~~END~~ + + +TRUNCATE TABLE newid_volatile +GO + + +DECLARE @i INT = 1 +DECLARE @num_iter INT = 5 +WHILE @i <= @num_iter +BEGIN + INSERT INTO newid_volatile VALUES (NEWSEQUENTIALID()) + SET @i = @i + 1 +END +-- should be equal to @num_iter +select count(distinct u) from newid_volatile +go +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +5 +~~END~~ + + +-- test volatility of function in procedure +-- should see different id values with each iteration +CREATE PROC p_newid AS +DECLARE @num_iter INT = 5 +DECLARE @i INT = 1 +CREATE TABLE newid_volatile_proc (u uniqueidentifier, v uniqueidentifier) +WHILE @i <= @num_iter +BEGIN + INSERT INTO newid_volatile_proc VALUES (NEWID(), NEWSEQUENTIALID()) + SET @i = @i + 1 +END +go + + +EXEC p_newid +-- should be equal to @num_iter +select count(distinct u) from newid_volatile_proc +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +5 +~~END~~ + + +select count(distinct v) from newid_volatile_proc +GO +~~START~~ +int +5 +~~END~~ + + +DROP TABLE newid_volatile +DROP TABLE newid_volatile_proc +DROP PROCEDURE p_newid +GO + +-- Add alter table coverage +CREATE TABLE TestNewId( + id INT, + name VARCHAR(32) +) +GO + +insert into TestNewId values(1, 'aaa') +insert into TestNewId values(2, 'bbb') +insert into TestNewId values(3, 'ccc') +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + + +-- should be successful +ALTER TABLE TestNewId ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +INSERT INTO TestNewId SELECT generate_series(1, 1000, 1), 'hello', NEWID() +GO +~~ROW COUNT: 1000~~ + + +-- should be 1000+3 +SELECT COUNT(DISTINCT uuid) FROM TestNewId +GO +~~START~~ +int +1003 +~~END~~ + + +-- psql +ANALYZE master_dbo.TestNewId +GO + +-- tsql + +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO +~~START~~ +text +off +~~END~~ + + +-- cannot use indexscan with volatile anymore +SET BABELFISH_SHOWPLAN_ALL ON +SELECT * FROM TestNewId WHERE uuid=NEWID() +GO +~~START~~ +text +Query Text: SELECT * FROM TestNewId WHERE uuid=NEWID() +Gather + Workers Planned: 2 + -> Parallel Seq Scan on testnewid + Filter: (uuid = newid()) +~~END~~ + + +SET BABELFISH_SHOWPLAN_ALL OFF +GO + +-- Repeat with NEWSEQUENTIALID instead of NEWID +ALTER TABLE TestNewId DROP COLUMN uuid +GO + +ALTER TABLE TestNewId ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO + +-- psql +ANALYZE master_dbo.TestNewId +GO + + +-- tsql +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO +~~START~~ +text +off +~~END~~ + + +-- cannot use indexscan with volatile anymore +SET BABELFISH_SHOWPLAN_ALL ON +SELECT * FROM TestNewId WHERE uuid=NEWID() +GO +~~START~~ +text +Query Text: SELECT * FROM TestNewId WHERE uuid=NEWID() +Gather + Workers Planned: 2 + -> Parallel Seq Scan on testnewid + Filter: (uuid = newid()) +~~END~~ + + +SET BABELFISH_SHOWPLAN_ALL OFF +GO + +-- should still be 1003 +SELECT COUNT(DISTINCT uuid) FROM TestNewId +GO +~~START~~ +int +1003 +~~END~~ + + +DROP TABLE TestNewId +GO diff --git a/test/JDBC/input/BABEL-1858.mix b/test/JDBC/input/BABEL-1858.mix new file mode 100644 index 0000000000..07a53a29bd --- /dev/null +++ b/test/JDBC/input/BABEL-1858.mix @@ -0,0 +1,133 @@ +-- parallel_query_expected +-- tsql + +USE master; +GO + +-- test volatility of function in batch +-- should see different id values with each iteration +DECLARE @i INT = 1 +DECLARE @num_iter INT = 5 +CREATE TABLE newid_volatile (u uniqueidentifier) +WHILE @i <= @num_iter +BEGIN + INSERT INTO newid_volatile VALUES (NEWID()) + SET @i = @i + 1 +END + +-- should be equal to @num_iter +select count(distinct u) from newid_volatile +go + +TRUNCATE TABLE newid_volatile +GO + +DECLARE @i INT = 1 +DECLARE @num_iter INT = 5 +WHILE @i <= @num_iter +BEGIN + INSERT INTO newid_volatile VALUES (NEWSEQUENTIALID()) + SET @i = @i + 1 +END + +-- should be equal to @num_iter +select count(distinct u) from newid_volatile +go + +-- test volatility of function in procedure +-- should see different id values with each iteration +CREATE PROC p_newid AS +DECLARE @num_iter INT = 5 +DECLARE @i INT = 1 +CREATE TABLE newid_volatile_proc (u uniqueidentifier, v uniqueidentifier) +WHILE @i <= @num_iter +BEGIN + INSERT INTO newid_volatile_proc VALUES (NEWID(), NEWSEQUENTIALID()) + SET @i = @i + 1 +END +go + +EXEC p_newid + +-- should be equal to @num_iter +select count(distinct u) from newid_volatile_proc +GO + +select count(distinct v) from newid_volatile_proc +GO + +DROP TABLE newid_volatile +DROP TABLE newid_volatile_proc +DROP PROCEDURE p_newid +GO + +-- Add alter table coverage +CREATE TABLE TestNewId( + id INT, + name VARCHAR(32) +) +GO + +insert into TestNewId values(1, 'aaa') +insert into TestNewId values(2, 'bbb') +insert into TestNewId values(3, 'ccc') +GO + +-- should be successful +ALTER TABLE TestNewId ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +INSERT INTO TestNewId SELECT generate_series(1, 1000, 1), 'hello', NEWID() +GO + +-- should be 1000+3 +SELECT COUNT(DISTINCT uuid) FROM TestNewId +GO + +-- psql +ANALYZE master_dbo.TestNewId +GO + +-- tsql + +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO + +-- cannot use indexscan with volatile anymore +SET BABELFISH_SHOWPLAN_ALL ON +SELECT * FROM TestNewId WHERE uuid=NEWID() +GO + +SET BABELFISH_SHOWPLAN_ALL OFF +GO + +-- Repeat with NEWSEQUENTIALID instead of NEWID +ALTER TABLE TestNewId DROP COLUMN uuid +GO + +ALTER TABLE TestNewId ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO + +-- psql +ANALYZE master_dbo.TestNewId +GO + + +-- tsql +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO + +-- cannot use indexscan with volatile anymore +SET BABELFISH_SHOWPLAN_ALL ON +SELECT * FROM TestNewId WHERE uuid=NEWID() +GO + +SET BABELFISH_SHOWPLAN_ALL OFF +GO + +-- should still be 1003 +SELECT COUNT(DISTINCT uuid) FROM TestNewId +GO + +DROP TABLE TestNewId +GO diff --git a/test/JDBC/input/BABEL-1858.sql b/test/JDBC/input/BABEL-1858.sql deleted file mode 100644 index fc22cc41e2..0000000000 --- a/test/JDBC/input/BABEL-1858.sql +++ /dev/null @@ -1,40 +0,0 @@ -USE master; -GO - --- test volatility of function in batch --- should see different id values with each iteration -DECLARE @i INT = 1 -DECLARE @num_iter INT = 5 -CREATE TABLE newid_volatile (u uniqueidentifier) -WHILE @i <= @num_iter -BEGIN - INSERT INTO newid_volatile VALUES (NEWID()) - SET @i = @i + 1 -END - --- should be equal to @num_iter -select count(distinct u) from newid_volatile -go - --- test volatility of function in procedure --- should see different id values with each iteration -CREATE PROC p_newid AS -DECLARE @num_iter INT = 5 -DECLARE @i INT = 1 -CREATE TABLE newid_volatile_proc (u uniqueidentifier) -WHILE @i <= @num_iter -BEGIN - INSERT INTO newid_volatile_proc VALUES (NEWID()) - SET @i = @i + 1 -END -go -EXEC p_newid --- should be equal to @num_iter -select count(distinct u) from newid_volatile_proc -GO - - -DROP TABLE newid_volatile -DROP TABLE newid_volatile_proc -DROP PROCEDURE p_newid -GO \ No newline at end of file diff --git a/test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-cleanup.sql b/test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-cleanup.sql new file mode 100644 index 0000000000..5778bb8ec0 --- /dev/null +++ b/test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-cleanup.sql @@ -0,0 +1,9 @@ + +DROP PROC babel_4923_newid_proc +DROP PROC babel_4923_newid_proc_2 +GO + +DROP TABLE babel_4923_newid_tab2 +DROP TABLE babel_4923_newid_tab3 +DROP TABLE babel_4923_newid_tab4 +GO \ No newline at end of file diff --git a/test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-prepare.mix b/test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-prepare.mix new file mode 100644 index 0000000000..1c0b7e60c5 --- /dev/null +++ b/test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-prepare.mix @@ -0,0 +1,115 @@ +-- tsql + +CREATE PROC babel_4923_newid_proc +AS + DECLARE @num_iter INT = 5 + DECLARE @i INT = 1 + + CREATE TABLE babel_4923_newid_tab1 (u uniqueidentifier) + WHILE @i <= @num_iter + BEGIN + INSERT INTO babel_4923_newid_tab1 VALUES (NEWID()) + SET @i = @i + 1 + END + + ALTER TABLE babel_4923_newid_tab1 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() + + -- should be 1 before the fix + SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab1 + + DROP TABLE babel_4923_newid_tab1 +GO + +EXEC babel_4923_newid_proc +GO + +CREATE PROC babel_4923_newid_proc_2 +AS + DECLARE @num_iter INT = 5 + DECLARE @i INT = 1 + + CREATE TABLE babel_4923_newid_tab1 (u uniqueidentifier) + WHILE @i <= @num_iter + BEGIN + INSERT INTO babel_4923_newid_tab1 VALUES (NEWSEQUENTIALID()) + SET @i = @i + 1 + END + + ALTER TABLE babel_4923_newid_tab1 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() + + -- should be 1 before the fix + SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab1 + + DROP TABLE babel_4923_newid_tab1 +GO + +EXEC babel_4923_newid_proc_2 +GO + + +-- Also create outside a proc showing we are not able to create primary key +-- before upgrade but will be able to after the upgrade + +CREATE TABLE babel_4923_newid_tab2( + id INT, + name CHAR(32) +) +GO + +insert into babel_4923_newid_tab2 values(1, 'aaa') +insert into babel_4923_newid_tab2 values(2, 'bbb') +insert into babel_4923_newid_tab2 values(3, 'ccc') +GO + +-- should fail before 14.12, 15.7, 16.3, should succeed after upgrade +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +-- should fail before 14.12, 15.7, 16.3, should succeed after upgrade +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO + +CREATE TABLE babel_4923_newid_tab3( + id INT, + name CHAR(32), + uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY +) +GO + +INSERT INTO babel_4923_newid_tab3(id, name) SELECT generate_series(1, 10000, 1), 'hello' +GO + +CREATE TABLE babel_4923_newid_tab4( + id INT, + name CHAR(32), + uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY +) +GO + +INSERT INTO babel_4923_newid_tab4(id, name) SELECT generate_series(1, 10000, 1), 'hello' +GO + +-- psql +ANALYZE master_dbo.babel_4923_newid_tab3 +GO + +ANALYZE master_dbo.babel_4923_newid_tab4 +GO + +-- tsql +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO + +SET BABELFISH_SHOWPLAN_ALL ON + +-- should show index scan before 14.12, 15.7, 16.3 +-- but it should show seqscan after upgrade + +SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +GO + +SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWSEQUENTIALID() +GO + +SET BABELFISH_SHOWPLAN_ALL OFF +GO diff --git a/test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-verify.mix b/test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-verify.mix new file mode 100644 index 0000000000..6be0adb753 --- /dev/null +++ b/test/JDBC/input/newid_before_14_12_or_15_7_or_16_3-vu-verify.mix @@ -0,0 +1,48 @@ +-- tsql + +-- should show 5 unique instead of 1 prior to upgrade +EXEC babel_4923_newid_proc +GO + +-- should succeed after upgrade +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +-- to verify success, this should return 3 +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO + +ALTER TABLE babel_4923_newid_tab3 DROP COLUMN uuid +GO + +-- should succeed after upgrade +ALTER TABLE babel_4923_newid_tab3 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO + +-- to verify success, this should return 3 +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO + +-- psql +ANALYZE master_dbo.babel_4923_newid_tab3 +GO + +ANALYZE master_dbo.babel_4923_newid_tab4 +GO + +-- tsql + +-- should show index scan before 14.12, 15.7, 16.3 +-- but it should show seqscan after upgrade because volatile function cant be use during index scan. +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO + +SET BABELFISH_SHOWPLAN_ALL ON +SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +GO + +SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWSEQUENTIALID() +GO + +SET BABELFISH_SHOWPLAN_ALL OFF +GO diff --git a/test/JDBC/input/newid_before_14_7_or_15_2-vu-cleanup.sql b/test/JDBC/input/newid_before_14_7_or_15_2-vu-cleanup.sql new file mode 100644 index 0000000000..5778bb8ec0 --- /dev/null +++ b/test/JDBC/input/newid_before_14_7_or_15_2-vu-cleanup.sql @@ -0,0 +1,9 @@ + +DROP PROC babel_4923_newid_proc +DROP PROC babel_4923_newid_proc_2 +GO + +DROP TABLE babel_4923_newid_tab2 +DROP TABLE babel_4923_newid_tab3 +DROP TABLE babel_4923_newid_tab4 +GO \ No newline at end of file diff --git a/test/JDBC/input/newid_before_14_7_or_15_2-vu-prepare.mix b/test/JDBC/input/newid_before_14_7_or_15_2-vu-prepare.mix new file mode 100644 index 0000000000..6aec90c85d --- /dev/null +++ b/test/JDBC/input/newid_before_14_7_or_15_2-vu-prepare.mix @@ -0,0 +1,124 @@ +-- tsql + +CREATE PROC babel_4923_newid_proc +AS + DECLARE @num_iter INT = 5 + DECLARE @i INT = 1 + + CREATE TABLE babel_4923_newid_tab1 (u uniqueidentifier) + WHILE @i <= @num_iter + BEGIN + INSERT INTO babel_4923_newid_tab1 VALUES (NEWID()) + SET @i = @i + 1 + END + + ALTER TABLE babel_4923_newid_tab1 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() + + -- should be 5 + SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab1 + + DROP TABLE babel_4923_newid_tab1 +GO + +EXEC babel_4923_newid_proc +GO + +CREATE PROC babel_4923_newid_proc_2 +AS + DECLARE @num_iter INT = 5 + DECLARE @i INT = 1 + + CREATE TABLE babel_4923_newid_tab1 (u uniqueidentifier) + WHILE @i <= @num_iter + BEGIN + INSERT INTO babel_4923_newid_tab1 VALUES (NEWSEQUENTIALID()) + SET @i = @i + 1 + END + + ALTER TABLE babel_4923_newid_tab1 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() + + -- should be 5 + SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab1 + + DROP TABLE babel_4923_newid_tab1 +GO + +EXEC babel_4923_newid_proc_2 +GO + +-- Also create outside a proc showing we are not able to create primary key +-- before upgrade but will be able to after the upgrade + +CREATE TABLE babel_4923_newid_tab2( + id INT, + name CHAR(32) +) +GO + +insert into babel_4923_newid_tab2 values(1, 'aaa') +insert into babel_4923_newid_tab2 values(2, 'bbb') +insert into babel_4923_newid_tab2 values(3, 'ccc') +GO + +-- should succeed before 14.7 / 15.2 +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO + +ALTER TABLE babel_4923_newid_tab2 DROP COLUMN uuid +GO + +-- should succeed before 14.7 / 15.2 +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO + +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO + +CREATE TABLE babel_4923_newid_tab3( + id INT, + name CHAR(32), + uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY +) +GO + +INSERT INTO babel_4923_newid_tab3(id, name) SELECT generate_series(1, 10000, 1), 'hello' +GO + +CREATE TABLE babel_4923_newid_tab4( + id INT, + name CHAR(32), + uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY +) +GO + +INSERT INTO babel_4923_newid_tab4(id, name) SELECT generate_series(1, 10000, 1), 'hello' +GO + + +-- psql +ANALYZE master_dbo.babel_4923_newid_tab3 +GO + +ANALYZE master_dbo.babel_4923_newid_tab4 +GO + +-- tsql + + +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO + +SET BABELFISH_SHOWPLAN_ALL ON + +-- should both show seqscan before and after upgrade +SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +GO + +SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWSEQUENTIALID() +GO + +SET BABELFISH_SHOWPLAN_ALL OFF +GO diff --git a/test/JDBC/input/newid_before_14_7_or_15_2-vu-verify.mix b/test/JDBC/input/newid_before_14_7_or_15_2-vu-verify.mix new file mode 100644 index 0000000000..7f9d6c3c83 --- /dev/null +++ b/test/JDBC/input/newid_before_14_7_or_15_2-vu-verify.mix @@ -0,0 +1,50 @@ +-- tsql +-- should still show 5 +EXEC babel_4923_newid_proc +GO + +-- should fail because primary key already exists before upgrade +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +ALTER TABLE babel_4923_newid_tab2 DROP COLUMN uuid +GO + +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY; +GO + +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO + +ALTER TABLE babel_4923_newid_tab2 DROP COLUMN uuid +GO + +ALTER TABLE babel_4923_newid_tab2 ADD uuid UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY; +GO + +SELECT COUNT(DISTINCT uuid) FROM babel_4923_newid_tab2 +GO + +-- psql +ANALYZE master_dbo.babel_4923_newid_tab3 +GO + +ANALYZE master_dbo.babel_4923_newid_tab4 +GO + +-- tsql + +select set_config('babelfishpg_tsql.explain_costs', 'off', false); +GO + +SET BABELFISH_SHOWPLAN_ALL ON + +-- should still show seqscan after upgrade otherwise something changed +SELECT * FROM babel_4923_newid_tab3 WHERE uuid=NEWID() +GO + +SELECT * FROM babel_4923_newid_tab4 WHERE uuid=NEWID() +GO + +SET BABELFISH_SHOWPLAN_ALL OFF +GO diff --git a/test/JDBC/jdbc_schedule b/test/JDBC/jdbc_schedule index 49eb4ca371..51a4656e7f 100644 --- a/test/JDBC/jdbc_schedule +++ b/test/JDBC/jdbc_schedule @@ -163,3 +163,11 @@ ignore#!#babel_726-before-14_12-or-15_7-or-16_3-vu-cleanup ignore#!#BABEL-4279-vu-prepare ignore#!#BABEL-4279-vu-verify ignore#!#BABEL-4279-vu-cleanup + +ignore#!#newid_before_14_12_or_15_7_or_16_3-vu-prepare +ignore#!#newid_before_14_12_or_15_7_or_16_3-vu-verify +ignore#!#newid_before_14_12_or_15_7_or_16_3-vu-cleanup +ignore#!#newid_before_14_7_or_15_2-vu-prepare +ignore#!#newid_before_14_7_or_15_2-vu-verify +ignore#!#newid_before_14_7_or_15_2-vu-cleanup + diff --git a/test/JDBC/upgrade/14_10/schedule b/test/JDBC/upgrade/14_10/schedule index 0ba20d3f58..236fd95b48 100644 --- a/test/JDBC/upgrade/14_10/schedule +++ b/test/JDBC/upgrade/14_10/schedule @@ -153,6 +153,7 @@ insteadof_nested_trigger_inside_proc insteadof_nested_trigger_with_dml nested_trigger_inside_proc nested_trigger_with_dml +newid_before_14_12_or_15_7_or_16_3 triggers_with_transaction Test-sp_addrole Test-sp_addrolemember diff --git a/test/JDBC/upgrade/14_11/schedule b/test/JDBC/upgrade/14_11/schedule index 070cbebc0a..a74d1a8436 100644 --- a/test/JDBC/upgrade/14_11/schedule +++ b/test/JDBC/upgrade/14_11/schedule @@ -152,6 +152,7 @@ insteadof_nested_trigger_inside_proc insteadof_nested_trigger_with_dml nested_trigger_inside_proc nested_trigger_with_dml +newid_before_14_12_or_15_7_or_16_3 triggers_with_transaction Test-sp_addrole Test-sp_addrolemember diff --git a/test/JDBC/upgrade/14_6/schedule b/test/JDBC/upgrade/14_6/schedule index 65e27872f0..a806760910 100644 --- a/test/JDBC/upgrade/14_6/schedule +++ b/test/JDBC/upgrade/14_6/schedule @@ -372,3 +372,4 @@ BABEL-4672 BABEL-4641-before-16_3-or-15_7-or-14_12 BABEL-4863-before-16_3-or-15_7-or-14_12 babel_726-before-14_12-or-15_7-or-16_3 +newid_before_14_7_or_15_2 diff --git a/test/JDBC/upgrade/14_8/schedule b/test/JDBC/upgrade/14_8/schedule index 2112c9887a..fa8928c5b6 100644 --- a/test/JDBC/upgrade/14_8/schedule +++ b/test/JDBC/upgrade/14_8/schedule @@ -416,3 +416,4 @@ BABEL-4641-before-16_3-or-15_7-or-14_12 BABEL_4817 BABEL-4863-before-16_3-or-15_7-or-14_12 babel_726-before-14_12-or-15_7-or-16_3 +newid_before_14_12_or_15_7_or_16_3 diff --git a/test/JDBC/upgrade/14_9/schedule b/test/JDBC/upgrade/14_9/schedule index 09e62f3670..29c81aeeb1 100644 --- a/test/JDBC/upgrade/14_9/schedule +++ b/test/JDBC/upgrade/14_9/schedule @@ -152,6 +152,7 @@ insteadof_nested_trigger_inside_proc insteadof_nested_trigger_with_dml nested_trigger_inside_proc nested_trigger_with_dml +newid_before_14_12_or_15_7_or_16_3 triggers_with_transaction Test-sp_addrole Test-sp_addrolemember