Skip to content

Commit 7235f5d

Browse files
authored
Fix the issue that inserted table refered in update join will cause crash (#2190) (#2211)
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 and lead to crash later. This fix resolved the corner case by add a if condition to exclude RTE named tuple store for being resultRelation. Task: BABEL-4606 Signed-off-by: Zhibai Song <szh@amazon.com>
1 parent add6247 commit 7235f5d

File tree

9 files changed

+280
-1
lines changed

9 files changed

+280
-1
lines changed

contrib/babelfishpg_tsql/src/tsql_analyze.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ pltsql_update_query_result_relation(Query *qry, Relation target_rel, List *rtabl
124124
for (int i = 0; i < list_length(rtable); i++)
125125
{
126126
RangeTblEntry *rte = (RangeTblEntry *) list_nth(rtable, i);
127-
if (rte->relid == target_relid)
127+
128+
if (rte->relid == target_relid && rte->rtekind != RTE_NAMEDTUPLESTORE)
128129
{
129130
qry->resultRelation = i + 1;
130131
return;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
drop trigger babel_4606_trigger
2+
go
3+
4+
drop table babel_4606
5+
go
6+
7+
drop trigger babel_4606_2_trigger
8+
go
9+
10+
drop table babel_4606_2
11+
go
12+
13+
drop trigger babel_4606_3_trigger
14+
go
15+
16+
drop table babel_4606_3
17+
go
18+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
create table babel_4606 (a int primary key, b int)
2+
go
3+
4+
insert into babel_4606 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
5+
go
6+
~~ROW COUNT: 6~~
7+
8+
9+
create trigger babel_4606_trigger
10+
on babel_4606
11+
after update
12+
AS
13+
begin
14+
update t
15+
set t.b = t.b + 1
16+
from inserted as i
17+
join babel_4606 as t
18+
on t.a = i.a
19+
end
20+
go
21+
22+
create table babel_4606_2 (a int primary key, b int)
23+
go
24+
25+
insert into babel_4606_2 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
26+
go
27+
~~ROW COUNT: 6~~
28+
29+
30+
create trigger babel_4606_2_trigger
31+
on babel_4606_2
32+
after update
33+
AS
34+
begin
35+
update babel_4606_2
36+
set babel_4606_2.b = babel_4606_2.b + 2
37+
from inserted as i
38+
where babel_4606_2.a = i.a
39+
end
40+
go
41+
42+
create table babel_4606_3 (a int primary key, b int)
43+
go
44+
45+
insert into babel_4606_3 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
46+
go
47+
~~ROW COUNT: 6~~
48+
49+
50+
create trigger babel_4606_3_trigger
51+
on babel_4606_3
52+
after update
53+
AS
54+
begin
55+
update babel_4606_3
56+
set babel_4606_3.b = babel_4606_3.b + 200
57+
from deleted as i
58+
where babel_4606_3.a = i.a
59+
end
60+
go
61+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
select * from babel_4606
2+
GO
3+
~~START~~
4+
int#!#int
5+
1#!#7
6+
2#!#8
7+
3#!#9
8+
4#!#10
9+
5#!#11
10+
6#!#12
11+
~~END~~
12+
13+
14+
update babel_4606 set b = 100 where a = 1;
15+
GO
16+
~~ROW COUNT: 1~~
17+
18+
~~ROW COUNT: 1~~
19+
20+
21+
select * from babel_4606
22+
GO
23+
~~START~~
24+
int#!#int
25+
2#!#8
26+
3#!#9
27+
4#!#10
28+
5#!#11
29+
6#!#12
30+
1#!#101
31+
~~END~~
32+
33+
34+
select * from babel_4606_2
35+
GO
36+
~~START~~
37+
int#!#int
38+
1#!#7
39+
2#!#8
40+
3#!#9
41+
4#!#10
42+
5#!#11
43+
6#!#12
44+
~~END~~
45+
46+
47+
update babel_4606_2 set b = 100 where a = 1;
48+
go
49+
~~ROW COUNT: 1~~
50+
51+
~~ROW COUNT: 1~~
52+
53+
54+
select * from babel_4606_2
55+
GO
56+
~~START~~
57+
int#!#int
58+
2#!#8
59+
3#!#9
60+
4#!#10
61+
5#!#11
62+
6#!#12
63+
1#!#102
64+
~~END~~
65+
66+
67+
select * from babel_4606_3
68+
GO
69+
~~START~~
70+
int#!#int
71+
1#!#7
72+
2#!#8
73+
3#!#9
74+
4#!#10
75+
5#!#11
76+
6#!#12
77+
~~END~~
78+
79+
80+
update babel_4606_3 set b = 100 where a = 1;
81+
go
82+
~~ROW COUNT: 1~~
83+
84+
~~ROW COUNT: 1~~
85+
86+
87+
select * from babel_4606_3
88+
GO
89+
~~START~~
90+
int#!#int
91+
2#!#8
92+
3#!#9
93+
4#!#10
94+
5#!#11
95+
6#!#12
96+
1#!#300
97+
~~END~~
98+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
drop trigger babel_4606_trigger
2+
go
3+
4+
drop table babel_4606
5+
go
6+
7+
drop trigger babel_4606_2_trigger
8+
go
9+
10+
drop table babel_4606_2
11+
go
12+
13+
drop trigger babel_4606_3_trigger
14+
go
15+
16+
drop table babel_4606_3
17+
go
18+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
create table babel_4606 (a int primary key, b int)
2+
go
3+
4+
insert into babel_4606 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
5+
go
6+
7+
create trigger babel_4606_trigger
8+
on babel_4606
9+
after update
10+
AS
11+
begin
12+
update t
13+
set t.b = t.b + 1
14+
from inserted as i
15+
join babel_4606 as t
16+
on t.a = i.a
17+
end
18+
go
19+
20+
create table babel_4606_2 (a int primary key, b int)
21+
go
22+
23+
insert into babel_4606_2 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
24+
go
25+
26+
create trigger babel_4606_2_trigger
27+
on babel_4606_2
28+
after update
29+
AS
30+
begin
31+
update babel_4606_2
32+
set babel_4606_2.b = babel_4606_2.b + 2
33+
from inserted as i
34+
where babel_4606_2.a = i.a
35+
end
36+
go
37+
38+
create table babel_4606_3 (a int primary key, b int)
39+
go
40+
41+
insert into babel_4606_3 (a, b) values (1,7),(2,8),(3,9),(4,10),(5,11),(6,12)
42+
go
43+
44+
create trigger babel_4606_3_trigger
45+
on babel_4606_3
46+
after update
47+
AS
48+
begin
49+
update babel_4606_3
50+
set babel_4606_3.b = babel_4606_3.b + 200
51+
from deleted as i
52+
where babel_4606_3.a = i.a
53+
end
54+
go
55+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
select * from babel_4606
2+
GO
3+
4+
update babel_4606 set b = 100 where a = 1;
5+
GO
6+
7+
select * from babel_4606
8+
GO
9+
10+
select * from babel_4606_2
11+
GO
12+
13+
update babel_4606_2 set b = 100 where a = 1;
14+
go
15+
16+
select * from babel_4606_2
17+
GO
18+
19+
select * from babel_4606_3
20+
GO
21+
22+
update babel_4606_3 set b = 100 where a = 1;
23+
go
24+
25+
select * from babel_4606_3
26+
GO

test/JDBC/upgrade/14_8/schedule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,4 @@ BABEL-3215
406406
orderby
407407
getdate
408408
BABEL_4330
409+
BABEL-4606

test/JDBC/upgrade/latest/schedule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,4 @@ babel_varbinary_int4_div
409409
sys-sql_expression_dependencies
410410
smalldatetimefromparts-dep
411411
BABEL_4330
412+
BABEL-4606

0 commit comments

Comments
 (0)