@@ -102,7 +102,7 @@ func (p *planner) DropFunction(ctx context.Context, n *tree.DropRoutine) (ret pl
102
102
103
103
func (n * dropFunctionNode ) startExec (params runParams ) error {
104
104
for _ , fnMutable := range n .toDrop {
105
- if err := params .p .dropFunctionImpl (params .ctx , fnMutable ); err != nil {
105
+ if err := params .p .dropFunctionImpl (params .ctx , fnMutable , n . dropBehavior ); err != nil {
106
106
return err
107
107
}
108
108
}
@@ -192,7 +192,9 @@ func (p *planner) canDropFunction(ctx context.Context, fnDesc catalog.FunctionDe
192
192
return nil
193
193
}
194
194
195
- func (p * planner ) dropFunctionImpl (ctx context.Context , fnMutable * funcdesc.Mutable ) error {
195
+ func (p * planner ) dropFunctionImpl (
196
+ ctx context.Context , fnMutable * funcdesc.Mutable , dropBehavior tree.DropBehavior ,
197
+ ) error {
196
198
if fnMutable .Dropped () {
197
199
return errors .Errorf ("function %q is already being dropped" , fnMutable .Name )
198
200
}
@@ -204,6 +206,23 @@ func (p *planner) dropFunctionImpl(ctx context.Context, fnMutable *funcdesc.Muta
204
206
return scerrors .ConcurrentSchemaChangeError (fnMutable )
205
207
}
206
208
209
+ // Drop dependent functions first if cascade is specified.
210
+ if dropBehavior == tree .DropCascade {
211
+ for _ , ref := range fnMutable .DependedOnBy {
212
+ depFuncMutable , err := p .Descriptors ().MutableByID (p .txn ).Function (ctx , ref .ID )
213
+ if err != nil {
214
+ return err
215
+ }
216
+ // Skip functions that are already being dropped.
217
+ if depFuncMutable .Dropped () {
218
+ continue
219
+ }
220
+ if err := p .dropFunctionImpl (ctx , depFuncMutable , dropBehavior ); err != nil {
221
+ return err
222
+ }
223
+ }
224
+ }
225
+
207
226
// Remove backreference from tables/views/sequences referenced by this UDF.
208
227
for _ , id := range fnMutable .DependsOn {
209
228
refMutable , err := p .Descriptors ().MutableByID (p .txn ).Table (ctx , id )
@@ -310,11 +329,11 @@ func (p *planner) writeDropFuncSchemaChange(ctx context.Context, funcDesc *funcd
310
329
}
311
330
312
331
func (p * planner ) removeDependentFunction (
313
- ctx context.Context , tbl * tabledesc.Mutable , fn * funcdesc.Mutable ,
332
+ ctx context.Context , tbl * tabledesc.Mutable , fn * funcdesc.Mutable , dropBehavior tree. DropBehavior ,
314
333
) error {
315
334
// In the table whose index is being removed, filter out all back-references
316
335
// that refer to the view that's being removed.
317
336
tbl .DependedOnBy = removeMatchingReferences (tbl .DependedOnBy , fn .ID )
318
337
// Then proceed to actually drop the view and log an event for it.
319
- return p .dropFunctionImpl (ctx , fn )
338
+ return p .dropFunctionImpl (ctx , fn , dropBehavior )
320
339
}
0 commit comments