Skip to content

Commit

Permalink
sql/schemachanger: declarative drop function
Browse files Browse the repository at this point in the history
This commit implements DROP FUNCTION given existing
declarative schema changer infra (rules and ops).

`RemoveObjectParent` is added to handle the removal
of function signature from schema.

Release note: None
  • Loading branch information
chengxiong-ruan committed Jan 27, 2023
1 parent 468b907 commit 07ba6fb
Show file tree
Hide file tree
Showing 66 changed files with 2,166 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ pq: cannot drop sequence sq1 because other objects depend on it
exec-sql
DROP TABLE sc1.tbl1
----
pq: cannot drop relation "tbl1" because function "f1" depends on it
HINT: you can drop f1 instead.
pq: cannot drop table tbl1 because other objects depend on it

exec-sql
ALTER TABLE sc1.tbl1 RENAME TO tbl1_new
Expand Down Expand Up @@ -274,8 +273,7 @@ pq: cannot drop sequence sq1 because other objects depend on it
exec-sql
DROP TABLE sc1.tbl1
----
pq: cannot drop relation "tbl1" because function "f1" depends on it
HINT: you can drop f1 instead.
pq: cannot drop table tbl1 because other objects depend on it

exec-sql
ALTER TABLE sc1.tbl1 RENAME TO tbl1_new
Expand Down
5 changes: 5 additions & 0 deletions pkg/ccl/schemachangerccl/backup_base_generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ write *eventpb.DropDatabase to event log:
statement: DROP DATABASE ‹multi_region_test_db› CASCADE
tag: DROP DATABASE
user: root
## StatementPhase stage 1 of 1 with 12 MutationType ops
## StatementPhase stage 1 of 1 with 15 MutationType ops
delete database namespace entry {0 0 multi_region_test_db} -> 104
delete schema namespace entry {104 0 public} -> 105
delete object namespace entry {104 105 crdb_internal_region} -> 106
Expand Down Expand Up @@ -85,7 +85,7 @@ upsert descriptor #108
## PreCommitPhase stage 1 of 2 with 1 MutationType op
undo all catalog changes within txn #1
persist all catalog changes to storage
## PreCommitPhase stage 2 of 2 with 22 MutationType ops
## PreCommitPhase stage 2 of 2 with 25 MutationType ops
delete database namespace entry {0 0 multi_region_test_db} -> 104
delete schema namespace entry {104 0 public} -> 105
delete object namespace entry {104 105 crdb_internal_region} -> 106
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ write *eventpb.DropTable to event log:
tag: DROP TABLE
user: root
tableName: multi_region_test_db.public.table_regional_by_row
## StatementPhase stage 1 of 1 with 6 MutationType ops
## StatementPhase stage 1 of 1 with 7 MutationType ops
delete object namespace entry {104 105 table_regional_by_row} -> 108
upsert descriptor #106
...
Expand Down Expand Up @@ -65,7 +65,7 @@ upsert descriptor #108
## PreCommitPhase stage 1 of 2 with 1 MutationType op
undo all catalog changes within txn #1
persist all catalog changes to storage
## PreCommitPhase stage 2 of 2 with 14 MutationType ops
## PreCommitPhase stage 2 of 2 with 15 MutationType ops
delete object namespace entry {104 105 table_regional_by_row} -> 108
upsert descriptor #106
type:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ write *eventpb.DropTable to event log:
tag: DROP TABLE
user: root
tableName: multi_region_test_db.public.table_regional_by_table
## StatementPhase stage 1 of 1 with 3 MutationType ops
## StatementPhase stage 1 of 1 with 4 MutationType ops
delete object namespace entry {104 105 table_regional_by_table} -> 108
upsert descriptor #106
...
Expand Down Expand Up @@ -58,7 +58,7 @@ upsert descriptor #108
## PreCommitPhase stage 1 of 2 with 1 MutationType op
undo all catalog changes within txn #1
persist all catalog changes to storage
## PreCommitPhase stage 2 of 2 with 9 MutationType ops
## PreCommitPhase stage 2 of 2 with 10 MutationType ops
delete object namespace entry {104 105 table_regional_by_table} -> 108
upsert descriptor #106
type:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ Schema change plan for DROP DATABASE ‹multi_region_test_db› CASCADE;
│ │ ├── PUBLIC → ABSENT IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 1}
│ │ ├── PUBLIC → VALIDATED PrimaryIndex:{DescID: 108, IndexID: 1, ConstraintID: 1}
│ │ └── PUBLIC → ABSENT IndexName:{DescID: 108, Name: table_regional_by_table_pkey, IndexID: 1}
│ └── 12 Mutation operations
│ └── 15 Mutation operations
│ ├── MarkDescriptorAsDropped {"DescriptorID":104}
│ ├── MarkDescriptorAsDropped {"DescriptorID":105}
│ ├── RemoveSchemaParent {"Parent":{"ParentDatabaseID":104,"SchemaID":105}}
│ ├── MarkDescriptorAsDropped {"DescriptorID":106}
│ ├── RemoveObjectParent {"ObjectID":106,"ParentSchemaID":105}
│ ├── MarkDescriptorAsDropped {"DescriptorID":107}
│ ├── RemoveObjectParent {"ObjectID":107,"ParentSchemaID":105}
│ ├── MarkDescriptorAsDropped {"DescriptorID":108}
│ ├── RemoveObjectParent {"ObjectID":108,"ParentSchemaID":105}
│ ├── RemoveBackReferenceInTypes {"BackReferencedDescriptorID":108}
│ ├── DrainDescriptorName {"Namespace":{"DescriptorID":104,"Name":"multi_region_tes..."}}
│ ├── DrainDescriptorName {"Namespace":{"DatabaseID":104,"DescriptorID":105,"Name":"public"}}
Expand Down Expand Up @@ -187,14 +190,17 @@ Schema change plan for DROP DATABASE ‹multi_region_test_db› CASCADE;
│ │ ├── PUBLIC → ABSENT IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 1}
│ │ ├── PUBLIC → ABSENT PrimaryIndex:{DescID: 108, IndexID: 1, ConstraintID: 1}
│ │ └── PUBLIC → ABSENT IndexName:{DescID: 108, Name: table_regional_by_table_pkey, IndexID: 1}
│ └── 22 Mutation operations
│ └── 25 Mutation operations
│ ├── MarkDescriptorAsDropped {"DescriptorID":104}
│ ├── RemoveDatabaseRoleSettings {"DatabaseID":104}
│ ├── MarkDescriptorAsDropped {"DescriptorID":105}
│ ├── RemoveSchemaParent {"Parent":{"ParentDatabaseID":104,"SchemaID":105}}
│ ├── MarkDescriptorAsDropped {"DescriptorID":106}
│ ├── RemoveObjectParent {"ObjectID":106,"ParentSchemaID":105}
│ ├── MarkDescriptorAsDropped {"DescriptorID":107}
│ ├── RemoveObjectParent {"ObjectID":107,"ParentSchemaID":105}
│ ├── MarkDescriptorAsDropped {"DescriptorID":108}
│ ├── RemoveObjectParent {"ObjectID":108,"ParentSchemaID":105}
│ ├── RemoveBackReferenceInTypes {"BackReferencedDescriptorID":108}
│ ├── DrainDescriptorName {"Namespace":{"DescriptorID":104,"Name":"multi_region_tes..."}}
│ ├── DrainDescriptorName {"Namespace":{"DatabaseID":104,"DescriptorID":105,"Name":"public"}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ Schema change plan for DROP TABLE ‹multi_region_test_db›.‹public›.‹tab
│ │ ├── PUBLIC → VALIDATED PrimaryIndex:{DescID: 108, IndexID: 1, ConstraintID: 1}
│ │ ├── PUBLIC → ABSENT IndexPartitioning:{DescID: 108, IndexID: 1}
│ │ └── PUBLIC → ABSENT IndexName:{DescID: 108, Name: table_regional_by_row_pkey, IndexID: 1}
│ └── 6 Mutation operations
│ └── 7 Mutation operations
│ ├── MarkDescriptorAsDropped {"DescriptorID":108}
│ ├── RemoveObjectParent {"ObjectID":108,"ParentSchemaID":105}
│ ├── RemoveColumnDefaultExpression {"ColumnID":2,"TableID":108}
│ ├── UpdateTableBackReferencesInTypes {"BackReferencedTableID":108}
│ ├── DrainDescriptorName {"Namespace":{"DatabaseID":104,"DescriptorID":108,"Name":"table_regional_b...","SchemaID":105}}
Expand Down Expand Up @@ -106,8 +107,9 @@ Schema change plan for DROP TABLE ‹multi_region_test_db›.‹public›.‹tab
│ │ ├── PUBLIC → ABSENT PrimaryIndex:{DescID: 108, IndexID: 1, ConstraintID: 1}
│ │ ├── PUBLIC → ABSENT IndexPartitioning:{DescID: 108, IndexID: 1}
│ │ └── PUBLIC → ABSENT IndexName:{DescID: 108, Name: table_regional_by_row_pkey, IndexID: 1}
│ └── 14 Mutation operations
│ └── 15 Mutation operations
│ ├── MarkDescriptorAsDropped {"DescriptorID":108}
│ ├── RemoveObjectParent {"ObjectID":108,"ParentSchemaID":105}
│ ├── RemoveColumnDefaultExpression {"ColumnID":2,"TableID":108}
│ ├── UpdateTableBackReferencesInTypes {"BackReferencedTableID":108}
│ ├── DrainDescriptorName {"Namespace":{"DatabaseID":104,"DescriptorID":108,"Name":"table_regional_b...","SchemaID":105}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ Schema change plan for DROP TABLE ‹multi_region_test_db›.‹public›.‹tab
│ │ ├── PUBLIC → ABSENT IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 1}
│ │ ├── PUBLIC → VALIDATED PrimaryIndex:{DescID: 108, IndexID: 1, ConstraintID: 1}
│ │ └── PUBLIC → ABSENT IndexName:{DescID: 108, Name: table_regional_by_table_pkey, IndexID: 1}
│ └── 3 Mutation operations
│ └── 4 Mutation operations
│ ├── MarkDescriptorAsDropped {"DescriptorID":108}
│ ├── RemoveObjectParent {"ObjectID":108,"ParentSchemaID":105}
│ ├── RemoveBackReferenceInTypes {"BackReferencedDescriptorID":108}
│ └── DrainDescriptorName {"Namespace":{"DatabaseID":104,"DescriptorID":108,"Name":"table_regional_b...","SchemaID":105}}
├── PreCommitPhase
Expand Down Expand Up @@ -82,8 +83,9 @@ Schema change plan for DROP TABLE ‹multi_region_test_db›.‹public›.‹tab
│ │ ├── PUBLIC → ABSENT IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 1}
│ │ ├── PUBLIC → ABSENT PrimaryIndex:{DescID: 108, IndexID: 1, ConstraintID: 1}
│ │ └── PUBLIC → ABSENT IndexName:{DescID: 108, Name: table_regional_by_table_pkey, IndexID: 1}
│ └── 9 Mutation operations
│ └── 10 Mutation operations
│ ├── MarkDescriptorAsDropped {"DescriptorID":108}
│ ├── RemoveObjectParent {"ObjectID":108,"ParentSchemaID":105}
│ ├── RemoveBackReferenceInTypes {"BackReferencedDescriptorID":108}
│ ├── DrainDescriptorName {"Namespace":{"DatabaseID":104,"DescriptorID":108,"Name":"table_regional_b...","SchemaID":105}}
│ ├── MakeDeleteOnlyColumnAbsent {"ColumnID":1,"TableID":108}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ EXPLAIN (ddl, verbose) DROP DATABASE multi_region_test_db CASCADE;
│ │ └── • skip PUBLIC → ABSENT operations
│ │ rule: "skip index dependents removal ops on relation drop"
│ │
│ └── • 12 Mutation operations
│ └── • 15 Mutation operations
│ │
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 104
Expand All @@ -482,12 +482,24 @@ EXPLAIN (ddl, verbose) DROP DATABASE multi_region_test_db CASCADE;
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 106
│ │
│ ├── • RemoveObjectParent
│ │ ObjectID: 106
│ │ ParentSchemaID: 105
│ │
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 107
│ │
│ ├── • RemoveObjectParent
│ │ ObjectID: 107
│ │ ParentSchemaID: 105
│ │
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 108
│ │
│ ├── • RemoveObjectParent
│ │ ObjectID: 108
│ │ ParentSchemaID: 105
│ │
│ ├── • RemoveBackReferenceInTypes
│ │ BackReferencedDescriptorID: 108
│ │ TypeIDs:
Expand Down Expand Up @@ -1192,7 +1204,7 @@ EXPLAIN (ddl, verbose) DROP DATABASE multi_region_test_db CASCADE;
│ │ └── • skip PUBLIC → ABSENT operations
│ │ rule: "skip index dependents removal ops on relation drop"
│ │
│ └── • 22 Mutation operations
│ └── • 25 Mutation operations
│ │
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 104
Expand All @@ -1211,12 +1223,24 @@ EXPLAIN (ddl, verbose) DROP DATABASE multi_region_test_db CASCADE;
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 106
│ │
│ ├── • RemoveObjectParent
│ │ ObjectID: 106
│ │ ParentSchemaID: 105
│ │
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 107
│ │
│ ├── • RemoveObjectParent
│ │ ObjectID: 107
│ │ ParentSchemaID: 105
│ │
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 108
│ │
│ ├── • RemoveObjectParent
│ │ ObjectID: 108
│ │ ParentSchemaID: 105
│ │
│ ├── • RemoveBackReferenceInTypes
│ │ BackReferencedDescriptorID: 108
│ │ TypeIDs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,15 @@ EXPLAIN (ddl, verbose) DROP TABLE multi_region_test_db.public.table_regional_by_
│ │ └── • skip PUBLIC → ABSENT operations
│ │ rule: "skip index dependents removal ops on relation drop"
│ │
│ └── • 6 Mutation operations
│ └── • 7 Mutation operations
│ │
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 108
│ │
│ ├── • RemoveObjectParent
│ │ ObjectID: 108
│ │ ParentSchemaID: 105
│ │
│ ├── • RemoveColumnDefaultExpression
│ │ ColumnID: 2
│ │ TableID: 108
Expand Down Expand Up @@ -735,11 +739,15 @@ EXPLAIN (ddl, verbose) DROP TABLE multi_region_test_db.public.table_regional_by_
│ │ └── • skip PUBLIC → ABSENT operations
│ │ rule: "skip index dependents removal ops on relation drop"
│ │
│ └── • 14 Mutation operations
│ └── • 15 Mutation operations
│ │
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 108
│ │
│ ├── • RemoveObjectParent
│ │ ObjectID: 108
│ │ ParentSchemaID: 105
│ │
│ ├── • RemoveColumnDefaultExpression
│ │ ColumnID: 2
│ │ TableID: 108
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,15 @@ EXPLAIN (ddl, verbose) DROP TABLE multi_region_test_db.public.table_regional_by_
│ │ └── • skip PUBLIC → ABSENT operations
│ │ rule: "skip index dependents removal ops on relation drop"
│ │
│ └── • 3 Mutation operations
│ └── • 4 Mutation operations
│ │
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 108
│ │
│ ├── • RemoveObjectParent
│ │ ObjectID: 108
│ │ ParentSchemaID: 105
│ │
│ ├── • RemoveBackReferenceInTypes
│ │ BackReferencedDescriptorID: 108
│ │ TypeIDs:
Expand Down Expand Up @@ -520,11 +524,15 @@ EXPLAIN (ddl, verbose) DROP TABLE multi_region_test_db.public.table_regional_by_
│ │ └── • skip PUBLIC → ABSENT operations
│ │ rule: "skip index dependents removal ops on relation drop"
│ │
│ └── • 9 Mutation operations
│ └── • 10 Mutation operations
│ │
│ ├── • MarkDescriptorAsDropped
│ │ DescriptorID: 108
│ │
│ ├── • RemoveObjectParent
│ │ ObjectID: 108
│ │ ParentSchemaID: 105
│ │
│ ├── • RemoveBackReferenceInTypes
│ │ BackReferencedDescriptorID: 108
│ │ TypeIDs:
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/schemadesc/schema_desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func (desc *Mutable) AddFunction(name string, f descpb.SchemaDescriptor_Function
// RemoveFunction removes a UDF overload signature from the schema descriptor.
func (desc *Mutable) RemoveFunction(name string, id descpb.ID) {
if fn, ok := desc.Functions[name]; ok {
updated := fn.Overloads[:0]
var updated []descpb.SchemaDescriptor_FunctionOverload
for _, ol := range fn.Overloads {
if ol.ID != id {
updated = append(updated, ol)
Expand Down
Loading

0 comments on commit 07ba6fb

Please sign in to comment.