@@ -241,7 +241,6 @@ func applyTriggers(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope,
241
241
return nil , transform .SameTree , err
242
242
}
243
243
244
- // triggerTable = getTableName(ct)
245
244
var triggerTable string
246
245
switch t := ct .Table .(type ) {
247
246
case * plan.ResolvedTable :
@@ -451,39 +450,6 @@ func getUpdateJoinSource(n sql.Node) *plan.UpdateSource {
451
450
return nil
452
451
}
453
452
454
- // Determines if a GetField expression references the triggered table in an UpdateJoin
455
- func isUpdateJoinTriggerField (getField * expression.GetField , updateJoin * plan.UpdateJoin , trigger * plan.CreateTrigger ) bool {
456
- updateTargets := updateJoin .UpdateTargets
457
- if updateTarget , isUpdateTarget := updateTargets [getField .Table ()]; isUpdateTarget {
458
- if getTableName (updateTarget ) == getTableName (trigger .Table ) {
459
- return true
460
- }
461
- }
462
- return false
463
- }
464
-
465
- // Returns the projection from an UpdateJoin with the non-triggered tables masked. This is to prevent conflicts if two
466
- // joined tables have columns with the same name
467
- func getMaskedUpdateJoinProject (updateJoin * plan.UpdateJoin , trigger * plan.CreateTrigger ) * plan.Project {
468
- if updateSrc , isUpdateSrc := updateJoin .Child .(* plan.UpdateSource ); isUpdateSrc {
469
- // get project parent
470
- if project , isProject := updateSrc .Child .(* plan.Project ); isProject {
471
- projections := project .Projections
472
- maskedProjections := make ([]sql.Expression , len (projections ))
473
- for i , projection := range projections {
474
- maskedProjections [i ] = projection
475
- if gf , isGf := projection .(* expression.GetField ); isGf {
476
- if ! isUpdateJoinTriggerField (gf , updateJoin , trigger ) {
477
- maskedProjections [i ] = gf .WithName ("" )
478
- }
479
- }
480
- }
481
- return plan .NewProject (maskedProjections , project .Child )
482
- }
483
- }
484
- panic ("UpdateJoin node is not correctly structured" )
485
- }
486
-
487
453
// getTriggerLogic analyzes and returns the Node representing the trigger body for the trigger given, applied to the
488
454
// plan node given, which must be an insert, update, or delete.
489
455
func getTriggerLogic (ctx * sql.Context , a * Analyzer , n sql.Node , scope * plan.Scope , trigger * plan.CreateTrigger , qFlags * sql.QueryFlags ) (sql.Node , error ) {
@@ -492,43 +458,41 @@ func getTriggerLogic(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scop
492
458
// fabricate one with the right properties (its child schema matches the table schema, with the right aliased name)
493
459
var triggerLogic sql.Node
494
460
var err error
495
- var scopeNode * plan.Project
496
461
qFlags = nil
497
462
498
463
switch trigger .TriggerEvent {
499
464
case sqlparser .InsertStr :
500
- scopeNode = plan .NewProject (
465
+ scopeNode : = plan .NewProject (
501
466
[]sql.Expression {expression .NewStar ()},
502
467
plan .NewTableAlias ("new" , trigger .Table ),
503
468
)
504
469
s := (* plan .Scope )(nil ).NewScope (scopeNode ).WithMemos (scope .Memo (n ).MemoNodes ()).WithProcedureCache (scope .ProcedureCache ())
505
470
triggerLogic , _ , err = a .analyzeWithSelector (ctx , trigger .Body , s , SelectAllBatches , DefaultRuleSelector , qFlags )
506
471
case sqlparser .UpdateStr :
507
- if updateJoin , isUpdateJoin := n .(* plan.Update ).Child .(* plan.UpdateJoin ); isUpdateJoin {
508
- masked := getMaskedUpdateJoinProject (updateJoin , trigger )
509
- // The scopeNode for an UpdateJoin should contain every node in the updateSource as new and old but should
510
- // have placeholder expressions for non-triggered tables.
472
+ var scopeNode * plan.Project
473
+ if updateSrc := getUpdateJoinSource (n ); updateSrc == nil {
511
474
scopeNode = plan .NewProject (
512
475
[]sql.Expression {expression .NewStar ()},
513
476
plan .NewCrossJoin (
514
- plan .NewSubqueryAlias ("old" , "" , masked ),
515
- plan .NewSubqueryAlias ("new" , "" , masked ),
477
+ plan .NewTableAlias ("old" , trigger . Table ),
478
+ plan .NewTableAlias ("new" , trigger . Table ),
516
479
),
517
480
)
518
481
} else {
482
+ // The scopeNode for an UpdateJoin should contain every node in the updateSource as new and old.
519
483
scopeNode = plan .NewProject (
520
484
[]sql.Expression {expression .NewStar ()},
521
485
plan .NewCrossJoin (
522
- plan .NewTableAlias ("old" , trigger . Table ),
523
- plan .NewTableAlias ("new" , trigger . Table ),
486
+ plan .NewSubqueryAlias ("old" , "" , updateSrc . Child ),
487
+ plan .NewSubqueryAlias ("new" , "" , updateSrc . Child ),
524
488
),
525
489
)
526
490
}
527
491
// Triggers are wrapped in prepend nodes, which means that the parent scope is included
528
492
s := (* plan .Scope )(nil ).NewScope (scopeNode ).WithMemos (scope .Memo (n ).MemoNodes ()).WithProcedureCache (scope .ProcedureCache ())
529
493
triggerLogic , _ , err = a .analyzeWithSelector (ctx , trigger .Body , s , SelectAllBatches , DefaultRuleSelector , qFlags )
530
494
case sqlparser .DeleteStr :
531
- scopeNode = plan .NewProject (
495
+ scopeNode : = plan .NewProject (
532
496
[]sql.Expression {expression .NewStar ()},
533
497
plan .NewTableAlias ("old" , trigger .Table ),
534
498
)
0 commit comments