Skip to content

Commit f9c30b3

Browse files
authored
fix stored procedures in triggers (#2870)
1 parent ca52c5d commit f9c30b3

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

enginetest/queries/trigger_queries.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3421,6 +3421,135 @@ end;
34213421
},
34223422
},
34233423
},
3424+
3425+
{
3426+
Name: "insert trigger with stored procedure with deletes",
3427+
SetUpScript: []string{
3428+
"create table t (i int);",
3429+
"create table t1 (j int);",
3430+
"insert into t1 values (1);",
3431+
"create table t2 (k int);",
3432+
"insert into t2 values (1);",
3433+
"create table t3 (l int);",
3434+
"insert into t3 values (1);",
3435+
"create table t4 (m int);",
3436+
`
3437+
create procedure proc(x int)
3438+
begin
3439+
delete from t2 where k = (select j from t1 where j = x);
3440+
update t3 set l = 10 where l = x;
3441+
insert into t4 values (x);
3442+
end;
3443+
`,
3444+
`
3445+
create trigger trig before insert on t
3446+
for each row
3447+
begin
3448+
call proc(new.i);
3449+
end;
3450+
`,
3451+
},
3452+
Assertions: []ScriptTestAssertion{
3453+
{
3454+
Query: "insert into t values (1);",
3455+
Expected: []sql.Row{
3456+
{types.NewOkResult(1)},
3457+
},
3458+
},
3459+
{
3460+
Query: "select * from t;",
3461+
Expected: []sql.Row{
3462+
{1},
3463+
},
3464+
},
3465+
{
3466+
Query: "select * from t1;",
3467+
Expected: []sql.Row{
3468+
{1},
3469+
},
3470+
},
3471+
{
3472+
Query: "select * from t2;",
3473+
Expected: []sql.Row{},
3474+
},
3475+
{
3476+
Query: "select * from t3;",
3477+
Expected: []sql.Row{
3478+
{10},
3479+
},
3480+
},
3481+
{
3482+
Query: "select * from t4;",
3483+
Expected: []sql.Row{
3484+
{1},
3485+
},
3486+
},
3487+
},
3488+
},
3489+
3490+
{
3491+
Name: "delete trigger with stored procedure with deletes",
3492+
SetUpScript: []string{
3493+
"create table t (i int);",
3494+
"insert into t values (1)",
3495+
"create table t1 (j int);",
3496+
"insert into t1 values (1);",
3497+
"create table t2 (k int);",
3498+
"insert into t2 values (1);",
3499+
"create table t3 (l int);",
3500+
"insert into t3 values (1);",
3501+
"create table t4 (m int);",
3502+
`
3503+
create procedure proc(x int)
3504+
begin
3505+
delete from t2 where k = (select j from t1 where j = x);
3506+
update t3 set l = 10 where l = x;
3507+
insert into t4 values (x);
3508+
end;
3509+
`,
3510+
`
3511+
create trigger trig before delete on t
3512+
for each row
3513+
begin
3514+
call proc(old.i);
3515+
end;
3516+
`,
3517+
},
3518+
Assertions: []ScriptTestAssertion{
3519+
{
3520+
Query: "delete from t where i = 1;",
3521+
Expected: []sql.Row{
3522+
{types.NewOkResult(1)},
3523+
},
3524+
},
3525+
{
3526+
Query: "select * from t;",
3527+
Expected: []sql.Row{},
3528+
},
3529+
{
3530+
Query: "select * from t1;",
3531+
Expected: []sql.Row{
3532+
{1},
3533+
},
3534+
},
3535+
{
3536+
Query: "select * from t2;",
3537+
Expected: []sql.Row{},
3538+
},
3539+
{
3540+
Query: "select * from t3;",
3541+
Expected: []sql.Row{
3542+
{10},
3543+
},
3544+
},
3545+
{
3546+
Query: "select * from t4;",
3547+
Expected: []sql.Row{
3548+
{1},
3549+
},
3550+
},
3551+
},
3552+
},
34243553
}
34253554

34263555
var TriggerCreateInSubroutineTests = []ScriptTest{

sql/rowexec/dml_iters.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@ func prependRowInPlanForTriggerExecution(row sql.Row) func(c transform.Context)
200200
}
201201
case *plan.ResolvedTable, *plan.IndexedTableAccess:
202202
return plan.NewPrependNode(n, row), transform.NewTree, nil
203+
case *plan.Call:
204+
newNode, same, err := transform.NodeWithCtx(n.Procedure, prependRowForTriggerExecutionSelector, prependRowInPlanForTriggerExecution(row))
205+
if err != nil {
206+
return nil, transform.SameTree, err
207+
}
208+
if same {
209+
return n, transform.SameTree, nil
210+
}
211+
return n.WithProcedure(newNode.(*plan.Procedure)), transform.NewTree, nil
203212
default:
204213
return n, transform.SameTree, nil
205214
}

0 commit comments

Comments
 (0)