Skip to content

Commit 9882720

Browse files
committed
Revert "fixed bug where column names conflict but only works when Project is the direct child of UpdateSource. will revert. committing for future reference"
This reverts commit dd846ab.
1 parent dd846ab commit 9882720

File tree

1 file changed

+9
-45
lines changed

1 file changed

+9
-45
lines changed

sql/analyzer/triggers.go

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ func applyTriggers(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope,
241241
return nil, transform.SameTree, err
242242
}
243243

244-
// triggerTable = getTableName(ct)
245244
var triggerTable string
246245
switch t := ct.Table.(type) {
247246
case *plan.ResolvedTable:
@@ -451,39 +450,6 @@ func getUpdateJoinSource(n sql.Node) *plan.UpdateSource {
451450
return nil
452451
}
453452

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-
487453
// getTriggerLogic analyzes and returns the Node representing the trigger body for the trigger given, applied to the
488454
// plan node given, which must be an insert, update, or delete.
489455
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
492458
// fabricate one with the right properties (its child schema matches the table schema, with the right aliased name)
493459
var triggerLogic sql.Node
494460
var err error
495-
var scopeNode *plan.Project
496461
qFlags = nil
497462

498463
switch trigger.TriggerEvent {
499464
case sqlparser.InsertStr:
500-
scopeNode = plan.NewProject(
465+
scopeNode := plan.NewProject(
501466
[]sql.Expression{expression.NewStar()},
502467
plan.NewTableAlias("new", trigger.Table),
503468
)
504469
s := (*plan.Scope)(nil).NewScope(scopeNode).WithMemos(scope.Memo(n).MemoNodes()).WithProcedureCache(scope.ProcedureCache())
505470
triggerLogic, _, err = a.analyzeWithSelector(ctx, trigger.Body, s, SelectAllBatches, DefaultRuleSelector, qFlags)
506471
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 {
511474
scopeNode = plan.NewProject(
512475
[]sql.Expression{expression.NewStar()},
513476
plan.NewCrossJoin(
514-
plan.NewSubqueryAlias("old", "", masked),
515-
plan.NewSubqueryAlias("new", "", masked),
477+
plan.NewTableAlias("old", trigger.Table),
478+
plan.NewTableAlias("new", trigger.Table),
516479
),
517480
)
518481
} else {
482+
// The scopeNode for an UpdateJoin should contain every node in the updateSource as new and old.
519483
scopeNode = plan.NewProject(
520484
[]sql.Expression{expression.NewStar()},
521485
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),
524488
),
525489
)
526490
}
527491
// Triggers are wrapped in prepend nodes, which means that the parent scope is included
528492
s := (*plan.Scope)(nil).NewScope(scopeNode).WithMemos(scope.Memo(n).MemoNodes()).WithProcedureCache(scope.ProcedureCache())
529493
triggerLogic, _, err = a.analyzeWithSelector(ctx, trigger.Body, s, SelectAllBatches, DefaultRuleSelector, qFlags)
530494
case sqlparser.DeleteStr:
531-
scopeNode = plan.NewProject(
495+
scopeNode := plan.NewProject(
532496
[]sql.Expression{expression.NewStar()},
533497
plan.NewTableAlias("old", trigger.Table),
534498
)

0 commit comments

Comments
 (0)