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 (#2140)


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 Dec 12, 2023
1 parent 507088e commit 91ba800
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 3 deletions.
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/src/tsql_analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pltsql_update_query_result_relation(Query *qry, Relation target_rel, List *rtabl
{
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 @@ -416,3 +416,4 @@ cast_eliminate
TestDatatypeAggSort
babel_index_nulls_order-before-15-5
BABEL-2999
BABEL-4606
1 change: 1 addition & 0 deletions test/JDBC/upgrade/14_11/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,4 @@ cast_eliminate
TestDatatypeAggSort
babel_index_nulls_order-before-15-5
BABEL-2999
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 @@ -413,3 +413,4 @@ cast_eliminate
TestDatatypeAggSort
babel_index_nulls_order-before-15-5
BABEL-2999
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 @@ -415,3 +415,4 @@ cast_eliminate
TestDatatypeAggSort
babel_index_nulls_order-before-15-5
BABEL-2999
BABEL-4606
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/15_1/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,5 @@ default_params
cast_eliminate
TestDatatypeAggSort
babel_index_nulls_order-before-15-5
BABEL-2999
BABEL-2999
BABEL-4606
1 change: 1 addition & 0 deletions test/JDBC/upgrade/15_2/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,4 @@ cast_eliminate
TestDatatypeAggSort
babel_index_nulls_order-before-15-5
BABEL-2999
BABEL-4606
1 change: 1 addition & 0 deletions test/JDBC/upgrade/15_3/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,4 @@ cast_eliminate
TestDatatypeAggSort
babel_index_nulls_order-before-15-5
BABEL-2999
BABEL-4606
2 changes: 1 addition & 1 deletion test/JDBC/upgrade/15_4/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,4 @@ cast_eliminate
TestDatatypeAggSort
babel_index_nulls_order-before-15-5
BABEL-2999

BABEL-4606
1 change: 1 addition & 0 deletions test/JDBC/upgrade/15_5/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,4 @@ cast_eliminate
TestDatatypeAggSort
babel_index_nulls_order
BABEL-2999
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 @@ -490,3 +490,4 @@ cast_eliminate
TestDatatypeAggSort
babel_index_nulls_order
BABEL-2999
BABEL-4606

0 comments on commit 91ba800

Please sign in to comment.