Skip to content

Commit

Permalink
Fix alter table drop constraint not able to drop some constraints (#2602
Browse files Browse the repository at this point in the history
)

Babelfish was hashing the constraint name for named table constraints. But no hashing was done for unnamed table constraints, unnamed column constraints or named column constraints. On the other hand the constraint name in DROP CONSTRAINT was always hashed without exception. This means we were failing DROP for all constraints which were not hashed during creation.

As a fix, we have removed the hashing step from named table constraints creation, making creation of constraints consistent for Babelfish. This also allows us to remove hashing from DROP CONSTRAINT command.

Not hashing constraint name has one repercussion, Users can now create a PRIMARY KEY/UNIQUE constraint with same name as an existing Index on the same table. This was previously blocked for named table constraints but not for other types of constraints. We could block this in the future in a more complete way.

Issues Resolved: BABEL-2047

Signed-off-by: Tanzeel Khan [email protected]
  • Loading branch information
tanscorpio7 authored May 22, 2024
1 parent c3340fd commit d89aba9
Show file tree
Hide file tree
Showing 47 changed files with 1,752 additions and 152 deletions.
7 changes: 0 additions & 7 deletions contrib/babelfishpg_tsql/src/pl_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,8 +1022,6 @@ pltsql_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
{
Constraint *c = (Constraint *) element;

c->conname = construct_unique_index_name(c->conname, stmt->relation->relname);

if (rowversion_column_name)
validate_rowversion_table_constraint(c, rowversion_column_name);
}
Expand Down Expand Up @@ -1111,8 +1109,6 @@ pltsql_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
{
Constraint *c = castNode(Constraint, cmd->def);

c->conname = construct_unique_index_name(c->conname, atstmt->relation->relname);

if (rowversion_column_name)
validate_rowversion_table_constraint(c, rowversion_column_name);
}
Expand Down Expand Up @@ -1192,9 +1188,6 @@ pltsql_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
}
}
break;
case AT_DropConstraint:
cmd->name = construct_unique_index_name(cmd->name, atstmt->relation->relname);
break;
default:
break;
}
Expand Down
13 changes: 5 additions & 8 deletions test/JDBC/expected/BABEL-1206-vu-prepare.out
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ go

CREATE TABLE babel_1206_vu_prepare_t4(
[DistinctApplicationID] [bigint] IDENTITY(1,1) NOT NULL,
[Description] [nvarchar](1024) NOT NULL,
[DescriptionHash] AS (hashbytes('MD5',[Description])) PERSISTED NOT NULL,
CONSTRAINT babel_1206_vu_prepare_t4_i3 UNIQUE NONCLUSTERED
(
[DescriptionHash] ASC
)
) ON [PRIMARY]
[Description] [nvarchar](1024) NOT NULL CONSTRAINT babel_1206_vu_prepare_t4_i3 UNIQUE NONCLUSTERED,
[DescriptionHash] AS (hashbytes('MD5',[Description])) PERSISTED NOT NULL
)
ON [PRIMARY]
go

insert into babel_1206_vu_prepare_t4 values ('abc');
Expand All @@ -56,5 +53,5 @@ insert into babel_1206_vu_prepare_t4 values ('abc');
go
~~ERROR (Code: 2627)~~

~~ERROR (Message: duplicate key value violates unique constraint "babel_1206_vu_prepare_t4_i3babeaa1c56a90ff7306469195c2b5d1e115d")~~
~~ERROR (Message: duplicate key value violates unique constraint "babel_1206_vu_prepare_t4_i3")~~

2 changes: 1 addition & 1 deletion test/JDBC/expected/BABEL-1206.out
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ insert into babel_1206_t3 values (0xaaa);
go
~~ERROR (Code: 2627)~~

~~ERROR (Message: duplicate key value violates unique constraint "babel_1206_t3_i3babel_1206_t36b8d8d363edb759764e0add37a7a2ded")~~
~~ERROR (Message: duplicate key value violates unique constraint "babel_1206_t3_i3")~~


drop table babel_1265_t1;
Expand Down
4 changes: 2 additions & 2 deletions test/JDBC/expected/BABEL-1715-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use master;
go

CREATE TABLE babel_1715_vu_prepare_t1 (a int, b int CONSTRAINT uk_a PRIMARY KEY (a));
CREATE TABLE babel_1715_vu_prepare_t1 (a int CONSTRAINT uk_a PRIMARY KEY, b int);
go

INSERT INTO babel_1715_vu_prepare_t1 VALUES (1, 1);
Expand All @@ -12,7 +12,7 @@ GO
~~ROW COUNT: 1~~


CREATE TABLE babel_1715_vu_prepare_t2 (a int, b as a+1 CONSTRAINT uk_a PRIMARY KEY (a));
CREATE TABLE babel_1715_vu_prepare_t2 (a int CONSTRAINT uk_a1 PRIMARY KEY, b as a+1);
go

INSERT INTO babel_1715_vu_prepare_t2 (a) VALUES (1);
Expand Down
4 changes: 2 additions & 2 deletions test/JDBC/expected/BABEL-1715-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ INSERT INTO babel_1715_vu_prepare_t1 VALUES (2, 3);
GO
~~ERROR (Code: 2627)~~

~~ERROR (Message: duplicate key value violates unique constraint "uk_ababel_1715_vu_prepare_t1309394cc13ed0160c093d0a0f82cb1c0")~~
~~ERROR (Message: duplicate key value violates unique constraint "uk_a")~~


INSERT INTO babel_1715_vu_prepare_t2 (a) VALUES (2);
GO
~~ERROR (Code: 2627)~~

~~ERROR (Message: duplicate key value violates unique constraint "uk_ababel_1715_vu_prepare_t2309394cc13ed0160c093d0a0f82cb1c0")~~
~~ERROR (Message: duplicate key value violates unique constraint "uk_a1")~~


CREATE TABLE babel_1715_vu_prepare_invalid1 (a int b int);
Expand Down
4 changes: 2 additions & 2 deletions test/JDBC/expected/BABEL-1715.out
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ INSERT INTO t1715 VALUES (2, 3);
go
~~ERROR (Code: 2627)~~

~~ERROR (Message: duplicate key value violates unique constraint "uk_at1715309394cc13ed0160c093d0a0f82cb1c0")~~
~~ERROR (Message: duplicate key value violates unique constraint "uk_a")~~

drop table t1715;
go
Expand All @@ -33,7 +33,7 @@ INSERT INTO t1715_2 (a) VALUES (2);
go
~~ERROR (Code: 2627)~~

~~ERROR (Message: duplicate key value violates unique constraint "uk_at1715_2309394cc13ed0160c093d0a0f82cb1c0")~~
~~ERROR (Message: duplicate key value violates unique constraint "uk_a")~~

drop table t1715_2;
go
Expand Down
41 changes: 41 additions & 0 deletions test/JDBC/expected/BABEL-621-after-14_12-before-15-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
DROP INDEX idx on babel_621_vu_prepare_table_1;
DROP INDEX idx on babel_621_vu_prepare_table_2;
GO

DROP INDEX uniq on babel_621_vu_prepare_table_3;
GO

DROP INDEX very_long_index_name_on_a_table_1234567890_1234567890_1234567890_1234567890_1234567890 on babel_621_vu_prepare_table_with_long_index_name;
GO

DROP INDEX very_long_index_name_on_a_table_1234567890_1234567890_1234567890_1234567890_1234567890 on babel_621_table_with_long_name_1234567890_1234567890_1234567890_1234567890_1234567890;
GO

DROP TABLE babel_621_vu_prepare_table_1;
GO
DROP TABLE babel_621_vu_prepare_table_2;
GO
DROP TABLE babel_621_vu_prepare_table_3;
GO
DROP TABLE babel_621_vu_prepare_table_4;
GO
DROP TABLE babel_621_vu_prepare_table_with_long_index_name;
GO
DROP TABLE babel_621_vu_prepare_second_table_with_long_index_name;
GO
DROP TABLE babel_621_table_with_long_name_1234567890_1234567890_1234567890_1234567890_1234567890;
GO
DROP TABLE babel_621_second_table_with_long_name_1234567890_1234567890_1234567890_1234567890_1234567890;
GO
DROP TABLE babel_621_vu_prepare_table_6;
GO
DROP TABLE table_6;
GO
DROP TABLE table_7;
GO
DROP TABLE babel_621_vu_prepare_table_7;
GO
DROP TABLE babel_621_vu_prepare_table_8;
GO
DROP TABLE babel_621_vu_prepare_table_10;
GO
114 changes: 114 additions & 0 deletions test/JDBC/expected/BABEL-621-after-14_12-before-15-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
EXECUTE sp_babelfish_configure 'escape_hatch_unique_constraint', 'ignore'
go

create table babel_621_vu_prepare_table_1 (a int);
go
create table babel_621_vu_prepare_table_2 (a int);
go
create index idx on babel_621_vu_prepare_table_1(a);
go
create index idx on babel_621_vu_prepare_table_2(a);
go

create table babel_621_vu_prepare_table_3 (a int);
go
alter table babel_621_vu_prepare_table_3 add constraint uniq unique (a);
go
create index uniq on babel_621_vu_prepare_table_3(a);
go

create table babel_621_vu_prepare_table_4 (a int);
go
create index uniq_table_4 on babel_621_vu_prepare_table_4(a);
go
alter table babel_621_vu_prepare_table_4 add constraint uniq_table_4 unique (a);
go
alter table babel_621_vu_prepare_table_4 drop constraint uniq_table_4;
go

-- Very long index name
create table babel_621_vu_prepare_table_with_long_index_name (a int);
go
create index very_long_index_name_on_a_table_1234567890_1234567890_1234567890_1234567890_1234567890 on babel_621_vu_prepare_table_with_long_index_name(a);
go

create table babel_621_vu_prepare_second_table_with_long_index_name (a int);
go
create index very_long_index_name_on_a_table_1234567890_1234567890_1234567890_1234567890_1234567890 on babel_621_vu_prepare_second_table_with_long_index_name(a);
go

-- Very long table name and very long index name
create table babel_621_table_with_long_name_1234567890_1234567890_1234567890_1234567890_1234567890 (a int);
go
create index very_long_index_name_on_a_table_1234567890_1234567890_1234567890_1234567890_1234567890 on babel_621_table_with_long_name_1234567890_1234567890_1234567890_1234567890_1234567890(a);
go

create table babel_621_second_table_with_long_name_1234567890_1234567890_1234567890_1234567890_1234567890 (a int);
go
create index very_long_index_name_on_a_table_1234567890_1234567890_1234567890_1234567890_1234567890 on babel_621_second_table_with_long_name_1234567890_1234567890_1234567890_1234567890_1234567890(a);
go

-- Situation where simple concatenation of table and index name does not work
-- E.g. table_a + index_a == table_b + index_b
create table babel_621_vu_prepare_table_6 (a int);
go
create index idx_ on babel_621_vu_prepare_table_6(a);
go

create table table_6 (a int);
go
create index idx_babel_621_vu_prepare_ on table_6(a);
go
-- Situation where simple concatenation of index and table name does not work (reverse of previous)
-- E.g. index_a + table_a == index_b + table_b
create table table_7 (a int);
go
create index idx_babel_621_vu_prepare_ on table_7(a);
go

create table babel_621_vu_prepare_table_7 (a int);
go
create index idx_ on babel_621_vu_prepare_table_7(a);
go

--
create table babel_621_vu_prepare_table_8 (
a int,
value int,
constraint constraint_8 unique nonclustered
(
value asc
)
)
go
alter table babel_621_vu_prepare_table_8 drop constraint constraint_8;
go
insert into babel_621_vu_prepare_table_8 values(1, 1);
insert into babel_621_vu_prepare_table_8 values(2, 1);
go
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~


create table babel_621_vu_prepare_table_10
(
a int,
b int,
c int
)
go
create unique index idx on babel_621_vu_prepare_table_10 (a, b);
go
insert into babel_621_vu_prepare_table_10 values(1, 1, 1);
insert into babel_621_vu_prepare_table_10 values(1, 2, 1);
insert into babel_621_vu_prepare_table_10 values(1, 2, 2);
go
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ERROR (Code: 2627)~~

~~ERROR (Message: duplicate key value violates unique constraint "idxbabel_621_vu_prepare_table_147dc0d6e295169e8ba6e11ebeb1c6472")~~

15 changes: 15 additions & 0 deletions test/JDBC/expected/BABEL-621-after-14_12-before-15-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
select a, value from babel_621_vu_prepare_table_8 order by a;
go
~~START~~
int#!#int
1#!#1
2#!#1
~~END~~


drop index idx on babel_621_vu_prepare_table_10;
go
insert into babel_621_vu_prepare_table_10 values(1, 2, 2);
go
~~ROW COUNT: 1~~

4 changes: 0 additions & 4 deletions test/JDBC/expected/BABEL-621-vu-cleanup.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ GO

DROP INDEX uniq on babel_621_vu_prepare_table_3;
GO
~~ERROR (Code: 3723)~~

~~ERROR (Message: cannot drop index uniqbabel_621_vu_prepare_table_d6716c55a366d04accd1036436eb2a86 because constraint uniqbabel_621_vu_prepare_table_d6716c55a366d04accd1036436eb2a86 on table babel_621_vu_prepare_table_3 requires it)~~


DROP INDEX very_long_index_name_on_a_table_1234567890_1234567890_1234567890_1234567890_1234567890 on babel_621_vu_prepare_table_with_long_index_name;
GO
Expand Down
12 changes: 0 additions & 12 deletions test/JDBC/expected/BABEL-621-vu-prepare.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,15 @@ alter table babel_621_vu_prepare_table_3 add constraint uniq unique (a);
go
create index uniq on babel_621_vu_prepare_table_3(a);
go
~~ERROR (Code: 2714)~~

~~ERROR (Message: relation "uniqbabel_621_vu_prepare_table_d6716c55a366d04accd1036436eb2a86" already exists)~~


create table babel_621_vu_prepare_table_4 (a int);
go
create index uniq_table_4 on babel_621_vu_prepare_table_4(a);
go
alter table babel_621_vu_prepare_table_4 add constraint uniq_table_4 unique (a);
go
~~ERROR (Code: 2714)~~

~~ERROR (Message: relation "uniq_table_4babel_621_vu_prepardeacd0a0f930dee973c93a41c32c3ffc" already exists)~~

alter table babel_621_vu_prepare_table_4 drop constraint uniq_table_4;
go
~~ERROR (Code: 3728)~~

~~ERROR (Message: constraint "uniq_table_4babel_621_vu_prepardeacd0a0f930dee973c93a41c32c3ffc" of relation "babel_621_vu_prepare_table_4" does not exist)~~


-- Very long index name
create table babel_621_vu_prepare_table_with_long_index_name (a int);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ ALTER TABLE [dbo].[alter_table_check_constraint] ADD CONSTRAINT [check_col1] CHE
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: check constraint "check_col1alter_table_check_con0f71bca531f50db1bd071ab46c3097ea" of relation "alter_table_check_constraint" is violated by some row)~~
~~ERROR (Message: check constraint "check_col1" of relation "alter_table_check_constraint" is violated by some row)~~

4 changes: 2 additions & 2 deletions test/JDBC/expected/BABEL-CHECK-CONSTRAINT-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ALTER TABLE [dbo].[alter_table_check_constraint] ADD CONSTRAINT [check_col1] CHE
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: check constraint "check_col1alter_table_check_con0f71bca531f50db1bd071ab46c3097ea" of relation "alter_table_check_constraint" is violated by some row)~~
~~ERROR (Message: check constraint "check_col1" of relation "alter_table_check_constraint" is violated by some row)~~


ALTER TABLE check_constraint ADD CONSTRAINT [check_a] CHECK (a not like N'123%')
Expand All @@ -18,7 +18,7 @@ INSERT INTO check_constraint VALUES ('1234'), ('123123')
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: new row for relation "check_constraint" violates check constraint "check_acheck_constraint719599f27c48404560d418ff018273e1")~~
~~ERROR (Message: new row for relation "check_constraint" violates check constraint "check_a")~~


INSERT INTO create_check_constraint VALUES ('abcdEFg')
Expand Down
2 changes: 1 addition & 1 deletion test/JDBC/expected/BABEL-TABLEOPTIONS-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE babel_tableoptions_vu_prepare_t1 (a INT, CONSTRAINT pk PRIMARY KEY CLUSTERED (a ASC)) WITH
CREATE TABLE babel_tableoptions_vu_prepare_t1 (a INT, CONSTRAINT pk_99 PRIMARY KEY CLUSTERED (a ASC)) WITH
(PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
Expand Down
6 changes: 3 additions & 3 deletions test/JDBC/expected/BABEL-UNSUPPORTED.out
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ INSERT INTO t_unsupported_cac1 VALUES (0, 0);
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: new row for relation "t_unsupported_cac1" violates check constraint "chk1t_unsupported_cac1848ea8bb1121ee393ad72ae0d412d8d2")~~
~~ERROR (Message: new row for relation "t_unsupported_cac1" violates check constraint "chk1")~~

DROP TABLE t_unsupported_cac1
GO
Expand All @@ -1680,7 +1680,7 @@ ALTER TABLE t_unsupported_cec1 ADD constraint chk1 check (a > 0)
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: check constraint "chk1t_unsupported_cec1848ea8bb1121ee393ad72ae0d412d8d2" of relation "t_unsupported_cec1" is violated by some row)~~
~~ERROR (Message: check constraint "chk1" of relation "t_unsupported_cec1" is violated by some row)~~

ALTER TABLE t_unsupported_cec1 CHECK constraint chk1
GO
Expand Down Expand Up @@ -1709,7 +1709,7 @@ ALTER TABLE t_unsupported_cec1 ADD constraint chk1 check (a > 0)
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: check constraint "chk1t_unsupported_cec1848ea8bb1121ee393ad72ae0d412d8d2" of relation "t_unsupported_cec1" is violated by some row)~~
~~ERROR (Message: check constraint "chk1" of relation "t_unsupported_cec1" is violated by some row)~~

ALTER TABLE t_unsupported_cec1 CHECK constraint chk1
GO
Expand Down
Loading

0 comments on commit d89aba9

Please sign in to comment.