Skip to content

Commit

Permalink
Fix the issue that inserted table refered in update join will cause c…
Browse files Browse the repository at this point in the history
…rash (#2190)

Previously when we implement update join, we missed a corner case that
one of the join relations can be a named tuple store instead of RTE
relation. And it'll lead analyzer to wrongly set resultRelation for the
Query.
This fix resolved the corner case by add a if condition to exclude
RTE_NAMEDTUPLESTORE for being resultRelation.

Task: BABEL-4606
Signed-off-by: Zhibai Song <[email protected]>
  • Loading branch information
forestkeeper authored Jan 2, 2024
1 parent a680738 commit 05b7fbb
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 1 deletion.
3 changes: 2 additions & 1 deletion contrib/babelfishpg_tsql/src/tsql_analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ pltsql_update_query_result_relation(Query *qry, Relation target_rel, List *rtabl
for (int i = 0; i < list_length(rtable); i++)
{
RangeTblEntry *rte = (RangeTblEntry *) list_nth(rtable, i);
if (rte->relid == target_relid)

if (rte->relid == target_relid && rte->rtekind != RTE_NAMEDTUPLESTORE)
{
qry->resultRelation = i + 1;
return;
Expand Down
18 changes: 18 additions & 0 deletions test/JDBC/expected/BABEL-4606-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
drop trigger babel_4606_trigger
go

drop table babel_4606
go

drop trigger babel_4606_2_trigger
go

drop table babel_4606_2
go

drop trigger babel_4606_3_trigger
go

drop table babel_4606_3
go

61 changes: 61 additions & 0 deletions test/JDBC/expected/BABEL-4606-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
create table babel_4606 (a int primary key, b int)
go

insert into babel_4606 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
go
~~ROW COUNT: 6~~


create trigger babel_4606_trigger
on babel_4606
after update
AS
begin
update t
set t.b = t.b + 1
from inserted as i
join babel_4606 as t
on t.a = i.a
end
go

create table babel_4606_2 (a int primary key, b int)
go

insert into babel_4606_2 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
go
~~ROW COUNT: 6~~


create trigger babel_4606_2_trigger
on babel_4606_2
after update
AS
begin
update babel_4606_2
set babel_4606_2.b = babel_4606_2.b + 2
from inserted as i
where babel_4606_2.a = i.a
end
go

create table babel_4606_3 (a int primary key, b int)
go

insert into babel_4606_3 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
go
~~ROW COUNT: 6~~


create trigger babel_4606_3_trigger
on babel_4606_3
after update
AS
begin
update babel_4606_3
set babel_4606_3.b = babel_4606_3.b + 200
from deleted as i
where babel_4606_3.a = i.a
end
go

98 changes: 98 additions & 0 deletions test/JDBC/expected/BABEL-4606-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
select * from babel_4606
GO
~~START~~
int#!#int
1#!#7
2#!#8
3#!#9
4#!#10
5#!#11
6#!#12
~~END~~


update babel_4606 set b = 100 where a = 1;
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~


select * from babel_4606
GO
~~START~~
int#!#int
2#!#8
3#!#9
4#!#10
5#!#11
6#!#12
1#!#101
~~END~~


select * from babel_4606_2
GO
~~START~~
int#!#int
1#!#7
2#!#8
3#!#9
4#!#10
5#!#11
6#!#12
~~END~~


update babel_4606_2 set b = 100 where a = 1;
go
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~


select * from babel_4606_2
GO
~~START~~
int#!#int
2#!#8
3#!#9
4#!#10
5#!#11
6#!#12
1#!#102
~~END~~


select * from babel_4606_3
GO
~~START~~
int#!#int
1#!#7
2#!#8
3#!#9
4#!#10
5#!#11
6#!#12
~~END~~


update babel_4606_3 set b = 100 where a = 1;
go
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~


select * from babel_4606_3
GO
~~START~~
int#!#int
2#!#8
3#!#9
4#!#10
5#!#11
6#!#12
1#!#300
~~END~~

18 changes: 18 additions & 0 deletions test/JDBC/input/BABEL-4606-vu-cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
drop trigger babel_4606_trigger
go

drop table babel_4606
go

drop trigger babel_4606_2_trigger
go

drop table babel_4606_2
go

drop trigger babel_4606_3_trigger
go

drop table babel_4606_3
go

55 changes: 55 additions & 0 deletions test/JDBC/input/BABEL-4606-vu-prepare.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
create table babel_4606 (a int primary key, b int)
go

insert into babel_4606 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
go

create trigger babel_4606_trigger
on babel_4606
after update
AS
begin
update t
set t.b = t.b + 1
from inserted as i
join babel_4606 as t
on t.a = i.a
end
go

create table babel_4606_2 (a int primary key, b int)
go

insert into babel_4606_2 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
go

create trigger babel_4606_2_trigger
on babel_4606_2
after update
AS
begin
update babel_4606_2
set babel_4606_2.b = babel_4606_2.b + 2
from inserted as i
where babel_4606_2.a = i.a
end
go

create table babel_4606_3 (a int primary key, b int)
go

insert into babel_4606_3 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
go

create trigger babel_4606_3_trigger
on babel_4606_3
after update
AS
begin
update babel_4606_3
set babel_4606_3.b = babel_4606_3.b + 200
from deleted as i
where babel_4606_3.a = i.a
end
go

26 changes: 26 additions & 0 deletions test/JDBC/input/BABEL-4606-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
select * from babel_4606
GO

update babel_4606 set b = 100 where a = 1;
GO

select * from babel_4606
GO

select * from babel_4606_2
GO

update babel_4606_2 set b = 100 where a = 1;
go

select * from babel_4606_2
GO

select * from babel_4606_3
GO

update babel_4606_3 set b = 100 where a = 1;
go

select * from babel_4606_3
GO
1 change: 1 addition & 0 deletions test/JDBC/upgrade/14_10/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,4 @@ BABEL-4231
sys_asymmetric_keys
sys_certificates
sys_database_permissions
BABEL-4606
1 change: 1 addition & 0 deletions test/JDBC/upgrade/14_8/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,4 @@ getdate
BABEL_4330
AUTO_ANALYZE-before-15-5-or-14-10
BABEL_4389
BABEL-4606
1 change: 1 addition & 0 deletions test/JDBC/upgrade/14_9/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,4 @@ smalldatetimefromparts-dep
BABEL_4330
BABEL-4410
AUTO_ANALYZE-before-15-5-or-14-10
BABEL-4606
1 change: 1 addition & 0 deletions test/JDBC/upgrade/latest/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,4 @@ BABEL-4231
sys_asymmetric_keys
sys_certificates
sys_database_permissions
BABEL-4606

0 comments on commit 05b7fbb

Please sign in to comment.