Skip to content

Commit 6256206

Browse files
committed
use child on no targets
1 parent bcd456b commit 6256206

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

sql/analyzer/apply_foreign_keys.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,8 @@ func applyForeignKeysToNodes(ctx *sql.Context, a *Analyzer, n sql.Node, cache *f
189189
if n.HasExplicitTargets() {
190190
return n.WithExplicitTargets(foreignKeyHandlers), transform.NewTree, nil
191191
} else {
192-
newNode, err := n.WithChildren(foreignKeyHandlers...)
193-
if err != nil {
194-
return nil, transform.SameTree, err
195-
}
196-
return newNode, transform.NewTree, nil
192+
// For simple DELETEs, update the targets array with foreign key handlers.
193+
return n.WithTargets(foreignKeyHandlers), transform.NewTree, nil
197194
}
198195
default:
199196
return n, transform.SameTree, nil

sql/plan/delete.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,21 @@ func (p *DeleteFrom) WithExplicitTargets(targets []sql.Node) *DeleteFrom {
7070
return &copy
7171
}
7272

73+
// WithTargets returns a new DeleteFrom node instance with the specified |targets| set, preserving the
74+
// hasExplicitTargets flag. This is used for simple DELETEs where targets need to be updated (e.g., with
75+
// foreign key handlers) without changing whether they were explicitly specified.
76+
func (p *DeleteFrom) WithTargets(targets []sql.Node) *DeleteFrom {
77+
copy := *p
78+
copy.targets = targets
79+
return &copy
80+
}
81+
7382
// GetDeleteTargets returns the sql.Nodes representing the tables from which rows should be deleted.
7483
func (p *DeleteFrom) GetDeleteTargets() []sql.Node {
75-
return p.targets
84+
if len(p.targets) > 0 {
85+
return p.targets
86+
}
87+
return []sql.Node{p.Child}
7688
}
7789

7890
// Schema implements the sql.Node interface.

0 commit comments

Comments
 (0)