Skip to content

Commit e410216

Browse files
authored
include new/old expressions when resolving projections in trigger scope (#2858)
1 parent a3d336d commit e410216

File tree

4 files changed

+177
-4
lines changed

4 files changed

+177
-4
lines changed

enginetest/queries/trigger_queries.go

Lines changed: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,6 +2868,7 @@ end;
28682868
},
28692869
},
28702870

2871+
// Nested Triggers
28712872
{
28722873
Name: "double nested triggers referencing multiple tables",
28732874
SetUpScript: []string{
@@ -2941,7 +2942,6 @@ for each row
29412942
},
29422943
},
29432944
},
2944-
29452945
{
29462946
Name: "triple nested delete triggers referencing multiple tables",
29472947
SetUpScript: []string{
@@ -3024,7 +3024,6 @@ for each row
30243024
},
30253025
},
30263026
},
3027-
30283027
{
30293028
Name: "triple nested insert triggers referencing multiple tables",
30303029
SetUpScript: []string{
@@ -3117,7 +3116,6 @@ for each row
31173116
},
31183117
},
31193118
},
3120-
31213119
{
31223120
Name: "triple nested update triggers referencing multiple tables",
31233121
SetUpScript: []string{
@@ -3227,6 +3225,170 @@ for each row
32273225
},
32283226
},
32293227
},
3228+
3229+
// Triggers with subqueries
3230+
{
3231+
Name: "insert trigger with subquery projections",
3232+
SetUpScript: []string{
3233+
"create table t (i int primary key, j int);",
3234+
`
3235+
create trigger trig1 before insert on t
3236+
for each row
3237+
begin
3238+
set @a = (select 10 * new.i);
3239+
set @b = (select 20 * new.j);
3240+
end;
3241+
`,
3242+
`
3243+
create trigger trig2 after insert on t
3244+
for each row
3245+
begin
3246+
set @c = (select 30 * new.i);
3247+
set @d = (select 40 * new.j);
3248+
end;
3249+
`,
3250+
},
3251+
Assertions: []ScriptTestAssertion{
3252+
{
3253+
Query: "select @a, @b, @c, @d;",
3254+
Expected: []sql.Row{
3255+
{nil, nil, nil, nil},
3256+
},
3257+
},
3258+
{
3259+
Query: "insert into t values (1, 2);",
3260+
Expected: []sql.Row{
3261+
{types.OkResult{RowsAffected: 1}},
3262+
},
3263+
},
3264+
{
3265+
Query: "select @a, @b, @c, @d;",
3266+
Expected: []sql.Row{
3267+
{10, 40, 30, 80},
3268+
},
3269+
},
3270+
{
3271+
Query: "insert into t values (1, 200);",
3272+
ExpectedErrStr: "duplicate primary key given: [1]",
3273+
},
3274+
{
3275+
Query: "select @a, @b, @c, @d;",
3276+
Expected: []sql.Row{
3277+
{10, 4000, 30, 80},
3278+
},
3279+
},
3280+
},
3281+
},
3282+
{
3283+
Name: "delete trigger with subquery projections",
3284+
SetUpScript: []string{
3285+
"create table t (i int primary key, j int);",
3286+
"insert into t values (1, 2), (3, 4);",
3287+
`
3288+
create trigger trig1 before delete on t
3289+
for each row
3290+
begin
3291+
set @a = (select 10 * old.i);
3292+
set @b = (select 20 * old.j);
3293+
end;
3294+
`,
3295+
`
3296+
create trigger trig2 after delete on t
3297+
for each row
3298+
begin
3299+
set @c = (select 30 * old.i);
3300+
set @d = (select 40 * old.j);
3301+
end;
3302+
`,
3303+
},
3304+
Assertions: []ScriptTestAssertion{
3305+
{
3306+
Query: "select @a, @b, @c, @d;",
3307+
Expected: []sql.Row{
3308+
{nil, nil, nil, nil},
3309+
},
3310+
},
3311+
{
3312+
Query: "delete from t where i = 1;",
3313+
Expected: []sql.Row{
3314+
{types.OkResult{RowsAffected: 1}},
3315+
},
3316+
},
3317+
{
3318+
Query: "select @a, @b, @c, @d;",
3319+
Expected: []sql.Row{
3320+
{10, 40, 30, 80},
3321+
},
3322+
},
3323+
{
3324+
Query: "delete from t where j = 4;",
3325+
Expected: []sql.Row{
3326+
{types.OkResult{RowsAffected: 1}},
3327+
},
3328+
},
3329+
{
3330+
Query: "select @a, @b, @c, @d;",
3331+
Expected: []sql.Row{
3332+
{30, 80, 90, 160},
3333+
},
3334+
},
3335+
},
3336+
},
3337+
{
3338+
Name: "update trigger with subquery projections",
3339+
SetUpScript: []string{
3340+
"create table t (i int primary key, j int);",
3341+
"insert into t values (1, 2), (3, 4);",
3342+
`
3343+
create trigger trig1 before update on t
3344+
for each row
3345+
begin
3346+
set @a = (select 10 * old.i + new.i);
3347+
set @b = (select 20 * old.j + new.j);
3348+
end;
3349+
`,
3350+
`
3351+
create trigger trig2 after update on t
3352+
for each row
3353+
begin
3354+
set @c = (select 30 * old.i + new.i);
3355+
set @d = (select 40 * old.j + new.j);
3356+
end;
3357+
`,
3358+
},
3359+
Assertions: []ScriptTestAssertion{
3360+
{
3361+
Query: "select @a, @b, @c, @d;",
3362+
Expected: []sql.Row{
3363+
{nil, nil, nil, nil},
3364+
},
3365+
},
3366+
{
3367+
Query: "update t set i = i * 10 where i = 1;",
3368+
Expected: []sql.Row{
3369+
{types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}},
3370+
},
3371+
},
3372+
{
3373+
Query: "select @a, @b, @c, @d;",
3374+
Expected: []sql.Row{
3375+
{20, 42, 40, 82},
3376+
},
3377+
},
3378+
{
3379+
Query: "update t set j = i * 10 where j = 4;",
3380+
Expected: []sql.Row{
3381+
{types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}},
3382+
},
3383+
},
3384+
{
3385+
Query: "select @a, @b, @c, @d;",
3386+
Expected: []sql.Row{
3387+
{33, 110, 93, 190},
3388+
},
3389+
},
3390+
},
3391+
},
32303392
}
32313393

32323394
var TriggerCreateInSubroutineTests = []ScriptTest{

sql/planbuilder/create_ddl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ func (b *Builder) buildCreateTrigger(inScope *scope, subQuery string, fullQuery
9393
triggerScope.addColumns(newScope.cols)
9494
triggerScope.addColumns(oldScope.cols)
9595

96+
triggerScope.addExpressions(newScope.exprs)
97+
triggerScope.addExpressions(oldScope.exprs)
98+
9699
bodyStr := strings.TrimSpace(fullQuery[c.SubStatementPositionStart:c.SubStatementPositionEnd])
97100
bodyScope := b.buildSubquery(triggerScope, c.TriggerSpec.Body, bodyStr, fullQuery)
98101
definer := getCurrentUserForDefiner(b.ctx, c.TriggerSpec.Definer)

sql/planbuilder/project.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ func (b *Builder) analyzeSelectList(inScope, outScope *scope, selectExprs ast.Se
7373
}
7474
if subqueryFound {
7575
outScope.refsSubquery = true
76-
7776
}
7877

7978
switch e := pe.(type) {

sql/planbuilder/scope.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,15 @@ func (s *scope) addColumns(cols []scopeColumn) {
541541
s.cols = append(s.cols, cols...)
542542
}
543543

544+
func (s *scope) addExpressions(newExprs map[string]columnId) {
545+
if s.exprs == nil {
546+
s.exprs = make(map[string]columnId)
547+
}
548+
for k, v := range newExprs {
549+
s.exprs[k] = v
550+
}
551+
}
552+
544553
// appendColumnsFromScope merges column definitions for
545554
// multi-relational expressions.
546555
func (s *scope) appendColumnsFromScope(src *scope) {

0 commit comments

Comments
 (0)